| 1 | /* test_compat.cc - Test compatibility layer |
| 2 | part of the minizip-ng project |
| 3 | |
| 4 | Copyright (C) Nathan Moinvaziri |
| 5 | https://github.com/zlib-ng/minizip-ng |
| 6 | |
| 7 | This program is distributed under the terms of the same license as zlib. |
| 8 | See the accompanying LICENSE file for the full text of the license. |
| 9 | */ |
| 10 | |
| 11 | #include "mz.h" |
| 12 | #include "mz_os.h" |
| 13 | #include "mz_zip.h" |
| 14 | |
| 15 | #include "unzip.h" |
| 16 | #include "zip.h" |
| 17 | #include "crypt.h" |
| 18 | |
| 19 | #include <gtest/gtest.h> |
| 20 | |
| 21 | #ifdef HAVE_ZLIB |
| 22 | static void test_zip_compat(zipFile zip, const char *filename, int32_t level) { |
| 23 | int32_t err = ZIP_OK; |
| 24 | zip_fileinfo file_info; |
| 25 | const char *buffer = "test data"; |
| 26 | |
| 27 | memset(&file_info, 0, sizeof(file_info)); |
| 28 | file_info.dosDate = mz_zip_time_t_to_dos_date(1588561637); |
| 29 | |
| 30 | EXPECT_EQ( |
| 31 | err = zipOpenNewFileInZip(zip, filename, &file_info, NULL, 0, NULL, 0, "test local comment", Z_DEFLATED, level), |
| 32 | ZIP_OK) |
| 33 | << "failed to open new file in zip (err: " << err << ")"; |
| 34 | if (err != ZIP_OK) |
| 35 | return; |
| 36 | |
| 37 | EXPECT_EQ(err = zipWriteInFileInZip(zip, buffer, (uint32_t)strlen(buffer)), ZIP_OK) |
| 38 | << "failed to write to file in zip (err: " << err << ")"; |
| 39 | |
| 40 | EXPECT_EQ(err = zipCloseFileInZip(zip), ZIP_OK) << "failed to close file in zip (err: " << err << ")"; |
| 41 | } |
| 42 | |
| 43 | TEST(compat, zip) { |
| 44 | zipFile zip; |
| 45 | |
| 46 | zip = zipOpen64("compat.zip", APPEND_STATUS_CREATE); |
| 47 | ASSERT_NE(zip, nullptr) << "cannot create test zip file"; |
| 48 | |
| 49 | test_zip_compat(zip, "test.txt", 1); |
| 50 | test_zip_compat(zip, "test2.txt", 0); |
| 51 | |
| 52 | zipClose(zip, "test global comment"); |
| 53 | } |
| 54 | |
| 55 | static void test_unzip_compat(unzFile unzip) { |
| 56 | unz_global_info64 global_info64; |
| 57 | unz_global_info global_info; |
| 58 | unz_file_info64 file_info64; |
| 59 | unz_file_info file_info; |
| 60 | unz_file_pos file_pos; |
| 61 | int32_t err = UNZ_OK; |
| 62 | int32_t bytes_read = 0; |
| 63 | char comment[120]; |
| 64 | char filename[120]; |
| 65 | char buffer[120]; |
| 66 | const char *test_data = "test data"; |
| 67 | |
| 68 | memset(&file_info, 0, sizeof(file_info)); |
| 69 | memset(&file_info64, 0, sizeof(file_info64)); |
| 70 | memset(&global_info, 0, sizeof(global_info)); |
| 71 | memset(&global_info64, 0, sizeof(global_info64)); |
| 72 | |
| 73 | comment[0] = 0; |
| 74 | filename[0] = 0; |
| 75 | |
| 76 | EXPECT_EQ(err = unzGetGlobalComment(unzip, comment, sizeof(comment)), UNZ_OK) |
| 77 | << "global comment (err: " << err << ")"; |
| 78 | |
| 79 | EXPECT_STREQ(comment, "test global comment"); |
| 80 | |
| 81 | EXPECT_EQ(err = unzGetGlobalInfo(unzip, &global_info), UNZ_OK) << "global info (err: " << err << ")"; |
| 82 | EXPECT_EQ(err = unzGetGlobalInfo64(unzip, &global_info64), UNZ_OK) |
| 83 | << "global info l info 64-bit (err: " << err << ")"; |
| 84 | |
| 85 | EXPECT_EQ(global_info.number_entry, 2) << "invalid number of entries"; |
| 86 | EXPECT_EQ(global_info64.number_entry, 2) << "invalid number of entries 64-bit"; |
| 87 | |
| 88 | EXPECT_EQ(global_info.number_disk_with_CD, 0) << "invalid disk with cd"; |
| 89 | EXPECT_EQ(global_info64.number_disk_with_CD, 0) << "invalid disk with cd 64-bit"; |
| 90 | |
| 91 | EXPECT_EQ(err = unzLocateFile(unzip, "test.txt", 1), UNZ_OK) << "cannot locate test file (err: " << err << ")"; |
| 92 | |
| 93 | EXPECT_EQ(err = unzGoToFirstFile(unzip), UNZ_OK); |
| 94 | if (err != UNZ_OK) |
| 95 | return; |
| 96 | |
| 97 | EXPECT_EQ(err = unzGetCurrentFileInfo64(unzip, &file_info64, filename, sizeof(filename), NULL, 0, NULL, 0), UNZ_OK) |
| 98 | << "failed to get current file info 64-bit (err: " << err << ")"; |
| 99 | EXPECT_EQ(err = unzOpenCurrentFile(unzip), UNZ_OK) << "failed to open current file (err: " << err << ")"; |
| 100 | |
| 101 | EXPECT_EQ(bytes_read = unzReadCurrentFile(unzip, buffer, sizeof(buffer)), (int32_t)strlen(test_data)) |
| 102 | << "failed to read zip entry data (err: " << err << ")"; |
| 103 | |
| 104 | EXPECT_EQ(unzEndOfFile(unzip), 1) << "end of zip not reported correctly"; |
| 105 | |
| 106 | EXPECT_EQ(err = unzCloseCurrentFile(unzip), UNZ_OK) << "failed to close current file (err: " << err << ")"; |
| 107 | |
| 108 | EXPECT_EQ(unztell(unzip), bytes_read) << "unzip position not reported correctly"; |
| 109 | |
| 110 | EXPECT_EQ(err = unzGoToNextFile(unzip), UNZ_OK); |
| 111 | if (err != UNZ_OK) |
| 112 | return; |
| 113 | |
| 114 | comment[0] = 0; |
| 115 | EXPECT_EQ( |
| 116 | err = unzGetCurrentFileInfo(unzip, &file_info, filename, sizeof(filename), NULL, 0, comment, sizeof(comment)), |
| 117 | UNZ_OK) |
| 118 | << "failed to get current file info (err: " << err << ")"; |
| 119 | |
| 120 | EXPECT_STREQ(comment, "test local comment"); |
| 121 | |
| 122 | EXPECT_EQ(err = unzGetFilePos(unzip, &file_pos), UNZ_OK) << "unexpected file position (err: " << err << ")"; |
| 123 | |
| 124 | EXPECT_EQ(file_pos.num_of_file, 1) << "invalid file position"; |
| 125 | |
| 126 | EXPECT_GT(unzGetOffset(unzip), 0) << "invalid offset"; |
| 127 | |
| 128 | EXPECT_EQ(err = unzSeek64(unzip, 0, SEEK_SET), UNZ_OK) << "cannot seek to beginning (err: " << err << ")"; |
| 129 | |
| 130 | EXPECT_EQ(err = unzGoToNextFile(unzip), UNZ_END_OF_LIST_OF_FILE) |
| 131 | << "failed to reach end of list of files (err: " << err << ")"; |
| 132 | |
| 133 | EXPECT_EQ(err = unzSeek64(unzip, 0, SEEK_SET), UNZ_PARAMERROR) << "cannot seek to beginning (err: " << err << ")"; |
| 134 | |
| 135 | unzCloseCurrentFile(unzip); |
| 136 | } |
| 137 | |
| 138 | # ifndef MZ_FILE32_API |
| 139 | # ifndef NO_FSEEKO |
| 140 | # define ftello64 ftello |
| 141 | # define fseeko64 fseeko |
| 142 | # elif defined(_MSC_VER) && (_MSC_VER >= 1400) |
| 143 | # define ftello64 _ftelli64 |
| 144 | # define fseeko64 _fseeki64 |
| 145 | # endif |
| 146 | # endif |
| 147 | # ifndef ftello64 |
| 148 | # define ftello64 ftell |
| 149 | # endif |
| 150 | # ifndef fseeko64 |
| 151 | # define fseeko64 fseek |
| 152 | # endif |
| 153 | |
| 154 | static void *ZCALLBACK fopen_file_func(void *opaque, const char *filename, int mode) { |
| 155 | FILE *file = NULL; |
| 156 | const char *mode_fopen = NULL; |
| 157 | |
| 158 | if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER) == ZLIB_FILEFUNC_MODE_READ) |
| 159 | mode_fopen = "rb"; |
| 160 | else if (mode & ZLIB_FILEFUNC_MODE_EXISTING) |
| 161 | mode_fopen = "r+b"; |
| 162 | else if (mode & ZLIB_FILEFUNC_MODE_CREATE) |
| 163 | mode_fopen = "wb"; |
| 164 | |
| 165 | if (filename && mode_fopen) |
| 166 | file = fopen(filename, mode_fopen); |
| 167 | |
| 168 | return file; |
| 169 | } |
| 170 | |
| 171 | static unsigned long ZCALLBACK fread_file_func(void *opaque, void *stream, void *buf, unsigned long size) { |
| 172 | return (unsigned long)fread(buf, 1, (size_t)size, (FILE *)stream); |
| 173 | } |
| 174 | |
| 175 | static unsigned long ZCALLBACK fwrite_file_func(void *opaque, void *stream, const void *buf, unsigned long size) { |
| 176 | return (unsigned long)fwrite(buf, 1, (size_t)size, (FILE *)stream); |
| 177 | } |
| 178 | |
| 179 | static long ZCALLBACK ftell_file_func(void *opaque, void *stream) { |
| 180 | return ftell((FILE *)stream); |
| 181 | } |
| 182 | |
| 183 | static ZPOS64_T ZCALLBACK ftell64_file_func(void *opaque, void *stream) { |
| 184 | return ftello64((FILE *)stream); |
| 185 | } |
| 186 | |
| 187 | static long ZCALLBACK fseek_file_func(void *opaque, void *stream, unsigned long offset, int origin) { |
| 188 | int fseek_origin = 0; |
| 189 | long ret = 0; |
| 190 | switch (origin) { |
| 191 | case ZLIB_FILEFUNC_SEEK_CUR: |
| 192 | fseek_origin = SEEK_CUR; |
| 193 | break; |
| 194 | case ZLIB_FILEFUNC_SEEK_END: |
| 195 | fseek_origin = SEEK_END; |
| 196 | break; |
| 197 | case ZLIB_FILEFUNC_SEEK_SET: |
| 198 | fseek_origin = SEEK_SET; |
| 199 | break; |
| 200 | default: |
| 201 | return -1; |
| 202 | } |
| 203 | if (fseek((FILE *)stream, offset, fseek_origin) != 0) |
| 204 | ret = -1; |
| 205 | return ret; |
| 206 | } |
| 207 | |
| 208 | static long ZCALLBACK fseek64_file_func(void *opaque, void *stream, ZPOS64_T offset, int origin) { |
| 209 | int fseek_origin = 0; |
| 210 | long ret = 0; |
| 211 | switch (origin) { |
| 212 | case ZLIB_FILEFUNC_SEEK_CUR: |
| 213 | fseek_origin = SEEK_CUR; |
| 214 | break; |
| 215 | case ZLIB_FILEFUNC_SEEK_END: |
| 216 | fseek_origin = SEEK_END; |
| 217 | break; |
| 218 | case ZLIB_FILEFUNC_SEEK_SET: |
| 219 | fseek_origin = SEEK_SET; |
| 220 | break; |
| 221 | default: |
| 222 | return -1; |
| 223 | } |
| 224 | if (fseeko64((FILE *)stream, offset, fseek_origin) != 0) |
| 225 | ret = -1; |
| 226 | return ret; |
| 227 | } |
| 228 | |
| 229 | static int ZCALLBACK fclose_file_func(void *opaque, void *stream) { |
| 230 | return fclose((FILE *)stream); |
| 231 | } |
| 232 | |
| 233 | static int ZCALLBACK ferror_file_func(void *opaque, void *stream) { |
| 234 | return ferror((FILE *)stream); |
| 235 | } |
| 236 | |
| 237 | void fill_ioapi32_filefunc(zlib_filefunc_def *pzlib_filefunc_def) { |
| 238 | pzlib_filefunc_def->zopen_file = fopen_file_func; |
| 239 | pzlib_filefunc_def->zread_file = fread_file_func; |
| 240 | pzlib_filefunc_def->zwrite_file = fwrite_file_func; |
| 241 | pzlib_filefunc_def->ztell_file = ftell_file_func; |
| 242 | pzlib_filefunc_def->zseek_file = fseek_file_func; |
| 243 | pzlib_filefunc_def->zclose_file = fclose_file_func; |
| 244 | pzlib_filefunc_def->zerror_file = ferror_file_func; |
| 245 | pzlib_filefunc_def->opaque = NULL; |
| 246 | } |
| 247 | |
| 248 | void fill_ioapi64_filefunc(zlib_filefunc64_def *pzlib_filefunc_def) { |
| 249 | pzlib_filefunc_def->zopen64_file = (open64_file_func)fopen_file_func; |
| 250 | pzlib_filefunc_def->zread_file = fread_file_func; |
| 251 | pzlib_filefunc_def->zwrite_file = fwrite_file_func; |
| 252 | pzlib_filefunc_def->ztell64_file = ftell64_file_func; |
| 253 | pzlib_filefunc_def->zseek64_file = fseek64_file_func; |
| 254 | pzlib_filefunc_def->zclose_file = fclose_file_func; |
| 255 | pzlib_filefunc_def->zerror_file = ferror_file_func; |
| 256 | pzlib_filefunc_def->opaque = NULL; |
| 257 | } |
| 258 | |
| 259 | TEST(compat, unzip) { |
| 260 | unzFile unzip; |
| 261 | |
| 262 | unzip = unzOpen("compat.zip"); |
| 263 | ASSERT_NE(unzip, nullptr) << "cannot open test zip file"; |
| 264 | |
| 265 | test_unzip_compat(unzip); |
| 266 | unzClose(unzip); |
| 267 | } |
| 268 | |
| 269 | TEST(compat, unzip32) { |
| 270 | unzFile unzip; |
| 271 | zlib_filefunc_def zlib_filefunc_def; |
| 272 | |
| 273 | fill_ioapi32_filefunc(&zlib_filefunc_def); |
| 274 | unzip = unzOpen2("compat.zip", &zlib_filefunc_def); |
| 275 | ASSERT_NE(unzip, nullptr) << "cannot open test zip file"; |
| 276 | |
| 277 | test_unzip_compat(unzip); |
| 278 | unzClose(unzip); |
| 279 | } |
| 280 | |
| 281 | TEST(compat, unzip64) { |
| 282 | unzFile unzip; |
| 283 | zlib_filefunc64_def zlib_filefunc_def; |
| 284 | |
| 285 | fill_ioapi64_filefunc(&zlib_filefunc_def); |
| 286 | unzip = unzOpen2_64("compat.zip", &zlib_filefunc_def); |
| 287 | ASSERT_NE(unzip, nullptr) << "cannot open test zip file"; |
| 288 | |
| 289 | test_unzip_compat(unzip); |
| 290 | unzClose(unzip); |
| 291 | } |
| 292 | #endif |