-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests_program_model_contract.py
More file actions
359 lines (330 loc) · 11.6 KB
/
Copy pathtests_program_model_contract.py
File metadata and controls
359 lines (330 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
from __future__ import annotations
import unittest
from dataclasses import dataclass
import program_model as pm
@dataclass
class FakeProvider:
snapshot: pm.ProgramSnapshot
entities: list[pm.ProgramEntity]
relationships: list[pm.ProgramRelationship]
evidence: dict[str, dict]
max_scan: int | None = None
def get_entity(self, entity_id: str):
return next((item for item in self.entities if item.entity_id == entity_id), None)
def query_entities(
self,
*,
kind=None,
text=None,
ownership_scope="application",
representation=None,
after=None,
limit=1000,
):
ordered = sorted(self.entities, key=pm.entity_sort_key)
accepted = []
scanned = 0
truncated = False
for item in ordered:
scanned += 1
if self.max_scan is not None and scanned > self.max_scan:
truncated = True
break
if kind and item.kind != kind:
continue
if representation and item.representation != representation:
continue
if text and text.lower() not in (
item.display_name + " " + item.semantic_key
).lower():
continue
if not pm.ownership_scope_accepts(item.ownership, ownership_scope):
continue
if after is not None and pm.entity_sort_key(item) <= after:
continue
accepted.append(item)
if len(accepted) > limit:
break
has_more = len(accepted) > limit
return pm.ProviderPage(tuple(accepted[:limit]), has_more=has_more, truncated=truncated)
def query_relationships(
self,
*,
entity_id,
kinds=None,
direction="both",
ownership_scope="application",
after=None,
limit=1000,
):
ordered = sorted(self.relationships, key=pm.relationship_sort_key)
accepted = []
for item in ordered:
if kinds and item.kind not in kinds:
continue
if direction == "incoming" and item.target_entity_id != entity_id:
continue
if direction == "outgoing" and item.source_entity_id != entity_id:
continue
if direction == "both" and entity_id not in {
item.source_entity_id,
item.target_entity_id,
}:
continue
if after is not None and pm.relationship_sort_key(item) <= after:
continue
accepted.append(item)
if len(accepted) > limit:
break
has_more = len(accepted) > limit
return pm.ProviderPage(tuple(accepted[:limit]), has_more=has_more)
def get_evidence(self, evidence_ref):
return self.evidence.get(evidence_ref)
def entity(snapshot, key, name, ownership="FIRST_PARTY", props=None):
return pm.ProgramEntity(
snapshot_id=snapshot.snapshot_id,
entity_id=pm.entity_id(snapshot, "FUNCTION", key),
semantic_key=key,
kind="FUNCTION",
display_name=name,
representation="dex",
ownership=ownership,
properties=props or {"signature": "()V"},
evidence_refs=("ev:1",),
)
class ProgramModelContractTests(unittest.TestCase):
def setUp(self):
self.snapshot = pm.ProgramSnapshot("a" * 64, "apk")
def test_snapshot_identity_is_provider_independent(self):
same = pm.ProgramSnapshot("a" * 64, "libapp.so")
self.assertEqual(self.snapshot.snapshot_id, same.snapshot_id)
self.assertNotIn("apk", self.snapshot.snapshot_key)
def test_entity_id_deterministic_and_snapshot_scoped(self):
key = "function:v1:dex:com.example.A#f()V"
first = pm.entity_id(self.snapshot, "FUNCTION", key)
self.assertEqual(first, pm.entity_id(self.snapshot, "FUNCTION", key))
self.assertNotEqual(
first,
pm.entity_id(pm.ProgramSnapshot("b" * 64), "FUNCTION", key),
)
def test_overload_identity_uses_signature(self):
self.assertNotEqual(
pm.entity_id(self.snapshot, "FUNCTION", "function:v1:dex:A#f()V"),
pm.entity_id(self.snapshot, "FUNCTION", "function:v1:dex:A#f(I)V"),
)
def test_backend_specific_property_rejected(self):
with self.assertRaises(pm.ProgramModelError):
entity(
self.snapshot,
"function:v1:dex:A#f()V",
"f",
props={"sqlite_rowid": 7},
)
def test_merge_duplicate_preserves_evidence(self):
left = entity(self.snapshot, "function:v1:dex:A#f()V", "f")
right = pm.ProgramEntity(
**{**left.__dict__, "evidence_refs": ("ev:2",)}
)
self.assertEqual(
pm.merge_entity(left, right).evidence_refs,
("ev:1", "ev:2"),
)
def test_property_conflict_does_not_last_write_win(self):
left = entity(
self.snapshot,
"function:v1:dex:A#f()V",
"f",
props={"signature": "()V", "parameter_count": 0},
)
right = entity(
self.snapshot,
"function:v1:dex:A#f()V",
"f",
props={"signature": "()V", "parameter_count": 1},
)
merged = pm.merge_entity(left, right)
self.assertEqual(merged.properties["signature"], "()V")
self.assertNotIn("parameter_count", merged.properties)
def test_conflicting_strong_ownership_becomes_unknown(self):
left = entity(
self.snapshot,
"function:v1:dex:A#f()V",
"f",
"FIRST_PARTY",
)
right = entity(
self.snapshot,
"function:v1:dex:A#f()V",
"f",
"THIRD_PARTY",
)
self.assertEqual(pm.merge_entity(left, right).ownership, "UNKNOWN")
def test_deterministic_pagination_independent_of_provider_order(self):
items = [
entity(self.snapshot, f"function:v1:dex:A#{name}()V", name)
for name in ("z", "a", "m")
]
repo_a = pm.ProgramRepository(
[FakeProvider(self.snapshot, list(items), [], {})]
)
repo_b = pm.ProgramRepository(
[FakeProvider(self.snapshot, list(reversed(items)), [], {})]
)
page_a = repo_a.find_entities(kind="FUNCTION", limit=2)
page_b = repo_b.find_entities(kind="FUNCTION", limit=2)
self.assertEqual(
[item.entity_id for item in page_a.items],
[item.entity_id for item in page_b.items],
)
self.assertTrue(page_a.has_more)
next_page = repo_a.find_entities(
kind="FUNCTION",
limit=2,
cursor=page_a.cursor,
)
self.assertEqual(len(next_page.items), 1)
def test_pagination_crosses_provider_window_without_losing_entities(self):
items = [
entity(
self.snapshot,
f"function:v1:dex:A#f{i:04d}()V",
f"f{i:04d}",
)
for i in range(25)
]
repo = pm.ProgramRepository(
[FakeProvider(self.snapshot, list(reversed(items)), [], {})]
)
seen = []
cursor = None
for _ in range(20):
page = repo.find_entities(
kind="FUNCTION",
limit=2,
cursor=cursor,
)
seen.extend(item.entity_id for item in page.items)
if not page.has_more:
break
self.assertIsNotNone(page.cursor)
cursor = page.cursor
self.assertEqual(len(seen), len(items))
self.assertEqual(len(set(seen)), len(items))
def test_provider_truncation_is_explicit(self):
items = [
entity(
self.snapshot,
f"function:v1:dex:A#f{i}()V",
f"f{i}",
)
for i in range(10)
]
repo = pm.ProgramRepository(
[FakeProvider(self.snapshot, items, [], {}, max_scan=1)]
)
page = repo.find_entities(kind="FUNCTION", limit=5)
self.assertTrue(page.truncated)
self.assertTrue(page.has_more)
def test_cursor_is_query_and_snapshot_bound(self):
items = [
entity(self.snapshot, f"function:v1:dex:A#{value}()V", value)
for value in ("a", "b")
]
repo = pm.ProgramRepository([FakeProvider(self.snapshot, items, [], {})])
cursor = repo.find_entities(limit=1).cursor
with self.assertRaises(pm.ProgramModelError):
repo.find_entities(kind="CLASS", limit=1, cursor=cursor)
other = pm.ProgramSnapshot("b" * 64)
with self.assertRaises(pm.ProgramModelError):
pm.ProgramRepository(
[FakeProvider(other, [], [], {})]
).find_entities(limit=1, cursor=cursor)
def test_cursor_tamper_rejected(self):
items = [
entity(self.snapshot, f"function:v1:dex:A#{value}()V", value)
for value in ("a", "b")
]
repo = pm.ProgramRepository([FakeProvider(self.snapshot, items, [], {})])
cursor = repo.find_entities(limit=1).cursor
self.assertIsNotNone(cursor)
tampered = cursor[:-1] + ("A" if cursor[-1] != "A" else "B")
with self.assertRaises(pm.ProgramModelError):
repo.find_entities(limit=1, cursor=tampered)
def test_relationship_merge_preserves_evidence(self):
left_entity = entity(
self.snapshot,
"function:v1:dex:A#a()V",
"a",
)
right_entity = entity(
self.snapshot,
"function:v1:dex:A#b()V",
"b",
)
rid = pm.relationship_id(
self.snapshot,
"CALLS",
left_entity.entity_id,
right_entity.entity_id,
)
left = pm.ProgramRelationship(
self.snapshot.snapshot_id,
rid,
"CALLS",
left_entity.entity_id,
right_entity.entity_id,
"dex",
{},
("ev:1",),
)
right = pm.ProgramRelationship(
self.snapshot.snapshot_id,
rid,
"CALLS",
left_entity.entity_id,
right_entity.entity_id,
"dex",
{},
("ev:2",),
)
self.assertEqual(
pm.merge_relationship(left, right).evidence_refs,
("ev:1", "ev:2"),
)
def test_application_scope_is_first_party_plus_unknown(self):
first = entity(
self.snapshot,
"function:v1:dex:A#a()V",
"a",
"FIRST_PARTY",
)
unknown = entity(
self.snapshot,
"function:v1:dex:A#u()V",
"u",
"UNKNOWN",
)
third = entity(
self.snapshot,
"function:v1:dex:A#t()V",
"t",
"THIRD_PARTY",
)
repo = pm.ProgramRepository(
[FakeProvider(self.snapshot, [third, unknown, first], [], {})]
)
self.assertEqual(
{item.ownership for item in repo.find_entities(limit=10).items},
{"FIRST_PARTY", "UNKNOWN"},
)
def test_repository_rejects_mixed_snapshots(self):
other = pm.ProgramSnapshot("b" * 64)
with self.assertRaises(pm.ProgramModelError):
pm.ProgramRepository(
[
FakeProvider(self.snapshot, [], [], {}),
FakeProvider(other, [], [], {}),
]
)
if __name__ == "__main__":
unittest.main()