Convert various tests to GTest.
BUG=129 Change-Id: I4a501fa620b7b5b4c585731ec1ece94407e9b727 Reviewed-on: https://boringssl-review.googlesource.com/16510 Reviewed-by: Adam Langley <agl@google.com>
This commit is contained in:
parent
8c2e8282ab
commit
e324de004a
@ -223,15 +223,6 @@ add_executable(
|
||||
target_link_libraries(thread_test crypto)
|
||||
add_dependencies(all_tests thread_test)
|
||||
|
||||
add_executable(
|
||||
refcount_test
|
||||
|
||||
refcount_test.cc
|
||||
)
|
||||
|
||||
target_link_libraries(refcount_test crypto)
|
||||
add_dependencies(all_tests refcount_test)
|
||||
|
||||
# TODO(davidben): Convert the remaining tests to GTest.
|
||||
add_executable(
|
||||
crypto_test
|
||||
@ -252,11 +243,16 @@ add_executable(
|
||||
dsa/dsa_test.cc
|
||||
err/err_test.cc
|
||||
evp/evp_extra_test.cc
|
||||
evp/pbkdf_test.cc
|
||||
fipsmodule/aes/aes_test.cc
|
||||
fipsmodule/ec/ec_test.cc
|
||||
fipsmodule/rand/ctrdrbg_test.cc
|
||||
test/file_test_gtest.cc
|
||||
hkdf/hkdf_test.cc
|
||||
lhash/lhash_test.cc
|
||||
pool/pool_test.cc
|
||||
refcount_test.cc
|
||||
rsa_extra/rsa_test.cc
|
||||
test/file_test_gtest.cc
|
||||
|
||||
$<TARGET_OBJECTS:crypto_test_data>
|
||||
$<TARGET_OBJECTS:gtest_main>
|
||||
|
@ -30,14 +30,5 @@ add_executable(
|
||||
$<TARGET_OBJECTS:test_support>
|
||||
)
|
||||
|
||||
add_executable(
|
||||
pbkdf_test
|
||||
|
||||
pbkdf_test.cc
|
||||
|
||||
$<TARGET_OBJECTS:test_support>
|
||||
)
|
||||
|
||||
target_link_libraries(evp_test crypto)
|
||||
target_link_libraries(pbkdf_test crypto)
|
||||
add_dependencies(all_tests evp_test pbkdf_test)
|
||||
add_dependencies(all_tests evp_test)
|
||||
|
@ -12,93 +12,53 @@
|
||||
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <openssl/crypto.h>
|
||||
#include <openssl/digest.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/evp.h>
|
||||
|
||||
#include "../internal.h"
|
||||
#include "../test/test_util.h"
|
||||
|
||||
|
||||
// Prints out the data buffer as a sequence of hex bytes.
|
||||
static void PrintDataHex(const void *data, size_t len) {
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
fprintf(stderr, "%02x", (int)((const uint8_t *)data)[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// Helper for testing that PBKDF2 derives the expected key from the given
|
||||
// inputs. Returns 1 on success, 0 otherwise.
|
||||
static bool TestPBKDF2(const void *password, size_t password_len,
|
||||
const void *salt, size_t salt_len, unsigned iterations,
|
||||
const EVP_MD *digest, size_t key_len,
|
||||
const uint8_t *expected_key) {
|
||||
uint8_t key[64];
|
||||
|
||||
if (key_len > sizeof(key)) {
|
||||
fprintf(stderr, "Output buffer is not large enough.\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!PKCS5_PBKDF2_HMAC((const char *)password, password_len,
|
||||
(const uint8_t *)salt, salt_len, iterations, digest,
|
||||
key_len, key)) {
|
||||
fprintf(stderr, "Call to PKCS5_PBKDF2_HMAC failed\n");
|
||||
ERR_print_errors_fp(stderr);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (OPENSSL_memcmp(key, expected_key, key_len) != 0) {
|
||||
fprintf(stderr, "Resulting key material does not match expectation\n");
|
||||
fprintf(stderr, "Expected:\n ");
|
||||
PrintDataHex(expected_key, key_len);
|
||||
fprintf(stderr, "\nActual:\n ");
|
||||
PrintDataHex(key, key_len);
|
||||
fprintf(stderr, "\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Tests deriving a key using an empty password (specified both as NULL and as
|
||||
// non-NULL). Note that NULL has special meaning to HMAC initialization.
|
||||
static bool TestEmptyPassword() {
|
||||
TEST(PBKDFTest, EmptyPassword) {
|
||||
const uint8_t kKey[] = {0xa3, 0x3d, 0xdd, 0xc3, 0x04, 0x78, 0x18,
|
||||
0x55, 0x15, 0x31, 0x1f, 0x87, 0x52, 0x89,
|
||||
0x5d, 0x36, 0xea, 0x43, 0x63, 0xa2};
|
||||
uint8_t key[sizeof(kKey)];
|
||||
|
||||
if (!TestPBKDF2(NULL, 0, "salt", 4, 1, EVP_sha1(), sizeof(kKey), kKey) ||
|
||||
!TestPBKDF2("", 0, "salt", 4, 1, EVP_sha1(), sizeof(kKey), kKey)) {
|
||||
return false;
|
||||
}
|
||||
ASSERT_TRUE(PKCS5_PBKDF2_HMAC(NULL, 0, (const uint8_t *)"salt", 4, 1,
|
||||
EVP_sha1(), sizeof(kKey), key));
|
||||
EXPECT_EQ(Bytes(kKey), Bytes(key));
|
||||
|
||||
return true;
|
||||
ASSERT_TRUE(PKCS5_PBKDF2_HMAC("", 0, (const uint8_t *)"salt", 4, 1,
|
||||
EVP_sha1(), sizeof(kKey), key));
|
||||
EXPECT_EQ(Bytes(kKey), Bytes(key));
|
||||
}
|
||||
|
||||
// Tests deriving a key using an empty salt. Note that the expectation was
|
||||
// generated using OpenSSL itself, and hence is not verified.
|
||||
static bool TestEmptySalt() {
|
||||
TEST(PBKDFTest, EmptySalt) {
|
||||
const uint8_t kKey[] = {0x8b, 0xc2, 0xf9, 0x16, 0x7a, 0x81, 0xcd, 0xcf,
|
||||
0xad, 0x12, 0x35, 0xcd, 0x90, 0x47, 0xf1, 0x13,
|
||||
0x62, 0x71, 0xc1, 0xf9, 0x78, 0xfc, 0xfc, 0xb3,
|
||||
0x5e, 0x22, 0xdb, 0xea, 0xfa, 0x46, 0x34, 0xf6};
|
||||
uint8_t key[sizeof(kKey)];
|
||||
|
||||
if (!TestPBKDF2("password", 8, NULL, 0, 2, EVP_sha256(), sizeof(kKey),
|
||||
kKey) ||
|
||||
!TestPBKDF2("password", 8, "", 0, 2, EVP_sha256(), sizeof(kKey), kKey)) {
|
||||
return false;
|
||||
}
|
||||
ASSERT_TRUE(PKCS5_PBKDF2_HMAC("password", 8, NULL, 0, 2, EVP_sha256(),
|
||||
sizeof(kKey), key));
|
||||
EXPECT_EQ(Bytes(kKey), Bytes(key));
|
||||
|
||||
return true;
|
||||
ASSERT_TRUE(PKCS5_PBKDF2_HMAC("password", 8, (const uint8_t *)"", 0, 2,
|
||||
EVP_sha256(), sizeof(kKey), key));
|
||||
EXPECT_EQ(Bytes(kKey), Bytes(key));
|
||||
}
|
||||
|
||||
// Exercises test vectors taken from https://tools.ietf.org/html/rfc6070.
|
||||
// Note that each of these test vectors uses SHA-1 as the digest.
|
||||
static bool TestRFC6070Vectors() {
|
||||
TEST(PBKDFTest, RFC6070Vectors) {
|
||||
const uint8_t kKey1[] = {0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e,
|
||||
0x71, 0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60,
|
||||
0x12, 0x06, 0x2f, 0xe0, 0x37, 0xa6};
|
||||
@ -107,21 +67,25 @@ static bool TestRFC6070Vectors() {
|
||||
0x41, 0xf0, 0xd8, 0xde, 0x89, 0x57};
|
||||
const uint8_t kKey3[] = {0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d,
|
||||
0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3};
|
||||
uint8_t key[sizeof(kKey1)];
|
||||
static_assert(sizeof(key) >= sizeof(kKey2), "output too small");
|
||||
static_assert(sizeof(key) >= sizeof(kKey3), "output too small");
|
||||
|
||||
if (!TestPBKDF2("password", 8, "salt", 4, 1, EVP_sha1(), sizeof(kKey1),
|
||||
kKey1) ||
|
||||
!TestPBKDF2("password", 8, "salt", 4, 2, EVP_sha1(), sizeof(kKey2),
|
||||
kKey2) ||
|
||||
!TestPBKDF2("pass\0word", 9, "sa\0lt", 5, 4096, EVP_sha1(),
|
||||
sizeof(kKey3), kKey3)) {
|
||||
return false;
|
||||
}
|
||||
ASSERT_TRUE(PKCS5_PBKDF2_HMAC("password", 8, (const uint8_t *)"salt", 4, 1,
|
||||
EVP_sha1(), sizeof(kKey1), key));
|
||||
EXPECT_EQ(Bytes(kKey1), Bytes(key, sizeof(kKey1)));
|
||||
|
||||
return true;
|
||||
ASSERT_TRUE(PKCS5_PBKDF2_HMAC("password", 8, (const uint8_t *)"salt", 4, 2,
|
||||
EVP_sha1(), sizeof(kKey2), key));
|
||||
EXPECT_EQ(Bytes(kKey2), Bytes(key, sizeof(kKey2)));
|
||||
|
||||
ASSERT_TRUE(PKCS5_PBKDF2_HMAC("pass\0word", 9, (const uint8_t *)"sa\0lt", 5,
|
||||
4096, EVP_sha1(), sizeof(kKey3), key));
|
||||
EXPECT_EQ(Bytes(kKey3), Bytes(key, sizeof(kKey3)));
|
||||
}
|
||||
|
||||
// Tests key derivation using SHA-2 digests.
|
||||
static bool TestSHA2() {
|
||||
TEST(PBKDFTest, SHA2) {
|
||||
// This test was taken from:
|
||||
// http://stackoverflow.com/questions/5130513/pbkdf2-hmac-sha2-test-vectors.
|
||||
const uint8_t kKey1[] = {0xae, 0x4d, 0x0c, 0x95, 0xaf, 0x6b, 0x46, 0xd3,
|
||||
@ -139,15 +103,18 @@ static bool TestSHA2() {
|
||||
0x75, 0xae, 0xfe, 0x30, 0x22, 0x5c, 0x58, 0x3a, 0x18, 0x6c, 0xd8,
|
||||
0x2b, 0xd4, 0xda, 0xea, 0x97, 0x24, 0xa3, 0xd3, 0xb8};
|
||||
|
||||
if (!TestPBKDF2("password", 8, "salt", 4, 2, EVP_sha256(), sizeof(kKey1),
|
||||
kKey1) ||
|
||||
!TestPBKDF2("passwordPASSWORDpassword", 24,
|
||||
"saltSALTsaltSALTsaltSALTsaltSALTsalt", 36, 4096,
|
||||
EVP_sha512(), sizeof(kKey2), kKey2)) {
|
||||
return false;
|
||||
}
|
||||
uint8_t key[sizeof(kKey2)];
|
||||
static_assert(sizeof(key) >= sizeof(kKey1), "output too small");
|
||||
|
||||
return true;
|
||||
ASSERT_TRUE(PKCS5_PBKDF2_HMAC("password", 8, (const uint8_t *)"salt", 4, 2,
|
||||
EVP_sha256(), sizeof(kKey1), key));
|
||||
EXPECT_EQ(Bytes(kKey1), Bytes(key, sizeof(kKey1)));
|
||||
|
||||
ASSERT_TRUE(
|
||||
PKCS5_PBKDF2_HMAC("passwordPASSWORDpassword", 24,
|
||||
(const uint8_t *)"saltSALTsaltSALTsaltSALTsaltSALTsalt",
|
||||
36, 4096, EVP_sha512(), sizeof(kKey2), key));
|
||||
EXPECT_EQ(Bytes(kKey2), Bytes(key, sizeof(kKey2)));
|
||||
}
|
||||
|
||||
// Tests key derivation using iterations=0.
|
||||
@ -155,7 +122,7 @@ static bool TestSHA2() {
|
||||
// RFC 2898 defines the iteration count (c) as a "positive integer". So doing a
|
||||
// key derivation with iterations=0 is ill-defined and should result in a
|
||||
// failure.
|
||||
static bool TestZeroIterations() {
|
||||
TEST(PBKDFTest, ZeroIterations) {
|
||||
static const char kPassword[] = "password";
|
||||
const size_t password_len = strlen(kPassword);
|
||||
static const uint8_t kSalt[] = {1, 2, 3, 4};
|
||||
@ -166,57 +133,18 @@ static bool TestZeroIterations() {
|
||||
const size_t key_len = sizeof(key);
|
||||
|
||||
// Verify that calling with iterations=1 works.
|
||||
if (!PKCS5_PBKDF2_HMAC(kPassword, password_len, kSalt, salt_len,
|
||||
1 /* iterations */, digest, key_len, key)) {
|
||||
fprintf(stderr, "PBKDF2 failed with iterations=1\n");
|
||||
return false;
|
||||
}
|
||||
ASSERT_TRUE(PKCS5_PBKDF2_HMAC(kPassword, password_len, kSalt, salt_len,
|
||||
1 /* iterations */, digest, key_len, key));
|
||||
|
||||
// Flip the first key byte (so can later test if it got set).
|
||||
const uint8_t expected_first_byte = key[0];
|
||||
key[0] = ~key[0];
|
||||
|
||||
// However calling it with iterations=0 fails.
|
||||
if (PKCS5_PBKDF2_HMAC(kPassword, password_len, kSalt, salt_len,
|
||||
0 /* iterations */, digest, key_len, key)) {
|
||||
fprintf(stderr, "PBKDF2 returned zero with iterations=0\n");
|
||||
return false;
|
||||
}
|
||||
ASSERT_FALSE(PKCS5_PBKDF2_HMAC(kPassword, password_len, kSalt, salt_len,
|
||||
0 /* iterations */, digest, key_len, key));
|
||||
|
||||
// For backwards compatibility, the iterations == 0 case still fills in
|
||||
// the out key.
|
||||
return key[0] == expected_first_byte;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
CRYPTO_library_init();
|
||||
|
||||
if (!TestEmptyPassword()) {
|
||||
fprintf(stderr, "TestEmptyPassword failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!TestEmptySalt()) {
|
||||
fprintf(stderr, "TestEmptySalt failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!TestRFC6070Vectors()) {
|
||||
fprintf(stderr, "TestRFC6070Vectors failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!TestSHA2()) {
|
||||
fprintf(stderr, "TestSHA2 failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!TestZeroIterations()) {
|
||||
fprintf(stderr, "TestZeroIterations failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("PASS\n");
|
||||
ERR_free_strings();
|
||||
return 0;
|
||||
EXPECT_EQ(expected_first_byte, key[0]);
|
||||
}
|
||||
|
@ -7,14 +7,3 @@ add_library(
|
||||
|
||||
hkdf.c
|
||||
)
|
||||
|
||||
add_executable(
|
||||
hkdf_test
|
||||
|
||||
hkdf_test.cc
|
||||
|
||||
$<TARGET_OBJECTS:test_support>
|
||||
)
|
||||
|
||||
target_link_libraries(hkdf_test crypto)
|
||||
add_dependencies(all_tests hkdf_test)
|
||||
|
@ -12,15 +12,12 @@
|
||||
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <openssl/crypto.h>
|
||||
#include <openssl/digest.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/hkdf.h>
|
||||
|
||||
#include "../internal.h"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "../test/test_util.h"
|
||||
|
||||
|
||||
@ -247,50 +244,25 @@ static const HKDFTestVector kTests[] = {
|
||||
},
|
||||
};
|
||||
|
||||
int main(void) {
|
||||
CRYPTO_library_init();
|
||||
|
||||
TEST(HKDFTest, TestVectors) {
|
||||
for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kTests); i++) {
|
||||
SCOPED_TRACE(i);
|
||||
const HKDFTestVector *test = &kTests[i];
|
||||
|
||||
uint8_t prk[EVP_MAX_MD_SIZE];
|
||||
size_t prk_len;
|
||||
if (!HKDF_extract(prk, &prk_len, test->md_func(), test->ikm, test->ikm_len,
|
||||
test->salt, test->salt_len)) {
|
||||
fprintf(stderr, "Call to HKDF_extract failed\n");
|
||||
ERR_print_errors_fp(stderr);
|
||||
return 1;
|
||||
}
|
||||
if (prk_len != test->prk_len ||
|
||||
OPENSSL_memcmp(prk, test->prk, test->prk_len) != 0) {
|
||||
fprintf(stderr, "%zu: Resulting PRK does not match test vector\n", i);
|
||||
return 1;
|
||||
}
|
||||
ASSERT_TRUE(HKDF_extract(prk, &prk_len, test->md_func(), test->ikm,
|
||||
test->ikm_len, test->salt, test->salt_len));
|
||||
EXPECT_EQ(Bytes(test->prk, test->prk_len), Bytes(prk, prk_len));
|
||||
|
||||
uint8_t buf[82];
|
||||
if (!HKDF_expand(buf, test->out_len, test->md_func(), prk, prk_len,
|
||||
test->info, test->info_len)) {
|
||||
fprintf(stderr, "Call to HKDF_expand failed\n");
|
||||
ERR_print_errors_fp(stderr);
|
||||
return 1;
|
||||
}
|
||||
if (OPENSSL_memcmp(buf, test->out, test->out_len) != 0) {
|
||||
fprintf(stderr,
|
||||
"%zu: Resulting key material does not match test vector\n", i);
|
||||
return 1;
|
||||
}
|
||||
ASSERT_TRUE(HKDF_expand(buf, test->out_len, test->md_func(), prk, prk_len,
|
||||
test->info, test->info_len));
|
||||
EXPECT_EQ(Bytes(test->out, test->out_len), Bytes(buf, test->out_len));
|
||||
|
||||
if (!HKDF(buf, test->out_len, test->md_func(), test->ikm, test->ikm_len,
|
||||
test->salt, test->salt_len, test->info, test->info_len)) {
|
||||
fprintf(stderr, "Call to HKDF failed\n");
|
||||
ERR_print_errors_fp(stderr);
|
||||
return 1;
|
||||
}
|
||||
if (OPENSSL_memcmp(buf, test->out, test->out_len) != 0) {
|
||||
fprintf(stderr,
|
||||
"%zu: Resulting key material does not match test vector\n", i);
|
||||
return 1;
|
||||
}
|
||||
ASSERT_TRUE(HKDF(buf, test->out_len, test->md_func(), test->ikm,
|
||||
test->ikm_len, test->salt, test->salt_len, test->info,
|
||||
test->info_len));
|
||||
EXPECT_EQ(Bytes(test->out, test->out_len), Bytes(buf, test->out_len));
|
||||
}
|
||||
|
||||
printf("PASS\n");
|
||||
return 0;
|
||||
}
|
||||
|
@ -7,14 +7,3 @@ add_library(
|
||||
|
||||
lhash.c
|
||||
)
|
||||
|
||||
add_executable(
|
||||
lhash_test
|
||||
|
||||
lhash_test.cc
|
||||
|
||||
$<TARGET_OBJECTS:test_support>
|
||||
)
|
||||
|
||||
target_link_libraries(lhash_test crypto)
|
||||
add_dependencies(all_tests lhash_test)
|
||||
|
@ -16,7 +16,6 @@
|
||||
#define _POSIX_C_SOURCE 201410L
|
||||
#endif
|
||||
|
||||
#include <openssl/crypto.h>
|
||||
#include <openssl/lhash.h>
|
||||
|
||||
#include <stdio.h>
|
||||
@ -30,6 +29,8 @@
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
|
||||
static std::unique_ptr<char[]> RandString(void) {
|
||||
unsigned len = 1 + (rand() % 3);
|
||||
@ -57,14 +58,10 @@ static const char *Lookup(
|
||||
return iter->second.get();
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
CRYPTO_library_init();
|
||||
|
||||
TEST(LHashTest, Basic) {
|
||||
std::unique_ptr<_LHASH, FreeLHASH> lh(
|
||||
lh_new((lhash_hash_func)lh_strhash, (lhash_cmp_func)strcmp));
|
||||
if (!lh) {
|
||||
return 1;
|
||||
}
|
||||
ASSERT_TRUE(lh);
|
||||
|
||||
// lh is expected to store a canonical instance of each string. dummy_lh
|
||||
// mirrors what it stores for comparison. It also manages ownership of the
|
||||
@ -72,10 +69,7 @@ int main(int argc, char **argv) {
|
||||
std::map<std::string, std::unique_ptr<char[]>> dummy_lh;
|
||||
|
||||
for (unsigned i = 0; i < 100000; i++) {
|
||||
if (dummy_lh.size() != lh_num_items(lh.get())) {
|
||||
fprintf(stderr, "Length mismatch\n");
|
||||
return 1;
|
||||
}
|
||||
EXPECT_EQ(dummy_lh.size(), lh_num_items(lh.get()));
|
||||
|
||||
// Check the entire contents and test |lh_doall_arg|. This takes O(N) time,
|
||||
// so only do it every few iterations.
|
||||
@ -98,10 +92,7 @@ int main(int argc, char **argv) {
|
||||
&actual);
|
||||
std::sort(actual.begin(), actual.end());
|
||||
|
||||
if (expected != actual) {
|
||||
fprintf(stderr, "Contents mismatch\n");
|
||||
return 1;
|
||||
}
|
||||
EXPECT_EQ(expected, actual);
|
||||
}
|
||||
|
||||
enum Action {
|
||||
@ -115,25 +106,15 @@ int main(int argc, char **argv) {
|
||||
case kRetrieve: {
|
||||
std::unique_ptr<char[]> key = RandString();
|
||||
void *value = lh_retrieve(lh.get(), key.get());
|
||||
if (value != Lookup(&dummy_lh, key.get())) {
|
||||
fprintf(stderr, "lh_retrieve failure\n");
|
||||
return 1;
|
||||
}
|
||||
EXPECT_EQ(Lookup(&dummy_lh, key.get()), value);
|
||||
break;
|
||||
}
|
||||
|
||||
case kInsert: {
|
||||
std::unique_ptr<char[]> key = RandString();
|
||||
void *previous;
|
||||
if (!lh_insert(lh.get(), &previous, key.get())) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (previous != Lookup(&dummy_lh, key.get())) {
|
||||
fprintf(stderr, "lh_insert failure\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
ASSERT_TRUE(lh_insert(lh.get(), &previous, key.get()));
|
||||
EXPECT_EQ(Lookup(&dummy_lh, key.get()), previous);
|
||||
dummy_lh[key.get()] = std::move(key);
|
||||
break;
|
||||
}
|
||||
@ -141,21 +122,10 @@ int main(int argc, char **argv) {
|
||||
case kDelete: {
|
||||
std::unique_ptr<char[]> key = RandString();
|
||||
void *value = lh_delete(lh.get(), key.get());
|
||||
|
||||
if (value != Lookup(&dummy_lh, key.get())) {
|
||||
fprintf(stderr, "lh_delete failure\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
EXPECT_EQ(Lookup(&dummy_lh, key.get()), value);
|
||||
dummy_lh.erase(key.get());
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
printf("PASS\n");
|
||||
return 0;
|
||||
}
|
||||
|
@ -7,14 +7,3 @@ add_library(
|
||||
|
||||
pool.c
|
||||
)
|
||||
|
||||
add_executable(
|
||||
pool_test
|
||||
|
||||
pool_test.cc
|
||||
|
||||
$<TARGET_OBJECTS:test_support>
|
||||
)
|
||||
|
||||
target_link_libraries(pool_test crypto)
|
||||
add_dependencies(all_tests pool_test)
|
||||
|
@ -12,78 +12,47 @@
|
||||
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <openssl/pool.h>
|
||||
|
||||
#include "../internal.h"
|
||||
#include "../test/test_util.h"
|
||||
|
||||
|
||||
static bool TestUnpooled() {
|
||||
TEST(PoolTest, Unpooled) {
|
||||
static const uint8_t kData[4] = {1, 2, 3, 4};
|
||||
bssl::UniquePtr<CRYPTO_BUFFER> buf(
|
||||
CRYPTO_BUFFER_new(kData, sizeof(kData), nullptr));
|
||||
if (!buf) {
|
||||
return false;
|
||||
}
|
||||
ASSERT_TRUE(buf);
|
||||
|
||||
if (CRYPTO_BUFFER_len(buf.get()) != sizeof(kData) ||
|
||||
OPENSSL_memcmp(kData, CRYPTO_BUFFER_data(buf.get()), sizeof(kData)) !=
|
||||
0) {
|
||||
fprintf(stderr, "CRYPTO_BUFFER corrupted data.\n");
|
||||
return false;
|
||||
}
|
||||
EXPECT_EQ(Bytes(kData),
|
||||
Bytes(CRYPTO_BUFFER_data(buf.get()), CRYPTO_BUFFER_len(buf.get())));
|
||||
|
||||
// Test that reference-counting works properly.
|
||||
CRYPTO_BUFFER_up_ref(buf.get());
|
||||
bssl::UniquePtr<CRYPTO_BUFFER> buf2(buf.get());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool TestEmpty() {
|
||||
TEST(PoolTest, Empty) {
|
||||
bssl::UniquePtr<CRYPTO_BUFFER> buf(CRYPTO_BUFFER_new(nullptr, 0, nullptr));
|
||||
if (!buf) {
|
||||
return false;
|
||||
}
|
||||
ASSERT_TRUE(buf);
|
||||
|
||||
return true;
|
||||
EXPECT_EQ(Bytes(""),
|
||||
Bytes(CRYPTO_BUFFER_data(buf.get()), CRYPTO_BUFFER_len(buf.get())));
|
||||
}
|
||||
|
||||
static bool TestPool() {
|
||||
TEST(PoolTest, Pooled) {
|
||||
bssl::UniquePtr<CRYPTO_BUFFER_POOL> pool(CRYPTO_BUFFER_POOL_new());
|
||||
if (!pool) {
|
||||
return false;
|
||||
}
|
||||
ASSERT_TRUE(pool);
|
||||
|
||||
static const uint8_t kData[4] = {1, 2, 3, 4};
|
||||
bssl::UniquePtr<CRYPTO_BUFFER> buf(
|
||||
CRYPTO_BUFFER_new(kData, sizeof(kData), pool.get()));
|
||||
if (!buf) {
|
||||
return false;
|
||||
}
|
||||
ASSERT_TRUE(buf);
|
||||
|
||||
bssl::UniquePtr<CRYPTO_BUFFER> buf2(
|
||||
CRYPTO_BUFFER_new(kData, sizeof(kData), pool.get()));
|
||||
if (!buf2) {
|
||||
return false;
|
||||
}
|
||||
ASSERT_TRUE(buf2);
|
||||
|
||||
if (buf.get() != buf2.get()) {
|
||||
fprintf(stderr, "CRYPTO_BUFFER_POOL did not dedup data.\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (!TestUnpooled() ||
|
||||
!TestEmpty() ||
|
||||
!TestPool()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("PASS\n");
|
||||
return 0;
|
||||
EXPECT_EQ(buf.get(), buf2.get()) << "CRYPTO_BUFFER_POOL did not dedup data.";
|
||||
}
|
||||
|
@ -14,46 +14,27 @@
|
||||
|
||||
#include "internal.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <openssl/type_check.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
TEST(RefCountTest, Basic) {
|
||||
CRYPTO_refcount_t count = 0;
|
||||
|
||||
CRYPTO_refcount_inc(&count);
|
||||
if (count != 1) {
|
||||
fprintf(stderr, "Incrementing reference count did not work.\n");
|
||||
return 1;
|
||||
}
|
||||
if (!CRYPTO_refcount_dec_and_test_zero(&count) || count != 0) {
|
||||
fprintf(stderr, "Decrementing reference count to zero did not work.\n");
|
||||
return 1;
|
||||
}
|
||||
EXPECT_EQ(1u, count);
|
||||
|
||||
EXPECT_TRUE(CRYPTO_refcount_dec_and_test_zero(&count));
|
||||
EXPECT_EQ(0u, count);
|
||||
|
||||
count = CRYPTO_REFCOUNT_MAX;
|
||||
CRYPTO_refcount_inc(&count);
|
||||
if (count != CRYPTO_REFCOUNT_MAX) {
|
||||
fprintf(stderr, "Count did not saturate correctly when incrementing.\n");
|
||||
return 1;
|
||||
}
|
||||
if (CRYPTO_refcount_dec_and_test_zero(&count) ||
|
||||
count != CRYPTO_REFCOUNT_MAX) {
|
||||
fprintf(stderr, "Count did not saturate correctly when decrementing.\n");
|
||||
return 1;
|
||||
}
|
||||
EXPECT_EQ(CRYPTO_REFCOUNT_MAX, count)
|
||||
<< "Count did not saturate correctly when incrementing.";
|
||||
EXPECT_FALSE(CRYPTO_refcount_dec_and_test_zero(&count));
|
||||
EXPECT_EQ(CRYPTO_REFCOUNT_MAX, count)
|
||||
<< "Count did not saturate correctly when decrementing.";
|
||||
|
||||
count = 2;
|
||||
if (CRYPTO_refcount_dec_and_test_zero(&count)) {
|
||||
fprintf(stderr, "Decrementing two resulted in zero!\n");
|
||||
return 1;
|
||||
}
|
||||
if (count != 1) {
|
||||
fprintf(stderr, "Decrementing two did not produce one!");
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("PASS\n");
|
||||
return 0;
|
||||
EXPECT_FALSE(CRYPTO_refcount_dec_and_test_zero(&count));
|
||||
EXPECT_EQ(1u, count);
|
||||
}
|
||||
|
@ -34,7 +34,6 @@
|
||||
["crypto/crypto_test"],
|
||||
["crypto/ecdh/ecdh_test", "crypto/ecdh/ecdh_tests.txt"],
|
||||
["crypto/evp/evp_test", "crypto/evp/evp_tests.txt"],
|
||||
["crypto/evp/pbkdf_test"],
|
||||
["crypto/fipsmodule/bn_test", "crypto/fipsmodule/bn/bn_tests.txt"],
|
||||
["crypto/fipsmodule/ctrdrbg_vector_test", "crypto/fipsmodule/rand/ctrdrbg_vectors.txt"],
|
||||
["crypto/fipsmodule/ecdsa_sign_test", "crypto/fipsmodule/ecdsa/ecdsa_sign_tests.txt"],
|
||||
@ -43,16 +42,12 @@
|
||||
["crypto/fipsmodule/example_mul"],
|
||||
["crypto/fipsmodule/gcm_test"],
|
||||
["crypto/fipsmodule/p256-x86_64_test", "crypto/fipsmodule/ec/p256-x86_64_tests.txt"],
|
||||
["crypto/hkdf/hkdf_test"],
|
||||
["crypto/hmac_extra/hmac_test", "crypto/hmac_extra/hmac_tests.txt"],
|
||||
["crypto/lhash/lhash_test"],
|
||||
["crypto/obj/obj_test"],
|
||||
["crypto/pkcs7/pkcs7_test"],
|
||||
["crypto/pkcs8/pkcs12_test"],
|
||||
["crypto/pkcs8/pkcs8_test"],
|
||||
["crypto/poly1305/poly1305_test", "crypto/poly1305/poly1305_tests.txt"],
|
||||
["crypto/pool/pool_test"],
|
||||
["crypto/refcount_test"],
|
||||
["crypto/thread_test"],
|
||||
["crypto/x509v3/tab_test"],
|
||||
["crypto/x509v3/v3name_test"],
|
||||
|
Loading…
Reference in New Issue
Block a user