Skip to content

Commit 228aaee

Browse files
committed
Swift: Add data flow tests for RawRepresentable, OptionSet.
1 parent 9e473eb commit 228aaee

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
2+
// --- stubs ---
3+
4+
// --- tests ---
5+
6+
func sourceInt() -> Int { return 0 }
7+
func sourceUInt() -> UInt { return 0 }
8+
func sink(arg: Any) {}
9+
10+
// ---
11+
12+
enum MyRawRepresentable : RawRepresentable {
13+
case valueOne
14+
case valueTwo
15+
16+
init?(rawValue: Int) {
17+
switch rawValue {
18+
case 1: self = .valueOne
19+
case 2: self = .valueTwo
20+
default: return nil
21+
}
22+
}
23+
24+
var rawValue: Int {
25+
switch self {
26+
case .valueOne: return 1
27+
case .valueTwo: return 2
28+
}
29+
}
30+
}
31+
32+
func testRawRepresentable() {
33+
let rr1 = MyRawRepresentable.valueOne
34+
let rr2 = MyRawRepresentable(rawValue: 1)!
35+
let rr3 = MyRawRepresentable(rawValue: sourceInt())!
36+
37+
sink(arg: rr1)
38+
sink(arg: rr2)
39+
sink(arg: rr3) // $ MISSING: tainted=
40+
41+
sink(arg: rr1.rawValue)
42+
sink(arg: rr2.rawValue)
43+
sink(arg: rr3.rawValue) // $ MISSING: tainted=
44+
}
45+
46+
// ---
47+
48+
struct MyOptionSet : OptionSet {
49+
let rawValue: UInt
50+
51+
static let red = MyOptionSet(rawValue: 1 << 0)
52+
static let green = MyOptionSet(rawValue: 1 << 1)
53+
static let blue = MyOptionSet(rawValue: 1 << 2)
54+
}
55+
56+
func testOptionSet() {
57+
sink(arg: MyOptionSet.red)
58+
sink(arg: MyOptionSet([.red, .green]))
59+
sink(arg: MyOptionSet(rawValue: 0))
60+
sink(arg: MyOptionSet(rawValue: sourceUInt())) // $ MISSING: tainted=
61+
62+
sink(arg: MyOptionSet.red.rawValue)
63+
sink(arg: MyOptionSet([.red, .green]).rawValue)
64+
sink(arg: MyOptionSet(rawValue: 0).rawValue)
65+
sink(arg: MyOptionSet(rawValue: sourceUInt()).rawValue) // $ MISSING: tainted=
66+
}

0 commit comments

Comments
 (0)