2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2012 Tatsuhiro Tsujikawa
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #include "nghttp2_gzip_test.h"
30 #include <CUnit/CUnit.h>
34 #include "nghttp2_gzip.h"
36 static ssize_t deflate_data(uint8_t *out, size_t outlen, const uint8_t *in,
45 rv = deflateInit(&zst, Z_DEFAULT_COMPRESSION);
48 zst.avail_in = (unsigned int)inlen;
49 zst.next_in = (uint8_t *)in;
50 zst.avail_out = (unsigned int)outlen;
52 rv = deflate(&zst, Z_SYNC_FLUSH);
57 return outlen - zst.avail_out;
60 static const char input[] =
61 "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND "
62 "EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
63 "MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND "
64 "NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE "
65 "LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION "
66 "OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION "
67 "WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.";
69 void test_nghttp2_gzip_inflate(void) {
70 nghttp2_gzip *inflater;
71 uint8_t in[4096], out[4096], *inptr;
72 size_t inlen = sizeof(in);
73 size_t inproclen, outproclen;
74 const char *inputptr = input;
76 inlen = deflate_data(in, inlen, (const uint8_t *)input, sizeof(input) - 1);
78 CU_ASSERT(0 == nghttp2_gzip_inflate_new(&inflater));
84 0 == nghttp2_gzip_inflate(inflater, out, &outproclen, inptr, &inproclen));
85 CU_ASSERT(16 == outproclen);
86 CU_ASSERT(inproclen > 0);
87 CU_ASSERT(0 == memcmp(inputptr, out, outproclen));
92 inputptr += outproclen;
95 0 == nghttp2_gzip_inflate(inflater, out, &outproclen, inptr, &inproclen));
96 CU_ASSERT(32 == outproclen);
97 CU_ASSERT(inproclen > 0);
98 CU_ASSERT(0 == memcmp(inputptr, out, outproclen));
103 inputptr += outproclen;
104 outproclen = sizeof(out);
106 0 == nghttp2_gzip_inflate(inflater, out, &outproclen, inptr, &inproclen));
107 CU_ASSERT(sizeof(input) - 49 == outproclen);
108 CU_ASSERT(inproclen > 0);
109 CU_ASSERT(0 == memcmp(inputptr, out, outproclen));
112 CU_ASSERT(0 == inlen);
114 nghttp2_gzip_inflate_del(inflater);