Skip to content

Commit 66615f8

Browse files
add tests for calls to functions from extensions and protocols
1 parent 7796815 commit 66615f8

File tree

1 file changed

+144
-0
lines changed
  • joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/ast

1 file changed

+144
-0
lines changed

joern-cli/frontends/swiftsrc2cpg/src/test/scala/io/joern/swiftsrc2cpg/passes/ast/CallTests.scala

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class CallTests extends SwiftCompilerSrc2CpgSuite {
2828
val List(fooCall) = cpg.call.nameExact("foo").l
2929
fooCall.methodFullName shouldBe x2cpg.Defines.DynamicCallUnknownFullName
3030
fooCall.signature shouldBe ""
31+
fooCall.dispatchType shouldBe DispatchTypes.DYNAMIC_DISPATCH
32+
3133
val List(fooCallReceiver) = fooCall.receiver.isIdentifier.l
3234
fooCallReceiver.name shouldBe "self"
3335
fooCallReceiver.typeFullName shouldBe "Sources/main.swift:<global>.Foo"
@@ -111,6 +113,148 @@ class CallTests extends SwiftCompilerSrc2CpgSuite {
111113
returnId.typeFullName shouldBe "SwiftTest.Foo"
112114
}
113115

116+
"be correct for simple calls to functions from extensions" in {
117+
val testCode =
118+
"""
119+
|extension Foo {
120+
| func foo() {}
121+
| func bar() {}
122+
|}
123+
|class Foo {
124+
| func main() {
125+
| foo()
126+
| self.bar()
127+
| }
128+
|}
129+
|""".stripMargin
130+
val cpg = code(testCode)
131+
132+
val List(fooCall) = cpg.call.nameExact("foo").l
133+
fooCall.methodFullName shouldBe x2cpg.Defines.DynamicCallUnknownFullName
134+
fooCall.signature shouldBe ""
135+
fooCall.dispatchType shouldBe DispatchTypes.DYNAMIC_DISPATCH
136+
137+
val List(fooCallReceiver) = fooCall.receiver.isIdentifier.l
138+
fooCallReceiver.name shouldBe "self"
139+
fooCallReceiver.typeFullName shouldBe "Sources/main.swift:<global>.Foo"
140+
141+
val List(barCall) = cpg.call.nameExact("bar").l
142+
barCall.methodFullName shouldBe x2cpg.Defines.DynamicCallUnknownFullName
143+
barCall.signature shouldBe ""
144+
barCall.dispatchType shouldBe DispatchTypes.DYNAMIC_DISPATCH
145+
146+
val List(barCallReceiverCall) = barCall.receiver.isIdentifier.l
147+
barCallReceiverCall.name shouldBe "self"
148+
barCallReceiverCall.typeFullName shouldBe "Sources/main.swift:<global>.Foo"
149+
}
150+
151+
"be correct for simple calls to functions from extensions with compiler support" in {
152+
val testCode =
153+
"""
154+
|extension Foo {
155+
| func foo() {}
156+
| func bar() {}
157+
|}
158+
|class Foo {
159+
| func main() {
160+
| foo()
161+
| self.bar()
162+
| }
163+
|}
164+
|""".stripMargin
165+
val cpg = codeWithSwiftSetup(testCode)
166+
167+
val List(fooCall) = cpg.call.nameExact("foo").l
168+
fooCall.methodFullName shouldBe "SwiftTest.Foo.foo:()->()"
169+
fooCall.signature shouldBe "()->()"
170+
fooCall.dispatchType shouldBe DispatchTypes.DYNAMIC_DISPATCH
171+
val List(fooCallReceiver) = fooCall.receiver.isIdentifier.l
172+
fooCallReceiver.name shouldBe "self"
173+
fooCallReceiver.typeFullName shouldBe "SwiftTest.Foo"
174+
175+
val List(barCall) = cpg.call.nameExact("bar").l
176+
barCall.methodFullName shouldBe "SwiftTest.Foo.bar:()->()"
177+
barCall.signature shouldBe "()->()"
178+
barCall.dispatchType shouldBe DispatchTypes.DYNAMIC_DISPATCH
179+
180+
val List(barCallReceiverCall) = barCall.receiver.isIdentifier.l
181+
barCallReceiverCall.name shouldBe "self"
182+
barCallReceiverCall.typeFullName shouldBe "SwiftTest.Foo"
183+
}
184+
185+
"be correct for simple calls to functions from protocols" in {
186+
val testCode =
187+
"""
188+
|protocol FooProtocol {
189+
| func foo()
190+
| func bar()
191+
|}
192+
|class Foo: FooProtocol {
193+
| func foo() {}
194+
| func bar() {}
195+
| func main() {
196+
| foo()
197+
| self.bar()
198+
| }
199+
|}
200+
|""".stripMargin
201+
val cpg = code(testCode)
202+
203+
val List(fooCall) = cpg.call.nameExact("foo").l
204+
fooCall.methodFullName shouldBe x2cpg.Defines.DynamicCallUnknownFullName
205+
fooCall.signature shouldBe ""
206+
fooCall.dispatchType shouldBe DispatchTypes.DYNAMIC_DISPATCH
207+
208+
val List(fooCallReceiver) = fooCall.receiver.isIdentifier.l
209+
fooCallReceiver.name shouldBe "self"
210+
fooCallReceiver.typeFullName shouldBe "Sources/main.swift:<global>.Foo"
211+
212+
val List(barCall) = cpg.call.nameExact("bar").l
213+
barCall.methodFullName shouldBe x2cpg.Defines.DynamicCallUnknownFullName
214+
barCall.signature shouldBe ""
215+
barCall.dispatchType shouldBe DispatchTypes.DYNAMIC_DISPATCH
216+
217+
val List(barCallReceiverCall) = barCall.receiver.isIdentifier.l
218+
barCallReceiverCall.name shouldBe "self"
219+
barCallReceiverCall.typeFullName shouldBe "Sources/main.swift:<global>.Foo"
220+
}
221+
222+
"be correct for simple calls to functions from protocols with compiler support" in {
223+
val testCode =
224+
"""
225+
|protocol FooProtocol {
226+
| func foo()
227+
| func bar()
228+
|}
229+
|class Foo: FooProtocol {
230+
| func foo() {}
231+
| func bar() {}
232+
| func main() {
233+
| foo()
234+
| self.bar()
235+
| }
236+
|}
237+
|""".stripMargin
238+
val cpg = codeWithSwiftSetup(testCode)
239+
240+
val List(fooCall) = cpg.call.nameExact("foo").l
241+
fooCall.methodFullName shouldBe "SwiftTest.Foo.foo:()->()"
242+
fooCall.signature shouldBe "()->()"
243+
fooCall.dispatchType shouldBe DispatchTypes.DYNAMIC_DISPATCH
244+
val List(fooCallReceiver) = fooCall.receiver.isIdentifier.l
245+
fooCallReceiver.name shouldBe "self"
246+
fooCallReceiver.typeFullName shouldBe "SwiftTest.Foo"
247+
248+
val List(barCall) = cpg.call.nameExact("bar").l
249+
barCall.methodFullName shouldBe "SwiftTest.Foo.bar:()->()"
250+
barCall.signature shouldBe "()->()"
251+
barCall.dispatchType shouldBe DispatchTypes.DYNAMIC_DISPATCH
252+
253+
val List(barCallReceiverCall) = barCall.receiver.isIdentifier.l
254+
barCallReceiverCall.name shouldBe "self"
255+
barCallReceiverCall.typeFullName shouldBe "SwiftTest.Foo"
256+
}
257+
114258
"be correct for simple call to static function" in {
115259
// TODO: extend the GsonTypeInfoReader to query for information whether the call is a call to a static function
116260
val testCode =

0 commit comments

Comments
 (0)