]> git.proxmox.com Git - ceph.git/blob - ceph/src/zstd/tests/legacy.c
46a8206c4422e66c46070a1fabf10961679b94a2
[ceph.git] / ceph / src / zstd / tests / legacy.c
1 /*
2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
8 * You may select, at your option, one of the above-listed licenses.
9 */
10
11 /*
12 This program uses hard-coded data compressed with Zstd legacy versions
13 and tests that the API decompresses them correctly
14 */
15
16 /*===========================================
17 * Dependencies
18 *==========================================*/
19 #include <stddef.h> /* size_t */
20 #include <stdlib.h> /* malloc, free */
21 #include <stdio.h> /* fprintf */
22 #include <string.h> /* strlen */
23 #include "zstd.h"
24 #include "zstd_errors.h"
25
26 /*===========================================
27 * Macros
28 *==========================================*/
29 #define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
30
31 /*===========================================
32 * Precompressed frames
33 *==========================================*/
34 const char* const COMPRESSED; /* content is at end of file */
35 size_t const COMPRESSED_SIZE = 917;
36 const char* const EXPECTED; /* content is at end of file */
37
38
39 int testSimpleAPI(void)
40 {
41 size_t const size = strlen(EXPECTED);
42 char* const output = malloc(size);
43
44 if (!output) {
45 DISPLAY("ERROR: Not enough memory!\n");
46 return 1;
47 }
48
49 {
50 size_t const ret = ZSTD_decompress(output, size, COMPRESSED, COMPRESSED_SIZE);
51 if (ZSTD_isError(ret)) {
52 if (ret == ZSTD_error_prefix_unknown) {
53 DISPLAY("ERROR: Invalid frame magic number, was this compiled "
54 "without legacy support?\n");
55 } else {
56 DISPLAY("ERROR: %s\n", ZSTD_getErrorName(ret));
57 }
58 return 1;
59 }
60 if (ret != size) {
61 DISPLAY("ERROR: Wrong decoded size\n");
62 }
63 }
64 if (memcmp(EXPECTED, output, size) != 0) {
65 DISPLAY("ERROR: Wrong decoded output produced\n");
66 return 1;
67 }
68
69 free(output);
70 DISPLAY("Simple API OK\n");
71 return 0;
72 }
73
74 int testStreamingAPI(void)
75 {
76 size_t const outBuffSize = ZSTD_DStreamOutSize();
77 char* const outBuff = malloc(outBuffSize);
78 ZSTD_DStream* const stream = ZSTD_createDStream();
79 ZSTD_inBuffer input = { COMPRESSED, COMPRESSED_SIZE, 0 };
80 size_t outputPos = 0;
81 int needsInit = 1;
82
83 if (outBuff == NULL) {
84 DISPLAY("ERROR: Could not allocate memory\n");
85 return 1;
86 }
87 if (stream == NULL) {
88 DISPLAY("ERROR: Could not create dstream\n");
89 return 1;
90 }
91
92 while (1) {
93 ZSTD_outBuffer output = {outBuff, outBuffSize, 0};
94 if (needsInit) {
95 size_t const ret = ZSTD_initDStream(stream);
96 if (ZSTD_isError(ret)) {
97 DISPLAY("ERROR: %s\n", ZSTD_getErrorName(ret));
98 return 1;
99 }
100 }
101 {
102 size_t const ret = ZSTD_decompressStream(stream, &output, &input);
103 if (ZSTD_isError(ret)) {
104 DISPLAY("ERROR: %s\n", ZSTD_getErrorName(ret));
105 return 1;
106 }
107
108 if (ret == 0) {
109 needsInit = 1;
110 }
111 }
112
113 if (memcmp(outBuff, EXPECTED + outputPos, output.pos) != 0) {
114 DISPLAY("ERROR: Wrong decoded output produced\n");
115 return 1;
116 }
117 outputPos += output.pos;
118 if (input.pos == input.size && output.pos < output.size) {
119 break;
120 }
121 }
122
123 free(outBuff);
124 ZSTD_freeDStream(stream);
125 DISPLAY("Streaming API OK\n");
126 return 0;
127 }
128
129 int main(void)
130 {
131 int ret;
132
133 ret = testSimpleAPI();
134 if (ret) return ret;
135 ret = testStreamingAPI();
136 if (ret) return ret;
137
138 DISPLAY("OK\n");
139
140 return 0;
141 }
142
143 /* Consists of the "EXPECTED" string compressed with default settings on
144 - v0.4.3
145 - v0.5.0
146 - v0.6.0
147 - v0.7.0
148 - v0.8.0
149 */
150 const char* const COMPRESSED =
151 "\x24\xB5\x2F\xFD\x00\x00\x00\xBB\xB0\x02\xC0\x10\x00\x1E\xB0\x01"
152 "\x02\x00\x00\x80\x00\xE8\x92\x34\x12\x97\xC8\xDF\xE9\xF3\xEF\x53"
153 "\xEA\x1D\x27\x4F\x0C\x44\x90\x0C\x8D\xF1\xB4\x89\x17\x00\x18\x00"
154 "\x18\x00\x3F\xE6\xE2\xE3\x74\xD6\xEC\xC9\x4A\xE0\x71\x71\x42\x3E"
155 "\x64\x4F\x6A\x45\x4E\x78\xEC\x49\x03\x3F\xC6\x80\xAB\x8F\x75\x5E"
156 "\x6F\x2E\x3E\x7E\xC6\xDC\x45\x69\x6C\xC5\xFD\xC7\x40\xB8\x84\x8A"
157 "\x01\xEB\xA8\xD1\x40\x39\x90\x4C\x64\xF8\xEB\x53\xE6\x18\x0B\x67"
158 "\x12\xAD\xB8\x99\xB3\x5A\x6F\x8A\x19\x03\x01\x50\x67\x56\xF5\x9F"
159 "\x35\x84\x60\xA0\x60\x91\xC9\x0A\xDC\xAB\xAB\xE0\xE2\x81\xFA\xCF"
160 "\xC6\xBA\x01\x0E\x00\x54\x00\x00\x19\x00\x00\x54\x14\x00\x24\x24"
161 "\x04\xFE\x04\x84\x4E\x41\x00\x27\xE2\x02\xC4\xB1\x00\xD2\x51\x00"
162 "\x79\x58\x41\x28\x00\xE0\x0C\x01\x68\x65\x00\x04\x13\x0C\xDA\x0C"
163 "\x80\x22\x06\xC0\x00\x00\x25\xB5\x2F\xFD\x00\x00\x00\xAD\x12\xB0"
164 "\x7D\x1E\xB0\x01\x02\x00\x00\x80\x00\xE8\x92\x34\x12\x97\xC8\xDF"
165 "\xE9\xF3\xEF\x53\xEA\x1D\x27\x4F\x0C\x44\x90\x0C\x8D\xF1\xB4\x89"
166 "\x03\x01\x50\x67\x56\xF5\x9F\x35\x84\x60\xA0\x60\x91\xC9\x0A\xDC"
167 "\xAB\xAB\xE0\xE2\x81\xFA\xCF\xC6\xBA\xEB\xA8\xD1\x40\x39\x90\x4C"
168 "\x64\xF8\xEB\x53\xE6\x18\x0B\x67\x12\xAD\xB8\x99\xB3\x5A\x6F\x8A"
169 "\xF9\x63\x0C\xB8\xFA\x58\xE7\xF5\xE6\xE2\xE3\x67\xCC\x5D\x94\xC6"
170 "\x56\xDC\x7F\x0C\x84\x4B\xA8\xF8\x63\x2E\x3E\x4E\x67\xCD\x9E\xAC"
171 "\x04\x1E\x17\x27\xE4\x43\xF6\xA4\x56\xE4\x84\xC7\x9E\x34\x0E\x00"
172 "\x00\x32\x40\x80\xA8\x00\x01\x49\x81\xE0\x3C\x01\x29\x1D\x00\x87"
173 "\xCE\x80\x75\x08\x80\x72\x24\x00\x7B\x52\x00\x94\x00\x20\xCC\x01"
174 "\x86\xD2\x00\x81\x09\x83\xC1\x34\xA0\x88\x01\xC0\x00\x00\x26\xB5"
175 "\x2F\xFD\x42\xEF\x00\x00\xA6\x12\xB0\x7D\x1E\xB0\x01\x02\x00\x00"
176 "\x54\xA0\xBA\x24\x8D\xC4\x25\xF2\x77\xFA\xFC\xFB\x94\x7A\xC7\xC9"
177 "\x13\x03\x11\x24\x43\x63\x3C\x6D\x22\x03\x01\x50\x67\x56\xF5\x9F"
178 "\x35\x84\x60\xA0\x60\x91\xC9\x0A\xDC\xAB\xAB\xE0\xE2\x81\xFA\xCF"
179 "\xC6\xBA\xEB\xA8\xD1\x40\x39\x90\x4C\x64\xF8\xEB\x53\xE6\x18\x0B"
180 "\x67\x12\xAD\xB8\x99\xB3\x5A\x6F\x8A\xF9\x63\x0C\xB8\xFA\x58\xE7"
181 "\xF5\xE6\xE2\xE3\x67\xCC\x5D\x94\xC6\x56\xDC\x7F\x0C\x84\x4B\xA8"
182 "\xF8\x63\x2E\x3E\x4E\x67\xCD\x9E\xAC\x04\x1E\x17\x27\xE4\x43\xF6"
183 "\xA4\x56\xE4\x84\xC7\x9E\x34\x0E\x00\x35\x0B\x71\xB5\xC0\x2A\x5C"
184 "\x26\x94\x22\x20\x8B\x4C\x8D\x13\x47\x58\x67\x15\x6C\xF1\x1C\x4B"
185 "\x54\x10\x9D\x31\x50\x85\x4B\x54\x0E\x01\x4B\x3D\x01\xC0\x00\x00"
186 "\x27\xB5\x2F\xFD\x20\xEF\x00\x00\xA6\x12\xE4\x84\x1F\xB0\x01\x10"
187 "\x00\x00\x00\x35\x59\xA6\xE7\xA1\xEF\x7C\xFC\xBD\x3F\xFF\x9F\xEF"
188 "\xEE\xEF\x61\xC3\xAA\x31\x1D\x34\x38\x22\x22\x04\x44\x21\x80\x32"
189 "\xAD\x28\xF3\xD6\x28\x0C\x0A\x0E\xD6\x5C\xAC\x19\x8D\x20\x5F\x45"
190 "\x02\x2E\x17\x50\x66\x6D\xAC\x8B\x9C\x6E\x07\x73\x46\xBB\x44\x14"
191 "\xE7\x98\xC3\xB9\x17\x32\x6E\x33\x7C\x0E\x21\xB1\xDB\xCB\x89\x51"
192 "\x23\x34\xAB\x9D\xBC\x6D\x20\xF5\x03\xA9\x91\x4C\x2E\x1F\x59\xDB"
193 "\xD9\x35\x67\x4B\x0C\x95\x79\x10\x00\x85\xA6\x96\x95\x2E\xDF\x78"
194 "\x7B\x4A\x5C\x09\x76\x97\xD1\x5C\x96\x12\x75\x35\xA3\x55\x4A\xD4"
195 "\x0B\x00\x35\x0B\x71\xB5\xC0\x2A\x5C\xE6\x08\x45\xF1\x39\x43\xF1"
196 "\x1C\x4B\x54\x10\x9D\x31\x50\x85\x4B\x54\x0E\x01\x4B\x3D\x01\xC0"
197 "\x00\x00\x28\xB5\x2F\xFD\x24\xEF\x35\x05\x00\x92\x0B\x21\x1F\xB0"
198 "\x01\x10\x00\x00\x00\x35\x59\xA6\xE7\xA1\xEF\x7C\xFC\xBD\x3F\xFF"
199 "\x9F\xEF\xEE\xEF\x61\xC3\xAA\x31\x1D\x34\x38\x22\x22\x04\x44\x21"
200 "\x80\x32\xAD\x28\xF3\xD6\x28\x0C\x0A\x0E\xD6\x5C\xAC\x19\x8D\x20"
201 "\x5F\x45\x02\x2E\x17\x50\x66\x6D\xAC\x8B\x9C\x6E\x07\x73\x46\xBB"
202 "\x44\x14\xE7\x98\xC3\xB9\x17\x32\x6E\x33\x7C\x0E\x21\xB1\xDB\xCB"
203 "\x89\x51\x23\x34\xAB\x9D\xBC\x6D\x20\xF5\x03\xA9\x91\x4C\x2E\x1F"
204 "\x59\xDB\xD9\x35\x67\x4B\x0C\x95\x79\x10\x00\x85\xA6\x96\x95\x2E"
205 "\xDF\x78\x7B\x4A\x5C\x09\x76\x97\xD1\x5C\x96\x12\x75\x35\xA3\x55"
206 "\x4A\xD4\x0B\x00\x35\x0B\x71\xB5\xC0\x2A\x5C\xE6\x08\x45\xF1\x39"
207 "\x43\xF1\x1C\x4B\x54\x10\x9D\x31\x50\x85\x4B\x54\x0E\x01\x4B\x3D"
208 "\x01\xD2\x2F\x21\x80";
209
210 const char* const EXPECTED =
211 "snowden is snowed in / he's now then in his snow den / when does the snow end?\n"
212 "goodbye little dog / you dug some holes in your day / they'll be hard to fill.\n"
213 "when life shuts a door, / just open it. it’s a door. / that is how doors work.\n"
214
215 "snowden is snowed in / he's now then in his snow den / when does the snow end?\n"
216 "goodbye little dog / you dug some holes in your day / they'll be hard to fill.\n"
217 "when life shuts a door, / just open it. it’s a door. / that is how doors work.\n"
218
219 "snowden is snowed in / he's now then in his snow den / when does the snow end?\n"
220 "goodbye little dog / you dug some holes in your day / they'll be hard to fill.\n"
221 "when life shuts a door, / just open it. it’s a door. / that is how doors work.\n"
222
223 "snowden is snowed in / he's now then in his snow den / when does the snow end?\n"
224 "goodbye little dog / you dug some holes in your day / they'll be hard to fill.\n"
225 "when life shuts a door, / just open it. it’s a door. / that is how doors work.\n"
226
227 "snowden is snowed in / he's now then in his snow den / when does the snow end?\n"
228 "goodbye little dog / you dug some holes in your day / they'll be hard to fill.\n"
229 "when life shuts a door, / just open it. it’s a door. / that is how doors work.\n";