static int fbytes(unsigned char *buf, int num) { int ret = 0; static int fbytes_counter = 0; BIGNUM *tmp = NULL; if (use_fake == 0) return old_rand->bytes(buf, num); use_fake = 0; if (!TEST_ptr(tmp = BN_new()) || !TEST_int_lt(fbytes_counter, OSSL_NELEM(numbers)) || !TEST_true(BN_hex2bn(&tmp, numbers[fbytes_counter])) /* tmp might need leading zeros so pad it out */ || !TEST_int_le(BN_num_bytes(tmp), num) || !TEST_true(BN_bn2binpad(tmp, buf, num))) goto err; fbytes_counter = (fbytes_counter + 1) % OSSL_NELEM(numbers); ret = 1; err: BN_free(tmp); return ret; }
static int test_query_cache_stochastic(void) { const int max = 10000, tail = 10; OSSL_METHOD_STORE *store; int i, res = 0; char buf[50]; void *result; int errors = 0; int v[10001]; if (!TEST_ptr(store = ossl_method_store_new(NULL)) || !add_property_names("n", NULL)) goto err; for (i = 1; i <= max; i++) { v[i] = 2 * i; BIO_snprintf(buf, sizeof(buf), "n=%d\n", i); if (!TEST_true(ossl_method_store_add(store, i, buf, "abc", NULL)) || !TEST_true(ossl_method_store_cache_set(store, i, buf, v + i)) || !TEST_true(ossl_method_store_cache_set(store, i, "n=1234", "miss"))) { TEST_note("iteration %d", i); goto err; } } for (i = 1; i <= max; i++) { BIO_snprintf(buf, sizeof(buf), "n=%d\n", i); if (!ossl_method_store_cache_get(store, i, buf, &result) || result != v + i) errors++; } /* There is a tiny probability that this will fail when it shouldn't */ res = TEST_int_gt(errors, tail) && TEST_int_lt(errors, max - tail); err: ossl_method_store_free(store); return res; }