Clean up unit tests

This commit is contained in:
chubtub 2020-02-27 08:44:12 -05:00
parent 094efb37df
commit 3692262055
7 changed files with 162 additions and 77 deletions

View File

@ -388,39 +388,6 @@ public class SwidTagGateway {
} }
} }
/**
* Given an input swidtag at [path] parse any PCRs in the payload into an InputStream object.
* This method will be used in a following pull request.
*
* @param path
* @return
* @throws IOException
*/
public ByteArrayInputStream parsePayload(String path) throws IOException {
JAXBElement jaxbe = unmarshallSwidTag(path);
SoftwareIdentity softwareIdentity = (SoftwareIdentity) jaxbe.getValue();
String pcrs = "";
if (!softwareIdentity.getEntityOrEvidenceOrLink().isEmpty()) {
List<Object> swidtag = softwareIdentity.getEntityOrEvidenceOrLink();
for (Object obj : swidtag) {
try {
JAXBElement element = (JAXBElement) obj;
String elementName = element.getName().getLocalPart();
if (elementName.equals(SwidTagConstants.PAYLOAD)) {
ResourceCollection rc = (ResourceCollection) element.getValue();
if (!rc.getDirectoryOrFileOrProcess().isEmpty()) {
pcrs = parsePCRs(rc.getDirectoryOrFileOrProcess());
}
}
} catch (ClassCastException e) {
System.out.println("Found a non-JAXBElement object!" + e.getMessage());
throw new IOException("Found an invalid element in the swidtag file!");
}
}
}
return new ByteArrayInputStream(pcrs.getBytes(StandardCharsets.UTF_8));
}
/** /**
* This method creates SoftwareIdentity element based on the parameters read in from * This method creates SoftwareIdentity element based on the parameters read in from
* a properties file. * a properties file.
@ -765,6 +732,39 @@ public class SwidTagGateway {
} }
} }
/**
* Given an input swidtag at [path] parse any PCRs in the payload into an InputStream object.
* This method will be used in a following pull request.
*
* @param path
* @return
* @throws IOException
*/
public ByteArrayInputStream parsePayload(String path) throws IOException {
JAXBElement jaxbe = unmarshallSwidTag(path);
SoftwareIdentity softwareIdentity = (SoftwareIdentity) jaxbe.getValue();
String pcrs = "";
if (!softwareIdentity.getEntityOrEvidenceOrLink().isEmpty()) {
List<Object> swidtag = softwareIdentity.getEntityOrEvidenceOrLink();
for (Object obj : swidtag) {
try {
JAXBElement element = (JAXBElement) obj;
String elementName = element.getName().getLocalPart();
if (elementName.equals(SwidTagConstants.PAYLOAD)) {
ResourceCollection rc = (ResourceCollection) element.getValue();
if (!rc.getDirectoryOrFileOrProcess().isEmpty()) {
pcrs = parsePCRs(rc.getDirectoryOrFileOrProcess());
}
}
} catch (ClassCastException e) {
System.out.println("Found a non-JAXBElement object!" + e.getMessage());
throw new IOException("Found an invalid element in the swidtag file!");
}
}
}
return new ByteArrayInputStream(pcrs.getBytes(StandardCharsets.UTF_8));
}
/** /**
* This method traverses a hirs.swid.xjc.Directory recursively until it finds at * This method traverses a hirs.swid.xjc.Directory recursively until it finds at
* least one hirs.swid.xjc.File. This File is expected to have an attribute of the form * least one hirs.swid.xjc.File. This File is expected to have an attribute of the form
@ -792,7 +792,7 @@ public class SwidTagGateway {
if (pcrHash.isEmpty()) { if (pcrHash.isEmpty()) {
pcrHash = "null"; pcrHash = "null";
} }
sb.append(pcr.getName() + "," + pcrHash + newline); sb.append(pcr.getName() + "," + pcrHash);
} }
} }
System.out.println(sb.toString()); System.out.println(sb.toString());
@ -800,8 +800,8 @@ public class SwidTagGateway {
} }
/** /**
* This method unmarshalls the swidtag found at [path] and validates it according to the * This method unmarshalls the swidtag found at [path] into a JAXBElement object
* schema. * and validates it according to the schema.
* *
* @param path to the input swidtag * @param path to the input swidtag
* @return the SoftwareIdentity element at the root of the swidtag * @return the SoftwareIdentity element at the root of the swidtag
@ -810,14 +810,14 @@ public class SwidTagGateway {
private JAXBElement unmarshallSwidTag(String path) throws IOException { private JAXBElement unmarshallSwidTag(String path) throws IOException {
File input = null; File input = null;
InputStream is = null; InputStream is = null;
JAXBElement jaxbe = null; JAXBElement swidtag = null;
try { try {
input = new File(path); input = new File(path);
is = SwidTagGateway.class.getClassLoader().getResourceAsStream(SwidTagConstants.SCHEMA_URL); is = SwidTagGateway.class.getClassLoader().getResourceAsStream(SwidTagConstants.SCHEMA_URL);
SchemaFactory schemaFactory = SchemaFactory.newInstance(SwidTagConstants.SCHEMA_LANGUAGE); SchemaFactory schemaFactory = SchemaFactory.newInstance(SwidTagConstants.SCHEMA_LANGUAGE);
Schema schema = schemaFactory.newSchema(new StreamSource(is)); Schema schema = schemaFactory.newSchema(new StreamSource(is));
unmarshaller.setSchema(schema); unmarshaller.setSchema(schema);
jaxbe = (JAXBElement) unmarshaller.unmarshal(input); swidtag = (JAXBElement) unmarshaller.unmarshal(input);
} catch (SAXException e) { } catch (SAXException e) {
System.out.println("Error setting schema for validation!"); System.out.println("Error setting schema for validation!");
} catch (UnmarshalException e) { } catch (UnmarshalException e) {
@ -834,8 +834,8 @@ public class SwidTagGateway {
System.out.println("Error closing input stream"); System.out.println("Error closing input stream");
} }
} }
if (jaxbe != null) { if (swidtag != null) {
return jaxbe; return swidtag;
} else { } else {
throw new IOException("Invalid swidtag file!"); throw new IOException("Invalid swidtag file!");
} }

View File

@ -106,12 +106,6 @@ public class Commander {
parse = true; parse = true;
parseFile = args[++i]; parseFile = args[++i];
break; break;
/*
case FULL_COMMAND_PREFIX + KEYSTORE_STRING:
case COMMAND_PREFIX + "k":
keystore = args[++i];
break;
*/
case FULL_COMMAND_PREFIX + SHOW_CERT_STRING: case FULL_COMMAND_PREFIX + SHOW_CERT_STRING:
showCert = true; showCert = true;
break; break;

View File

@ -15,9 +15,10 @@ import org.testng.annotations.Test;
public class TestSwidTagGateway { public class TestSwidTagGateway {
private SwidTagGateway gateway; private SwidTagGateway gateway;
private String inputFile, outputFile, hashType; private String inputFile, outputFile, hashType;
private final String DEFAULT_OUTPUT = "generated_swidTag.swidtag";
private final String DEFAULT_WITH_CERT = "generated_with_cert.swidtag";
private final String DEFAULT_NO_CERT = "generated_no_cert.swidtag";
private InputStream expectedFile; private InputStream expectedFile;
private static final String TEST_CSV_INPUT = "testCsv.swidtag";
private static final String TEST_BLANK_SWIDTAG = "generated_swidTag.swidtag";
@BeforeClass @BeforeClass
public void setUp() throws Exception { public void setUp() throws Exception {
@ -33,43 +34,56 @@ public class TestSwidTagGateway {
} }
} }
/**
* Creating a base RIM with default attributes with an X509Certificate element.
*/
@Test @Test
public void testGenerateSwidTagStringStringString() { public void testGenerateDefaultWithCert() {
outputFile = "testGenerateSwidTagStringStringString.swidtag"; gateway.setShowCert(true);
gateway.generateSwidTag(inputFile, outputFile, hashType); gateway.generateSwidTag();
expectedFile = (InputStream) TestSwidTagGateway.class.getClassLoader().getResourceAsStream(TEST_CSV_INPUT); expectedFile = (InputStream) TestSwidTagGateway.class.getClassLoader().getResourceAsStream(DEFAULT_WITH_CERT);
Assert.assertTrue(compareFileBytesToExpectedFile(outputFile)); Assert.assertTrue(compareFileBytesToExpectedFile(DEFAULT_OUTPUT));
} }
/**
* Create a base RIM with default attributes without an X509Certificate element.
*/
@Test @Test
public void testGenerateSwidTagFile() { public void testGenerateDefaultNoCert() {
outputFile = "testGenerateSwidTagFile.swidtag"; gateway.setShowCert(false);
gateway.generateSwidTag(new File(outputFile)); gateway.generateSwidTag();
expectedFile = (InputStream) TestSwidTagGateway.class.getClassLoader().getResourceAsStream(TEST_BLANK_SWIDTAG); expectedFile = (InputStream) TestSwidTagGateway.class.getClassLoader().getResourceAsStream(DEFAULT_NO_CERT);
Assert.assertTrue(compareFileBytesToExpectedFile(outputFile)); Assert.assertTrue(compareFileBytesToExpectedFile(DEFAULT_OUTPUT));
} }
/**
* Validate a base RIM with default attributes with an X509Certificate element.
*/
@Test @Test
public void testValidateSwidTag() { public void testValidateSwidTag() {
try { try {
Assert.assertTrue(gateway.validateSwidTag(TestSwidTagGateway.class.getClassLoader().getResource(TEST_BLANK_SWIDTAG).getPath())); Assert.assertTrue(gateway.validateSwidTag(TestSwidTagGateway.class.getClassLoader().getResource(DEFAULT_WITH_CERT).getPath()));
} catch (IOException e) { } catch (IOException e) {
Assert.fail("Invalid swidtag!"); Assert.fail("Invalid swidtag!");
} }
} }
/**
* Verify expected values of a File element in a Payload element.
*/
@Test @Test
public void testParsePayload() { public void testParsePayload() {
InputStream is = null; InputStream is = null;
outputFile = TestSwidTagGateway.class.getClassLoader().getResource(DEFAULT_WITH_CERT).getPath();
try { try {
is = gateway.parsePayload(outputFile); is = gateway.parsePayload(outputFile);
Scanner scanner = new Scanner(is, "UTF-8"); Scanner scanner = new Scanner(is, "UTF-8");
String test = "PCR0,18382098108101841048"; String test = "Example.com.iotBase.bin,688e293e3ccb522f6cf8a027c9ade7960f84bd0bf3a0b99812bc1fa498a2db8d";
String temp = ""; String temp = "";
while (scanner.hasNext()) { while (scanner.hasNext()) {
temp = scanner.next(); temp = scanner.next();
Assert.assertEquals(temp, test, "temp: " + temp + ", test: " + test);
} }
Assert.assertEquals(test, temp);
} catch (IOException e) { } catch (IOException e) {
Assert.fail("Error parsing test file!"); Assert.fail("Error parsing test file!");
} finally { } finally {
@ -83,6 +97,11 @@ public class TestSwidTagGateway {
} }
} }
/**
* This method compares two files by bytes to determine if they are the same or not.
* @param file to be compared to the expected value.
* @return true if they are equal, false if not.
*/
private boolean compareFileBytesToExpectedFile(String file) { private boolean compareFileBytesToExpectedFile(String file) {
FileInputStream testFile = null; FileInputStream testFile = null;
try { try {

View File

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<SoftwareIdentity xmlns="http://standards.iso.org/iso/19770/-2/2015/schema.xsd" xmlns:ns2="http://www.w3.org/2000/09/xmldsig#" corpus="false" name="TCG RIM example" patch="false" supplemental="false" tagId="hirs.swid.SwidTags.example" tagVersion="1" version="0.1" versionScheme="multipartnumeric" xml:lang="en">
<Entity name="HIRS" role="softwareCreator tagCreator"/>
<Link href="https://Example.com/support/ProductA/firmware/installfiles" rel="installationmedia"/>
<Meta xmlns:rim="https://trustedcomputinggroup.org/wp-content/uploads/TCG_RIM_Model" rim:bindingSpec="IOT RIM" rim:bindingSpecVersion="1.2" rim:platformManufacturerId="00201234" rim:platformManufacturerStr="Example.com" rim:platformModel="ProductA" rim:rimLinkHash="88f21d8e44d4271149297404df91caf207130bfa116582408abd04ede6db7f51"/>
<Payload>
<Directory name="iotBase">
<File xmlns:SHA256="http://www.w3.org/2001/04/xmlenc#sha256" SHA256:hash="688e293e3ccb522f6cf8a027c9ade7960f84bd0bf3a0b99812bc1fa498a2db8d" name="Example.com.iotBase.bin" size="15400"/>
</Directory>
</Payload>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
<Reference URI="">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
<DigestValue>e3V54WPCVKryiRHONI37GttXgePQDEYz1GGPcpity5E=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>OMPKPXsLr0wbtQuUTlGAD9W0fkqmw8XJ3nQHc/LsRpzCZWdN/xtfxe3JleLbXcUt4PItqj1uB5Eg
8iBWyBSy+WJYvsoROjLjZ1sUQ92jMdCO69uBjaIihn1HS2H/YnB4trjc92AUIdhoJZt9KF90IlJQ
zu3HTmQfeRYs/c6Ck1k3bL1jnyWoNzhBqCuPYrZtPbv9opVP0YOxM5IjRkRgkZIDgYbh1k4WXw8O
/iIMZuVJDfKQJSNCTAZsIbUatGDQc/nOihLHdI90wG8zu9amgrl1AEKzH8z864Fan5uuXolfAaak
sLJl6RPCNcp+JNCXMMZiS8bmYPQnVJc1ze0I1A==</SignatureValue>
<KeyInfo>
<X509Data>
<X509SubjectName>CN=example.RIM.signer,OU=PCClient,O=Example,ST=VA,C=US</X509SubjectName>
</X509Data>
</KeyInfo>
</Signature>
</SoftwareIdentity>

View File

@ -1,12 +0,0 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<SoftwareIdentity xmlns="http://standards.iso.org/iso/19770/-2/2015/schema.xsd" xmlns:ns2="http://www.w3.org/2000/09/xmldsig#" corpus="false" patch="false" name="HIRS SWID Tag example" supplemental="false" tagId="hirs.swid.SwidTags.example" version="0.1">
<Entity name="HIRS" regid="hirs.org" role="softwareCreator tagCreator"/>
<Link href="https://Example.com/support/ProductA/firmware/installfiles" rel="installationmedia"/>
<Meta xmlns:rim="https://trustedcomputinggroup.org/wp-content/uploads/TCG_RIM_Model" rim:componentManufacturerId="00213022" rim:platformManufacturerId="00201234" rim:bindingSpec="IOT RIM" rim:pcURILocal="/boot/tcg/manifest/swidtag" rim:componentManufacturer="BIOSVendorA" rim:rimLinkHash="88f21d8e44d4271149297404df91caf207130bfa116582408abd04ede6db7f51" rim:componentClass="Firmware" rim:platformManufacturerStr="Example.com" rim:platformModel="ProductA" rim:bindingSpecVersion="1.2"/>
<Payload xmlns:n8060="http://csrc.nist.gov/ns/swid/2015-extensions/1.0" n8060:envVarPrefix="$" n8060:pathSeparator="/" n8060:envVarSuffix="">
<Directory location="/boot/iot/" name="iotBase">
<File xmlns:SHA256="http://www.w3.org/2001/04/xmlenc#sha256" size="15400" version="01.00" name="Example.com.iotBase.bin" SHA256:hash="688e293e3ccb522f6cf8a027c9ade7960f84bd0bf3a0b99812bc1fa498a2db8d"/>
<File xmlns:SHA256="http://www.w3.org/2001/04/xmlenc#sha256" size="1024" version="01.00" name="iotExec.bin" SHA256:hash="7afb71275b8036a43d75f3bf1a4b84867de289b2edc6980890ec9748a112156e"/>
</Directory>
</Payload>
</SoftwareIdentity>

View File

@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<SoftwareIdentity xmlns="http://standards.iso.org/iso/19770/-2/2015/schema.xsd" xmlns:ns2="http://www.w3.org/2000/09/xmldsig#" corpus="false" name="TCG RIM example" patch="false" supplemental="false" tagId="hirs.swid.SwidTags.example" tagVersion="1" version="0.1" versionScheme="multipartnumeric" xml:lang="en">
<Entity name="HIRS" role="softwareCreator tagCreator"/>
<Link href="https://Example.com/support/ProductA/firmware/installfiles" rel="installationmedia"/>
<Meta xmlns:rim="https://trustedcomputinggroup.org/wp-content/uploads/TCG_RIM_Model" rim:bindingSpec="IOT RIM" rim:bindingSpecVersion="1.2" rim:platformManufacturerId="00201234" rim:platformManufacturerStr="Example.com" rim:platformModel="ProductA" rim:rimLinkHash="88f21d8e44d4271149297404df91caf207130bfa116582408abd04ede6db7f51"/>
<Payload>
<Directory name="iotBase">
<File xmlns:SHA256="http://www.w3.org/2001/04/xmlenc#sha256" SHA256:hash="688e293e3ccb522f6cf8a027c9ade7960f84bd0bf3a0b99812bc1fa498a2db8d" name="Example.com.iotBase.bin" size="15400"/>
</Directory>
</Payload>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
<Reference URI="">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
<DigestValue>e3V54WPCVKryiRHONI37GttXgePQDEYz1GGPcpity5E=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>OMPKPXsLr0wbtQuUTlGAD9W0fkqmw8XJ3nQHc/LsRpzCZWdN/xtfxe3JleLbXcUt4PItqj1uB5Eg
8iBWyBSy+WJYvsoROjLjZ1sUQ92jMdCO69uBjaIihn1HS2H/YnB4trjc92AUIdhoJZt9KF90IlJQ
zu3HTmQfeRYs/c6Ck1k3bL1jnyWoNzhBqCuPYrZtPbv9opVP0YOxM5IjRkRgkZIDgYbh1k4WXw8O
/iIMZuVJDfKQJSNCTAZsIbUatGDQc/nOihLHdI90wG8zu9amgrl1AEKzH8z864Fan5uuXolfAaak
sLJl6RPCNcp+JNCXMMZiS8bmYPQnVJc1ze0I1A==</SignatureValue>
<KeyInfo>
<X509Data>
<X509SubjectName>CN=example.RIM.signer,OU=PCClient,O=Example,ST=VA,C=US</X509Subj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2kr3UhmEy8x/6y4
KigsK8sBwmNv4N2Pu3RppeIcpjYj5NVA1hwRA4eeMgJp2u+urm3l1oo1UNX1HsSSBHp1Owc9zZLm
07Pl8T46kpIA4sroCAU=</X509Certificate>
</X509Data>
</KeyInfo>
</Signature>
</SoftwareIdentity>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<SoftwareIdentity xmlns="http://standards.iso.org/iso/19770/-2/2015/schema.xsd" xmlns:ns2="http://www.w3.org/2000/09/xmldsig#" corpus="false" patch="false" name="HIRS SWID Tag application" supplemental="false" tagId="hirs.swid.SwidTags" version="0.1"> <SoftwareIdentity xmlns="http://standards.iso.org/iso/19770/-2/2015/schema.xsd" xmlns:ns2="http://www.w3.org/2000/09/xmldsig#" corpus="false" patch="false" supplemental="false" tagVersion="0" xml:lang="en">
<Entity name="HIRS" regid="hirs.org" role="softwareCreator tagCreator"/> <Entity regid="invalid.unavailable" role=""/>
<Payload> <Payload>
<File name="PCR0" ns2:SHA256="18382098108101841048"/> <File name="PCR0" ns2:SHA256="18382098108101841048"/>
</Payload> </Payload>