-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests_flow_ir.py
More file actions
292 lines (268 loc) · 12.1 KB
/
Copy pathtests_flow_ir.py
File metadata and controls
292 lines (268 loc) · 12.1 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
from __future__ import annotations
import json
import unittest
from unittest import mock
import flow_ir as flow
import program_model as pm
class FlowIRTests(unittest.TestCase):
def setUp(self):
self.snapshot = pm.ProgramSnapshot("a" * 64).snapshot_id
self.owner = "pm:v1:function:owner"
def node(
self,
key,
kind="LOCAL",
*,
roles=(),
label="",
properties=None,
snapshot=None,
):
snapshot = snapshot or self.snapshot
semantic_key = (
flow.constant_semantic_key(key)
if kind == "CONSTANT" and not str(key).startswith("constant:")
else key
)
return flow.FlowNode(
snapshot_id=snapshot,
node_id=flow.flow_node_id(snapshot, kind, self.owner, semantic_key),
semantic_key=semantic_key,
value_kind=kind,
owner_entity_id=self.owner,
representation="dex",
roles=tuple(roles),
label=label,
properties=properties or {},
evidence_refs=("pme:test",),
)
def edge(self, source, target, kind="ASSIGNMENT", *, discriminator="", properties=None):
return flow.FlowEdge(
snapshot_id=self.snapshot,
edge_id=flow.flow_edge_id(
self.snapshot,
kind,
source.node_id,
target.node_id,
discriminator,
),
kind=kind,
source_node_id=source.node_id,
target_node_id=target.node_id,
representation="dex",
producer="fixture-producer",
discriminator=discriminator,
properties=properties or {},
evidence_refs=("pme:edge",),
)
def gap(self, source, target, kind="DYNAMIC_DISPATCH", *, discriminator=""):
return flow.FlowGap(
snapshot_id=self.snapshot,
gap_id=flow.flow_gap_id(
self.snapshot,
kind,
self.owner,
source.node_id,
target.node_id,
discriminator,
),
kind=kind,
owner_entity_id=self.owner,
representation="dex",
producer="fixture-producer",
source_node_id=source.node_id,
target_node_id=target.node_id,
discriminator=discriminator,
reason="target cannot be resolved statically",
evidence_refs=("pme:gap",),
)
def path(self, nodes, segments, *, complete):
node_ids = tuple(item.node_id for item in nodes)
segment_ids = tuple(
item.edge_id if isinstance(item, flow.FlowEdge) else item.gap_id
for item in segments
)
return flow.FlowPath(
snapshot_id=self.snapshot,
path_id=flow.flow_path_id(self.snapshot, node_ids, segment_ids),
node_ids=node_ids,
segment_ids=segment_ids,
complete=complete,
)
def test_deterministic_identity_and_serialization(self):
left = self.node("local:z")
right = self.node("local:a", roles=("SINK", "SOURCE", "SINK"))
edge = self.edge(right, left, discriminator="statement:12")
path = self.path((right, left), (edge,), complete=True)
first = flow.FlowDocument(self.snapshot, nodes=(left, right), edges=(edge,), paths=(path,))
second = flow.FlowDocument(self.snapshot, nodes=(right, left), edges=(edge,), paths=(path,))
self.assertEqual(first.to_dict(), second.to_dict())
self.assertEqual(right.roles, ("SINK", "SOURCE"))
self.assertEqual(
json.dumps(first.to_dict(), sort_keys=True, separators=(",", ":")),
json.dumps(second.to_dict(), sort_keys=True, separators=(",", ":")),
)
self.assertTrue(left.node_id.startswith("flown:v1:local:"))
self.assertTrue(edge.edge_id.startswith("flowe:v1:assignment:"))
self.assertTrue(path.path_id.startswith("flowp:v1:"))
def test_node_identity_role_and_property_validation(self):
with self.assertRaises(flow.FlowIRError):
flow.FlowNode(
self.snapshot,
"flown:v1:local:not-canonical",
"x",
"LOCAL",
self.owner,
"dex",
)
with self.assertRaises(flow.FlowIRError):
self.node("x", roles=("AUTH_MAGIC",))
with self.assertRaises(flow.FlowIRError):
self.node("secret", kind="CONSTANT", properties={"raw_value": "secret"})
with self.assertRaisesRegex(flow.FlowIRError, "constant label"):
self.node("secret-label", kind="CONSTANT", label="Bearer real-secret")
with self.assertRaisesRegex(flow.FlowIRError, "constant semantic_key"):
flow.FlowNode(
self.snapshot,
flow.flow_node_id(self.snapshot, "CONSTANT", self.owner, "constant:plaintext"),
"constant:plaintext",
"CONSTANT",
self.owner,
"dex",
)
with self.assertRaisesRegex(flow.FlowIRError, "value_fingerprint"):
self.node(
"bad-fingerprint",
kind="CONSTANT",
properties={"value_fingerprint": "sha256:abc"},
)
with self.assertRaisesRegex(flow.FlowIRError, "parameter_index"):
self.node("parameter:bad-index", kind="PARAMETER", properties={"parameter_index": "0"})
constant = self.node(
"bearer-literal@statement-24",
kind="CONSTANT",
properties={
"literal_kind": "string",
"type": "java.lang.String",
"value_fingerprint": "sha256:" + "a" * 64,
},
)
self.assertRegex(constant.semantic_key, r"^constant:[0-9a-f]{64}$")
self.assertNotIn("value", constant.properties)
self.assertEqual(constant.label, "")
def test_edge_property_types_fail_closed(self):
left = self.node("left")
right = self.node("right")
with self.assertRaisesRegex(flow.FlowIRError, "statement_offset"):
self.edge(left, right, properties={"statement_offset": -1})
with self.assertRaisesRegex(flow.FlowIRError, "statement_offset"):
self.edge(left, right, properties={"statement_offset": True})
with self.assertRaisesRegex(flow.FlowIRError, "non-canonical"):
self.edge(left, right, properties={"callsite_guess": 42})
def test_multi_edge_and_gap_discriminator_preserves_occurrences(self):
left = self.node("left")
right = self.node("right")
edge_a = self.edge(left, right, discriminator="callsite:10")
edge_b = self.edge(left, right, discriminator="callsite:20")
self.assertNotEqual(edge_a.edge_id, edge_b.edge_id)
gap_a = self.gap(left, right, discriminator="dispatch:10")
gap_b = self.gap(left, right, discriminator="dispatch:20")
self.assertNotEqual(gap_a.gap_id, gap_b.gap_id)
document = flow.FlowDocument(
self.snapshot,
nodes=(left, right),
edges=(edge_a, edge_b),
gaps=(gap_a, gap_b),
)
self.assertEqual(len(document.edges), 2)
self.assertEqual(len(document.gaps), 2)
def test_calls_and_xref_are_not_flow_edges(self):
left = self.node("left")
right = self.node("right")
for kind in ("CALLS", "XREF", "CALLS_EXTERNAL"):
with self.subTest(kind=kind), self.assertRaises(flow.FlowIRError):
flow.flow_edge_id(self.snapshot, kind, left.node_id, right.node_id)
descriptor = flow.descriptor()
self.assertFalse(descriptor["calls_xref_are_data_flow"])
self.assertNotIn("CALLS", descriptor["edge_kinds"])
self.assertNotIn("XREF", descriptor["edge_kinds"])
def test_edge_endpoints_must_resolve(self):
left = self.node("left")
right = self.node("right")
edge = self.edge(left, right)
with self.assertRaisesRegex(flow.FlowIRError, "endpoint"):
flow.FlowDocument(self.snapshot, nodes=(left,), edges=(edge,))
def test_valid_gap_path_is_explicitly_incomplete(self):
left = self.node("left", roles=("SOURCE",))
right = self.node("right", roles=("SINK",))
gap = self.gap(left, right, "REFLECTION")
path = self.path((left, right), (gap,), complete=False)
document = flow.FlowDocument(self.snapshot, nodes=(left, right), gaps=(gap,), paths=(path,))
self.assertEqual(document.paths[0].complete, False)
self.assertEqual(document.gaps[0].kind, "REFLECTION")
def test_gap_path_cannot_claim_complete(self):
left = self.node("left")
right = self.node("right")
gap = self.gap(left, right)
path = self.path((left, right), (gap,), complete=True)
with self.assertRaisesRegex(flow.FlowIRError, "complete.*gap"):
flow.FlowDocument(self.snapshot, nodes=(left, right), gaps=(gap,), paths=(path,))
def test_incomplete_path_requires_gap(self):
left = self.node("left")
right = self.node("right")
edge = self.edge(left, right)
path = self.path((left, right), (edge,), complete=False)
with self.assertRaisesRegex(flow.FlowIRError, "incomplete.*gap"):
flow.FlowDocument(self.snapshot, nodes=(left, right), edges=(edge,), paths=(path,))
def test_path_complete_is_strict_boolean(self):
left = self.node("left")
right = self.node("right")
edge = self.edge(left, right)
node_ids = (left.node_id, right.node_id)
segment_ids = (edge.edge_id,)
path_id = flow.flow_path_id(self.snapshot, node_ids, segment_ids)
with self.assertRaisesRegex(flow.FlowIRError, "complete must be boolean"):
flow.FlowPath(self.snapshot, path_id, node_ids, segment_ids, "false") # type: ignore[arg-type]
def test_path_segment_must_connect_exact_adjacent_nodes(self):
one = self.node("one")
two = self.node("two")
three = self.node("three")
wrong = self.edge(one, three)
node_ids = (one.node_id, two.node_id)
segment_ids = (wrong.edge_id,)
path = flow.FlowPath(
self.snapshot,
flow.flow_path_id(self.snapshot, node_ids, segment_ids),
node_ids,
segment_ids,
True,
)
with self.assertRaisesRegex(flow.FlowIRError, "adjacent"):
flow.FlowDocument(self.snapshot, nodes=(one, two, three), edges=(wrong,), paths=(path,))
def test_snapshot_mismatch_and_duplicate_ids_fail_closed(self):
left = self.node("left")
with self.assertRaisesRegex(flow.FlowIRError, "duplicate"):
flow.FlowDocument(self.snapshot, nodes=(left, left))
other = pm.ProgramSnapshot("b" * 64).snapshot_id
foreign = self.node("foreign", snapshot=other)
with self.assertRaisesRegex(flow.FlowIRError, "snapshot mismatch"):
flow.FlowDocument(self.snapshot, nodes=(left, foreign))
def test_hard_count_and_serialized_size_bounds(self):
node = self.node("repeated")
with self.assertRaisesRegex(flow.FlowIRError, "node count"):
flow.FlowDocument(self.snapshot, nodes=(node,) * (flow.MAX_FLOW_NODES + 1))
one = self.node("one", label="x" * 400)
two = self.node("two", label="y" * 400)
with mock.patch.object(flow, "MAX_FLOW_DOCUMENT_BYTES", 512):
with self.assertRaisesRegex(flow.FlowIRError, "serialized size"):
flow.FlowDocument(self.snapshot, nodes=(one, two))
def test_descriptor_is_shared_ir_not_public_operation_or_storage(self):
descriptor = flow.descriptor()
self.assertEqual(descriptor["flow_ir_version"], 1)
self.assertEqual(descriptor["program_model_version"], pm.PROGRAM_MODEL_VERSION)
self.assertEqual(descriptor["durable_concepts"], ["FlowNode", "FlowEdge", "FlowPath", "FlowGap"])
self.assertFalse(descriptor["persistent_flow_storage"])
self.assertFalse(descriptor["public_operation_added"])
self.assertFalse(descriptor["raw_constant_values"])
if __name__ == "__main__":
unittest.main()