Skip to content

Commit 2916926

Browse files
committed
[SQLite] Readd the old PQS cast tests
These were previously removed in commit 36ede0c.
1 parent 468667b commit 2916926

5 files changed

Lines changed: 351 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package sqlancer.pqs.sqlite.cast;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import org.junit.jupiter.api.Test;
8+
9+
import sqlancer.sqlite3.ast.SQLite3Cast;
10+
import sqlancer.sqlite3.ast.SQLite3Constant;
11+
12+
public class TestCastToBlob {
13+
14+
@Test
15+
public void testNull() {
16+
SQLite3Constant nullVal = SQLite3Constant.createNullConstant();
17+
SQLite3Constant castNullVal = SQLite3Cast.castToBlob(nullVal);
18+
assertTrue(castNullVal.isNull());
19+
}
20+
21+
@Test
22+
public void testEmptyString() {
23+
SQLite3Constant emptyBinary = SQLite3Constant.createTextConstant("");
24+
SQLite3Constant castVal = SQLite3Cast.castToBlob(emptyBinary);
25+
assertArrayEquals(new byte[0], castVal.asBinary());
26+
}
27+
28+
@Test
29+
public void testString1() {
30+
assertCastStringToBlob("0x12", "x'30783132'");
31+
}
32+
33+
@Test
34+
public void testString2() {
35+
assertCastStringToBlob("123", "x'313233'");
36+
}
37+
38+
void assertCastStringToBlob(String val, String expectedBlob) {
39+
SQLite3Constant c = SQLite3Constant.createTextConstant(val);
40+
SQLite3Constant binVal = SQLite3Cast.castToBlob(c);
41+
assertEquals(binVal.getValue(), expectedBlob);
42+
}
43+
44+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package sqlancer.pqs.sqlite.cast;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
import sqlancer.sqlite3.ast.SQLite3Constant;
8+
9+
public class TestCastToBoolean {
10+
11+
@Test
12+
void nan() {
13+
SQLite3Constant text = SQLite3Constant.createTextConstant("NaN");
14+
assertEquals(text.castToBoolean().asInt(), 0);
15+
}
16+
17+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package sqlancer.pqs.sqlite.cast;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
import sqlancer.sqlite3.ast.SQLite3Cast;
8+
import sqlancer.sqlite3.ast.SQLite3Constant;
9+
10+
class TestCastToInt {
11+
12+
@Test
13+
void test1() {
14+
assertBinaryCastToInt("dbb25259", 0);
15+
}
16+
17+
@Test
18+
void test2() {
19+
assertBinaryCastToInt("d9a3", 0);
20+
}
21+
22+
@Test
23+
void test3() {
24+
assertCastStringToInt("1231231922047954197746780200000", Long.MAX_VALUE);
25+
}
26+
27+
@Test
28+
void test4() {
29+
assertCastStringToInt("1231231922047954197746780200000.5", Long.MAX_VALUE);
30+
}
31+
32+
@Test
33+
void test5() {
34+
assertCastStringToInt("-1231231922047954197746780200000.5", Long.MIN_VALUE);
35+
}
36+
37+
@Test
38+
void testSign1() {
39+
assertCastStringToInt("++123", 0);
40+
}
41+
42+
@Test
43+
void testSign2() {
44+
assertCastStringToInt("+123", 123);
45+
}
46+
47+
@Test
48+
void testSign3() {
49+
assertCastStringToInt("-123", -123);
50+
}
51+
52+
@Test
53+
void testSign4() {
54+
assertCastStringToInt("-+123", 0);
55+
}
56+
57+
@Test
58+
void testSign5() {
59+
assertCastStringToInt("+-123", 0);
60+
}
61+
62+
@Test
63+
void testInfinity1() {
64+
assertCastStringToInt("Infinity", 0);
65+
}
66+
67+
@Test
68+
void testInfinity2() {
69+
assertCastStringToInt("-Infinity", 0);
70+
}
71+
72+
@Test
73+
void testNan() {
74+
assertCastStringToInt("NaN", 0);
75+
}
76+
77+
void assertCastStringToInt(String val, long expectedLong) {
78+
SQLite3Constant c = SQLite3Constant.createTextConstant(val);
79+
SQLite3Constant intVal = SQLite3Cast.castToInt(c);
80+
assertEquals(intVal.asInt(), expectedLong);
81+
}
82+
83+
void assertBinaryCastToInt(String val, long expectedLong) {
84+
SQLite3Constant c = SQLite3Constant.createBinaryConstant(val);
85+
SQLite3Constant intVal = SQLite3Cast.castToInt(c);
86+
assertEquals(intVal.asInt(), expectedLong);
87+
}
88+
89+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package sqlancer.pqs.sqlite.cast;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import org.junit.jupiter.api.Test;
9+
10+
import sqlancer.sqlite3.SQLite3Visitor;
11+
import sqlancer.sqlite3.ast.SQLite3Cast;
12+
import sqlancer.sqlite3.ast.SQLite3Constant;
13+
import sqlancer.sqlite3.schema.SQLite3DataType;
14+
15+
class TestCastToNumeric {
16+
17+
@Test
18+
void testLong() {
19+
long numbers[] = new long[] { 0, 1, 123, Long.MAX_VALUE, Long.MIN_VALUE };
20+
for (long number : numbers) {
21+
assertEquals(castLongConstant(number), number);
22+
}
23+
}
24+
25+
class StringTestTriple {
26+
String value;
27+
SQLite3DataType type;
28+
Object expectedCastValue;
29+
30+
public StringTestTriple(String value, SQLite3DataType type, Object expectedCastValue) {
31+
this.value = value;
32+
this.type = type;
33+
this.expectedCastValue = expectedCastValue;
34+
}
35+
}
36+
37+
@Test
38+
void testString() {
39+
List<StringTestTriple> triples = new ArrayList<>();
40+
triples.add(new StringTestTriple("-3.0", SQLite3DataType.INT, -3L));
41+
triples.add(new StringTestTriple("8.2250617031974513E18", SQLite3DataType.REAL, 8.2250617031974513E18));
42+
43+
triples.add(new StringTestTriple("-2277224522334683278", SQLite3DataType.INT, -2277224522334683278L));
44+
45+
triples.add(new StringTestTriple("123a", SQLite3DataType.INT, 123L));
46+
triples.add(new StringTestTriple("", SQLite3DataType.INT, 0L));
47+
triples.add(new StringTestTriple("a", SQLite3DataType.INT, 0L));
48+
triples.add(new StringTestTriple("3", SQLite3DataType.INT, 3L));
49+
triples.add(new StringTestTriple("-3", SQLite3DataType.INT, -3L));
50+
triples.add(new StringTestTriple("0.0", SQLite3DataType.INT, 0L));
51+
triples.add(new StringTestTriple("+0", SQLite3DataType.INT, 0L));
52+
triples.add(new StringTestTriple("+9", SQLite3DataType.INT, 9L));
53+
triples.add(new StringTestTriple("++9", SQLite3DataType.INT, 0L));
54+
triples.add(new StringTestTriple("+-9", SQLite3DataType.INT, 0L));
55+
triples.add(new StringTestTriple("-1748799336", SQLite3DataType.INT, -1748799336L));
56+
triples.add(new StringTestTriple("-0", SQLite3DataType.INT, 0L));
57+
58+
triples.add(new StringTestTriple("4E ", SQLite3DataType.INT, 4L));
59+
triples.add(new StringTestTriple("3.0e+5", SQLite3DataType.INT, 300000L));
60+
triples.add(new StringTestTriple("-3.2", SQLite3DataType.REAL, -3.2d));
61+
triples.add(new StringTestTriple("10e9", SQLite3DataType.INT, 10000000000L));
62+
// triples.add(new StringTestTriple("-0.0", SQLite3DataType.REAL, 0.0d));
63+
triples.add(new StringTestTriple("9223372036854775807", SQLite3DataType.INT, 9223372036854775807L));
64+
triples.add(new StringTestTriple("4337561223119921152", SQLite3DataType.INT, 4337561223119921152L));
65+
triples.add(new StringTestTriple("7839344951195291815", SQLite3DataType.INT, 7839344951195291815L));
66+
67+
// infinities
68+
triples.add(new StringTestTriple("-Infinity", SQLite3DataType.INT, 0L)); //
69+
triples.add(new StringTestTriple("Infinity", SQLite3DataType.INT, 0L)); //
70+
triples.add(new StringTestTriple("Inf", SQLite3DataType.INT, 0L)); //
71+
triples.add(new StringTestTriple("-Inf", SQLite3DataType.INT, 0L)); //
72+
triples.add(new StringTestTriple("NaN", SQLite3DataType.INT, 0L)); //
73+
triples.add(new StringTestTriple("1e500", SQLite3DataType.REAL, Double.POSITIVE_INFINITY)); //
74+
triples.add(new StringTestTriple("-1e500", SQLite3DataType.REAL, Double.NEGATIVE_INFINITY)); //
75+
76+
for (StringTestTriple triple : triples) {
77+
SQLite3Constant castVal = SQLite3Cast.castToNumeric(SQLite3Constant.createTextConstant(triple.value));
78+
assertEquals(triple.expectedCastValue, castVal.getValue(), triple.value.toString());
79+
}
80+
}
81+
82+
@Test
83+
void testBinary() {
84+
List<StringTestTriple> triples = new ArrayList<>();
85+
triples.add(new StringTestTriple("112B3980", SQLite3DataType.INT, 0L)); // +9�
86+
triples.add(new StringTestTriple("0936", SQLite3DataType.INT, 6L)); // 6
87+
triples.add(new StringTestTriple("0C36", SQLite3DataType.INT, 6L)); // 6
88+
triples.add(new StringTestTriple("0a36", SQLite3DataType.INT, 6L)); // 6
89+
triples.add(new StringTestTriple("0b36", SQLite3DataType.INT, 6L)); // 6
90+
triples.add(new StringTestTriple("0c36", SQLite3DataType.INT, 6L)); // 6
91+
triples.add(new StringTestTriple("0d36", SQLite3DataType.INT, 6L)); // 6
92+
triples.add(new StringTestTriple("0e36", SQLite3DataType.INT, 0L)); // 6
93+
triples.add(new StringTestTriple("1a347C", SQLite3DataType.INT, 0L)); //
94+
triples.add(new StringTestTriple("1b347C", SQLite3DataType.INT, 0L)); //
95+
triples.add(new StringTestTriple("1C32", SQLite3DataType.INT, 0L)); // FS2
96+
triples.add(new StringTestTriple("1D32", SQLite3DataType.INT, 0L)); // GS2
97+
triples.add(new StringTestTriple("1e32", SQLite3DataType.INT, 0L)); // RS2
98+
triples.add(new StringTestTriple("1f32", SQLite3DataType.INT, 0L)); // RS2
99+
triples.add(new StringTestTriple("2032", SQLite3DataType.INT, 2L)); // RS2
100+
triples.add(new StringTestTriple("09013454", SQLite3DataType.INT, 0L)); // RS2
101+
triples.add(new StringTestTriple("2016347C", SQLite3DataType.INT, 0L)); //
102+
triples.add(new StringTestTriple("2017347C", SQLite3DataType.INT, 0L)); //
103+
triples.add(new StringTestTriple("2018347C", SQLite3DataType.INT, 0L)); //
104+
triples.add(new StringTestTriple("2019347C", SQLite3DataType.INT, 0L)); //
105+
106+
for (StringTestTriple triple : triples) {
107+
SQLite3Constant castVal = SQLite3Cast.castToNumeric(
108+
SQLite3Constant.createBinaryConstant(SQLite3Visitor.hexStringToByteArray(triple.value)));
109+
assertEquals(triple.expectedCastValue, castVal.getValue(), triple.value.toString());
110+
}
111+
}
112+
113+
private long castLongConstant(long constant) {
114+
return SQLite3Cast.castToNumeric(SQLite3Constant.createIntConstant(constant)).asInt();
115+
}
116+
117+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package sqlancer.pqs.sqlite.cast;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
import sqlancer.sqlite3.ast.SQLite3Cast;
8+
import sqlancer.sqlite3.ast.SQLite3Constant;
9+
10+
public class TestCastToText {
11+
12+
@Test
13+
void test0() {
14+
assertBinaryCastToText("3dca", "=�");
15+
}
16+
17+
@Test
18+
void test1() {
19+
assertBinaryCastToText("7e0fa8", "~�");
20+
}
21+
22+
@Test
23+
void test2() {
24+
assertBinaryCastToText("a4ee", "��");
25+
}
26+
27+
@Test
28+
void test3() {
29+
assertBinaryCastToText("2D8A", "-�");
30+
}
31+
32+
void assertBinaryCastToText(String val, String expected) {
33+
SQLite3Constant c = SQLite3Constant.createBinaryConstant(val);
34+
SQLite3Constant intVal = SQLite3Cast.castToText(c);
35+
assertEquals(intVal.asString(), expected);
36+
}
37+
38+
@Test
39+
void testString1() {
40+
assertRealCastToText(1562730931.0, "1562730931.0");
41+
}
42+
43+
@Test
44+
void testString2() {
45+
assertRealCastToText(1.834665208E9, "1834665208.0");
46+
}
47+
48+
@Test
49+
void testString3() {
50+
assertRealCastToText(-1.5, "-1.5");
51+
}
52+
53+
@Test
54+
void testString4() {
55+
assertRealCastToText(0.8205349286718593, "0.8205349286718593");
56+
}
57+
58+
@Test
59+
void testString5() {
60+
assertRealCastToText(-0.6792529217385632, "-0.679252921738563");
61+
}
62+
63+
@Test
64+
void testString6() {
65+
assertRealCastToText(0.6918798430590762, "0.691879843059076");
66+
}
67+
68+
@Test
69+
void testString7() {
70+
assertRealCastToText(0.021848023722833787, "0.0218480237228338");
71+
}
72+
73+
@Test
74+
void testString8() {
75+
assertRealCastToText(-1.6391052705683897E308, "-1.63910527056839e+308");
76+
}
77+
78+
void assertRealCastToText(double val, String expected) {
79+
SQLite3Constant c = SQLite3Constant.createRealConstant(val);
80+
SQLite3Constant intVal = SQLite3Cast.castToText(c);
81+
assertEquals(expected, intVal.asString());
82+
}
83+
84+
}

0 commit comments

Comments
 (0)