| 1 | /* test_stream_compress.cc - Test basic compression |
| 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_strm.h" |
| 14 | #include "mz_strm_mem.h" |
| 15 | #include "mz_strm_os.h" |
| 16 | |
| 17 | #ifdef HAVE_BZIP2 |
| 18 | # include "mz_strm_bzip.h" |
| 19 | #endif |
| 20 | #ifdef HAVE_LZMA |
| 21 | # include "mz_strm_lzma.h" |
| 22 | #endif |
| 23 | #ifdef HAVE_ZLIB |
| 24 | # include "mz_strm_zlib.h" |
| 25 | #endif |
| 26 | #ifdef HAVE_ZSTD |
| 27 | # include "mz_strm_zstd.h" |
| 28 | #endif |
| 29 | |
| 30 | #include <gtest/gtest.h> |
| 31 | |
| 32 | static void test_compare_stream_to_end(void *source1, void *source2) { |
| 33 | uint8_t source1_buf[4096]; |
| 34 | uint8_t source2_buf[4096]; |
| 35 | int32_t source1_read = 0; |
| 36 | int32_t source2_read = 0; |
| 37 | |
| 38 | do { |
| 39 | source1_read = mz_stream_read(source1, source1_buf, sizeof(source1_buf)); |
| 40 | source2_read = mz_stream_read(source2, source2_buf, sizeof(source2_buf)); |
| 41 | |
| 42 | EXPECT_EQ(source1_read, source2_read); |
| 43 | if (source1_read <= 0) |
| 44 | break; |
| 45 | |
| 46 | EXPECT_EQ(memcmp(source1_buf, source2_buf, source1_read), 0); |
| 47 | } while (1); |
| 48 | } |
| 49 | |
| 50 | static void test_compress(const char *method, mz_stream_create_cb create_compress) { |
| 51 | int64_t total_in = 0; |
| 52 | int64_t total_out = 0; |
| 53 | void *org_stream = NULL; |
| 54 | void *compress_stream = NULL; |
| 55 | void *uncompress_stream = NULL; |
| 56 | void *deflate_stream = NULL; |
| 57 | void *inflate_stream = NULL; |
| 58 | |
| 59 | /* Open file to be compressed */ |
| 60 | org_stream = mz_stream_os_create(); |
| 61 | ASSERT_NE(org_stream, nullptr); |
| 62 | ASSERT_EQ(mz_stream_os_open(org_stream, "LICENSE", MZ_OPEN_MODE_READ), MZ_OK); |
| 63 | |
| 64 | /* Compress data into memory stream */ |
| 65 | compress_stream = mz_stream_mem_create(); |
| 66 | ASSERT_NE(compress_stream, nullptr); |
| 67 | ASSERT_EQ(mz_stream_mem_open(compress_stream, NULL, MZ_OPEN_MODE_CREATE), MZ_OK); |
| 68 | |
| 69 | deflate_stream = create_compress(); |
| 70 | ASSERT_NE(deflate_stream, nullptr); |
| 71 | mz_stream_set_base(deflate_stream, compress_stream); |
| 72 | |
| 73 | /* Copy data from file stream and write to compression stream */ |
| 74 | mz_stream_open(deflate_stream, NULL, MZ_OPEN_MODE_WRITE); |
| 75 | mz_stream_copy_stream_to_end(deflate_stream, NULL, org_stream, NULL); |
| 76 | mz_stream_close(deflate_stream); |
| 77 | |
| 78 | mz_stream_get_prop_int64(deflate_stream, MZ_STREAM_PROP_TOTAL_IN, &total_in); |
| 79 | EXPECT_EQ(total_in, mz_stream_tell(org_stream)); |
| 80 | |
| 81 | mz_stream_get_prop_int64(deflate_stream, MZ_STREAM_PROP_TOTAL_OUT, &total_out); |
| 82 | EXPECT_EQ(total_out, mz_stream_tell(compress_stream)); |
| 83 | |
| 84 | mz_stream_delete(&deflate_stream); |
| 85 | |
| 86 | printf("%s compressed from %u to %u\n", method, (uint32_t)total_in, (uint32_t)total_out); |
| 87 | |
| 88 | /* Decompress data into memory stream */ |
| 89 | uncompress_stream = mz_stream_mem_create(); |
| 90 | ASSERT_NE(uncompress_stream, nullptr); |
| 91 | ASSERT_EQ(mz_stream_mem_open(uncompress_stream, NULL, MZ_OPEN_MODE_CREATE), MZ_OK); |
| 92 | |
| 93 | mz_stream_seek(compress_stream, 0, MZ_SEEK_SET); |
| 94 | |
| 95 | inflate_stream = create_compress(); |
| 96 | ASSERT_NE(inflate_stream, nullptr); |
| 97 | mz_stream_set_base(inflate_stream, compress_stream); |
| 98 | |
| 99 | mz_stream_open(inflate_stream, NULL, MZ_OPEN_MODE_READ); |
| 100 | mz_stream_copy_stream_to_end(uncompress_stream, NULL, inflate_stream, NULL); |
| 101 | mz_stream_close(inflate_stream); |
| 102 | |
| 103 | mz_stream_get_prop_int64(inflate_stream, MZ_STREAM_PROP_TOTAL_IN, &total_in); |
| 104 | EXPECT_EQ(total_in, mz_stream_tell(compress_stream)); |
| 105 | |
| 106 | mz_stream_get_prop_int64(inflate_stream, MZ_STREAM_PROP_TOTAL_OUT, &total_out); |
| 107 | EXPECT_EQ(total_out, mz_stream_tell(uncompress_stream)); |
| 108 | |
| 109 | mz_stream_delete(&inflate_stream); |
| 110 | |
| 111 | printf("%s uncompressed from %u to %u\n", method, (uint32_t)total_in, (uint32_t)total_out); |
| 112 | |
| 113 | /* Compare uncompress stream to original file stream */ |
| 114 | mz_stream_seek(org_stream, 0, MZ_SEEK_SET); |
| 115 | mz_stream_seek(uncompress_stream, 0, MZ_SEEK_SET); |
| 116 | |
| 117 | test_compare_stream_to_end(org_stream, uncompress_stream); |
| 118 | |
| 119 | mz_stream_mem_close(uncompress_stream); |
| 120 | mz_stream_mem_delete(&uncompress_stream); |
| 121 | |
| 122 | mz_stream_mem_close(compress_stream); |
| 123 | mz_stream_mem_delete(&compress_stream); |
| 124 | |
| 125 | mz_stream_os_close(org_stream); |
| 126 | mz_stream_os_delete(&org_stream); |
| 127 | } |
| 128 | |
| 129 | #ifdef HAVE_BZIP2 |
| 130 | TEST(stream, bzip) { |
| 131 | return test_compress("bzip", mz_stream_bzip_create); |
| 132 | } |
| 133 | #endif |
| 134 | #ifdef HAVE_LZMA |
| 135 | TEST(stream, lzma) { |
| 136 | return test_compress("lzma", mz_stream_lzma_create); |
| 137 | } |
| 138 | #endif |
| 139 | #ifdef HAVE_ZLIB |
| 140 | TEST(stream, zlib) { |
| 141 | return test_compress("zlib", mz_stream_zlib_create); |
| 142 | } |
| 143 | #endif |
| 144 | #ifdef HAVE_ZSTD |
| 145 | TEST(stream, zstd) { |
| 146 | return test_compress("zstd", mz_stream_zstd_create); |
| 147 | } |
| 148 | #endif |