schannel: Fix a failing test in Vista.
[wine] / dlls / schannel / tests / main.c
1 /*
2  * Schannel tests
3  *
4  * Copyright 2006 Yuval Fledel
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 #include <stdio.h>
22 #include <stdarg.h>
23 #include "ntstatus.h"
24 #define WIN32_NO_STATUS
25 #include <windef.h>
26 #include <winbase.h>
27 #define SECURITY_WIN32
28 #include <security.h>
29 #include <schannel.h>
30 #include <ntsecapi.h>
31 #include <ntsecpkg.h>
32
33 #include "wine/test.h"
34
35 /* Helper macros to find the size of SECPKG_FUNCTION_TABLE */
36 #define SECPKG_FUNCTION_TABLE_SIZE_1 FIELD_OFFSET(SECPKG_FUNCTION_TABLE, \
37     SetContextAttributes)
38 #define SECPKG_FUNCTION_TABLE_SIZE_2 FIELD_OFFSET(SECPKG_FUNCTION_TABLE, \
39     SetCredentialsAttributes)
40 #define SECPKG_FUNCTION_TABLE_SIZE_3 sizeof(SECPKG_FUNCTION_TABLE)
41
42 static NTSTATUS (NTAPI *pSpLsaModeInitialize)(ULONG, PULONG,
43     PSECPKG_FUNCTION_TABLE*, PULONG);
44 static NTSTATUS (NTAPI *pSpUserModeInitialize)(ULONG, PULONG,
45     PSECPKG_USER_FUNCTION_TABLE*, PULONG);
46
47 static void testInitialize(void)
48 {
49     PSECPKG_USER_FUNCTION_TABLE pUserTables, pUserTables2;
50     PSECPKG_FUNCTION_TABLE pTables, pTables2;
51     ULONG cTables = 0, cUserTables = 0, Version = 0;
52     NTSTATUS status;
53
54     /* Passing NULL into one of the parameters of SpLsaModeInitialize or
55        SpUserModeInitialize causes a crash. */
56
57     /* SpLsaModeInitialize does not care about the LSA version. */
58     status = pSpLsaModeInitialize(0, &Version, &pTables2, &cTables);
59     ok(status == STATUS_SUCCESS, "status: 0x%x\n", status);
60     ok(cTables == 2, "cTables: %d\n", cTables);
61     ok(pTables2 != NULL,"pTables: %p\n", pTables2);
62
63     /* We can call it as many times we want. */
64     status = pSpLsaModeInitialize(0x10000, &Version, &pTables, &cTables);
65     ok(status == STATUS_SUCCESS, "status: 0x%x\n", status);
66     ok(cTables == 2, "cTables: %d\n", cTables);
67     ok(pTables != NULL, "pTables: %p\n", pTables);
68     /* It will always return the same pointer. */
69     ok(pTables == pTables2, "pTables: %p, pTables2: %p\n", pTables, pTables2);
70
71     status = pSpLsaModeInitialize(0x23456, &Version, &pTables, &cTables);
72     ok(status == STATUS_SUCCESS, "status: 0x%x\n", status);
73     ok(cTables == 2, "cTables: %d\n", cTables);
74     ok(pTables != NULL, "pTables: %p\n", pTables);
75     ok(pTables == pTables2, "pTables: %p, pTables2: %p\n", pTables, pTables2);
76
77     /* Bad versions to SpUserModeInitialize. Parameters unchanged */
78     Version = 0xdead;
79     cUserTables = 0xdead;
80     pUserTables = NULL;
81     status = pSpUserModeInitialize(0, &Version, &pUserTables, &cUserTables);
82     ok(status == STATUS_INVALID_PARAMETER, "status: 0x%x\n", status);
83     ok(Version == 0xdead, "Version: 0x%x\n", Version);
84     ok(cUserTables == 0xdead, "cTables: %d\n", cUserTables);
85     ok(pUserTables == NULL, "pUserTables: %p\n", pUserTables);
86
87     status = pSpUserModeInitialize(0x20000, &Version, &pUserTables,
88                                    &cUserTables);
89     ok(status == STATUS_INVALID_PARAMETER, "status: 0x%x\n", status);
90     ok(Version == 0xdead, "Version: 0x%x\n", Version);
91     ok(cUserTables == 0xdead, "cTables: %d\n", cUserTables);
92     ok(pUserTables == NULL, "pUserTables: %p\n", pUserTables);
93
94     /* Good version to SpUserModeInitialize */
95     status = pSpUserModeInitialize(SECPKG_INTERFACE_VERSION, &Version,
96                                    &pUserTables, &cUserTables);
97     ok(status == STATUS_SUCCESS, "status: 0x%x\n", status);
98     ok(Version == SECPKG_INTERFACE_VERSION, "Version: 0x%x\n", Version);
99     ok(cUserTables == 2, "cUserTables: %d\n", cUserTables);
100     ok(pUserTables != NULL, "pUserTables: %p\n", pUserTables);
101
102     /* Initializing user again */
103     status = pSpUserModeInitialize(SECPKG_INTERFACE_VERSION, &Version,
104                                    &pUserTables2, &cTables);
105     ok(status == STATUS_SUCCESS, "status: 0x%x\n", status);
106     ok(pUserTables == pUserTables2, "pUserTables: %p, pUserTables2: %p\n",
107        pUserTables, pUserTables2);
108 }
109
110 /* A helper function to find the dispatch table of the next package.
111    Needed because SECPKG_FUNCTION_TABLE's size depend on the version */
112 static PSECPKG_FUNCTION_TABLE getNextSecPkgTable(PSECPKG_FUNCTION_TABLE pTable,
113                                                  ULONG Version)
114 {
115     size_t size;
116
117     if (Version == SECPKG_INTERFACE_VERSION)
118         size = SECPKG_FUNCTION_TABLE_SIZE_1;
119     else if (Version == SECPKG_INTERFACE_VERSION_2)
120         size = SECPKG_FUNCTION_TABLE_SIZE_2;
121     else if (Version == SECPKG_INTERFACE_VERSION_3)
122         size = SECPKG_FUNCTION_TABLE_SIZE_3;
123     else {
124         ok(FALSE, "Unknown package version 0x%x\n", Version);
125         return NULL;
126     }
127
128     return (PSECPKG_FUNCTION_TABLE)((PBYTE)pTable + size);
129 }
130
131 static void testGetInfo(void)
132 {
133     PSECPKG_FUNCTION_TABLE pTables;
134     SecPkgInfoW PackageInfo;
135     ULONG cTables, Version;
136     NTSTATUS status;
137
138     /* Get the dispatch table */
139     status = pSpLsaModeInitialize(0, &Version, &pTables, &cTables);
140     ok(status == STATUS_SUCCESS, "status: 0x%x\n", status);
141
142     /* Passing NULL into ->GetInfo causes a crash. */
143
144     /* First package: Unified */
145     status = pTables->GetInfo(&PackageInfo);
146     ok(status == STATUS_SUCCESS, "status: 0x%x\n", status);
147     ok(PackageInfo.fCapabilities == 0x107b3, "fCapabilities: 0x%lx\n",
148        PackageInfo.fCapabilities);
149     ok(PackageInfo.wVersion == 1, "wVersion: %d\n", PackageInfo.wVersion);
150     ok(PackageInfo.wRPCID == 14, "wRPCID: %d\n", PackageInfo.wRPCID);
151     ok(PackageInfo.cbMaxToken == 0x4000 ||
152        PackageInfo.cbMaxToken == 0x6000, /* Vista */
153        "cbMaxToken: 0x%lx\n",
154        PackageInfo.cbMaxToken);
155
156     /* Second package: SChannel */
157     pTables = getNextSecPkgTable(pTables, Version);
158     if (!pTables)
159         return;
160     status = pTables->GetInfo(&PackageInfo);
161     ok(status == STATUS_SUCCESS ||
162        status == SEC_E_UNSUPPORTED_FUNCTION, /* win2k3 */
163        "status: 0x%x\n", status);
164
165     if (status == STATUS_SUCCESS)
166     {
167         ok(PackageInfo.fCapabilities == 0x107b3, "fCapabilities: 0x%lx\n",
168            PackageInfo.fCapabilities);
169         ok(PackageInfo.wVersion == 1, "wVersion: %d\n", PackageInfo.wVersion);
170         ok(PackageInfo.wRPCID == 14, "wRPCID: %d\n", PackageInfo.wRPCID);
171         ok(PackageInfo.cbMaxToken == 0x4000, "cbMaxToken: 0x%lx\n",
172            PackageInfo.cbMaxToken);
173     }
174 }
175
176 START_TEST(main)
177 {
178     HMODULE hMod = LoadLibraryA("schannel.dll");
179     if (!hMod) {
180         skip("schannel.dll not found.\n");
181         return;
182     }
183
184     pSpLsaModeInitialize  = (void *)GetProcAddress(hMod, "SpLsaModeInitialize");
185     pSpUserModeInitialize = (void *)GetProcAddress(hMod, "SpUserModeInitialize");
186
187     if (pSpLsaModeInitialize && pSpUserModeInitialize)
188     {
189         testInitialize();
190         testGetInfo();
191     }
192     else skip( "schannel functions not found\n" );
193
194     FreeLibrary(hMod);
195 }