[PATCH 2/2] msvcrt/tests: Add basic tests for UTF-8 mode

Detlef Riekenberg wine.dev at web.de
Fri Aug 24 09:47:59 CDT 2012


--
By by ... Detlef
---
 dlls/msvcrt/tests/file.c |  151 ++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 151 insertions(+), 0 deletions(-)

diff --git a/dlls/msvcrt/tests/file.c b/dlls/msvcrt/tests/file.c
index e450b72..03c62a5 100644
--- a/dlls/msvcrt/tests/file.c
+++ b/dlls/msvcrt/tests/file.c
@@ -39,6 +39,59 @@ static HANDLE proc_handles[2];
 static int (__cdecl *p_fopen_s)(FILE**, const char*, const char*);
 static int (__cdecl *p__wfopen_s)(FILE**, const wchar_t*, const wchar_t*);
 
+static WCHAR tmpfile_prefix[] = {'c','r','t',0};
+static WCHAR dataW[] = {'t', 'e', 's', 't', '_', 0xe4, 0xf6, 0xfc, 0xdf, 0}; /* äöüß */
+static CHAR data_utf8[] = { 0xef, 0xbb, 0xbf, 't','e','s','t','_',
+                            0xc3, 0xa4, 0xc3, 0xb6, 0xc3, 0xbc, 0xc3, 0x9f, 0}; /* äöüß */
+static WCHAR mode_w_utf8[] = {'w',',',' ','c','c','s','=','U','T','F','-','8',0};
+static WCHAR mode_r_utf8[] = {'r',',',' ','c','c','s','=','U','T','F','-','8',0};
+static WCHAR mode_rb[] = {'r','b',0};
+static WCHAR mode_wb[] = {'w','b',0};
+
+static const char * wine_dbgstr_an(const char * data, int len)
+{
+    static const char hex_data[] = "0123456789abcdef";
+    static char buffer[512];
+    char * ptr = buffer;
+
+    if (!data)
+        return "(null)";
+
+    if (len == -1)
+        len = strlen(data);
+
+    ptr[0] = '"';
+    ptr++;
+    while ((len > 0) && (ptr < (buffer + sizeof(buffer) - 8)))
+    {
+        if (*data == '\\')
+        {
+            ptr[0] = '\\';
+            ptr[1] = '\\';
+            ptr +=2;
+        }
+        else if ((*data < ' ') || (*data > '~'))
+        {
+            ptr[0] = '\\';
+            ptr[1] = hex_data[((*data) >>4) & 0x0f];
+            ptr[2] = hex_data[(*data) & 0x0f];
+            ptr += 3;
+        }
+        else
+        {
+            ptr[0] = *data;
+            ptr++;
+        }
+        len--;
+        data++;
+    }
+    ptr[0] = '"';
+    ptr++;
+
+    *ptr = 0;
+    return buffer;
+}
+
 static void init(void)
 {
     HMODULE hmod = GetModuleHandleA("msvcrt.dll");
@@ -1621,6 +1674,103 @@ static void test_dup2(void)
     ok(-1 == _dup2(0, -1), "expected _dup2 to fail when second arg is negative\n" );
 }
 
+static void test_utf8(void)
+{
+    WCHAR temppath[MAX_PATH];
+    WCHAR tempfile[MAX_PATH];
+    FILE* f;
+    DWORD len;
+    int res;
+    char buffer[64];
+
+    GetTempPathW(MAX_PATH, temppath);
+    GetTempFileNameW(temppath, tmpfile_prefix, 0, tempfile);
+
+    /* test write functions */
+    /* write as bytes: fwrite */
+    errno = 0xdeadbeef;
+    f = _wfopen(tempfile, mode_w_utf8);
+    len = fwrite(dataW, 1, sizeof(dataW), f);
+    ok(len == sizeof(dataW), "%d bytes written: 0x%x(expected 20)\n", len, errno);
+    fclose(f);
+
+    /* read in binary mode to get the UTF-8 data */
+    f = _wfopen(tempfile, mode_rb);
+    memset(buffer, 0, sizeof(buffer) -1);
+    len = fread(buffer, 1, sizeof(buffer) - 1, f);
+    if(len == sizeof(dataW))
+    {
+        skip("UTF-8 mode not supported\n");
+        fclose(f);
+        _wunlink(tempfile);
+        return;
+    }
+
+    /* data must start with UTF-8 BOM */
+    ok(len == sizeof(data_utf8), "read %d bytes with 0x%x (expected 17): %s\n",
+        len, errno, wine_dbgstr_an(buffer, len));
+    if(len == sizeof(data_utf8))
+        ok(!memcmp(buffer, data_utf8, len), "wrong file data: %s\n", wine_dbgstr_an(buffer, len));
+    fclose(f);
+
+
+    /* write as string: fputws */
+    errno = 0xdeadbeef;
+    f = _wfopen(tempfile, mode_w_utf8);
+    res = fputws(dataW, f);
+    ok(!res, "got %d with 0x%x (expected 0)\n", res, errno);
+    fclose(f);
+    /* read in binary mode to get the UTF-8 data */
+    f = _wfopen(tempfile, mode_rb);
+    memset(buffer, 0, sizeof(buffer) -1);
+    len = fread(buffer, 1, sizeof(buffer) - 1, f);
+    /* terminating 0 was not written */
+    ok(len == strlen(data_utf8), "read %d bytes with 0x%x (expected 16): %s\n",
+        len, errno, wine_dbgstr_an(buffer, len));
+    if(len == strlen(data_utf8))
+        ok(!memcmp(buffer, data_utf8, len), "wrong file data: %s\n", wine_dbgstr_an(buffer, len));
+    fclose(f);
+
+    /* write as string: fwprintf */
+    errno = 0xdeadbeef;
+    f = _wfopen(tempfile, mode_w_utf8);
+    len = fwprintf(f, dataW);
+    ok(len == lstrlenW(dataW), "got %d with 0x%x (expected 9)\n", len, errno);
+    fclose(f);
+    /* read in binary mode to get the UTF-8 data */
+    f = _wfopen(tempfile, mode_rb);
+    memset(buffer, 0, sizeof(buffer) -1);
+    len = fread(buffer, 1, sizeof(buffer) - 1, f);
+    fclose(f);
+    /* terminating 0 was not written */
+    ok(len == strlen(data_utf8), "read %d bytes with 0x%x (expected 16): %s\n",
+        len, errno, wine_dbgstr_an(buffer, len));
+    if(len == strlen(data_utf8))
+        ok(!memcmp(buffer, data_utf8, len), "wrong file data: %s\n", wine_dbgstr_an(buffer, len));
+
+
+    /* test read functions */
+    /* write UTF-8 data in binary mode */
+    errno = 0xdeadbeef;
+    f = _wfopen(tempfile, mode_wb);
+    len = fwrite(data_utf8, 1, sizeof(data_utf8), f);
+    ok(len == sizeof(data_utf8), "%d bytes written: 0x%x (expected 20)\n", len, errno);
+    fclose(f);
+
+    /* read back in UTF-8 mode: automatic converted to unicode */
+    f = _wfopen(tempfile, mode_r_utf8);
+    memset(buffer, 0, sizeof(buffer) -1);
+    len = fread(buffer, 1, sizeof(buffer) - 1, f);
+    fclose(f);
+
+    ok( len == sizeof(dataW), "read %d bytes with 0x%x (expected 20): %s\n",
+        len, errno, wine_dbgstr_an(buffer, len));
+    if (len == sizeof(dataW))
+        ok(!memcmp(buffer, dataW, len), "wrong file data: %s\n", wine_dbgstr_an(buffer, len));
+
+    _wunlink(tempfile);
+}
+
 START_TEST(file)
 {
     int arg_c;
@@ -1674,6 +1824,7 @@ START_TEST(file)
     test_get_osfhandle();
     test_setmaxstdio();
     test_pipes(arg_v[0]);
+    test_utf8();
 
     /* Wait for the (_P_NOWAIT) spawned processes to finish to make sure the report
      * file contains lines in the correct order
-- 
1.7.5.4




More information about the wine-patches mailing list