Skip to content

Commit

Permalink
Fixing unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Megha Goyal <[email protected]>
  • Loading branch information
goyamegh committed Feb 18, 2024
1 parent 5160a63 commit c4e9920
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import org.junit.Assert;
import org.opensearch.securityanalytics.rules.exceptions.SigmaError;
import org.opensearch.securityanalytics.rules.exceptions.SigmaErrorList;
import org.opensearch.securityanalytics.rules.exceptions.SigmaTypeError;
import org.opensearch.securityanalytics.rules.exceptions.SigmaValueError;
import org.opensearch.securityanalytics.rules.objects.SigmaRule;
import org.opensearch.test.OpenSearchTestCase;

Expand Down Expand Up @@ -377,7 +375,7 @@ public void testConvertCompare() throws IOException, SigmaError, SigmaErrorList

public void testConvertCompareStr() throws IOException {
OSQueryBackend queryBackend = testBackend();
assertThrows(SigmaTypeError.class, () -> {
assertThrows(SigmaErrorList.class, () -> {
queryBackend.convertRule(SigmaRule.fromYaml(
" title: Test\n" +
" id: 39f919f3-980b-4e6f-a975-8af7e507ef2b\n" +
Expand Down Expand Up @@ -605,7 +603,7 @@ public void testConvertUnboundValues() throws IOException, SigmaError, SigmaErro

public void testConvertInvalidUnboundBool() throws IOException {
OSQueryBackend queryBackend = testBackend();
Exception exception = assertThrows(SigmaValueError.class, () -> {
SigmaErrorList exception = assertThrows(SigmaErrorList.class, () -> {
queryBackend.convertRule(SigmaRule.fromYaml(
" title: Test\n" +
" id: 39f919f3-980b-4e6f-a975-8af7e507ef2b\n" +
Expand All @@ -622,15 +620,15 @@ public void testConvertInvalidUnboundBool() throws IOException {
" condition: sel", false));
});

String expectedMessage = "Unexpected Values";
String actualMessage = exception.getMessage();
String expectedMessage = "Sigma rule must have a detection definitions";
String actualMessage = exception.getErrors().get(0).getMessage();

assertTrue(actualMessage.contains(expectedMessage));
}

public void testConvertInvalidCidr() throws IOException {
OSQueryBackend queryBackend = testBackend();
Exception exception = assertThrows(SigmaValueError.class, () -> {
SigmaErrorList exception = assertThrows(SigmaErrorList.class, () -> {
queryBackend.convertRule(SigmaRule.fromYaml(
" title: Test\n" +
" id: 39f919f3-980b-4e6f-a975-8af7e507ef2b\n" +
Expand All @@ -648,8 +646,8 @@ public void testConvertInvalidCidr() throws IOException {
" condition: sel", false));
});

String expectedMessage = "Unexpected Values";
String actualMessage = exception.getMessage();
String expectedMessage = "Sigma rule must have a detection definitions";
String actualMessage = exception.getErrors().get(0).getMessage();

assertTrue(actualMessage.contains(expectedMessage));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@

import org.junit.Assert;
import org.opensearch.securityanalytics.rules.condition.ConditionOR;
import org.opensearch.securityanalytics.rules.exceptions.SigmaDateError;
import org.opensearch.securityanalytics.rules.exceptions.SigmaDetectionError;
import org.opensearch.securityanalytics.rules.exceptions.SigmaError;
import org.opensearch.securityanalytics.rules.exceptions.SigmaErrorList;
import org.opensearch.securityanalytics.rules.exceptions.SigmaIdentifierError;
import org.opensearch.securityanalytics.rules.exceptions.SigmaLevelError;
import org.opensearch.securityanalytics.rules.exceptions.SigmaLogsourceError;
import org.opensearch.securityanalytics.rules.exceptions.SigmaModifierError;
import org.opensearch.securityanalytics.rules.exceptions.SigmaRegularExpressionError;
import org.opensearch.securityanalytics.rules.exceptions.SigmaStatusError;
import org.opensearch.securityanalytics.rules.exceptions.SigmaValueError;
import org.opensearch.securityanalytics.rules.modifiers.SigmaContainsModifier;
import org.opensearch.securityanalytics.rules.modifiers.SigmaEndswithModifier;
Expand All @@ -38,12 +34,12 @@
public class SigmaRuleTests extends OpenSearchTestCase {

public void testSigmaRuleBadUuid() {
Exception exception = assertThrows(SigmaIdentifierError.class, () -> {
SigmaErrorList exception = assertThrows(SigmaErrorList.class, () -> {
SigmaRule.fromDict(Collections.singletonMap("id", "no-uuid"), false);
});

String expectedMessage = "Sigma rule identifier must be an UUID";
String actualMessage = exception.getMessage();
String actualMessage = exception.getErrors().get(0).getMessage();

assertTrue(actualMessage.contains(expectedMessage));
}
Expand All @@ -52,12 +48,12 @@ public void testSigmaRuleBadLevel() {
Map<String, Object> sigmaRule = new HashMap<>();
sigmaRule.put("id", java.util.UUID.randomUUID().toString());

Exception exception = assertThrows(SigmaLevelError.class, () -> {
SigmaErrorList exception = assertThrows(SigmaErrorList.class, () -> {
SigmaRule.fromDict(sigmaRule, false);
});

String expectedMessage = "null is no valid Sigma rule level";
String actualMessage = exception.getMessage();
String actualMessage = exception.getErrors().get(0).getMessage();

assertTrue(actualMessage.contains(expectedMessage));
}
Expand All @@ -67,12 +63,12 @@ public void testSigmaRuleBadStatus() {
sigmaRule.put("id", java.util.UUID.randomUUID().toString());
sigmaRule.put("level", "critical");

Exception exception = assertThrows(SigmaStatusError.class, () -> {
SigmaErrorList exception = assertThrows(SigmaErrorList.class, () -> {
SigmaRule.fromDict(sigmaRule, false);
});

String expectedMessage = "null is no valid Sigma rule status";
String actualMessage = exception.getMessage();
String actualMessage = exception.getErrors().get(0).getMessage();

assertTrue(actualMessage.contains(expectedMessage));
}
Expand All @@ -84,7 +80,7 @@ public void testSigmaRuleBadDate() {
sigmaRule.put("status", "experimental");
sigmaRule.put("date", "15/05");

assertThrows(SigmaDateError.class, () -> {
assertThrows(SigmaErrorList.class, () -> {
SigmaRule.fromDict(sigmaRule, false);
});
}
Expand All @@ -96,12 +92,12 @@ public void testSigmaRuleNoLogSource() {
sigmaRule.put("status", "experimental");
sigmaRule.put("date", "2017/05/15");

Exception exception = assertThrows(SigmaLogsourceError.class, () -> {
SigmaErrorList exception = assertThrows(SigmaErrorList.class, () -> {
SigmaRule.fromDict(sigmaRule, false);
});

String expectedMessage = "Sigma rule must have a log source";
String actualMessage = exception.getMessage();
String actualMessage = exception.getErrors().get(0).getMessage();

assertTrue(actualMessage.contains(expectedMessage));
}
Expand All @@ -118,12 +114,12 @@ public void testSigmaRuleNoDetections() {
sigmaRule.put("logsource", logSource);


Exception exception = assertThrows(SigmaDetectionError.class, () -> {
SigmaErrorList exception = assertThrows(SigmaErrorList.class, () -> {
SigmaRule.fromDict(sigmaRule, false);
});

String expectedMessage = "Sigma rule must have a detection definitions";
String actualMessage = exception.getMessage();
String actualMessage = exception.getErrors().get(0).getMessage();

assertTrue(actualMessage.contains(expectedMessage));
}
Expand Down

0 comments on commit c4e9920

Please sign in to comment.