Skip to content

Commit e19e28f

Browse files
authored
Merge pull request github#12263 from geoffw0/flowsourceinline
Swift: Convert the flow sources test to inline expectations.
2 parents 6894803 + 690b5de commit e19e28f

File tree

14 files changed

+144
-237
lines changed

14 files changed

+144
-237
lines changed

swift/ql/lib/codeql/swift/dataflow/FlowSources.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ abstract class RemoteFlowSource extends FlowSource { }
2828
/**
2929
* A data flow source of local user input that is defined through 'models as data'.
3030
*/
31-
private class ExternalLocalFlowSource extends RemoteFlowSource {
31+
private class ExternalLocalFlowSource extends LocalFlowSource {
3232
ExternalLocalFlowSource() { sourceNode(this, "local") }
3333

3434
override string getSourceType() { result = "external" }

swift/ql/test/library-tests/dataflow/flowsources/FlowSources.ql renamed to swift/ql/test/library-tests/dataflow/flowsources/FlowConfig.qll

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,3 @@ class CustomTestSourcesCsv extends SourceModelCsv {
3434
]
3535
}
3636
}
37-
38-
from RemoteFlowSource source
39-
select source, concat(source.getSourceType(), ", ")

swift/ql/test/library-tests/dataflow/flowsources/FlowSources.expected

Lines changed: 0 additions & 117 deletions
This file was deleted.

swift/ql/test/library-tests/dataflow/flowsources/FlowSourcesInline.expected

Whitespace-only changes.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import swift
2+
import TestUtilities.InlineExpectationsTest
3+
import FlowConfig
4+
5+
string describe(FlowSource source) {
6+
source instanceof RemoteFlowSource and result = "remote"
7+
or
8+
source instanceof LocalFlowSource and result = "local"
9+
}
10+
11+
class FlowSourcesTest extends InlineExpectationsTest {
12+
FlowSourcesTest() { this = "FlowSourcesTest" }
13+
14+
override string getARelevantTag() { result = "source" }
15+
16+
override predicate hasActualResult(Location location, string element, string tag, string value) {
17+
exists(FlowSource source |
18+
location = source.getLocation() and
19+
location.getFile().getBaseName() != "" and
20+
element = source.toString() and
21+
tag = "source" and
22+
value = describe(source)
23+
)
24+
}
25+
}

swift/ql/test/library-tests/dataflow/flowsources/alamofire.swift

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,15 @@ struct DataResponse<Success, Failure: Error> {
8888

8989
let result: Result<Success, Failure>
9090

91-
var value: Success? { result.success } // SOURCE
91+
var value: Success? { result.success } // $ source=remote
9292
}
9393

9494
struct DownloadResponse<Success, Failure: Error> {
9595
let fileURL: URL?
9696

9797
let result: Result<Success, Failure>
9898

99-
var value: Success? { result.success } // SOURCE
99+
var value: Success? { result.success } // $ source=remote
100100
}
101101

102102
typealias AFDataResponse<Success> = DataResponse<Success, AFError>
@@ -341,42 +341,42 @@ func testAlamofire() {
341341

342342
AF.request("http://example.com/").response {
343343
response in
344-
if let data = response.data { // SOURCE
344+
if let data = response.data { // $ source=remote
345345
// ...
346346
}
347347
}
348348

349349
AF.request("http://example.com/").response(responseSerializer: MySerializer()) {
350350
response in
351-
if let obj = response.value { // SOURCE
351+
if let obj = response.value { // $ source=remote
352352
// ...
353353
}
354354
}
355355

356356
AF.request("http://example.com/").responseData {
357357
response in
358-
if let data = response.value { // SOURCE
358+
if let data = response.value { // $ source=remote
359359
// ...
360360
}
361361
}
362362

363363
AF.request("http://example.com/").responseString {
364364
response in
365-
if let str = response.value { // SOURCE
365+
if let str = response.value { // $ source=remote
366366
// ...
367367
}
368368
}
369369

370370
AF.request("http://example.com/").responseJSON {
371371
response in
372-
if let json = response.value { // SOURCE
372+
if let json = response.value { // $ source=remote
373373
// ...
374374
}
375375
}
376376

377377
AF.request("http://example.com/").responseDecodable(of: MyDecodable.self) {
378378
response in
379-
if let decodable = response.value { // SOURCE
379+
if let decodable = response.value { // $ source=remote
380380
// ...
381381
}
382382
}
@@ -386,49 +386,49 @@ func testAlamofire() {
386386
AF.download("http://example.com/").response {
387387
response in
388388
if let path = response.fileURL?.path {
389-
let str = try? String(contentsOfFile: path) // SOURCE
389+
let str = try? String(contentsOfFile: path) // $ MISSING: source=remote $ SPURIOUS: source=local
390390
// ...
391391
}
392392
}
393393

394394
AF.download("http://example.com/").response(responseSerializer: MySerializer()) {
395395
response in
396-
if let obj = response.value { // SOURCE
396+
if let obj = response.value { // $ source=remote
397397
// ...
398398
}
399399
}
400400

401401
AF.download("http://example.com/").responseURL {
402402
response in
403-
if let url = response.value { // just the URL [FALSE POSITIVE]
404-
let str = try? String(contentsOf: url) // SOURCE
403+
if let url = response.value { // $ SPURIOUS: source=remote (this is just the URL)
404+
let str = try? String(contentsOf: url) // $ source=remote
405405
// ...
406406
}
407407
}
408408

409409
AF.download("http://example.com/").responseData {
410410
response in
411-
if let data = response.value { // SOURCE
411+
if let data = response.value { // $ source=remote
412412
// ...
413413
}
414414
}
415415

416416
AF.download("http://example.com/").responseString {
417417
response in
418-
if let str = response.value { // SOURCE
418+
if let str = response.value { // $ source=remote
419419
// ...
420420
}
421421
}
422422

423423
AF.download("http://example.com/").responseJSON {
424424
response in
425-
if let json = response.value { // SOURCE
425+
if let json = response.value { // $ source=remote
426426
}
427427
}
428428

429429
AF.download("http://example.com/").responseDecodable(of: MyDecodable.self) {
430430
response in
431-
if let decodable = response.value { // SOURCE
431+
if let decodable = response.value { // $ source=remote
432432
// ...
433433
}
434434
}
@@ -445,20 +445,20 @@ func testAlamofire() {
445445
// ...
446446
}
447447
// ...
448-
let str = try? String(contentsOfFile: myPath) // SOURCE
448+
let str = try? String(contentsOfFile: myPath) // $ MISSING: source=remote SPURIOUS: source=local
449449

450450
// chaining
451451
// - in practice there are a wide range of calls that can be chained through.
452452

453453
AF.request("http://example.com/").response {
454454
response in
455-
if let data = response.data { // SOURCE
455+
if let data = response.data { // $ source=remote
456456
// ...
457457
}
458458
}
459459
.response {
460460
response in
461-
if let data = response.data { // SOURCE
461+
if let data = response.data { // $ source=remote
462462
// ...
463463
}
464464
}
@@ -470,7 +470,7 @@ func testAlamofire() {
470470
switch stream.event {
471471
case let .stream(result):
472472
switch result {
473-
case let .success(data): // SOURCE [NOT DETECTED]
473+
case let .success(data): // $ MISSING: source=remote
474474
doSomethingWith(data)
475475
// ...
476476
}
@@ -485,7 +485,7 @@ func testAlamofire() {
485485
switch stream.event {
486486
case let .stream(result):
487487
switch result {
488-
case let .success(value): // SOURCE [NOT DETECTED]
488+
case let .success(value): // $ MISSING: source=remote
489489
doSomethingWith(value)
490490
// ...
491491
}
@@ -500,7 +500,7 @@ func testAlamofire() {
500500
switch stream.event {
501501
case let .stream(result):
502502
switch result {
503-
case let .success(value): // SOURCE [NOT DETECTED]
503+
case let .success(value): // MISSING: source=remote
504504
doSomethingWith(value)
505505
// ...
506506
}
@@ -515,7 +515,7 @@ func testAlamofire() {
515515
switch stream.event {
516516
case let .stream(result):
517517
switch result {
518-
case let .success(value): // SOURCE [NOT DETECTED]
518+
case let .success(value): // MISSING: source=remote
519519
doSomethingWith(value)
520520
// ...
521521
}
@@ -530,7 +530,7 @@ func testAlamofire() {
530530
AF.streamRequest("http://example.com/").responseStream {
531531
stream in
532532
if case let .stream(myResult) = stream.event {
533-
if case let .success(myData) = myResult { // SOURCE [NOT DETECTED]
533+
if case let .success(myData) = myResult { // MISSING: source=remote
534534
doSomethingWith(myData)
535535
}
536536
}
@@ -539,7 +539,7 @@ func testAlamofire() {
539539
AF.streamRequest("http://example.com/").responseStream {
540540
stream in
541541
if case let .stream(myResult) = stream.event {
542-
doSomethingWith(myResult.success!) // SOURCE [NOT DETECTED]
542+
doSomethingWith(myResult.success!) // MISSING: source=remote
543543
}
544544
}
545545

0 commit comments

Comments
 (0)