Skip to content

Commit

Permalink
add tests for ex_data
Browse files Browse the repository at this point in the history
  • Loading branch information
samuel40791765 committed May 9, 2024
1 parent 211258d commit af5fac9
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
9 changes: 0 additions & 9 deletions crypto/bio/bio_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -789,15 +789,6 @@ TEST(BIOTest, Gets) {
EXPECT_EQ(c, 'a');
}

typedef struct {
int custom_data;
} CustomData;

static void CustomDataFree(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
int index, long argl, void *argp) {
free(ptr);
}

TEST(BIOTest, ExternalData) {
// Create a |BIO| object
bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
Expand Down
1 change: 1 addition & 0 deletions crypto/crypto_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <openssl/service_indicator.h>

#include <gtest/gtest.h>
#include "test/test_util.h"

// Test that OPENSSL_VERSION_NUMBER and OPENSSL_VERSION_TEXT are consistent.
// Node.js parses the version out of OPENSSL_VERSION_TEXT instead of using
Expand Down
6 changes: 6 additions & 0 deletions crypto/test/test_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,9 @@ FILE* createRawTempFILE() {
TempFILE createTempFILE() {
return TempFILE(createRawTempFILE());
}

void CustomDataFree(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
int index, long argl, void *argp) {
free(ptr);
}

8 changes: 8 additions & 0 deletions crypto/test/test_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,12 @@ size_t createTempFILEpath(char buffer[PATH_MAX]);
FILE* createRawTempFILE();
TempFILE createTempFILE();

// CustomData is for testing new structs that we add support for |ex_data|.
typedef struct {
int custom_data;
} CustomData;

void CustomDataFree(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
int index, long argl, void *argp);

#endif // OPENSSL_HEADER_CRYPTO_TEST_TEST_UTIL_H
22 changes: 22 additions & 0 deletions crypto/x509/x509_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7091,3 +7091,25 @@ TEST(X509Test, GetSigInfo) {
EXPECT_EQ(sec_bits, -1);
EXPECT_TRUE(flags & X509_SIG_INFO_VALID);
}

TEST(X509Test, ExternalData) {
// Create a |X509_STORE| object
bssl::UniquePtr<X509_STORE> store(X509_STORE_new());
int store_index =
X509_STORE_get_ex_new_index(0, nullptr, nullptr, nullptr, CustomDataFree);
ASSERT_GT(store_index, 0);

// Associate custom data with the |X509_STORE| using |X509_STORE_set_ex_data|
// and set an arbitrary number.
auto *custom_data = static_cast<CustomData *>(malloc(sizeof(CustomData)));
ASSERT_TRUE(custom_data);
custom_data->custom_data = 123;
ASSERT_TRUE(X509_STORE_set_ex_data(store.get(), store_index, custom_data));

// Retrieve the custom data using |X509_STORE_get_ex_data|.
auto *retrieved_data = static_cast<CustomData *>(
X509_STORE_get_ex_data(store.get(), store_index));
ASSERT_TRUE(retrieved_data);
EXPECT_EQ(retrieved_data->custom_data, 123);
}

0 comments on commit af5fac9

Please sign in to comment.