1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 /*
5  * stress_test.c
6  *
7  * Creates and deletes many objects
8  *
9  */
10 
11 #include "testutil.h"
12 #include "testutil_nss.h"
13 
14 static void *plContext = NULL;
15 
16 int
stress_test(int argc,char * argv[])17 stress_test(int argc, char *argv[])
18 {
19 
20     PKIX_UInt32 i, k, length, hashcode;
21     PKIX_UInt32 size = 17576;
22     char temp[4];
23     PKIX_Boolean result;
24     PKIX_PL_String *strings[17576], *tempString;
25     PKIX_PL_String *utf16strings[17576];
26     PKIX_PL_ByteArray *byteArrays[17576];
27     void *dest;
28     PKIX_PL_HashTable *ht = NULL;
29     PKIX_UInt32 actualMinorVersion;
30     PKIX_UInt32 j = 0;
31 
32     PKIX_TEST_STD_VARS();
33 
34     startTests("Stress Test");
35 
36     PKIX_TEST_EXPECT_NO_ERROR(
37         PKIX_PL_NssContext_Create(0, PKIX_FALSE, NULL, &plContext));
38 
39     /* ---------------------------- */
40     subTest("Create every three letter String");
41 
42     for (i = 0; i < 26; i++)
43         for (j = 0; j < 26; j++)
44             for (k = 0; k < 26; k++) {
45                 temp[0] = (char)('a' + i);
46                 temp[1] = (char)('a' + j);
47                 temp[2] = (char)('a' + k);
48                 temp[3] = 0;
49                 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(PKIX_ESCASCII, temp, 3,
50                                                                 &strings[26 * (i * 26 + j) + k],
51                                                                 plContext));
52             }
53 
54     /* ---------------------------- */
55     subTest("Create a bytearray from each string's UTF-16 encoding");
56     for (i = 0; i < size; i++) {
57         PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(strings[i],
58                                                             PKIX_UTF16,
59                                                             &dest,
60                                                             &length,
61                                                             plContext));
62 
63         PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_ByteArray_Create(dest, length, &byteArrays[i], plContext));
64         PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
65     }
66 
67     /* ---------------------------- */
68     subTest("Create a copy string from each bytearray");
69     for (i = 0; i < size; i++) {
70         PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(PKIX_UTF16, *(void **)byteArrays[i], 6,
71                                                         &utf16strings[i], plContext));
72     }
73 
74     /* ---------------------------- */
75     subTest("Compare each original string with the copy");
76     for (i = 0; i < size; i++) {
77         PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Equals((PKIX_PL_Object *)strings[i],
78                                                         (PKIX_PL_Object *)utf16strings[i],
79                                                         &result,
80                                                         plContext));
81         if (result == 0)
82             testError("Strings do not match");
83     }
84 
85     /* ---------------------------- */
86     subTest("Put each string into a Hashtable");
87     PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_HashTable_Create(size /
88                                                            2,
89                                                        0, &ht, plContext));
90 
91     for (i = 0; i < size; i++) {
92         PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Hashcode((PKIX_PL_Object *)strings[i],
93                                                           &hashcode,
94                                                           plContext));
95 
96         PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_HashTable_Add(ht,
97                                                         (void *)&hashcode,
98                                                         (void *)strings[i],
99                                                         plContext));
100     }
101 
102     /* ---------------------------- */
103     subTest("Compare each copy string with the hashtable entries ");
104     for (i = 0; i < size; i++) {
105         PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Hashcode((PKIX_PL_Object *)utf16strings[i],
106                                                           &hashcode,
107                                                           plContext));
108 
109         PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_HashTable_Lookup(ht,
110                                                            (void *)&hashcode,
111                                                            (PKIX_PL_Object **)&tempString,
112                                                            plContext));
113 
114         if (tempString == NULL)
115             testError("String not found in hashtable");
116         else {
117             PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Equals((PKIX_PL_Object *)tempString,
118                                                             (PKIX_PL_Object *)utf16strings[i],
119                                                             &result,
120                                                             plContext));
121             if (result == 0)
122                 testError("Strings do not match");
123             PKIX_TEST_DECREF_BC(tempString);
124         }
125     }
126 
127 cleanup:
128 
129     /* ---------------------------- */
130     subTest("Destroy All Objects");
131 
132     PKIX_TEST_DECREF_AC(ht);
133 
134     for (i = 0; i < size; i++) {
135         PKIX_TEST_DECREF_AC(strings[i]);
136         PKIX_TEST_DECREF_AC(utf16strings[i]);
137         PKIX_TEST_DECREF_AC(byteArrays[i]);
138     }
139 
140     PKIX_Shutdown(plContext);
141 
142     PKIX_TEST_RETURN();
143 
144     endTests("Stress Test");
145     return (0);
146 }
147