-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add implementation for the length predicate
- Loading branch information
Showing
6 changed files
with
239 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
CryptoAnalysis/src/test/java/tests/error/lengthpred/Length.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package tests.error.lengthpred; | ||
|
||
public class Length { | ||
|
||
public Length() {} | ||
|
||
public void lengthArray(@SuppressWarnings("unused") byte[] arr) {} | ||
|
||
public void lengthString(@SuppressWarnings("unused") String s) {} | ||
|
||
public void lengthInteger(@SuppressWarnings("unused") int i) {} | ||
} |
99 changes: 99 additions & 0 deletions
99
CryptoAnalysis/src/test/java/tests/error/lengthpred/LengthTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package tests.error.lengthpred; | ||
|
||
import java.util.UUID; | ||
import org.apache.commons.lang3.RandomUtils; | ||
import org.junit.Test; | ||
import test.TestConstants; | ||
import test.UsagePatternTestingFramework; | ||
import test.assertions.Assertions; | ||
|
||
public class LengthTest extends UsagePatternTestingFramework { | ||
|
||
@Override | ||
protected String getRulesetPath() { | ||
return TestConstants.RULES_TEST_DIR + "predefinedPredicates"; | ||
} | ||
|
||
@Test | ||
public void positiveArrayLengthTest() { | ||
Length length = new Length(); | ||
|
||
// length[arr] == 7 | ||
byte[] arr = new byte[] {'a', 'b', 'c', 'd', 'e', 'f', 'g'}; | ||
length.lengthArray(arr); | ||
|
||
Assertions.constraintErrors(length, 0); | ||
} | ||
|
||
@Test | ||
public void negativeArrayLengthTest() { | ||
Length length = new Length(); | ||
|
||
// length[arr] == 7 | ||
byte[] arr = new byte[] {'a', 'b'}; | ||
length.lengthArray(arr); | ||
|
||
Assertions.constraintErrors(length, 1); | ||
} | ||
|
||
@Test | ||
public void positiveStringLengthTest() { | ||
Length length = new Length(); | ||
|
||
// length[s] == 5 | ||
String s = "12345"; | ||
length.lengthString(s); | ||
|
||
Assertions.constraintErrors(length, 0); | ||
} | ||
|
||
@Test | ||
public void negativeStringLengthTest() { | ||
Length length = new Length(); | ||
|
||
// length[s] == 5 | ||
String s = "1234567"; | ||
length.lengthString(s); | ||
|
||
Assertions.constraintErrors(length, 1); | ||
} | ||
|
||
@Test | ||
public void positiveIntegerLengthTest() { | ||
Length length = new Length(); | ||
|
||
// length[i] == 2 | ||
int i = 12; | ||
length.lengthInteger(i); | ||
|
||
Assertions.constraintErrors(length, 0); | ||
} | ||
|
||
@Test | ||
public void negativeIntegerLengthTest() { | ||
Length length = new Length(); | ||
|
||
// length[i] == 2 | ||
int i = 123; | ||
length.lengthInteger(i); | ||
|
||
Assertions.constraintErrors(length, 1); | ||
} | ||
|
||
@Test | ||
public void unknownLengthTest() { | ||
byte[] arr = RandomUtils.secure().randomBytes(7); | ||
Length arrLength = new Length(); | ||
arrLength.lengthArray(arr); | ||
|
||
String s = UUID.randomUUID().toString(); | ||
Length stringLength = new Length(); | ||
stringLength.lengthString(s); | ||
|
||
int i = (int) (Math.random() * 99) + 10; | ||
Length integerLength = new Length(); | ||
integerLength.lengthInteger(i); | ||
|
||
Assertions.impreciseValueExtractionErrors(3); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
CryptoAnalysis/src/test/resources/testrules/predefinedPredicates/Length.crysl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
SPEC tests.error.lengthpred.Length | ||
|
||
OBJECTS | ||
byte[] arr; | ||
java.lang.String s; | ||
int i; | ||
|
||
EVENTS | ||
Con: Length(); | ||
Arr: lengthArray(arr); | ||
Str: lengthString(s); | ||
Int: lengthInteger(i); | ||
|
||
Len := Arr | Str | Int; | ||
|
||
ORDER | ||
Con, Len* | ||
|
||
CONSTRAINTS | ||
length[arr] == 7; // corresponds to arr.length | ||
length[s] == 5; // corresponds to s.length() | ||
length[i] == 2; // corresponds to String.valueOf(i).length() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters