Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
Signed-off-by: Juanjo Alvarez <[email protected]>
  • Loading branch information
juanjux committed Jan 9, 2025
1 parent 518f031 commit 3ff50d1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
7 changes: 5 additions & 2 deletions ddtrace/appsec/_iast/_ast/ast_patching.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,18 +329,20 @@


class _TrieNode:
__slots__ = ('children', 'is_end')
__slots__ = ("children", "is_end")

def __init__(self):
self.children = {}
self.is_end = False

def __iter__(self):
if self.is_end:
yield ("", None)
else:
for k, v in self.children.items():
yield (k, dict(v))


def build_trie(words: Iterable[str]) -> _TrieNode:
root = _TrieNode()
for word in words:
Expand All @@ -352,6 +354,7 @@ def build_trie(words: Iterable[str]) -> _TrieNode:
node.is_end = True
return root


_TRIE_ALLOWLIST = build_trie(IAST_ALLOWLIST)
_TRIE_DENYLIST = build_trie(IAST_DENYLIST)

Expand Down
22 changes: 10 additions & 12 deletions tests/appsec/iast/_ast/test_ast_patching.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,60 +312,58 @@ def test_astpatch_dir_patched_with_or_without_custom_dir(module_name, expected_n
assert name in patched_dir



def test_build_trie():
from ddtrace.appsec._iast._ast.ast_patching import build_trie

trie = build_trie(["abc", "def", "ghi", "jkl", "mno", "pqr", "stu", "vwx", "yz"])
dict_trie = dict(trie)
assert dict(trie) == {
"a": {
"b": {
"c": {'': None},
"c": {"": None},
},
},
"d": {
"e": {
"f": {'': None},
"f": {"": None},
},
},
"g": {
"h": {
"i": {'': None},
"i": {"": None},
},
},
"j": {
"k": {
"l": {'': None},
"l": {"": None},
},
},
"m": {
"n": {
"o": {'': None},
"o": {"": None},
},
},
"p": {
"q": {
"r": {'': None},
"r": {"": None},
},
},
"s": {
"t": {
"u": {'': None},
"u": {"": None},
},
},
"v": {
"w": {
"x": {'': None},
"x": {"": None},
},
},
"y": {
"z": {'': None},
"z": {"": None},
},
}

def test_trie_has_string_match():

def test_trie_has_string_match():
trie = build_trie(["abc", "def", "ghi", "jkl", "mno", "pqr", "stu", "vwx", "yz"])
assert _trie_has_prefix_for(trie, "abc")
assert not _trie_has_prefix_for(trie, "ab")
Expand Down

0 comments on commit 3ff50d1

Please sign in to comment.