security/nss/lib/dbm/tests/lots.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /* use sequental numbers printed to strings
michael@0 7 * to store lots and lots of entries in the
michael@0 8 * database.
michael@0 9 *
michael@0 10 * Start with 100 entries, put them and then
michael@0 11 * read them out. Then delete the first
michael@0 12 * half and verify that all of the first half
michael@0 13 * is gone and then verify that the second
michael@0 14 * half is still there.
michael@0 15 * Then add the first half back and verify
michael@0 16 * again. Then delete the middle third
michael@0 17 * and verify again.
michael@0 18 * Then increase the size by 1000 and do
michael@0 19 * the whole add delete thing again.
michael@0 20 *
michael@0 21 * The data for each object is the number string translated
michael@0 22 * to hex and replicated a random number of times. The
michael@0 23 * number of times that the data is replicated is the first
michael@0 24 * int32 in the data.
michael@0 25 */
michael@0 26
michael@0 27 #include <stdio.h>
michael@0 28
michael@0 29 #include <stdlib.h>
michael@0 30 #ifdef STDC_HEADERS
michael@0 31 #include <stdarg.h>
michael@0 32 #else
michael@0 33 #include <varargs.h>
michael@0 34 #endif
michael@0 35
michael@0 36 #ifdef HAVE_MEMORY_H
michael@0 37 #include <memory.h>
michael@0 38 #endif
michael@0 39 #include <string.h>
michael@0 40 #include <assert.h>
michael@0 41 #include "mcom_db.h"
michael@0 42
michael@0 43 DB *database=0;
michael@0 44 int MsgPriority=5;
michael@0 45
michael@0 46 #if defined(_WINDOWS) && !defined(WIN32)
michael@0 47 #define int32 long
michael@0 48 #define uint32 unsigned long
michael@0 49 #else
michael@0 50 #define int32 int
michael@0 51 #define uint32 unsigned int
michael@0 52 #endif
michael@0 53
michael@0 54 typedef enum {
michael@0 55 USE_LARGE_KEY,
michael@0 56 USE_SMALL_KEY
michael@0 57 } key_type_enum;
michael@0 58
michael@0 59 #define TraceMe(priority, msg) \
michael@0 60 do { \
michael@0 61 if(priority <= MsgPriority) \
michael@0 62 { \
michael@0 63 ReportStatus msg; \
michael@0 64 } \
michael@0 65 } while(0)
michael@0 66
michael@0 67 int
michael@0 68 ReportStatus(char *string, ...)
michael@0 69 {
michael@0 70 va_list args;
michael@0 71
michael@0 72 #ifdef STDC_HEADERS
michael@0 73 va_start(args, string);
michael@0 74 #else
michael@0 75 va_start(args);
michael@0 76 #endif
michael@0 77 vfprintf(stderr, string, args);
michael@0 78 va_end(args);
michael@0 79
michael@0 80 fprintf (stderr, "\n");
michael@0 81
michael@0 82 return(0);
michael@0 83 }
michael@0 84
michael@0 85 int
michael@0 86 ReportError(char *string, ...)
michael@0 87 {
michael@0 88 va_list args;
michael@0 89
michael@0 90 #ifdef STDC_HEADERS
michael@0 91 va_start(args, string);
michael@0 92 #else
michael@0 93 va_start(args);
michael@0 94 #endif
michael@0 95 fprintf (stderr, "\n ");
michael@0 96 vfprintf(stderr, string, args);
michael@0 97 fprintf (stderr, "\n");
michael@0 98 va_end(args);
michael@0 99
michael@0 100 return(0);
michael@0 101 }
michael@0 102
michael@0 103 DBT * MakeLargeKey(int32 num)
michael@0 104 {
michael@0 105 int32 low_bits;
michael@0 106 static DBT rv;
michael@0 107 static char *string_rv=0;
michael@0 108 int rep_char;
michael@0 109 size_t size;
michael@0 110
michael@0 111 if(string_rv)
michael@0 112 free(string_rv);
michael@0 113
michael@0 114 /* generate a really large text key derived from
michael@0 115 * an int32
michael@0 116 */
michael@0 117 low_bits = (num % 10000) + 1;
michael@0 118
michael@0 119 /* get the repeat char from the low 26 */
michael@0 120 rep_char = (char) ((low_bits % 26) + 'a');
michael@0 121
michael@0 122 /* malloc a string low_bits wide */
michael@0 123 size = low_bits*sizeof(char);
michael@0 124 string_rv = (char *)malloc(size);
michael@0 125
michael@0 126 memset(string_rv, rep_char, size);
michael@0 127
michael@0 128 rv.data = string_rv;
michael@0 129 rv.size = size;
michael@0 130
michael@0 131 return(&rv);
michael@0 132 }
michael@0 133
michael@0 134 DBT * MakeSmallKey(int32 num)
michael@0 135 {
michael@0 136 static DBT rv;
michael@0 137 static char data_string[64];
michael@0 138
michael@0 139 rv.data = data_string;
michael@0 140
michael@0 141 sprintf(data_string, "%ld", (long)num);
michael@0 142 rv.size = strlen(data_string);
michael@0 143
michael@0 144 return(&rv);
michael@0 145
michael@0 146 }
michael@0 147
michael@0 148 DBT * GenKey(int32 num, key_type_enum key_type)
michael@0 149 {
michael@0 150 DBT *key;
michael@0 151
michael@0 152 switch(key_type)
michael@0 153 {
michael@0 154 case USE_LARGE_KEY:
michael@0 155 key = MakeLargeKey(num);
michael@0 156 break;
michael@0 157 case USE_SMALL_KEY:
michael@0 158 key = MakeSmallKey(num);
michael@0 159 break;
michael@0 160 default:
michael@0 161 abort();
michael@0 162 break;
michael@0 163 }
michael@0 164
michael@0 165 return(key);
michael@0 166 }
michael@0 167
michael@0 168 int
michael@0 169 SeqDatabase()
michael@0 170 {
michael@0 171 int status;
michael@0 172 DBT key, data;
michael@0 173
michael@0 174 ReportStatus("SEQuencing through database...");
michael@0 175
michael@0 176 /* seq through the whole database */
michael@0 177 if(!(status = (*database->seq)(database, &key, &data, R_FIRST)))
michael@0 178 {
michael@0 179 while(!(status = (database->seq) (database, &key, &data, R_NEXT)))
michael@0 180 ; /* null body */
michael@0 181 }
michael@0 182
michael@0 183 if(status < 0)
michael@0 184 ReportError("Error seq'ing database");
michael@0 185
michael@0 186 return(status);
michael@0 187 }
michael@0 188
michael@0 189 int
michael@0 190 VerifyData(DBT *data, int32 num, key_type_enum key_type)
michael@0 191 {
michael@0 192 int32 count, compare_num;
michael@0 193 size_t size;
michael@0 194 int32 *int32_array;
michael@0 195
michael@0 196 /* The first int32 is count
michael@0 197 * The other n entries should
michael@0 198 * all equal num
michael@0 199 */
michael@0 200 if(data->size < sizeof(int32))
michael@0 201 {
michael@0 202 ReportError("Data size corrupted");
michael@0 203 return -1;
michael@0 204 }
michael@0 205
michael@0 206 memcpy(&count, data->data, sizeof(int32));
michael@0 207
michael@0 208 size = sizeof(int32)*(count+1);
michael@0 209
michael@0 210 if(size != data->size)
michael@0 211 {
michael@0 212 ReportError("Data size corrupted");
michael@0 213 return -1;
michael@0 214 }
michael@0 215
michael@0 216 int32_array = (int32*)data->data;
michael@0 217
michael@0 218 for(;count > 0; count--)
michael@0 219 {
michael@0 220 memcpy(&compare_num, &int32_array[count], sizeof(int32));
michael@0 221
michael@0 222 if(compare_num != num)
michael@0 223 {
michael@0 224 ReportError("Data corrupted");
michael@0 225 return -1;
michael@0 226 }
michael@0 227 }
michael@0 228
michael@0 229 return(0);
michael@0 230 }
michael@0 231
michael@0 232
michael@0 233 /* verify that a range of number strings exist
michael@0 234 * or don't exist. And that the data is valid
michael@0 235 */
michael@0 236 #define SHOULD_EXIST 1
michael@0 237 #define SHOULD_NOT_EXIST 0
michael@0 238 int
michael@0 239 VerifyRange(int32 low, int32 high, int32 should_exist, key_type_enum key_type)
michael@0 240 {
michael@0 241 DBT *key, data;
michael@0 242 int32 num;
michael@0 243 int status;
michael@0 244
michael@0 245 TraceMe(1, ("Verifying: %ld to %ld, using %s keys",
michael@0 246 low, high, key_type == USE_SMALL_KEY ? "SMALL" : "LARGE"));
michael@0 247
michael@0 248 for(num = low; num <= high; num++)
michael@0 249 {
michael@0 250
michael@0 251 key = GenKey(num, key_type);
michael@0 252
michael@0 253 status = (*database->get)(database, key, &data, 0);
michael@0 254
michael@0 255 if(status == 0)
michael@0 256 {
michael@0 257 /* got the item */
michael@0 258 if(!should_exist)
michael@0 259 {
michael@0 260 ReportError("Item exists but shouldn't: %ld", num);
michael@0 261 }
michael@0 262 else
michael@0 263 {
michael@0 264 /* else verify the data */
michael@0 265 VerifyData(&data, num, key_type);
michael@0 266 }
michael@0 267 }
michael@0 268 else if(status > 0)
michael@0 269 {
michael@0 270 /* item not found */
michael@0 271 if(should_exist)
michael@0 272 {
michael@0 273 ReportError("Item not found but should be: %ld", num);
michael@0 274 }
michael@0 275 }
michael@0 276 else
michael@0 277 {
michael@0 278 /* database error */
michael@0 279 ReportError("Database error");
michael@0 280 return(-1);
michael@0 281 }
michael@0 282
michael@0 283 }
michael@0 284
michael@0 285 TraceMe(1, ("Correctly verified: %ld to %ld", low, high));
michael@0 286
michael@0 287 return(0);
michael@0 288
michael@0 289 }
michael@0 290
michael@0 291 DBT *
michael@0 292 GenData(int32 num)
michael@0 293 {
michael@0 294 int32 n;
michael@0 295 static DBT *data=0;
michael@0 296 int32 *int32_array;
michael@0 297 size_t size;
michael@0 298
michael@0 299 if(!data)
michael@0 300 {
michael@0 301 data = (DBT*)malloc(sizeof(DBT));
michael@0 302 data->size = 0;
michael@0 303 data->data = 0;
michael@0 304 }
michael@0 305 else if(data->data)
michael@0 306 {
michael@0 307 free(data->data);
michael@0 308 }
michael@0 309
michael@0 310 n = rand();
michael@0 311
michael@0 312 n = n % 512; /* bound to a 2K size */
michael@0 313
michael@0 314
michael@0 315 size = sizeof(int32)*(n+1);
michael@0 316 int32_array = (int32 *) malloc(size);
michael@0 317
michael@0 318 memcpy(&int32_array[0], &n, sizeof(int32));
michael@0 319
michael@0 320 for(; n > 0; n--)
michael@0 321 {
michael@0 322 memcpy(&int32_array[n], &num, sizeof(int32));
michael@0 323 }
michael@0 324
michael@0 325 data->data = (void*)int32_array;
michael@0 326 data->size = size;
michael@0 327
michael@0 328 return(data);
michael@0 329 }
michael@0 330
michael@0 331 #define ADD_RANGE 1
michael@0 332 #define DELETE_RANGE 2
michael@0 333
michael@0 334 int
michael@0 335 AddOrDelRange(int32 low, int32 high, int action, key_type_enum key_type)
michael@0 336 {
michael@0 337 DBT *key, *data;
michael@0 338 #if 0 /* only do this if your really analy checking the puts */
michael@0 339 DBT tmp_data;
michael@0 340 #endif
michael@0 341 int32 num;
michael@0 342 int status;
michael@0 343
michael@0 344 if(action != ADD_RANGE && action != DELETE_RANGE)
michael@0 345 assert(0);
michael@0 346
michael@0 347 if(action == ADD_RANGE)
michael@0 348 {
michael@0 349 TraceMe(1, ("Adding: %ld to %ld: %s keys", low, high,
michael@0 350 key_type == USE_SMALL_KEY ? "SMALL" : "LARGE"));
michael@0 351 }
michael@0 352 else
michael@0 353 {
michael@0 354 TraceMe(1, ("Deleting: %ld to %ld: %s keys", low, high,
michael@0 355 key_type == USE_SMALL_KEY ? "SMALL" : "LARGE"));
michael@0 356 }
michael@0 357
michael@0 358 for(num = low; num <= high; num++)
michael@0 359 {
michael@0 360
michael@0 361 key = GenKey(num, key_type);
michael@0 362
michael@0 363 if(action == ADD_RANGE)
michael@0 364 {
michael@0 365 data = GenData(num);
michael@0 366 status = (*database->put)(database, key, data, 0);
michael@0 367 }
michael@0 368 else
michael@0 369 {
michael@0 370 status = (*database->del)(database, key, 0);
michael@0 371 }
michael@0 372
michael@0 373 if(status < 0)
michael@0 374 {
michael@0 375 ReportError("Database error %s item: %ld",
michael@0 376 action == ADD_RANGE ? "ADDING" : "DELETING",
michael@0 377 num);
michael@0 378 }
michael@0 379 else if(status > 0)
michael@0 380 {
michael@0 381 ReportError("Could not %s item: %ld",
michael@0 382 action == ADD_RANGE ? "ADD" : "DELETE",
michael@0 383 num);
michael@0 384 }
michael@0 385 else if(action == ADD_RANGE)
michael@0 386 {
michael@0 387 #define SYNC_EVERY_TIME
michael@0 388 #ifdef SYNC_EVERY_TIME
michael@0 389 status = (*database->sync)(database, 0);
michael@0 390 if(status != 0)
michael@0 391 ReportError("Database error syncing after add");
michael@0 392 #endif
michael@0 393
michael@0 394 #if 0 /* only do this if your really analy checking the puts */
michael@0 395
michael@0 396 /* make sure we can still get it
michael@0 397 */
michael@0 398 status = (*database->get)(database, key, &tmp_data, 0);
michael@0 399
michael@0 400 if(status != 0)
michael@0 401 {
michael@0 402 ReportError("Database error checking item just added: %d",
michael@0 403 num);
michael@0 404 }
michael@0 405 else
michael@0 406 {
michael@0 407 /* now verify that none of the ones we already
michael@0 408 * put in have disappeared
michael@0 409 */
michael@0 410 VerifyRange(low, num, SHOULD_EXIST, key_type);
michael@0 411 }
michael@0 412 #endif
michael@0 413
michael@0 414 }
michael@0 415 }
michael@0 416
michael@0 417
michael@0 418 if(action == ADD_RANGE)
michael@0 419 {
michael@0 420 TraceMe(1, ("Successfully added: %ld to %ld", low, high));
michael@0 421 }
michael@0 422 else
michael@0 423 {
michael@0 424 TraceMe(1, ("Successfully deleted: %ld to %ld", low, high));
michael@0 425 }
michael@0 426
michael@0 427 return(0);
michael@0 428 }
michael@0 429
michael@0 430 int
michael@0 431 TestRange(int32 low, int32 range, key_type_enum key_type)
michael@0 432 {
michael@0 433 int status; int32 low_of_range1, high_of_range1; int32 low_of_range2, high_of_range2;
michael@0 434 int32 low_of_range3, high_of_range3;
michael@0 435
michael@0 436 status = AddOrDelRange(low, low+range, ADD_RANGE, key_type);
michael@0 437 status = VerifyRange(low, low+range, SHOULD_EXIST, key_type);
michael@0 438
michael@0 439 TraceMe(1, ("Finished with sub test 1"));
michael@0 440
michael@0 441 SeqDatabase();
michael@0 442
michael@0 443 low_of_range1 = low;
michael@0 444 high_of_range1 = low+(range/2);
michael@0 445 low_of_range2 = high_of_range1+1;
michael@0 446 high_of_range2 = low+range;
michael@0 447 status = AddOrDelRange(low_of_range1, high_of_range1, DELETE_RANGE, key_type);
michael@0 448 status = VerifyRange(low_of_range1, high_of_range1, SHOULD_NOT_EXIST, key_type);
michael@0 449 status = VerifyRange(low_of_range2, low_of_range2, SHOULD_EXIST, key_type);
michael@0 450
michael@0 451 TraceMe(1, ("Finished with sub test 2"));
michael@0 452
michael@0 453 SeqDatabase();
michael@0 454
michael@0 455 status = AddOrDelRange(low_of_range1, high_of_range1, ADD_RANGE, key_type);
michael@0 456 /* the whole thing should exist now */
michael@0 457 status = VerifyRange(low, low+range, SHOULD_EXIST, key_type);
michael@0 458
michael@0 459 TraceMe(1, ("Finished with sub test 3"));
michael@0 460
michael@0 461 SeqDatabase();
michael@0 462
michael@0 463 status = AddOrDelRange(low_of_range2, high_of_range2, DELETE_RANGE, key_type);
michael@0 464 status = VerifyRange(low_of_range1, high_of_range1, SHOULD_EXIST, key_type);
michael@0 465 status = VerifyRange(low_of_range2, high_of_range2, SHOULD_NOT_EXIST, key_type);
michael@0 466
michael@0 467 TraceMe(1, ("Finished with sub test 4"));
michael@0 468
michael@0 469 SeqDatabase();
michael@0 470
michael@0 471 status = AddOrDelRange(low_of_range2, high_of_range2, ADD_RANGE, key_type);
michael@0 472 /* the whole thing should exist now */
michael@0 473 status = VerifyRange(low, low+range, SHOULD_EXIST, key_type);
michael@0 474
michael@0 475 TraceMe(1, ("Finished with sub test 5"));
michael@0 476
michael@0 477 SeqDatabase();
michael@0 478
michael@0 479 low_of_range1 = low;
michael@0 480 high_of_range1 = low+(range/3);
michael@0 481 low_of_range2 = high_of_range1+1;
michael@0 482 high_of_range2 = high_of_range1+(range/3);
michael@0 483 low_of_range3 = high_of_range2+1;
michael@0 484 high_of_range3 = low+range;
michael@0 485 /* delete range 2 */
michael@0 486 status = AddOrDelRange(low_of_range2, high_of_range2, DELETE_RANGE, key_type);
michael@0 487 status = VerifyRange(low_of_range1, high_of_range1, SHOULD_EXIST, key_type);
michael@0 488 status = VerifyRange(low_of_range2, low_of_range2, SHOULD_NOT_EXIST, key_type);
michael@0 489 status = VerifyRange(low_of_range3, low_of_range2, SHOULD_EXIST, key_type);
michael@0 490
michael@0 491 TraceMe(1, ("Finished with sub test 6"));
michael@0 492
michael@0 493 SeqDatabase();
michael@0 494
michael@0 495 status = AddOrDelRange(low_of_range2, high_of_range2, ADD_RANGE, key_type);
michael@0 496 /* the whole thing should exist now */
michael@0 497 status = VerifyRange(low, low+range, SHOULD_EXIST, key_type);
michael@0 498
michael@0 499 TraceMe(1, ("Finished with sub test 7"));
michael@0 500
michael@0 501 return(0);
michael@0 502 }
michael@0 503
michael@0 504 #define START_RANGE 109876
michael@0 505 int
michael@0 506 main(int argc, char **argv)
michael@0 507 {
michael@0 508 int32 i, j=0;
michael@0 509 int quick_exit = 0;
michael@0 510 int large_keys = 0;
michael@0 511 HASHINFO hash_info = {
michael@0 512 16*1024,
michael@0 513 0,
michael@0 514 0,
michael@0 515 0,
michael@0 516 0,
michael@0 517 0};
michael@0 518
michael@0 519
michael@0 520 if(argc > 1)
michael@0 521 {
michael@0 522 while(argc > 1)
michael@0 523 {
michael@0 524 if(!strcmp(argv[argc-1], "-quick"))
michael@0 525 quick_exit = 1;
michael@0 526 else if(!strcmp(argv[argc-1], "-large"))
michael@0 527 {
michael@0 528 large_keys = 1;
michael@0 529 }
michael@0 530 argc--;
michael@0 531 }
michael@0 532 }
michael@0 533
michael@0 534 database = dbopen("test.db", O_RDWR | O_CREAT, 0644, DB_HASH, &hash_info);
michael@0 535
michael@0 536 if(!database)
michael@0 537 {
michael@0 538 ReportError("Could not open database");
michael@0 539 #ifdef unix
michael@0 540 perror("");
michael@0 541 #endif
michael@0 542 exit(1);
michael@0 543 }
michael@0 544
michael@0 545 if(quick_exit)
michael@0 546 {
michael@0 547 if(large_keys)
michael@0 548 TestRange(START_RANGE, 200, USE_LARGE_KEY);
michael@0 549 else
michael@0 550 TestRange(START_RANGE, 200, USE_SMALL_KEY);
michael@0 551
michael@0 552 (*database->sync)(database, 0);
michael@0 553 (*database->close)(database);
michael@0 554 exit(0);
michael@0 555 }
michael@0 556
michael@0 557 for(i=100; i < 10000000; i+=200)
michael@0 558 {
michael@0 559 if(1 || j)
michael@0 560 {
michael@0 561 TestRange(START_RANGE, i, USE_LARGE_KEY);
michael@0 562 j = 0;
michael@0 563 }
michael@0 564 else
michael@0 565 {
michael@0 566 TestRange(START_RANGE, i, USE_SMALL_KEY);
michael@0 567 j = 1;
michael@0 568 }
michael@0 569
michael@0 570 if(1 == rand() % 3)
michael@0 571 {
michael@0 572 (*database->sync)(database, 0);
michael@0 573 }
michael@0 574
michael@0 575 if(1 == rand() % 3)
michael@0 576 {
michael@0 577 /* close and reopen */
michael@0 578 (*database->close)(database);
michael@0 579 database = dbopen("test.db", O_RDWR | O_CREAT, 0644, DB_HASH, 0);
michael@0 580 if(!database)
michael@0 581 {
michael@0 582 ReportError("Could not reopen database");
michael@0 583 #ifdef unix
michael@0 584 perror("");
michael@0 585 #endif
michael@0 586 exit(1);
michael@0 587 }
michael@0 588 }
michael@0 589 else
michael@0 590 {
michael@0 591 /* reopen database without closeing the other */
michael@0 592 database = dbopen("test.db", O_RDWR | O_CREAT, 0644, DB_HASH, 0);
michael@0 593 if(!database)
michael@0 594 {
michael@0 595 ReportError("Could not reopen database "
michael@0 596 "after not closing the other");
michael@0 597 #ifdef unix
michael@0 598 perror("");
michael@0 599 #endif
michael@0 600 exit(1);
michael@0 601 }
michael@0 602 }
michael@0 603 }
michael@0 604
michael@0 605 return(0);
michael@0 606 }

mercurial