- add some tests for OID functions
[wine/testsucceed.git] / dlls / crypt32 / tests / encode.c
blob193ba548c70fcc8ef3fcf183e10dfcfef2e5175c
1 /*
2 * Unit test suite for crypt32.dll's CryptEncodeObjectEx
4 * Copyright 2005 Juan Lang
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <stdio.h>
21 #include <stdarg.h>
22 #include <windef.h>
23 #include <winbase.h>
24 #include <winerror.h>
25 #include <wincrypt.h>
27 #include "wine/test.h"
29 struct encodedInt
31 int val;
32 BYTE encoded[6];
35 static void test_encodeint(void)
37 static const struct encodedInt ints[] = {
38 { 1, { 2, 1, 1 } },
39 { 127, { 2, 1, 0x7f } },
40 { 128, { 2, 2, 0x00, 0x80 } },
41 { 256, { 2, 2, 0x01, 0x00 } },
42 { -128, { 2, 1, 0x80 } },
43 { -129, { 2, 2, 0xff, 0x7f } },
44 { 0xbaddf00d, { 2, 4, 0xba, 0xdd, 0xf0, 0x0d } },
46 DWORD bufSize = 0;
47 int i;
48 BOOL ret;
50 /* CryptEncodeObjectEx with NULL bufSize crashes..
51 ret = CryptEncodeObjectEx(3, X509_INTEGER, &ints[0].val, 0, NULL, NULL,
52 NULL);
54 /* check bogus encoding */
55 ret = CryptEncodeObjectEx(0, X509_INTEGER, &ints[0].val, 0, NULL, NULL,
56 &bufSize);
57 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
58 "Expected ERROR_FILE_NOT_FOUND, got %ld\n", GetLastError());
59 /* check with NULL integer buffer. Windows XP incorrectly returns an
60 * NTSTATUS.
62 ret = CryptEncodeObjectEx(X509_ASN_ENCODING, X509_INTEGER, NULL, 0, NULL,
63 NULL, &bufSize);
64 ok(!ret && (GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() ==
65 STATUS_ACCESS_VIOLATION), "Unexpected error code %ld\n", GetLastError());
66 for (i = 0; i < sizeof(ints) / sizeof(ints[0]); i++)
68 BYTE *buf = NULL;
70 ret = CryptEncodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
71 X509_INTEGER, &ints[i].val, 0, NULL, NULL, &bufSize);
72 ok(ret || GetLastError() == ERROR_MORE_DATA,
73 "Expected success or ERROR_MORE_DATA, got %ld\n", GetLastError());
74 ret = CryptEncodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
75 X509_INTEGER, &ints[i].val, CRYPT_ENCODE_ALLOC_FLAG, NULL,
76 (BYTE *)&buf, &bufSize);
77 ok(ret, "CryptEncodeObjectEx failed: %ld\n", GetLastError());
78 ok(buf[0] == 2, "Got unexpected type %d for integer (expected 2)\n",
79 buf[0]);
80 ok(!memcmp(buf + 1, ints[i].encoded + 1, ints[i].encoded[1] + 1),
81 "Encoded value of 0x%08x didn't match expected\n", ints[i].val);
82 LocalFree(buf);
86 static void test_registerOIDFunction(void)
88 static const WCHAR bogusDll[] = { 'b','o','g','u','s','.','d','l','l',0 };
89 BOOL ret;
91 /* oddly, this succeeds under WinXP; the function name key is merely
92 * omitted. This may be a side effect of the registry code, I don't know.
93 * I don't check it because I doubt anyone would depend on it.
94 ret = CryptRegisterOIDFunction(X509_ASN_ENCODING, NULL,
95 "1.2.3.4.5.6.7.8.9.10", bogusDll, NULL);
97 /* On windows XP, GetLastError is incorrectly being set with an HRESULT,
98 * HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER)
100 ret = CryptRegisterOIDFunction(X509_ASN_ENCODING, "foo", NULL, bogusDll,
101 NULL);
102 ok(!ret && (GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() ==
103 HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER)),
104 "Expected ERROR_INVALID_PARAMETER: %ld\n", GetLastError());
105 /* This has no effect, but "succeeds" on XP */
106 ret = CryptRegisterOIDFunction(X509_ASN_ENCODING, "foo",
107 "1.2.3.4.5.6.7.8.9.10", NULL, NULL);
108 ok(ret, "Expected pseudo-success, got %ld\n", GetLastError());
109 ret = CryptRegisterOIDFunction(X509_ASN_ENCODING, "CryptDllEncodeObject",
110 "1.2.3.4.5.6.7.8.9.10", bogusDll, NULL);
111 ok(ret, "CryptRegisterOIDFunction failed: %ld\n", GetLastError());
112 ret = CryptUnregisterOIDFunction(X509_ASN_ENCODING, "CryptDllEncodeObject",
113 "1.2.3.4.5.6.7.8.9.10");
114 ok(ret, "CryptUnregisterOIDFunction failed: %ld\n", GetLastError());
115 ret = CryptRegisterOIDFunction(X509_ASN_ENCODING, "bogus",
116 "1.2.3.4.5.6.7.8.9.10", bogusDll, NULL);
117 ok(ret, "CryptRegisterOIDFunction failed: %ld\n", GetLastError());
118 ret = CryptUnregisterOIDFunction(X509_ASN_ENCODING, "bogus",
119 "1.2.3.4.5.6.7.8.9.10");
120 ok(ret, "CryptUnregisterOIDFunction failed: %ld\n", GetLastError());
121 /* This has no effect */
122 ret = CryptRegisterOIDFunction(PKCS_7_ASN_ENCODING, "CryptDllEncodeObject",
123 "1.2.3.4.5.6.7.8.9.10", bogusDll, NULL);
124 ok(ret, "CryptRegisterOIDFunction failed: %ld\n", GetLastError());
125 /* Check with bogus encoding type: */
126 ret = CryptRegisterOIDFunction(0, "CryptDllEncodeObject",
127 "1.2.3.4.5.6.7.8.9.10", bogusDll, NULL);
128 ok(ret, "CryptRegisterOIDFunction failed: %ld\n", GetLastError());
129 /* This is written with value 3 verbatim. Thus, the encoding type isn't
130 * (for now) treated as a mask.
132 ret = CryptRegisterOIDFunction(3, "CryptDllEncodeObject",
133 "1.2.3.4.5.6.7.8.9.10", bogusDll, NULL);
134 ok(ret, "CryptRegisterOIDFunction failed: %ld\n", GetLastError());
135 ret = CryptUnregisterOIDFunction(3, "CryptDllEncodeObject",
136 "1.2.3.4.5.6.7.8.9.10");
137 ok(ret, "CryptUnregisterOIDFunction failed: %ld\n", GetLastError());
140 START_TEST(encode)
142 test_encodeint();
143 test_registerOIDFunction();