12c593315Sopenharmony_ci/*
22c593315Sopenharmony_ci * nghttp2 - HTTP/2 C Library
32c593315Sopenharmony_ci *
42c593315Sopenharmony_ci * Copyright (c) 2012 Tatsuhiro Tsujikawa
52c593315Sopenharmony_ci *
62c593315Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining
72c593315Sopenharmony_ci * a copy of this software and associated documentation files (the
82c593315Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
92c593315Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
102c593315Sopenharmony_ci * distribute, sublicense, and/or sell copies of the Software, and to
112c593315Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
122c593315Sopenharmony_ci * the following conditions:
132c593315Sopenharmony_ci *
142c593315Sopenharmony_ci * The above copyright notice and this permission notice shall be
152c593315Sopenharmony_ci * included in all copies or substantial portions of the Software.
162c593315Sopenharmony_ci *
172c593315Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
182c593315Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
192c593315Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
202c593315Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
212c593315Sopenharmony_ci * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
222c593315Sopenharmony_ci * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
232c593315Sopenharmony_ci * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
242c593315Sopenharmony_ci */
252c593315Sopenharmony_ci#include "nghttp2_gzip_test.h"
262c593315Sopenharmony_ci
272c593315Sopenharmony_ci#include <stdio.h>
282c593315Sopenharmony_ci#include <assert.h>
292c593315Sopenharmony_ci
302c593315Sopenharmony_ci#include <CUnit/CUnit.h>
312c593315Sopenharmony_ci
322c593315Sopenharmony_ci#include <zlib.h>
332c593315Sopenharmony_ci
342c593315Sopenharmony_ci#include "nghttp2_gzip.h"
352c593315Sopenharmony_ci
362c593315Sopenharmony_cistatic size_t deflate_data(uint8_t *out, size_t outlen, const uint8_t *in,
372c593315Sopenharmony_ci                           size_t inlen) {
382c593315Sopenharmony_ci  int rv;
392c593315Sopenharmony_ci  z_stream zst = {0};
402c593315Sopenharmony_ci
412c593315Sopenharmony_ci  rv = deflateInit(&zst, Z_DEFAULT_COMPRESSION);
422c593315Sopenharmony_ci  CU_ASSERT(rv == Z_OK);
432c593315Sopenharmony_ci
442c593315Sopenharmony_ci  zst.avail_in = (unsigned int)inlen;
452c593315Sopenharmony_ci  zst.next_in = (uint8_t *)in;
462c593315Sopenharmony_ci  zst.avail_out = (unsigned int)outlen;
472c593315Sopenharmony_ci  zst.next_out = out;
482c593315Sopenharmony_ci  rv = deflate(&zst, Z_SYNC_FLUSH);
492c593315Sopenharmony_ci  CU_ASSERT(rv == Z_OK);
502c593315Sopenharmony_ci
512c593315Sopenharmony_ci  deflateEnd(&zst);
522c593315Sopenharmony_ci
532c593315Sopenharmony_ci  return outlen - zst.avail_out;
542c593315Sopenharmony_ci}
552c593315Sopenharmony_ci
562c593315Sopenharmony_cistatic const char input[] =
572c593315Sopenharmony_ci    "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND "
582c593315Sopenharmony_ci    "EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
592c593315Sopenharmony_ci    "MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND "
602c593315Sopenharmony_ci    "NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE "
612c593315Sopenharmony_ci    "LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION "
622c593315Sopenharmony_ci    "OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION "
632c593315Sopenharmony_ci    "WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.";
642c593315Sopenharmony_ci
652c593315Sopenharmony_civoid test_nghttp2_gzip_inflate(void) {
662c593315Sopenharmony_ci  nghttp2_gzip *inflater;
672c593315Sopenharmony_ci  uint8_t in[4096], out[4096], *inptr;
682c593315Sopenharmony_ci  size_t inlen = sizeof(in);
692c593315Sopenharmony_ci  size_t inproclen, outproclen;
702c593315Sopenharmony_ci  const char *inputptr = input;
712c593315Sopenharmony_ci
722c593315Sopenharmony_ci  inlen = deflate_data(in, inlen, (const uint8_t *)input, sizeof(input) - 1);
732c593315Sopenharmony_ci
742c593315Sopenharmony_ci  CU_ASSERT(0 == nghttp2_gzip_inflate_new(&inflater));
752c593315Sopenharmony_ci  /* First 16 bytes */
762c593315Sopenharmony_ci  inptr = in;
772c593315Sopenharmony_ci  inproclen = inlen;
782c593315Sopenharmony_ci  outproclen = 16;
792c593315Sopenharmony_ci  CU_ASSERT(
802c593315Sopenharmony_ci      0 == nghttp2_gzip_inflate(inflater, out, &outproclen, inptr, &inproclen));
812c593315Sopenharmony_ci  CU_ASSERT(16 == outproclen);
822c593315Sopenharmony_ci  CU_ASSERT(inproclen > 0);
832c593315Sopenharmony_ci  CU_ASSERT(0 == memcmp(inputptr, out, outproclen));
842c593315Sopenharmony_ci  /* Next 32 bytes */
852c593315Sopenharmony_ci  inptr += inproclen;
862c593315Sopenharmony_ci  inlen -= inproclen;
872c593315Sopenharmony_ci  inproclen = inlen;
882c593315Sopenharmony_ci  inputptr += outproclen;
892c593315Sopenharmony_ci  outproclen = 32;
902c593315Sopenharmony_ci  CU_ASSERT(
912c593315Sopenharmony_ci      0 == nghttp2_gzip_inflate(inflater, out, &outproclen, inptr, &inproclen));
922c593315Sopenharmony_ci  CU_ASSERT(32 == outproclen);
932c593315Sopenharmony_ci  CU_ASSERT(inproclen > 0);
942c593315Sopenharmony_ci  CU_ASSERT(0 == memcmp(inputptr, out, outproclen));
952c593315Sopenharmony_ci  /* Rest */
962c593315Sopenharmony_ci  inptr += inproclen;
972c593315Sopenharmony_ci  inlen -= inproclen;
982c593315Sopenharmony_ci  inproclen = inlen;
992c593315Sopenharmony_ci  inputptr += outproclen;
1002c593315Sopenharmony_ci  outproclen = sizeof(out);
1012c593315Sopenharmony_ci  CU_ASSERT(
1022c593315Sopenharmony_ci      0 == nghttp2_gzip_inflate(inflater, out, &outproclen, inptr, &inproclen));
1032c593315Sopenharmony_ci  CU_ASSERT(sizeof(input) - 49 == outproclen);
1042c593315Sopenharmony_ci  CU_ASSERT(inproclen > 0);
1052c593315Sopenharmony_ci  CU_ASSERT(0 == memcmp(inputptr, out, outproclen));
1062c593315Sopenharmony_ci
1072c593315Sopenharmony_ci  inlen -= inproclen;
1082c593315Sopenharmony_ci  CU_ASSERT(0 == inlen);
1092c593315Sopenharmony_ci
1102c593315Sopenharmony_ci  nghttp2_gzip_inflate_del(inflater);
1112c593315Sopenharmony_ci}
112