Skip to content

Commit 7ca7d36

Browse files
Add regex support for ExpectedError (#672)
* Add Regex support in ExpectedErrors * Add Regex support in ExpectedErrors * Add Regex support in ExpectedErrors * Add unit test cases for regex in ExpectedError * Revert "Add unit test cases for regex in ExpectedError" This reverts commit cc9fce2. * Add unit test cases for regex in ExpectedError * Removed config file from PR * Removed config file from PR * Remove unused imports * Fix PMD violation * Remove usingXXX fields in ExpectedError * Remove test cases for usingXXX fields in ExpectedError * Remove test cases for usingXXX fields in ExpectedError Co-authored-by: Manuel Rigger <rigger@nus.edu.sg>
1 parent 30f8ac5 commit 7ca7d36

File tree

2 files changed

+65
-17
lines changed

2 files changed

+65
-17
lines changed
Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package sqlancer.common.query;
22

3+
import java.util.ArrayList;
34
import java.util.Collection;
45
import java.util.HashSet;
6+
import java.util.List;
57
import java.util.Set;
8+
import java.util.regex.Pattern;
69

710
/**
811
* This class represents the errors that executing a statement might result in. For example, an INSERT statement might
@@ -12,6 +15,7 @@
1215
public class ExpectedErrors {
1316

1417
private final Set<String> errors = new HashSet<>();
18+
private final List<Pattern> regexes = new ArrayList<>();
1519

1620
public ExpectedErrors add(String error) {
1721
if (error == null) {
@@ -21,6 +25,35 @@ public ExpectedErrors add(String error) {
2125
return this;
2226
}
2327

28+
public ExpectedErrors addRegex(Pattern errorPattern) {
29+
if (errorPattern == null) {
30+
throw new IllegalArgumentException();
31+
}
32+
regexes.add(errorPattern);
33+
return this;
34+
}
35+
36+
public ExpectedErrors addAll(Collection<String> list) {
37+
errors.addAll(list);
38+
return this;
39+
}
40+
41+
public ExpectedErrors addAllRegexes(Collection<Pattern> list) {
42+
if (list == null) {
43+
throw new IllegalArgumentException();
44+
}
45+
regexes.addAll(list);
46+
return this;
47+
}
48+
49+
public static ExpectedErrors from(String... errors) {
50+
ExpectedErrors expectedErrors = new ExpectedErrors();
51+
for (String error : errors) {
52+
expectedErrors.add(error);
53+
}
54+
return expectedErrors;
55+
}
56+
2457
/**
2558
* Checks whether the error message (e.g., returned by the DBMS under test) contains any of the added error
2659
* messages.
@@ -34,25 +67,17 @@ public boolean errorIsExpected(String error) {
3467
if (error == null) {
3568
throw new IllegalArgumentException();
3669
}
37-
for (String s : errors) {
70+
for (String s : this.errors) {
3871
if (error.contains(s)) {
3972
return true;
4073
}
4174
}
42-
return false;
43-
}
44-
45-
public ExpectedErrors addAll(Collection<String> list) {
46-
errors.addAll(list);
47-
return this;
48-
}
49-
50-
public static ExpectedErrors from(String... errors) {
51-
ExpectedErrors expectedErrors = new ExpectedErrors();
52-
for (String error : errors) {
53-
expectedErrors.add(error);
75+
for (Pattern p : this.regexes) {
76+
if (p.matcher(error).find()) {
77+
return true;
78+
}
5479
}
55-
return expectedErrors;
80+
return false;
5681
}
5782

5883
}

test/sqlancer/TestExpectedErrors.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package sqlancer;
22

3+
import java.util.regex.Pattern;
4+
35
import static org.junit.jupiter.api.Assertions.assertFalse;
46
import static org.junit.jupiter.api.Assertions.assertTrue;
57

@@ -16,7 +18,7 @@ public void testEmpty() {
1618
}
1719

1820
@Test
19-
public void testSimple() {
21+
public void testStringSimple() {
2022
ExpectedErrors errors = new ExpectedErrors();
2123
errors.add("a");
2224
errors.add("b");
@@ -25,16 +27,37 @@ public void testSimple() {
2527
assertTrue(errors.errorIsExpected("b"));
2628
assertTrue(errors.errorIsExpected("c"));
2729
assertTrue(errors.errorIsExpected("aa"));
28-
2930
assertFalse(errors.errorIsExpected("d"));
31+
32+
}
33+
34+
@Test
35+
public void testRegexSimple() {
36+
ExpectedErrors errors = new ExpectedErrors();
37+
errors.addRegex(Pattern.compile("a\\d"));
38+
errors.addRegex(Pattern.compile("b\\D"));
39+
errors.add("c");
40+
assertTrue(errors.errorIsExpected("a0"));
41+
assertTrue(errors.errorIsExpected("bb"));
42+
assertTrue(errors.errorIsExpected("c"));
43+
assertFalse(errors.errorIsExpected("aa"));
44+
3045
}
3146

3247
@Test
33-
public void testRealistic() {
48+
public void testStringRealistic() {
3449
ExpectedErrors errors = new ExpectedErrors();
3550
errors.add("violated");
3651
assertTrue(errors.errorIsExpected("UNIQUE constraint was violated!"));
3752
assertTrue(errors.errorIsExpected("PRIMARY KEY constraint was violated!"));
3853
}
3954

55+
@Test
56+
public void testRegexRealistic() {
57+
ExpectedErrors errors = new ExpectedErrors();
58+
errors.addRegex(Pattern.compile(".violated."));
59+
assertTrue(errors.errorIsExpected("UNIQUE constraint was violated!"));
60+
assertTrue(errors.errorIsExpected("PRIMARY KEY constraint was violated!"));
61+
}
62+
4063
}

0 commit comments

Comments
 (0)