-
Notifications
You must be signed in to change notification settings - Fork 11
/
sudo_tests
executable file
·1714 lines (1198 loc) · 52.6 KB
/
sudo_tests
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
#!/data/data/com.termux/files/usr/bin/bash
#title: sudo_tests
#description: a script to run automated tests for the sudo command types
#author: agnostic-apollo
#usage: run "sudo_tests --help" for detailed list of usages
#date: 14-Dec-2020
#bash version: 4.1 or higher
#credits:
SUDO_TESTS_HELP="$(cat<<'SUDO_TESTS_HELP_EOF'
sudo_tests is a script that run tests for the sudo script.
Usage:
sudo_tests [command_options]
sudo_tests [command_options] <su|asu|path|script>
sudo_tests [command_options] <su|asu|path|script> <shell> <test_number>
Available command_options:
[ -h | --help ] display this help screen
[ -v | -vv ] set verbose level to 1 or 2
[ --help-extra ] display more help about how sudo_tests command works
Enabling verbose mode will fail test validation for tests that match
the output, its mainly for debugging.
SUDO_TESTS_HELP_EOF
)"
SUDO_TESTS_HELP_EXTRA="$(cat<<'SUDO_TESTS_HELP_EXTRA'
### Install and Usage Instructions For Termux In Android:
#Install sudo and sudo_tests scripts and set ownership and executable permission
curl -L 'https://github.com/agnostic-apollo/sudo/raw/master/sudo' -o "/data/data/com.termux/files/usr/bin/sudo"
curl -L 'https://github.com/agnostic-apollo/sudo/raw/master/tests/sudo_tests' -o ~/sudo_tests
or
cat "/storage/emulated/0/Download/sudo" > "/data/data/com.termux/files/usr/bin/sudo"
cat "/storage/emulated/0/Download/sudo_tests" > ~/sudo_tests
export termux_bin_path="/data/data/com.termux/files/usr/bin"; export owner="$(stat -c "%u" "$termux_bin_path")"; chown "$owner:$owner" "$termux_bin_path/sudo" && chmod 700 "$termux_bin_path/sudo"; chown "$owner:$owner" ~/sudo_tests && chmod 700 ~/sudo_tests;
#Install shells used by sudo_tests script
pkg install bash zsh dash fish python ruby nodejs perl lua52 lua53 lua54 php rlwrap
#Install termux_tasker_basic_bash_test and termux_tasker_basic_python_test script and set permissions
mkdir -p /data/data/com.termux/files/home/.termux/tasker
chmod 700 -R /data/data/com.termux/files/home/.termux
curl -L 'https://github.com/termux/termux-tasker/raw/master/templates/scripts/termux_tasker_basic_bash_test' -o "/data/data/com.termux/files/home/.termux/tasker/termux_tasker_basic_bash_test"
curl -L 'https://github.com/termux/termux-tasker/raw/master/templates/scripts/termux_tasker_basic_python_test' -o "/data/data/com.termux/files/home/.termux/tasker/termux_tasker_basic_python_test"
or
cat "/storage/emulated/0/Download/termux_tasker_basic_bash_test" > "/data/data/com.termux/files/home/.termux/tasker/termux_tasker_basic_bash_test"
cat "/storage/emulated/0/Download/termux_tasker_basic_python_test" > "/data/data/com.termux/files/home/.termux/tasker/termux_tasker_basic_python_test"
chmod 700 "/data/data/com.termux/files/home/.termux/tasker/termux_tasker_basic_bash_test"
chmod 700 "/data/data/com.termux/files/home/.termux/tasker/termux_tasker_basic_python_test"
#Install youtube-dl
apt install curl ffmpeg python
curl -L https://yt-dl.org/downloads/latest/youtube-dl -o /data/data/com.termux/files/usr/bin/youtube-dl
chmod 700 /data/data/com.termux/files/usr/bin/youtube-dl
#Fix shebang, must be run on install and on each update
#otherwise will get "bad interpreter: /usr/bin/env" errors when running with root shell, plugin or TermuxCommand,
#because termux-exec doesn't work for those cases and LD_PRELOAD isn't set
#but its not required for usage with sudo since it sets the LD_PRELOAD
termux-fix-shebang /data/data/com.termux/files/usr/bin/youtube-dl
#
#Update youtube-dl
youtube-dl -U
#Install bandcamp-dl
pip3 install bandcamp-downloader
#
#Update bandcamp-dl
pkg install git
pip install git+https://github.com/iheanyi/bandcamp-dl --upgrade
#Following are not required for the tests currently
#Install pry, the ruby interactive shell alternative
gem install pry
#Install python2
pkg install python2
#Install ksh
apt install unstable-repo
pkg intsall loksh
#Follow instructions in `RC File Variables` section of README.md to fix `rc` files.
#Finally run tests
#Run all tests
bash ~/sudo_tests
#Run only script command type tests
bash ~/sudo_tests script
#Run only script command type bash shell test 1
bash ~/sudo_tests script bash 1
#Run only script command type bash shell test 1 with verbose level 2
bash ~/sudo_tests -vv script bash 1
#For some tests **sometimes** where *-stdin-string option is used to pass a string to an interactive shell using stdin,
#the stdout is returned in an unsynchronized manner which fails the validation, possibly due to magisk su or maybe an android 10 issue
#The tests run fine with SuperSU v2.82 on android 7 without issues. Currently not sure of the real reason
#If that happens, just rerun the test and it should ideally work
#This is also the reason why su and asu tests use matches instead of equality checks, some script type tests still use exact matches
SUDO_TESTS_HELP_EXTRA
)"
### Set Default Variables Start
sudo_tests_verbose_level=2 #default to log level 2
sudo_tests_args_verbose_level=0 #set this to 1 manually, if you want to debug arguments received
SUDO_TESTS_TEMP_DIRECTORY_SUFFIX="" #default to none
declare -ga SUDO_TESTS_DIRECTORIES=()
declare -ga sudo_v_args=()
#set regexes for validation
valid_number_regex='^[0-9]+$'
valid_absolute_path_regex='^(/[^/]+)+$'
### Set Default Variables
[[ x"${BASH_SOURCE[0]}" == x"$0" ]] && sudo_tests_exit_command="exit" || sudo_tests_exit_command="return"
function sudo_tests_log () { local log_level="${1}"; shift; if [[ $sudo_tests_verbose_level -ge $log_level ]]; then echo "$@"; fi }
function sudo_tests_log_literal() { local log_level="${1}"; shift; if [[ $sudo_tests_verbose_level -ge $log_level ]]; then echo -e "$@"; fi }
function sudo_tests_log_errors () { echo "$@" 1>&2; }
function sudo_tests_log_args() { if [[ $sudo_tests_args_verbose_level -ge "1" ]]; then echo "$@"; fi }
function sudo_tests_log_arg_errors() { echo "$@" 1>&2; }
#stdin in closed for some tests as a patch to SuperSU automatic redirection of stderr to stdout when running in interactive mode
function close_stdin () { if [ -t 0 ]; then exec <&-; fi }
function reopen_stdin () { if [[ "$tty_path" == "/dev/"* ]]; then exec <$tty_path; fi }
sudo_tests_main() {
local return_value
#source the sudo script
if [ ! -x /data/data/com.termux/files/usr/bin/sudo ]; then
sudo_tests_log_errors "Failed to find \"/data/data/com.termux/files/usr/bin/sudo\""
$sudo_tests_exit_command 1
else
source /data/data/com.termux/files/usr/bin/sudo
sudo_set_default_variables
fi
local -a SUDO_TESTS_REQUIRED_VARIABLES_ARRAY=(
TERMUX_FILES
TERMUX_PREFIX
TERMUX_HOME
TERMUX_PATH
TERMUX_LD_LIBRARY_PATH
ANDROID_PATH
)
local var
for var in "${SUDO_TESTS_REQUIRED_VARIABLES_ARRAY[@]}"; do
#if var is not set
if [ -z "${!var}" ]; then
sudo_tests_log_errors "The required variable \"$var\" for sudo_tests is not set"
$sudo_tests_exit_command 1
fi
done
#process the command or options passed to sudo_tests
process_sudo_tests_parameters "$@"
sudo_tests_run
$sudo_tests_exit_command $?
}
sudo_tests_run() {
local return_value
#set SUDO_TESTS_WORKING_DIR to use for tests
SUDO_TESTS_WORKING_DIR="$TERMUX_HOME"
cd "$SUDO_TESTS_WORKING_DIR"
if [ -t 0 ]; then
tty_path="$(tty 2>/dev/null)"
fi
#find machine arch
ARCH="$(dpkg --print-architecture)"
return_value=$?
if [ $return_value -ne 0 ]; then
sudo_tests_log_errors "Failed to find machine arch"
return $return_value
fi
#find android sdk/os version
ANDROID_SDK_VERSION="$(getprop "ro.build.version.sdk")"
return_value=$?
if [ $return_value -ne 0 ] || [[ ! "$ANDROID_SDK_VERSION" =~ $valid_number_regex ]]; then
sudo_tests_log_errors "Failure while finding \"ro.build.version.sdk\" property"
sudo_tests_log_errors "ANDROID_SDK_VERSION = \"$ANDROID_SDK_VERSION\""
if [ $return_value -eq 0 ]; then
return_value=1
fi
return $return_value
fi
#generate unique suffix to use for sudo_tests directories
SUDO_TESTS_TEMP_DIRECTORY_SUFFIX="$(mktemp -d --dry-run sudo.XXXXXX)"
return_value=$?
if [ $return_value -ne 0 ] || [[ ! "$SUDO_TESTS_TEMP_DIRECTORY_SUFFIX" =~ ^sudo\.......$ ]]; then
sudo_tests_log_errors "Failure while running mktemp to create SUDO_TESTS_TEMP_DIRECTORY_SUFFIX"
sudo_tests_log_errors "SUDO_TESTS_TEMP_DIRECTORY_SUFFIX=\"$SUDO_TESTS_TEMP_DIRECTORY_SUFFIX\""
if [ $return_value -eq 0 ]; then
return_value=1
fi
return $return_value
fi
export SUDO_SHELL_HOME="$SUDO_TESTS_WORKING_DIR/.shell_home.$SUDO_TESTS_TEMP_DIRECTORY_SUFFIX"
export SUDO_POST_SHELL_HOME="$SUDO_TESTS_WORKING_DIR/.post_shell_home.$SUDO_TESTS_TEMP_DIRECTORY_SUFFIX"
SUDO_SHELL_HOME_BASENAME="${SUDO_SHELL_HOME##*/}" #strip longest match of */ from start
SUDO_POST_SHELL_HOME_BASENAME="${SUDO_POST_SHELL_HOME##*/}" #strip longest match of */ from start
SUDO_TESTS_DIRECTORIES+=("$SUDO_SHELL_HOME")
SUDO_TESTS_DIRECTORIES+=("$SUDO_POST_SHELL_HOME")
#set traps to run commands before exiting sudo_tests
trap 'sudo_tests_cleanup' EXIT
trap 'sudo_tests_cleanup TERM' TERM
trap 'sudo_tests_cleanup INT' INT
trap 'sudo_tests_cleanup HUP' HUP
trap 'sudo_tests_cleanup QUIT' QUIT
#run tests
if [ -z "$sudo_tests_command_type" ]; then
sudo_tests_log 0 "Running 'sudo' tests"
sudo_tests_log 0 $'\n\n'; sudo_tests_run_su_tests || return $?
sudo_tests_log 0 $'\n\n'; sudo_tests_run_asu_tests || return $?
sudo_tests_log 0 $'\n\n'; sudo_tests_run_path_tests || return $?
sudo_tests_log 0 $'\n\n'; sudo_tests_run_script_tests || return $?
elif [[ "$sudo_tests_command_type" != *,* ]] && [[ ",su,asu,path,script," == *",$sudo_tests_command_type,"* ]]; then
sudo_tests_run_"$sudo_tests_command_type"_tests || return $?
else
sudo_tests_log_errors "Unknown tests command type $sudo_tests_command_type"
show_sudo_tests_help
return 1
fi
sudo_tests_log 0 $'\n\n'"All 'sudo' tests successful"
return 0
}
sudo_tests_run_su_tests() {
local return_value
sudo_tests_command_type="su"
sudo_tests_log 0 "Running '$sudo_tests_command_type' command type tests"
#Run bash tests
set_sudo_tests_current_test_shell "bash"
shell="bash"
run_sudo_test 1 && \
output="$(sudo "${sudo_v_args[@]}" --dry-run --shell-stdin-string='cd "$HOME"; pwd; exit;' su)"
validate_sudo_test 0 $? "$output" ""
run_sudo_test 2 && \
output="$(sudo "${sudo_v_args[@]}" --shell-stdin-string='cd "$HOME"; pwd; exit;' su)"
validate_sudo_test 0 $? "$output" "*" "$SUDO_SHELL_HOME" "*"
run_sudo_test 3 && \
output="$(sudo "${sudo_v_args[@]}" --shell-stdin-string='echo "$PATH"; exit;' su)"
validate_sudo_test 0 $? "$output" "*" "$TERMUX_PATH" "*"
run_sudo_test 4 && \
output="$(sudo "${sudo_v_args[@]}" --shell-stdin-string='echo "$LD_LIBRARY_PATH"; exit;' su)"
validate_sudo_test 0 $? "$output" "*" "$TERMUX_LD_LIBRARY_PATH" "*"
sudo_tests_log 0 $'\n\n'"All 'su' command type tests successful"
return 0
}
sudo_tests_run_asu_tests() {
local return_value
sudo_tests_command_type="asu"
sudo_tests_log 0 "Running '$sudo_tests_command_type' command type tests"
#Run bash tests
set_sudo_tests_current_test_shell "bash"
shell="bash"
run_sudo_test 1 && \
output="$(sudo "${sudo_v_args[@]}" --dry-run --shell-stdin-string='cd "$HOME"; pwd; exit;' asu)"
validate_sudo_test 0 $? "$output" ""
run_sudo_test 2 && \
output="$(sudo "${sudo_v_args[@]}" --shell-stdin-string='cd "$HOME"; pwd; exit;' asu)"
validate_sudo_test 0 $? "$output" "*" "$SUDO_SHELL_HOME" "*"
run_sudo_test 3 && \
output="$(sudo "${sudo_v_args[@]}" --shell-stdin-string='echo "$PATH"; exit;' asu)"
validate_sudo_test 0 $? "$output" "*" "$ANDROID_PATH" "*"
run_sudo_test 4 && \
output="$(sudo "${sudo_v_args[@]}" --shell-stdin-string='echo "$LD_LIBRARY_PATH"; exit;' asu)"
return_value=$?
#if ARCH is 64 bit
if [[ "$ARCH" == *64 ]]; then
validate_sudo_test 0 $return_value "$output" "*" "/system/lib64:/system/lib:" "*"
else
validate_sudo_test 0 $return_value "$output" "*" "/system/lib:" "*"
fi
sudo_tests_log 0 $'\n\n'"All 'asu' command type tests successful"
return 0
}
sudo_tests_run_path_tests() {
local return_value
sudo_tests_command_type="path"
sudo_tests_log 0 "Running '$sudo_tests_command_type' command type tests"
#Run bash tests
set_sudo_tests_current_test_shell "bash"
shell="bash"
if [ -x "$TERMUX_HOME/.termux/tasker/termux_tasker_basic_bash_test" ]; then
run_sudo_test 1 && \
output="$(sudo "${sudo_v_args[@]}" "$TERMUX_HOME/.termux/tasker/termux_tasker_basic_bash_test" "$shell" "shell")"
validate_sudo_test 0 $? "$output" "\$1=\`$shell\`"$'\n'"\$2=\`shell\`"
else
skip_sudo_test 1
fi
if [ -x "$TERMUX_HOME/.termux/tasker/termux_tasker_basic_bash_test" ]; then
run_sudo_test 2 && \
output="$(sudo "${sudo_v_args[@]}" '~/.termux/tasker/termux_tasker_basic_bash_test' "$shell" "shell")"
validate_sudo_test 0 $? "$output" "\$1=\`$shell\`"$'\n'"\$2=\`shell\`"
else
skip_sudo_test 2
fi
if [ -x "$TERMUX_HOME/.termux/tasker/termux_tasker_basic_bash_test" ]; then
run_sudo_test 3 && \
output="$(cd "$TERMUX_HOME"; sudo "${sudo_v_args[@]}" './.termux/tasker/termux_tasker_basic_bash_test' "$shell" "shell")"
validate_sudo_test 0 $? "$output" "\$1=\`$shell\`"$'\n'"\$2=\`shell\`"
else
skip_sudo_test 3
fi
if [ -x "$TERMUX_HOME/.termux/tasker/termux_tasker_basic_bash_test" ]; then
run_sudo_test 4 && \
output="$(cd "$TERMUX_HOME/.termux/tasker"; sudo "${sudo_v_args[@]}" 'termux_tasker_basic_bash_test' "$shell" "shell")"
validate_sudo_test 0 $? "$output" "\$1=\`$shell\`"$'\n'"\$2=\`shell\`"
else
skip_sudo_test 4
fi
run_sudo_test 5 && \
output="$(sudo "${sudo_v_args[@]}" ps -e -o cmd | grep "$TERMUX_PREFIX/bin/ps")"
validate_sudo_test 0 $? "$output" "*" "$TERMUX_PREFIX/bin/ps -e -o cmd" "*"
run_sudo_test 6 && \
output="$(sudo "${sudo_v_args[@]}" dumpsys activity services | grep -E 'ServiceRecord\{.*com\.termux\/.*\.app\.TermuxService\}')"
validate_sudo_test 0 $? "$output" "*" "com.termux/.app.TermuxService" "*"
run_sudo_test 7 && \
output="$(sudo "${sudo_v_args[@]}" -r echo "$COMMA_ALTERNATIVE")"
validate_sudo_test 0 $? "$output" ","
sudo_tests_log 0 $'\n\n'"All 'path' command type tests successful"
return 0
}
sudo_tests_run_script_tests() {
local return_value
sudo_tests_command_type="script"
sudo_tests_log 0 "Running '$sudo_tests_command_type' command type tests"
### Run bash tests
set_sudo_tests_current_test_shell "bash"
shell="bash"
run_sudo_test 1 && \
output="$(sudo "${sudo_v_args[@]}" -s --dry-run 'cd "$HOME"; pwd;')"
validate_sudo_test 0 $? "$output" ""
run_sudo_test 2 && \
output="$(sudo "${sudo_v_args[@]}" -s 'cd "$HOME"; pwd;')"
validate_sudo_test 0 $? "$output" "$SUDO_SHELL_HOME"
run_sudo_test 3 && \
output="$(cd "$SUDO_TESTS_WORKING_DIR"; sudo "${sudo_v_args[@]}" -s --shell-home="./$SUDO_SHELL_HOME_BASENAME" 'cd "$HOME"; pwd;')"
validate_sudo_test 0 $? "$output" "$SUDO_SHELL_HOME"
run_sudo_test 4 && \
output="$(cd "$SUDO_TESTS_WORKING_DIR"; sudo "${sudo_v_args[@]}" -s --shell-home="$SUDO_SHELL_HOME_BASENAME" 'cd "$HOME"; pwd;')"
validate_sudo_test 0 $? "$output" "$SUDO_SHELL_HOME"
run_sudo_test 5 && \
output="$(sudo "${sudo_v_args[@]}" -si --post-shell-home="$SUDO_POST_SHELL_HOME" --post-shell-stdin-string='exit' --post-shell-pre-commands='cd "$HOME"; pwd;')"
validate_sudo_test 0 $? "$output" "" "$SUDO_POST_SHELL_HOME" "*"
run_sudo_test 6 && \
output="$(sudo "${sudo_v_args[@]}" -s 'readlink -f /proc/$$/exe')"
validate_sudo_test 0 $? "$output" "$TERMUX_PREFIX/bin/bash"
run_sudo_test 7 && \
output="$(sudo "${sudo_v_args[@]}" -s --shell="$TERMUX_PREFIX/bin/bash" 'readlink -f /proc/$$/exe')"
validate_sudo_test 0 $? "$output" "$TERMUX_PREFIX/bin/bash"
run_sudo_test 8 && \
output="$(sudo "${sudo_v_args[@]}" -si --post-shell-stdin-string='readlink -f /proc/$$/exe; exit;')"
validate_sudo_test 0 $? "$output" '# readlink -f /proc/$$/exe; exit;'$'\n'"$TERMUX_PREFIX/bin/bash"$'\n'"exit"
run_sudo_test 9 && \
output="$(sudo "${sudo_v_args[@]}" -si --post-shell="$TERMUX_PREFIX/bin/bash" --post-shell-stdin-string='readlink -f /proc/$$/exe; exit;')"
validate_sudo_test 0 $? "$output" '# readlink -f /proc/$$/exe; exit;'$'\n'"$TERMUX_PREFIX/bin/bash"$'\n'"exit"
run_sudo_test 10 && \
output="$(sudo "${sudo_v_args[@]}" -s --shell="$shell" <(cat <<'SUDO_EOF'
echo "Hi, $1 $2."
SUDO_EOF
) "$shell" "shell"
)"
validate_sudo_test 0 $? "$output" "Hi, $shell shell."
run_sudo_test 11 && \
output="$(sudo "${sudo_v_args[@]}" -s --shell="$shell" --shell-stdin-string="$shell" '
echo '\''What is your name?'\''
read name
echo "Hi, $name."
'
)"
validate_sudo_test 0 $? "$output" "What is your name?"$'\n'"Hi, $shell."
run_sudo_test 12 && \
output="$(sudo "${sudo_v_args[@]}" -sd --shell="$shell" '
echo '\''What is your name?'\''
read name
echo "Hi, $name."
'
)"
validate_sudo_test 0 $? "$output" "What is your name?"$'\n'"Hi, ."
run_sudo_test 13 && \
output="$(sudo "${sudo_v_args[@]}" -so --shell="$shell" --shell-stdin-string="$shell" '
echo '\''What is your name?'\''
read name
echo "Hi, $name." 1>&2
'
)"
validate_sudo_test 0 $? "$output" "What is your name?"$'\n'"Hi, $shell."
run_sudo_test 14 && \
output="$(sudo "${sudo_v_args[@]}" -sf 'echo "Running $1 $2 script: $(basename "$0")"' "$shell" "shell")"
validate_sudo_test 0 $? "$output" "Running $shell shell script: sudo_core_script"
run_sudo_test 15 && \
output="$(sudo "${sudo_v_args[@]}" -sf --script-name="sudo_core_script_custom" 'echo "Running $1 $2 script: $(basename "$0")"' "$shell" "shell")"
validate_sudo_test 0 $? "$output" "Running $shell shell script: sudo_core_script_custom"
if [ -x "$TERMUX_HOME/.termux/tasker/termux_tasker_basic_bash_test" ]; then
run_sudo_test 16 && \
output="$(sudo "${sudo_v_args[@]}" -sF '~/.termux/tasker/termux_tasker_basic_bash_test' "$shell" "shell")"
validate_sudo_test 0 $? "$output" "\$1=\`$shell\`"$'\n'"\$2=\`shell\`"
else
skip_sudo_test 16
fi
if [ -x "$TERMUX_PREFIX/bin/tasker_config_utils" ]; then
run_sudo_test 17 && \
output="$(sudo "${sudo_v_args[@]}" -s --script-name="tasker_config_utils" <(cat "$TERMUX_PREFIX/bin/tasker_config_utils") --help 1>/dev/null)"
validate_sudo_test 0 $? "$output"
else
skip_sudo_test 17
fi
if [ -x "$TERMUX_PREFIX/bin/tasker_config_utils" ]; then
run_sudo_test 18 && \
output="$(sudo "${sudo_v_args[@]}" -sf --script-name="tasker_config_utils" <(cat "$TERMUX_PREFIX/bin/tasker_config_utils") --help 1>/dev/null)"
validate_sudo_test 0 $? "$output"
else
skip_sudo_test 18
fi
run_sudo_test 19 && \
output="$(sudo "${sudo_v_args[@]}" -s --shell-pre-commands='
export VARIABLE_1="VARIABLE_VALUE_1"
export VARIABLE_2="VARIABLE_VALUE_2"
' '
echo "Hi, $1 $2."
echo "VARIABLE_1=\`$VARIABLE_1\`"
echo "VARIABLE_2=\`$VARIABLE_2\`"
' "$shell" "shell"
)"
validate_sudo_test 0 $? "$output" "Hi, $shell shell."$'\n'"VARIABLE_1=\`VARIABLE_VALUE_1\`"$'\n'"VARIABLE_2=\`VARIABLE_VALUE_2\`"
run_sudo_test 20 && \
output="$(sudo "${sudo_v_args[@]}" -si --post-shell-stdin-string='exit' --shell-pre-commands='echo "Start script"' --shell-post-commands='echo "Exited script"' --post-shell-pre-commands='echo "Starting interactive shell"' --post-shell-post-commands='echo "Exited interactive shell"' 'echo "Hi, $1 $2."' "$shell" "shell")"
validate_sudo_test 0 $? "$output" "Start script"$'\n'"Hi, $shell shell."$'\n'"Exited script"$'\n'"Starting interactive shell"$'\n'"# exit"$'\n'"exit"$'\n'"Exited interactive shell"
sudo_test_temp_shell_home="/shell_home.$SUDO_TESTS_TEMP_DIRECTORY_SUFFIX"
SUDO_TESTS_DIRECTORIES+=("$sudo_test_temp_shell_home")
run_sudo_test 21 && \
output="$(sudo "${sudo_v_args[@]}" -s --shell-home="$sudo_test_temp_shell_home" 'cd "$HOME"; pwd;' 2>&1)"
return_value=$?
# If android >= 10
if [ "$ANDROID_SDK_VERSION" -ge 29 ]; then
validate_sudo_test 1 $return_value "$output" "" "The rootfs \"/\" partition cannot be mounted as rw to be used for SUDO_SHELL_HOME \"$sudo_test_temp_shell_home\"" "*"
else
validate_sudo_test 0 $return_value "$output" "$sudo_test_temp_shell_home"
fi
sudo_test_temp_post_shell_home="/post_shell_home.$SUDO_TESTS_TEMP_DIRECTORY_SUFFIX"
SUDO_TESTS_DIRECTORIES+=("$sudo_test_temp_post_shell_home")
run_sudo_test 22 && \
output="$(sudo "${sudo_v_args[@]}" -si --post-shell-home="$sudo_test_temp_post_shell_home" --post-shell-stdin-string='exit' --post-shell-pre-commands='cd "$HOME"; pwd;' 2>&1)"
return_value=$?
# If android >= 10
if [ "$ANDROID_SDK_VERSION" -ge 29 ]; then
validate_sudo_test 1 $return_value "$output" "" "The rootfs \"/\" partition cannot be mounted as rw to be used for SUDO_POST_SHELL_HOME \"$sudo_test_temp_post_shell_home\"" "*"
else
validate_sudo_test 0 $return_value "$output" "$sudo_test_temp_post_shell_home"$'\n'"# exit"$'\n'"exit"
fi
sudo_test_temp_shell_home="/system/shell_home.$SUDO_TESTS_TEMP_DIRECTORY_SUFFIX"
SUDO_TESTS_DIRECTORIES+=("$sudo_test_temp_shell_home")
run_sudo_test 23 && \
output="$(sudo "${sudo_v_args[@]}" -s --shell-home="$sudo_test_temp_shell_home" 'cd "$HOME"; pwd;' 2>&1)"
return_value=$?
# If android >= 10
if [ "$ANDROID_SDK_VERSION" -ge 29 ]; then
validate_sudo_test 1 $return_value "$output" "" "The system \"/system\" partition cannot be mounted as rw to be used for SUDO_SHELL_HOME \"$sudo_test_temp_shell_home\"" "*"
else
validate_sudo_test 0 $return_value "$output" "$sudo_test_temp_shell_home"
fi
sudo_test_temp_post_shell_home="/system/post_shell_home.$SUDO_TESTS_TEMP_DIRECTORY_SUFFIX"
SUDO_TESTS_DIRECTORIES+=("$sudo_test_temp_post_shell_home")
run_sudo_test 24 && \
output="$(sudo "${sudo_v_args[@]}" -si --post-shell-home="$sudo_test_temp_post_shell_home" --post-shell-stdin-string='exit' --post-shell-pre-commands='cd "$HOME"; pwd;' 2>&1)"
return_value=$?
# If android >= 10
if [ "$ANDROID_SDK_VERSION" -ge 29 ]; then
validate_sudo_test 1 $return_value "$output" "" "The system \"/system\" partition cannot be mounted as rw to be used for SUDO_POST_SHELL_HOME \"$sudo_test_temp_post_shell_home\"" "*"
else
validate_sudo_test 0 $return_value "$output" "$sudo_test_temp_post_shell_home"$'\n'"# exit"$'\n'"exit"
fi
sudo_test_temp_work_dir="$SUDO_TESTS_WORKING_DIR/work_dir.$SUDO_TESTS_TEMP_DIRECTORY_SUFFIX"
SUDO_TESTS_DIRECTORIES+=("$sudo_test_temp_work_dir")
run_sudo_test 25 && \
output="$(sudo "${sudo_v_args[@]}" -s --work-dir="$sudo_test_temp_work_dir" 'pwd' 2>&1)"
return_value=$?
validate_sudo_test 0 $return_value "$output" "$sudo_test_temp_work_dir"
run_sudo_test 26 && \
output="$(sudo "${sudo_v_args[@]}" -s --work-dir='~/work_dir.'"$SUDO_TESTS_TEMP_DIRECTORY_SUFFIX" 'pwd' 2>&1)"
return_value=$?
validate_sudo_test 0 $return_value "$output" "$sudo_test_temp_work_dir"
run_sudo_test 27 && \
output="$(cd "$SUDO_TESTS_WORKING_DIR"; sudo "${sudo_v_args[@]}" -s --work-dir="./work_dir.$SUDO_TESTS_TEMP_DIRECTORY_SUFFIX" 'pwd' 2>&1)"
return_value=$?
validate_sudo_test 0 $return_value "$output" "$sudo_test_temp_work_dir"
run_sudo_test 28 && \
output="$(cd "$SUDO_TESTS_WORKING_DIR"; sudo "${sudo_v_args[@]}" -s --work-dir="work_dir.$SUDO_TESTS_TEMP_DIRECTORY_SUFFIX" 'pwd' 2>&1)"
return_value=$?
validate_sudo_test 0 $return_value "$output" "$sudo_test_temp_work_dir"
#redirect stderr to stdout, same as `--script-redirect=0`
close_stdin
run_sudo_test 29 && \
sudo_tests_run_subshell_command stdout stderr sudo "${sudo_v_args[@]}" -so --shell="$shell" 'echo stdout; echo stderr 1>&2'
return_value=$?
reopen_stdin
validate_sudo_test 0 $return_value "$stdout" "stdout"$'\n'"stderr"
validate_sudo_test 0 $return_value "$stderr" ""
#redirect stdout to stderr, same as `--script-redirect=1`
close_stdin
run_sudo_test 30 && \
sudo_tests_run_subshell_command stdout stderr sudo "${sudo_v_args[@]}" -sO --shell="$shell" 'echo stdout; echo stderr 1>&2'
return_value=$?
reopen_stdin
validate_sudo_test 0 $return_value "$stdout" ""
validate_sudo_test 0 $return_value "$stderr" "stdout"$'\n'"stderr"
#redirect redirect stdout to /dev/null
close_stdin
run_sudo_test 31 && \
sudo_tests_run_subshell_command stdout stderr sudo "${sudo_v_args[@]}" -s --script-redirect=2 --shell="$shell" 'echo stdout; echo stderr 1>&2'
return_value=$?
reopen_stdin
validate_sudo_test 0 $return_value "$stdout" ""
validate_sudo_test 0 $return_value "$stderr" "stderr"
#redirect stderr to /dev/null for core_script, same as `--script-redirect=3`
close_stdin
run_sudo_test 32 && \
sudo_tests_run_subshell_command stdout stderr sudo "${sudo_v_args[@]}" -sn --shell="$shell" 'echo stdout; echo stderr 1>&2'
return_value=$?
reopen_stdin
validate_sudo_test 0 $return_value "$stdout" "stdout"
validate_sudo_test 0 $return_value "$stderr" ""
#redirect both stdout and stderr to /dev/null, same as `--script-redirect=4`
close_stdin
run_sudo_test 33 && \
sudo_tests_run_subshell_command stdout stderr sudo "${sudo_v_args[@]}" -sN --shell="$shell" 'echo stdout; echo stderr 1>&2'
return_value=$?
reopen_stdin
validate_sudo_test 0 $return_value "$stdout" ""
validate_sudo_test 0 $return_value "$stderr" ""
#redirect stderr to stdout and redirect stdout to stderr
close_stdin
run_sudo_test 34 && \
sudo_tests_run_subshell_command stdout stderr sudo "${sudo_v_args[@]}" -s --script-redirect=5 --shell="$shell" 'echo stdout; echo stderr 1>&2'
return_value=$?
reopen_stdin
validate_sudo_test 0 $return_value "$stdout" "stderr"
validate_sudo_test 0 $return_value "$stderr" "stdout"
#redirect stderr to stdout and redirect stdout to /dev/null
close_stdin
run_sudo_test 35 && \
sudo_tests_run_subshell_command stdout stderr sudo "${sudo_v_args[@]}" -s --script-redirect=6 --shell="$shell" 'echo stdout; echo stderr 1>&2'
return_value=$?
reopen_stdin
validate_sudo_test 0 $return_value "$stdout" "stderr"
validate_sudo_test 0 $return_value "$stderr" ""
close_stdin
run_sudo_test 36 && \
sudo_tests_run_subshell_command stdout stderr sudo "${sudo_v_args[@]}" -sie --post-shell-stdin-string='exit' --shell="$shell" <(cat <<'SUDO_EOF'
#if parameter count is not 2
if [ $# -ne 2 ]; then
echo "Invalid parameter count '$#' to 'termux_tasker_basic_bash_test'" 1>&2
echo "$*" 1>&2
exit 1
fi
echo "\$1=\`$1\`"
echo "\$2=\`$2\`"
exit 0
SUDO_EOF
) "$shell"
return_value=$?
reopen_stdin
validate_sudo_test 1 $return_value "$stdout" ""
validate_sudo_test 1 $return_value "$stderr" "Invalid parameter count '1' to 'termux_tasker_basic_bash_test'"$'\n'"$shell"
run_sudo_test 37 && \
output="$(sudo "${sudo_v_args[@]}" -s --shell=bas 'echo "Hi, $1 $2."' "$shell" "shell" 2>&1)"
validate_sudo_test 1 $? "$output" "" "The SUDO_SHELL \"bas\" is not supported. It must be one of" "*"
run_sudo_test 38 && \
output="$(sudo "${sudo_v_args[@]}" -si --post-shell=bas 'echo "Hi, $1 $2."' "$shell" "shell" 2>&1)"
validate_sudo_test 1 $? "$output" "" "The SUDO_POST_SHELL \"bas\" is not supported. It must be one of" "*"
run_sudo_test 39 && \
output="$(sudo "${sudo_v_args[@]}" -s --shell-home='~/.config' 'echo "Hi, $1 $2."' "$shell" "shell" 2>&1)"
validate_sudo_test 1 $? "$output" "" "The \"$TERMUX_HOME/.config\" cannot be used as SUDO_SHELL_HOME" "*"
run_sudo_test 40 && \
output="$(sudo "${sudo_v_args[@]}" -s --shell-home='$PREFIX/' 'echo "Hi, $1 $2."' "$shell" "shell" 2>&1)"
validate_sudo_test 1 $? "$output" "" "The \"$TERMUX_PREFIX\" cannot be used as SUDO_SHELL_HOME" "*"
run_sudo_test 41 && \
output="$(sudo "${sudo_v_args[@]}" -s --shell-home='$PREFIX/home' 'echo "Hi, $1 $2."' "$shell" "shell" 2>&1)"
validate_sudo_test 1 $? "$output" "" "The \"$TERMUX_PREFIX/home\" cannot be used as SUDO_SHELL_HOME" "*"
run_sudo_test 42 && \
output="$(sudo "${sudo_v_args[@]}" -si --post-shell-home='~/.config' 'echo "Hi, $1 $2."' "$shell" "shell" 2>&1)"
validate_sudo_test 1 $? "$output" "" "The \"$TERMUX_HOME/.config\" cannot be used as SUDO_POST_SHELL_HOME" "*"
run_sudo_test 43 && \
output="$(sudo "${sudo_v_args[@]}" -si --post-shell-home='$PREFIX/' 'echo "Hi, $1 $2."' "$shell" "shell" 2>&1)"
validate_sudo_test 1 $? "$output" "" "The \"$TERMUX_PREFIX\" cannot be used as SUDO_POST_SHELL_HOME" "*"
run_sudo_test 44 && \
output="$(sudo "${sudo_v_args[@]}" -si --post-shell-home='$PREFIX/home' 'echo "Hi, $1 $2."' "$shell" "shell" 2>&1)"
validate_sudo_test 1 $? "$output" "" "The \"$TERMUX_PREFIX/home\" cannot be used as SUDO_POST_SHELL_HOME" "*"
run_sudo_test 45 && \
output="$(sudo "${sudo_v_args[@]}" -sr "[[ \",\" == \"$COMMA_ALTERNATIVE\" ]] && [[ "\$1" == \"$COMMA_ALTERNATIVE\" ]] && echo \"arg1 equals simple comma\"" "$COMMA_ALTERNATIVE")"
validate_sudo_test 0 $? "$output" "arg1 equals simple comma"
run_sudo_test 46 && \
output="$(sudo "${sudo_v_args[@]}" -si --post-shell="python" --post-shell-stdin-string='exit(0)' 2>&1)"
validate_sudo_test 0 $? "$output" "*" "Python" "*"
### Run zsh tests
if [ -x "$TERMUX_PREFIX/bin/zsh" ]; then
set_sudo_tests_current_test_shell "zsh"
shell="zsh"
run_sudo_test 1 && \
output="$(sudo "${sudo_v_args[@]}" -s --shell="$shell" 'readlink -f /proc/$$/exe')"
validate_sudo_test 0 $? "$output" "$TERMUX_PREFIX/bin/zsh"
run_sudo_test 2 && \
output="$(sudo "${sudo_v_args[@]}" -s --shell="$shell" <(cat <<'SUDO_EOF'
echo "Hi, $1 $2."
SUDO_EOF
) "$shell" "shell"
)"
validate_sudo_test 0 $? "$output" "Hi, $shell shell."
run_sudo_test 3 && \
output="$(sudo "${sudo_v_args[@]}" -s --shell="$shell" --shell-stdin-string="$shell" <(cat <<'SUDO_EOF'
echo "What is your name?"
read name
echo "Hi, $name."
SUDO_EOF
)
)"
validate_sudo_test 0 $? "$output" "What is your name?"$'\n'"Hi, $shell."
else
sudo_tests_log 1 "Skipping zsh tests since its not installed"
fi
### Run dash tests
if [ -x "$TERMUX_PREFIX/bin/dash" ]; then
set_sudo_tests_current_test_shell "dash"
shell="dash"
run_sudo_test 1 && \
output="$(sudo "${sudo_v_args[@]}" -s --shell="$shell" <(cat <<'SUDO_EOF'
echo "Hi, $1 $2."
SUDO_EOF
) "$shell" "shell"
)"
validate_sudo_test 0 $? "$output" "Hi, $shell shell."
run_sudo_test 2 && \
output="$(sudo "${sudo_v_args[@]}" -s --shell="$shell" --shell-stdin-string="$shell" <(cat <<'SUDO_EOF'
echo "What is your name?"
read name
echo "Hi, $name."
SUDO_EOF
)
)"
validate_sudo_test 0 $? "$output" "What is your name?"$'\n'"Hi, $shell."
else
sudo_tests_log 1 "Skipping dash tests since its not installed"
fi
### Run sh tests
if [ -x "$TERMUX_PREFIX/bin/sh" ]; then
set_sudo_tests_current_test_shell "sh"
shell="sh"
run_sudo_test 1 && \
output="$(sudo "${sudo_v_args[@]}" -s --shell="$shell" <(cat <<'SUDO_EOF'
echo "Hi, $1 $2."
SUDO_EOF
) "$shell" "shell"
)"
validate_sudo_test 0 $? "$output" "Hi, $shell shell."
run_sudo_test 2 && \
output="$(sudo "${sudo_v_args[@]}" -s --shell="$shell" --shell-stdin-string="$shell" <(cat <<'SUDO_EOF'
echo "What is your name?"
read name
echo "Hi, $name."
SUDO_EOF
)
)"
validate_sudo_test 0 $? "$output" "What is your name?"$'\n'"Hi, $shell."
else
sudo_tests_log 1 "Skipping sh tests since its not installed"
fi
### Run fish tests
if [ -x "$TERMUX_PREFIX/bin/fish" ]; then
set_sudo_tests_current_test_shell "fish"
shell="fish"
run_sudo_test 1 && \
output="$(sudo "${sudo_v_args[@]}" -s --shell="$shell" 'readlink -f /proc/$fish_pid/exe')"
validate_sudo_test 0 $? "$output" "$TERMUX_PREFIX/bin/fish"
run_sudo_test 2 && \
output="$(sudo "${sudo_v_args[@]}" -s --shell="$shell" <(cat <<'SUDO_EOF'
echo "Hi, $argv[1] $argv[2]."
SUDO_EOF
) "$shell" "shell"
)"
validate_sudo_test 0 $? "$output" "Hi, $shell shell."
run_sudo_test 3 && \
output="$(sudo "${sudo_v_args[@]}" -s --shell="$shell" --shell-stdin-string="$shell" <(cat <<'SUDO_EOF'
echo "What is your name?"
read -p "" name
echo "Hi, $name."
SUDO_EOF
)
)"
validate_sudo_test 0 $? "$output" "What is your name?"$'\n'"Hi, $shell."
else
sudo_tests_log 1 "Skipping fish tests since its not installed"
fi
### Run python tests
if [ -x "$TERMUX_PREFIX/bin/python" ]; then
set_sudo_tests_current_test_shell "python"
shell="python"
run_sudo_test 1 && \