4 #include <winpr/string.h>
5 #include <winpr/windows.h>
7 static const CHAR testStringA[] = {
'T',
'h',
'e',
' ',
'q',
'u',
'i',
'c',
'k',
' ',
'b',
8 'r',
'o',
'w',
'n',
' ',
'f',
'o',
'x',
' ',
'j',
'u',
9 'm',
'p',
's',
' ',
'o',
'v',
'e',
'r',
' ',
't',
'h',
10 'e',
' ',
'l',
'a',
'z',
'y',
' ',
'd',
'o',
'g',
'\0' };
12 #define testStringA_Length ((sizeof(testStringA) / sizeof(CHAR)) - 1)
14 static const CHAR testToken1A[] = {
'q',
'u',
'i',
'c',
'k',
'\0' };
15 static const CHAR testToken2A[] = {
'b',
'r',
'o',
'w',
'n',
'\0' };
16 static const CHAR testToken3A[] = {
'f',
'o',
'x',
'\0' };
18 #define testToken1A_Length ((sizeof(testToken1A) / sizeof(CHAR)) - 1)
19 #define testToken2A_Length ((sizeof(testToken2A) / sizeof(CHAR)) - 1)
20 #define testToken3A_Length ((sizeof(testToken3A) / sizeof(CHAR)) - 1)
22 static const CHAR testTokensA[] = {
'q',
'u',
'i',
'c',
'k',
'\r',
'\n',
'b',
'r',
'o',
23 'w',
'n',
'\r',
'\n',
'f',
'o',
'x',
'\r',
'\n',
'\0' };
25 #define testTokensA_Length ((sizeof(testTokensA) / sizeof(CHAR)) - 1)
27 static const CHAR testDelimiterA[] = {
'\r',
'\n',
'\0' };
29 #define testDelimiterA_Length ((sizeof(testDelimiter) / sizeof(CHAR)) - 1)
37 static const struct url_test_pair url_tests[] = {
38 {
"xxx%bar ga<ka>ee#%%#%{h}g{f{e%d|c\\b^a~p[q]r`s;t/u?v:w@x=y&z$xxx",
39 "xxx%25bar%20ga%3Cka%3Eee%23%25%25%23%25%7Bh%7Dg%7Bf%7Be%25d%7Cc%5Cb%5Ea~p%5Bq%5Dr%60s%3Bt%"
40 "2Fu%3Fv%3Aw%40x%3Dy%26z%24xxx" },
41 {
"äöúëü",
"%C3%A4%C3%B6%C3%BA%C3%AB%C3%BC" },
42 {
"🎅🏄🤘😈",
"%F0%9F%8E%85%F0%9F%8F%84%F0%9F%A4%98%F0%9F%98%88" },
43 {
"foo$.%.^.&.\\.txt+",
"foo%24.%25.%5E.%26.%5C.txt%2B" }
46 static BOOL test_url_escape(
void)
48 for (
size_t x = 0; x < ARRAYSIZE(url_tests); x++)
50 const struct url_test_pair* cur = &url_tests[x];
52 char* escaped = winpr_str_url_encode(cur->what, strlen(cur->what) + 1);
53 char* what = winpr_str_url_decode(cur->escaped, strlen(cur->escaped) + 1);
55 const size_t elen = strlen(escaped);
56 const size_t wlen = strlen(what);
57 const size_t pelen = strlen(cur->escaped);
58 const size_t pwlen = strlen(cur->what);
60 if (!escaped || (elen != pelen) || (strcmp(escaped, cur->escaped) != 0))
62 printf(
"expected: [%" PRIuz
"] %s\n", pelen, cur->escaped);
63 printf(
"got : [%" PRIuz
"] %s\n", elen, escaped);
66 else if (!what || (wlen != pwlen) || (strcmp(what, cur->what) != 0))
68 printf(
"expected: [%" PRIuz
"] %s\n", pwlen, cur->what);
69 printf(
"got : [%" PRIuz
"] %s\n", wlen, what);
82 static BOOL test_winpr_asprintf(
void)
85 const char test[] =
"test string case";
86 const size_t len = strnlen(test,
sizeof(test));
90 const int res = winpr_asprintf(&str, &slen,
"%s", test);
95 if ((
size_t)res != len)
99 if (strnlen(str, slen + 10) != slen)
107 int TestString(
int argc,
char* argv[])
109 const WCHAR* p = NULL;
112 WCHAR* context = NULL;
117 if (!test_winpr_asprintf())
120 if (!test_url_escape())
124 WCHAR testStringW[ARRAYSIZE(testStringA)] = { 0 };
125 (void)ConvertUtf8NToWChar(testStringA, ARRAYSIZE(testStringA), testStringW,
126 ARRAYSIZE(testStringW));
127 const size_t testStringW_Length = testStringA_Length;
128 length = _wcslen(testStringW);
130 if (length != testStringW_Length)
132 printf(
"_wcslen error: length mismatch: Actual: %" PRIuz
", Expected: %" PRIuz
"\n", length,
146 p = _wcschr(testStringW, search.w);
147 pos = (p - testStringW);
151 printf(
"_wcschr error: position mismatch: Actual: %" PRIuz
", Expected: 11\n", pos);
155 p = _wcschr(&testStringW[pos + 1], search.w);
156 pos = (p - testStringW);
160 printf(
"_wcschr error: position mismatch: Actual: %" PRIuz
", Expected: 29\n", pos);
164 p = _wcschr(&testStringW[pos + 1], search.w);
168 printf(
"_wcschr error: return value mismatch: Actual: %p, Expected: NULL\n",
174 WCHAR testDelimiterW[ARRAYSIZE(testDelimiterA)] = { 0 };
175 WCHAR testTokensW[ARRAYSIZE(testTokensA)] = { 0 };
176 (void)ConvertUtf8NToWChar(testTokensA, ARRAYSIZE(testTokensA), testTokensW,
177 ARRAYSIZE(testTokensW));
178 (void)ConvertUtf8NToWChar(testDelimiterA, ARRAYSIZE(testDelimiterA), testDelimiterW,
179 ARRAYSIZE(testDelimiterW));
180 p = wcstok_s(testTokensW, testDelimiterW, &context);
182 WCHAR testToken1W[ARRAYSIZE(testToken1A)] = { 0 };
183 (void)ConvertUtf8NToWChar(testToken1A, ARRAYSIZE(testToken1A), testToken1W,
184 ARRAYSIZE(testToken1W));
185 if (memcmp(p, testToken1W,
sizeof(testToken1W)) != 0)
187 printf(
"wcstok_s error: token #1 mismatch\n");
191 p = wcstok_s(NULL, testDelimiterW, &context);
193 WCHAR testToken2W[ARRAYSIZE(testToken2A)] = { 0 };
194 (void)ConvertUtf8NToWChar(testToken2A, ARRAYSIZE(testToken2A), testToken2W,
195 ARRAYSIZE(testToken2W));
196 if (memcmp(p, testToken2W,
sizeof(testToken2W)) != 0)
198 printf(
"wcstok_s error: token #2 mismatch\n");
202 p = wcstok_s(NULL, testDelimiterW, &context);
204 WCHAR testToken3W[ARRAYSIZE(testToken3A)] = { 0 };
205 (void)ConvertUtf8NToWChar(testToken3A, ARRAYSIZE(testToken3A), testToken3W,
206 ARRAYSIZE(testToken3W));
207 if (memcmp(p, testToken3W,
sizeof(testToken3W)) != 0)
209 printf(
"wcstok_s error: token #3 mismatch\n");
213 p = wcstok_s(NULL, testDelimiterW, &context);
217 printf(
"wcstok_s error: return value is not NULL\n");