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
36static size_t deflate_data(uint8_t *out, size_t outlen, const uint8_t *in,
37                           size_t inlen) {
38  int rv;
39  z_stream zst = {0};
40
41  rv = deflateInit(&zst, Z_DEFAULT_COMPRESSION);
42  CU_ASSERT(rv == Z_OK);
43
44  zst.avail_in = (unsigned int)inlen;
45  zst.next_in = (uint8_t *)in;
46  zst.avail_out = (unsigned int)outlen;
47  zst.next_out = out;
48  rv = deflate(&zst, Z_SYNC_FLUSH);
49  CU_ASSERT(rv == Z_OK);
50
51  deflateEnd(&zst);
52
53  return outlen - zst.avail_out;
54}
55
56static const char input[] =
57    "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND "
58    "EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
59    "MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND "
60    "NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE "
61    "LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION "
62    "OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION "
63    "WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.";
64
65void test_nghttp2_gzip_inflate(void) {
66  nghttp2_gzip *inflater;
67  uint8_t in[4096], out[4096], *inptr;
68  size_t inlen = sizeof(in);
69  size_t inproclen, outproclen;
70  const char *inputptr = input;
71
72  inlen = deflate_data(in, inlen, (const uint8_t *)input, sizeof(input) - 1);
73
74  CU_ASSERT(0 == nghttp2_gzip_inflate_new(&inflater));
75  /* First 16 bytes */
76  inptr = in;
77  inproclen = inlen;
78  outproclen = 16;
79  CU_ASSERT(
80      0 == nghttp2_gzip_inflate(inflater, out, &outproclen, inptr, &inproclen));
81  CU_ASSERT(16 == outproclen);
82  CU_ASSERT(inproclen > 0);
83  CU_ASSERT(0 == memcmp(inputptr, out, outproclen));
84  /* Next 32 bytes */
85  inptr += inproclen;
86  inlen -= inproclen;
87  inproclen = inlen;
88  inputptr += outproclen;
89  outproclen = 32;
90  CU_ASSERT(
91      0 == nghttp2_gzip_inflate(inflater, out, &outproclen, inptr, &inproclen));
92  CU_ASSERT(32 == outproclen);
93  CU_ASSERT(inproclen > 0);
94  CU_ASSERT(0 == memcmp(inputptr, out, outproclen));
95  /* Rest */
96  inptr += inproclen;
97  inlen -= inproclen;
98  inproclen = inlen;
99  inputptr += outproclen;
100  outproclen = sizeof(out);
101  CU_ASSERT(
102      0 == nghttp2_gzip_inflate(inflater, out, &outproclen, inptr, &inproclen));
103  CU_ASSERT(sizeof(input) - 49 == outproclen);
104  CU_ASSERT(inproclen > 0);
105  CU_ASSERT(0 == memcmp(inputptr, out, outproclen));
106
107  inlen -= inproclen;
108  CU_ASSERT(0 == inlen);
109
110  nghttp2_gzip_inflate_del(inflater);
111}
112