combase: Implement functions for accessing HSTRING objects.
[wine.git] / dlls / combase / tests / string.c
blobec5e517a7fc7824cc1115235d919548a7f8fc435
1 /*
2 * Unit tests for Windows String functions
4 * Copyright (c) 2014 Martin Storsjo
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winerror.h"
26 #include "winstring.h"
28 #include "wine/test.h"
30 static HRESULT (WINAPI *pWindowsCreateString)(LPCWSTR, UINT32, HSTRING *);
31 static HRESULT (WINAPI *pWindowsCreateStringReference)(LPCWSTR, UINT32, HSTRING_HEADER *, HSTRING *);
32 static HRESULT (WINAPI *pWindowsDeleteString)(HSTRING);
33 static HRESULT (WINAPI *pWindowsDuplicateString)(HSTRING, HSTRING *);
34 static UINT32 (WINAPI *pWindowsGetStringLen)(HSTRING);
35 static LPCWSTR (WINAPI *pWindowsGetStringRawBuffer)(HSTRING, UINT32 *);
36 static BOOL (WINAPI *pWindowsIsStringEmpty)(HSTRING);
37 static HRESULT (WINAPI *pWindowsStringHasEmbeddedNull)(HSTRING, BOOL *);
39 #define SET(x) p##x = (void*)GetProcAddress(hmod, #x)
41 static BOOL init_functions(void)
43 HMODULE hmod = LoadLibraryA("combase.dll");
44 if (!hmod)
46 win_skip("Failed to load combase.dll, skipping tests\n");
47 return FALSE;
49 SET(WindowsCreateString);
50 SET(WindowsCreateStringReference);
51 SET(WindowsDeleteString);
52 SET(WindowsDuplicateString);
53 SET(WindowsGetStringLen);
54 SET(WindowsGetStringRawBuffer);
55 SET(WindowsIsStringEmpty);
56 SET(WindowsStringHasEmbeddedNull);
57 return TRUE;
60 #undef SET
63 #define check_string(str, content, length, has_null) _check_string(__LINE__, str, content, length, has_null)
64 static void _check_string(int line, HSTRING str, LPCWSTR content, UINT32 length, BOOL has_null)
66 BOOL out_null;
67 BOOL empty = length == 0;
68 UINT32 out_length;
69 LPCWSTR ptr;
71 ok_(__FILE__, line)(pWindowsIsStringEmpty(str) == empty, "WindowsIsStringEmpty failed\n");
72 ok_(__FILE__, line)(pWindowsStringHasEmbeddedNull(str, &out_null) == S_OK, "pWindowsStringHasEmbeddedNull failed\n");
73 ok_(__FILE__, line)(out_null == has_null, "WindowsStringHasEmbeddedNull failed\n");
74 ok_(__FILE__, line)(pWindowsGetStringLen(str) == length, "WindowsGetStringLen failed\n");
75 ptr = pWindowsGetStringRawBuffer(str, &out_length);
76 /* WindowsGetStringRawBuffer should return a non-null, null terminated empty string
77 * even if str is NULL. */
78 ok_(__FILE__, line)(ptr != NULL, "WindowsGetStringRawBuffer returned null\n");
79 ok_(__FILE__, line)(out_length == length, "WindowsGetStringRawBuffer returned incorrect length\n");
80 ptr = pWindowsGetStringRawBuffer(str, NULL);
81 ok_(__FILE__, line)(ptr != NULL, "WindowsGetStringRawBuffer returned null\n");
82 ok_(__FILE__, line)(ptr[length] == '\0', "WindowsGetStringRawBuffer doesn't return a null terminated buffer\n");
83 ok_(__FILE__, line)(memcmp(ptr, content, sizeof(*content) * length) == 0, "Incorrect string content\n");
86 static const WCHAR input_string[] = { 'a', 'b', 'c', 'd', 'e', 'f', '\0', '\0' };
87 static const WCHAR input_empty_string[] = { '\0' };
88 static const WCHAR input_embed_null[] = { 'a', '\0', 'c', '\0', 'e', 'f', '\0' };
90 static void test_create_delete(void)
92 HSTRING str;
93 HSTRING_HEADER header;
95 /* Test normal creation of a string */
96 ok(pWindowsCreateString(input_string, 6, &str) == S_OK, "Failed to create string\n");
97 check_string(str, input_string, 6, FALSE);
98 ok(pWindowsDeleteString(str) == S_OK, "Failed to delete string\n");
99 /* Test error handling in WindowsCreateString */
100 ok(pWindowsCreateString(input_string, 6, NULL) == E_INVALIDARG, "Incorrect error handling\n");
101 ok(pWindowsCreateString(NULL, 6, &str) == E_POINTER, "Incorrect error handling\n");
103 /* Test handling of a NULL string */
104 ok(pWindowsDeleteString(NULL) == S_OK, "Failed to delete null string\n");
106 /* Test creation of a string reference */
107 ok(pWindowsCreateStringReference(input_string, 6, &header, &str) == S_OK, "Failed to create string ref\n");
108 check_string(str, input_string, 6, FALSE);
109 ok(pWindowsDeleteString(str) == S_OK, "Failed to delete string ref\n");
111 /* Test error handling in WindowsCreateStringReference */
112 /* Strings to CreateStringReference must be null terminated with the correct
113 * length. According to MSDN this should be E_INVALIDARG, but it returns
114 * 0x80000017 in practice. */
115 ok(FAILED(pWindowsCreateStringReference(input_string, 5, &header, &str)), "Incorrect error handling\n");
116 ok(pWindowsCreateStringReference(input_string, 6, NULL, &str) == E_INVALIDARG, "Incorrect error handling\n");
117 ok(pWindowsCreateStringReference(input_string, 6, &header, NULL) == E_INVALIDARG, "Incorrect error handling\n");
118 ok(pWindowsCreateStringReference(NULL, 6, &header, &str) == E_POINTER, "Incorrect error handling\n");
120 /* Test creating a string without a null-termination at the specified length */
121 ok(pWindowsCreateString(input_string, 3, &str) == S_OK, "Failed to create string\n");
122 check_string(str, input_string, 3, FALSE);
123 ok(pWindowsDeleteString(str) == S_OK, "Failed to delete string\n");
125 /* Test an empty string */
126 ok(pWindowsCreateString(input_empty_string, 0, &str) == S_OK, "Failed to create string\n");
127 ok(str == NULL, "Empty string not a null string\n");
128 ok(pWindowsDeleteString(str) == S_OK, "Failed to delete string\n");
130 ok(pWindowsCreateStringReference(input_empty_string, 0, &header, &str) == S_OK, "Failed to create string\n");
131 ok(str == NULL, "Empty string not a null string\n");
132 ok(pWindowsDeleteString(str) == S_OK, "Failed to delete string\n");
135 static void test_duplicate(void)
137 HSTRING str, str2;
138 HSTRING_HEADER header;
139 ok(pWindowsCreateString(input_string, 6, &str) == S_OK, "Failed to create string\n");
140 ok(pWindowsDuplicateString(str, NULL) == E_INVALIDARG, "Incorrect error handling\n");
141 ok(pWindowsDuplicateString(str, &str2) == S_OK, "Failed to duplicate string\n");
142 ok(str == str2, "Duplicated string created new string\n");
143 ok(pWindowsDeleteString(str) == S_OK, "Failed to delete string\n");
144 ok(pWindowsDeleteString(str2) == S_OK, "Failed to delete string\n");
146 ok(pWindowsCreateStringReference(input_string, 6, &header, &str) == S_OK, "Failed to create string ref\n");
147 ok(pWindowsDuplicateString(str, &str2) == S_OK, "Failed to duplicate string\n");
148 ok(str != str2, "Duplicated string ref didn't create new string\n");
149 ok(pWindowsDeleteString(str) == S_OK, "Failed to delete string\n");
150 ok(pWindowsDeleteString(str2) == S_OK, "Failed to delete string ref\n");
152 ok(pWindowsDuplicateString(NULL, &str2) == S_OK, "Failed to duplicate NULL string\n");
153 ok(str2 == NULL, "Duplicated string created new string\n");
154 ok(pWindowsDeleteString(str2) == S_OK, "Failed to delete string\n");
157 static void test_access(void)
159 HSTRING str;
160 HSTRING_HEADER header;
162 /* Test handling of a NULL string */
163 check_string(NULL, NULL, 0, FALSE);
165 /* Test strings with embedded null chars */
166 ok(pWindowsCreateString(input_embed_null, 6, &str) == S_OK, "Failed to create string\n");
167 check_string(str, input_embed_null, 6, TRUE);
168 ok(pWindowsDeleteString(str) == S_OK, "Failed to delete string\n");
170 ok(pWindowsCreateStringReference(input_embed_null, 6, &header, &str) == S_OK, "Failed to create string ref\n");
171 check_string(str, input_embed_null, 6, TRUE);
172 ok(pWindowsDeleteString(str) == S_OK, "Failed to delete string ref\n");
174 /* Test normal creation of a string with trailing null */
175 ok(pWindowsCreateString(input_string, 7, &str) == S_OK, "Failed to create string\n");
176 check_string(str, input_string, 7, TRUE);
177 ok(pWindowsDeleteString(str) == S_OK, "Failed to delete string\n");
179 ok(pWindowsCreateStringReference(input_string, 7, &header, &str) == S_OK, "Failed to create string ref\n");
180 check_string(str, input_string, 7, TRUE);
181 ok(pWindowsDeleteString(str) == S_OK, "Failed to delete string ref\n");
184 START_TEST(string)
186 if (!init_functions())
187 return;
188 test_create_delete();
189 test_duplicate();
190 test_access();