Skip to content

Commit cb6aed1

Browse files
committed
Swift: Add tests.
1 parent a1234d4 commit cb6aed1

File tree

1 file changed

+54
-5
lines changed
  • swift/ql/test/library-tests/dataflow/flowsources

1 file changed

+54
-5
lines changed
Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,82 @@
11
// --- stubs ---
22

33
class NSObject { }
4-
class NSAttributedString: NSObject {}
5-
class UIResponder: NSObject {}
6-
class UIView: UIResponder {}
7-
class UIControl: UIView {}
4+
5+
struct _NSRange { }
6+
7+
typealias NSRange = _NSRange
8+
9+
class NSAttributedString: NSObject { }
10+
11+
class UIResponder: NSObject { }
12+
13+
class UIView: UIResponder { }
14+
15+
class UIControl: UIView { }
16+
17+
class UITextRange : NSObject {
18+
}
19+
20+
protocol UITextInput {
21+
func text(in range: UITextRange) -> String?
22+
23+
func shouldChangeText(in range: UITextRange, replacementText text: String) -> Bool
24+
}
25+
826
class UITextField: UIControl {
927
var text: String? {
1028
get { nil }
1129
set { }
1230
}
31+
1332
var attributedText: NSAttributedString? {
1433
get { nil }
1534
set { }
1635
}
36+
1737
var placeholder: String? {
1838
get { nil }
1939
set { }
2040
}
2141
}
42+
2243
class UISearchTextField : UITextField {
2344
}
2445

46+
protocol UITextFieldDelegate {
47+
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
48+
}
49+
2550
// --- tests ---
2651

27-
func testUITextField(textField: UITextField, searchTextField: UISearchTextField) {
52+
func sink(arg: Any) { }
53+
54+
class MyTextInput : UITextInput {
55+
func text(in range: UITextRange) -> String? { return nil }
56+
func harmless(in range: UITextRange) -> String? { return nil }
57+
58+
func shouldChangeText(in range: UITextRange, replacementText text: String) -> Bool { // $ MISSING: source=local
59+
sink(arg: text) // $ MISSING: tainted=
60+
61+
return true
62+
}
63+
}
64+
65+
class MyUITextFieldDelegate : UITextFieldDelegate {
66+
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { // $ MISSING: source=local
67+
sink(arg: string) // $ MISSING: tainted=
68+
69+
return true
70+
}
71+
}
72+
73+
func test(textField: UITextField, searchTextField: UISearchTextField, myTextInput: MyTextInput, range: UITextRange) {
2874
_ = textField.text // $ source=local
2975
_ = textField.attributedText // $ source=local
3076
_ = textField.placeholder // GOOD (not input)
3177
_ = textField.text?.uppercased() // $ source=local
3278
_ = searchTextField.text // $ source=local
79+
80+
_ = myTextInput.text(in: range)! // $ MISSING: source=local
81+
_ = myTextInput.harmless(in: range)! // GOOD (not input)
3382
}

0 commit comments

Comments
 (0)