kernel32: FindFirstChangeNotification needs a static IO_STATUS_BLOCK.
[wine] / dlls / crypt32 / tests / str.c
1 /*
2  * Unit test suite for crypt32.dll's Cert*ToStr and CertStrToName functions.
3  *
4  * Copyright 2006 Juan Lang, Aric Stewart for Codeweavers
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 #include <stdio.h>
21 #include <stdarg.h>
22 #include <windef.h>
23 #include <winbase.h>
24 #include <winerror.h>
25 #include <wincrypt.h>
26
27 #include "wine/test.h"
28 #include "wine/debug.h"
29
30 typedef struct _CertRDNAttrEncoding {
31     LPCSTR pszObjId;
32     DWORD  dwValueType;
33     CERT_RDN_VALUE_BLOB Value;
34     LPCSTR str;
35 } CertRDNAttrEncoding, *PCertRDNAttrEncoding;
36
37 typedef struct _CertRDNAttrEncodingW {
38     LPCSTR pszObjId;
39     DWORD  dwValueType;
40     CERT_RDN_VALUE_BLOB Value;
41     LPCWSTR str;
42 } CertRDNAttrEncodingW, *PCertRDNAttrEncodingW;
43
44 static const BYTE bin1[] = { 0x55, 0x53 };
45 static const BYTE bin2[] = { 0x4d, 0x69, 0x6e, 0x6e, 0x65, 0x73, 0x6f, 0x74,
46  0x61 };
47 static const BYTE bin3[] = { 0x4d, 0x69, 0x6e, 0x6e, 0x65, 0x61, 0x70, 0x6f,
48  0x6c, 0x69, 0x73 };
49 static const BYTE bin4[] = { 0x43, 0x6f, 0x64, 0x65, 0x57, 0x65, 0x61, 0x76,
50  0x65, 0x72, 0x73 };
51 static const BYTE bin5[] = { 0x57, 0x69, 0x6e, 0x65, 0x20, 0x44, 0x65, 0x76,
52  0x65, 0x6c, 0x6f, 0x70, 0x6d, 0x65, 0x6e, 0x74 };
53 static const BYTE bin6[] = { 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73,
54  0x74 };
55 static const BYTE bin7[] = { 0x61, 0x72, 0x69, 0x63, 0x40, 0x63, 0x6f, 0x64,
56  0x65, 0x77, 0x65, 0x61, 0x76, 0x65, 0x72, 0x73, 0x2e, 0x63, 0x6f, 0x6d };
57
58 static const BYTE cert[] = 
59 {0x30,0x82,0x2,0xbb,0x30,0x82,0x2,0x24,0x2,0x9,0x0,0xe3,0x5a,0x10,0xf1,0xfc,
60  0x4b,0xf3,0xa2,0x30,0xd,0x6,0x9,0x2a,0x86,0x48,0x86,0xf7,0xd,0x1,0x1,0x4,0x5,
61  0x0,0x30,0x81,0xa1,0x31,0xb,0x30,0x9,0x6,0x3,0x55,0x4,0x6,0x13,0x2,0x55,0x53,
62  0x31,0x12,0x30,0x10,0x6,0x3,0x55,0x4,0x8,0x13,0x9,0x4d,0x69,0x6e,0x6e,0x65,
63  0x73,0x6f,0x74,0x61,0x31,0x14,0x30,0x12,0x6,0x3,0x55,0x4,0x7,0x13,0xb,0x4d,
64  0x69,0x6e,0x6e,0x65,0x61,0x70,0x6f,0x6c,0x69,0x73,0x31,0x14,0x30,0x12,0x6,0x3,
65  0x55,0x4,0xa,0x13,0xb,0x43,0x6f,0x64,0x65,0x57,0x65,0x61,0x76,0x65,0x72,0x73,
66  0x31,0x19,0x30,0x17,0x6,0x3,0x55,0x4,0xb,0x13,0x10,0x57,0x69,0x6e,0x65,0x20,
67  0x44,0x65,0x76,0x65,0x6c,0x6f,0x70,0x6d,0x65,0x6e,0x74,0x31,0x12,0x30,0x10,
68  0x6,0x3,0x55,0x4,0x3,0x13,0x9,0x6c,0x6f,0x63,0x61,0x6c,0x68,0x6f,0x73,0x74,
69  0x31,0x23,0x30,0x21,0x6,0x9,0x2a,0x86,0x48,0x86,0xf7,0xd,0x1,0x9,0x1,0x16,
70  0x14,0x61,0x72,0x69,0x63,0x40,0x63,0x6f,0x64,0x65,0x77,0x65,0x61,0x76,0x65,
71  0x72,0x73,0x2e,0x63,0x6f,0x6d,0x30,0x1e,0x17,0xd,0x30,0x36,0x30,0x31,0x32,
72  0x35,0x31,0x33,0x35,0x37,0x32,0x34,0x5a,0x17,0xd,0x30,0x36,0x30,0x32,0x32,
73  0x34,0x31,0x33,0x35,0x37,0x32,0x34,0x5a,0x30,0x81,0xa1,0x31,0xb,0x30,0x9,0x6,
74  0x3,0x55,0x4,0x6,0x13,0x2,0x55,0x53,0x31,0x12,0x30,0x10,0x6,0x3,0x55,0x4,0x8,
75  0x13,0x9,0x4d,0x69,0x6e,0x6e,0x65,0x73,0x6f,0x74,0x61,0x31,0x14,0x30,0x12,0x6,
76  0x3,0x55,0x4,0x7,0x13,0xb,0x4d,0x69,0x6e,0x6e,0x65,0x61,0x70,0x6f,0x6c,0x69,
77  0x73,0x31,0x14,0x30,0x12,0x6,0x3,0x55,0x4,0xa,0x13,0xb,0x43,0x6f,0x64,0x65,
78  0x57,0x65,0x61,0x76,0x65,0x72,0x73,0x31,0x19,0x30,0x17,0x6,0x3,0x55,0x4,0xb,
79  0x13,0x10,0x57,0x69,0x6e,0x65,0x20,0x44,0x65,0x76,0x65,0x6c,0x6f,0x70,0x6d,
80  0x65,0x6e,0x74,0x31,0x12,0x30,0x10,0x6,0x3,0x55,0x4,0x3,0x13,0x9,0x6c,0x6f,
81  0x63,0x61,0x6c,0x68,0x6f,0x73,0x74,0x31,0x23,0x30,0x21,0x6,0x9,0x2a,0x86,0x48,
82  0x86,0xf7,0xd,0x1,0x9,0x1,0x16,0x14,0x61,0x72,0x69,0x63,0x40,0x63,0x6f,0x64,
83  0x65,0x77,0x65,0x61,0x76,0x65,0x72,0x73,0x2e,0x63,0x6f,0x6d,0x30,0x81,0x9f,
84  0x30,0xd,0x6,0x9,0x2a,0x86,0x48,0x86,0xf7,0xd,0x1,0x1,0x1,0x5,0x0,0x3,0x81,
85  0x8d,0x0,0x30,0x81,0x89,0x2,0x81,0x81,0x0,0x9b,0xb5,0x8f,0xaf,0xfb,0x9a,0xaf,
86  0xdc,0xa2,0x4d,0xb1,0xc8,0x72,0x44,0xef,0x79,0x7f,0x28,0xb6,0xfe,0x50,0xdc,
87  0x8a,0xf7,0x11,0x2f,0x90,0x70,0xed,0xa4,0xa9,0xd,0xbf,0x82,0x3e,0x56,0xd8,
88  0x36,0xb6,0x9,0x52,0x83,0xab,0x65,0x95,0x0,0xe2,0xea,0x3c,0x4f,0x85,0xb8,0xc,
89  0x41,0x42,0x77,0x5c,0x9d,0x44,0xeb,0xcf,0x7d,0x60,0x64,0x7a,0x6c,0x4c,0xac,
90  0x4a,0x9a,0x23,0x25,0x15,0xd7,0x92,0xb4,0x10,0xe7,0x95,0xad,0x4b,0x93,0xda,
91  0x6a,0x76,0xe0,0xa5,0xd2,0x13,0x8,0x12,0x30,0x68,0xde,0xb9,0x5b,0x6e,0x2a,
92  0x97,0x43,0xaa,0x7b,0x22,0x33,0x34,0xb1,0xca,0x5d,0x19,0xd8,0x42,0x26,0x45,
93  0xc6,0xe9,0x1d,0xee,0x7,0xc2,0x27,0x95,0x87,0xd8,0x12,0xec,0x4b,0x16,0x9f,0x2,
94  0x3,0x1,0x0,0x1,0x30,0xd,0x6,0x9,0x2a,0x86,0x48,0x86,0xf7,0xd,0x1,0x1,0x4,0x5,
95  0x0,0x3,0x81,0x81,0x0,0x96,0xf9,0xf6,0x6a,0x3d,0xd9,0xca,0x6e,0xd5,0x76,0x73,
96  0xab,0x75,0xc1,0xcc,0x98,0x44,0xc3,0xa9,0x90,0x68,0x88,0x76,0xb9,0xeb,0xb6,
97  0xbe,0x60,0x62,0xb9,0x67,0x1e,0xcc,0xf4,0xe1,0xe7,0x6c,0xc8,0x67,0x3f,0x1d,
98  0xf3,0x68,0x86,0x30,0xee,0xaa,0x92,0x61,0x37,0xd7,0x82,0x90,0x28,0xaa,0x7a,
99  0x18,0x88,0x60,0x14,0x88,0x75,0xc0,0x4a,0x4e,0x7d,0x48,0xe7,0x3,0xa6,0xfd,
100  0xd7,0xce,0x3c,0xe5,0x9b,0xaf,0x2f,0xdc,0xbb,0x7c,0xbd,0x20,0x49,0xd9,0x68,
101  0x37,0xeb,0x5d,0xbb,0xe2,0x6d,0x66,0xe3,0x11,0xc1,0xa7,0x88,0x49,0xc6,0x6f,
102  0x65,0xd3,0xce,0xae,0x26,0x19,0x3,0x2e,0x4f,0x78,0xa5,0xa,0x97,0x7e,0x4f,0xc4,
103  0x91,0x8a,0xf8,0x5,0xef,0x5b,0x3b,0x49,0xbf,0x5f,0x2b};
104
105 static char issuerStr[] =
106  "US, Minnesota, Minneapolis, CodeWeavers, Wine Development, localhost, aric@codeweavers.com";
107 static char issuerStrSemicolon[] =
108  "US; Minnesota; Minneapolis; CodeWeavers; Wine Development; localhost; aric@codeweavers.com";
109 static char issuerStrCRLF[] =
110  "US\r\nMinnesota\r\nMinneapolis\r\nCodeWeavers\r\nWine Development\r\nlocalhost\r\naric@codeweavers.com";
111 static char subjectStr[] =
112  "2.5.4.6=US, 2.5.4.8=Minnesota, 2.5.4.7=Minneapolis, 2.5.4.10=CodeWeavers, 2.5.4.11=Wine Development, 2.5.4.3=localhost, 1.2.840.113549.1.9.1=aric@codeweavers.com";
113 static char subjectStrSemicolon[] =
114  "2.5.4.6=US; 2.5.4.8=Minnesota; 2.5.4.7=Minneapolis; 2.5.4.10=CodeWeavers; 2.5.4.11=Wine Development; 2.5.4.3=localhost; 1.2.840.113549.1.9.1=aric@codeweavers.com";
115 static char subjectStrCRLF[] =
116  "2.5.4.6=US\r\n2.5.4.8=Minnesota\r\n2.5.4.7=Minneapolis\r\n2.5.4.10=CodeWeavers\r\n2.5.4.11=Wine Development\r\n2.5.4.3=localhost\r\n1.2.840.113549.1.9.1=aric@codeweavers.com";
117 static WCHAR issuerStrW[] = {
118  'U','S',',',' ','M','i','n','n','e','s','o','t','a',',',' ','M','i','n','n',
119  'e','a','p','o','l','i','s',',',' ','C','o','d','e','W','e','a','v','e','r',
120  's',',',' ','W','i','n','e',' ','D','e','v','e','l','o','p','m','e','n','t',
121  ',',' ','l','o','c','a','l','h','o','s','t',',',' ','a','r','i','c','@','c',
122  'o','d','e','w','e','a','v','e','r','s','.','c','o','m',0 };
123 static WCHAR issuerStrSemicolonW[] = {
124  'U','S',';',' ','M','i','n','n','e','s','o','t','a',';',' ','M','i','n','n',
125  'e','a','p','o','l','i','s',';',' ','C','o','d','e','W','e','a','v','e','r',
126  's',';',' ','W','i','n','e',' ','D','e','v','e','l','o','p','m','e','n','t',
127  ';',' ','l','o','c','a','l','h','o','s','t',';',' ','a','r','i','c','@','c',
128  'o','d','e','w','e','a','v','e','r','s','.','c','o','m',0 };
129 static WCHAR issuerStrCRLFW[] = {
130  'U','S','\r','\n','M','i','n','n','e','s','o','t','a','\r','\n','M','i','n',
131  'n','e','a','p','o','l','i','s','\r','\n','C','o','d','e','W','e','a','v','e',
132  'r','s','\r','\n','W','i','n','e',' ','D','e','v','e','l','o','p','m','e','n',
133  't','\r','\n','l','o','c','a','l','h','o','s','t','\r','\n','a','r','i','c',
134  '@','c','o','d','e','w','e','a','v','e','r','s','.','c','o','m',0 };
135 static WCHAR subjectStrW[] = {
136  '2','.','5','.','4','.','6','=','U','S',',',' ','2','.','5','.','4','.','8',
137  '=','M','i','n','n','e','s','o','t','a',',',' ','2','.','5','.','4','.','7',
138  '=','M','i','n','n','e','a','p','o','l','i','s',',',' ','2','.','5','.','4',
139  '.','1','0','=','C','o','d','e','W','e','a','v','e','r','s',',',' ','2','.',
140  '5','.','4','.','1','1','=','W','i','n','e',' ','D','e','v','e','l','o','p',
141  'm','e','n','t',',',' ','2','.','5','.','4','.','3','=','l','o','c','a','l',
142  'h','o','s','t',',',' ','1','.','2','.','8','4','0','.','1','1','3','5','4',
143  '9','.','1','.','9','.','1','=','a','r','i','c','@','c','o','d','e','w','e',
144  'a','v','e','r','s','.','c','o','m',0 };
145 static WCHAR subjectStrSemicolonW[] = {
146  '2','.','5','.','4','.','6','=','U','S',';',' ','2','.','5','.','4','.','8',
147  '=','M','i','n','n','e','s','o','t','a',';',' ','2','.','5','.','4','.','7',
148  '=','M','i','n','n','e','a','p','o','l','i','s',';',' ','2','.','5','.','4',
149  '.','1','0','=','C','o','d','e','W','e','a','v','e','r','s',';',' ','2','.',
150  '5','.','4','.','1','1','=','W','i','n','e',' ','D','e','v','e','l','o','p',
151  'm','e','n','t',';',' ','2','.','5','.','4','.','3','=','l','o','c','a','l',
152  'h','o','s','t',';',' ','1','.','2','.','8','4','0','.','1','1','3','5','4',
153  '9','.','1','.','9','.','1','=','a','r','i','c','@','c','o','d','e','w','e',
154  'a','v','e','r','s','.','c','o','m',0 };
155 static WCHAR subjectStrCRLFW[] = {
156  '2','.','5','.','4','.','6','=','U','S','\r','\n','2','.','5','.','4','.','8',
157  '=','M','i','n','n','e','s','o','t','a','\r','\n','2','.','5','.','4','.','7',
158  '=','M','i','n','n','e','a','p','o','l','i','s','\r','\n','2','.','5','.','4',
159  '.','1','0','=','C','o','d','e','W','e','a','v','e','r','s','\r','\n','2','.',
160  '5','.','4','.','1','1','=','W','i','n','e',' ','D','e','v','e','l','o','p',
161  'm','e','n','t','\r','\n','2','.','5','.','4','.','3','=','l','o','c','a','l',
162  'h','o','s','t','\r','\n','1','.','2','.','8','4','0','.','1','1','3','5','4',
163  '9','.','1','.','9','.','1','=','a','r','i','c','@','c','o','d','e','w','e',
164  'a','v','e','r','s','.','c','o','m',0 };
165
166 typedef BOOL (WINAPI *CryptDecodeObjectFunc)(DWORD, LPCSTR, const BYTE *,
167  DWORD, DWORD, void *, DWORD *);
168 typedef DWORD (WINAPI *CertNameToStrAFunc)(DWORD,LPVOID,DWORD,LPSTR,DWORD);
169 typedef DWORD (WINAPI *CertNameToStrWFunc)(DWORD,LPVOID,DWORD,LPWSTR,DWORD);
170 typedef DWORD (WINAPI *CertRDNValueToStrAFunc)(DWORD, PCERT_RDN_VALUE_BLOB,
171  LPSTR, DWORD);
172 typedef DWORD (WINAPI *CertRDNValueToStrWFunc)(DWORD, PCERT_RDN_VALUE_BLOB,
173  LPWSTR, DWORD);
174
175 HMODULE dll;
176 static CertNameToStrAFunc pCertNameToStrA;
177 static CertNameToStrWFunc pCertNameToStrW;
178 static CryptDecodeObjectFunc pCryptDecodeObject;
179 static CertRDNValueToStrAFunc pCertRDNValueToStrA;
180 static CertRDNValueToStrWFunc pCertRDNValueToStrW;
181
182 static void test_CertRDNValueToStrA(void)
183 {
184     CertRDNAttrEncoding attrs[] = {
185      { "2.5.4.6", CERT_RDN_PRINTABLE_STRING,
186        { sizeof(bin1), (PBYTE)bin1 }, "US" },
187      { "2.5.4.8", CERT_RDN_PRINTABLE_STRING,
188        { sizeof(bin2), (PBYTE)bin2 }, "Minnesota" },
189      { "2.5.4.7", CERT_RDN_PRINTABLE_STRING,
190        { sizeof(bin3), (PBYTE)bin3 }, "Minneapolis" },
191      { "2.5.4.10", CERT_RDN_PRINTABLE_STRING,
192        { sizeof(bin4), (PBYTE)bin4 }, "CodeWeavers" },
193      { "2.5.4.11", CERT_RDN_PRINTABLE_STRING,
194        { sizeof(bin5), (PBYTE)bin5 }, "Wine Development" },
195      { "2.5.4.3", CERT_RDN_PRINTABLE_STRING,
196        { sizeof(bin6), (PBYTE)bin6 }, "localhost" },
197      { "1.2.840.113549.1.9.1", CERT_RDN_IA5_STRING,
198        { sizeof(bin7), (PBYTE)bin7 }, "aric@codeweavers.com" },
199     };
200     DWORD i, ret;
201     char buffer[2000];
202     CERT_RDN_VALUE_BLOB blob = { 0, NULL };
203
204     if (!pCertRDNValueToStrA) return;
205
206     /* This crashes
207     ret = pCertRDNValueToStrA(0, NULL, NULL, 0);
208      */
209     /* With empty input, it generates the empty string */
210     SetLastError(0xdeadbeef);
211     ret = pCertRDNValueToStrA(0, &blob, NULL, 0);
212     ok(ret == 1 && GetLastError() == 0xdeadbeef, "Expected empty string\n");
213     ret = pCertRDNValueToStrA(0, &blob, buffer, sizeof(buffer));
214     ok(ret == 1 && GetLastError() == 0xdeadbeef, "Expected empty string\n");
215     ok(!buffer[0], "Expected empty string\n");
216
217     for (i = 0; i < sizeof(attrs) / sizeof(attrs[0]); i++)
218     {
219         ret = pCertRDNValueToStrA(attrs[i].dwValueType, &attrs[i].Value,
220          buffer, sizeof(buffer));
221         ok(ret == strlen(attrs[i].str) + 1, "Expected length %d, got %ld\n",
222          strlen(attrs[i].str) + 1, ret);
223         ok(!strcmp(buffer, attrs[i].str), "Expected %s, got %s\n", attrs[i].str,
224          buffer);
225     }
226 }
227
228 static void test_CertRDNValueToStrW(void)
229 {
230     static const WCHAR usW[] = { 'U','S',0 };
231     static const WCHAR minnesotaW[] = { 'M','i','n','n','e','s','o','t','a',0 };
232     static const WCHAR minneapolisW[] = { 'M','i','n','n','e','a','p','o','l',
233      'i','s',0 };
234     static const WCHAR codeweaversW[] = { 'C','o','d','e','W','e','a','v','e',
235      'r','s',0 };
236     static const WCHAR wineDevW[] = { 'W','i','n','e',' ','D','e','v','e','l',
237      'o','p','m','e','n','t',0 };
238     static const WCHAR localhostW[] = { 'l','o','c','a','l','h','o','s','t',0 };
239     static const WCHAR aricW[] = { 'a','r','i','c','@','c','o','d','e','w','e',
240      'a','v','e','r','s','.','c','o','m',0 };
241     CertRDNAttrEncodingW attrs[] = {
242      { "2.5.4.6", CERT_RDN_PRINTABLE_STRING,
243        { sizeof(bin1), (PBYTE)bin1 }, usW },
244      { "2.5.4.8", CERT_RDN_PRINTABLE_STRING,
245        { sizeof(bin2), (PBYTE)bin2 }, minnesotaW },
246      { "2.5.4.7", CERT_RDN_PRINTABLE_STRING,
247        { sizeof(bin3), (PBYTE)bin3 }, minneapolisW },
248      { "2.5.4.10", CERT_RDN_PRINTABLE_STRING,
249        { sizeof(bin4), (PBYTE)bin4 }, codeweaversW },
250      { "2.5.4.11", CERT_RDN_PRINTABLE_STRING,
251        { sizeof(bin5), (PBYTE)bin5 }, wineDevW },
252      { "2.5.4.3", CERT_RDN_PRINTABLE_STRING,
253        { sizeof(bin6), (PBYTE)bin6 }, localhostW },
254      { "1.2.840.113549.1.9.1", CERT_RDN_IA5_STRING,
255        { sizeof(bin7), (PBYTE)bin7 }, aricW },
256     };
257     DWORD i, ret;
258     WCHAR buffer[2000];
259     CERT_RDN_VALUE_BLOB blob = { 0, NULL };
260
261     if (!pCertRDNValueToStrW) return;
262
263     /* This crashes
264     ret = pCertRDNValueToStrW(0, NULL, NULL, 0);
265      */
266     /* With empty input, it generates the empty string */
267     SetLastError(0xdeadbeef);
268     ret = pCertRDNValueToStrW(0, &blob, NULL, 0);
269     ok(ret == 1 && GetLastError() == 0xdeadbeef, "Expected empty string\n");
270     ret = pCertRDNValueToStrW(0, &blob, buffer,
271      sizeof(buffer) / sizeof(buffer[0]));
272     ok(ret == 1 && GetLastError() == 0xdeadbeef, "Expected empty string\n");
273     ok(!buffer[0], "Expected empty string\n");
274
275     for (i = 0; i < sizeof(attrs) / sizeof(attrs[0]); i++)
276     {
277         ret = pCertRDNValueToStrW(attrs[i].dwValueType, &attrs[i].Value,
278          buffer, sizeof(buffer) / sizeof(buffer[0]));
279         ok(ret == lstrlenW(attrs[i].str) + 1, "Expected length %d, got %ld\n",
280          lstrlenW(attrs[i].str) + 1, ret);
281         ok(!lstrcmpW(buffer, attrs[i].str), "Expected %s, got %s\n",
282          wine_dbgstr_w(attrs[i].str), wine_dbgstr_w(buffer));
283     }
284 }
285
286 static void test_NameToStrConversionA(PCERT_NAME_BLOB pName, DWORD dwStrType,
287  LPCSTR expected)
288 {
289     char buffer[2000] = { 0 };
290     DWORD i;
291
292     i = pCertNameToStrA(X509_ASN_ENCODING,pName, dwStrType, buffer,
293      sizeof(buffer));
294     ok(i == strlen(expected) + 1, "Expected %d chars, got %ld\n",
295      strlen(expected) + 1, i);
296     ok(!strcmp(buffer, expected), "Expected %s, got %s\n", expected, buffer);
297 }
298
299 static void test_CertNameToStrA(void)
300 {
301     PCCERT_CONTEXT context;
302
303     if (!pCertNameToStrA) return;
304
305     context = CertCreateCertificateContext(X509_ASN_ENCODING, cert,
306      sizeof(cert));
307     ok(context != NULL, "CertCreateCertificateContext failed: %08lx\n",
308      GetLastError());
309     if (context)
310     {
311         DWORD ret;
312
313         /* This crashes
314         ret = pCertNameToStrA(0, NULL, 0, NULL, 0);
315          */
316         /* Test with a bogus encoding type */
317         SetLastError(0xdeadbeef);
318         ret = pCertNameToStrA(0, &context->pCertInfo->Issuer, 0, NULL, 0);
319         ok(ret == 1 && GetLastError() == ERROR_FILE_NOT_FOUND,
320          "Expected retval 1 and ERROR_FILE_NOT_FOUND, got %ld - %08lx\n",
321          ret, GetLastError());
322         SetLastError(0xdeadbeef);
323         ret = pCertNameToStrA(X509_ASN_ENCODING, &context->pCertInfo->Issuer,
324          0, NULL, 0);
325         ok(ret && GetLastError() == ERROR_SUCCESS,
326          "Expected positive return and ERROR_SUCCESS, got %ld - %08lx\n",
327          ret, GetLastError());
328
329         test_NameToStrConversionA(&context->pCertInfo->Issuer,
330          CERT_SIMPLE_NAME_STR, issuerStr);
331         test_NameToStrConversionA(&context->pCertInfo->Issuer,
332          CERT_SIMPLE_NAME_STR | CERT_NAME_STR_SEMICOLON_FLAG,
333          issuerStrSemicolon);
334         test_NameToStrConversionA(&context->pCertInfo->Issuer,
335          CERT_SIMPLE_NAME_STR | CERT_NAME_STR_CRLF_FLAG,
336          issuerStrCRLF);
337         test_NameToStrConversionA(&context->pCertInfo->Subject,
338          CERT_OID_NAME_STR, subjectStr);
339         test_NameToStrConversionA(&context->pCertInfo->Subject,
340          CERT_OID_NAME_STR | CERT_NAME_STR_SEMICOLON_FLAG,
341          subjectStrSemicolon);
342         test_NameToStrConversionA(&context->pCertInfo->Subject,
343          CERT_OID_NAME_STR | CERT_NAME_STR_CRLF_FLAG,
344          subjectStrCRLF);
345
346         CertFreeCertificateContext(context);
347     }
348 }
349
350 static void test_NameToStrConversionW(PCERT_NAME_BLOB pName, DWORD dwStrType,
351  LPCWSTR expected)
352 {
353     WCHAR buffer[2000] = { 0 };
354     DWORD i;
355
356     i = pCertNameToStrW(X509_ASN_ENCODING,pName, dwStrType, buffer,
357      sizeof(buffer) / sizeof(buffer[0]));
358     ok(i == lstrlenW(expected) + 1, "Expected %d chars, got %ld\n",
359      lstrlenW(expected) + 1, i);
360     ok(!lstrcmpW(buffer, expected), "Expected %s, got %s\n",
361      wine_dbgstr_w(expected), wine_dbgstr_w(buffer));
362 }
363
364 static void test_CertNameToStrW(void)
365 {
366     PCCERT_CONTEXT context;
367
368     if (!pCertNameToStrW) return;
369
370     context = CertCreateCertificateContext(X509_ASN_ENCODING, cert,
371      sizeof(cert));
372     ok(context != NULL, "CertCreateCertificateContext failed: %08lx\n",
373      GetLastError());
374     if (context)
375     {
376         DWORD ret;
377
378         /* This crashes
379         ret = pCertNameToStrW(0, NULL, 0, NULL, 0);
380          */
381         /* Test with a bogus encoding type */
382         SetLastError(0xdeadbeef);
383         ret = pCertNameToStrW(0, &context->pCertInfo->Issuer, 0, NULL, 0);
384         ok(ret == 1 && GetLastError() == ERROR_FILE_NOT_FOUND,
385          "Expected retval 1 and ERROR_FILE_NOT_FOUND, got %ld - %08lx\n",
386          ret, GetLastError());
387         SetLastError(0xdeadbeef);
388         ret = pCertNameToStrW(X509_ASN_ENCODING, &context->pCertInfo->Issuer,
389          0, NULL, 0);
390         ok(ret && GetLastError() == ERROR_SUCCESS,
391          "Expected positive return and ERROR_SUCCESS, got %ld - %08lx\n",
392          ret, GetLastError());
393
394         test_NameToStrConversionW(&context->pCertInfo->Issuer,
395          CERT_SIMPLE_NAME_STR, issuerStrW);
396         test_NameToStrConversionW(&context->pCertInfo->Issuer,
397          CERT_SIMPLE_NAME_STR | CERT_NAME_STR_SEMICOLON_FLAG,
398          issuerStrSemicolonW);
399         test_NameToStrConversionW(&context->pCertInfo->Issuer,
400          CERT_SIMPLE_NAME_STR | CERT_NAME_STR_CRLF_FLAG,
401          issuerStrCRLFW);
402         test_NameToStrConversionW(&context->pCertInfo->Subject,
403          CERT_OID_NAME_STR, subjectStrW);
404         test_NameToStrConversionW(&context->pCertInfo->Subject,
405          CERT_OID_NAME_STR | CERT_NAME_STR_SEMICOLON_FLAG,
406          subjectStrSemicolonW);
407         test_NameToStrConversionW(&context->pCertInfo->Subject,
408          CERT_OID_NAME_STR | CERT_NAME_STR_CRLF_FLAG,
409          subjectStrCRLFW);
410
411         CertFreeCertificateContext(context);
412     }
413 }
414
415 START_TEST(str)
416 {
417     dll = LoadLibrary("Crypt32.dll");
418
419     pCertNameToStrA = (CertNameToStrAFunc)GetProcAddress(dll,"CertNameToStrA");
420     pCertNameToStrW = (CertNameToStrWFunc)GetProcAddress(dll,"CertNameToStrW");
421     pCertRDNValueToStrA = (CertRDNValueToStrAFunc)GetProcAddress(dll,
422      "CertRDNValueToStrA");
423     pCertRDNValueToStrW = (CertRDNValueToStrWFunc)GetProcAddress(dll,
424      "CertRDNValueToStrW");
425     pCryptDecodeObject = (CryptDecodeObjectFunc)GetProcAddress(dll,
426      "CryptDecodeObject");
427
428     test_CertRDNValueToStrA();
429     test_CertRDNValueToStrW();
430     test_CertNameToStrA();
431     test_CertNameToStrW();
432
433     FreeLibrary(dll);
434 }