-
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathtest_integration.py
More file actions
677 lines (605 loc) · 18.6 KB
/
test_integration.py
File metadata and controls
677 lines (605 loc) · 18.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
"""End-to-end integration tests.
Each test writes a non-trivial Python program, executes it with an empty
config (no transforms) to get the baseline output, then executes the fully
obfuscated version and asserts the output is identical.
Output is captured by overriding ``print`` in the execution namespace.
This works even when ``exec_wrapper`` is active because the inner ``exec``
call inherits the outer globals dict (which contains our fake print), and
``variable_renamer`` and ``dead_code_injector`` never touch the ``print``
name (it is a builtin and therefore excluded from all transforms).
"""
from __future__ import annotations
import textwrap
from python_obfuscator import ObfuscationConfig, obfuscate
def run(source: str, config: ObfuscationConfig | None = None) -> list[str]:
"""Execute *source* and return every string passed to print(), in order."""
captured: list[str] = []
def fake_print(
*args: object, sep: str = " ", end: str = "\n", **_: object
) -> None: # noqa: ARG001
captured.append(sep.join(str(a) for a in args))
cfg = config or ObfuscationConfig.all_enabled()
ns: dict = {"print": fake_print}
exec(obfuscate(source, config=cfg), ns) # noqa: S102
return captured
_BASELINE = ObfuscationConfig.only() # no transforms — plain exec
def check(source: str) -> None:
"""Assert that fully-obfuscated output matches unobfuscated output."""
expected = run(source, config=_BASELINE)
actual = run(source)
assert (
actual == expected
), f"Obfuscated output differs.\nExpected: {expected}\nActual: {actual}"
# ---------------------------------------------------------------------------
# Individual technique passes (useful for isolating regressions)
# ---------------------------------------------------------------------------
class TestIndividualTechniques:
_SRC = textwrap.dedent(
"""\
x = 10
y = 20
print(x + y)
print(x * y)
"""
)
def test_string_hex_encoder_only(self) -> None:
cfg = ObfuscationConfig.only("string_hex_encoder")
assert run(self._SRC, config=_BASELINE) == run(self._SRC, config=cfg)
def test_variable_renamer_only(self) -> None:
cfg = ObfuscationConfig.only("variable_renamer")
assert run(self._SRC, config=_BASELINE) == run(self._SRC, config=cfg)
def test_dead_code_injector_only(self) -> None:
cfg = ObfuscationConfig.only("dead_code_injector")
assert run(self._SRC, config=_BASELINE) == run(self._SRC, config=cfg)
def test_exec_wrapper_only(self) -> None:
cfg = ObfuscationConfig.only("exec_wrapper")
assert run(self._SRC, config=_BASELINE) == run(self._SRC, config=cfg)
# ---------------------------------------------------------------------------
# Full-pipeline scenarios
# ---------------------------------------------------------------------------
class TestArithmetic:
def test_basic_arithmetic(self) -> None:
check(
textwrap.dedent(
"""\
a = 6
b = 7
print(a + b)
print(a * b)
print(a - b)
print(100 // a)
print(100 % b)
"""
)
)
def test_float_arithmetic(self) -> None:
check(
textwrap.dedent(
"""\
x = 3.14
y = 2.0
print(round(x * y, 4))
print(round(x / y, 4))
"""
)
)
def test_chained_operations(self) -> None:
check(
textwrap.dedent(
"""\
result = (1 + 2) * (3 + 4) - (5 * 6) + (7 ** 2)
print(result)
"""
)
)
class TestFunctions:
def test_simple_function(self) -> None:
check(
textwrap.dedent(
"""\
def add(a, b):
return a + b
print(add(3, 4))
print(add(10, -3))
"""
)
)
def test_recursive_fibonacci(self) -> None:
check(
textwrap.dedent(
"""\
def fib(n):
if n <= 1:
return n
return fib(n - 1) + fib(n - 2)
for i in range(8):
print(fib(i))
"""
)
)
def test_default_arguments(self) -> None:
# Keyword-argument call sites use bare strings (ast.keyword.arg), not
# ast.Name nodes, so variable_renamer cannot update them when a parameter
# is renamed. Use positional arguments to keep the test self-consistent
# after renaming.
check(
textwrap.dedent(
"""\
def greet(name, prefix="Hello"):
return prefix + " " + name
print(greet("world"))
print(greet("Python", "Hi"))
"""
)
)
def test_multiple_return_values(self) -> None:
check(
textwrap.dedent(
"""\
def minmax(seq):
return min(seq), max(seq)
lo, hi = minmax([3, 1, 4, 1, 5, 9, 2, 6])
print(lo, hi)
"""
)
)
def test_variadic_args(self) -> None:
check(
textwrap.dedent(
"""\
def total(*nums):
return sum(nums)
print(total(1, 2, 3, 4, 5))
"""
)
)
def test_higher_order_function(self) -> None:
check(
textwrap.dedent(
"""\
def apply(fn, values):
return [fn(v) for v in values]
result = apply(lambda x: x * x, [1, 2, 3, 4])
for v in result:
print(v)
"""
)
)
class TestClosures:
def test_simple_closure(self) -> None:
check(
textwrap.dedent(
"""\
def make_adder(n):
def inner(x):
return x + n
return inner
add5 = make_adder(5)
print(add5(10))
print(add5(0))
"""
)
)
def test_nonlocal(self) -> None:
check(
textwrap.dedent(
"""\
def counter():
count = 0
def inc():
nonlocal count
count += 1
return count
return inc
c = counter()
print(c())
print(c())
print(c())
"""
)
)
class TestClasses:
def test_simple_class(self) -> None:
check(
textwrap.dedent(
"""\
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def distance(self):
return (self.x ** 2 + self.y ** 2) ** 0.5
p = Point(3, 4)
print(p.distance())
"""
)
)
def test_inheritance(self) -> None:
check(
textwrap.dedent(
"""\
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return "..."
class Dog(Animal):
def speak(self):
return "Woof"
class Cat(Animal):
def speak(self):
return "Meow"
for animal in [Dog("Rex"), Cat("Whiskers")]:
print(animal.speak())
"""
)
)
def test_class_method_and_static(self) -> None:
check(
textwrap.dedent(
"""\
class MathHelper:
factor = 2
@classmethod
def double(cls, n):
return n * cls.factor
@staticmethod
def square(n):
return n * n
print(MathHelper.double(5))
print(MathHelper.square(4))
"""
)
)
class TestControlFlow:
def test_for_loop(self) -> None:
check(
textwrap.dedent(
"""\
total = 0
for i in range(1, 6):
total += i
print(total)
"""
)
)
def test_while_loop(self) -> None:
check(
textwrap.dedent(
"""\
n = 1
while n < 32:
n *= 2
print(n)
"""
)
)
def test_nested_for_loops(self) -> None:
check(
textwrap.dedent(
"""\
pairs = []
for i in range(3):
for j in range(3):
if i != j:
pairs.append((i, j))
print(len(pairs))
"""
)
)
def test_if_elif_else(self) -> None:
check(
textwrap.dedent(
"""\
def classify(n):
if n < 0:
return "negative"
elif n == 0:
return "zero"
else:
return "positive"
for v in [-5, 0, 7]:
print(classify(v))
"""
)
)
def test_break_and_continue(self) -> None:
check(
textwrap.dedent(
"""\
evens = []
for i in range(20):
if i % 2 != 0:
continue
if i > 10:
break
evens.append(i)
print(evens)
"""
)
)
class TestDataStructures:
def test_list_comprehension(self) -> None:
check(
textwrap.dedent(
"""\
squares = [x * x for x in range(6)]
print(squares)
"""
)
)
def test_dict_comprehension(self) -> None:
check(
textwrap.dedent(
"""\
word = "hello"
freq = {c: word.count(c) for c in set(word)}
for key in sorted(freq):
print(key, freq[key])
"""
)
)
def test_set_operations(self) -> None:
check(
textwrap.dedent(
"""\
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
print(sorted(a & b))
print(sorted(a | b))
print(sorted(a - b))
"""
)
)
def test_nested_list(self) -> None:
check(
textwrap.dedent(
"""\
matrix = [[i * j for j in range(1, 4)] for i in range(1, 4)]
for row in matrix:
print(row)
"""
)
)
class TestExceptionHandling:
def test_try_except(self) -> None:
check(
textwrap.dedent(
"""\
def safe_div(a, b):
try:
return a // b
except ZeroDivisionError:
return None
print(safe_div(10, 2))
print(safe_div(10, 0))
"""
)
)
def test_try_except_finally(self) -> None:
check(
textwrap.dedent(
"""\
log = []
try:
log.append("try")
x = 1 // 0
except ZeroDivisionError:
log.append("except")
finally:
log.append("finally")
print(log)
"""
)
)
def test_multiple_except_clauses(self) -> None:
check(
textwrap.dedent(
"""\
def parse_int(s):
try:
return int(s)
except (ValueError, TypeError):
return -1
print(parse_int("42"))
print(parse_int("abc"))
print(parse_int(None))
"""
)
)
def test_try_else(self) -> None:
check(
textwrap.dedent(
"""\
results = []
for val in ["10", "x", "5"]:
try:
n = int(val)
except ValueError:
results.append("bad")
else:
results.append(n * 2)
print(results)
"""
)
)
class TestGenerators:
def test_generator_function(self) -> None:
check(
textwrap.dedent(
"""\
def countdown(n):
while n > 0:
yield n
n -= 1
print(list(countdown(5)))
"""
)
)
def test_generator_expression(self) -> None:
check(
textwrap.dedent(
"""\
gen = (x * x for x in range(5))
print(list(gen))
"""
)
)
class TestStringOperations:
def test_string_formatting(self) -> None:
check(
textwrap.dedent(
"""\
name = "world"
n = 42
print(f"Hello, {name}!")
print(f"The answer is {n}.")
"""
)
)
def test_string_methods(self) -> None:
check(
textwrap.dedent(
"""\
s = "Hello, World!"
print(s.lower())
print(s.upper())
print(s.replace("World", "Python"))
print(s.split(", "))
"""
)
)
def test_multiline_string(self) -> None:
check(
textwrap.dedent(
"""\
text = "line one\\nline two\\nline three"
lines = text.split("\\n")
for line in lines:
print(line)
"""
)
)
def test_string_with_quotes(self) -> None:
check(
textwrap.dedent(
"""\
s1 = "it's a test"
s2 = 'say "hello"'
print(s1)
print(s2)
"""
)
)
class TestComplexPrograms:
def test_bubble_sort(self) -> None:
check(
textwrap.dedent(
"""\
def bubble_sort(lst):
n = len(lst)
for i in range(n):
for j in range(n - i - 1):
if lst[j] > lst[j + 1]:
lst[j], lst[j + 1] = lst[j + 1], lst[j]
return lst
data = [64, 34, 25, 12, 22, 11, 90]
print(bubble_sort(data))
"""
)
)
def test_binary_search(self) -> None:
check(
textwrap.dedent(
"""\
def binary_search(lst, target):
lo, hi = 0, len(lst) - 1
while lo <= hi:
mid = (lo + hi) // 2
if lst[mid] == target:
return mid
elif lst[mid] < target:
lo = mid + 1
else:
hi = mid - 1
return -1
arr = list(range(0, 20, 2))
print(binary_search(arr, 8))
print(binary_search(arr, 7))
"""
)
)
def test_class_with_dunder_methods(self) -> None:
check(
textwrap.dedent(
"""\
class Stack:
def __init__(self):
self._data = []
def push(self, item):
self._data.append(item)
def pop(self):
return self._data.pop()
def __len__(self):
return len(self._data)
def __repr__(self):
return repr(self._data)
s = Stack()
s.push(1)
s.push(2)
s.push(3)
print(len(s))
print(s.pop())
print(len(s))
"""
)
)
def test_functional_pipeline(self) -> None:
check(
textwrap.dedent(
"""\
from functools import reduce
numbers = list(range(1, 11))
evens = list(filter(lambda x: x % 2 == 0, numbers))
doubled = list(map(lambda x: x * 2, evens))
total = reduce(lambda a, b: a + b, doubled)
print(evens)
print(doubled)
print(total)
"""
)
)
def test_deeply_nested_closures(self) -> None:
check(
textwrap.dedent(
"""\
def level1(a):
def level2(b):
def level3(c):
return a + b + c
return level3
return level2
fn = level1(1)(2)
print(fn(3))
print(fn(10))
"""
)
)
def test_memoisation_with_dict(self) -> None:
check(
textwrap.dedent(
"""\
cache = {}
def fib(n):
if n in cache:
return cache[n]
if n <= 1:
cache[n] = n
else:
cache[n] = fib(n - 1) + fib(n - 2)
return cache[n]
for i in [0, 1, 5, 10, 15]:
print(fib(i))
"""
)
)