X-Git-Url: https://git.saurik.com/apple/security.git/blobdiff_plain/80e2389990082500d76eb566d4946be3e786c3ef..d8f41ccd20de16f8ebe2ccc84d47bf1cb2b26bbb:/SecurityTests/clxutils/threadTest/attach.cpp diff --git a/SecurityTests/clxutils/threadTest/attach.cpp b/SecurityTests/clxutils/threadTest/attach.cpp new file mode 100644 index 00000000..9c29cb28 --- /dev/null +++ b/SecurityTests/clxutils/threadTest/attach.cpp @@ -0,0 +1,270 @@ +/* + * attach/creatContext/deleteContext/detach test + */ +#include "testParams.h" +#include +#include +#include +#include +#include +#include +#include + +/* for memory leak debug only, with only one thread running */ +#define DO_PAUSE 0 + +#define ATTACHES_PER_LOOP 100 +#define ATTACH_EACH_LOOP 1 /* 1 ==> ATTACHES_PER_LOOP moduleAttaches */ + /* 0 ==> all thru testParams->cspHand */ +#define CONTEXT_EACH_LOOP 1 /* 1 ==> CSSM_CSP_Create*Context for each attach */ + /* 0 ==> just do attach/detach */ +#define CSPDL_ENABLE 1 /* attach to CSP and DL sides of CSPDL */ +#define DO_UNLOAD 0 /* enable CSSM_ModuleUnload() */ + +#if (!ATTACH_EACH_LOOP && !CONTEXT_EACH_LOOP) +#error Hey! Must configure for attach and/or createContext! +#endif + +static CSSM_API_MEMORY_FUNCS memFuncs = { + appMalloc, + appFree, + appRealloc, + appCalloc, + NULL + }; + +int attachTestInit(TestParams *testParams) +{ + /* nothing for now */ + return 0; +} + +static uint8 bogusKeyBits[] = {0, 1, 2, 3}; +static CSSM_VERSION vers = {2, 0}; + +static CSSM_RETURN attachMod( + const CSSM_GUID *guid, + CSSM_SERVICE_TYPE svc, + CSSM_MODULE_HANDLE_PTR hand) +{ + CSSM_RETURN crtn = CSSM_ModuleLoad(guid, + CSSM_KEY_HIERARCHY_NONE, + NULL, // eventHandler + NULL); // AppNotifyCallbackCtx + if(crtn) { + return crtn; + } + return CSSM_ModuleAttach(guid, + &vers, + &memFuncs, // memFuncs + 0, // SubserviceID + svc, // SubserviceFlags + 0, // AttachFlags + CSSM_KEY_HIERARCHY_NONE, + NULL, // FunctionTable + 0, // NumFuncTable + NULL, // reserved + hand); +} + +static int detachUnload( + CSSM_HANDLE hand, + const CSSM_GUID *guid) +{ + CSSM_RETURN crtn = CSSM_ModuleDetach(hand); + if(crtn) { + cssmPerror("CSSM_ModuleDetach", crtn); + return crtn; + } + #if DO_UNLOAD + crtn = CSSM_ModuleUnload(guid, NULL, NULL); + if(crtn) { + cssmPerror("CSSM_ModuleUnload", crtn); + } + #endif + return crtn; +} + +int attachTest(TestParams *testParams) +{ + unsigned loop; + CSSM_RETURN crtn; + CSSM_CSP_HANDLE cspHand[ATTACHES_PER_LOOP]; + CSSM_CL_HANDLE clHand[ATTACHES_PER_LOOP]; + CSSM_TP_HANDLE tpHand[ATTACHES_PER_LOOP]; + #if CSPDL_ENABLE + CSSM_DL_HANDLE dlHand[ATTACHES_PER_LOOP]; + CSSM_CSP_HANDLE cspDlHand[ATTACHES_PER_LOOP]; + #endif + CSSM_CC_HANDLE ccHand[ATTACHES_PER_LOOP]; + unsigned dex; + CSSM_KEY bogusKey; + + memset(cspHand, 0, ATTACHES_PER_LOOP * sizeof(CSSM_CSP_HANDLE)); + memset(ccHand, 0, ATTACHES_PER_LOOP * sizeof(CSSM_CC_HANDLE)); + + /* set up a bogus keyÊwhich the CSP won't even see */ + memset(&bogusKey, 0, sizeof(CSSM_KEY)); + bogusKey.KeyData.Data = bogusKeyBits; + bogusKey.KeyData.Length = sizeof(bogusKeyBits); + + for(loop=0; loopnumLoops; loop++) { + if(testParams->verbose) { + printf("attachTest thread %d: loop %d\n", + testParams->threadNum, loop); + } + else if(!testParams->quiet) { + printChar(testParams->progressChar); + } + + if(ATTACH_EACH_LOOP) { + /* 'n' attaches, skipping load (which has known leaks) */ + for(dex=0; dexcspHand; + } + + switch(dex & 3) { + case 0: + /* symmetric context */ + ccHand[dex] = genCryptHandle(curCspHand, + CSSM_ALGID_DES, + CSSM_ALGMODE_NONE, + CSSM_PADDING_NONE, + &bogusKey, + NULL, // key2 + NULL, // IV + 0, // effectiveKeySizeInBits + 0); // rounds + break; + case 1: + /* asymmetric context */ + ccHand[dex] = genCryptHandle(curCspHand, + CSSM_ALGID_RSA, + CSSM_ALGMODE_NONE, + CSSM_PADDING_NONE, + &bogusKey, + NULL, // key2 + NULL, // IV + 0, // effectiveKeySizeInBits + 0); // rounds + break; + case 2: + /* Digest */ + crtn = CSSM_CSP_CreateDigestContext(curCspHand, + CSSM_ALGID_SHA1, + &ccHand[dex]); + if(crtn) { + printError("CSSM_CSP_CreateDigestContext", crtn); + ccHand[dex] = CSSM_INVALID_HANDLE; + } + break; + case 3: + /* Digest */ + crtn = CSSM_CSP_CreateSignatureContext(curCspHand, + CSSM_ALGID_SHA1WithRSA, + NULL, // AccessCred + &bogusKey, + &ccHand[dex]); + if(crtn) { + printError("CSSM_CSP_CreateSignatureContext", crtn); + ccHand[dex] = CSSM_INVALID_HANDLE; + } + break; + } + if(curCspHand == CSSM_INVALID_HANDLE) { + return 1; + } + } + + /* free handles */ + for(dex=0; dex