lib/util/charset rename iconv_convenience to iconv_handle
[nivanova/samba-autobuild/.git] / lib / util / charset / tests / iconv.c
index aeb42c2fa13f84cc4a0c040bfe84282880b391f9..c27c263cfc9494ce89755822e666c325ceccafa1 100644 (file)
@@ -27,6 +27,7 @@
 #include "libcli/raw/libcliraw.h"
 #include "param/param.h"
 #include "torture/util.h"
+#include "talloc.h"
 
 #if HAVE_NATIVE_ICONV
 
@@ -34,7 +35,7 @@ static bool iconv_untestable(struct torture_context *tctx)
 {
        iconv_t cd;
 
-       if (!lp_parm_bool(tctx->lp_ctx, NULL, "iconv", "native", true))
+       if (!lpcfg_parm_bool(tctx->lp_ctx, NULL, "iconv", "native", true))
                torture_skip(tctx, "system iconv disabled - skipping test");
 
        cd = iconv_open("UTF-16LE", "UCS-4LE");
@@ -133,7 +134,7 @@ static bool test_buffer(struct torture_context *test,
 {
        uint8_t buf1[1000], buf2[1000], buf3[1000];
        size_t outsize1, outsize2, outsize3;
-       const char *ptr_in;
+       char *ptr_in;
        char *ptr_out;
        size_t size_in1, size_in2, size_in3;
        size_t ret1, ret2, ret3, len1, len2;
@@ -157,31 +158,31 @@ static bool test_buffer(struct torture_context *test,
                                                     "failed to open %s to UTF-16LE",
                                                     charset));
                }
-               cd2 = smb_iconv_open_ex(test, charset, "UTF-16LE", lp_parm_bool(test->lp_ctx, NULL, "iconv", "native", true));
-               cd3 = smb_iconv_open_ex(test, "UTF-16LE", charset, lp_parm_bool(test->lp_ctx, NULL, "iconv", "native", true));
+               cd2 = smb_iconv_open_ex(test, charset, "UTF-16LE", lpcfg_parm_bool(test->lp_ctx, NULL, "iconv", "native", true));
+               cd3 = smb_iconv_open_ex(test, "UTF-16LE", charset, lpcfg_parm_bool(test->lp_ctx, NULL, "iconv", "native", true));
                last_charset = charset;
        }
 
        /* internal convert to charset - placing result in buf1 */
-       ptr_in = (const char *)inbuf;
+       ptr_in = (char *)inbuf;
        ptr_out = (char *)buf1;
        size_in1 = size;
        outsize1 = sizeof(buf1);
 
        memset(ptr_out, 0, outsize1);
        errno = 0;
-       ret1 = smb_iconv(cd2, &ptr_in, &size_in1, &ptr_out, &outsize1);
+       ret1 = smb_iconv(cd2, (const char **) &ptr_in, &size_in1, &ptr_out, &outsize1);
        errno1 = errno;
 
        /* system convert to charset - placing result in buf2 */
-       ptr_in = (const char *)inbuf;
+       ptr_in = (char *)inbuf;
        ptr_out = (char *)buf2;
        size_in2 = size;
        outsize2 = sizeof(buf2);
        
        memset(ptr_out, 0, outsize2);
        errno = 0;
-       ret2 = iconv(cd, discard_const_p(char *, &ptr_in), &size_in2, &ptr_out, &outsize2);
+       ret2 = iconv(cd, &ptr_in, &size_in2, &ptr_out, &outsize2);
        errno2 = errno;
 
        len1 = sizeof(buf1) - outsize1;
@@ -235,13 +236,13 @@ static bool test_buffer(struct torture_context *test,
 
        /* convert back to UTF-16, putting result in buf3 */
        size = size - size_in1;
-       ptr_in = (const char *)buf1;
+       ptr_in = (char *)buf1;
        ptr_out = (char *)buf3;
        size_in3 = len1;
        outsize3 = sizeof(buf3);
 
        memset(ptr_out, 0, outsize3);
-       ret3 = smb_iconv(cd3, &ptr_in, &size_in3, &ptr_out, &outsize3);
+       ret3 = smb_iconv(cd3, (const char **) &ptr_in, &size_in3, &ptr_out, &outsize3);
 
        /* we only internally support the first 1M codepoints */
        if (outsize3 != sizeof(buf3) - size &&
@@ -288,7 +289,7 @@ static bool test_codepoint(struct torture_context *tctx, unsigned int codepoint)
        size_t size, size2;
        codepoint_t c;
 
-       size = push_codepoint(lp_iconv_convenience(tctx->lp_ctx), (char *)buf, codepoint);
+       size = push_codepoint_handle(lpcfg_iconv_handle(tctx->lp_ctx), (char *)buf, codepoint);
        torture_assert(tctx, size != -1 || (codepoint >= 0xd800 && codepoint <= 0x10000), 
                       "Invalid Codepoint range");
 
@@ -299,7 +300,7 @@ static bool test_codepoint(struct torture_context *tctx, unsigned int codepoint)
        buf[size+2] = random();
        buf[size+3] = random();
 
-       c = next_codepoint(lp_iconv_convenience(tctx->lp_ctx), (char *)buf, &size2);
+       c = next_codepoint_handle(lpcfg_iconv_handle(tctx->lp_ctx), (char *)buf, &size2);
 
        torture_assert(tctx, c == codepoint, 
                       talloc_asprintf(tctx, 
@@ -398,9 +399,62 @@ static bool test_random_5m(struct torture_context *tctx)
        return true;
 }
 
+
+static bool test_string2key(struct torture_context *tctx)
+{
+       uint16_t *buf;
+       char *dest = NULL;
+       TALLOC_CTX *mem_ctx = talloc_new(tctx);
+       size_t len = (random()%1000)+1;
+       const uint16_t in1[10] = { 'a', 0xd805, 'b', 0xdcf0, 'c', 0, 'd', 'e', 'f', 'g' };
+       uint8_t le1[20];
+       uint8_t *munged1;
+       uint8_t *out1;
+       size_t ret;
+       int i;
+       const char *correct = "a\357\277\275b\357\277\275c\001defg";
+
+       buf = talloc_size(mem_ctx, len*2);
+       generate_random_buffer((uint8_t *)buf, len*2);
+
+       torture_comment(tctx, "converting random buffer\n");
+
+       if (!convert_string_talloc(mem_ctx, CH_UTF16MUNGED, CH_UTF8, (void *)buf, len*2, (void**)&dest, &ret)) {
+               torture_fail(tctx, "Failed to convert random buffer\n");
+       }
+
+       for (i=0;i<10;i++) {
+               SSVAL(&le1[2*i], 0, in1[i]);
+       }
+
+       torture_comment(tctx, "converting fixed buffer to UTF16\n");
+
+       if (!convert_string_talloc(mem_ctx, CH_UTF16MUNGED, CH_UTF16, (void *)le1, 20, (void**)&munged1, &ret)) {
+               torture_fail(tctx, "Failed to convert fixed buffer to UTF16_MUNGED\n");
+       }
+
+       torture_assert(tctx, ret == 20, "conversion should give 20 bytes\n");
+
+       torture_comment(tctx, "converting fixed buffer to UTF8\n");
+
+       if (!convert_string_talloc(mem_ctx, CH_UTF16MUNGED, CH_UTF8, (void *)le1, 20, (void**)&out1, &ret)) {
+               torture_fail(tctx, "Failed to convert fixed buffer to UTF8\n");
+       }
+
+       torture_assert(tctx, strcmp(correct, (const char *) out1) == 0,
+               "conversion gave incorrect result\n");
+
+       talloc_free(mem_ctx);
+
+       return true;
+}
+
 struct torture_suite *torture_local_iconv(TALLOC_CTX *mem_ctx)
 {
-       struct torture_suite *suite = torture_suite_create(mem_ctx, "ICONV");
+       struct torture_suite *suite = torture_suite_create(mem_ctx, "iconv");
+
+       torture_suite_add_simple_test(suite, "string2key",
+                                     test_string2key);
 
        torture_suite_add_simple_test(suite, "next_codepoint()",
                                      test_next_codepoint);
@@ -410,6 +464,9 @@ struct torture_suite *torture_local_iconv(TALLOC_CTX *mem_ctx)
 
        torture_suite_add_simple_test(suite, "5M random UTF-16LE sequences",
                                      test_random_5m);
+
+       torture_suite_add_simple_test(suite, "string2key",
+                                     test_string2key);
        return suite;
 }