Skip to content

Commit ae8deec

Browse files
committed
Add missing ignore unit tests
1 parent 514d654 commit ae8deec

3 files changed

Lines changed: 208 additions & 1 deletion

File tree

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
tmp
44
./*.zip
55
./*.bin
6-
test/unit/test_*
76
test/simple_test
87
test/verify_deflate
98
*.a

test/unit/test_empty_zip.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include <stdio.h>
2+
#include <stdint.h>
3+
#include <stdlib.h>
4+
#include <string.h>
5+
#include <unistd.h>
6+
7+
#include "../../src/include/otezip/zip.h"
8+
9+
static int write_empty_zip(const char *path) {
10+
static const uint8_t empty_zip[] = {
11+
0x50, 0x4b, 0x05, 0x06,
12+
0x00, 0x00,
13+
0x00, 0x00,
14+
0x00, 0x00,
15+
0x00, 0x00,
16+
0x00, 0x00, 0x00, 0x00,
17+
0x00, 0x00, 0x00, 0x00,
18+
0x00, 0x00
19+
};
20+
FILE *fp = fopen (path, "wb");
21+
if (!fp) {
22+
perror ("fopen");
23+
return 1;
24+
}
25+
if (fwrite (empty_zip, 1, sizeof (empty_zip), fp) != sizeof (empty_zip)) {
26+
perror ("fwrite");
27+
fclose (fp);
28+
return 1;
29+
}
30+
if (fclose (fp) != 0) {
31+
perror ("fclose");
32+
return 1;
33+
}
34+
return 0;
35+
}
36+
37+
int main(void) {
38+
char path[] = "/tmp/otezip-empty-XXXXXX";
39+
int fd = mkstemp (path);
40+
if (fd < 0) {
41+
perror ("mkstemp");
42+
return 1;
43+
}
44+
close (fd);
45+
46+
if (write_empty_zip (path) != 0) {
47+
unlink (path);
48+
return 1;
49+
}
50+
51+
int err = -1;
52+
zip_t *za = zip_open (path, ZIP_RDONLY, &err);
53+
if (!za) {
54+
fprintf (stderr, "zip_open failed for empty archive: err=%d\n", err);
55+
unlink (path);
56+
return 1;
57+
}
58+
59+
if (zip_get_num_files (za) != 0) {
60+
fprintf (stderr, "expected 0 entries, got %llu\n",
61+
(unsigned long long)zip_get_num_files (za));
62+
zip_close (za);
63+
unlink (path);
64+
return 1;
65+
}
66+
67+
if (zip_close (za) != 0) {
68+
fprintf (stderr, "zip_close failed\n");
69+
unlink (path);
70+
return 1;
71+
}
72+
73+
if (unlink (path) != 0) {
74+
perror ("unlink");
75+
return 1;
76+
}
77+
78+
puts ("TEST PASSED: Empty EOCD-only ZIP archive opens successfully.");
79+
return 0;
80+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#include <stdio.h>
2+
#include <stdint.h>
3+
#include <stdlib.h>
4+
#include <string.h>
5+
#include <unistd.h>
6+
7+
#include "../../src/include/otezip/zip.h"
8+
9+
int main(void) {
10+
enum { payload_size = 4096 };
11+
char path[] = "/tmp/otezip-compress-XXXXXX";
12+
char expected[payload_size];
13+
memset (expected, 'A', sizeof (expected));
14+
int fd = mkstemp (path);
15+
if (fd < 0) {
16+
perror ("mkstemp");
17+
return 1;
18+
}
19+
close (fd);
20+
unlink (path);
21+
22+
int err = -1;
23+
zip_t *za = zip_open (path, ZIP_CREATE | ZIP_TRUNCATE, &err);
24+
if (!za) {
25+
fprintf (stderr, "zip_open(create) failed: %d\n", err);
26+
return 1;
27+
}
28+
29+
char *buf = (char *)malloc (sizeof (expected));
30+
if (!buf) {
31+
perror ("malloc");
32+
zip_close (za);
33+
unlink (path);
34+
return 1;
35+
}
36+
memcpy (buf, expected, sizeof (expected));
37+
38+
zip_source_t *src = zip_source_buffer (za, buf, sizeof (expected), 1);
39+
if (!src) {
40+
fprintf (stderr, "zip_source_buffer failed\n");
41+
free (buf);
42+
zip_close (za);
43+
unlink (path);
44+
return 1;
45+
}
46+
47+
zip_int64_t idx = zip_file_add (za, "hello.txt", src, 0);
48+
if (idx < 0) {
49+
fprintf (stderr, "zip_file_add failed\n");
50+
zip_source_free (src);
51+
zip_close (za);
52+
unlink (path);
53+
return 1;
54+
}
55+
if (zip_set_file_compression (za, (zip_uint64_t)idx, ZIP_CM_DEFLATE, 0) != 0) {
56+
fprintf (stderr, "zip_set_file_compression failed\n");
57+
zip_close (za);
58+
unlink (path);
59+
return 1;
60+
}
61+
if (zip_close (za) != 0) {
62+
fprintf (stderr, "zip_close(write) failed\n");
63+
unlink (path);
64+
return 1;
65+
}
66+
67+
za = zip_open (path, ZIP_RDONLY, &err);
68+
if (!za) {
69+
fprintf (stderr, "zip_open(read) failed: %d\n", err);
70+
unlink (path);
71+
return 1;
72+
}
73+
74+
const char *name = zip_get_name (za, 0, 0);
75+
if (!name || strcmp (name, "hello.txt") != 0) {
76+
fprintf (stderr, "unexpected name: %s\n", name? name: "(null)");
77+
zip_close (za);
78+
unlink (path);
79+
return 1;
80+
}
81+
82+
zip_stat_t st;
83+
zip_stat_init (&st);
84+
if (zip_stat_index (za, 0, 0, &st) != 0) {
85+
fprintf (stderr, "zip_stat_index failed\n");
86+
zip_close (za);
87+
unlink (path);
88+
return 1;
89+
}
90+
if (st.size != sizeof (expected)) {
91+
fprintf (stderr, "unexpected size: %llu\n", (unsigned long long)st.size);
92+
zip_close (za);
93+
unlink (path);
94+
return 1;
95+
}
96+
97+
zip_file_t *zf = zip_fopen_index (za, 0, 0);
98+
if (!zf) {
99+
fprintf (stderr, "zip_fopen_index failed\n");
100+
zip_close (za);
101+
unlink (path);
102+
return 1;
103+
}
104+
105+
char out[payload_size];
106+
memset (out, 0, sizeof (out));
107+
zip_int64_t nr = zip_fread (zf, out, sizeof (out));
108+
if (nr != (zip_int64_t)sizeof (expected)) {
109+
fprintf (stderr, "unexpected read size: %lld\n", (long long)nr);
110+
zip_fclose (zf);
111+
zip_close (za);
112+
unlink (path);
113+
return 1;
114+
}
115+
if (memcmp (out, expected, sizeof (expected)) != 0) {
116+
fprintf (stderr, "payload mismatch\n");
117+
zip_fclose (zf);
118+
zip_close (za);
119+
unlink (path);
120+
return 1;
121+
}
122+
123+
zip_fclose (zf);
124+
zip_close (za);
125+
unlink (path);
126+
puts ("TEST PASSED: compression can be set via public API after add.");
127+
return 0;
128+
}

0 commit comments

Comments
 (0)