2 * Copyright (c) 2000-2001,2004 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
28 // testutils - utilities for unit test drivers
30 #include "testutils.h"
32 using namespace CssmClient
;
38 // Error and diagnostic drivers
40 void error(const char *msg
= NULL
, ...)
45 vfprintf(stderr
, msg
, args
);
52 void error(const CssmCommonError
&err
, const char *msg
= NULL
, ...)
57 vfprintf(stderr
, msg
, args
);
59 fprintf(stderr
, ": %s", cssmErrorString(err
.cssmError()).c_str());
65 void detail(const char *msg
= NULL
, ...)
70 vfprintf(stdout
, msg
, args
);
76 void detail(const CssmCommonError
&err
, const char *msg
)
79 printf("%s (ok): %s\n", msg
, cssmErrorString(err
).c_str());
82 void prompt(const char *msg
)
84 if (isatty(fileno(stdin
)))
90 if (isatty(fileno(stdin
)))
96 // FakeContext management
98 FakeContext::FakeContext(CSSM_CONTEXT_TYPE type
, CSSM_ALGORITHMS alg
, uint32 count
)
101 NumberOfAttributes
= count
;
102 ContextAttributes
= new Attr
[count
];
106 FakeContext::FakeContext(CSSM_CONTEXT_TYPE type
, CSSM_ALGORITHMS alg
, ...)
113 while (va_arg(args
, Attr
*))
118 NumberOfAttributes
= count
;
119 ContextAttributes
= new Attr
[count
];
123 for (uint32 n
= 0; n
< count
; n
++)
124 (*this)[n
] = *va_arg(args
, Attr
*);
130 // ACL test driver class
132 AclTester::AclTester(ClientSession
&ss
, const AclEntryInput
*acl
) : session(ss
)
135 StringData
keyBits("Tweedle!");
136 CssmKey
key(keyBits
);
137 key
.header().KeyClass
= CSSM_KEYCLASS_SESSION_KEY
;
140 CssmData unwrappedData
;
141 FakeContext
unwrapContext(CSSM_ALGCLASS_SYMMETRIC
, CSSM_ALGID_NONE
, 0);
142 CssmKey::Header keyHeader
;
143 ss
.unwrapKey(noDb
, unwrapContext
, noKey
, noKey
,
145 CSSM_KEYUSE_ENCRYPT
| CSSM_KEYUSE_DECRYPT
,
146 CSSM_KEYATTR_EXTRACTABLE
,
148 unwrappedData
, keyRef
, keyHeader
);
149 detail("Key seeded with ACL");
153 void AclTester::testWrap(const AccessCredentials
*cred
, const char *howWrong
)
155 FakeContext
wrapContext(CSSM_ALGCLASS_SYMMETRIC
, CSSM_ALGID_NONE
, 0);
156 CssmWrappedKey wrappedKey
;
158 session
.wrapKey(wrapContext
, noKey
, keyRef
,
159 cred
, NULL
/*descriptive*/, wrappedKey
);
161 error("WRAP MISTAKENLY SUCCEEDED: %s", howWrong
);
163 detail("extract OK");
164 } catch (const CssmCommonError
&err
) {
166 error(err
, "FAILED TO EXTRACT KEY");
167 detail(err
, "extract failed OK");
171 void AclTester::testEncrypt(const AccessCredentials
*cred
, const char *howWrong
)
173 CssmKey keyForm
; memset(&keyForm
, 0, sizeof(keyForm
));
174 StringData
iv("Aardvark");
175 StringData
clearText("blah");
176 CssmData remoteCipher
;
179 FakeContext
cryptoContext(CSSM_ALGCLASS_SYMMETRIC
, CSSM_ALGID_DES
,
180 &::Context::Attr(CSSM_ATTRIBUTE_KEY
, keyForm
),
181 &::Context::Attr(CSSM_ATTRIBUTE_INIT_VECTOR
, iv
),
182 &::Context::Attr(CSSM_ATTRIBUTE_MODE
, CSSM_ALGMODE_CBC_IV8
),
183 &::Context::Attr(CSSM_ATTRIBUTE_PADDING
, CSSM_PADDING_PKCS1
),
184 &::Context::Attr(CSSM_ATTRIBUTE_ACCESS_CREDENTIALS
, *cred
),
186 session
.encrypt(cryptoContext
, keyRef
, clearText
, remoteCipher
);
188 FakeContext
cryptoContext(CSSM_ALGCLASS_SYMMETRIC
, CSSM_ALGID_DES
,
189 &::Context::Attr(CSSM_ATTRIBUTE_KEY
, keyForm
),
190 &::Context::Attr(CSSM_ATTRIBUTE_INIT_VECTOR
, iv
),
191 &::Context::Attr(CSSM_ATTRIBUTE_MODE
, CSSM_ALGMODE_CBC_IV8
),
192 &::Context::Attr(CSSM_ATTRIBUTE_PADDING
, CSSM_PADDING_PKCS1
),
194 session
.encrypt(cryptoContext
, keyRef
, clearText
, remoteCipher
);
197 error("ENCRYPT MISTAKENLY SUCCEEDED: %s", howWrong
);
199 detail("encrypt OK");
200 } catch (CssmCommonError
&err
) {
202 error(err
, "FAILED TO ENCRYPT");
203 detail(err
, "encrypt failed");
209 // Database test driver class
211 DbTester::DbTester(ClientSession
&ss
, const char *path
,
212 const AccessCredentials
*cred
, int timeout
, bool sleepLock
)
213 : session(ss
), dbId(ssuid
, path
, NULL
)
215 params
.idleTimeout
= timeout
;
216 params
.lockOnSleep
= sleepLock
;
217 dbRef
= ss
.createDb(dbId
, cred
, NULL
, params
);
218 detail("Database %s created", path
);
222 void DbTester::unlock(const char *howWrong
)
226 session
.unlock(dbRef
);
228 error("DATABASE MISTAKENLY UNLOCKED: %s", howWrong
);
229 } catch (CssmError
&err
) {
231 error(err
, howWrong
);
232 detail(err
, howWrong
);
236 void DbTester::changePassphrase(const AccessCredentials
*cred
, const char *howWrong
)
240 session
.changePassphrase(dbRef
, cred
);
242 error("PASSPHRASE CHANGE MISTAKENLY SUCCEEDED: %s", howWrong
);
243 } catch (CssmError
&err
) {
245 error(err
, howWrong
);
246 detail(err
, howWrong
);