nghttp2/src/nghttp2_gzip_test.c

116 lines
3.7 KiB
C
Raw Normal View History

/*
2014-03-30 12:09:21 +02:00
* nghttp2 - HTTP/2 C Library
*
* Copyright (c) 2012 Tatsuhiro Tsujikawa
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
2013-07-12 17:19:03 +02:00
#include "nghttp2_gzip_test.h"
#include <stdio.h>
#include <assert.h>
#include <CUnit/CUnit.h>
#include <zlib.h>
2013-07-12 17:19:03 +02:00
#include "nghttp2_gzip.h"
2015-09-23 07:41:53 +02:00
static size_t deflate_data(uint8_t *out, size_t outlen, const uint8_t *in,
size_t inlen) {
int rv;
z_stream zst;
zst.next_in = Z_NULL;
zst.zalloc = Z_NULL;
zst.zfree = Z_NULL;
zst.opaque = Z_NULL;
rv = deflateInit(&zst, Z_DEFAULT_COMPRESSION);
2017-05-18 16:10:44 +02:00
CU_ASSERT(rv == Z_OK);
2012-10-01 14:51:24 +02:00
zst.avail_in = (unsigned int)inlen;
2014-11-27 15:39:04 +01:00
zst.next_in = (uint8_t *)in;
zst.avail_out = (unsigned int)outlen;
zst.next_out = out;
rv = deflate(&zst, Z_SYNC_FLUSH);
2017-05-18 16:10:44 +02:00
CU_ASSERT(rv == Z_OK);
deflateEnd(&zst);
2014-11-27 15:39:04 +01:00
return outlen - zst.avail_out;
}
static const char input[] =
2014-11-27 15:39:04 +01:00
"THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND "
"EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
"MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND "
"NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE "
"LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION "
"OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION "
"WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.";
2014-11-27 15:39:04 +01:00
void test_nghttp2_gzip_inflate(void) {
2013-07-12 17:19:03 +02:00
nghttp2_gzip *inflater;
uint8_t in[4096], out[4096], *inptr;
size_t inlen = sizeof(in);
size_t inproclen, outproclen;
const char *inputptr = input;
2014-11-27 15:39:04 +01:00
inlen = deflate_data(in, inlen, (const uint8_t *)input, sizeof(input) - 1);
2013-07-12 17:19:03 +02:00
CU_ASSERT(0 == nghttp2_gzip_inflate_new(&inflater));
/* First 16 bytes */
inptr = in;
inproclen = inlen;
outproclen = 16;
2014-11-27 15:39:04 +01:00
CU_ASSERT(
0 == nghttp2_gzip_inflate(inflater, out, &outproclen, inptr, &inproclen));
CU_ASSERT(16 == outproclen);
CU_ASSERT(inproclen > 0);
CU_ASSERT(0 == memcmp(inputptr, out, outproclen));
/* Next 32 bytes */
inptr += inproclen;
inlen -= inproclen;
inproclen = inlen;
inputptr += outproclen;
outproclen = 32;
2014-11-27 15:39:04 +01:00
CU_ASSERT(
0 == nghttp2_gzip_inflate(inflater, out, &outproclen, inptr, &inproclen));
CU_ASSERT(32 == outproclen);
CU_ASSERT(inproclen > 0);
CU_ASSERT(0 == memcmp(inputptr, out, outproclen));
/* Rest */
inptr += inproclen;
inlen -= inproclen;
inproclen = inlen;
inputptr += outproclen;
outproclen = sizeof(out);
2014-11-27 15:39:04 +01:00
CU_ASSERT(
0 == nghttp2_gzip_inflate(inflater, out, &outproclen, inptr, &inproclen));
CU_ASSERT(sizeof(input) - 49 == outproclen);
CU_ASSERT(inproclen > 0);
CU_ASSERT(0 == memcmp(inputptr, out, outproclen));
2012-10-01 14:51:24 +02:00
inlen -= inproclen;
CU_ASSERT(0 == inlen);
2013-07-12 17:19:03 +02:00
nghttp2_gzip_inflate_del(inflater);
}