Skip to content

Commit b34bb22

Browse files
committed
ret vals for zip/unzip changed, minor chamges in main, new test cases, test code updated
1 parent 73174c3 commit b34bb22

5 files changed

Lines changed: 77 additions & 54 deletions

File tree

build/unzip

24.7 KB
Binary file not shown.

build/zip

25.7 KB
Binary file not shown.

src/unzip.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,8 @@ int main(int argc, char** argv) {
2222
}
2323

2424
if (config.hFlag) {
25-
retCode = ERROR;
2625
printInfo();
27-
}
28-
29-
if (retCode == (int)OK) {
26+
} else if (retCode == (int)OK) {
3027
// filename or filename + archive name
3128
if (argc >= 1 && argc <= 2) {
3229
const char* archive_filename = argv[0];
@@ -35,9 +32,10 @@ int main(int argc, char** argv) {
3532
} else {
3633
if (!config.hFlag) fprintf(stderr, USAGE);
3734
}
38-
} else if (retCode == ERROR) {
39-
if (!config.hFlag) fprintf(stderr, USAGE);
35+
} else {
36+
fprintf(stderr, USAGE);
4037
}
38+
4139
return retCode;
4240
}
4341

@@ -74,22 +72,22 @@ int unzip(const char* archive_filename, const char* filename,
7472
unsigned char hash_start[SHA256_DIGEST_LENGTH];
7573
if (calculate_file_hash(archive_filename, hash_start) == -1) {
7674
perror("Error while hashing files");
77-
return EXIT_FAILURE;
75+
return (int)ERROR;
7876
}
7977

8078
// open archive file for reading
8179
int archive_fd = open(archive_filename, O_RDONLY);
8280
if (archive_fd == -1) {
8381
perror("Error opening input file");
84-
return EXIT_FAILURE;
82+
return (int)ERROR;
8583
}
8684

8785
// getting input file stats
8886
struct stat st;
8987
if (fstat(archive_fd, &st) == -1) {
9088
perror("Error getting file size");
9189
close(archive_fd);
92-
return EXIT_FAILURE;
90+
return (int)ERROR;
9391
}
9492

9593
// open output file for writing
@@ -103,7 +101,7 @@ int unzip(const char* archive_filename, const char* filename,
103101
perror("Error opening output file");
104102
}
105103
close(archive_fd);
106-
return EXIT_FAILURE;
104+
return (int)ERROR;
107105
}
108106

109107
// getting archive file size
@@ -120,7 +118,7 @@ int unzip(const char* archive_filename, const char* filename,
120118
perror("Error mapping input file to memory");
121119
close(archive_fd);
122120
close(output_fd);
123-
return EXIT_FAILURE;
121+
return (int)ERROR;
124122
}
125123

126124
// struct init for decompression
@@ -132,7 +130,7 @@ int unzip(const char* archive_filename, const char* filename,
132130
munmap(archive_data, input_size);
133131
close(archive_fd);
134132
close(output_fd);
135-
return EXIT_FAILURE;
133+
return (int)ERROR;
136134
}
137135

138136
// translating archive data ptr and its size to struct
@@ -152,7 +150,7 @@ int unzip(const char* archive_filename, const char* filename,
152150
munmap(archive_data, input_size);
153151
close(archive_fd);
154152
close(output_fd);
155-
return EXIT_FAILURE;
153+
return (int)ERROR;
156154
}
157155
write(output_fd, out, DEFAULT_CHUNK_SIZE - stream.avail_out);
158156
} while (ret != Z_STREAM_END);
@@ -164,7 +162,7 @@ int unzip(const char* archive_filename, const char* filename,
164162
munmap(archive_data, input_size);
165163
close(archive_fd);
166164
close(output_fd);
167-
return EXIT_FAILURE;
165+
return (int)ERROR;
168166
}
169167

170168
// checking hashes (archive at start <-> archive at end)
@@ -173,7 +171,7 @@ int unzip(const char* archive_filename, const char* filename,
173171
munmap(archive_data, input_size);
174172
close(archive_fd);
175173
close(output_fd);
176-
return EXIT_FAILURE;
174+
return (int)ERROR;
177175
}
178176

179177
// free

src/zip.c

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@ int main(int argc, char** argv) {
2323
}
2424

2525
if (config.hFlag) {
26-
retCode = ERROR;
2726
printInfo();
28-
}
29-
30-
if (retCode == (int)OK) {
27+
28+
} else if (retCode == (int)OK) {
3129
// filename or filename + archive name
3230
if (argc >= 1 && argc <= 2) {
3331
const char* input_filename = argv[0];
@@ -37,10 +35,10 @@ int main(int argc, char** argv) {
3735
}
3836
retCode = zip(input_filename, archive_filename, config);
3937
} else {
40-
if (!config.hFlag) fprintf(stderr, USAGE);
38+
fprintf(stderr, USAGE);
4139
}
42-
} else if (retCode == ERROR) {
43-
if (!config.hFlag) fprintf(stderr, USAGE);
40+
} else {
41+
fprintf(stderr, USAGE);
4442
}
4543

4644
return retCode;
@@ -99,22 +97,22 @@ int zip(const char* filename, const char* archive_filename,
9997
unsigned char hash_start[SHA256_DIGEST_LENGTH];
10098
if (calculate_file_hash(filename, hash_start) == -1) {
10199
perror("Error hashing files");
102-
return EXIT_FAILURE;
100+
return (int)ERROR;
103101
}
104102

105103
// open input file for reading
106104
int input_fd = open(filename, O_RDONLY);
107105
if (input_fd == -1) {
108106
perror("Error opening input file");
109-
return EXIT_FAILURE;
107+
return (int)ERROR;
110108
}
111109

112110
// getting input file stats
113111
struct stat st;
114112
if (fstat(input_fd, &st) == -1) {
115113
perror("Error getting file size");
116114
close(input_fd);
117-
return EXIT_FAILURE;
115+
return (int)ERROR;
118116
}
119117

120118
// open archive file for writing
@@ -129,7 +127,7 @@ int zip(const char* filename, const char* archive_filename,
129127
perror("Error opening output file");
130128
}
131129
close(input_fd);
132-
return EXIT_FAILURE;
130+
return (int)ERROR;
133131
}
134132

135133
// getting input file size
@@ -142,7 +140,7 @@ int zip(const char* filename, const char* archive_filename,
142140
perror("Error mapping input file to memory");
143141
close(input_fd);
144142
close(archive_fd);
145-
return EXIT_FAILURE;
143+
return (int)ERROR;
146144
}
147145

148146
// struct init for compression
@@ -154,7 +152,7 @@ int zip(const char* filename, const char* archive_filename,
154152
munmap(input_data, input_size);
155153
close(input_fd);
156154
close(archive_fd);
157-
return EXIT_FAILURE;
155+
return (int)ERROR;
158156
}
159157

160158
// passing input data ptr and its size to struct
@@ -172,7 +170,7 @@ int zip(const char* filename, const char* archive_filename,
172170
munmap(input_data, input_size);
173171
close(input_fd);
174172
close(archive_fd);
175-
return EXIT_FAILURE;
173+
return (int)ERROR;
176174
}
177175
write(archive_fd, out, config.chunksNumber - stream.avail_out);
178176
} while (stream.avail_out == 0);
@@ -184,7 +182,7 @@ int zip(const char* filename, const char* archive_filename,
184182
munmap(input_data, input_size);
185183
close(input_fd);
186184
close(archive_fd);
187-
return EXIT_FAILURE;
185+
return (int)ERROR;
188186
}
189187

190188
// checking hashes (file at start <-> file at end)
@@ -193,7 +191,7 @@ int zip(const char* filename, const char* archive_filename,
193191
munmap(input_data, input_size);
194192
close(input_fd);
195193
close(archive_fd);
196-
return EXIT_FAILURE;
194+
return (int)ERROR;
197195
}
198196

199197
// free

tests/test.sh

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ filenames=(
55
"tests/files/test2.txt"
66
)
77

8+
filename_not_existing=(
9+
"aaaooo.txt"
10+
""
11+
)
12+
813
zip_name="tests/files/test.zip"
914
unzip_name="tests/files/test.unzip"
1015
log_name="tests/files/output.file"
@@ -45,28 +50,35 @@ check_leaks() {
4550
}
4651

4752
# running with valgrind zip and unzip using as args for zip $1 and $2 for unzip, $3 is error/no_error code
53+
# usage: zip_unzip [zip_flags] [unzip_flags] [result] [filename_list_elements]
4854
zip_unzip() {
55+
local zip_flags="$1"
56+
local unzip_flags="$2"
57+
local result="$3"
58+
shift 3
59+
local current_filenames=("$@") # list of filename array elements
60+
4961
# executing test files in order
50-
for test_file in ${filenames[@]}
51-
do
52-
valgrind --log-file=${log_name} --leak-check=yes --tool=memcheck -s ./${ZIP_APP} ${test_file} ${zip_name} $1 > ${log_name}
62+
for test_file in "${current_filenames[@]}"; do
63+
valgrind --log-file=${log_name} --leak-check=yes --tool=memcheck -s ./${ZIP_APP} ${test_file} ${zip_name} ${zip_flags} > ${log_name}
5364
ZIP_RET_CODE=$?
5465
check_leaks ${log_name}
55-
56-
# dont execute if zipping ended with error
57-
if [ "${ZIP_RET_CODE}" -eq 0 ]; then
58-
valgrind --log-file=${log_name} --leak-check=yes --tool=memcheck -s ./${UNZIP_APP} ${zip_name} ${unzip_name} $2 > ${log_name}
66+
67+
# execute unzip if zipping ended without error and called without -h
68+
if [ "${ZIP_RET_CODE}" -eq 0 ] && grep -vq 'h' <<< ${zip_flags}; then
69+
valgrind --log-file=${log_name} --leak-check=yes --tool=memcheck -s ./${UNZIP_APP} ${zip_name} ${unzip_name} ${unzip_flags} > ${log_name}
5970
UNZIP_RET_CODE=$?
6071
check_leaks ${log_name}
61-
if [ "${UNZIP_RET_CODE}" -eq 0 ]; then
72+
# compare files if unzip ended without error and called without -h
73+
if [ "${UNZIP_RET_CODE}" -eq 0 ] && grep -vq 'h' <<< ${unzip_flags}; then
6274
compare_files ${test_file} ${unzip_name}
6375
fi
6476
fi
6577

6678
# if one of two is error and error expected -- SUCCESS, else -- FAIL
6779
if [ "${ZIP_RET_CODE}" -ne 0 ] || [ "${UNZIP_RET_CODE}" -ne 0 ]; then
6880
(( count++ ))
69-
if [ $3 -ne 0 ]; then
81+
if [ ${result} -ne 0 ]; then
7082
(( success++ ))
7183
echo "-------------------"
7284
echo "test $count success"
@@ -77,8 +89,16 @@ zip_unzip() {
7789
echo "-------------------"
7890
echo "test $count fail"
7991
echo "-------------------"
80-
echo PARAMS ARE $1 $2 $3
92+
echo PARAMS ARE ${zip_flags} ${unzip_flags} ${result}
8193
fi
94+
95+
elif grep -q 'h' <<< ${zip_flags} || grep -q 'h' <<< ${unzip_flags} ; then
96+
(( count++ ))
97+
(( success++ ))
98+
echo "-------------------"
99+
echo "test $count success"
100+
echo "-------------------"
101+
echo
82102
fi
83103

84104
sudo rm -f ${zip_name} ${unzip_name} ${log_name} tests/files/*.zip tests/files/*.unzip
@@ -97,21 +117,28 @@ if [ -e ${ZIP_APP} ] && [ -e ${UNZIP_APP} ]; then
97117
fi
98118
if [ -e ${log_name} ]; then
99119
rm ${log_name}
100-
fi
120+
fis
101121

102122
# test cases: args of zip, args of unzip and expected error(1)/no_error(0)
103-
zip_unzip "" "" "0"
104-
zip_unzip "-f -c 100" "-f" "0"
105-
zip_unzip "-f -c 1" "-f" "0"
106-
zip_unzip "-f" "-f" "0"
107-
zip_unzip "-f -c -400" "-f" "1"
108-
zip_unzip "-f -c 99999999999999999999999999" "-f" "1"
109-
zip_unzip "-f -h" "" "1"
110-
zip_unzip "-f" "" "0"
111-
zip_unzip "" "-f" "0"
112-
zip_unzip "-fabc" "-f" "1"
113-
zip_unzip "-f" "-fa" "1"
114-
zip_unzip "-f" "-h" "1"
123+
zip_unzip "" "" "0" "${filenames[@]}"
124+
zip_unzip "" "" "1" "${filename_not_existing[@]}"
125+
zip_unzip "-f -c 100" "-f" "0" "${filenames[@]}"
126+
zip_unzip "-f -c 100" "-f" "1" "${filename_not_existing[@]}"
127+
zip_unzip "-f -c 1" "-f" "0" "${filenames[@]}"
128+
zip_unzip "-f -c 1" "-f" "1" "${filename_not_existing[@]}"
129+
zip_unzip "-f" "-f" "0" "${filenames[@]}"
130+
zip_unzip "-f" "-f" "1" "${filename_not_existing[@]}"
131+
zip_unzip "-f -c -400" "-f" "1" "${filenames[@]}"
132+
zip_unzip "-f -c 99999999999999999999999999" "-f" "1" "${filenames[@]}"
133+
zip_unzip "-f -h" "" "0" "${filenames[@]}"
134+
zip_unzip "-f" "" "0" "${filenames[@]}"
135+
zip_unzip "-f" "" "1" "${filename_not_existing[@]}"
136+
zip_unzip "" "-f" "0" "${filenames[@]}"
137+
zip_unzip "" "-f" "1" "${filename_not_existing[@]}"
138+
zip_unzip "-fabc" "-f" "1" "${filenames[@]}"
139+
zip_unzip "-f" "-fa" "1" "${filenames[@]}"
140+
zip_unzip "-f" "-h" "0" "${filenames[@]}"
141+
115142

116143

117144
sudo rm -f tests/files/*.zip tests/files/*.unzip

0 commit comments

Comments
 (0)