secur32/tests: Add simple tests for GetUserNameExA/W().
[wine] / dlls / secur32 / tests / secur32.c
1 /*
2  * tests
3  *
4  * Copyright 2006 Robert Reif
5  *
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.
10  *
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.
15  *
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
19  */
20 #include <stdio.h>
21 #include <stdarg.h>
22 #include <windef.h>
23 #include <winbase.h>
24 #include <winnls.h>
25 #define SECURITY_WIN32
26 #include <security.h>
27 #include <schannel.h>
28
29 #include "wine/test.h"
30
31 static HMODULE secdll;
32
33 static BOOLEAN (WINAPI * pGetComputerObjectNameA)(EXTENDED_NAME_FORMAT NameFormat, LPSTR lpNameBuffer, PULONG lpnSize);
34 static BOOLEAN (WINAPI * pGetComputerObjectNameW)(EXTENDED_NAME_FORMAT NameFormat, LPWSTR lpNameBuffer, PULONG lpnSize);
35 static BOOLEAN (WINAPI * pGetUserNameExA)(EXTENDED_NAME_FORMAT NameFormat, LPSTR lpNameBuffer, PULONG lpnSize);
36 static BOOLEAN (WINAPI * pGetUserNameExW)(EXTENDED_NAME_FORMAT NameFormat, LPWSTR lpNameBuffer, PULONG lpnSize);
37 static PSecurityFunctionTableA (SEC_ENTRY * pInitSecurityInterfaceA)(void);
38 static PSecurityFunctionTableW (SEC_ENTRY * pInitSecurityInterfaceW)(void);
39
40 static EXTENDED_NAME_FORMAT formats[] = {
41     NameUnknown, NameFullyQualifiedDN, NameSamCompatible, NameDisplay,
42     NameUniqueId, NameCanonical, NameUserPrincipal, NameCanonicalEx,
43     NameServicePrincipal, NameDnsDomain
44 };
45
46 static void testGetComputerObjectNameA(void)
47 {
48     char name[256];
49     ULONG size;
50     BOOLEAN rc;
51     int i;
52
53     for (i = 0; i < (sizeof(formats) / sizeof(formats[0])); i++) {
54         size = sizeof(name);
55         ZeroMemory(name, sizeof(name));
56         rc = pGetComputerObjectNameA(formats[i], name, &size);
57         ok(rc || ((formats[i] == NameUnknown) &&
58            (GetLastError() == ERROR_INVALID_PARAMETER)) ||
59            (GetLastError() == ERROR_CANT_ACCESS_DOMAIN_INFO) ||
60            (GetLastError() == ERROR_NO_SUCH_DOMAIN) ||
61            (GetLastError() == ERROR_NO_SUCH_USER) ||
62            (GetLastError() == ERROR_NONE_MAPPED) ||
63            (GetLastError() == ERROR_ACCESS_DENIED),
64            "GetComputerObjectNameA(%d) failed: %d\n",
65            formats[i], GetLastError());
66         if (rc)
67             trace("GetComputerObjectNameA() returned %s\n", name);
68     }
69 }
70
71 static void testGetComputerObjectNameW(void)
72 {
73     WCHAR nameW[256];
74     ULONG size;
75     BOOLEAN rc;
76     int i;
77
78     for (i = 0; i < (sizeof(formats) / sizeof(formats[0])); i++) {
79         size = sizeof(nameW)/sizeof(nameW[0]);
80         ZeroMemory(nameW, sizeof(nameW));
81         rc = pGetComputerObjectNameW(formats[i], nameW, &size);
82         ok(rc || ((formats[i] == NameUnknown) &&
83            (GetLastError() == ERROR_INVALID_PARAMETER)) ||
84            (GetLastError() == ERROR_CANT_ACCESS_DOMAIN_INFO) ||
85            (GetLastError() == ERROR_NO_SUCH_DOMAIN) ||
86            (GetLastError() == ERROR_NO_SUCH_USER) ||
87            (GetLastError() == ERROR_NONE_MAPPED) ||
88            (GetLastError() == ERROR_ACCESS_DENIED),
89            "GetComputerObjectNameW(%d) failed: %d\n",
90            formats[i], GetLastError());
91         if (rc) {
92             char name[256];
93             WideCharToMultiByte( CP_ACP, 0, nameW, -1, name, sizeof(name), NULL, NULL );
94             trace("GetComputerObjectNameW() returned %s\n", name);
95         }
96     }
97 }
98
99 static void testGetUserNameExA(void)
100 {
101     char name[256];
102     ULONG size;
103     BOOLEAN rc;
104     int i;
105
106     for (i = 0; i < (sizeof(formats) / sizeof(formats[0])); i++) {
107         size = sizeof(name);
108         ZeroMemory(name, sizeof(name));
109         rc = pGetUserNameExA(formats[i], name, &size);
110         ok(rc || ((formats[i] == NameUnknown) &&
111            (GetLastError() == ERROR_INVALID_PARAMETER)) ||
112            (GetLastError() == ERROR_CANT_ACCESS_DOMAIN_INFO) ||
113            (GetLastError() == ERROR_NO_SUCH_DOMAIN) ||
114            (GetLastError() == ERROR_NO_SUCH_USER) ||
115            (GetLastError() == ERROR_NONE_MAPPED) ||
116            (GetLastError() == ERROR_ACCESS_DENIED),
117            "GetUserNameExA(%d) failed: %d\n",
118            formats[i], GetLastError());
119     }
120
121     size = 0;
122     rc = pGetUserNameExA(NameSamCompatible, NULL, &size);
123     ok(! rc && GetLastError() == ERROR_MORE_DATA, "Expected fail with ERROR_MORE_DATA, got %d with %u\n", rc, GetLastError());
124     ok(size != 0, "Expected size to be set to required size\n");
125     size = 0;
126     rc = pGetUserNameExA(NameSamCompatible, name, &size);
127     ok(! rc && GetLastError() == ERROR_MORE_DATA, "Expected fail with ERROR_MORE_DATA, got %d with %u\n", rc, GetLastError());
128     ok(size != 0, "Expected size to be set to required size\n");
129     size = 1;
130     name[0] = 0xff;
131     rc = pGetUserNameExA(NameSamCompatible, name, &size);
132     ok(! rc && GetLastError() == ERROR_MORE_DATA, "Expected fail with ERROR_MORE_DATA, got %d with %u\n", rc, GetLastError());
133     ok(1 < size, "Expected size to be set to required size\n");
134     ok(name[0] == (char) 0xff, "Expected unchanged buffer\n");
135 }
136
137 static void testGetUserNameExW(void)
138 {
139     WCHAR nameW[256];
140     ULONG size;
141     BOOLEAN rc;
142     int i;
143
144     for (i = 0; i < (sizeof(formats) / sizeof(formats[0])); i++) {
145         size = sizeof(nameW);
146         ZeroMemory(nameW, sizeof(nameW));
147         rc = pGetUserNameExW(formats[i], nameW, &size);
148         ok(rc || ((formats[i] == NameUnknown) &&
149            (GetLastError() == ERROR_INVALID_PARAMETER)) ||
150            (GetLastError() == ERROR_CANT_ACCESS_DOMAIN_INFO) ||
151            (GetLastError() == ERROR_NO_SUCH_DOMAIN) ||
152            (GetLastError() == ERROR_NO_SUCH_USER) ||
153            (GetLastError() == ERROR_NONE_MAPPED) ||
154            (GetLastError() == ERROR_ACCESS_DENIED),
155            "GetUserNameExW(%d) failed: %d\n",
156            formats[i], GetLastError());
157     }
158
159     size = 0;
160     rc = pGetUserNameExW(NameSamCompatible, NULL, &size);
161     ok(! rc && GetLastError() == ERROR_MORE_DATA, "Expected fail with ERROR_MORE_DATA, got %d with %u\n", rc, GetLastError());
162     ok(size != 0, "Expected size to be set to required size\n");
163     size = 0;
164     rc = pGetUserNameExW(NameSamCompatible, nameW, &size);
165     ok(! rc && GetLastError() == ERROR_MORE_DATA, "Expected fail with ERROR_MORE_DATA, got %d with %u\n", rc, GetLastError());
166     ok(size != 0, "Expected size to be set to required size\n");
167     size = 1;
168     nameW[0] = 0xff;
169     rc = pGetUserNameExW(NameSamCompatible, nameW, &size);
170     ok(! rc && GetLastError() == ERROR_MORE_DATA, "Expected fail with ERROR_MORE_DATA, got %d with %u\n", rc, GetLastError());
171     ok(1 < size, "Expected size to be set to required size\n");
172     ok(nameW[0] == (WCHAR) 0xff, "Expected unchanged buffer\n");
173 }
174
175 static void test_InitSecurityInterface(void)
176 {
177     PSecurityFunctionTableA sftA;
178     PSecurityFunctionTableW sftW;
179
180     sftA = pInitSecurityInterfaceA();
181     ok(sftA != NULL, "pInitSecurityInterfaceA failed\n");
182     ok(sftA->dwVersion == SECURITY_SUPPORT_PROVIDER_INTERFACE_VERSION, "wrong dwVersion %d in security function table\n", sftA->dwVersion);
183     ok(!sftA->Reserved2 || broken(sftA->Reserved2 != NULL) /* WinME */,
184        "Reserved2 should be NULL instead of %p in security function table\n",
185        sftA->Reserved2);
186     ok(sftA->Reserved3 == sftA->EncryptMessage ||
187        broken(sftA->Reserved3 != sftA->EncryptMessage) /* Win9x */,
188        "Reserved3 should be equal to EncryptMessage in the security function table\n");
189     ok(sftA->Reserved4 == sftA->DecryptMessage ||
190        broken(sftA->Reserved4 != sftA->DecryptMessage) /* Win9x */,
191        "Reserved4 should be equal to DecryptMessage in the security function table\n");
192
193     if (!pInitSecurityInterfaceW)
194     {
195         skip("InitSecurityInterfaceW not exported by secur32.dll\n");
196         return;
197     }
198
199     sftW = pInitSecurityInterfaceW();
200     ok(sftW != NULL, "pInitSecurityInterfaceW failed\n");
201     ok(sftW->dwVersion == SECURITY_SUPPORT_PROVIDER_INTERFACE_VERSION, "wrong dwVersion %d in security function table\n", sftW->dwVersion);
202     ok(!sftW->Reserved2, "Reserved2 should be NULL instead of %p in security function table\n", sftW->Reserved2);
203     ok(sftW->Reserved3 == sftW->EncryptMessage, "Reserved3 should be equal to EncryptMessage in the security function table\n");
204     ok(sftW->Reserved4 == sftW->DecryptMessage, "Reserved4 should be equal to DecryptMessage in the security function table\n");
205 }
206
207 START_TEST(secur32)
208 {
209     secdll = LoadLibraryA("secur32.dll");
210
211     if (!secdll)
212         secdll = LoadLibraryA("security.dll");
213
214     if (secdll)
215     {
216         pGetComputerObjectNameA = (PVOID)GetProcAddress(secdll, "GetComputerObjectNameA");
217         pGetComputerObjectNameW = (PVOID)GetProcAddress(secdll, "GetComputerObjectNameW");
218         pGetUserNameExA = (PVOID)GetProcAddress(secdll, "GetUserNameExA");
219         pGetUserNameExW = (PVOID)GetProcAddress(secdll, "GetUserNameExW");
220         pInitSecurityInterfaceA = (PVOID)GetProcAddress(secdll, "InitSecurityInterfaceA");
221         pInitSecurityInterfaceW = (PVOID)GetProcAddress(secdll, "InitSecurityInterfaceW");
222  
223         if (pGetComputerObjectNameA)
224             testGetComputerObjectNameA();
225
226         if (pGetComputerObjectNameW)
227             testGetComputerObjectNameW();
228
229         if (pGetUserNameExA)
230             testGetUserNameExA();
231
232         if (pGetUserNameExW)
233             testGetUserNameExW();
234
235         test_InitSecurityInterface();
236
237         FreeLibrary(secdll);
238     }
239 }