secur32: Check for actual return value on invalid QuerySecurityPackageInfo call.
[wine] / dlls / secur32 / tests / main.c
1 /*
2  * Miscellaneous secur32 tests
3  *
4  * Copyright 2005, 2006 Kai Blin
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
21 #define SECURITY_WIN32
22
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <assert.h>
26 #include <windef.h>
27 #include <winbase.h>
28 #include <sspi.h>
29
30 #include "wine/test.h"
31
32 static HMODULE secdll;
33 static PSecurityFunctionTableA (SEC_ENTRY * pInitSecurityInterfaceA)(void);
34 static SECURITY_STATUS (SEC_ENTRY * pEnumerateSecurityPackagesA)(PULONG, PSecPkgInfoA*);
35 static SECURITY_STATUS (SEC_ENTRY * pFreeContextBuffer)(PVOID pv);
36 static SECURITY_STATUS (SEC_ENTRY * pQuerySecurityPackageInfoA)(SEC_CHAR*, PSecPkgInfoA*);
37 static SECURITY_STATUS (SEC_ENTRY * pAcquireCredentialsHandleA)(SEC_CHAR*, SEC_CHAR*,
38                             ULONG, PLUID, PVOID, SEC_GET_KEY_FN, PVOID, PCredHandle, PTimeStamp);
39 static SECURITY_STATUS (SEC_ENTRY * pInitializeSecurityContextA)(PCredHandle, PCtxtHandle,
40                             SEC_CHAR*, ULONG, ULONG, ULONG, PSecBufferDesc, ULONG, 
41                             PCtxtHandle, PSecBufferDesc, PULONG, PTimeStamp);
42 static SECURITY_STATUS (SEC_ENTRY * pCompleteAuthToken)(PCtxtHandle, PSecBufferDesc);
43 static SECURITY_STATUS (SEC_ENTRY * pAcceptSecurityContext)(PCredHandle, PCtxtHandle,
44                             PSecBufferDesc, ULONG, ULONG, PCtxtHandle, PSecBufferDesc,
45                             PULONG, PTimeStamp);
46 static SECURITY_STATUS (SEC_ENTRY * pFreeCredentialsHandle)(PCredHandle);
47 static SECURITY_STATUS (SEC_ENTRY * pDeleteSecurityContext)(PCtxtHandle);
48 static SECURITY_STATUS (SEC_ENTRY * pQueryContextAttributesA)(PCtxtHandle, ULONG, PVOID);
49
50 static void InitFunctionPtrs(void)
51 {
52     secdll = LoadLibraryA("secur32.dll");
53     if(!secdll)
54         secdll = LoadLibraryA("security.dll");
55     if(secdll)
56     {
57         pInitSecurityInterfaceA = (PVOID)GetProcAddress(secdll, "InitSecurityInterfaceA");
58         pEnumerateSecurityPackagesA = (PVOID)GetProcAddress(secdll, "EnumerateSecurityPackagesA");
59         pFreeContextBuffer = (PVOID)GetProcAddress(secdll, "FreeContextBuffer");
60         pQuerySecurityPackageInfoA = (PVOID)GetProcAddress(secdll, "QuerySecurityPackageInfoA");
61         pAcquireCredentialsHandleA = (PVOID)GetProcAddress(secdll, "AcquireCredentialsHandleA");
62         pInitializeSecurityContextA = (PVOID)GetProcAddress(secdll, "InitializeSecurityContextA");
63         pCompleteAuthToken = (PVOID)GetProcAddress(secdll, "CompleteAuthToken");
64         pAcceptSecurityContext = (PVOID)GetProcAddress(secdll, "AcceptSecurityContext");
65         pFreeCredentialsHandle = (PVOID)GetProcAddress(secdll, "FreeCredentialsHandle");
66         pDeleteSecurityContext = (PVOID)GetProcAddress(secdll, "DeleteSecurityContext");
67         pQueryContextAttributesA = (PVOID)GetProcAddress(secdll, "QueryContextAttributesA");
68     }
69 }
70
71 /*---------------------------------------------------------*/
72 /* General helper functions */
73
74 static const char* getSecError(SECURITY_STATUS status)
75 {
76     static char buf[20];
77
78 #define _SEC_ERR(x) case (x): return #x;
79     switch(status)
80     {
81         _SEC_ERR(SEC_E_OK);
82         _SEC_ERR(SEC_E_INSUFFICIENT_MEMORY);
83         _SEC_ERR(SEC_E_INVALID_HANDLE);
84         _SEC_ERR(SEC_E_UNSUPPORTED_FUNCTION);
85         _SEC_ERR(SEC_E_TARGET_UNKNOWN);
86         _SEC_ERR(SEC_E_INTERNAL_ERROR);
87         _SEC_ERR(SEC_E_SECPKG_NOT_FOUND);
88         _SEC_ERR(SEC_E_NOT_OWNER);
89         _SEC_ERR(SEC_E_CANNOT_INSTALL);
90         _SEC_ERR(SEC_E_INVALID_TOKEN);
91         _SEC_ERR(SEC_E_CANNOT_PACK);
92         _SEC_ERR(SEC_E_QOP_NOT_SUPPORTED);
93         _SEC_ERR(SEC_E_NO_IMPERSONATION);
94         _SEC_ERR(SEC_I_CONTINUE_NEEDED);
95         _SEC_ERR(SEC_E_BUFFER_TOO_SMALL);
96         _SEC_ERR(SEC_E_ILLEGAL_MESSAGE);
97         _SEC_ERR(SEC_E_LOGON_DENIED);
98         _SEC_ERR(SEC_E_NO_CREDENTIALS);
99         _SEC_ERR(SEC_E_OUT_OF_SEQUENCE);
100         default:
101             sprintf(buf, "%08x\n", status);
102             return buf;
103     }
104 #undef _SEC_ERR
105 }
106
107 /*---------------------------------------------------------*/
108 /* Helper for testQuerySecurityPagageInfo */
109
110 static SECURITY_STATUS setupPackageA(SEC_CHAR *p_package_name, 
111         PSecPkgInfo *p_pkg_info)
112 {
113     SECURITY_STATUS ret;
114     
115     ret = pQuerySecurityPackageInfoA( p_package_name, p_pkg_info);
116     return ret;
117 }
118
119 /*--------------------------------------------------------- */
120 /* The test functions */
121
122 static void testInitSecurityInterface(void)
123 {
124     PSecurityFunctionTable sec_fun_table = NULL;
125
126     sec_fun_table = pInitSecurityInterfaceA();
127     ok(sec_fun_table != NULL, "InitSecurityInterface() returned NULL.\n");
128
129 }
130
131 static void testEnumerateSecurityPackages(void)
132 {
133
134     SECURITY_STATUS sec_status;
135     ULONG           num_packages, i;
136     PSecPkgInfo     pkg_info = NULL;
137
138     trace("Running testEnumerateSecurityPackages\n");
139     
140     sec_status = pEnumerateSecurityPackagesA(&num_packages, &pkg_info);
141
142     ok(sec_status == SEC_E_OK, 
143             "EnumerateSecurityPackages() should return %d, not %08x\n",
144             SEC_E_OK, sec_status);
145
146     ok(num_packages > 0, "Number of sec packages should be > 0 ,but is %d\n",
147             num_packages);
148
149     ok(pkg_info != NULL, 
150             "pkg_info should not be NULL after EnumerateSecurityPackages\n");
151     
152     trace("Number of packages: %d\n", num_packages);
153     for(i = 0; i < num_packages; ++i){
154         trace("%d: Package \"%s\"\n", i, pkg_info[i].Name);
155         trace("Supported flags:\n");
156         if(pkg_info[i].fCapabilities & SECPKG_FLAG_INTEGRITY)
157             trace("\tSECPKG_FLAG_INTEGRITY\n");
158         if(pkg_info[i].fCapabilities & SECPKG_FLAG_PRIVACY)
159             trace("\tSECPKG_FLAG_PRIVACY\n");
160         if(pkg_info[i].fCapabilities & SECPKG_FLAG_TOKEN_ONLY)
161             trace("\tSECPKG_FLAG_TOKEN_ONLY\n");
162         if(pkg_info[i].fCapabilities & SECPKG_FLAG_DATAGRAM)
163             trace("\tSECPKG_FLAG_DATAGRAM\n");
164         if(pkg_info[i].fCapabilities & SECPKG_FLAG_CONNECTION)
165             trace("\tSECPKG_FLAG_CONNECTION\n");
166         if(pkg_info[i].fCapabilities & SECPKG_FLAG_MULTI_REQUIRED)
167             trace("\tSECPKG_FLAG_MULTI_REQUIRED\n");
168         if(pkg_info[i].fCapabilities & SECPKG_FLAG_CLIENT_ONLY)
169             trace("\tSECPKG_FLAG_CLIENT_ONLY\n");
170         if(pkg_info[i].fCapabilities & SECPKG_FLAG_EXTENDED_ERROR)
171             trace("\tSECPKG_FLAG_EXTENDED_ERROR\n");
172         if(pkg_info[i].fCapabilities & SECPKG_FLAG_IMPERSONATION)
173             trace("\tSECPKG_FLAG_IMPERSONATION\n");
174         if(pkg_info[i].fCapabilities & SECPKG_FLAG_ACCEPT_WIN32_NAME)
175             trace("\tSECPKG_FLAG_ACCEPT_WIN32_NAME\n");
176         if(pkg_info[i].fCapabilities & SECPKG_FLAG_STREAM)
177             trace("\tSECPKG_FLAG_STREAM\n");
178         if(pkg_info[i].fCapabilities & SECPKG_FLAG_READONLY_WITH_CHECKSUM)
179             trace("\tSECPKG_FLAG_READONLY_WITH_CHECKSUM\n");
180         trace("Comment: %s\n", pkg_info[i].Comment);
181         trace("\n");
182     }
183
184     pFreeContextBuffer(pkg_info);
185 }
186
187
188 static void testQuerySecurityPackageInfo(void)
189 {
190     SECURITY_STATUS     sec_status;
191     PSecPkgInfo         pkg_info;
192     static SEC_CHAR     ntlm[]     = "NTLM",
193                         winetest[] = "Winetest";
194
195     trace("Running testQuerySecurityPackageInfo\n");
196
197     /* Test with an existing package. Test should pass */
198
199     pkg_info = (void *)0xdeadbeef;
200     sec_status = setupPackageA(ntlm, &pkg_info);
201
202     ok((sec_status == SEC_E_OK) || (sec_status == SEC_E_SECPKG_NOT_FOUND), 
203        "Return value of QuerySecurityPackageInfo() shouldn't be %s\n",
204        getSecError(sec_status) );
205
206     if (sec_status == SEC_E_OK)
207     {
208         ok(pkg_info != (void *)0xdeadbeef, "wrong pkg_info address %p\n", pkg_info);
209         ok(pkg_info->wVersion == 1, "wVersion always should be 1, but is %d\n", pkg_info->wVersion);
210         /* there is no point in testing pkg_info->cbMaxToken since it varies
211          * between implementations.
212          */
213     }
214
215     sec_status = pFreeContextBuffer(pkg_info);
216
217     ok( sec_status == SEC_E_OK,
218         "Return value of FreeContextBuffer() shouldn't be %s\n",
219         getSecError(sec_status) );
220
221     /* Test with a nonexistent package, test should fail */
222
223     pkg_info = (void *)0xdeadbeef;
224     sec_status = pQuerySecurityPackageInfoA(winetest, &pkg_info);
225
226     ok( sec_status == SEC_E_SECPKG_NOT_FOUND,
227         "Return value of QuerySecurityPackageInfo() should be %s for a nonexistent package\n",
228         getSecError(SEC_E_SECPKG_NOT_FOUND));
229
230     ok(pkg_info == (void *)0xdeadbeef, "wrong pkg_info address %p\n", pkg_info);
231
232     sec_status = pFreeContextBuffer(pkg_info);
233
234     ok( sec_status == SEC_E_OK,
235         "Return value of FreeContextBuffer() shouldn't be %s\n",
236         getSecError(sec_status) );
237 }
238
239 START_TEST(main)
240 {
241     InitFunctionPtrs();
242     if(pInitSecurityInterfaceA)
243         testInitSecurityInterface();
244     if(pFreeContextBuffer)
245     {
246         if(pEnumerateSecurityPackagesA)
247             testEnumerateSecurityPackages();
248         if(pQuerySecurityPackageInfoA)
249         {
250             testQuerySecurityPackageInfo();
251         }
252     }
253     if(secdll)
254         FreeLibrary(secdll);
255 }