-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.sh
More file actions
executable file
·236 lines (163 loc) · 5.57 KB
/
test.sh
File metadata and controls
executable file
·236 lines (163 loc) · 5.57 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
#! /bin/bash
BIN_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SECURESTORE="$BIN_DIR/securestore"
VCSTORE="$BIN_DIR/vcstore"
PASS="$BIN_DIR/pass"
EDITOR=cat
WORKING_DIR="$(mktemp --directory)"
TMP_FILE="$(mktemp)"
NUM_PASSED=0
NUM_FAILED=0
#
# Helper functions
#
result_of() {
if [ $? -eq 0 ]; then
echo -en "\e[32m ✓ Pass: "
(( NUM_PASSED++ ))
else
echo -en "\e[31m ✗ Fail: "
(( NUM_FAILED++ ))
fi
echo -e "$@" "\e[m"
}
run_test_func() {
echo "$1 Tests"
echo
$2
echo
rm -Rf * .git .gpg_id
}
#
# Test functions
#
test_securestore() {
[[ "$($SECURESTORE 2>&1)" =~ not\ initialised ]]
result_of "Run command"
[[ "$($SECURESTORE init 2>&1)" =~ provide\ a\ GPG\ Key ]]
result_of "Initialise store, no key"
[[ "$($SECURESTORE init $GPG_KEY_ID 2>&1)" =~ ^$ ]]
result_of "Initialise store"
[[ "$($SECURESTORE init $GPG_KEY_ID 2>&1)" =~ already\ initialised ]]
result_of "Reinitialise store"
[[ "$($SECURESTORE list 2>&1)" =~ \. ]]
result_of "List empty store"
[[ "$(ls -a 2>&1)" =~ \.gpg_id ]]
result_of "Verify .gpg_id file exists"
[[ "$($SECURESTORE add Entry $TMP_FILE 2>&1)" =~ ^$ ]]
result_of "Add file to store"
[[ "$($SECURESTORE file Entry 2>&1)" =~ encrypted|^Entry:\ data$ ]]
result_of "Verify entry is encrypted"
[[ "$($SECURESTORE list 2>&1)" =~ Entry ]]
result_of "List store containing single entry"
[[ "$($SECURESTORE move Entry MovedEntry 2>&1)" =~ ^$ ]]
result_of "Move entry"
[[ "$($SECURESTORE list 2>&1)" =~ Entry ]]
result_of "Verify store does not contain old entry"
[[ "$($SECURESTORE list 2>&1)" =~ MovedEntry ]]
result_of "Verify store contains moved entry"
[[ "$($SECURESTORE get MovedEntry 2>&1)" =~ Hello\ World ]]
result_of "Get contents of entry"
OLD_HASH="$(md5sum MovedEntry 2> /dev/null)"
[[ "$($SECURESTORE edit MovedEntry 2>&1)" =~ Hello\ World ]]
result_of "Edit entry"
[[ "$(md5sum MovedEntry 2>&1)" == "$OLD_HASH" ]]
result_of "Verify entry was not re-encrypted (i.e. changed)"
[[ "$($SECURESTORE file MovedEntry 2>&1)" =~ encrypted ]]
result_of "Verify entry is encrypted"
! [[ "$($SECURESTORE strings MovedEntry 2>&1)" =~ Username ]]
result_of "Verify entry is encrypted (alternate method)"
[[ "$($SECURESTORE remove MovedEntry 2>&1)" =~ ^$ ]]
result_of "Remove entry"
! [[ "$($SECURESTORE list 2>&1)" =~ MovedEntry ]]
result_of "Verify store does not contain moved entry"
}
test_vcstore() {
[[ "$($VCSTORE 2>&1)" =~ not\ initialised ]]
result_of "Run command"
[[ "$($VCSTORE init $GPG_KEY_ID 2>&1)" =~ ^$ ]]
result_of "Initialise store"
[[ "$(git status 2>&1)" =~ On\ branch\ master ]]
result_of "Verify git respository was initialised"
[[ "$(git log --oneline 2>&1)" =~ Added\ .gpg_id\ file ]]
result_of "Verify initial commit was made"
[[ "$($VCSTORE add Entry $TMP_FILE 2>&1)" =~ ^$ ]]
result_of "Add file to store"
[[ "$(git log --oneline 2>&1)" =~ Added\ \'Entry\' ]]
result_of "Verify commit was made"
[[ "$($VCSTORE move Entry MovedEntry 2>&1)" =~ ^$ ]]
result_of "Move entry"
[[ "$(git log --oneline 2>&1)" =~ Renamed\ \'Entry\' ]]
result_of "Verify commit was made"
[[ "$($VCSTORE edit MovedEntry 2>&1)" =~ Hello\ World ]]
result_of "Edit entry"
! [[ "$(git log --oneline 2>&1)" =~ Updated\ \'MovedEntry\' ]]
result_of "Verify no commit was made"
[[ "$($VCSTORE file MovedEntry 2>&1)" =~ encrypted ]]
result_of "Verify entry is encrypted"
! [[ "$($VCSTORE strings MovedEntry 2>&1)" =~ Username ]]
result_of "Verify entry is encrypted (alternate method)"
[[ "$($VCSTORE remove MovedEntry 2>&1)" =~ ^$ ]]
result_of "Remove entry"
[[ "$(git log --oneline 2>&1)" =~ Removed\ \'MovedEntry\' ]]
result_of "Verify commit was made"
}
test_pass() {
export STORE_DIR="$WORKING_DIR"
export NEW_PASS_CHARS='a-zA-Z'
export NEW_PASS_LEN=10
cat > .template <<-EOF
Username="TestUser"
Password="{PASSWORD}"
AutoType() {
return 0
}
DoSomething() {
echo -n "Running Function"
}
EOF
[[ "$($PASS 2>&1)" =~ not\ initialised ]]
result_of "Run command"
[[ "$($PASS init $GPG_KEY_ID 2>&1)" =~ ^$ ]]
result_of "Initialise store"
[[ "$($PASS add Entry 2>&1)" =~ ^Username ]]
result_of "Add entry to store"
[[ "$($PASS add 2>&1)" =~ provide\ a\ unique\ name ]]
result_of "Add blank entry to store"
! [[ "$($PASS add OtherEntry 2>&1)" =~ \{PASSWORD\} ]]
result_of "Add entry to store with automated password generation"
[[ "$($PASS list 2>&1)" =~ Entry.+OtherEntry ]]
result_of "List store containing multiple entries"
[[ "$($PASS list-properties Entry 2>&1)" =~ ^Username.Password$ ]]
result_of "List properties of entry"
[[ "$($PASS list-functions Entry 2>&1)" =~ ^AutoType.DoSomething$ ]]
result_of "List functions from entry"
[[ "$($PASS get-property Entry Username 2>&1)" =~ ^TestUser$ ]]
result_of "Get property from entry"
[[ "$($PASS run-function Entry DoSomething 2>&1)" =~ ^Running\ Function$ ]]
result_of "Run function from entry"
[[ "$($PASS file Entry 2>&1)" =~ encrypted ]]
result_of "Verify entry is encrypted"
! [[ "$($PASS strings Entry 2>&1)" =~ Username ]]
result_of "Verify entry is encrypted (alternate method)"
[[ "$($PASS pass_generate_password 2>&1)" =~ ^[a-zA-Z]{10}$ ]]
result_of "Generate password"
}
#
# Initialisation
#
echo "Hello World" > "$TMP_FILE"
if [ -z "$GPG_KEY_ID" ]; then
read -p 'GPG Key ID to use for tests: ' GPG_KEY_ID
fi
#
# Run tests
#
pushd "$WORKING_DIR" &> /dev/null
run_test_func "SecureStore" test_securestore
run_test_func "VCStore" test_vcstore
run_test_func "Pass" test_pass
echo "Passed $NUM_PASSED tests, failed $NUM_FAILED"
popd &> /dev/null
rm -Rf "$WORKING_DIR" "$TMP_FILE"
[ $NUM_FAILED -eq 0 ] && exit 0 || exit 1