tizen 2.4 release
[external/nghttp2.git] / src / nghttp2_gzip_test.c
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 <CUnit/CUnit.h>
31
32 #include <zlib.h>
33
34 #include "nghttp2_gzip.h"
35
36 static ssize_t deflate_data(uint8_t *out, size_t outlen, const uint8_t *in,
37                             size_t inlen) {
38   int rv;
39   z_stream zst;
40   zst.next_in = Z_NULL;
41   zst.zalloc = Z_NULL;
42   zst.zfree = Z_NULL;
43   zst.opaque = Z_NULL;
44
45   rv = deflateInit(&zst, Z_DEFAULT_COMPRESSION);
46   assert(rv == Z_OK);
47
48   zst.avail_in = (unsigned int)inlen;
49   zst.next_in = (uint8_t *)in;
50   zst.avail_out = (unsigned int)outlen;
51   zst.next_out = out;
52   rv = deflate(&zst, Z_SYNC_FLUSH);
53   assert(rv == Z_OK);
54
55   deflateEnd(&zst);
56
57   return outlen - zst.avail_out;
58 }
59
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.";
68
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;
75
76   inlen = deflate_data(in, inlen, (const uint8_t *)input, sizeof(input) - 1);
77
78   CU_ASSERT(0 == nghttp2_gzip_inflate_new(&inflater));
79   /* First 16 bytes */
80   inptr = in;
81   inproclen = inlen;
82   outproclen = 16;
83   CU_ASSERT(
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));
88   /* Next 32 bytes */
89   inptr += inproclen;
90   inlen -= inproclen;
91   inproclen = inlen;
92   inputptr += outproclen;
93   outproclen = 32;
94   CU_ASSERT(
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));
99   /* Rest */
100   inptr += inproclen;
101   inlen -= inproclen;
102   inproclen = inlen;
103   inputptr += outproclen;
104   outproclen = sizeof(out);
105   CU_ASSERT(
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));
110
111   inlen -= inproclen;
112   CU_ASSERT(0 == inlen);
113
114   nghttp2_gzip_inflate_del(inflater);
115 }