Skip to content

Commit 495c92d

Browse files
committed
Shared: Also take query ID into account in PathProblemSourceTestInput
1 parent ff9811b commit 495c92d

File tree

2 files changed

+69
-57
lines changed

2 files changed

+69
-57
lines changed

csharp/ql/test/TestUtilities/inline-tests/PathProblemQuery.expected

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,11 @@ testFailures
2121
| InlineTests.cs:34:18:34:25 | "Source" | Unexpected result: Source |
2222
| InlineTests.cs:35:16:35:21 | "Sink" | Unexpected result: Sink |
2323
| InlineTests.cs:36:13:36:23 | InlineTests.cs:34:18:34:25 | Unexpected result: Alert |
24+
| InlineTests.cs:58:16:58:21 | "Sink" | Unexpected result: Sink=source2 |
25+
| InlineTests.cs:58:24:58:60 | // ... | Missing result: Sink[path-problem-query]=source1 |
26+
| InlineTests.cs:64:13:64:23 | InlineTests.cs:62:18:62:25 | Unexpected result: Alert=source3 |
27+
| InlineTests.cs:64:26:64:63 | // ... | Missing result: Alert[path-problem-query]=source2 |
28+
| InlineTests.cs:72:13:72:23 | "Alert:1:0" | Unexpected result: Alert=source5 |
29+
| InlineTests.cs:72:26:72:63 | // ... | Missing result: Alert[path-problem-query]=source4 |
30+
| InlineTests.cs:79:16:79:21 | "Sink" | Unexpected result: Sink=sink1 |
31+
| InlineTests.cs:79:24:79:58 | // ... | Missing result: Sink[path-problem-query]=sink2 |

shared/util/codeql/util/test/InlineExpectationsTest.qll

Lines changed: 61 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -717,13 +717,44 @@ module TestPostProcessing {
717717
)
718718
}
719719

720+
private string getTagRegex() {
721+
exists(string sourceSinkTags |
722+
(
723+
getQueryKind() = "problem"
724+
or
725+
not exists(getSourceTag(_)) and
726+
not exists(getSinkTag(_))
727+
) and
728+
sourceSinkTags = ""
729+
or
730+
sourceSinkTags = "|" + getSourceTag(_) + "|" + getSinkTag(_)
731+
|
732+
result = "(Alert" + sourceSinkTags + ")(\\[(.*)\\])?"
733+
)
734+
}
735+
720736
/**
721737
* A configuration for matching `// $ Source=foo` comments against actual
722738
* path-problem sources.
739+
*
740+
* Whenever a source is tagged with a value, like `foo`, we will use that
741+
* to define the expected tags at the sink and the alert.
723742
*/
724743
private module PathProblemSourceTestInput implements TestSig {
725744
string getARelevantTag() { result = getSourceTag(_) }
726745

746+
bindingset[expectedTag, actualTag]
747+
predicate tagMatches(string expectedTag, string actualTag) {
748+
actualTag = expectedTag.regexpCapture(getTagRegex(), 1) and
749+
(
750+
// expected tag is annotated with a query ID
751+
getQueryId() = expectedTag.regexpCapture(getTagRegex(), 3)
752+
or
753+
// expected tag is not annotated with a query ID
754+
not exists(expectedTag.regexpCapture(getTagRegex(), 3))
755+
)
756+
}
757+
727758
bindingset[expectedValue, actualValue]
728759
predicate valueMatches(string expectedValue, string actualValue) {
729760
exists(expectedValue) and
@@ -754,28 +785,7 @@ module TestPostProcessing {
754785
bindingset[result]
755786
string getARelevantTag() { any() }
756787

757-
private string getTagRegex() {
758-
exists(string sourceSinkTags |
759-
getQueryKind() = "problem" and
760-
sourceSinkTags = ""
761-
or
762-
sourceSinkTags = "|" + getSourceTag(_) + "|" + getSinkTag(_)
763-
|
764-
result = "(Alert" + sourceSinkTags + ")(\\[(.*)\\])?"
765-
)
766-
}
767-
768-
bindingset[expectedTag, actualTag]
769-
predicate tagMatches(string expectedTag, string actualTag) {
770-
actualTag = expectedTag.regexpCapture(getTagRegex(), 1) and
771-
(
772-
// expected tag is annotated with a query ID
773-
getQueryId() = expectedTag.regexpCapture(getTagRegex(), 3)
774-
or
775-
// expected tag is not annotated with a query ID
776-
not exists(expectedTag.regexpCapture(getTagRegex(), 3))
777-
)
778-
}
788+
predicate tagMatches = PathProblemSourceTestInput::tagMatches/2;
779789

780790
bindingset[expectedTag]
781791
predicate tagIsOptional(string expectedTag) {
@@ -789,64 +799,58 @@ module TestPostProcessing {
789799
)
790800
}
791801

792-
bindingset[expectedValue, actualValue]
793-
predicate valueMatches(string expectedValue, string actualValue) {
794-
expectedValue = actualValue
795-
or
796-
actualValue = ""
797-
}
798-
799802
private predicate hasPathProblemSource = PathProblemSourceTestInput::hasPathProblemSource/5;
800803

801-
/**
802-
* Gets the expected sink value for result row `row`. This value must
803-
* match the value at the corresponding path-problem source (if it is
804-
* present).
805-
*/
806-
private string getSinkValue(int row) {
807-
exists(Input::Location location, string element, string tag, string val |
808-
hasPathProblemSource(row, location, element, tag, val) and
809-
result =
810-
PathProblemSourceTest::getAMatchingExpectation(location, element, tag, val, false)
811-
.getValue()
812-
)
813-
}
814-
815804
private predicate hasPathProblemSink(
816-
int row, Input::Location location, string element, string tag, string value
805+
int row, Input::Location location, string element, string tag
817806
) {
818807
getQueryKind() = "path-problem" and
819808
exists(string loc |
820809
queryResults("#select", row, 4, loc) and
821810
queryResults("#select", row, 5, element) and
822811
tag = getSinkTag(row) and
823812
Input2::getRelativeUrl(location) = loc
824-
|
825-
not exists(getSinkValue(row)) and value = ""
826-
or
827-
value = getSinkValue(row)
828813
)
829814
}
830815

831-
private predicate hasAlert(Input::Location location, string element, string tag, string value) {
816+
private predicate hasAlert(int row, Input::Location location, string element, string tag) {
832817
getQueryKind() = ["problem", "path-problem"] and
833-
exists(int row, string loc |
818+
exists(string loc |
834819
queryResults("#select", row, 0, loc) and
835820
queryResults("#select", row, 2, element) and
836821
tag = "Alert" and
837-
value = "" and
838822
Input2::getRelativeUrl(location) = loc and
839823
not hasPathProblemSource(row, location, _, _, _) and
840-
not hasPathProblemSink(row, location, _, _, _)
824+
not hasPathProblemSink(row, location, _, _)
825+
)
826+
}
827+
828+
/**
829+
* Gets the expected value for result row `row`, if any. This value must
830+
* match the value at the corresponding path-problem source (if it is
831+
* present).
832+
*/
833+
private string getValue(int row) {
834+
exists(Input::Location location, string element, string tag, string val |
835+
hasPathProblemSource(row, location, element, tag, val) and
836+
result =
837+
PathProblemSourceTest::getAMatchingExpectation(location, element, tag, val, false)
838+
.getValue()
841839
)
842840
}
843841

844842
predicate hasActualResult(Input::Location location, string element, string tag, string value) {
845-
hasPathProblemSource(_, location, element, tag, value)
846-
or
847-
hasPathProblemSink(_, location, element, tag, value)
848-
or
849-
hasAlert(location, element, tag, value)
843+
exists(int row |
844+
hasPathProblemSource(row, location, element, tag, _)
845+
or
846+
hasPathProblemSink(row, location, element, tag)
847+
or
848+
hasAlert(row, location, element, tag)
849+
|
850+
not exists(getValue(row)) and value = ""
851+
or
852+
value = getValue(row)
853+
)
850854
}
851855
}
852856

0 commit comments

Comments
 (0)