Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(361)

Side by Side Diff: net/quic/core/crypto/chacha20_poly1305_encrypter_test.cc

Issue 2848203002: Add a platform implementation of QuicTest and QuicTestWithParam (Closed)
Patch Set: net/quic/platform/impl/quic_test_impl.cc Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/quic/core/crypto/chacha20_poly1305_encrypter.h" 5 #include "net/quic/core/crypto/chacha20_poly1305_encrypter.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "net/quic/core/crypto/chacha20_poly1305_decrypter.h" 9 #include "net/quic/core/crypto/chacha20_poly1305_decrypter.h"
10 #include "net/quic/core/quic_utils.h" 10 #include "net/quic/core/quic_utils.h"
11 #include "net/quic/platform/api/quic_test.h"
11 #include "net/quic/platform/api/quic_text_utils.h" 12 #include "net/quic/platform/api/quic_text_utils.h"
12 #include "net/quic/test_tools/quic_test_utils.h" 13 #include "net/quic/test_tools/quic_test_utils.h"
13 14
14 using std::string; 15 using std::string;
15 16
16 namespace { 17 namespace {
17 18
18 // The test vectors come from RFC 7539 Section 2.8.2. 19 // The test vectors come from RFC 7539 Section 2.8.2.
19 20
20 // Each test vector consists of five strings of lowercase hexadecimal digits. 21 // Each test vector consists of five strings of lowercase hexadecimal digits.
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 std::unique_ptr<char[]> ciphertext(new char[ciphertext_size]); 77 std::unique_ptr<char[]> ciphertext(new char[ciphertext_size]);
77 78
78 if (!encrypter->Encrypt(nonce, associated_data, plaintext, 79 if (!encrypter->Encrypt(nonce, associated_data, plaintext,
79 reinterpret_cast<unsigned char*>(ciphertext.get()))) { 80 reinterpret_cast<unsigned char*>(ciphertext.get()))) {
80 return nullptr; 81 return nullptr;
81 } 82 }
82 83
83 return new QuicData(ciphertext.release(), ciphertext_size, true); 84 return new QuicData(ciphertext.release(), ciphertext_size, true);
84 } 85 }
85 86
86 TEST(ChaCha20Poly1305EncrypterTest, EncryptThenDecrypt) { 87 class ChaCha20Poly1305EncrypterTest : public QuicTest {};
88
89 TEST_F(ChaCha20Poly1305EncrypterTest, EncryptThenDecrypt) {
87 ChaCha20Poly1305Encrypter encrypter; 90 ChaCha20Poly1305Encrypter encrypter;
88 ChaCha20Poly1305Decrypter decrypter; 91 ChaCha20Poly1305Decrypter decrypter;
89 92
90 string key = QuicTextUtils::HexDecode(test_vectors[0].key); 93 string key = QuicTextUtils::HexDecode(test_vectors[0].key);
91 ASSERT_TRUE(encrypter.SetKey(key)); 94 ASSERT_TRUE(encrypter.SetKey(key));
92 ASSERT_TRUE(decrypter.SetKey(key)); 95 ASSERT_TRUE(decrypter.SetKey(key));
93 ASSERT_TRUE(encrypter.SetNoncePrefix("abcd")); 96 ASSERT_TRUE(encrypter.SetNoncePrefix("abcd"));
94 ASSERT_TRUE(decrypter.SetNoncePrefix("abcd")); 97 ASSERT_TRUE(decrypter.SetNoncePrefix("abcd"));
95 98
96 QuicPacketNumber packet_number = UINT64_C(0x123456789ABC); 99 QuicPacketNumber packet_number = UINT64_C(0x123456789ABC);
97 string associated_data = "associated_data"; 100 string associated_data = "associated_data";
98 string plaintext = "plaintext"; 101 string plaintext = "plaintext";
99 char encrypted[1024]; 102 char encrypted[1024];
100 size_t len; 103 size_t len;
101 ASSERT_TRUE(encrypter.EncryptPacket(QuicVersionMax(), packet_number, 104 ASSERT_TRUE(encrypter.EncryptPacket(QuicVersionMax(), packet_number,
102 associated_data, plaintext, encrypted, 105 associated_data, plaintext, encrypted,
103 &len, arraysize(encrypted))); 106 &len, arraysize(encrypted)));
104 QuicStringPiece ciphertext(encrypted, len); 107 QuicStringPiece ciphertext(encrypted, len);
105 char decrypted[1024]; 108 char decrypted[1024];
106 ASSERT_TRUE(decrypter.DecryptPacket(QuicVersionMax(), packet_number, 109 ASSERT_TRUE(decrypter.DecryptPacket(QuicVersionMax(), packet_number,
107 associated_data, ciphertext, decrypted, 110 associated_data, ciphertext, decrypted,
108 &len, arraysize(decrypted))); 111 &len, arraysize(decrypted)));
109 } 112 }
110 113
111 TEST(ChaCha20Poly1305EncrypterTest, Encrypt) { 114 TEST_F(ChaCha20Poly1305EncrypterTest, Encrypt) {
112 for (size_t i = 0; test_vectors[i].key != nullptr; i++) { 115 for (size_t i = 0; test_vectors[i].key != nullptr; i++) {
113 // Decode the test vector. 116 // Decode the test vector.
114 string key = QuicTextUtils::HexDecode(test_vectors[i].key); 117 string key = QuicTextUtils::HexDecode(test_vectors[i].key);
115 string pt = QuicTextUtils::HexDecode(test_vectors[i].pt); 118 string pt = QuicTextUtils::HexDecode(test_vectors[i].pt);
116 string iv = QuicTextUtils::HexDecode(test_vectors[i].iv); 119 string iv = QuicTextUtils::HexDecode(test_vectors[i].iv);
117 string fixed = QuicTextUtils::HexDecode(test_vectors[i].fixed); 120 string fixed = QuicTextUtils::HexDecode(test_vectors[i].fixed);
118 string aad = QuicTextUtils::HexDecode(test_vectors[i].aad); 121 string aad = QuicTextUtils::HexDecode(test_vectors[i].aad);
119 string ct = QuicTextUtils::HexDecode(test_vectors[i].ct); 122 string ct = QuicTextUtils::HexDecode(test_vectors[i].ct);
120 123
121 ChaCha20Poly1305Encrypter encrypter; 124 ChaCha20Poly1305Encrypter encrypter;
122 ASSERT_TRUE(encrypter.SetKey(key)); 125 ASSERT_TRUE(encrypter.SetKey(key));
123 std::unique_ptr<QuicData> encrypted(EncryptWithNonce( 126 std::unique_ptr<QuicData> encrypted(EncryptWithNonce(
124 &encrypter, fixed + iv, 127 &encrypter, fixed + iv,
125 // This deliberately tests that the encrypter can handle an AAD that 128 // This deliberately tests that the encrypter can handle an AAD that
126 // is set to nullptr, as opposed to a zero-length, non-nullptr pointer. 129 // is set to nullptr, as opposed to a zero-length, non-nullptr pointer.
127 QuicStringPiece(aad.length() ? aad.data() : nullptr, aad.length()), 130 QuicStringPiece(aad.length() ? aad.data() : nullptr, aad.length()),
128 pt)); 131 pt));
129 ASSERT_TRUE(encrypted.get()); 132 ASSERT_TRUE(encrypted.get());
130 EXPECT_EQ(12u, ct.size() - pt.size()); 133 EXPECT_EQ(12u, ct.size() - pt.size());
131 EXPECT_EQ(12u, encrypted->length() - pt.size()); 134 EXPECT_EQ(12u, encrypted->length() - pt.size());
132 135
133 test::CompareCharArraysWithHexError("ciphertext", encrypted->data(), 136 test::CompareCharArraysWithHexError("ciphertext", encrypted->data(),
134 encrypted->length(), ct.data(), 137 encrypted->length(), ct.data(),
135 ct.length()); 138 ct.length());
136 } 139 }
137 } 140 }
138 141
139 TEST(ChaCha20Poly1305EncrypterTest, GetMaxPlaintextSize) { 142 TEST_F(ChaCha20Poly1305EncrypterTest, GetMaxPlaintextSize) {
140 ChaCha20Poly1305Encrypter encrypter; 143 ChaCha20Poly1305Encrypter encrypter;
141 EXPECT_EQ(1000u, encrypter.GetMaxPlaintextSize(1012)); 144 EXPECT_EQ(1000u, encrypter.GetMaxPlaintextSize(1012));
142 EXPECT_EQ(100u, encrypter.GetMaxPlaintextSize(112)); 145 EXPECT_EQ(100u, encrypter.GetMaxPlaintextSize(112));
143 EXPECT_EQ(10u, encrypter.GetMaxPlaintextSize(22)); 146 EXPECT_EQ(10u, encrypter.GetMaxPlaintextSize(22));
144 } 147 }
145 148
146 TEST(ChaCha20Poly1305EncrypterTest, GetCiphertextSize) { 149 TEST_F(ChaCha20Poly1305EncrypterTest, GetCiphertextSize) {
147 ChaCha20Poly1305Encrypter encrypter; 150 ChaCha20Poly1305Encrypter encrypter;
148 EXPECT_EQ(1012u, encrypter.GetCiphertextSize(1000)); 151 EXPECT_EQ(1012u, encrypter.GetCiphertextSize(1000));
149 EXPECT_EQ(112u, encrypter.GetCiphertextSize(100)); 152 EXPECT_EQ(112u, encrypter.GetCiphertextSize(100));
150 EXPECT_EQ(22u, encrypter.GetCiphertextSize(10)); 153 EXPECT_EQ(22u, encrypter.GetCiphertextSize(10));
151 } 154 }
152 155
153 } // namespace test 156 } // namespace test
154 } // namespace net 157 } // namespace net
OLDNEW
« no previous file with comments | « net/quic/core/crypto/chacha20_poly1305_decrypter_test.cc ('k') | net/quic/core/crypto/channel_id_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698