a20a2c3602d1f7aab3e6abea6a6fd08c8024beaf
[reactos.git] / rostests / apitests / advapi32 / IsTextUnicode.c
1 /*
2 * PROJECT: ReactOS api tests
3 * LICENSE: GPLv2+ - See COPYING in the top level directory
4 * PURPOSE: Tests for (Rtl)IsTextUnicode.
5 * PROGRAMMER: Hermes Belusca-Maito
6 */
7
8 #include <apitest.h>
9
10 #define WIN32_NO_STATUS
11 #include <ndk/rtlfuncs.h>
12 #include <winbase.h>
13 #include <winnls.h>
14 #include <wincon.h>
15 #include <stdio.h>
16
17 PVOID LoadCodePageData(ULONG Code)
18 {
19 char filename[MAX_PATH], sysdir[MAX_PATH];
20 HANDLE hFile;
21 PVOID Data = NULL;
22 GetSystemDirectoryA(sysdir, MAX_PATH);
23
24 if (Code != -1)
25 sprintf(filename, "%s\\c_%lu.nls", sysdir, Code);
26 else
27 sprintf(filename, "%s\\l_intl.nls", sysdir);
28
29 hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
30 if (hFile != INVALID_HANDLE_VALUE)
31 {
32 DWORD dwRead;
33 DWORD dwFileSize = GetFileSize(hFile, NULL);
34 Data = malloc(dwFileSize);
35 ReadFile(hFile, Data, dwFileSize, &dwRead, NULL);
36 CloseHandle(hFile);
37 }
38 return Data;
39 }
40
41 /* https://www.microsoft.com/resources/msdn/goglobal/default.mspx */
42 void SetupLocale(ULONG AnsiCode, ULONG OemCode, ULONG Unicode)
43 {
44 NLSTABLEINFO NlsTable;
45 PVOID AnsiCodePageData;
46 PVOID OemCodePageData;
47 PVOID UnicodeCaseTableData;
48
49 AnsiCodePageData = LoadCodePageData(AnsiCode);
50 OemCodePageData = LoadCodePageData(OemCode);
51 UnicodeCaseTableData = LoadCodePageData(Unicode);
52
53 RtlInitNlsTables(AnsiCodePageData, OemCodePageData, UnicodeCaseTableData, &NlsTable);
54 RtlResetRtlTranslations(&NlsTable);
55 /* Do NOT free the buffers here, they are directly used!
56 Yes, we leak the old buffers, but this is a test anyway... */
57
58 }
59
60 START_TEST(IsTextUnicode)
61 {
62 #define INVALID_FLAG 0xFFFFFFFF
63
64 #define NEW_TEST(Buffer, Flags, ResultFlags, Success) \
65 { (PVOID)(Buffer), sizeof((Buffer)), (Flags), (ResultFlags), (Success) }
66
67 static struct
68 {
69 /* Input */
70 PVOID Buffer;
71 INT Size;
72 INT Flags;
73
74 /* Output */
75 INT ResultFlags;
76 BOOL Success;
77 } Tests[] =
78 {
79 /* ANSI string */
80
81 // 0
82 NEW_TEST("ANSI string", IS_TEXT_UNICODE_ASCII16, 0, FALSE),
83 NEW_TEST("ANSI string", IS_TEXT_UNICODE_STATISTICS, 0, FALSE),
84 NEW_TEST("ANSI string", INVALID_FLAG, 0, FALSE),
85
86 /* UNICODE strings */
87
88 // 3
89 NEW_TEST(L"a", IS_TEXT_UNICODE_ASCII16, IS_TEXT_UNICODE_ASCII16, TRUE),
90 NEW_TEST(L"a", IS_TEXT_UNICODE_UNICODE_MASK, IS_TEXT_UNICODE_STATISTICS | IS_TEXT_UNICODE_ASCII16, TRUE),
91 NEW_TEST(L"a", IS_TEXT_UNICODE_STATISTICS, IS_TEXT_UNICODE_STATISTICS, TRUE),
92 NEW_TEST(L"a", INVALID_FLAG, 0, TRUE),
93
94 // 7
95 NEW_TEST(L"UNICODE String 0", IS_TEXT_UNICODE_ASCII16, 0, FALSE),
96 NEW_TEST(L"UNICODE String 0", IS_TEXT_UNICODE_UNICODE_MASK, IS_TEXT_UNICODE_CONTROLS | IS_TEXT_UNICODE_STATISTICS, TRUE),
97 NEW_TEST(L"UNICODE String 0", IS_TEXT_UNICODE_STATISTICS, IS_TEXT_UNICODE_STATISTICS, TRUE),
98 NEW_TEST(L"UNICODE String 0", INVALID_FLAG, 0, TRUE),
99
100 // 11
101 NEW_TEST(L"\xFEFF" L"UNICODE String 1", IS_TEXT_UNICODE_ASCII16, 0, FALSE),
102 NEW_TEST(L"\xFEFF" L"UNICODE String 1", IS_TEXT_UNICODE_UNICODE_MASK, IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_CONTROLS, TRUE),
103 NEW_TEST(L"\xFEFF" L"UNICODE String 1", IS_TEXT_UNICODE_STATISTICS, 0, FALSE),
104 NEW_TEST(L"\xFEFF" L"UNICODE String 1", INVALID_FLAG, 0, TRUE),
105
106 // 15
107 NEW_TEST(L"\xFFFE" L"UNICODE String 2", IS_TEXT_UNICODE_ASCII16, 0, FALSE),
108 NEW_TEST(L"\xFFFE" L"UNICODE String 2", IS_TEXT_UNICODE_UNICODE_MASK, IS_TEXT_UNICODE_CONTROLS, TRUE),
109 NEW_TEST(L"\xFFFE" L"UNICODE String 2", IS_TEXT_UNICODE_STATISTICS, 0, FALSE),
110 NEW_TEST(L"\xFFFE" L"UNICODE String 2", INVALID_FLAG, 0, FALSE),
111
112 // 19
113 NEW_TEST(L"UNICODE String 3 Привет!", IS_TEXT_UNICODE_ASCII16, 0, FALSE),
114 NEW_TEST(L"UNICODE String 3 Привет!", IS_TEXT_UNICODE_UNICODE_MASK, IS_TEXT_UNICODE_CONTROLS | IS_TEXT_UNICODE_STATISTICS, TRUE),
115 NEW_TEST(L"UNICODE String 3 Привет!", IS_TEXT_UNICODE_STATISTICS, IS_TEXT_UNICODE_STATISTICS, TRUE),
116 NEW_TEST(L"UNICODE String 3 Привет!", INVALID_FLAG, 0, TRUE),
117
118 // 23
119 NEW_TEST(L"\xFEFF" L"UNICODE String 4 Привет!", IS_TEXT_UNICODE_ASCII16, 0, FALSE),
120 NEW_TEST(L"\xFEFF" L"UNICODE String 4 Привет!", IS_TEXT_UNICODE_UNICODE_MASK, IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_CONTROLS, TRUE),
121 NEW_TEST(L"\xFEFF" L"UNICODE String 4 Привет!", IS_TEXT_UNICODE_STATISTICS, 0, FALSE),
122 NEW_TEST(L"\xFEFF" L"UNICODE String 4 Привет!", INVALID_FLAG, 0, TRUE),
123
124 // 27
125 NEW_TEST(L"\xFFFE" L"UNICODE String 5 Привет!", IS_TEXT_UNICODE_ASCII16, 0, FALSE),
126 NEW_TEST(L"\xFFFE" L"UNICODE String 5 Привет!", IS_TEXT_UNICODE_UNICODE_MASK, IS_TEXT_UNICODE_CONTROLS, TRUE),
127 NEW_TEST(L"\xFFFE" L"UNICODE String 5 Привет!", IS_TEXT_UNICODE_STATISTICS, 0, FALSE),
128 NEW_TEST(L"\xFFFE" L"UNICODE String 5 Привет!", INVALID_FLAG, 0, FALSE),
129
130 // 31
131 /* Reverse BOM */
132 NEW_TEST(L"UNICODE S" L"\xFFFE" L"tring 5 Привет!", IS_TEXT_UNICODE_ILLEGAL_CHARS, IS_TEXT_UNICODE_ILLEGAL_CHARS, FALSE),
133 /* UNICODE_NUL */
134 NEW_TEST(L"UNICODE S" L"\x0000" L"tring 5 Привет!", IS_TEXT_UNICODE_ILLEGAL_CHARS, IS_TEXT_UNICODE_ILLEGAL_CHARS, FALSE),
135 /* ASCII CRLF (packed into one word) */
136 NEW_TEST(L"UNICODE S" L"\x0A0D" L"tring 5 Привет!", IS_TEXT_UNICODE_ILLEGAL_CHARS, IS_TEXT_UNICODE_ILLEGAL_CHARS, FALSE),
137 /* Unicode 0xFFFF */
138 NEW_TEST(L"UNICODE S" L"\xFFFF" L"tring 5 Привет!", IS_TEXT_UNICODE_ILLEGAL_CHARS, IS_TEXT_UNICODE_ILLEGAL_CHARS, FALSE),
139
140 // 35
141 NEW_TEST(L"UNICODE String 0", IS_TEXT_UNICODE_DBCS_LEADBYTE, 0, FALSE)
142 };
143
144 const char japanese_with_lead[] = "ABC" "\x83\x40" "D";
145 const char simplfied_chinese_with_lead[] = "ABC" "\xC5\xC5" "D";
146 const char korean_with_lead[] = "ABC" "\xBF\xAD" "D";
147 const char traditional_chinese_with_lead[] = "ABC" "\xB1\xC1" "D";
148
149 UINT i;
150 BOOL Success;
151 INT Result;
152
153 for (i = 0; i < ARRAYSIZE(Tests); ++i)
154 {
155 Result = Tests[i].Flags;
156 Success = IsTextUnicode(Tests[i].Buffer, Tests[i].Size, ((Result != INVALID_FLAG) ? &Result : NULL));
157 ok(Success == Tests[i].Success, "IsTextUnicode(%u) returned 0x%x, expected %s\n", i, Success, (Tests[i].Success ? "TRUE" : "FALSE"));
158 if (Result != INVALID_FLAG)
159 ok(Result == Tests[i].ResultFlags, "IsTextUnicode(%u) Result returned 0x%x, expected 0x%x\n", i, Result, Tests[i].ResultFlags);
160 }
161
162 /* Japanese */
163 SetupLocale(932, 932, -1);
164
165 Result = IS_TEXT_UNICODE_DBCS_LEADBYTE;
166 ok(!IsTextUnicode(japanese_with_lead, sizeof(japanese_with_lead), &Result), "IsTextUnicode() returned TRUE, expected FALSE\n");
167 ok(Result == IS_TEXT_UNICODE_DBCS_LEADBYTE, "Result returned 0x%x, expected 0x%x\n", Result, IS_TEXT_UNICODE_DBCS_LEADBYTE);
168
169 /* Simplified Chinese */
170 SetupLocale(936, 936, -1);
171
172 Result = IS_TEXT_UNICODE_DBCS_LEADBYTE;
173 ok(!IsTextUnicode(simplfied_chinese_with_lead, sizeof(simplfied_chinese_with_lead), &Result), "IsTextUnicode() returned TRUE, expected FALSE\n");
174 ok(Result == IS_TEXT_UNICODE_DBCS_LEADBYTE, "Result returned 0x%x, expected 0x%x\n", Result, IS_TEXT_UNICODE_DBCS_LEADBYTE);
175
176 /* Korean */
177 SetupLocale(949, 949, -1);
178
179 Result = IS_TEXT_UNICODE_DBCS_LEADBYTE;
180 ok(!IsTextUnicode(korean_with_lead, sizeof(korean_with_lead), &Result), "IsTextUnicode() returned TRUE, expected FALSE\n");
181 ok(Result == IS_TEXT_UNICODE_DBCS_LEADBYTE, "Result returned 0x%x, expected 0x%x\n", Result, IS_TEXT_UNICODE_DBCS_LEADBYTE);
182
183 /* Traditional Chinese */
184 SetupLocale(950, 950, -1);
185
186 Result = IS_TEXT_UNICODE_DBCS_LEADBYTE;
187 ok(!IsTextUnicode(traditional_chinese_with_lead, sizeof(traditional_chinese_with_lead), &Result), "IsTextUnicode() returned TRUE, expected FALSE\n");
188 ok(Result == IS_TEXT_UNICODE_DBCS_LEADBYTE, "Result returned 0x%x, expected 0x%x\n", Result, IS_TEXT_UNICODE_DBCS_LEADBYTE);
189 }