Browse Source

[unittests] add test_buffer and test_base64 unit tests

From: Stefan Bühler <stbuehler@web.de>

git-svn-id: svn://svn.lighttpd.net/lighttpd/branches/lighttpd-1.4.x@3118 152afb58-edef-0310-8abb-c4023f1b3aa9
svn/heads/lighttpd-1.4.x
Stefan Bühler 6 years ago
parent
commit
92c3da847b
  1. 1
      NEWS
  2. 18
      src/CMakeLists.txt
  3. 13
      src/Makefile.am
  4. 146
      src/test_base64.c
  5. 85
      src/test_buffer.c

1
NEWS

@ -45,6 +45,7 @@ NEWS
* [core] add generic vector implementation
* [core] replace array weakref with vector
* [base64] fix crash due to broken force_assert
* [unittests] add test_buffer and test_base64 unit tests
- 1.4.39 - 2016-01-02
* [core] fix memset_s call (fixes #2698)

18
src/CMakeLists.txt

@ -566,6 +566,19 @@ add_and_install_library(mod_userdir mod_userdir.c)
add_and_install_library(mod_usertrack mod_usertrack.c)
add_and_install_library(mod_webdav mod_webdav.c)
add_executable(test_buffer
test_buffer.c
buffer.c
)
add_test(NAME test_buffer COMMAND test_buffer)
add_executable(test_base64
test_base64.c
buffer.c
base64.c
)
add_test(NAME test_base64 COMMAND test_base64)
if(HAVE_PCRE_H)
target_link_libraries(lighttpd ${PCRE_LDFLAGS})
add_target_properties(lighttpd COMPILE_FLAGS ${PCRE_CFLAGS})
@ -685,6 +698,11 @@ endif()
if(WITH_LIBUNWIND)
target_link_libraries(lighttpd ${LIBUNWIND_LDFLAGS})
add_target_properties(lighttpd COMPILE_FLAGS ${LIBUNWIND_CFLAGS})
target_link_libraries(test_buffer ${LIBUNWIND_LDFLAGS})
add_target_properties(test_buffer COMPILE_FLAGS ${LIBUNWIND_CFLAGS})
target_link_libraries(test_base64 ${LIBUNWIND_LDFLAGS})
add_target_properties(test_base64 COMPILE_FLAGS ${LIBUNWIND_CFLAGS})
endif()
if(NOT WIN32)

13
src/Makefile.am

@ -1,9 +1,13 @@
AM_CFLAGS = $(FAM_CFLAGS) $(LIBUNWIND_CFLAGS)
noinst_PROGRAMS=proc_open lemon # simple-fcgi #graphic evalo bench ajp ssl error_test adserver gen-license
noinst_PROGRAMS=lemon proc_open test_buffer test_base64
sbin_PROGRAMS=lighttpd lighttpd-angel
LEMON=$(top_builddir)/src/lemon$(EXEEXT)
TESTS=\
test_buffer$(EXEEXT) \
test_base64$(EXEEXT)
lemon_SOURCES=lemon.c
lighttpd_angel_SOURCES=lighttpd-angel.c
@ -295,6 +299,12 @@ proc_open_SOURCES = proc_open.c buffer.c
proc_open_LDADD = $(LIBUNWIND_LIBS)
proc_open_CPPFLAGS= -DDEBUG_PROC_OPEN
test_buffer_SOURCES = test_buffer.c buffer.c
test_buffer_LDADD = $(LIBUNWIND_LIBS)
test_base64_SOURCES = test_base64.c base64.c buffer.c
test_base64_LDADD = $(LIBUNWIND_LIBS)
noinst_HEADERS = $(hdr)
EXTRA_DIST = \
mod_skeleton.c \
@ -303,3 +313,4 @@ EXTRA_DIST = \
lempar.c \
SConscript \
CMakeLists.txt config.h.cmake

146
src/test_base64.c

@ -0,0 +1,146 @@
#include "first.h"
#include "base64.h"
static void check_all_len_0() {
buffer *check = buffer_init();
const char empty[] = "";
{
unsigned char* check_res;
force_assert(0 == li_to_base64_no_padding(NULL, 0, NULL, 0, BASE64_STANDARD));
buffer_reset(check);
check_res = buffer_append_base64_decode(check, NULL, 0, BASE64_STANDARD);
force_assert((check_res != NULL) && (check_res == (unsigned char*) check->ptr));
force_assert(buffer_is_equal_string(check, empty, 0));
}
{
unsigned char* check_res;
force_assert(0 == li_to_base64_no_padding(NULL, 0, NULL, 0, BASE64_URL));
buffer_reset(check);
check_res = buffer_append_base64_decode(check, NULL, 0, BASE64_URL);
force_assert((check_res != NULL) && (check_res == (unsigned char*) check->ptr));
force_assert(buffer_is_equal_string(check, empty, 0));
}
buffer_free(check);
}
static void check_all_len_1() {
unsigned int c1;
buffer *check = buffer_init();
for (c1 = 0; c1 < 256; ++c1) {
{
unsigned char in[] = { c1 };
char out[2] = { 0, 0 };
unsigned char* check_res;
force_assert(sizeof(out) == li_to_base64_no_padding(out, sizeof(out), in, sizeof(in), BASE64_STANDARD));
buffer_reset(check);
check_res = buffer_append_base64_decode(check, out, sizeof(out), BASE64_STANDARD);
force_assert((check_res != NULL) && (check_res == (unsigned char*) check->ptr));
force_assert(buffer_is_equal_string(check, (const char*) in, sizeof(in)));
}
{
unsigned char in[] = { c1 };
char out[2] = { 0, 0 };
unsigned char* check_res;
force_assert(sizeof(out) == li_to_base64_no_padding(out, sizeof(out), in, sizeof(in), BASE64_URL));
buffer_reset(check);
check_res = buffer_append_base64_decode(check, out, sizeof(out), BASE64_URL);
force_assert((check_res != NULL) && (check_res == (unsigned char*) check->ptr));
force_assert(buffer_is_equal_string(check, (const char*) in, sizeof(in)));
}
}
buffer_free(check);
}
static void check_all_len_2() {
unsigned int c1, c2;
buffer *check = buffer_init();
for (c1 = 0; c1 < 256; ++c1) for (c2 = 0; c2 < 256; ++c2) {
{
unsigned char in[] = { c1, c2 };
char out[3] = { 0, 0, 0 };
unsigned char* check_res;
force_assert(sizeof(out) == li_to_base64_no_padding(out, sizeof(out), in, sizeof(in), BASE64_STANDARD));
buffer_reset(check);
check_res = buffer_append_base64_decode(check, out, sizeof(out), BASE64_STANDARD);
force_assert((check_res != NULL) && (check_res == (unsigned char*) check->ptr));
force_assert(buffer_is_equal_string(check, (const char*) in, sizeof(in)));
}
{
unsigned char in[] = { c1, c2 };
char out[3] = { 0, 0, 0 };
unsigned char* check_res;
force_assert(sizeof(out) == li_to_base64_no_padding(out, sizeof(out), in, sizeof(in), BASE64_URL));
buffer_reset(check);
check_res = buffer_append_base64_decode(check, out, sizeof(out), BASE64_URL);
force_assert((check_res != NULL) && (check_res == (unsigned char*) check->ptr));
force_assert(buffer_is_equal_string(check, (const char*) in, sizeof(in)));
}
}
buffer_free(check);
}
static void check_all_len_3() {
unsigned int c1, c2, c3;
buffer *check = buffer_init();
for (c1 = 0; c1 < 256; ++c1) for (c2 = 0; c2 < 256; ++c2) for (c3 = 0; c3 < 256; ++c3) {
{
unsigned char in[] = { c1, c2, c3 };
char out[4] = { 0, 0, 0, 0 };
unsigned char* check_res;
force_assert(sizeof(out) == li_to_base64_no_padding(out, sizeof(out), in, sizeof(in), BASE64_STANDARD));
buffer_reset(check);
check_res = buffer_append_base64_decode(check, out, sizeof(out), BASE64_STANDARD);
force_assert((check_res != NULL) && (check_res == (unsigned char*) check->ptr));
force_assert(buffer_is_equal_string(check, (const char*) in, sizeof(in)));
}
{
unsigned char in[] = { c1, c2, c3 };
char out[4] = { 0, 0, 0, 0 };
unsigned char* check_res;
force_assert(sizeof(out) == li_to_base64_no_padding(out, sizeof(out), in, sizeof(in), BASE64_URL));
buffer_reset(check);
check_res = buffer_append_base64_decode(check, out, sizeof(out), BASE64_URL);
force_assert((check_res != NULL) && (check_res == (unsigned char*) check->ptr));
force_assert(buffer_is_equal_string(check, (const char*) in, sizeof(in)));
}
}
buffer_free(check);
}
int main() {
check_all_len_0();
check_all_len_1();
check_all_len_2();
check_all_len_3();
return 0;
}

85
src/test_buffer.c

@ -0,0 +1,85 @@
#include "first.h"
#include "buffer.h"
static void run_buffer_path_simplify(buffer *psrc, buffer *pdest, const char *in, size_t in_len, const char *out, size_t out_len) {
buffer_copy_string_len(psrc, in, in_len);
buffer_path_simplify(pdest, psrc);
if (!buffer_is_equal_string(pdest, out, out_len)) {
fprintf(stderr,
"%s.%d: buffer_path_simplify('%s') failed: expected '%s', got '%s'\n",
__FILE__,
__LINE__,
in,
out,
pdest->ptr ? pdest->ptr : "");
fflush(stderr);
abort();
} else {
fprintf(stdout,
"%s.%d: buffer_path_simplify('%s') succeeded: got '%s'\n",
__FILE__,
__LINE__,
in,
out);
if (psrc != pdest) buffer_copy_buffer(psrc, pdest);
buffer_path_simplify(pdest, psrc);
if (!buffer_is_equal_string(pdest, out, out_len)) {
fprintf(stderr,
"%s.%d: buffer_path_simplify('%s') failed - not idempotent: expected '%s', got '%s'\n",
__FILE__,
__LINE__,
out,
out,
pdest->ptr ? pdest->ptr : "");
fflush(stderr);
abort();
}
}
}
static void test_buffer_path_simplify_with(buffer *psrc, buffer *pdest) {
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN(""), CONST_STR_LEN(""));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN(" "), CONST_STR_LEN("/"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("/"), CONST_STR_LEN("/"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("//"), CONST_STR_LEN("/"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("abc"), CONST_STR_LEN("/abc"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("abc//"), CONST_STR_LEN("/abc/"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("abc/./xyz"), CONST_STR_LEN("/abc/xyz"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("abc/.//xyz"), CONST_STR_LEN("/abc/xyz"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("abc/../xyz"), CONST_STR_LEN("/xyz"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("/abc/./xyz"), CONST_STR_LEN("/abc/xyz"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("/abc//./xyz"), CONST_STR_LEN("/abc/xyz"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("/abc/../xyz"), CONST_STR_LEN("/xyz"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("abc/../xyz/."), CONST_STR_LEN("/xyz/"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("/abc/../xyz/."), CONST_STR_LEN("/xyz/"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("abc/./xyz/.."), CONST_STR_LEN("/abc/"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("/abc/./xyz/.."), CONST_STR_LEN("/abc/"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("./xyz/.."), CONST_STR_LEN("/"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN(".//xyz/.."), CONST_STR_LEN("/"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("/./xyz/.."), CONST_STR_LEN("/"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN(".././xyz/.."), CONST_STR_LEN("/"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("/.././xyz/.."), CONST_STR_LEN("/"));
}
static void test_buffer_path_simplify() {
buffer *psrc = buffer_init();
buffer *pdest = buffer_init();
/* test with using the same buffer and with using different buffers */
test_buffer_path_simplify_with(psrc, psrc);
test_buffer_path_simplify_with(pdest, psrc);
buffer_free(psrc);
buffer_free(pdest);
}
int main() {
test_buffer_path_simplify();
return 0;
}
Loading…
Cancel
Save