-
Notifications
You must be signed in to change notification settings - Fork 129
Implement BIO_dump #2331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Implement BIO_dump #2331
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1250,3 +1250,32 @@ TEST(BIOTest, GetMemDataBackwardsCompat) { | |
ASSERT_EQ((size_t)data_len, sizeof(contents)); | ||
EXPECT_EQ(Bytes(contents, sizeof(contents)), Bytes(ptr, data_len)); | ||
} | ||
|
||
TEST(BIOTest, Dump) { | ||
bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem())); | ||
ASSERT_TRUE(bio); | ||
|
||
// Test BIO_dump with binary data | ||
const uint8_t data[] = {0x00, 0x01, 0x02, 0x7f, 0x80, 0xff, 'A', 'B', 'C', '\n', '\r', '\t'}; | ||
|
||
// BIO_dump should return an estimate of bytes written | ||
int ret = BIO_dump(bio.get(), data, sizeof(data)); | ||
ASSERT_GT(ret, 0); | ||
|
||
// Check that BIO_dump produced the expected output format | ||
const uint8_t *contents; | ||
size_t len; | ||
ASSERT_TRUE(BIO_mem_contents(bio.get(), &contents, &len)); | ||
std::string output(reinterpret_cast<const char *>(contents), len); | ||
Comment on lines
+1268
to
+1269
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should also make an assertion about the value of |
||
|
||
// Verify the output contains the expected elements (hex values and ASCII representation) | ||
EXPECT_NE(output.find("00 01 02 7f 80 ff"), std::string::npos); | ||
EXPECT_NE(output.find("ABC"), std::string::npos); | ||
EXPECT_NE(output.find("|"), std::string::npos); // ASCII section divider | ||
|
||
// Verify BIO_dump works with an empty buffer | ||
bio.reset(BIO_new(BIO_s_mem())); | ||
ASSERT_TRUE(bio); | ||
ret = BIO_dump(bio.get(), data, 0); | ||
ASSERT_GE(ret, 0); // Should return 0 or positive for empty input | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -192,3 +192,17 @@ int BIO_hexdump(BIO *bio, const uint8_t *data, size_t len, unsigned indent) { | |
|
||
return 1; | ||
} | ||
|
||
int BIO_dump(BIO *bio, const void *data, int len) { | ||
if (bio == NULL || data == NULL || len < 0) { | ||
return -1; | ||
} | ||
|
||
// Use existing hexdump functionality with no indentation | ||
if (!BIO_hexdump(bio, (const uint8_t *)data, (size_t)len, 0)) { | ||
return -1; | ||
} | ||
|
||
// Return estimate of bytes written (each line is ~80 bytes) | ||
return ((len + 15) / 16) * 80; | ||
Comment on lines
+202
to
+207
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of needing to estimate how many bytes were written, this could be exact. Why not modify |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could make this assertion more specific.