|
| 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