-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathPostgresOracleFactory.java
More file actions
117 lines (107 loc) · 5.17 KB
/
PostgresOracleFactory.java
File metadata and controls
117 lines (107 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package sqlancer.postgres;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import sqlancer.OracleFactory;
import sqlancer.common.oracle.CERTOracle;
import sqlancer.common.oracle.CompositeTestOracle;
import sqlancer.common.oracle.NoRECOracle;
import sqlancer.common.oracle.TLPWhereOracle;
import sqlancer.common.oracle.TestOracle;
import sqlancer.common.query.ExpectedErrors;
import sqlancer.common.query.SQLancerResultSet;
import sqlancer.postgres.gen.PostgresCommon;
import sqlancer.postgres.gen.PostgresExpressionGenerator;
import sqlancer.postgres.oracle.PostgresFuzzer;
import sqlancer.postgres.oracle.PostgresPivotedQuerySynthesisOracle;
import sqlancer.postgres.oracle.tlp.PostgresTLPAggregateOracle;
import sqlancer.postgres.oracle.tlp.PostgresTLPHavingOracle;
public enum PostgresOracleFactory implements OracleFactory<PostgresGlobalState> {
NOREC {
@Override
public TestOracle<PostgresGlobalState> create(PostgresGlobalState globalState) throws SQLException {
PostgresExpressionGenerator gen = new PostgresExpressionGenerator(globalState);
ExpectedErrors errors = ExpectedErrors.newErrors().with(PostgresCommon.getCommonExpressionErrors())
.with(PostgresCommon.getCommonFetchErrors())
.withRegex(PostgresCommon.getCommonExpressionRegexErrors()).build();
return new NoRECOracle<>(globalState, gen, errors);
}
},
PQS {
@Override
public TestOracle<PostgresGlobalState> create(PostgresGlobalState globalState) throws SQLException {
return new PostgresPivotedQuerySynthesisOracle(globalState);
}
@Override
public boolean requiresAllTablesToContainRows() {
return true;
}
},
WHERE {
@Override
public TestOracle<PostgresGlobalState> create(PostgresGlobalState globalState) throws SQLException {
PostgresExpressionGenerator gen = new PostgresExpressionGenerator(globalState);
ExpectedErrors expectedErrors = ExpectedErrors.newErrors().with(PostgresCommon.getCommonExpressionErrors())
.with(PostgresCommon.getCommonFetchErrors())
.withRegex(PostgresCommon.getCommonExpressionRegexErrors()).build();
return new TLPWhereOracle<>(globalState, gen, expectedErrors);
}
},
HAVING {
@Override
public TestOracle<PostgresGlobalState> create(PostgresGlobalState globalState) throws SQLException {
return new PostgresTLPHavingOracle(globalState);
}
},
QUERY_PARTITIONING {
@Override
public TestOracle<PostgresGlobalState> create(PostgresGlobalState globalState) throws Exception {
List<TestOracle<PostgresGlobalState>> oracles = new ArrayList<>();
oracles.add(WHERE.create(globalState));
oracles.add(HAVING.create(globalState));
oracles.add(new PostgresTLPAggregateOracle(globalState));
return new CompositeTestOracle<PostgresGlobalState>(oracles, globalState);
}
},
CERT {
@Override
public TestOracle<PostgresGlobalState> create(PostgresGlobalState globalState) throws SQLException {
PostgresExpressionGenerator gen = new PostgresExpressionGenerator(globalState);
ExpectedErrors errors = ExpectedErrors.newErrors().with(PostgresCommon.getCommonExpressionErrors())
.withRegex(PostgresCommon.getCommonExpressionRegexErrors())
.with(PostgresCommon.getCommonFetchErrors()).with(PostgresCommon.getCommonInsertUpdateErrors())
.with(PostgresCommon.getGroupingErrors()).with(PostgresCommon.getCommonInsertUpdateErrors())
.with(PostgresCommon.getCommonRangeExpressionErrors()).build();
CERTOracle.CheckedFunction<SQLancerResultSet, Optional<Long>> rowCountParser = (rs) -> {
String content = rs.getString(1).trim();
if (content.contains("Result") && content.contains("rows=")) {
try {
int ind = content.indexOf("rows=");
long number = Long.parseLong(content.substring(ind + 5).split(" ")[0]);
return Optional.of(number);
} catch (Exception e) {
}
}
return Optional.empty();
};
CERTOracle.CheckedFunction<SQLancerResultSet, Optional<String>> queryPlanParser = (rs) -> {
String content = rs.getString(1).trim();
String[] planPart = content.split("-> ");
String plan = planPart[planPart.length - 1];
return Optional.of(plan.split(" ")[0].trim());
};
return new CERTOracle<>(globalState, gen, errors, rowCountParser, queryPlanParser);
}
@Override
public boolean requiresAllTablesToContainRows() {
return true;
}
},
FUZZER {
@Override
public TestOracle<PostgresGlobalState> create(PostgresGlobalState globalState) throws Exception {
return new PostgresFuzzer(globalState);
}
};
}