diff --git a/crypto/pkcs7/pkcs7_internal_bio_test.cc b/crypto/pkcs7/pkcs7_internal_bio_test.cc index 355bb84b5bc..16debd15b27 100644 --- a/crypto/pkcs7/pkcs7_internal_bio_test.cc +++ b/crypto/pkcs7/pkcs7_internal_bio_test.cc @@ -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)); @@ -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 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}) { @@ -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 @@ -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? }