-
Notifications
You must be signed in to change notification settings - Fork 40
Headless Tests
In Ecllipse,
- Create a new java class that extends AbstractHeadlessTest which is located in the package "src/test/java/tests/headless/".
class DemoHeadlessTest extends AbstractHeadlessTest {
-
Create a test method annotated with @Test in the above-created class.
@Test
public void testHMac() {
2.1. AbstractHeadlessTest contains a method createAndComple(mavenProjectPath) which takes maven project path as an argument and returns MavenProject object containing the classes in the specified path.
2.1.1. Create a MavenProject object by specifying the path of the maven project to be analysed using CogniCrypt.
@Test
public void testHMac() {
MavenProject mavenProject = createAndCompile(new File("../Crypto/DemoMavenProject/"));
2.2. AbstractHeadlessTest contains a method createScanner(mavenProject, rulesDir) which takes MavenProject and path to the CrySL rules directory as parameters and returns HeadlessCryptoScanner object.
@Test
public void testHMac() {
MavenProject mavenProject = createAndCompile(new File("../Crypto/DemoMavenProject/"));
HeadlessCryptoScanner scanner = createScanner(mavenProject, "../../src/test/resources/crysl/");
2.3. Above returned HeadlessCryptoScanner object contains a method "exec" which executes CongiCrypt static analysis when invoked.
2.4. Before we invoke "exec", we have to specify the expected errors that might occur on running the analysis over the specified maven project.
2.4.1 This can be done using setErrorsCount(methodSignature, errorType, errorCount) method which takes method signature, type of the error (TypeStateError, ConstraintError, e.t.c.) and count the specific error in the method.
2.4.2 All the expected errors can be specified using setErrorsCount before "exec" is called.
@Test
public void testHMac() {
MavenProject mavenProject = createAndCompile(new File("../Crypto/DemoMavenProject/"));
HeadlessCryptoScanner scanner = createScanner(mavenProject, "../../src/test/resources/crysl/");
setErrorsCount("<DemoMavenProject.CipherTest: void testCipherOne()>", ConstraintError.class, 1);
setErrorsCount("<DemoMavenProject.CipherTest: void testCipherOne()>", TypeStateError.class, 1);
setErrorsCount("<DemoMavenProject.CipherTest: void testCipherOne()>", RequiredPredicateError.class, 2);
2.5. Analysis is run when HeadlessCryptoScanner.exec() is invoked.
@Test
public void testHMac() {
MavenProject mavenProject = createAndCompile(new File("../Crypto/DemoMavenProject/"));
HeadlessCryptoScanner scanner = createScanner(mavenProject, "../../src/test/resources/crysl/");
setErrorsCount("<DemoMavenProject.CipherTest: void testCipherOne()>", ConstraintError.class, 1);
setErrorsCount("<DemoMavenProject.CipherTest: void testCipherOne()>", TypeStateError.class, 1);
setErrorsCount("<DemoMavenProject.CipherTest: void testCipherOne()>", RequiredPredicateError.class, 2);
scanner.exec();
2.6. Actual errors are verified with expected errors by calling assertErrors() method provided by AbstractHeadlessTest.
@Test
public void testHMac() {
MavenProject mavenProject = createAndCompile(new File("../Crypto/DemoMavenProject/"));
HeadlessCryptoScanner scanner = createScanner(mavenProject, "../../src/test/resources/crysl/");
setErrorsCount("<DemoMavenProject.CipherTest: void testCipherOne()>", ConstraintError.class, 1);
setErrorsCount("<DemoMavenProject.CipherTest: void testCipherOne()>", TypeStateError.class, 1);
setErrorsCount("<DemoMavenProject.CipherTest: void testCipherOne()>", RequiredPredicateError.class, 2);
scanner.exec();
assertErrors();
}
}
-
To run the headless test written, maven home path should be specified as an argument. To do the following,
3.1. Go to Run -> Run Configurations...
3.2. Select the respective test under JUnit.
3.3. Select (x)= Arguments tab shown in the right window
3.4. Under VM arguments specify the following,
-ea -Dmaven.home=<maven_home_path> -Xss100M -Xmx8g
3.5. Click Apply and Run.