-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest-hashdeep.sh
More file actions
executable file
·177 lines (143 loc) · 4.99 KB
/
test-hashdeep.sh
File metadata and controls
executable file
·177 lines (143 loc) · 4.99 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
#!/bin/bash
# Exit on error
set -e
# --- Configuration ---
GREEN='\033[1;32m'
RED='\033[0;31m'
NC='\033[0m'
TEST_DIR="/tmp/hashit-test"
AUDIT_FILE="audit.txt"
# --- Helper Functions ---
print_pass() {
echo -e "${GREEN}PASSED: $1${NC}"
}
print_fail() {
echo -e "${RED}FAILED: $1${NC}"
exit 1
}
# --- Setup and Cleanup ---
setup() {
echo "Setting up test environment in $TEST_DIR..."
rm -rf "$TEST_DIR"
mkdir -p "$TEST_DIR/dir1"
mkdir -p "$TEST_DIR/dir2"
echo "file1" > "$TEST_DIR/dir1/file1.txt"
echo "file2" > "$TEST_DIR/dir1/file2.txt"
echo "file3" > "$TEST_DIR/dir2/file3.txt"
echo "unique" > "$TEST_DIR/unique_file.txt"
echo "Building hashit..."
go build -ldflags="-s -w"
}
cleanup() {
echo "Cleaning up..."
rm -rf "$TEST_DIR"
rm -f "$AUDIT_FILE"
rm -f ./hashit
}
# --- Prerequisite Check ---
check_hashdeep() {
if ! command -v hashdeep &> /dev/null; then
echo "hashdeep could not be found. Please install it to run these tests."
exit 1
fi
echo "hashdeep found."
}
# --- Test Cases ---
test_audit_success_hashit_to_hashdeep() {
echo "Running Test: Audit Success (hashit -> hashdeep)"
./hashit --format hashdeep "$TEST_DIR" > "$AUDIT_FILE"
if hashdeep -l -r -a -k "$AUDIT_FILE" "$TEST_DIR" | grep -q 'Audit passed'; then
print_pass "hashit created a valid audit file for hashdeep"
else
print_fail "hashit did not create a valid audit file for hashdeep"
fi
}
test_audit_success_hashdeep_to_hashit() {
echo "Running Test: Audit Success (hashdeep -> hashit)"
hashdeep -l -r "$TEST_DIR" > "$AUDIT_FILE"
if ./hashit -a "$AUDIT_FILE" "$TEST_DIR" | grep -q 'Audit passed'; then
print_pass "hashit correctly passed a hashdeep audit file"
else
print_fail "hashit did not pass a hashdeep audit file"
fi
}
test_modified_file() {
echo "Running Test: Modified File Detection"
hashdeep -l -r "$TEST_DIR" > "$AUDIT_FILE"
echo "modified" >> "$TEST_DIR/dir1/file1.txt"
output=$(./hashit -a "$AUDIT_FILE" "$TEST_DIR" || true)
if echo "$output" | grep -q 'Audit failed' && echo "$output" | grep -q 'Files modified: 1'; then
print_pass "Correctly detected 1 modified file"
else
print_fail "Failed to detect 1 modified file. Output:\n$output"
fi
}
test_new_file() {
echo "Running Test: New File Detection"
hashdeep -l -r "$TEST_DIR" > "$AUDIT_FILE"
echo "new file" > "$TEST_DIR/new_file.txt"
output=$(./hashit -a "$AUDIT_FILE" "$TEST_DIR" || true)
if echo "$output" | grep -q 'Audit failed' && echo "$output" | grep -q 'New files found: 1'; then
print_pass "Correctly detected 1 new file"
else
print_fail "Failed to detect 1 new file. Output:\n$output"
fi
}
test_missing_file() {
echo "Running Test: Missing File Detection"
hashdeep -l -r "$TEST_DIR" > "$AUDIT_FILE" # This creates audit.txt
rm "$TEST_DIR/dir1/file1.txt" # This deletes a file from the test directory
ls -l "$AUDIT_FILE" # Debugging line
# This is where hashit is called, and it reports audit.txt missing
output=$(./hashit -a "$AUDIT_FILE" "$TEST_DIR" --debug 2>&1 || true)
# The current implementation will show 1 missing file, which is what we want to test against.
# When the logic is improved, this test should still pass but for a different reason.
if echo "$output" | grep -q 'Audit failed' && echo "$output" | grep -q 'Files missing: 1'; then
print_pass "Correctly detected 1 missing file"
else
print_fail "Failed to detect 1 missing file. Output:\n$output"
fi
}
test_moved_file() {
echo "Running Test: Moved File Detection"
hashdeep -l -r "$TEST_DIR" > "$AUDIT_FILE"
mv "$TEST_DIR/dir1/file1.txt" "$TEST_DIR/dir2/file1_moved.txt"
output=$(./hashit -a "$AUDIT_FILE" "$TEST_DIR" || true)
# This test WILL FAIL until the TODOs are implemented.
# The desired output is 'Files moved: 1'.
# The current (broken) output will be 'New files found: 1' and 'Files missing: 1'.
if echo "$output" | grep -q 'Audit failed' && echo "$output" | grep -q 'Files moved: 1'; then
print_pass "Correctly detected 1 moved file"
else
echo "NOTE: This test is expected to fail until the audit logic is implemented."
print_fail "Failed to detect 1 moved file. Output:\n$output"
fi
}
# --- Main Execution ---
trap cleanup EXIT
check_hashdeep
# Run tests
setup
test_audit_success_hashit_to_hashdeep
cleanup
setup
test_audit_success_hashdeep_to_hashit
cleanup
setup
test_modified_file
cleanup
setup
test_new_file
cleanup
setup
test_missing_file
cleanup
# This test is expected to fail for now.
# I'm including it as per the plan.
# If you want me to remove it until the logic is implemented, let me know.
setup
test_moved_file
cleanup
echo -e "${GREEN}================================================="
echo -e "ALL HASHDEEP TESTS PASSED (or failed as expected)"
echo -e "================================================="