Skip to content

Commit

Permalink
Add test case to cover failed writes to underlying BIO
Browse files Browse the repository at this point in the history
  • Loading branch information
WillChilds-Klein committed Sep 11, 2024
1 parent 4a98ff0 commit a7e3d1a
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions crypto/pkcs7/pkcs7_internal_bio_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ TEST(PKCS7Test, CipherBIO) {
EXPECT_TRUE(BIO_get_cipher_status(bio_cipher.get()));
EXPECT_EQ(Bytes(pt, sizeof(pt)), Bytes(pt_decrypted, sizeof(pt_decrypted)));

// TODO [childw]
// TODO [childw] variable read/write sizes
bio_cipher.reset(BIO_new(BIO_f_cipher()));
ASSERT_TRUE(bio_cipher);
EXPECT_TRUE(BIO_get_cipher_ctx(bio_cipher.get(), &ctx));
Expand All @@ -122,7 +122,6 @@ TEST(PKCS7Test, CipherBIO) {
bio_mem.reset(BIO_new(BIO_s_mem()));
ASSERT_TRUE(bio_mem);
ASSERT_TRUE(BIO_push(bio_cipher.get(), bio_mem.get()));

std::vector<uint8_t> pt_vec, ct_vec, decrypted_pt_vec;
uint8_t buff[1024*1024];
for (size_t wsize : (size_t[]){1, 3, 7, 8, 64, 7, 0, 923, sizeof(buff), 1, 8}) {
Expand All @@ -136,7 +135,6 @@ TEST(PKCS7Test, CipherBIO) {
size_t bytes_read = BIO_read(bio_mem.get(), buff, sizeof(buff));
ct_vec.insert(ct_vec.end(), buff, buff + bytes_read);
}

EXPECT_TRUE(BIO_reset(bio_cipher.get())); // also resets owned |bio_mem|
EXPECT_TRUE(BIO_write(bio_mem.get(), ct_vec.data(), ct_vec.size())); // replace ct
bio_mem.release(); // |bio_cipher| took ownership
Expand All @@ -150,4 +148,43 @@ TEST(PKCS7Test, CipherBIO) {
EXPECT_EQ(pt_vec.size(), decrypted_pt_vec.size());
EXPECT_EQ(Bytes(pt_vec.data(), pt_vec.size()),
Bytes(decrypted_pt_vec.data(), decrypted_pt_vec.size()));

// TODO [childw] explain induce write failure
pt_vec.clear();
decrypted_pt_vec.clear();
bio_cipher.reset(BIO_new(BIO_f_cipher()));
ASSERT_TRUE(bio_cipher);
EXPECT_TRUE(BIO_get_cipher_ctx(bio_cipher.get(), &ctx));
ASSERT_TRUE(
EVP_CipherInit_ex(ctx, EVP_aes_128_gcm(), NULL, key, iv, /*enc*/ 1));
bio_mem.reset(BIO_new(BIO_s_mem()));
ASSERT_TRUE(bio_mem);
ASSERT_TRUE(BIO_push(bio_cipher.get(), bio_mem.get()));
const int wsize = 16;
pt_vec.insert(pt_vec.end(), buff, buff + wsize);
EXPECT_TRUE(BIO_write(bio_cipher.get(), buff, wsize));
EXPECT_EQ(0UL, BIO_wpending(bio_cipher.get()));
BIO_set_flags(bio_mem.get(), BIO_FLAGS_MEM_RDONLY);
pt_vec.insert(pt_vec.end(), buff, buff + wsize);
EXPECT_TRUE(BIO_write(bio_cipher.get(), buff, wsize));
BIO_clear_flags(bio_mem.get(), BIO_FLAGS_MEM_RDONLY);
EXPECT_LT(0UL, BIO_wpending(bio_cipher.get()));
EXPECT_TRUE(BIO_flush(bio_cipher.get()));
EXPECT_EQ(0UL, BIO_wpending(bio_cipher.get()));
EXPECT_TRUE(BIO_get_cipher_status(bio_cipher.get()));
EXPECT_TRUE(BIO_get_cipher_ctx(bio_cipher.get(), &ctx));
ASSERT_TRUE(
EVP_CipherInit_ex(ctx, EVP_aes_128_gcm(), NULL, key, iv, /*enc*/ 0));
decrypted_pt_vec.resize(pt_vec.size());
// Must seek back to beginning of file before reading
ASSERT_EQ(0, BIO_seek(bio_mem.get(), 0)); // 0 indicates success here
EXPECT_TRUE(BIO_read(bio_cipher.get(), decrypted_pt_vec.data(), decrypted_pt_vec.size()));
EXPECT_TRUE(BIO_get_cipher_status(bio_cipher.get()));
EXPECT_EQ(pt_vec.size(), decrypted_pt_vec.size());
EXPECT_EQ(Bytes(pt_vec.data(), pt_vec.size()),
Bytes(decrypted_pt_vec.data(), decrypted_pt_vec.size()));
bio_mem.release(); // |bio_cipher| took ownership


// TODO [childw] induce read failures?
}

0 comments on commit a7e3d1a

Please sign in to comment.