From: Pekka Riikonen Date: Sun, 27 Mar 2005 21:02:00 +0000 (+0000) Subject: Added stringprep tests. X-Git-Tag: silc.server.0.9.19~34 X-Git-Url: http://git.silcnet.org/gitweb/?p=silc.git;a=commitdiff_plain;h=65f01f4af613a4908f1602d57043552327f7dc78 Added stringprep tests. --- diff --git a/lib/silcutil/tests/Makefile.am b/lib/silcutil/tests/Makefile.am index ec7d5f30..3a61c0f2 100644 --- a/lib/silcutil/tests/Makefile.am +++ b/lib/silcutil/tests/Makefile.am @@ -17,9 +17,10 @@ AUTOMAKE_OPTIONS = 1.0 no-dependencies foreign -bin_PROGRAMS = test_silcstrutil +bin_PROGRAMS = test_silcstrutil test_silcstringprep test_silcstrutil_SOURCES = test_silcstrutil.c +test_silcstringprep_SOURCES = test_silcstringprep.c LIBS = $(SILC_COMMON_LIBS) LDADD = -L.. -L../.. -lsilc diff --git a/lib/silcutil/tests/test_silcstringprep.c b/lib/silcutil/tests/test_silcstringprep.c new file mode 100644 index 00000000..81328e87 --- /dev/null +++ b/lib/silcutil/tests/test_silcstringprep.c @@ -0,0 +1,107 @@ +/* Stringprep tests */ + +#include "silcincludes.h" + +typedef struct { + const char *comment; + const char *in; + const char *out; + int ret; + int enc; +} test_st; + +const test_st tests[] = { + {"Prohibited *", + "foo*", "", SILC_STRINGPREP_ERR_PROHIBITED}, + {"Prohibited ?", + "?foo", "", SILC_STRINGPREP_ERR_PROHIBITED}, + {"Prohibited ,", + "f,f", "", SILC_STRINGPREP_ERR_PROHIBITED}, + {"Prohibited !", + "!", "", SILC_STRINGPREP_ERR_PROHIBITED}, + {"Prohibited @", + "foo@faa", "", SILC_STRINGPREP_ERR_PROHIBITED}, + {"Normal casefold", + "Foobbeli-BofJFlkJDF", "foobbeli-bofjflkjdf"}, + {"Nothing", + "sauna.silcnet.org", "sauna.silcnet.org"}, + {"Nothing with #", + "#silc", "#silc"}, + {"Locale test", + "Päivää", "päivää", 0, SILC_STRING_LOCALE}, + {"Locale test2", + "#öäöö/&#\\#(&(&#(.äöäÄÖäÄÖÄÖ^'", + "#öäöö/&#\\#(&(&#(.äöääöääöäö^'", 0, SILC_STRING_LOCALE}, + + /* Some libidn tests */ + {"Map to nothing", + "foo\xC2\xAD\xCD\x8F\xE1\xA0\x86\xE1\xA0\x8B" + "bar" "\xE2\x80\x8B\xE2\x81\xA0" "baz\xEF\xB8\x80\xEF\xB8\x88" + "\xEF\xB8\x8F\xEF\xBB\xBF", "foobarbaz"}, + {"Case folding ASCII U+0043 U+0041 U+0046 U+0045", "CAFE", "cafe"}, + {"Case folding 8bit U+00DF (german sharp s)", "\xC3\x9F", "ss"}, + {"Case folding U+0130 (turkish capital I with dot)", + "\xC4\xB0", "i\xcc\x87"}, + {"ASCII space character U+0020", "\x20", "\x20", + SILC_STRINGPREP_ERR_PROHIBITED}, + {"ASCII control characters U+0010 U+007F", "\x10\x7F", "\x10\x7F", + SILC_STRINGPREP_ERR_PROHIBITED}, +}; + +int main(int argc, char **argv) +{ + bool success = FALSE; + int i, enc; + unsigned char *out = NULL; + SilcUInt32 out_len; + SilcStringprepStatus ret; + + if (argc > 1 && !strcmp(argv[1], "-d")) { + silc_debug = 1; + silc_debug_hexdump = 1; + silc_log_set_debug_string("*stringprep*,*utf8*"); + } + + for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) { + SILC_LOG_DEBUG(("Test case %d", i)); + SILC_LOG_DEBUG((" %d: %s", i, tests[i].comment)); + SILC_LOG_DEBUG((" %d: in: %s", i, tests[i].in)); + SILC_LOG_DEBUG((" %d: out: %s", i, tests[i].out)); + SILC_LOG_DEBUG((" %d: ret: %d", i, tests[i].ret)); + + if (!tests[i].enc) + enc = SILC_STRING_UTF8; + else + enc = tests[i].enc; + ret = silc_stringprep(tests[i].in, strlen(tests[i].in), + enc, SILC_IDENTIFIER_PREP, 0, + &out, &out_len, enc); + if (ret != SILC_STRINGPREP_OK) { + if (tests[i].ret != SILC_STRINGPREP_OK) { + SILC_LOG_DEBUG((" %d: Expected ret %d", i, ret)); + } else { + SILC_LOG_DEBUG(("%d: Error: %d", i, ret)); + goto err; + } + } else { + SILC_LOG_DEBUG((" %d: prepared out: %s", i, out)); + SILC_LOG_HEXDUMP((" %d: prepared dump", i), out, out_len); + if (memcmp(out, tests[i].out, out_len)) { + SILC_LOG_DEBUG((" %d: Output mismatch", i)); + goto err; + } + } + SILC_LOG_DEBUG((" %d: Output match", i)); + + silc_free(out); + out = NULL; + } + + success = TRUE; + + err: + SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE")); + fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE"); + + return success; +}