forked from dashingsoft/pyarmor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction-test.sh
executable file
·2160 lines (1775 loc) · 74.6 KB
/
function-test.sh
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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
source test-header.sh
# ======================================================================
#
# Initial setup.
#
# ======================================================================
PYARMOR="${PYTHON} pyarmor.py"
csih_inform "Python is $PYTHON"
csih_inform "Tested Package: $pkgfile"
csih_inform "PyArmor is $PYARMOR"
csih_inform "Make workpath ${workpath}"
rm -rf ${workpath}
mkdir -p ${workpath} || csih_error "Make workpath FAILED"
csih_inform "Clean pyarmor data"
rm -rf ~/.pyarmor ~/.pyarmor_capsule.*
PYARMOR_DATA=~/.pyarmor
if [[ "${PLATFORM}" == "win_amd64" && "${PYTHON}" == *Python38*/python ]] ; then
[[ -n "$USERPROFILE" ]] && rm -rf "$USERPROFILE\\.pyarmor*" \
&& PYARMOR_DATA=$USERPROFILE\\.pyarmor
fi
csih_inform "PyArmor data at ${PYARMOR_DATA}"
[[ -d data ]] || csih_error "No path 'data' found in current path"
datapath=$(pwd)/data
cd ${workpath}
[[ ${pkgfile} == *.zip ]] && unzip ${pkgfile} > /dev/null 2>&1
[[ ${pkgfile} == *.tar.bz2 ]] && tar xjf ${pkgfile}
cd pyarmor-$version || csih_error "Invalid pyarmor package file"
# From pyarmor 3.5.1, main scripts are moved to src
[[ -d src ]] && mv src/* ./
# Fix issue: assert_builtin(open) fails in python 3.0
patch_cross_protection_code_for_python3.0
# From pyarmor 4.5.4, platform name is renamed
# From pyarmor 5.7.5, platform name is changed
csih_inform "Add execute permission to dynamic library"
find ./platforms -name _pytransform.dll -exec chmod +x {} \;
csih_inform "Make path test/data"
mkdir -p test/data
csih_inform "Copy test files from ${datapath} to ./test/data"
cp ${datapath}/*.py test/data
cp -a ${datapath}/sound test/data
cp ${datapath}/project.zip test/data
cp ${datapath}/project.zip test/data/project-orig.zip
if [[ "${PLATFORM}" != "win32" ]] ; then
csih_inform "Make link to platforms for super mode"
if [[ "OK" == $($PYTHON -c'from sys import version_info as ver, stdout
stdout.write("OK" if (ver[0] * 100 + ver[1]) in (207, 307, 308, 309, 310) else "")') ]] ; then
SUPERMODE=yes
mkdir -p ${PYARMOR_DATA}/platforms
PLATFORM_INDEX=${PYARMOR_DATA}/platforms/index.json
csih_inform "Copy platform index.json from pyarmor-core"
cp ../../../../pyarmor-core/platforms/index.json ${PLATFORM_INDEX}
(cd ${PYARMOR_DATA}/platforms;
for x in ${PYARMOR_CORE_PLATFORM}/*.py?? ; do
name=$(basename ${x})
name=${name//./\/}
mkdir -p ${name}
if [[ "${PLATFORM}" == "win_amd64" ]] ; then
for y in ${x}/pytransform.* ; do
[[ -f "${y}" ]] && cp ${y} ${name}
done
else
ln -s ${x}/pytransform.* ${name}/
fi
update_pytransform_hash256 ${PLATFORM_INDEX} ${x}/pytransform.* $(basename ${x})
done)
fi
if [[ "${PLATFORM}" == "macosx_x86_64" ]] ; then
for minor in 9 10 ; do
name="pytransform.cpython-3${minor}-darwin.so"
src="${PYARMOR_CORE_PLATFORM}/darwin.x86_64.11.py3${minor}/$name"
dest="${PYARMOR_DATA}/platforms/darwin/x86_64/11/py3${minor}/$name"
rm -f $dest
mkdir -p $(dirname $dest)
cp $src $dest
install_name_tool \
-change @rpath/Frameworks/Python.framework/Versions/3.${minor}/Python \
/Users/jondy/workspace/pytransform/python/lib/libpython3.${minor}.dylib \
$dest
update_pytransform_hash256 ${PLATFORM_INDEX} $dest darwin.x86_64.11.py3${minor}
done
fi
csih_inform "Super mode test is ${SUPERMODE}"
fi
csih_inform "Prepare for function testing"
echo ""
# ======================================================================
#
# Bootstrap: help and version
#
# ======================================================================
echo ""
echo "-------------------- Bootstrap ---------------------------------"
echo ""
csih_inform "1. show help and import pytransform"
$PYARMOR --help >result.log 2>&1 || csih_bug "Case 1.1 FAILED"
csih_inform "2. show version information"
$PYARMOR --version >result.log 2>&1 || csih_bug "show version FAILED"
echo ""
echo "-------------------- Bootstrap End -----------------------------"
echo ""
# ======================================================================
#
# Command: init, config, licenses, obfuscate
#
# ======================================================================
echo ""
echo "-------------------- Command ---------------------------------"
echo ""
csih_inform "C-1. Test option --capsule for init"
$PYARMOR init --src=examples/simple --capsule=test/data/project.zip \
projects/test-capsule >result.log 2>&1
check_return_value
csih_inform "C-2. Test option --capsule for config"
cp test/data/project.zip projects/test-capsule/project2.zip
$PYARMOR config --capsule=project2.zip projects/test-capsule >result.log 2>&1
check_return_value
csih_inform "C-3. Test option --capsule for obfuscate"
$PYARMOR init --src=examples/simple test-capsule >result.log 2>&1
$PYARMOR obfuscate --capsule=test-capsule/.pyarmor-capsule.zip \
--output=dist-capsule examples/simple/queens.py \
>result.log 2>&1
check_file_exists dist-capsule/queens.py
check_file_content dist-capsule/queens.py '__pyarmor__(__name__'
(cd dist-capsule; $PYTHON queens.py >result.log 2>&1 )
check_return_value
check_file_content dist-capsule/result.log 'Found 92 solutions'
csih_inform "C-4. Test command licenses"
cp test/data/project.zip .pyarmor_capsule.zip
$PYARMOR licenses --expired=2018-05-12 Customer-Jordan >result.log 2>&1
check_file_exists licenses/Customer-Jordan/license.lic
$PYARMOR licenses --capsule=test/data/project.zip --output=projects/licenses \
--expired=2018-05-12 Customer-Jordan >result.log 2>&1
check_file_exists projects/licenses/Customer-Jordan/license.lic
$PYARMOR init --src=examples/simple --entry=queens.py \
projects/test-licenses >result.log 2>&1
$PYARMOR licenses --project=projects/test-licenses \
--expired=2018-05-12 Customer-Jordan >result.log 2>&1
check_file_exists projects/test-licenses/licenses/Customer-Jordan/license.lic
$PYARMOR licenses --project=projects/test-licenses \
-e 2018-05-12 --bind-disk '100304PBN2081SF3NJ5T' \
Customer-Tom >result.log 2>&1
$PYARMOR licenses --project=projects/test-licenses \
-e 2018-05-12 Customer-A Customer-B Customer-C >result.log 2>&1
check_file_exists projects/test-licenses/licenses/Customer-A/license.lic
check_file_exists projects/test-licenses/licenses/Customer-B/license.lic
check_file_exists projects/test-licenses/licenses/Customer-C/license.lic
csih_inform "C-5. Test option --bind-file for licenses"
$PYARMOR init --src=examples/simple --entry queens.py \
projects/test-bind-file >result.log 2>&1
check_return_value
cat <<EOF > projects/test-bind-file/id_rsa
-----BEGIN RSA PRIVATE KEY-----
-----END RSA PRIVATE KEY-----
EOF
(cd projects/test-bind-file && $ARMOR build >result.log 2>&1 &&
$ARMOR licenses --bind-file="id_rsa;id_rsa" \
TESTER >result.log 2>&1 &&
cd dist && cp ../id_rsa ./ &&
cp ../licenses/TESTER/license.lic ./ &&
$PYTHON queens.py >result.log 2>&1 )
check_return_value
check_file_content projects/test-bind-file/dist/result.log 'Found 92 solutions'
csih_inform "C-6. Test option --recursive for obfuscate"
rm .pyarmor_capsule.zip
$PYARMOR obfuscate --recursive -O dist-recursive \
examples/simple/queens.py >result.log 2>&1
check_return_value
(cd dist-recursive; $PYTHON queens.py >result.log 2>&1 )
check_return_value
check_file_content dist-recursive/result.log 'Found 92 solutions'
csih_inform "C-7. Test no entry script for obfuscate"
$PYARMOR obfuscate -O test-no-entry examples/simple >result.log 2>&1
check_return_value
check_file_exists test-no-entry/queens.py
csih_inform "C-8. Test 'gbk' codec for obfuscate"
if [[ "$PYTHON" == C:/Python30/python || "$PYTHON" == *python3.0 ||
( "$PLATFORM" == "macosx_x86_64" && "$PYTHON" == *python ) ||
( "$PLATFORM" == "macosx_x86_64" && "$PYTHON" == *python2.7 ) ]] ; then
csih_inform "This testcase is ignored in Python 3.0, and Python2 in MacOS"
else
mkdir -p test-codec
cp test/data/gbk.py test-codec
$PYARMOR obfuscate -O dist-codec test-codec/gbk.py >result.log 2>&1
check_return_value
check_file_exists dist-codec/gbk.py
(cd dist-codec; $PYTHON gbk.py >result.log 2>&1 )
check_return_value
check_file_content dist-codec/result.log 'PyArmor'
fi
csih_inform "C-9. Test --upgrade for capsule (ignored)"
# mkdir -p test-upgrade
# cp test/data/project-orig.zip test-upgrade/.pyarmor_capsule.zip
# $PYARMOR capsule --upgrade ./test-upgrade/ >result.log 2>&1
# check_return_value
# check_file_content result.log "Upgrade capsule OK"
# (cd test-upgrade; unzip .pyarmor_capsule.zip >result.log 2>&1)
# check_file_exists test-upgrade/pytransform.key
csih_inform "C-10. Test output == src for obfuscate"
mkdir -p abc
$PYARMOR obfuscate -O abc abc >result.log 2>&1
check_file_content result.log "Output path can not be same as src"
csih_inform "C-11. Test output is sub-directory of src for obfuscate"
cp -a examples/simple test-subpath
(cd test-subpath;
$PYTHON ../pyarmor.py obfuscate -O output . >result.log 2>&1)
check_return_value
check_file_exists test-subpath/output/queens.py
csih_inform "C-12. Test utf-8 with BOM for obfuscate"
mkdir -p test-utf8bom
cp test/data/utf8bom.py test-utf8bom
$PYARMOR obfuscate -O dist-utf8bom test-utf8bom/utf8bom.py >result.log 2>&1
check_return_value
check_file_exists dist-utf8bom/utf8bom.py
(cd dist-utf8bom; $PYTHON utf8bom.py >result.log 2>&1 )
check_return_value
check_file_content dist-utf8bom/result.log 'PyArmor'
csih_inform "C-13. Test option --exclude for obfuscate"
$PYARMOR obfuscate -O test-exclude -r --exclude "mypkg,dist" \
examples/testpkg/main.py >result.log 2>&1
check_return_value
check_file_exists test-exclude/main.py
check_file_not_exists test-exclude/mypkg/foo.py
csih_inform "C-14. Test option --exact for obfuscate"
$PYARMOR obfuscate -O test-exact --exact \
examples/testpkg/mypkg/foo.py >result.log 2>&1
check_return_value
check_file_exists test-exact/foo.py
check_file_not_exists test-exact/__init__.py
csih_inform "C-15. Test package for obfuscate"
$PYARMOR obfuscate -O test-package/mypkg \
examples/testpkg/mypkg/__init__.py >result.log 2>&1
check_return_value
check_file_exists test-package/mypkg/foo.py
check_file_exists test-package/mypkg/__init__.py
(cd test-package; $PYTHON -c "import mypkg" >result.log 2>&1 )
check_return_value
csih_inform "C-16. Test option --no-bootstrap for obfuscate"
$PYARMOR obfuscate -O test-no-bootstrap --no-bootstrap \
examples/testpkg/main.py >result.log 2>&1
check_return_value
check_file_exists test-no-bootstrap/main.py
check_file_content test-no-bootstrap/main.py "pyarmor_runtime" not
csih_inform "C-17. Test option --no-cross-protection for obfuscate"
$PYARMOR obfuscate -O test-no-cross-protection --no-cross-protection \
examples/testpkg/main.py >result.log 2>&1
check_return_value
check_file_exists test-no-cross-protection/main.py
csih_inform "C-18. Test option --plugin for obfuscate"
echo "# {PyArmor Plugins}" > test_plugin.py
echo "print('Hello Plugin')" > plugin_hello.py
$PYARMOR obfuscate -O dist-plugin --plugin "plugin_hello" \
--exact test_plugin.py >result.log 2>&1
check_return_value
(cd dist-plugin; $PYTHON test_plugin.py >result.log 2>&1 )
check_return_value
check_file_content dist-plugin/result.log 'Hello Plugin'
csih_inform "C-19. Test option --plugin with special value 'on'"
mkdir test-plugins
echo "# PyArmor Plugin: print('Hello World')" > test-plugins/foo.py
$PYARMOR obfuscate -O dist-plugin2 --plugin on \
--exact test-plugins/foo.py >result.log 2>&1
check_return_value
(cd dist-plugin2; $PYTHON foo.py >result.log 2>&1 )
check_return_value
check_file_content dist-plugin2/result.log 'Hello World'
csih_inform "C-20. Test absolute jump critical value in wrap mode"
$PYARMOR obfuscate -O dist-pop-jmp --exact test/data/pop_jmp.py >result.log 2>&1
check_return_value
(cd dist-pop-jmp; $PYTHON pop_jmp.py >result.log 2>&1 )
check_return_value
check_file_content dist-pop-jmp/result.log 'jmp_simple return 3'
check_file_content dist-pop-jmp/result.log 'jmp_short return 30'
csih_inform "C-21. Test last argument is directory for obfuscate"
$PYARMOR obfuscate -O test-path-1 examples/simple >result.log 2>&1
check_return_value
check_file_exists test-path-1/queens.py
csih_inform "C-22. Test more than one path for obfuscate"
$PYARMOR obfuscate examples/simple examples/testpkg >result.log 2>&1
check_file_content result.log "Only one path is allowed"
csih_inform "C-23. Test option --bind-data for licenses"
$PYARMOR licenses -x 20191011 test-bind-data >result.log 2>&1
check_return_value
check_file_exists licenses/test-bind-data/license.lic
check_file_content licenses/test-bind-data/license.lic.txt 'test-bind-data;20191011'
csih_inform "C-24. Test option --package-runtime=1 for obfuscate"
$PYARMOR obfuscate --package-runtime 1 --output test-package-runtime \
examples/simple/queens.py >result.log 2>&1
check_return_value
check_file_exists test-package-runtime/pytransform/__init__.py
check_file_content test-package-runtime/pytransform/__init__.py 'def init_runtime'
(cd test-package-runtime; $PYTHON queens.py >result.log 2>&1 )
check_return_value
check_file_content test-package-runtime/result.log 'Found 92 solutions'
(cd test-package-runtime; mkdir another; mv pytransform another;
PYTHONPATH=another $PYTHON queens.py >result2.log 2>&1 )
check_return_value
check_file_content test-package-runtime/result2.log 'Found 92 solutions'
csih_inform "C-25. Test option --bootstrap=2 for obfuscate"
$PYARMOR obfuscate --package-runtime 1 --bootstrap 2 --output test-bootstrap2 \
examples/testpkg/mypkg/__init__.py >result.log 2>&1
check_return_value
check_file_exists test-bootstrap2/pytransform/__init__.py
check_file_content test-bootstrap2/pytransform/__init__.py 'def init_runtime'
check_file_exists test-bootstrap2/__init__.py
check_file_content test-bootstrap2/__init__.py 'from pytransform import pyarmor_runtime'
csih_inform "C-25. Test command runtime with default option"
$PYARMOR runtime > result.log 2>&1
check_return_value
check_file_exists dist/pytransform/__init__.py
csih_inform "C-26. Test command obfuscation with --src"
$PYARMOR obfuscate -r --src examples/testpkg -O test-src-path \
mypkg/foo.py > result.log 2>&1
check_return_value
check_file_exists test-src-path/main.py
check_file_exists test-src-path/mypkg/foo.py
check_file_exists test-src-path/mypkg/__init__.py
csih_inform "C-27. Test no very long line in traceback"
echo "raise Exception('Elinimate long line from traceback')" > e.py
$PYARMOR obfuscate --exact -O test-exception e.py > result.log 2>&1
check_return_value
check_file_exists test-exception/e.py
(cd test-exception; $PYTHON e.py >result.log 2>&1 )
check_file_content test-exception/result.log 'Elinimate long line from traceback'
check_file_content test-exception/result.log '__pyarmor__' not
csih_inform "C-28. Test option --bootstrap=3 for obfuscate"
$PYARMOR obfuscate --package-runtime 1 --bootstrap 3 --output test-bootstrap3 \
examples/simple/queens.py >result.log 2>&1
check_return_value
check_file_exists test-bootstrap3/pytransform/__init__.py
check_file_content test-bootstrap3/pytransform/__init__.py 'def init_runtime'
check_file_exists test-bootstrap3/queens.py
check_file_content test-bootstrap3/queens.py 'from .pytransform import pyarmor_runtime'
csih_inform "C-29. Test option --exclude .py file for obfuscate"
$PYARMOR obfuscate -O test-exclude3 -r --exclude "mypkg/foo.py" \
examples/testpkg/main.py >result.log 2>&1
check_return_value
check_file_exists test-exclude3/main.py
check_file_not_exists test-exclude3/mypkg/foo.py
csih_inform "C-30. Test many entry scripts in exact mode"
echo "" > a.py
echo "" > b.py
echo "" > c.py
output=test-many-scripts
$PYARMOR obfuscate --exact -O $output a.py b.py > result.log 2>&1
check_return_value
check_file_exists $output/a.py
check_file_exists $output/b.py
check_file_not_exists $output/c.py
check_file_content $output/a.py 'import pyarmor_runtime'
check_file_content $output/b.py 'import pyarmor_runtime'
csih_inform "C-31. Output license key to stdout"
$PYARMOR licenses --output stdout reg0001 > result.log 2>&1
check_return_value
check_file_content result.log 'Generate 1 licenses OK.'
csih_inform "C-32. Test multiple option --exclude for obfuscate"
$PYARMOR obfuscate -O test-exclude2 -r --exclude mypkg --exclude dist \
examples/testpkg/main.py >result.log 2>&1
check_return_value
check_file_exists test-exclude2/main.py
check_file_not_exists test-exclude2/mypkg/foo.py
csih_inform "C-33. Generate license with epoch expired"
$PYARMOR licenses --expired 1579316961.2 reg-epoch-1 > result.log 2>&1
check_return_value
check_file_content result.log 'Generate 1 licenses OK.'
check_file_exists licenses/reg-epoch-1/license.lic
csih_inform "C-34. Test obfuscate script in the non-ascii path"
propath=examples/中文路径
mkdir $propath
cp examples/simple/queens.py $propath
$PYARMOR obfuscate -O test-non-ascii-path $propath/queens.py > result.log 2>&1
check_return_value
check_file_exists test-non-ascii-path/queens.py
(cd test-non-ascii-path; $PYTHON queens.py >result.log 2>&1 )
check_return_value
check_file_content test-non-ascii-path/result.log 'Found 92 solutions'
csih_inform "C-35. Test licenses with --fixed"
if [[ "${PYTHON}" == *Python??-32/python ]] ; then
csih_inform "Ignore this case for $PYTHON"
else
propath=test-fixed-key
mkdir $propath
cp examples/simple/queens.py $propath
$PYARMOR licenses --fixed 1 -O $propath/a/license.lic > result.log 2>&1
$PYARMOR licenses --fixed 101 -O $propath/b/license.lic > result.log 2>&1
$PYARMOR obfuscate -O $propath/dist --with-license $propath/a/license.lic \
$propath/queens.py > result.log 2>&1
(cd $propath/dist; $PYTHON queens.py >result.log 2>&1 )
check_return_value
check_file_content $propath/dist/result.log 'Found 92 solutions'
$PYARMOR obfuscate -O $propath/dist --with-license $propath/b/license.lic \
$propath/queens.py > result.log 2>&1
(cd $propath/dist; $PYTHON queens.py >result.log 2>&1 )
check_file_content $propath/dist/result.log 'License is not for this machine'
check_file_content $propath/dist/result.log 'Found 92 solutions' not
fi
csih_inform "C-36. Test got default capsule from old location"
(cd "${PYARMOR_DATA}"; mv .pyarmor_capsule.zip ..)
$PYARMOR obfuscate -O test-default-capsule examples/simple/queens.py >result.log 2>&1
check_return_value
(cd "${PYARMOR_DATA}" && check_file_exists .pyarmor_capsule.zip)
csih_inform "C-37. Test customized cross protection script"
echo "print('This is customized protection code')" > protection.py
dist=test-c-36
$PYARMOR obfuscate -O $dist --cross-protection protection.py \
examples/simple/queens.py >result.log 2>&1
check_return_value
(cd $dist; $PYTHON queens.py >result.log 2>&1)
check_return_value
check_file_content $dist/result.log 'This is customized protection code'
csih_inform "C-38. Test obfuscate command with obf-mod is 2"
dist=test-obf-mod-2
$PYARMOR obfuscate -O $dist --obf-mod 2 examples/simple/queens.py >result.log 2>&1
check_return_value
(cd $dist; $PYTHON queens.py >result.log 2>&1)
check_return_value
check_file_content $dist/result.log 'Found 92 solutions'
csih_inform "C-39. Test obfuscate command with inner license"
dist=test-with-license-2
$PYARMOR licenses -e 2020-05-02 r001 >result.log 2>&1
$PYARMOR obfuscate -O $dist --with-license licenses/r001/license.lic \
examples/simple/queens.py >result.log 2>&1
check_return_value
(cd $dist; $PYTHON queens.py >result.log 2>&1)
check_file_content $dist/result.log 'Found 92 solutions' not
check_file_content $dist/result.log 'License is expired'
csih_inform "C-40. Test obfuscate command with special license outer"
dist=test-outer-license
$PYARMOR obfuscate -O $dist --with-license outer examples/simple/queens.py >result.log 2>&1
check_return_value
(cd $dist; $PYTHON queens.py >result.log 2>&1)
check_file_content $dist/result.log 'Found 92 solutions' not
check_file_content $dist/result.log 'Read file license.lic failed'
cp licenses/r001/license.lic $dist
(cd $dist; $PYTHON queens.py >result.log 2>&1)
check_file_content $dist/result.log 'Found 92 solutions' not
check_file_content $dist/result.log 'License is expired'
csih_inform "C-41. Test obfuscate command with --runtime"
dist=test-runtime-1
$PYARMOR runtime -O $dist/runtime >result.log 2>&1
$PYARMOR obfuscate -O $dist/dist --runtime $dist/runtime examples/simple/queens.py >result.log 2>&1
check_return_value
(cd $dist; $PYTHON dist/queens.py >result.log 2>&1)
check_file_content $dist/result.log 'Found 92 solutions'
csih_inform "C-42. Test obfuscate command with --runtime and outer license"
dist=test-runtime-2
$PYARMOR runtime -O $dist/runtime --with-license outer >result.log 2>&1
$PYARMOR obfuscate -O $dist/dist --runtime $dist/runtime examples/simple/queens.py >result.log 2>&1
check_return_value
(cd $dist; $PYTHON dist/queens.py >result.log 2>&1)
check_file_content $dist/result.log 'Found 92 solutions' not
$PYARMOR licenses -O $dist/dist/pytransform/license.lic >result.log 2>&1
check_file_exists $dist/dist/pytransform/license.lic
(cd $dist; $PYTHON dist/queens.py >result.log 2>&1)
check_file_content $dist/result.log 'Found 92 solutions'
csih_inform "C-43. Test entry script is .pyw"
dist=test-c-43
echo "print('Hello')" > foo.pyw
$PYARMOR obfuscate -O $dist --exact foo.pyw >result.log 2>&1
check_return_value
check_file_exists $dist/foo.pyw
check_file_content $dist/foo.pyw 'from pytransform import pyarmor_runtime'
csih_inform "C-44. Test __del__ works for non-super mode"
if ! [[ "yes" == "${SUPERMODE}" ]] ; then
dist=test-c-44
$PYARMOR obfuscate --exact -O $dist test/data/foo__del.py >result.log 2>&1
check_return_value
(cd $dist; $PYTHON foo__del.py >result.log 2>&1)
check_return_value
check_file_content $dist/result.log "test __del__ OK"
fi
csih_inform "C-45. Test outer license with sys.PYARMOR_LICENSE"
dist=test-c-45
echo "print('Outer license OK')" > foo-c-45.py
$PYARMOR obfuscate --exact -O $dist --with-license outer \
foo-c-45.py >result.log 2>&1
check_return_value
$PYARMOR licenses -O $dist --restrict 0 r001 >result.log 2>&1
check_file_exists $dist/r001/license.lic
echo "import sys" > $dist/foo.py
echo "sys.PYARMOR_LICENSE = 'r001/license.lic'" >> $dist/foo.py
cat $dist/foo-c-45.py >> $dist/foo.py
(cd $dist; $PYTHON foo.py >result.log 2>&1)
check_return_value
check_file_content $dist/result.log "Outer license OK"
echo ""
echo "-------------------- Command End -----------------------------"
echo ""
# ======================================================================
#
# Super mode
#
# ======================================================================
if [[ "yes" == "${SUPERMODE}" ]] ; then
echo ""
echo "-------------------- Super Mode ----------------------------"
echo ""
cat <<EOF > sufoo.py
def empty():
pass
def hello(n):
empty()
print('Hello Super Mode: %s' % n)
hello(2)
EOF
csih_inform "S-1. Test basic function of super mode"
dist=dist-super-mode-1
$PYARMOR obfuscate --exact --advanced 2 -O $dist sufoo.py >result.log 2>&1
check_return_value
(cd $dist; $PYTHON sufoo.py >result.log 2>&1)
check_return_value
check_file_content $dist/result.log "Hello Super Mode:"
csih_inform "S-2. Test super mode with jump header"
dist=dist-super-mode-2
$PYARMOR obfuscate --exact --advanced 2 -O $dist test/data/no_wrap.py >result.log 2>&1
check_return_value
(cd $dist; $PYTHON no_wrap.py >result.log 2>&1)
check_return_value
check_file_content $dist/result.log "Test no wrap obfuscate mode: OK"
csih_inform "S-3. Test super mode with recursive function"
dist=dist-super-mode-3
$PYARMOR obfuscate --exact --advanced 2 -O $dist examples/simple/queens.py >result.log 2>&1
check_return_value
(cd $dist; $PYTHON queens.py >result.log 2>&1)
check_return_value
check_file_content $dist/result.log "Found 92 solutions"
csih_inform "S-4. Test super mode with obf-code 2"
dist=dist-super-mode-4
$PYARMOR obfuscate --exact --advanced 2 --obf-code 2 -O $dist sufoo.py >result.log 2>&1
check_return_value
(cd $dist; $PYTHON sufoo.py >result.log 2>&1)
check_return_value
check_file_content $dist/result.log "Hello Super Mode:"
csih_inform "S-5. Generate runtime files with super mode"
dist=dist-super-mode-5
$PYARMOR runtime -O $dist --super-mode >result.log 2>&1
check_return_value
check_file_exists $dist/pytransform_protection.py
csih_inform "S-6. Test super mode with auto patch"
dist=dist-super-mode-6
cat <<EOF > sufoo2.py
# Test auto patch
import threading
from time import sleep
def foo(n):
'''This is a test function'''
while True:
print('Super Mode: %s' % n)
break
k = 0
n += 1
n += 1
n += 1
n += 1
n += 1
n += 1
n += 1
n += 1
n += 1
n += 1
n += 1
n += 1
n += 1
n += 1
n += 1
n += 1
n += 1
n += 1
n += 1
n += 1
n += 1
n += 1
n += 1
n += 1
n += 1
if n > 3:
print('n is %s' % n)
sleep(0.5)
n += 1
n += 1
print('At last the value is %s' % n)
for i in range(3):
threading.Thread(target=foo, args=(i,)).start()
EOF
$PYARMOR obfuscate --exact --advanced 2 -O $dist sufoo2.py >result.log 2>&1
check_return_value
(cd $dist; $PYTHON sufoo2.py >result.log 2>&1)
check_return_value
check_file_content $dist/result.log "Super Mode: 2"
check_file_content $dist/result.log "n is 25"
check_file_content $dist/result.log "n is 26"
check_file_content $dist/result.log "n is 27"
check_file_content $dist/result.log "At last the value is 28"
csih_inform "S-7. Test super mode with clean_str"
dist=dist-super-mode-7
cat <<EOF > suclean.py
from sys import version_info as ver
from pytransform import clean_str
data = ('a' * 30) if ver.major == 2 else (b'a' * 30).decode()
print('Clean data: %s' % clean_str(data))
print(data)
EOF
$PYARMOR obfuscate --exact --advanced 2 -O $dist suclean.py >result.log 2>&1
check_return_value
(cd $dist; $PYTHON suclean.py >result.log 2>&1)
check_return_value
check_file_content $dist/result.log "Clean data: 30"
check_file_content $dist/result.log "aaaaaaaaaa" not
csih_inform "S-8. Test super mode with obf_mod = 2"
dist=dist-super-mode-8
$PYARMOR obfuscate --exact --advanced 2 -O $dist --obf-mod 2 \
examples/simple/queens.py >result.log 2>&1
check_return_value
(cd $dist; $PYTHON queens.py >result.log 2>&1)
check_return_value
check_file_content $dist/result.log "Found 92 solutions"
csih_inform "S-9. Test super mode with jump header and obf_code 2"
dist=dist-super-mode-9
$PYARMOR obfuscate --exact --advanced 2 --obf-code 2 -O $dist \
test/data/no_wrap.py >result.log 2>&1
check_return_value
(cd $dist; $PYTHON no_wrap.py >result.log 2>&1)
check_return_value
check_file_content $dist/result.log "Test no wrap obfuscate mode: OK"
csih_inform "S-10. Test special wrap and obf-code is 2"
dist=test-special-wrap-obf-code-2
$PYARMOR obfuscate --exact --advanced 2 --obf-code 2 -O $dist \
test/data/no_wrap.py >result.log 2>&1
check_return_value
(cd $dist; $PYTHON no_wrap.py >result.log 2>&1)
check_return_value
check_file_content $dist/result.log "Test no wrap obfuscate mode: OK"
csih_inform "S-11. Test sub-package with relative import"
dist=test-super-mode-11
mkdir -p $dist/src/pkg/a
echo "" > $dist/src/pkg/__init__.py
echo "" > $dist/src/pkg/a/__init__.py
$PYARMOR obfuscate -r --advanced 2 -O $dist/pkg --bootstrap 3 \
$dist/src/pkg/__init__.py >result.log 2>&1
check_return_value
check_file_content $dist/pkg/__init__.py "from .pytransform import pyarmor"
check_file_content $dist/pkg/a/__init__.py "from ..pytransform import pyarmor"
$PYARMOR obfuscate -r --advanced 2 -O $dist/dist --bootstrap 3 \
$dist/src/pkg/__init__.py >result.log 2>&1
check_return_value
check_file_content $dist/dist/__init__.py "from .pytransform import pyarmor"
check_file_content $dist/dist/a/__init__.py "from ..pytransform import pyarmor"
csih_inform "S-12. Test builtin functions locals() and eval()"
dist=test-super-mode-12
cat <<EOF > sulocals.py
def test_locals():
x = 2
x = eval('x + 2')
d = locals()
print('Local x is %s' % d['x'])
test_locals()
EOF
$PYARMOR obfuscate --exact --advanced 2 -O $dist sulocals.py >result.log 2>&1
check_return_value
(cd $dist; $PYTHON sulocals.py >result.log 2>&1)
check_return_value
check_file_content $dist/result.log "Local x is 4"
csih_inform "S-13. Test check_armored in super mode"
dist=test-super-mode-13
cat <<EOF > sfoo13.py
from pytransform import assert_armored
class Queen(object):
def hello(self):
print('Hello')
def hello():
print('Hello')
@assert_armored(Queen.hello)
def hello2():
print('Hello 2')
EOF
cat <<EOF > smain13.py
def fake_check_armored(*args):
print('it is fake_check_armored')
return True
import pytransform
pytransform.check_armored = fake_check_armored
from pytransform import check_armored
import sfoo13 as foo
print('Check armored return: %s' % check_armored(foo.hello, foo.hello2, foo))
EOF
$PYARMOR obfuscate --exact --advanced 2 -O $dist smain13.py sfoo13.py >result.log 2>&1
check_return_value
(cd $dist; $PYTHON smain13.py >result.log 2>&1)
check_return_value
check_file_content $dist/result.log "Check armored return: True"
check_file_content $dist/result.log "it is fake_check_armored" not
cp sfoo13.py $dist
(cd $dist; rm -rf sfoo13.pyc __pycache__; $PYTHON smain13.py >result.log 2>&1)
check_return_value
check_file_content $dist/result.log "Check armored return: False"
check_file_content $dist/result.log "it is fake_check_armored" not
csih_inform "S-14. Test check_armored in super mode with restrict mode 4"
dist=test-super-mode-14
$PYARMOR obfuscate --exact --advanced 2 --restrict 4 \
-O $dist smain13.py sfoo13.py >result.log 2>&1
check_return_value
(cd $dist; $PYTHON smain13.py >result.log 2>&1)
check_return_value
check_file_content $dist/result.log "Check armored return: True"
check_file_content $dist/result.log "it is fake_check_armored" not
cp sfoo13.py $dist
(cd $dist; rm -rf sfoo13.pyc __pycache__; $PYTHON smain13.py >result.log 2>&1)
check_return_value
check_file_content $dist/result.log "Check armored return: False"
check_file_content $dist/result.log "it is fake_check_armored" not
csih_inform "S-15. Test __del__ works in super mode"
dist=test-super-mode-15
$PYARMOR obfuscate --exact --advanced 2 -O $dist \
test/data/foo__del.py >result.log 2>&1
check_return_value
(cd $dist; $PYTHON foo__del.py >result.log 2>&1)
check_return_value
check_file_content $dist/result.log "test __del__ OK"
csih_inform "S-16. Test outer license with sys.PYARMOR_LICENSE in super mode"
dist=test-s-16
echo "print('Outer license OK')" > foo-s-16.py
$PYARMOR obfuscate --exact -O $dist --with-license outer \
--advanced 2 foo-s-16.py >result.log 2>&1
check_return_value
$PYARMOR licenses -O $dist --restrict 0 r001 >result.log 2>&1
check_file_exists $dist/r001/license.lic
echo "import sys" > $dist/foo.py
echo "sys.PYARMOR_LICENSE = 'r001/license.lic'" >> $dist/foo.py
cat $dist/foo-s-16.py >> $dist/foo.py
(cd $dist; $PYTHON foo.py >result.log 2>&1)
check_return_value
check_file_content $dist/result.log "Outer license OK"
echo ""
echo "-------------------- Super Mode End --------------------------"
echo ""
fi
# ======================================================================
#
# Project
#
# ======================================================================
echo ""
echo "-------------------- Test Project ----------------------------"
echo ""
csih_inform "Case P-1: project output path is '.'"
PROPATH=projects/test-blank-ouput
$PYARMOR init --src=examples/simple $PROPATH >result.log 2>&1
check_return_value
(cd $PROPATH;
$ARMOR config --output="." --package-runtime=0 >result.log 2>&1;
$ARMOR build >result.log 2>&1)
check_return_value
check_file_exists $PROPATH/queens.py
check_file_exists $PROPATH/pytransform.py
csih_inform "Case P-2: project output path is sub-directory of src"
PROPATH=projects/test-project-output
cp -a examples/simple $PROPATH
$PYARMOR init --src=$PROPATH $PROPATH >result.log 2>&1
(cd $PROPATH; $ARMOR build >result.log 2>&1)
check_return_value
check_file_exists $PROPATH/dist/queens.py
(cd $PROPATH; $ARMOR build -B >result.log 2>&1)
check_return_value
check_file_exists $PROPATH/dist/queens.py
check_file_not_exists $PROPATH/dist/dist/queens.py
csih_inform "Case P-3: project entry script is not in the src path"
PROPATH=projects/test-entry
mkdir -p $PROPATH/scripts
cat <<EOF > $PROPATH/scripts/foo.py
#! /usr/bin/env python
print('Hello')
EOF
$PYARMOR init --src=$PROPATH --entry scripts/foo.py $PROPATH >result.log 2>&1
(cd $PROPATH; $ARMOR build >result.log 2>&1)
check_return_value
check_file_exists $PROPATH/dist/scripts/foo.py
check_file_content $PROPATH/dist/scripts/foo.py '#! /usr/bin/env python'
check_file_content $PROPATH/dist/scripts/foo.py 'pyarmor_runtime'
check_file_content $PROPATH/dist/scripts/foo.py 'print' not
csih_inform "Case P-4: no leading dot is inserted into entry script without runtime"
PROPATH=projects/test-package-init
$PYARMOR init --src=examples/testpkg/mypkg/ --entry __init__.py $PROPATH >result.log 2>&1
(cd $PROPATH; $ARMOR build --no-runtime >result.log 2>&1)
check_return_value
check_file_exists $PROPATH/dist/mypkg/__init__.py
check_file_content $PROPATH/dist/mypkg/__init__.py 'from pytransform import pyarmor_runtime'
check_file_content $PROPATH/dist/mypkg/__init__.py 'pyarmor_runtime'
csih_inform "Case P-5: build project with --package-runtime=1"
PROPATH=projects/test-project-package-runtime
$PYARMOR init --src=examples/simple --entry queens.py $PROPATH >result.log 2>&1
$PYARMOR config --package-runtime=1 $PROPATH >result.log 2>&1
check_return_value
(cd $PROPATH; $ARMOR build >result.log 2>&1)
check_return_value
check_file_exists $PROPATH/dist/pytransform/__init__.py
check_file_content $PROPATH/dist/pytransform/__init__.py 'def pyarmor_runtime'
(cd $PROPATH/dist; $PYTHON queens.py >result.log 2>&1 )
check_return_value
check_file_content $PROPATH/dist/result.log 'Found 92 solutions'
csih_inform "Case P-6: build project with --bootstrap=2"
PROPATH=projects/test-bootstrap2
$PYARMOR init --src=examples/testpkg/mypkg/ --entry __init__.py $PROPATH >result.log 2>&1
$PYARMOR config --package-runtime=1 --bootstrap=2 $PROPATH >result.log 2>&1
check_return_value
(cd $PROPATH; $ARMOR build >result.log 2>&1)
check_return_value
check_file_exists $PROPATH/dist/mypkg/pytransform/__init__.py
check_file_content $PROPATH/dist/mypkg/pytransform/__init__.py 'def pyarmor_runtime'
check_file_exists $PROPATH/dist/mypkg/__init__.py
check_file_content $PROPATH/dist/mypkg/__init__.py 'from pytransform import pyarmor_runtime'
csih_inform "Case P-7: build project with --restrict-mode=0"
PROPATH=projects/test-p7
$PYARMOR init --src=examples/testpkg/mypkg/ --entry __init__.py $PROPATH >result.log 2>&1
$PYARMOR config --restrict=0 $PROPATH >result.log 2>&1
check_return_value
(cd $PROPATH; $ARMOR build >result.log 2>&1)
check_return_value
check_file_exists $PROPATH/dist/mypkg/pytransform/__init__.py