2 * Copyright (c) 2000-2001,2004 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 // testutils - utilities for unit test drivers
28 #include "testutils.h"
30 using namespace CssmClient
;
36 // Error and diagnostic drivers
38 void error(const char *msg
= NULL
, ...)
43 vfprintf(stderr
, msg
, args
);
50 void error(const CssmCommonError
&err
, const char *msg
= NULL
, ...)
55 vfprintf(stderr
, msg
, args
);
57 fprintf(stderr
, ": %s", cssmErrorString(err
.cssmError()).c_str());
63 void detail(const char *msg
= NULL
, ...)
68 vfprintf(stdout
, msg
, args
);
74 void detail(const CssmCommonError
&err
, const char *msg
)
77 printf("%s (ok): %s\n", msg
, cssmErrorString(err
).c_str());
80 void prompt(const char *msg
)
82 if (isatty(fileno(stdin
)))
88 if (isatty(fileno(stdin
)))
94 // FakeContext management
96 FakeContext::FakeContext(CSSM_CONTEXT_TYPE type
, CSSM_ALGORITHMS alg
, uint32 count
)
99 NumberOfAttributes
= count
;
100 ContextAttributes
= new Attr
[count
];
104 FakeContext::FakeContext(CSSM_CONTEXT_TYPE type
, CSSM_ALGORITHMS alg
, ...)
111 while (va_arg(args
, Attr
*))
116 NumberOfAttributes
= count
;
117 ContextAttributes
= new Attr
[count
];
121 for (uint32 n
= 0; n
< count
; n
++)
122 (*this)[n
] = *va_arg(args
, Attr
*);
128 // ACL test driver class
130 AclTester::AclTester(ClientSession
&ss
, const AclEntryInput
*acl
) : session(ss
)
133 StringData
keyBits("Tweedle!");
134 CssmKey
key(keyBits
);
135 key
.header().KeyClass
= CSSM_KEYCLASS_SESSION_KEY
;
138 CssmData unwrappedData
;
139 FakeContext
unwrapContext(CSSM_ALGCLASS_SYMMETRIC
, CSSM_ALGID_NONE
, 0);
140 CssmKey::Header keyHeader
;
141 ss
.unwrapKey(noDb
, unwrapContext
, noKey
, noKey
,
143 CSSM_KEYUSE_ENCRYPT
| CSSM_KEYUSE_DECRYPT
,
144 CSSM_KEYATTR_EXTRACTABLE
,
146 unwrappedData
, keyRef
, keyHeader
);
147 detail("Key seeded with ACL");
151 void AclTester::testWrap(const AccessCredentials
*cred
, const char *howWrong
)
153 FakeContext
wrapContext(CSSM_ALGCLASS_SYMMETRIC
, CSSM_ALGID_NONE
, 0);
154 CssmWrappedKey wrappedKey
;
156 session
.wrapKey(wrapContext
, noKey
, keyRef
,
157 cred
, NULL
/*descriptive*/, wrappedKey
);
159 error("WRAP MISTAKENLY SUCCEEDED: %s", howWrong
);
161 detail("extract OK");
162 } catch (const CssmCommonError
&err
) {
164 error(err
, "FAILED TO EXTRACT KEY");
165 detail(err
, "extract failed OK");
169 void AclTester::testEncrypt(const AccessCredentials
*cred
, const char *howWrong
)
171 CssmKey keyForm
; memset(&keyForm
, 0, sizeof(keyForm
));
172 StringData
iv("Aardvark");
173 StringData
clearText("blah");
174 CssmData remoteCipher
;
177 FakeContext
cryptoContext(CSSM_ALGCLASS_SYMMETRIC
, CSSM_ALGID_DES
,
178 &::Context::Attr(CSSM_ATTRIBUTE_KEY
, keyForm
),
179 &::Context::Attr(CSSM_ATTRIBUTE_INIT_VECTOR
, iv
),
180 &::Context::Attr(CSSM_ATTRIBUTE_MODE
, CSSM_ALGMODE_CBC_IV8
),
181 &::Context::Attr(CSSM_ATTRIBUTE_PADDING
, CSSM_PADDING_PKCS1
),
182 &::Context::Attr(CSSM_ATTRIBUTE_ACCESS_CREDENTIALS
, *cred
),
184 session
.encrypt(cryptoContext
, keyRef
, clearText
, remoteCipher
);
186 FakeContext
cryptoContext(CSSM_ALGCLASS_SYMMETRIC
, CSSM_ALGID_DES
,
187 &::Context::Attr(CSSM_ATTRIBUTE_KEY
, keyForm
),
188 &::Context::Attr(CSSM_ATTRIBUTE_INIT_VECTOR
, iv
),
189 &::Context::Attr(CSSM_ATTRIBUTE_MODE
, CSSM_ALGMODE_CBC_IV8
),
190 &::Context::Attr(CSSM_ATTRIBUTE_PADDING
, CSSM_PADDING_PKCS1
),
192 session
.encrypt(cryptoContext
, keyRef
, clearText
, remoteCipher
);
195 error("ENCRYPT MISTAKENLY SUCCEEDED: %s", howWrong
);
197 detail("encrypt OK");
198 } catch (CssmCommonError
&err
) {
200 error(err
, "FAILED TO ENCRYPT");
201 detail(err
, "encrypt failed");
207 // Database test driver class
209 DbTester::DbTester(ClientSession
&ss
, const char *path
,
210 const AccessCredentials
*cred
, int timeout
, bool sleepLock
)
211 : session(ss
), dbId(ssuid
, path
, NULL
)
213 params
.idleTimeout
= timeout
;
214 params
.lockOnSleep
= sleepLock
;
215 dbRef
= ss
.createDb(dbId
, cred
, NULL
, params
);
216 detail("Database %s created", path
);
220 void DbTester::unlock(const char *howWrong
)
224 session
.unlock(dbRef
);
226 error("DATABASE MISTAKENLY UNLOCKED: %s", howWrong
);
227 } catch (CssmError
&err
) {
229 error(err
, howWrong
);
230 detail(err
, howWrong
);
234 void DbTester::changePassphrase(const AccessCredentials
*cred
, const char *howWrong
)
238 session
.changePassphrase(dbRef
, cred
);
240 error("PASSPHRASE CHANGE MISTAKENLY SUCCEEDED: %s", howWrong
);
241 } catch (CssmError
&err
) {
243 error(err
, howWrong
);
244 detail(err
, howWrong
);