Upload upstream chromium 69.0.3497
[platform/framework/web/chromium-efl.git] / crypto / aead_unittest.cc
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "crypto/aead.h"
6
7 #include <string>
8
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace {
12
13 const crypto::Aead::AeadAlgorithm kAllAlgorithms[]{
14     crypto::Aead::AES_128_CTR_HMAC_SHA256, crypto::Aead::AES_256_GCM,
15     crypto::Aead::AES_256_GCM_SIV,
16 };
17
18 class AeadTest : public testing::TestWithParam<crypto::Aead::AeadAlgorithm> {};
19
20 INSTANTIATE_TEST_CASE_P(, AeadTest, testing::ValuesIn(kAllAlgorithms));
21
22 TEST_P(AeadTest, SealOpen) {
23   crypto::Aead::AeadAlgorithm alg = GetParam();
24   crypto::Aead aead(alg);
25   std::string key(aead.KeyLength(), 0);
26   aead.Init(&key);
27   std::string nonce(aead.NonceLength(), 0);
28   std::string plaintext("this is the plaintext");
29   std::string ad("this is the additional data");
30   std::string ciphertext;
31   EXPECT_TRUE(aead.Seal(plaintext, nonce, ad, &ciphertext));
32   EXPECT_LT(0U, ciphertext.size());
33
34   std::string decrypted;
35   EXPECT_TRUE(aead.Open(ciphertext, nonce, ad, &decrypted));
36
37   EXPECT_EQ(plaintext, decrypted);
38 }
39
40 TEST_P(AeadTest, SealOpenWrongKey) {
41   crypto::Aead::AeadAlgorithm alg = GetParam();
42   crypto::Aead aead(alg);
43   std::string key(aead.KeyLength(), 0);
44   std::string wrong_key(aead.KeyLength(), 1);
45   aead.Init(&key);
46   crypto::Aead aead_wrong_key(alg);
47   aead_wrong_key.Init(&wrong_key);
48
49   std::string nonce(aead.NonceLength(), 0);
50   std::string plaintext("this is the plaintext");
51   std::string ad("this is the additional data");
52   std::string ciphertext;
53   EXPECT_TRUE(aead.Seal(plaintext, nonce, ad, &ciphertext));
54   EXPECT_LT(0U, ciphertext.size());
55
56   std::string decrypted;
57   EXPECT_FALSE(aead_wrong_key.Open(ciphertext, nonce, ad, &decrypted));
58   EXPECT_EQ(0U, decrypted.size());
59 }
60
61 }  // namespace