Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joefarebrother committed Jan 7, 2025
1 parent 3df5866 commit 7e83b0f
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 2 deletions.
2 changes: 1 addition & 1 deletion python/ql/lib/semmle/python/frameworks/Stdlib.qll
Original file line number Diff line number Diff line change
Expand Up @@ -4576,7 +4576,7 @@ module StdlibPrivate {
)
// TODO: Once we have DictKeyContent, we need to transform that into ListElementContent
) and
output = "Argument[0].Parameter[0]" and
(output = "Argument[0].Parameter[0]" or output = "ReturnValue.ListElement") and
preservesValue = true
}
}
Expand Down
108 changes: 108 additions & 0 deletions python/ql/test/library-tests/dataflow/coverage/test_builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,111 @@ def test_next_dict():
i = iter(d)
n = next(i)
SINK(n) #$ MISSING: flow="SOURCE, l:-3 -> n"

### map

@expects(4)
def test_map_list():
l1 = [SOURCE]
l2 = [NONSOURCE]

def f(p1,p2):
SINK(p1) #$ flow="SOURCE, l:-4 -> p1"
SINK_F(p2)

return p1,p2

rl = list(map(f, l1, l2))
SINK(rl[0][0]) #$ flow="SOURCE, l:-10 -> rl[0][0]"
SINK_F(rl[0][1])

@expects(4)
def test_map_set():
s1 = {SOURCE}
s2 = {NONSOURCE}

def f(p1,p2):
SINK(p1) #$ flow="SOURCE, l:-4 -> p1"
SINK_F(p2)

return p1,p2

rl = list(map(f, s1, s2))
SINK(rl[0][0]) #$ flow="SOURCE, l:-10 -> rl[0][0]"
SINK_F(rl[0][1])

@expects(4)
def test_map_tuple():
t1 = (SOURCE,)
t2 = (NONSOURCE,)

def f(p1,p2):
SINK(p1) #$ flow="SOURCE, l:-4 -> p1"
SINK_F(p2)

return p1,p2

rl = list(map(f, t1, t2))
SINK(rl[0][0]) #$ flow="SOURCE, l:-10 -> rl[0][0]"
SINK_F(rl[0][1])

@expects(4)
def test_map_dict():
d1 = {SOURCE: "v1"}
d2 = {NONSOURCE: "v2"}

def f(p1,p2):
SINK(p1) #$ MISSING: flow="SOURCE, l:-4 -> p1"
SINK_F(p2)

return p1,p2

rl = list(map(f, d1, d2))
SINK(rl[0][0]) #$ MISSING: flow="SOURCE, l:-10 -> rl[0][0]"
SINK_F(rl[0][1])

### filter

@expects(2)
def test_filter_list():
l = [SOURCE]

def f(p):
SINK(p) #$ flow="SOURCE, l:-3 -> p"
return True

rl = list(filter(f,l))
SINK(rl[0]) #$ flow="SOURCE, l:-7 -> rl[0]"

@expects(2)
def test_filter_set():
s = {SOURCE}

def f(p):
SINK(p) #$ flow="SOURCE, l:-3 -> p"
return True

rl = list(filter(f,s))
SINK(rl[0]) #$ flow="SOURCE, l:-7 -> rl[0]"

@expects(2)
def test_filter_tuple():
t = (SOURCE,)

def f(p):
SINK(p) #$ flow="SOURCE, l:-3 -> p"
return True

rl = list(filter(f,t))
SINK(rl[0]) #$ flow="SOURCE, l:-7 -> rl[0]"

@expects(2)
def test_filter_dict():
d = {SOURCE: "v"}

def f(p):
SINK(p) #$ MISSING: flow="SOURCE, l:-3 -> p"
return True

rl = list(filter(f,d))
SINK(rl[0]) #$ MISSING: flow="SOURCE, l:-7 -> rl[0]"
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ def set(x):
for x in map(set, [1]):
pass

SINK(captured["x"]) #$ MISSING: captured
SINK(captured["x"]) #$ captured

0 comments on commit 7e83b0f

Please sign in to comment.