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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/lib/dbm/tests/lots.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,606 @@
     1.4 +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/* use sequental numbers printed to strings
    1.10 + * to store lots and lots of entries in the
    1.11 + * database.
    1.12 + *
    1.13 + * Start with 100 entries, put them and then
    1.14 + * read them out.  Then delete the first
    1.15 + * half and verify that all of the first half
    1.16 + * is gone and then verify that the second
    1.17 + * half is still there.
    1.18 + * Then add the first half back and verify
    1.19 + * again.  Then delete the middle third
    1.20 + * and verify again.
    1.21 + * Then increase the size by 1000 and do
    1.22 + * the whole add delete thing again.
    1.23 + *
    1.24 + * The data for each object is the number string translated
    1.25 + * to hex and replicated a random number of times.  The
    1.26 + * number of times that the data is replicated is the first
    1.27 + * int32 in the data.
    1.28 + */
    1.29 +
    1.30 +#include <stdio.h>
    1.31 +
    1.32 +#include <stdlib.h>
    1.33 +#ifdef STDC_HEADERS
    1.34 +#include <stdarg.h>
    1.35 +#else
    1.36 +#include <varargs.h>
    1.37 +#endif
    1.38 +
    1.39 +#ifdef HAVE_MEMORY_H
    1.40 +#include <memory.h>
    1.41 +#endif
    1.42 +#include <string.h>
    1.43 +#include <assert.h>
    1.44 +#include "mcom_db.h"
    1.45 +
    1.46 +DB *database=0;
    1.47 +int MsgPriority=5;
    1.48 +
    1.49 +#if defined(_WINDOWS) && !defined(WIN32)
    1.50 +#define int32 long
    1.51 +#define uint32 unsigned long
    1.52 +#else
    1.53 +#define int32 int
    1.54 +#define uint32 unsigned int
    1.55 +#endif
    1.56 +
    1.57 +typedef enum {
    1.58 +USE_LARGE_KEY,
    1.59 +USE_SMALL_KEY
    1.60 +} key_type_enum;
    1.61 +
    1.62 +#define TraceMe(priority, msg) 		\
    1.63 +	do {							\
    1.64 +		if(priority <= MsgPriority)	\
    1.65 +		  {							\
    1.66 +			ReportStatus msg;		\
    1.67 +		  }							\
    1.68 +	} while(0)
    1.69 +
    1.70 +int
    1.71 +ReportStatus(char *string, ...)
    1.72 +{
    1.73 +    va_list args;
    1.74 +
    1.75 +#ifdef STDC_HEADERS
    1.76 +    va_start(args, string);
    1.77 +#else
    1.78 +    va_start(args);
    1.79 +#endif
    1.80 +    vfprintf(stderr, string, args);
    1.81 +    va_end(args);
    1.82 +
    1.83 +	fprintf (stderr, "\n");
    1.84 +
    1.85 +	return(0);
    1.86 +}
    1.87 +
    1.88 +int
    1.89 +ReportError(char *string, ...)
    1.90 +{
    1.91 +    va_list args;
    1.92 +
    1.93 +#ifdef STDC_HEADERS
    1.94 +    va_start(args, string);
    1.95 +#else
    1.96 +    va_start(args);
    1.97 +#endif
    1.98 +	fprintf (stderr, "\n	");
    1.99 +    vfprintf(stderr, string, args);
   1.100 +	fprintf (stderr, "\n");
   1.101 +    va_end(args);
   1.102 +
   1.103 +	return(0);
   1.104 +}
   1.105 +
   1.106 +DBT * MakeLargeKey(int32 num)
   1.107 +{
   1.108 +	int32 low_bits;
   1.109 +	static DBT rv;
   1.110 +	static char *string_rv=0;
   1.111 +	int rep_char;
   1.112 +	size_t size;
   1.113 +
   1.114 +	if(string_rv)
   1.115 +		free(string_rv);
   1.116 +
   1.117 +	/* generate a really large text key derived from
   1.118 +	 * an int32
   1.119 +	 */
   1.120 +	low_bits = (num % 10000) + 1;
   1.121 +
   1.122 +	/* get the repeat char from the low 26 */
   1.123 +	rep_char = (char) ((low_bits % 26) + 'a');
   1.124 +
   1.125 +	/* malloc a string low_bits wide */
   1.126 +	size = low_bits*sizeof(char);
   1.127 +	string_rv = (char *)malloc(size);
   1.128 +
   1.129 +	memset(string_rv, rep_char, size);
   1.130 +
   1.131 +	rv.data = string_rv;
   1.132 +	rv.size = size;
   1.133 +
   1.134 +	return(&rv);
   1.135 +}
   1.136 +
   1.137 +DBT * MakeSmallKey(int32 num)
   1.138 +{
   1.139 +	static DBT rv;
   1.140 +	static char data_string[64];
   1.141 +
   1.142 +	rv.data = data_string;
   1.143 +
   1.144 +	sprintf(data_string, "%ld", (long)num);
   1.145 +	rv.size = strlen(data_string);
   1.146 +
   1.147 +	return(&rv);
   1.148 +
   1.149 +}
   1.150 +
   1.151 +DBT * GenKey(int32 num, key_type_enum key_type)
   1.152 +{
   1.153 +	DBT *key;
   1.154 +
   1.155 +	switch(key_type)
   1.156 +	  {
   1.157 +		case USE_LARGE_KEY:
   1.158 +			key = MakeLargeKey(num);
   1.159 +			break;
   1.160 +		case USE_SMALL_KEY:
   1.161 +			key = MakeSmallKey(num);
   1.162 +			break;
   1.163 +		default:
   1.164 +			abort();
   1.165 +			break;
   1.166 +	  }
   1.167 +
   1.168 +	return(key);
   1.169 +}
   1.170 +
   1.171 +int
   1.172 +SeqDatabase()
   1.173 +{
   1.174 +	int status;
   1.175 +	DBT key, data;
   1.176 +
   1.177 +	ReportStatus("SEQuencing through database...");
   1.178 +
   1.179 +	/* seq through the whole database */
   1.180 +    if(!(status = (*database->seq)(database, &key, &data, R_FIRST)))
   1.181 +	  {
   1.182 +        while(!(status = (database->seq) (database, &key, &data, R_NEXT)))
   1.183 +			; /* null body */
   1.184 +	  }
   1.185 +
   1.186 +	if(status < 0)
   1.187 +		ReportError("Error seq'ing database");
   1.188 +
   1.189 +	return(status);
   1.190 +}
   1.191 +
   1.192 +int 
   1.193 +VerifyData(DBT *data, int32 num, key_type_enum key_type)
   1.194 +{
   1.195 +	int32 count, compare_num;
   1.196 +	size_t size;
   1.197 +	int32 *int32_array;
   1.198 +
   1.199 +	/* The first int32 is count 
   1.200 +	 * The other n entries should
   1.201 +	 * all equal num
   1.202 +	 */
   1.203 +	if(data->size < sizeof(int32))
   1.204 +	  {
   1.205 +		ReportError("Data size corrupted");
   1.206 +		return -1;
   1.207 +	  }
   1.208 +
   1.209 +	memcpy(&count, data->data, sizeof(int32));
   1.210 +
   1.211 +	size = sizeof(int32)*(count+1);
   1.212 +
   1.213 +	if(size != data->size)
   1.214 +	  {
   1.215 +		ReportError("Data size corrupted");
   1.216 +		return -1;
   1.217 +	  }
   1.218 +
   1.219 +	int32_array = (int32*)data->data;
   1.220 +
   1.221 +	for(;count > 0; count--)
   1.222 +	  {
   1.223 +		memcpy(&compare_num, &int32_array[count], sizeof(int32));
   1.224 +
   1.225 +		if(compare_num != num)
   1.226 +	      {
   1.227 +		    ReportError("Data corrupted");
   1.228 +		    return -1;
   1.229 +	      }
   1.230 +	  }
   1.231 +
   1.232 +	return(0);
   1.233 +}
   1.234 +
   1.235 +
   1.236 +/* verify that a range of number strings exist
   1.237 + * or don't exist. And that the data is valid
   1.238 + */
   1.239 +#define SHOULD_EXIST 1
   1.240 +#define SHOULD_NOT_EXIST 0
   1.241 +int
   1.242 +VerifyRange(int32 low, int32 high, int32 should_exist, key_type_enum key_type)
   1.243 +{
   1.244 +	DBT *key, data;
   1.245 +	int32 num;
   1.246 +	int status;
   1.247 +
   1.248 +	TraceMe(1, ("Verifying: %ld to %ld, using %s keys", 
   1.249 +		    low, high, key_type == USE_SMALL_KEY ? "SMALL" : "LARGE"));
   1.250 +
   1.251 +	for(num = low; num <= high; num++)
   1.252 +	  {
   1.253 +
   1.254 +		key = GenKey(num, key_type);
   1.255 +
   1.256 +		status = (*database->get)(database, key, &data, 0);
   1.257 +
   1.258 +		if(status == 0)
   1.259 +		  {
   1.260 +			/* got the item */
   1.261 +			if(!should_exist)
   1.262 +			  {
   1.263 +				ReportError("Item exists but shouldn't: %ld", num);
   1.264 +			  }
   1.265 +			else
   1.266 +			  {
   1.267 +			    /* else verify the data */
   1.268 +			    VerifyData(&data, num, key_type);
   1.269 +			  }
   1.270 +		  }
   1.271 +		else if(status > 0)
   1.272 +		  {
   1.273 +			/* item not found */
   1.274 +			if(should_exist)
   1.275 +			  {
   1.276 +				ReportError("Item not found but should be: %ld", num);
   1.277 +			  }
   1.278 +		  }
   1.279 +		else
   1.280 +		  {
   1.281 +			/* database error */
   1.282 +			ReportError("Database error");
   1.283 +			return(-1);
   1.284 +		  }
   1.285 +			
   1.286 +	  }
   1.287 +
   1.288 +	TraceMe(1, ("Correctly verified: %ld to %ld", low, high));
   1.289 +
   1.290 +	return(0);
   1.291 +
   1.292 +}
   1.293 +
   1.294 +DBT *
   1.295 +GenData(int32 num)
   1.296 +{
   1.297 +	int32 n;
   1.298 +	static DBT *data=0;
   1.299 +	int32 *int32_array;
   1.300 +	size_t size;
   1.301 +
   1.302 +	if(!data)
   1.303 +	  {
   1.304 +		data = (DBT*)malloc(sizeof(DBT));
   1.305 +		data->size = 0;
   1.306 +		data->data = 0;
   1.307 +	  }
   1.308 +	else if(data->data)
   1.309 +	  {
   1.310 +		free(data->data);
   1.311 +	  }
   1.312 +
   1.313 +	n = rand();
   1.314 +
   1.315 +	n = n % 512;  /* bound to a 2K size */
   1.316 +
   1.317 +	
   1.318 +	size = sizeof(int32)*(n+1);
   1.319 +	int32_array = (int32 *) malloc(size);
   1.320 +
   1.321 +	memcpy(&int32_array[0], &n, sizeof(int32));
   1.322 +
   1.323 +	for(; n > 0; n--)
   1.324 +	  {
   1.325 +		memcpy(&int32_array[n], &num, sizeof(int32));
   1.326 +	  }
   1.327 +
   1.328 +	data->data = (void*)int32_array;
   1.329 +	data->size = size;
   1.330 +
   1.331 +	return(data);
   1.332 +}
   1.333 +
   1.334 +#define ADD_RANGE 1
   1.335 +#define DELETE_RANGE 2
   1.336 +
   1.337 +int
   1.338 +AddOrDelRange(int32 low, int32 high, int action, key_type_enum key_type)
   1.339 +{
   1.340 +	DBT *key, *data;
   1.341 +#if 0 /* only do this if your really analy checking the puts */
   1.342 +	DBT tmp_data;
   1.343 +#endif 
   1.344 +	int32 num;
   1.345 +	int status;
   1.346 +
   1.347 +	if(action != ADD_RANGE && action != DELETE_RANGE)
   1.348 +		assert(0);
   1.349 +
   1.350 +	if(action == ADD_RANGE)
   1.351 +	  {
   1.352 +		TraceMe(1, ("Adding: %ld to %ld: %s keys", low, high,
   1.353 +		    	key_type == USE_SMALL_KEY ? "SMALL" : "LARGE"));
   1.354 +	  }
   1.355 +	else
   1.356 +	  {
   1.357 +		TraceMe(1, ("Deleting: %ld to %ld: %s keys", low, high,
   1.358 +		    	key_type == USE_SMALL_KEY ? "SMALL" : "LARGE"));
   1.359 +	  }
   1.360 +
   1.361 +	for(num = low; num <= high; num++)
   1.362 +	  {
   1.363 +
   1.364 +		key = GenKey(num, key_type);
   1.365 +
   1.366 +		if(action == ADD_RANGE)
   1.367 +		  {
   1.368 +			data = GenData(num);
   1.369 +			status = (*database->put)(database, key, data, 0);
   1.370 +		  }
   1.371 +		else
   1.372 +		  {
   1.373 +			status = (*database->del)(database, key, 0);
   1.374 +		  }
   1.375 +
   1.376 +		if(status < 0)
   1.377 +		  {
   1.378 +			ReportError("Database error %s item: %ld",
   1.379 +							action == ADD_RANGE ? "ADDING" : "DELETING", 
   1.380 +							num);
   1.381 +		  }
   1.382 +		else if(status > 0)
   1.383 +		  {
   1.384 +			ReportError("Could not %s item: %ld", 
   1.385 +							action == ADD_RANGE ? "ADD" : "DELETE", 
   1.386 +							num);
   1.387 +		  }
   1.388 +		else if(action == ADD_RANGE)
   1.389 +		  {
   1.390 +#define SYNC_EVERY_TIME
   1.391 +#ifdef SYNC_EVERY_TIME
   1.392 +			status = (*database->sync)(database, 0);
   1.393 +			if(status != 0)
   1.394 +				ReportError("Database error syncing after add");
   1.395 +#endif
   1.396 +
   1.397 +#if 0 /* only do this if your really analy checking the puts */
   1.398 +	 
   1.399 +			/* make sure we can still get it
   1.400 +			 */
   1.401 +			status = (*database->get)(database, key, &tmp_data, 0);
   1.402 +
   1.403 +			if(status != 0)
   1.404 +			  {
   1.405 +				ReportError("Database error checking item just added: %d",
   1.406 +							num);
   1.407 +			  }
   1.408 +			else
   1.409 +			  {
   1.410 +				/* now verify that none of the ones we already
   1.411 +				 * put in have disappeared
   1.412 +				 */
   1.413 +				VerifyRange(low, num, SHOULD_EXIST, key_type);
   1.414 +			  }
   1.415 +#endif
   1.416 +			
   1.417 +		  }
   1.418 +	  }
   1.419 +
   1.420 +
   1.421 +	if(action == ADD_RANGE)
   1.422 +	  {
   1.423 +		TraceMe(1, ("Successfully added: %ld to %ld", low, high));
   1.424 +	  }
   1.425 +	else
   1.426 +	  {
   1.427 +		TraceMe(1, ("Successfully deleted: %ld to %ld", low, high));
   1.428 +	  }
   1.429 +
   1.430 +	return(0);
   1.431 +}
   1.432 +
   1.433 +int
   1.434 +TestRange(int32 low, int32 range, key_type_enum key_type)
   1.435 +{
   1.436 +	int status; int32 low_of_range1, high_of_range1; int32 low_of_range2, high_of_range2;
   1.437 +	int32 low_of_range3, high_of_range3;
   1.438 +
   1.439 +	status = AddOrDelRange(low, low+range, ADD_RANGE, key_type);
   1.440 +	status = VerifyRange(low, low+range, SHOULD_EXIST, key_type);
   1.441 +
   1.442 +	TraceMe(1, ("Finished with sub test 1"));
   1.443 +
   1.444 +	SeqDatabase();
   1.445 +
   1.446 +	low_of_range1 = low;
   1.447 +	high_of_range1 = low+(range/2);
   1.448 +	low_of_range2 = high_of_range1+1;
   1.449 +	high_of_range2 = low+range;
   1.450 +	status = AddOrDelRange(low_of_range1, high_of_range1, DELETE_RANGE, key_type);
   1.451 +	status = VerifyRange(low_of_range1, high_of_range1, SHOULD_NOT_EXIST, key_type);
   1.452 +	status = VerifyRange(low_of_range2, low_of_range2, SHOULD_EXIST, key_type);
   1.453 +
   1.454 +	TraceMe(1, ("Finished with sub test 2"));
   1.455 +
   1.456 +	SeqDatabase();
   1.457 +
   1.458 +	status = AddOrDelRange(low_of_range1, high_of_range1, ADD_RANGE, key_type);
   1.459 +	/* the whole thing should exist now */
   1.460 +	status = VerifyRange(low, low+range, SHOULD_EXIST, key_type);
   1.461 +
   1.462 +	TraceMe(1, ("Finished with sub test 3"));
   1.463 +
   1.464 +	SeqDatabase();
   1.465 +
   1.466 +	status = AddOrDelRange(low_of_range2, high_of_range2, DELETE_RANGE, key_type);
   1.467 +	status = VerifyRange(low_of_range1, high_of_range1, SHOULD_EXIST, key_type);
   1.468 +	status = VerifyRange(low_of_range2, high_of_range2, SHOULD_NOT_EXIST, key_type);
   1.469 +
   1.470 +	TraceMe(1, ("Finished with sub test 4"));
   1.471 +
   1.472 +	SeqDatabase();
   1.473 +
   1.474 +	status = AddOrDelRange(low_of_range2, high_of_range2, ADD_RANGE, key_type);
   1.475 +	/* the whole thing should exist now */
   1.476 +	status = VerifyRange(low, low+range, SHOULD_EXIST, key_type);
   1.477 +
   1.478 +	TraceMe(1, ("Finished with sub test 5"));
   1.479 +
   1.480 +	SeqDatabase();
   1.481 +
   1.482 +	low_of_range1 = low;
   1.483 +	high_of_range1 = low+(range/3);
   1.484 +	low_of_range2 = high_of_range1+1;
   1.485 +	high_of_range2 = high_of_range1+(range/3);
   1.486 +	low_of_range3 = high_of_range2+1;
   1.487 +	high_of_range3 = low+range;
   1.488 +	/* delete range 2 */
   1.489 +	status = AddOrDelRange(low_of_range2, high_of_range2, DELETE_RANGE, key_type);
   1.490 +	status = VerifyRange(low_of_range1, high_of_range1, SHOULD_EXIST, key_type);
   1.491 +	status = VerifyRange(low_of_range2, low_of_range2, SHOULD_NOT_EXIST, key_type);
   1.492 +	status = VerifyRange(low_of_range3, low_of_range2, SHOULD_EXIST, key_type);
   1.493 +
   1.494 +	TraceMe(1, ("Finished with sub test 6"));
   1.495 +
   1.496 +	SeqDatabase();
   1.497 +
   1.498 +	status = AddOrDelRange(low_of_range2, high_of_range2, ADD_RANGE, key_type);
   1.499 +	/* the whole thing should exist now */
   1.500 +	status = VerifyRange(low, low+range, SHOULD_EXIST, key_type);
   1.501 +
   1.502 +	TraceMe(1, ("Finished with sub test 7"));
   1.503 +
   1.504 +	return(0);
   1.505 +}
   1.506 +
   1.507 +#define START_RANGE 109876
   1.508 +int
   1.509 +main(int argc, char **argv)
   1.510 +{
   1.511 +	int32 i, j=0;
   1.512 +    int quick_exit = 0;
   1.513 +    int large_keys = 0;
   1.514 +    HASHINFO hash_info = {
   1.515 +        16*1024,
   1.516 +        0,
   1.517 +        0,
   1.518 +        0,
   1.519 +        0,
   1.520 +        0};
   1.521 +
   1.522 +
   1.523 +    if(argc > 1)
   1.524 +      {
   1.525 +        while(argc > 1)
   1.526 +	  {
   1.527 +            if(!strcmp(argv[argc-1], "-quick"))
   1.528 +                quick_exit = 1;
   1.529 +            else if(!strcmp(argv[argc-1], "-large"))
   1.530 +			  {
   1.531 +                large_keys = 1;
   1.532 +			  }
   1.533 +            argc--;
   1.534 +          }
   1.535 +      }
   1.536 +
   1.537 +	database = dbopen("test.db", O_RDWR | O_CREAT, 0644, DB_HASH, &hash_info);
   1.538 +
   1.539 +	if(!database)
   1.540 +	  {
   1.541 +		ReportError("Could not open database");
   1.542 +#ifdef unix
   1.543 +		perror("");
   1.544 +#endif
   1.545 +		exit(1);
   1.546 +	  }
   1.547 +
   1.548 +	if(quick_exit)
   1.549 +	  {
   1.550 +		if(large_keys)
   1.551 +			TestRange(START_RANGE, 200, USE_LARGE_KEY);
   1.552 +		else
   1.553 +			TestRange(START_RANGE, 200, USE_SMALL_KEY);
   1.554 +
   1.555 +		(*database->sync)(database, 0);
   1.556 +		(*database->close)(database);
   1.557 +        exit(0);
   1.558 +	  }
   1.559 +
   1.560 +	for(i=100; i < 10000000; i+=200)
   1.561 +	  {
   1.562 +		if(1 || j)
   1.563 +		  {
   1.564 +			TestRange(START_RANGE, i, USE_LARGE_KEY);
   1.565 +			j = 0;
   1.566 +		  }
   1.567 +		else
   1.568 +		  {
   1.569 +			TestRange(START_RANGE, i, USE_SMALL_KEY);
   1.570 +			j = 1;
   1.571 +		  }
   1.572 +
   1.573 +		if(1 == rand() % 3)
   1.574 +		  {
   1.575 +			(*database->sync)(database, 0);
   1.576 +		  }
   1.577 +		
   1.578 +		if(1 == rand() % 3)
   1.579 +	 	  {
   1.580 +			/* close and reopen */
   1.581 +			(*database->close)(database);
   1.582 +			database = dbopen("test.db", O_RDWR | O_CREAT, 0644, DB_HASH, 0);
   1.583 +			if(!database)
   1.584 +	  		{
   1.585 +				ReportError("Could not reopen database");
   1.586 +#ifdef unix
   1.587 +				perror("");
   1.588 +#endif
   1.589 +				exit(1);
   1.590 +	  		}
   1.591 +	 	  }
   1.592 +		else
   1.593 +		  {
   1.594 +			/* reopen database without closeing the other */
   1.595 +			database = dbopen("test.db", O_RDWR | O_CREAT, 0644, DB_HASH, 0);
   1.596 +			if(!database)
   1.597 +	  		{
   1.598 +				ReportError("Could not reopen database "
   1.599 +							"after not closing the other");
   1.600 +#ifdef unix
   1.601 +				perror("");
   1.602 +#endif
   1.603 +				exit(1);
   1.604 +	  		}
   1.605 +	 	  }
   1.606 +	  }
   1.607 +
   1.608 +	return(0);
   1.609 +}

mercurial