Skip to content

Commit

Permalink
Python: Add proper type-tracking tests for content
Browse files Browse the repository at this point in the history
Instead of just relying on the call-graph tests
  • Loading branch information
RasmusWL committed Mar 12, 2024
1 parent 475efd9 commit f9f5775
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
78 changes: 78 additions & 0 deletions python/ql/test/experimental/dataflow/typetracking/content_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# test of other content types than attributes

def test_tuple(index_arg):
tup = (tracked, other) # $tracked

tup[0] # $ tracked
tup[1]

a,b = tup # $tracked
a # $ tracked
b

# non-precise access is not supported right now (and it's not 100% clear if we want
# to support it, or if it will lead to bad results)
tup[index_arg]

for x in tup:
print(x)

for i in range(len(tup)):
print(tup[i])


def test_dict(key_arg):
d1 = {"t": tracked, "o": other} # $tracked
d1["t"] # $ tracked
d1.get("t") # $ MISSING: tracked
d1.setdefault("t") # $ MISSING: tracked

d1["o"]
d1.get("o")
d1.setdefault("o")


# non-precise access is not supported right now (and it's not 100% clear if we want
# to support it, or if it will lead to bad results)
d1[key_arg]

for k in d1:
d1[k]

for v in d1.values():
v

for k, v in d1.items():
v


# construction with inline updates
d2 = dict()
d2["t"] = tracked # $ tracked
d2["o"] = other

d2["t"] # $ tracked
d2["o"]

# notice that time-travel is also possible (just as with attributes)
d3 = dict()
d3["t"] # $ SPURIOUS: tracked
d3["t"] = tracked # $ tracked
d3["t"] # $ tracked


def test_list(index_arg):
l = [tracked, other] # $tracked

l[0] # $ MISSING: tracked
l[1]

# non-precise access is not supported right now (and it's not 100% clear if we want
# to support it, or if it will lead to bad results)
l[index_arg]

for x in l:
print(x)

for i in range(len(l)):
print(l[i])
8 changes: 8 additions & 0 deletions python/ql/test/experimental/dataflow/typetracking/tracked.ql
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ module TrackedTest implements TestSig {
not e instanceof DataFlow::ScopeEntryDefinitionNode and
// ...same for `SynthCaptureNode`s
not e instanceof DP::SynthCaptureNode and
// after starting to track all kinds of content, we generally just want to show
// annotations after reading the tracked data out again. (we keep the old
// attribute logic to not rewrite all our tests)
(
t.getContent().isNone()
or
t.getContent().asSome() instanceof DataFlow::AttributeContent
) and
tag = "tracked" and
location = e.getLocation() and
value = t.getAttr() and
Expand Down

0 comments on commit f9f5775

Please sign in to comment.