TPMBaseline.isEmpty() method, activated tests

Added unit test for TPMBaseline.isEmpty(). Change exception type thrown in generator class

Added unit tests to account for both an empty and a non-empty baseline object

Checkstyle changes
This commit is contained in:
Michael Tsai 2018-11-29 16:15:54 -05:00 committed by apldev3
parent 02cb30ad6d
commit e2e07a3ec2
4 changed files with 59 additions and 7 deletions

View File

@ -23,6 +23,7 @@ import java.util.Set;
public abstract class TPMBaseline extends Baseline {
private static final Logger LOGGER = LogManager.getLogger(TPMBaseline.class);
private static final String NOT_SPECIFIED = "Not Specified";
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "TPMBaselineRecords",
@ -273,4 +274,34 @@ public abstract class TPMBaseline extends Baseline {
return pcrRecords.remove(record);
}
/**
* Checks the properties of FirmwareInfo, HardwareInfo, OSInfo, and TPMInfo and the contents of
* pcrRecords to determine if this instance of TPMBaseline is empty or not.
*
* @return true if baseline has no data
*/
public boolean isEmpty() {
LOGGER.debug("Check for empty baseline");
return (firmwareInfo.getBiosReleaseDate().equals(NOT_SPECIFIED)
&& firmwareInfo.getBiosVendor().equals(NOT_SPECIFIED)
&& firmwareInfo.getBiosVersion().equals(NOT_SPECIFIED)
&& hardwareInfo.getBaseboardSerialNumber().equals(NOT_SPECIFIED)
&& hardwareInfo.getChassisSerialNumber().equals(NOT_SPECIFIED)
&& hardwareInfo.getManufacturer().equals(NOT_SPECIFIED)
&& hardwareInfo.getProductName().equals(NOT_SPECIFIED)
&& hardwareInfo.getSystemSerialNumber().equals(NOT_SPECIFIED)
&& hardwareInfo.getVersion().equals(NOT_SPECIFIED)
&& osInfo.getDistribution().equals(NOT_SPECIFIED)
&& osInfo.getDistributionRelease().equals(NOT_SPECIFIED)
&& osInfo.getOSArch().equals(NOT_SPECIFIED)
&& osInfo.getOSName().equals(NOT_SPECIFIED)
&& osInfo.getOSVersion().equals(NOT_SPECIFIED)
&& tpmInfo.getTPMMake().equals(NOT_SPECIFIED)
&& tpmInfo.getTPMVersionMajor() == 0
&& tpmInfo.getTPMVersionMinor() == 0
&& tpmInfo.getTPMVersionRevMajor() == 0
&& tpmInfo.getTPMVersionRevMinor() == 0
&& pcrRecords.isEmpty());
}
}

View File

@ -44,6 +44,7 @@ public class TPMBaselineGenerator {
= LogManager.getLogger(TPMBaselineGenerator.class);
private static final String KERNEL_UPDATE_BASELINE_NAME = "Kernel Update %s %s";
private static final String VALID_REGEX = "[0-9a-zA-Z./()_,\" -]+";
/**
@ -595,19 +596,14 @@ public class TPMBaselineGenerator {
HashMap<TPMBaselineFields, String> fieldMap = new HashMap<>();
try {
while (reader.ready()) {
if (StringUtils.isBlank(dataRow)) {
dataRow = reader.readLine();
continue;
}
while (dataRow != null && dataRow.matches(VALID_REGEX)) {
String[] dataArray = dataRow.split(",", 2); // looking for two values per row
if (dataArray.length != 2) { // could be 1, if there were no commas
final String msg = String.format(
"invalid number of fields: %d", dataArray.length);
LOGGER.error(msg);
throw new TPMBaselineGeneratorException(msg);
} else if (!dataArray[1].matches("[0-9a-zA-Z./()_,\" -]+")) {
} else if (!dataArray[1].matches(VALID_REGEX)) {
final String msg = String.format("One record contained invalid data"
+ "while parsing a CSV file for TPM Baseline '%s'.", baselineName);
LOGGER.error(msg);
@ -641,6 +637,10 @@ public class TPMBaselineGenerator {
TPMBaselineFields.toOSInfo(fieldMap, baseline.getOSInfo()));
baseline.setTPMInfo(
TPMBaselineFields.toTPMInfo(fieldMap, baseline.getTPMInfo()));
if (baseline.isEmpty()) {
throw new TPMBaselineGeneratorException("TPM baseline is empty!");
}
//Checks that PCR values are actual
} catch (NumberFormatException nfe) {
String recordInfo = "";

View File

@ -623,6 +623,24 @@ public class TPMBaselineTest extends SpringPersistenceTest {
Assert.assertEquals(baseline.getTPMInfo(), tpmInfo);
}
/**
* Verify that a baseline with valid data returns false from isEmpty().
*/
@Test
public final void testIsEmptyFalse() {
final TpmWhiteListBaseline baseline = getDefaultWhiteListBaseline();
Assert.assertFalse(baseline.isEmpty());
}
/**
* Verify that a baseline with no data returns true from isEmpty().
*/
@Test
public final void testIsEmptyTrue() {
final TpmWhiteListBaseline baseline = new TpmWhiteListBaseline();
Assert.assertTrue(baseline.isEmpty());
}
private TpmWhiteListBaseline getDefaultWhiteListBaseline() {
final int pcr0 = 0;
TpmWhiteListBaseline baseline = new TpmWhiteListBaseline("TestTpmWhiteListBaseline");

View File

@ -88,6 +88,7 @@ public class TPMBaselineGeneratorTest {
* if error encountered when retrieving measurement entries from
* input stream
*/
@Test
public final void generateBaselineFromCSVFileNullBaselineName()
throws IOException, ParseException, TPMBaselineGeneratorException {
Exception expectedEx = null;
@ -131,6 +132,7 @@ public class TPMBaselineGeneratorTest {
* @throws ParseException
* if error encountered parsing data
*/
@Test
public final void generateBaselineFromCSVFileContainsNoRecords()
throws IOException, ParseException {
Exception expectedEx = null;
@ -187,6 +189,7 @@ public class TPMBaselineGeneratorTest {
* @throws ParseException
* if error encountered parsing data
*/
@Test
public final void generateBaselineFromCSVFileContainsInvalidRecords()
throws IOException, ParseException {
Exception expectedEx = null;