Skip to content

Commit 04a546d

Browse files
author
Lee Fine
committed
initial-integration-tests
1 parent 1458677 commit 04a546d

File tree

3 files changed

+55
-25
lines changed

3 files changed

+55
-25
lines changed

RemoteFileIntegrationTests/BaseRFPEMTest.cs

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ public class BaseRFPEMTest : BaseTest
1717
{
1818
private static string pemCertificate = string.Empty;
1919
private static string pemKey = string.Empty;
20+
private static string b64PFXCertificate = string.Empty;
21+
22+
public enum CERT_TYPE_ENUM
23+
{
24+
PEM,
25+
PFX
26+
}
2027

2128

2229
public static void CreateStore(string fileName, bool withExtKeyFile, bool withCertificate, STORE_ENVIRONMENT_ENUM storeEnvironment)
@@ -34,9 +41,14 @@ public static void RemoveStore(string fileName, bool withExtKeyFile, STORE_ENVIR
3441
RemoveFile($"{fileName}.key", storeEnvironment);
3542
}
3643

37-
public static string CreateCertificateAndKey(string certNameString)
44+
public string GetNewCert()
3845
{
39-
if (!string.IsNullOrEmpty(pemCertificate))
46+
return b64PFXCertificate;
47+
}
48+
49+
public static string CreateCertificateAndKey(string certNameString, CERT_TYPE_ENUM certType)
50+
{
51+
if (!string.IsNullOrEmpty(certType == CERT_TYPE_ENUM.PEM ? pemCertificate : b64PFXCertificate))
4052
return string.Empty;
4153

4254
var keyGen = new RsaKeyPairGenerator();
@@ -63,22 +75,38 @@ public static string CreateCertificateAndKey(string certNameString)
6375
// Generate the certificate
6476
X509Certificate certificate = certGen.Generate(new Asn1SignatureFactory("SHA256WITHRSA", keyPair.Private));
6577

66-
// Export certificate as PEM
67-
using (var sw = new StringWriter())
78+
if (certType == CERT_TYPE_ENUM.PEM)
6879
{
69-
var pw = new PemWriter(sw);
70-
pw.WriteObject(certificate);
71-
pw.Writer.Flush();
72-
pemCertificate = sw.ToString();
73-
}
80+
// Export certificate as PEM
81+
using (var sw = new StringWriter())
82+
{
83+
var pw = new PemWriter(sw);
84+
pw.WriteObject(certificate);
85+
pw.Writer.Flush();
86+
pemCertificate = sw.ToString();
87+
}
7488

75-
// Export private key as PEM (unencrypted)
76-
using (var sw = new StringWriter())
89+
// Export private key as PEM (unencrypted)
90+
using (var sw = new StringWriter())
91+
{
92+
var pw = new PemWriter(sw);
93+
pw.WriteObject((new Pkcs8Generator(keyPair.Private)).Generate());
94+
pw.Writer.Flush();
95+
pemKey = sw.ToString();
96+
}
97+
}
98+
else
7799
{
78-
var pw = new PemWriter(sw);
79-
pw.WriteObject((new Pkcs8Generator(keyPair.Private)).Generate());
80-
pw.Writer.Flush();
81-
pemKey = sw.ToString();
100+
Pkcs12StoreBuilder builder = new Pkcs12StoreBuilder();
101+
Pkcs12Store store = builder.Build();
102+
store.SetCertificateEntry("abc", new X509CertificateEntry(certificate));
103+
store.SetKeyEntry("abc", new AsymmetricKeyEntry(keyPair.Private), new[] { new X509CertificateEntry(certificate) });
104+
105+
using (MemoryStream ms = new MemoryStream())
106+
{
107+
store.Save(ms, EnvironmentVariables.StorePassword?.ToCharArray(), new SecureRandom());
108+
b64PFXCertificate = Convert.ToBase64String(ms.ToArray());
109+
}
82110
}
83111

84112
return certificate.Thumbprint();

RemoteFileIntegrationTests/RFPEMIManagementAddTests.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ public void RFPEM_Inventory_InternalPrivateKey_WithCert_Linux_Test0004()
5151

5252
private void RunTest(TestConfig testConfig)
5353
{
54-
InventoryJobConfiguration config = BuildBaseInventoryConfig(testConfig.WithCertificate ? ExistingAlias : string.Empty);
54+
ManagementJobConfiguration config = BuildBaseInventoryConfig();
55+
config.JobCertificate.Alias = testConfig.WithCertificate ? ExistingAlias : string.Empty;
5556
config.CertificateStoreDetails.ClientMachine = EnvironmentVariables.LinuxServer;
5657
config.CertificateStoreDetails.StorePath = EnvironmentVariables.LinuxStorePath + $"{testConfig.FileName}.pem";
5758
config.CertificateStoreDetails.Properties = "{}";
@@ -64,8 +65,8 @@ private void RunTest(TestConfig testConfig)
6465

6566
Mock<SubmitInventoryUpdate> submitInventoryUpdate = new Mock<SubmitInventoryUpdate>();
6667

67-
Inventory inventory = new Inventory(secretResolver.Object);
68-
JobResult result = inventory.ProcessJob(config, submitInventoryUpdate.Object);
68+
Management management = new Management(secretResolver.Object);
69+
management.ProcessJob(config);
6970

7071
Assert.Equal(OrchestratorJobStatusJobResult.Success, result.Result);
7172

@@ -91,7 +92,7 @@ private ManagementJobConfiguration BuildBaseInventoryConfig()
9192
{
9293
ManagementJobConfiguration config = new ManagementJobConfiguration();
9394
config.JobCertificate = new ManagementJobCertificate();
94-
config.JobCertificate.Contents = ;
95+
config.JobCertificate.Contents = GetNewCert();
9596
config.Capability = "Management";
9697
config.CertificateStoreDetails = new CertificateStore();
9798
config.JobId = new Guid();
@@ -116,17 +117,18 @@ public class RFPEMManagementAddTestsFixture : IDisposable
116117
{
117118
public RFPEMManagementAddTestsFixture()
118119
{
119-
RFPEMManagementAddTests.ExistingAlias = SetUp(EnvironmentVariables.ExistingCertificateSubjectDN ?? string.Empty);
120+
RFPEMManagementAddTests.ExistingAlias = SetUp(EnvironmentVariables.ExistingCertificateSubjectDN ?? string.Empty, EnvironmentVariables.NewCertificaetSubjectDN ?? string.Empty);
120121
}
121122

122123
public void Dispose()
123124
{
124125
TearDown();
125126
}
126127

127-
private string SetUp(string certName)
128+
private string SetUp(string certName, string newCertName)
128129
{
129-
string existingAlias = BaseRFPEMTest.CreateCertificateAndKey(certName);
130+
string existingAlias = BaseRFPEMTest.CreateCertificateAndKey(certName, BaseRFPEMTest.CERT_TYPE_ENUM.PEM);
131+
string newAlias = BaseRFPEMTest.CreateCertificateAndKey(newCertName, BaseRFPEMTest.CERT_TYPE_ENUM.PFX);
130132

131133
BaseRFPEMTest.CreateStore(RFPEMManagementAddTests.TestConfigs[0].FileName, RFPEMManagementAddTests.TestConfigs[0].HasSeparatePrivateKey, RFPEMManagementAddTests.TestConfigs[0].WithCertificate, RFPEMManagementAddTests.TestConfigs[0].StoreEnvironment);
132134
BaseRFPEMTest.CreateStore(RFPEMManagementAddTests.TestConfigs[1].FileName, RFPEMManagementAddTests.TestConfigs[1].HasSeparatePrivateKey, RFPEMManagementAddTests.TestConfigs[1].WithCertificate, RFPEMManagementAddTests.TestConfigs[1].StoreEnvironment);

RemoteFileIntegrationTests/RFPEMInventoryTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,17 @@ public class RFPEMInventoryTestsFixture : IDisposable
110110
{
111111
public RFPEMInventoryTestsFixture()
112112
{
113-
SetUp();
113+
SetUp(EnvironmentVariables.ExistingCertificateSubjectDN ?? string.Empty);
114114
}
115115

116116
public void Dispose()
117117
{
118118
TearDown();
119119
}
120120

121-
private void SetUp()
121+
private void SetUp(string certName)
122122
{
123-
BaseRFPEMTest.CreateCertificateAndKey(EnvironmentVariables.ExistingCertificateSubjectDN ?? string.Empty);
123+
BaseRFPEMTest.CreateCertificateAndKey(certName, BaseRFPEMTest.CERT_TYPE_ENUM.PEM);
124124

125125
BaseRFPEMTest.CreateStore(RFPEMInventoryTests.TestConfigs[0].FileName, RFPEMInventoryTests.TestConfigs[0].HasSeparatePrivateKey, RFPEMInventoryTests.TestConfigs[0].WithCertificate, RFPEMInventoryTests.TestConfigs[0].StoreEnvironment);
126126
BaseRFPEMTest.CreateStore(RFPEMInventoryTests.TestConfigs[1].FileName, RFPEMInventoryTests.TestConfigs[1].HasSeparatePrivateKey, RFPEMInventoryTests.TestConfigs[1].WithCertificate, RFPEMInventoryTests.TestConfigs[1].StoreEnvironment);

0 commit comments

Comments
 (0)