Initial commit.

This commit is contained in:
Darren 'Tadgy' Austin 2019-07-16 12:41:49 +01:00
commit 2dda2e227d
52 changed files with 2704 additions and 0 deletions

View file

@ -0,0 +1,28 @@
DIR=$(dirname $0)
cd $DIR
TESTS=$(ls test*.sh | grep -v test.sh | sed 's/\.sh$//')
for test in $TESTS
do
bash $test.sh &> $test.out
# bash $test.sh >$test.out 2>$test.err
# Fail and bail out if test didn't pass
PASSED=$(diff $test.out $test.out.correct 2>&1)
if [ ! -z "$PASSED" ]
then
echo "Test $test failed. Output is in $DIR/$test.out"
exit 1
else
rm -rf $test.out
fi
done
echo "All tests passed"

View file

@ -0,0 +1,40 @@
; Comment beginning with ';'
# Comment beginning with '#'
; var1 - simple var
var1=VAR 1
; var2 - space before '='; leading space that should be stripped
var2 = VAR 2
; var3 - with leading and trailing space that should be stripped
var3 = VAR 3
; var4 - value in double quotes
var4="VAR 4"
; var5 - value in double quotes; value's leading and trailing whitespace should
; be preserved; leading and trailing whitespace before/after double quotes should
; not become part of the value
var5 = " VAR 5 "
; var6 - value in double quotes; value's leading and trailing whitespace should
; be preserved; leading and trailing whitespace before/after double quotes should
; not become part of the value
var6 = ' VAR 6 '
; var7 - value contains a single double quote
var7 = VAR " 7
; var8 - value contains a single single quote
var8 = VAR ' 8
; var9 - leading whitespace before variable name
var9=VAR 9
; var10 - leading whitespace before variable name
var10 = VAR 10

View file

@ -0,0 +1,10 @@
var1:VAR 1
var2:VAR 2
var3:VAR 3
var4:VAR 4
var5: VAR 5
var6: VAR 6
var7:VAR " 7
var8:VAR ' 8
var9:VAR 9
var10:VAR 10

View file

@ -0,0 +1,21 @@
# Test 1
#
# Basic variable values; leading/trailing whitespace; comments
. ../read_ini.sh
read_ini test1.ini
#echo "a:LOCALVAR=$LOCALVAR"
echo "var1:$INI__var1"
echo "var2:$INI__var2"
echo "var3:$INI__var3"
echo "var4:$INI__var4"
echo "var5:$INI__var5"
echo "var6:$INI__var6"
echo "var7:$INI__var7"
echo "var8:$INI__var8"
echo "var9:$INI__var9"
echo "var10:$INI__var10"

View file

@ -0,0 +1,8 @@
var1=VAR1
; Invalid line - should throw an error and stop processing
INVALID LINE
var2=VAR2

View file

@ -0,0 +1,2 @@
Error: Invalid line:
6: INVALID LINE

View file

@ -0,0 +1,8 @@
# Test 2
#
# Invalid line
. ../read_ini.sh
read_ini test2.ini

View file

@ -0,0 +1,23 @@
; Testing sections
var1=VAR 1
var2= "VAR 2 "
[section1]
var1="section 1 VAR 1"
var2= "section 1 VAR 2"
var3 = "section 1 VAR 3 "
[section2]
var1 = section 2 VAR 1
var2=" section 2 VAR 2"
var3=section 2 VAR 3
var4=section 2 VAR 4
var5=section 2 VAR 5
[section3]
var1="section 3 VAR 1"
var2= "section 3 VAR 2"

View file

@ -0,0 +1,12 @@
var1:VAR 1
var2:VAR 2
setion1_var1:section 1 VAR 1
setion1_var2:section 1 VAR 2
setion1_var3:section 1 VAR 3
setion2_var1:section 2 VAR 1
setion2_var2: section 2 VAR 2
setion2_var3:section 2 VAR 3
setion2_var4:section 2 VAR 4
setion2_var5:section 2 VAR 5
setion3_var1:section 3 VAR 1
setion3_var2:section 3 VAR 2

View file

@ -0,0 +1,25 @@
# Test 3
#
# Sections
. ../read_ini.sh
read_ini test3.ini
echo "var1:$INI__var1"
echo "var2:$INI__var2"
echo "setion1_var1:$INI__section1__var1"
echo "setion1_var2:$INI__section1__var2"
echo "setion1_var3:$INI__section1__var3"
echo "setion2_var1:$INI__section2__var1"
echo "setion2_var2:$INI__section2__var2"
echo "setion2_var3:$INI__section2__var3"
echo "setion2_var4:$INI__section2__var4"
echo "setion2_var5:$INI__section2__var5"
echo "setion3_var1:$INI__section3__var1"
echo "setion3_var2:$INI__section3__var2"

View file

@ -0,0 +1,14 @@
; Testing dots in variable names (these get converted to underscores in
; output variable names)
var.1 = "VAR 1"
var.two="VAR 2"
var.3.two_dots="VAR 3"
[section1]
var.1="section 1 VAR 1"
var.two="section 1 VAR 2"

View file

@ -0,0 +1,5 @@
var_1:VAR 1
var_two:VAR 2
var_3_two_dots:VAR 3
section 1 var_1:section 1 VAR 1
section 1 var_two:section 1 VAR 2

View file

@ -0,0 +1,15 @@
# Test 4
#
# Dots in variable names (converted to underscores in output var names)
. ../read_ini.sh
read_ini test4.ini
echo "var_1:$INI__var_1"
echo "var_two:$INI__var_two"
echo "var_3_two_dots:$INI__var_3_two_dots"
echo "section 1 var_1:$INI__section1__var_1"
echo "section 1 var_two:$INI__section1__var_two"

View file

@ -0,0 +1,13 @@
; Testing prevention of in-value code execution via eval
var1 = blah blah `ls` blah
var2 = blah blah $(ls) blah
; these code injections worked on 0.3
var3 = blah blah \\`ls\\` blah
var4 = blah blah \\$(ls) blah
; these code injections could work with read -r
var5 = blah blah \`ls\` blah
var6 = blah blah \$(ls) blah

View file

@ -0,0 +1,6 @@
var1:blah blah `ls` blah
var2:blah blah $(ls) blah
var3:blah blah \\`ls\\` blah
var4:blah blah \\$(ls) blah
var5:blah blah \`ls\` blah
var6:blah blah \$(ls) blah

View file

@ -0,0 +1,16 @@
# Test 5
#
# Prevention of in-value code execution via backticks or $() notation.
# This must be done because the value is run through an eval statement.
. ../read_ini.sh
read_ini test5.ini
echo "var1:$INI__var1"
echo "var2:$INI__var2"
echo "var3:$INI__var3"
echo "var4:$INI__var4"
echo "var5:$INI__var5"
echo "var6:$INI__var6"

View file

@ -0,0 +1,27 @@
; Testing processing of special boolean constants: unquoted
; strings 'yes', 'true' and 'on' are interpreted as 1;
; unquoted strings 'no', 'false' and 'off' as 0.
yes1 = yes
yes2 = "yes"
yes3 = Yes
true1=true
true2= true
true3 = "tRuE"
on1 = on
on2 = oN
on3 = "ON"
no1 = no
no2 = "no"
no3=No
false1 = false
false2 = faLSe
false3 = "FALSE"
off1 = off
off2 = Off
off3 = "off"

View file

@ -0,0 +1,30 @@
yes1:1
yes2:yes
yes3:1
true1:1
true2:1
true3:tRuE
on1:1
on2:1
on3:ON
no1:0
no2:no
no3:0
false1:0
false2:0
false3:FALSE
off1:0
off2:0
off3:off
yes1:1
true2:1
on3:ON
no3:0
false2:0
off1:0
yes1:yes
true2:true
on3:ON
no3:No
false2:faLSe
off1:off

View file

@ -0,0 +1,56 @@
# Testing booleans processing
# First: with default booleans processing (on)
. ../read_ini.sh
read_ini test6.ini
echo "yes1:$INI__yes1"
echo "yes2:$INI__yes2"
echo "yes3:$INI__yes3"
echo "true1:$INI__true1"
echo "true2:$INI__true2"
echo "true3:$INI__true3"
echo "on1:$INI__on1"
echo "on2:$INI__on2"
echo "on3:$INI__on3"
echo "no1:$INI__no1"
echo "no2:$INI__no2"
echo "no3:$INI__no3"
echo "false1:$INI__false1"
echo "false2:$INI__false2"
echo "false3:$INI__false3"
echo "off1:$INI__off1"
echo "off2:$INI__off2"
echo "off3:$INI__off3"
# Second: with booleans processing explicitly turned on via "--booleans 1"
. ../read_ini.sh
read_ini --booleans 1 test6.ini
echo "yes1:$INI__yes1"
echo "true2:$INI__true2"
echo "on3:$INI__on3"
echo "no3:$INI__no3"
echo "false2:$INI__false2"
echo "off1:$INI__off1"
# Third: with booleans processing explicity switched off via "--booleans 0"
. ../read_ini.sh
read_ini test6.ini --booleans 0
echo "yes1:$INI__yes1"
echo "true2:$INI__true2"
echo "on3:$INI__on3"
echo "no3:$INI__no3"
echo "false2:$INI__false2"
echo "off1:$INI__off1"

View file

@ -0,0 +1,8 @@
; Simple definitions for testing --prefix option
var1=VAR 1
[section1]
var1 = "section 1 VAR 1"

View file

@ -0,0 +1,7 @@
# 1
var1:VAR 1
section1 var1:section 1 VAR 1
# 2
read_ini: invalid prefix 'PR:EFIX,'
# 3
read_ini: invalid prefix '1PREFIX'

View file

@ -0,0 +1,30 @@
# Test 7
#
# Option: [--prefix | -p] PREFIX
# First: valid value for prefix
echo "# 1"
. ../read_ini.sh
read_ini test7.ini --prefix PREFIX1
echo "var1:$PREFIX1__var1"
echo "section1 var1:$PREFIX1__section1__var1"
# Second: invalid value for prefix (contains illegal chars)
echo "# 2"
. ../read_ini.sh
read_ini -p PR:EFIX, test7.ini &&
{
echo "var1:$PREFIX1__var1"
echo "section1 var1:$PREFIX1__section1__var1"
}
# Third: invalid value for prefix (begins with a number)
echo "# 3"
. ../read_ini.sh
read_ini --prefix 1PREFIX test7.ini &&
{
echo "var1:$PREFIX1__var1"
echo "section1 var1:$PREFIX1__section1__var1"
}

View file

@ -0,0 +1,4 @@
; this code injection via section worked on 0.3
[x=blah; ls ;]
var1 = blah

View file

@ -0,0 +1,2 @@
Error: Invalid line:
3: [x=blah; ls ;]

View file

@ -0,0 +1,9 @@
# Test 8
#
# Prevention of in-value code execution via invalid section
. ../read_ini.sh
read_ini test8.ini

View file

@ -0,0 +1,12 @@
; Testing quoted quotes (didnt't worked in 0.3)
; code injection
var1="" ls "."
var2='' ls '.'
; or just an error: unexpected EOF while looking for matching `"'
var3="""
var4='''
var5="
var6='

View file

@ -0,0 +1,6 @@
var1:" ls ".
var2:' ls '.
var3:"
var4:'
var5:"
var6:'

View file

@ -0,0 +1,16 @@
# Test 9
#
# further stuff which didn't worked in 0.3
. ../read_ini.sh
if ! read_ini test9.ini ;then exit 1; fi
echo "var1:$INI__var1"
echo "var2:$INI__var2"
echo "var3:$INI__var3"
echo "var4:$INI__var4"
echo "var5:$INI__var5"
echo "var6:$INI__var6"