2 * attach/creatContext/deleteContext/detach test
4 #include "testParams.h"
5 #include <Security/cssm.h>
10 #include <utilLib/common.h>
11 #include <utilLib/cspwrap.h>
13 /* for memory leak debug only, with only one thread running */
16 #define ATTACHES_PER_LOOP 100
17 #define ATTACH_EACH_LOOP 1 /* 1 ==> ATTACHES_PER_LOOP moduleAttaches */
18 /* 0 ==> all thru testParams->cspHand */
19 #define CONTEXT_EACH_LOOP 1 /* 1 ==> CSSM_CSP_Create*Context for each attach */
20 /* 0 ==> just do attach/detach */
21 #define CSPDL_ENABLE 1 /* attach to CSP and DL sides of CSPDL */
22 #define DO_UNLOAD 0 /* enable CSSM_ModuleUnload() */
24 #if (!ATTACH_EACH_LOOP && !CONTEXT_EACH_LOOP)
25 #error Hey! Must configure for attach and/or createContext!
28 static CSSM_API_MEMORY_FUNCS memFuncs
= {
36 int attachTestInit(TestParams
*testParams
)
42 static uint8 bogusKeyBits
[] = {0, 1, 2, 3};
43 static CSSM_VERSION vers
= {2, 0};
45 static CSSM_RETURN
attachMod(
46 const CSSM_GUID
*guid
,
47 CSSM_SERVICE_TYPE svc
,
48 CSSM_MODULE_HANDLE_PTR hand
)
50 CSSM_RETURN crtn
= CSSM_ModuleLoad(guid
,
51 CSSM_KEY_HIERARCHY_NONE
,
53 NULL
); // AppNotifyCallbackCtx
57 return CSSM_ModuleAttach(guid
,
59 &memFuncs
, // memFuncs
61 svc
, // SubserviceFlags
63 CSSM_KEY_HIERARCHY_NONE
,
64 NULL
, // FunctionTable
70 static int detachUnload(
72 const CSSM_GUID
*guid
)
74 CSSM_RETURN crtn
= CSSM_ModuleDetach(hand
);
76 cssmPerror("CSSM_ModuleDetach", crtn
);
80 crtn
= CSSM_ModuleUnload(guid
, NULL
, NULL
);
82 cssmPerror("CSSM_ModuleUnload", crtn
);
88 int attachTest(TestParams
*testParams
)
92 CSSM_CSP_HANDLE cspHand
[ATTACHES_PER_LOOP
];
93 CSSM_CL_HANDLE clHand
[ATTACHES_PER_LOOP
];
94 CSSM_TP_HANDLE tpHand
[ATTACHES_PER_LOOP
];
96 CSSM_DL_HANDLE dlHand
[ATTACHES_PER_LOOP
];
97 CSSM_CSP_HANDLE cspDlHand
[ATTACHES_PER_LOOP
];
99 CSSM_CC_HANDLE ccHand
[ATTACHES_PER_LOOP
];
103 memset(cspHand
, 0, ATTACHES_PER_LOOP
* sizeof(CSSM_CSP_HANDLE
));
104 memset(ccHand
, 0, ATTACHES_PER_LOOP
* sizeof(CSSM_CC_HANDLE
));
106 /* set up a bogus keyÊwhich the CSP won't even see */
107 memset(&bogusKey
, 0, sizeof(CSSM_KEY
));
108 bogusKey
.KeyData
.Data
= bogusKeyBits
;
109 bogusKey
.KeyData
.Length
= sizeof(bogusKeyBits
);
111 for(loop
=0; loop
<testParams
->numLoops
; loop
++) {
112 if(testParams
->verbose
) {
113 printf("attachTest thread %d: loop %d\n",
114 testParams
->threadNum
, loop
);
116 else if(!testParams
->quiet
) {
117 printChar(testParams
->progressChar
);
120 if(ATTACH_EACH_LOOP
) {
121 /* 'n' attaches, skipping load (which has known leaks) */
122 for(dex
=0; dex
<ATTACHES_PER_LOOP
; dex
++) {
123 crtn
= attachMod(&gGuidAppleCSP
,
124 CSSM_SERVICE_CSP
, &cspHand
[dex
]);
126 printError("CSSM_ModuleAttach(CSP)", crtn
);
129 crtn
= attachMod(&gGuidAppleX509CL
,
130 CSSM_SERVICE_CL
, &clHand
[dex
]);
132 printError("CSSM_ModuleAttach(CL)", crtn
);
135 crtn
= attachMod(&gGuidAppleX509TP
,
136 CSSM_SERVICE_TP
, &tpHand
[dex
]);
138 printError("CSSM_ModuleAttach(TP)", crtn
);
142 crtn
= attachMod(&gGuidAppleCSPDL
,
143 CSSM_SERVICE_DL
, &dlHand
[dex
]);
145 printError("CSSM_ModuleAttach(DL)", crtn
);
148 crtn
= attachMod(&gGuidAppleCSPDL
,
149 CSSM_SERVICE_CSP
, &cspDlHand
[dex
]);
151 printError("CSSM_ModuleAttach(CSPDL)", crtn
);
158 /* now one of various crypt handles */
159 if(CONTEXT_EACH_LOOP
) {
160 for(dex
=0; dex
<ATTACHES_PER_LOOP
; dex
++) {
161 CSSM_CSP_HANDLE curCspHand
;
162 if(ATTACH_EACH_LOOP
) {
163 curCspHand
= cspHand
[dex
];
166 curCspHand
= testParams
->cspHand
;
171 /* symmetric context */
172 ccHand
[dex
] = genCryptHandle(curCspHand
,
179 0, // effectiveKeySizeInBits
183 /* asymmetric context */
184 ccHand
[dex
] = genCryptHandle(curCspHand
,
191 0, // effectiveKeySizeInBits
196 crtn
= CSSM_CSP_CreateDigestContext(curCspHand
,
200 printError("CSSM_CSP_CreateDigestContext", crtn
);
201 ccHand
[dex
] = CSSM_INVALID_HANDLE
;
206 crtn
= CSSM_CSP_CreateSignatureContext(curCspHand
,
207 CSSM_ALGID_SHA1WithRSA
,
212 printError("CSSM_CSP_CreateSignatureContext", crtn
);
213 ccHand
[dex
] = CSSM_INVALID_HANDLE
;
217 if(curCspHand
== CSSM_INVALID_HANDLE
) {
223 for(dex
=0; dex
<ATTACHES_PER_LOOP
; dex
++) {
224 crtn
= CSSM_DeleteContext(ccHand
[dex
]);
226 printError("CSSM_DeleteContext", crtn
);
232 if(ATTACH_EACH_LOOP
) {
234 for(dex
=0; dex
<ATTACHES_PER_LOOP
; dex
++) {
236 crtn
= detachUnload(cspHand
[dex
], &gGuidAppleCSP
);
240 crtn
= detachUnload(clHand
[dex
], &gGuidAppleX509CL
);
244 crtn
= detachUnload(tpHand
[dex
], &gGuidAppleX509TP
);
249 crtn
= detachUnload(dlHand
[dex
], &gGuidAppleCSPDL
);
253 crtn
= detachUnload(cspDlHand
[dex
], &gGuidAppleCSPDL
);
264 printf("Hit CR to proceed: ");