From: konn Date: Thu, 1 May 2008 11:52:12 +0000 (+0000) Subject: * Added test code of the chxj_encoding() function. X-Git-Tag: v0.12.20~203 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=050dce0102874f8368489c6f5b054ef922d94fd2;p=modchxj%2Fmod_chxj.git * Added test code of the chxj_encoding() function. git-svn-id: svn+ssh://svn.sourceforge.jp/svnroot/modchxj/mod_chxj/branches/RELEASE_0_12_0@2600 1a406e8e-add9-4483-a2c8-d8cac5b7c224 --- diff --git a/src/chxj_encoding.c b/src/chxj_encoding.c index c48fb17f..c81fb515 100644 --- a/src/chxj_encoding.c +++ b/src/chxj_encoding.c @@ -45,11 +45,15 @@ chxj_encoding(request_rec *r, const char *src, apr_size_t *len) DBG(r,"none encoding."); return (char*)src; } + if ((int)*len < 0) { + ERR(r, "runtime exception: chxj_encoding(): invalid string size.[%d]", (int)*len); + return (char *)src; + } entryp = chxj_apply_convrule(r, dconf->convrules); if (entryp->encoding == NULL) { DBG(r,"none encoding."); - return (char*)src; + return (char *)src; } if (STRCASEEQ('n','N',"none", entryp->encoding)) { @@ -59,8 +63,8 @@ chxj_encoding(request_rec *r, const char *src, apr_size_t *len) ilen = *len; ibuf = apr_palloc(r->pool, ilen+1); if (ibuf == NULL) { - DBG(r,"end chxj_encoding()"); - return (char*)src; + ERR(r, "runtime exception: chxj_encoding(): Out of memory."); + return (char *)src; } memset(ibuf, 0, ilen+1); memcpy(ibuf, src, ilen); diff --git a/test/chxj_encoding/Makefile b/test/chxj_encoding/Makefile new file mode 100644 index 00000000..8672d221 --- /dev/null +++ b/test/chxj_encoding/Makefile @@ -0,0 +1,25 @@ +.SUFFIXES: +.SUFFIXES: .c.o + +TARGET= test_chxj_encoding + +TOP_DIR=../.. + +INC_DIR=-I$(TOP_DIR)/include \ + -I/usr/include/apache2 \ + -I/usr/include/apr-1.0 \ + -I$(TOP_DIR)/src + +all:test + + + +test: $(TARGET) + ./$(TARGET) + +$(TARGET): $(TARGET).c + gcc -g -o $@ -Wall -lcunit $< $(INC_DIR) -DLINUX=2 -D_REENTRANT -D_GNU_SOURCE -D_LARGEFILE64_SOURCE -lapr-1 -laprutil-1 + + +clean: + rm -f ./$(TARGET) diff --git a/test/chxj_encoding/test_chxj_encoding.c b/test/chxj_encoding/test_chxj_encoding.c new file mode 100644 index 00000000..68337af0 --- /dev/null +++ b/test/chxj_encoding/test_chxj_encoding.c @@ -0,0 +1,268 @@ +#include +#include +#include +#include +#define CHXJ_TEST +#define IMG_NOT_CONVERT_FILENAME + +#include "chxj_apache.h" +#include "apr.h" +#include "qs_malloc.c" +#include "qs_log.c" +#include "qs_parse_tag.c" +#include "qs_parse_string.c" +#include "qs_parse_attr.c" +#include "qs_ignore_sp.c" +#include "qs_parse_file.c" +#include "chxj_buffered_write.c" +#include "chxj_chtml10.c" +#include "chxj_chtml20.c" +#include "chxj_chtml30.c" +#include "chxj_chtml40.c" +#include "chxj_chtml50.c" +#include "chxj_jxhtml.c" +#include "chxj_jhtml.c" +#include "chxj_hdml.c" +#include "chxj_xhtml_mobile_1_0.c" +#include "chxj_node_convert.c" +#include "chxj_tag_util.c" +#include "chxj_encoding.c" +#include "chxj_cookie.c" +#include "chxj_img_conv.c" +#include "chxj_url_encode.c" +#include "chxj_apache.c" +#include "chxj_dbm.c" +#include "chxj_str_util.c" +#include + + +/*===========================================================================*/ +/* chxj_encoding() */ +/*===========================================================================*/ +void test_chxj_encoding_001(); +void test_chxj_encoding_002(); +void test_chxj_encoding_003(); +void test_chxj_encoding_004(); +void test_chxj_encoding_005(); +/* pend */ + +void test_log_rerror(const char *file, int line, int level, apr_status_t status, const request_rec *r, const char *fmt, ...) +{ + va_list ap; + fprintf(stderr, "ERROR LOG %s:%d ", file,line); + va_start(ap, fmt); + vfprintf(stderr, fmt,ap); + va_end(ap); + fprintf(stderr, "\n"); +} +void test_log_error(const char *file, int line, int level, apr_status_t status, const request_rec *r, const char *fmt, ...) +{ + va_list ap; + fprintf(stderr, "ERROR LOG %s:%d ", file,line); + va_start(ap, fmt); + vfprintf(stderr, fmt,ap); + va_end(ap); + fprintf(stderr, "\n"); +} + +char *chxj_qr_code_blob_handler(request_rec* r, const char* indata, size_t* len) +{ + return NULL; +} + +char *test_os_escape_path(apr_pool_t *p, const char *path, int partial) +{ + return (char *)path; +} + +void test_set_content_type(request_rec *r, const char *ct) +{ + fprintf(stderr, "%s:%d set_content_type(%s)\n", __FILE__,__LINE__,ct); +} + +const char *test_run_http_scheme(request_rec *r) +{ + static char *s = "http"; + return s; +} + +void * test_get_module_config(const ap_conf_vector_t *cv, const module *m) +{ + static mod_chxj_config cfg; + memset(&cfg, 0, sizeof(mod_chxj_config)); + cfg.new_line_type = NLTYPE_NONE; + return &cfg; +} + +chxjconvrule_entry * +chxj_apply_convrule(request_rec* r, apr_array_header_t* convrules) +{ + static chxjconvrule_entry entries; + memset(&entries, 0, sizeof(chxjconvrule_entry)); + entries.encoding = apr_pstrdup(r->pool, "UTF8"); + return &entries; +} + +char *test_ap_escape_html(apr_pool_t *pool, const char *s) +{ + return (char *)s; +} + +int +main() +{ + CU_pSuite str_util_suite; + CU_initialize_registry(); + str_util_suite = CU_add_suite("test chxj_encoding.c", NULL, NULL); + /*=========================================================================*/ + /* chxj_encoding() */ + /*=========================================================================*/ + CU_add_test(str_util_suite, "chxj_encoding() 001", test_chxj_encoding_001); + CU_add_test(str_util_suite, "chxj_encoding() 002", test_chxj_encoding_002); + CU_add_test(str_util_suite, "chxj_encoding() 003", test_chxj_encoding_003); + CU_add_test(str_util_suite, "chxj_encoding() 004", test_chxj_encoding_004); + CU_add_test(str_util_suite, "chxj_encoding() 005", test_chxj_encoding_005); + /* aend */ + + CU_basic_run_tests(); + CU_cleanup_registry(); + + return(0); +} + + + +#define APR_INIT \ + apr_pool_t *p; \ + request_rec r; \ + do { \ + apr_initialize(); \ + apr_pool_create(&p, NULL); \ + r.pool = p; \ + } \ + while (0) + +#define APR_TERM \ + do { \ + apr_terminate(); \ + } while (0) + +#define COOKIE_INIT(X) \ + do { \ + X.cookie_id = "test_cookie_id"; \ + } while (0) \ + +#define SPEC_INIT(X) \ + do { \ + X.html_spec_type = CHXJ_SPEC_Chtml_1_0; \ + } while (0) + + + + +/*===========================================================================*/ +/* chxj_encoding() */ +/*===========================================================================*/ +void test_chxj_encoding_001() +{ +#define TEST_STRING "あいうえお" +#define RESULT_STRING "\x82\xa0\x82\xa2\x82\xa4\x82\xa6\x82\xa8" + char *ret; + APR_INIT; + apr_size_t len; + + len = sizeof(TEST_STRING)-1; + ret = chxj_encoding(&r, TEST_STRING, &len); + fprintf(stderr, "actual:[%s]\n", ret); + fprintf(stderr, "expect:[%s]\n", RESULT_STRING); + fprintf(stderr, "len:[%d]\n", len); + CU_ASSERT(strcmp(ret, RESULT_STRING) == 0); + CU_ASSERT(len == sizeof(RESULT_STRING)-1); + + APR_TERM; +#undef RESULT_STRING +#undef TEST_STRING +} +void test_chxj_encoding_002() +{ +#define TEST_STRING "" +#define RESULT_STRING "" + char *ret; + APR_INIT; + apr_size_t len; + + len = sizeof(TEST_STRING)-1; + ret = chxj_encoding(&r, TEST_STRING, &len); + fprintf(stderr, "actual:[%s]\n", ret); + fprintf(stderr, "expect:[%s]\n", RESULT_STRING); + fprintf(stderr, "len:[%d]\n", len); + CU_ASSERT(strcmp(ret, RESULT_STRING) == 0); + CU_ASSERT(len == sizeof(RESULT_STRING)-1); + + APR_TERM; +#undef RESULT_STRING +#undef TEST_STRING +} +void test_chxj_encoding_003() +{ +#define TEST_STRING NULL +#define RESULT_STRING "" + char *ret; + APR_INIT; + apr_size_t len; + + len = 0; + ret = chxj_encoding(&r, TEST_STRING, &len); + fprintf(stderr, "actual:[%s]\n", ret); + fprintf(stderr, "expect:[%s]\n", RESULT_STRING); + fprintf(stderr, "len:[%d]\n", len); + CU_ASSERT(strcmp(ret, RESULT_STRING) == 0); + CU_ASSERT(len == sizeof(RESULT_STRING)-1); + + APR_TERM; +#undef RESULT_STRING +#undef TEST_STRING +} +void test_chxj_encoding_004() +{ +#define TEST_STRING NULL +#define RESULT_STRING "" + char *ret; + APR_INIT; + apr_size_t len; + + len = -1; + ret = chxj_encoding(&r, "", &len); + fprintf(stderr, "actual:[%s]\n", ret); + fprintf(stderr, "expect:[%s]\n", RESULT_STRING); + fprintf(stderr, "len:[%d]\n", len); + CU_ASSERT(strcmp(ret, RESULT_STRING) == 0); + CU_ASSERT(len == -1); + + APR_TERM; +#undef RESULT_STRING +#undef TEST_STRING +} +void test_chxj_encoding_005() +{ +#define TEST_STRING "\x82\xa0\x82\xa2\x82\xa4\x82\xa6\x82\xa8" +#define RESULT_STRING "" + char *ret; + APR_INIT; + apr_size_t len; + + len = sizeof(TEST_STRING)-1; + ret = chxj_encoding(&r, TEST_STRING, &len); + fprintf(stderr, "actual:[%s]\n", ret); + fprintf(stderr, "expect:[%s]\n", RESULT_STRING); + fprintf(stderr, "len:[%d]\n", len); + CU_ASSERT(strcmp(ret, RESULT_STRING) == 0); + CU_ASSERT(len == 0); + + APR_TERM; +#undef RESULT_STRING +#undef TEST_STRING +} +/* + * vim:ts=2 et + */