1 /*
2 * nghttp2 - HTTP/2 C Library
3 *
4 * Copyright (c) 2012 Tatsuhiro Tsujikawa
5 *
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:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
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.
24 */
25 #include "nghttp2_gzip_test.h"
26
27 #include <stdio.h>
28 #include <assert.h>
29
30 #include "munit.h"
31
32 #include <zlib.h>
33
34 #include "nghttp2_gzip.h"
35
36 static const MunitTest tests[] = {
37 munit_void_test(test_nghttp2_gzip_inflate),
38 munit_test_end(),
39 };
40
41 const MunitSuite gzip_suite = {
42 "/gzip", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE,
43 };
44
deflate_data(uint8_t * out,size_t outlen,const uint8_t * in,size_t inlen)45 static size_t deflate_data(uint8_t *out, size_t outlen, const uint8_t *in,
46 size_t inlen) {
47 int rv;
48 z_stream zst = {0};
49
50 rv = deflateInit(&zst, Z_DEFAULT_COMPRESSION);
51 assert_int(Z_OK, ==, rv);
52
53 zst.avail_in = (unsigned int)inlen;
54 zst.next_in = (uint8_t *)in;
55 zst.avail_out = (unsigned int)outlen;
56 zst.next_out = out;
57 rv = deflate(&zst, Z_SYNC_FLUSH);
58 assert_int(Z_OK, ==, rv);
59
60 deflateEnd(&zst);
61
62 return outlen - zst.avail_out;
63 }
64
65 static const char input[] =
66 "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND "
67 "EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
68 "MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND "
69 "NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE "
70 "LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION "
71 "OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION "
72 "WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.";
73
test_nghttp2_gzip_inflate(void)74 void test_nghttp2_gzip_inflate(void) {
75 nghttp2_gzip *inflater;
76 uint8_t in[4096], out[4096], *inptr;
77 size_t inlen = sizeof(in);
78 size_t inproclen, outproclen;
79 const char *inputptr = input;
80
81 inlen = deflate_data(in, inlen, (const uint8_t *)input, sizeof(input) - 1);
82
83 assert_int(0, ==, nghttp2_gzip_inflate_new(&inflater));
84 /* First 16 bytes */
85 inptr = in;
86 inproclen = inlen;
87 outproclen = 16;
88 assert_int(
89 0, ==,
90 nghttp2_gzip_inflate(inflater, out, &outproclen, inptr, &inproclen));
91 assert_size(16, ==, outproclen);
92 assert_size(0, <, inproclen);
93 assert_memory_equal(outproclen, inputptr, out);
94 /* Next 32 bytes */
95 inptr += inproclen;
96 inlen -= inproclen;
97 inproclen = inlen;
98 inputptr += outproclen;
99 outproclen = 32;
100 assert_int(
101 0, ==,
102 nghttp2_gzip_inflate(inflater, out, &outproclen, inptr, &inproclen));
103 assert_size(32, ==, outproclen);
104 assert_size(0, <, inproclen);
105 assert_memory_equal(outproclen, inputptr, out);
106 /* Rest */
107 inptr += inproclen;
108 inlen -= inproclen;
109 inproclen = inlen;
110 inputptr += outproclen;
111 outproclen = sizeof(out);
112 assert_int(
113 0, ==,
114 nghttp2_gzip_inflate(inflater, out, &outproclen, inptr, &inproclen));
115 assert_size(sizeof(input) - 49, ==, outproclen);
116 assert_size(0, <, inproclen);
117 assert_memory_equal(outproclen, inputptr, out);
118
119 inlen -= inproclen;
120 assert_size(0, ==, inlen);
121
122 nghttp2_gzip_inflate_del(inflater);
123 }
124