aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLaurent Ghigonis <laurent@p1sec.com>2013-06-18 03:52:07 +0200
committerLaurent Ghigonis <laurent@p1sec.com>2013-06-18 03:52:07 +0200
commit9b087ec220c4da8cf962a886d66706efb6394bc9 (patch)
treeb3dab577b34770a1f4dfb9b056d4cda69179f5ea
parentjsaccess: WIP store, getting there (diff)
downloadlaurent-tools-9b087ec220c4da8cf962a886d66706efb6394bc9.tar.xz
laurent-tools-9b087ec220c4da8cf962a886d66706efb6394bc9.zip
jsaccess: store.sh working + unittests
-rwxr-xr-xjsaccess/store.sh65
-rw-r--r--jsaccess/tests/example.txt2
-rwxr-xr-xjsaccess/tests/test_store_local.sh39
3 files changed, 77 insertions, 29 deletions
diff --git a/jsaccess/store.sh b/jsaccess/store.sh
index b49c3e7..2b6a2c4 100755
--- a/jsaccess/store.sh
+++ b/jsaccess/store.sh
@@ -45,20 +45,22 @@ usage_exit() {
echo "By default store is ./store/ or ./jsa/store/"
echo "Use \"unset HISTFILE; export JSA_PASS=mypass\" to avoid typing the passphrase"
echo "Use \"unset JSA_PASS\" to forget the passphrase"
- exit 1
+ clean_exit 1
}
-cleanup() {
+clean_exit() {
+ ret=9
+ [ X"$1" != X"" ] && ret=$1
rm -f $tmp
umask $sumask
- exit 0
+ exit $ret
}
confirm_exit() {
if [ X"$JSA_FORCE" = X"" ]; then
echo -n "Are you sure ? [y/N] "
read r
- [ X"$r" != "y" ] && exit 0
+ [ X"$r" != X"y" ] && clean_exit 0
fi
}
@@ -69,8 +71,10 @@ _store_get() {
[[ X"$1" != X"" ]] && store=$1 # priority 1
[[ -z $store ]] && echo "ERROR: store not found !" && \
echo "Not specified as argument and local stores" \
- "./store/ or ./jsa/store/ not found" && exit 2
- echo "Using store $store"
+ "./store/ or ./jsa/store/ not found" && clean_exit 1
+ [[ ! -d $store ]] && echo "ERROR: specified store does not exist !" && \
+ echo "Cannot access $store" && clean_exit 1
+ echo "Using store \"$store\""
}
_pass_read() {
@@ -81,13 +85,14 @@ _pass_read() {
echo -n "> "
read pass
fi
+ [ -z $pass ] && echo "ERROR: empty passphrase" && clean_exit 1
enc_dir_hash=`echo -n $pass |openssl rmd160 |cut -d' ' -f2`
enc_path="$store/$enc_dir_hash"
}
_index_decrypt() {
if [ -f $enc_path/index.txt ]; then
- echo -n $pass |openssl enc -d -a -aes-256-cbc -in $enc_path/index.txt -out $tmp -pass stdin ||exit $2
+ echo -n $pass |openssl enc -d -a -aes-256-cbc -in $enc_path/index.txt -out $tmp -pass stdin ||clean_exit 2
else
echo > $tmp
fi
@@ -95,8 +100,8 @@ _index_decrypt() {
_index_encrypt() {
rm -f $enc_path/index.txt
- echo -n $pass |openssl enc -e -a -aes-256-cbc -in $tmp -out $enc_path/index.txt -pass stdin ||exit $2
- echo "UPDATED $enc_path/index.txt"
+ echo -n $pass |openssl enc -e -a -aes-256-cbc -in $tmp -out $enc_path/index.txt -pass stdin ||clean_exit 2
+ echo "UPDATED file $enc_path/index.txt"
}
__file_get_encname() {
@@ -111,18 +116,18 @@ _file_add() {
if [ ! -d $enc_path ]; then
mkdir -p $enc_path
touch $enc_path/index.html
- echo "CREATED $enc_path (new passphrase)"
+ echo "CREATED directory $enc_path (new passphrase)"
fi
- base64 -w0 $clear_path > $tmp ||exit 2
- echo -n $pass |openssl enc -e -a -aes-256-cbc -in $tmp -out $enc_path/$enc_name -pass stdin ||exit 2
- echo "CREATED $enc_path/$enc_name"
+ base64 -w0 $clear_path > $tmp ||clean_exit 2
+ echo -n $pass |openssl enc -e -a -aes-256-cbc -in $tmp -out $enc_path/$enc_name -pass stdin ||clean_exit 2
+ echo "CREATED file $enc_path/$enc_name"
}
_file_rm() {
- clear_name=$2
+ clear_name=$1
__file_get_encname $clear_name
- rm $enc_path/$enc_name ||exit 1
- echo "DELETED $enc_path/$enc_name"
+ rm $enc_path/$enc_name ||clean_exit 1
+ echo "DELETED file $enc_path/$enc_name"
}
_rset() {
@@ -137,7 +142,8 @@ _rset() {
_rget() {
[ ! -f $store/.rsync_uri ] && echo "ERROR: no rsync_uri set !" && \
- echo "set it with \"store.sh rset <rsync_uri> $store\"" && exit 1
+ echo "set it with \"store.sh rset <rsync_uri> $store\"" && \
+ clean_exit 1
rsync_uri=`cat $store/.rsync_uri`
}
@@ -145,7 +151,7 @@ action_ls() {
_pass_read
_index_decrypt
[ ! -f $enc_path/index.txt ] && \
- echo "Passphrase not used in store !" && exit 1
+ echo "Passphrase not used in store !" && clean_exit 1
echo "$enc_dir_hash/index.txt:"
cat $tmp
}
@@ -157,7 +163,7 @@ action_add() {
_index_decrypt
if [ `egrep -c "^$clear_name$" $tmp` -ne 0 ]; then
echo "File already encrypted with this passphrase"
- exit 1
+ clean_exit 1
fi
_file_add $clear_path $clear_name
_index_decrypt
@@ -166,17 +172,16 @@ action_add() {
}
action_rm() {
- clear_path=$1
- clear_name=`basename $clear_path`
+ clear_name=$1
_pass_read
_index_decrypt
if [ `egrep -c "^$clear_name$" $tmp` -eq 0 ]; then
echo "File does not exist for this passphrase"
- exit 1
+ clean_exit 1
fi
_file_rm $clear_name
_index_decrypt
- sed -i d/$1/ $tmp
+ sed -i /^$clear_name$/d $tmp
_index_encrypt
}
@@ -185,19 +190,21 @@ action_rmall() {
echo "This will delete all file encrypted with this passphrase"
confirm_exit
rm -rf $enc_path
+ echo "DELETED directory $enc_path"
}
action_init() {
store=$1
- mkdir $store ||exit 1
+ mkdir $store ||clean_exit 1
echo "The monster has emptied me !" > $store/index.html
- echo "CREATED store $store"
+ echo "CREATED store \"$store\""
}
action_wipe() {
echo "This will delete all file encrypted with all passphrases"
confirm_exit
rm -rf $store
+ echo "DELETED store \"$store\""
}
action_rset() {
@@ -223,14 +230,14 @@ action_pull() {
if [ X"`which base64`" == X"" \
-o X"`which openssl`" == X"" ]; then
echo "You need to have openssl and base64 available in your path !"
- exit 1
+ clean_exit 1
fi
# Initialize temporary stuff
sumask=$(umask)
umask 077
tmp=`mktemp ./jsaXXXXXXXX` # Used for storing index / new files
-trap cleanup INT TERM EXIT
+trap clean_exit INT TERM
# Run action
case $1 in
@@ -247,7 +254,7 @@ add)
rm)
[ $# -ne 2 -a $# -ne 3 ] && usage_exit
_store_get $3
- action_rm $1
+ action_rm $2
;;
rmall)
[ $# -ne 1 -a $# -ne 2 ] && usage_exit
@@ -301,5 +308,5 @@ version|-V)
action_add $1
esac
-# cleanup() executed in trap
+clean_exit 0
diff --git a/jsaccess/tests/example.txt b/jsaccess/tests/example.txt
new file mode 100644
index 0000000..475f789
--- /dev/null
+++ b/jsaccess/tests/example.txt
@@ -0,0 +1,2 @@
+I am an example.
+Don't laugh and follow me.
diff --git a/jsaccess/tests/test_store_local.sh b/jsaccess/tests/test_store_local.sh
new file mode 100755
index 0000000..0cec47a
--- /dev/null
+++ b/jsaccess/tests/test_store_local.sh
@@ -0,0 +1,39 @@
+#!/bin/sh
+
+# Unittests for jsaccess store.sh
+
+storesh=../store.sh
+TMP=tmp
+export JSA_PASS=jsa_unittest_passphrase
+export JSA_FORCE=1
+
+$storesh init store ||exit 1
+[ -d store ] ||exit 2
+
+$storesh add example.txt ||exit 10
+[ -d /home/user/code/laurent-tools/jsaccess/tests/store/ad2c5eb7c4fca722235f5df80e11fa619adbd533/ ] ||exit 11
+[ -f /home/user/code/laurent-tools/jsaccess/tests/store/ad2c5eb7c4fca722235f5df80e11fa619adbd533/8e895f3f4317fb442747a40b9025d6ad8c9c8cf3 ] ||exit 12
+
+$storesh ls > $TMP ||exit 20
+[ `grep -c "example.txt" $TMP` -eq 1 ] || exit 21
+rm $TMP
+
+$storesh rm example.txt ||exit 30
+[ ! -f /home/user/code/laurent-tools/jsaccess/tests/store/ad2c5eb7c4fca722235f5df80e11fa619adbd533/8e895f3f4317fb442747a40b9025d6ad8c9c8cf3 ] ||exit 31
+
+$storesh ls > $TMP ||exit 40
+[ `grep -c "example.txt" $TMP` -eq 0 ] || exit 41
+rm $TMP
+
+$storesh add example.txt ||exit 50
+[ -d /home/user/code/laurent-tools/jsaccess/tests/store/ad2c5eb7c4fca722235f5df80e11fa619adbd533/ ] ||exit 51
+[ -f /home/user/code/laurent-tools/jsaccess/tests/store/ad2c5eb7c4fca722235f5df80e11fa619adbd533/8e895f3f4317fb442747a40b9025d6ad8c9c8cf3 ] ||exit 52
+
+$storesh rmall ||exit 60
+[ ! -d /home/user/code/laurent-tools/jsaccess/tests/store/ad2c5eb7c4fca722235f5df80e11fa619adbd533/ ] ||exit 61
+
+$storesh wipe store ||exit 70
+[ ! -d store ] ||exit 71
+
+echo TEST OK
+exit 0