2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 // testutils - utilities for unit test drivers
22 #include "testutils.h"
24 using namespace CssmClient
;
30 // Error and diagnostic drivers
32 void error(const char *msg
= NULL
, ...)
37 vfprintf(stderr
, msg
, args
);
44 void error(const CssmCommonError
&err
, const char *msg
= NULL
, ...)
49 vfprintf(stderr
, msg
, args
);
51 fprintf(stderr
, ": %s", cssmErrorString(err
.cssmError()).c_str());
57 void detail(const char *msg
= NULL
, ...)
62 vfprintf(stdout
, msg
, args
);
68 void detail(const CssmCommonError
&err
, const char *msg
)
71 printf("%s (ok): %s\n", msg
, cssmErrorString(err
).c_str());
74 void prompt(const char *msg
)
76 if (isatty(fileno(stdin
)))
82 if (isatty(fileno(stdin
)))
88 // FakeContext management
90 FakeContext::FakeContext(CSSM_CONTEXT_TYPE type
, CSSM_ALGORITHMS alg
, uint32 count
)
93 NumberOfAttributes
= count
;
94 ContextAttributes
= new Attr
[count
];
98 FakeContext::FakeContext(CSSM_CONTEXT_TYPE type
, CSSM_ALGORITHMS alg
, ...)
105 while (va_arg(args
, Attr
*))
110 NumberOfAttributes
= count
;
111 ContextAttributes
= new Attr
[count
];
115 for (uint32 n
= 0; n
< count
; n
++)
116 (*this)[n
] = *va_arg(args
, Attr
*);
122 // ACL test driver class
124 AclTester::AclTester(ClientSession
&ss
, const AclEntryInput
*acl
) : session(ss
)
127 StringData
keyBits("Tweedle!");
128 CssmKey
key(keyBits
);
129 key
.header().KeyClass
= CSSM_KEYCLASS_SESSION_KEY
;
132 CssmData unwrappedData
;
133 FakeContext
unwrapContext(CSSM_ALGCLASS_SYMMETRIC
, CSSM_ALGID_NONE
, 0);
134 CssmKey::Header keyHeader
;
135 ss
.unwrapKey(noDb
, unwrapContext
, noKey
, noKey
,
137 CSSM_KEYUSE_ENCRYPT
| CSSM_KEYUSE_DECRYPT
,
138 CSSM_KEYATTR_EXTRACTABLE
,
140 unwrappedData
, keyRef
, keyHeader
);
141 detail("Key seeded with ACL");
145 void AclTester::testWrap(const AccessCredentials
*cred
, const char *howWrong
)
147 FakeContext
wrapContext(CSSM_ALGCLASS_SYMMETRIC
, CSSM_ALGID_NONE
, 0);
148 CssmWrappedKey wrappedKey
;
150 session
.wrapKey(wrapContext
, noKey
, keyRef
,
151 cred
, NULL
/*descriptive*/, wrappedKey
);
153 error("WRAP MISTAKENLY SUCCEEDED: %s", howWrong
);
155 detail("extract OK");
156 } catch (const CssmCommonError
&err
) {
158 error(err
, "FAILED TO EXTRACT KEY");
159 detail(err
, "extract failed OK");
163 void AclTester::testEncrypt(const AccessCredentials
*cred
, const char *howWrong
)
165 CssmKey keyForm
; memset(&keyForm
, 0, sizeof(keyForm
));
166 StringData
iv("Aardvark");
167 StringData
clearText("blah");
168 CssmData remoteCipher
;
171 FakeContext
cryptoContext(CSSM_ALGCLASS_SYMMETRIC
, CSSM_ALGID_DES
,
172 &::Context::Attr(CSSM_ATTRIBUTE_KEY
, keyForm
),
173 &::Context::Attr(CSSM_ATTRIBUTE_INIT_VECTOR
, iv
),
174 &::Context::Attr(CSSM_ATTRIBUTE_MODE
, CSSM_ALGMODE_CBC_IV8
),
175 &::Context::Attr(CSSM_ATTRIBUTE_PADDING
, CSSM_PADDING_PKCS1
),
176 &::Context::Attr(CSSM_ATTRIBUTE_ACCESS_CREDENTIALS
, *cred
),
178 session
.encrypt(cryptoContext
, keyRef
, clearText
, remoteCipher
);
180 FakeContext
cryptoContext(CSSM_ALGCLASS_SYMMETRIC
, CSSM_ALGID_DES
,
181 &::Context::Attr(CSSM_ATTRIBUTE_KEY
, keyForm
),
182 &::Context::Attr(CSSM_ATTRIBUTE_INIT_VECTOR
, iv
),
183 &::Context::Attr(CSSM_ATTRIBUTE_MODE
, CSSM_ALGMODE_CBC_IV8
),
184 &::Context::Attr(CSSM_ATTRIBUTE_PADDING
, CSSM_PADDING_PKCS1
),
186 session
.encrypt(cryptoContext
, keyRef
, clearText
, remoteCipher
);
189 error("ENCRYPT MISTAKENLY SUCCEEDED: %s", howWrong
);
191 detail("encrypt OK");
192 } catch (CssmCommonError
&err
) {
194 error(err
, "FAILED TO ENCRYPT");
195 detail(err
, "encrypt failed");
201 // Database test driver class
203 DbTester::DbTester(ClientSession
&ss
, const char *path
,
204 const AccessCredentials
*cred
, int timeout
, bool sleepLock
)
205 : session(ss
), dbId(ssuid
, path
, NULL
)
207 params
.idleTimeout
= timeout
;
208 params
.lockOnSleep
= sleepLock
;
209 dbRef
= ss
.createDb(dbId
, cred
, NULL
, params
);
210 detail("Database %s created", path
);
214 void DbTester::unlock(const char *howWrong
)
218 session
.unlock(dbRef
);
220 error("DATABASE MISTAKENLY UNLOCKED: %s", howWrong
);
221 } catch (CssmError
&err
) {
223 error(err
, howWrong
);
224 detail(err
, howWrong
);
228 void DbTester::changePassphrase(const AccessCredentials
*cred
, const char *howWrong
)
232 session
.changePassphrase(dbRef
, cred
);
234 error("PASSPHRASE CHANGE MISTAKENLY SUCCEEDED: %s", howWrong
);
235 } catch (CssmError
&err
) {
237 error(err
, howWrong
);
238 detail(err
, howWrong
);