[M120 Migration][VD] Enable direct rendering for TVPlus
[platform/framework/web/chromium-efl.git] / crypto / aead_unittest.cc
1 // Copyright 2015 The Chromium Authors
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,
15     crypto::Aead::AES_256_GCM,
16     crypto::Aead::AES_256_GCM_SIV,
17     crypto::Aead::CHACHA20_POLY1305,
18 };
19
20 class AeadTest : public testing::TestWithParam<crypto::Aead::AeadAlgorithm> {};
21
22 INSTANTIATE_TEST_SUITE_P(All, AeadTest, testing::ValuesIn(kAllAlgorithms));
23
24 TEST_P(AeadTest, SealOpen) {
25   crypto::Aead::AeadAlgorithm alg = GetParam();
26   crypto::Aead aead(alg);
27   std::string key(aead.KeyLength(), 0);
28   aead.Init(&key);
29   std::string nonce(aead.NonceLength(), 0);
30   std::string plaintext("this is the plaintext");
31   std::string ad("this is the additional data");
32   std::string ciphertext;
33   EXPECT_TRUE(aead.Seal(plaintext, nonce, ad, &ciphertext));
34   EXPECT_LT(0U, ciphertext.size());
35
36   std::string decrypted;
37   EXPECT_TRUE(aead.Open(ciphertext, nonce, ad, &decrypted));
38
39   EXPECT_EQ(plaintext, decrypted);
40 }
41
42 TEST_P(AeadTest, SealOpenSpan) {
43   crypto::Aead::AeadAlgorithm alg = GetParam();
44   crypto::Aead aead(alg);
45   std::vector<uint8_t> key(aead.KeyLength(), 0u);
46   aead.Init(key);
47   std::vector<uint8_t> nonce(aead.NonceLength(), 0u);
48   static constexpr uint8_t kPlaintext[] = "plaintext";
49   static constexpr uint8_t kAdditionalData[] = "additional data input";
50   std::vector<uint8_t> ciphertext =
51       aead.Seal(kPlaintext, nonce, kAdditionalData);
52   EXPECT_LT(sizeof(kPlaintext), ciphertext.size());
53
54   absl::optional<std::vector<uint8_t>> decrypted =
55       aead.Open(ciphertext, nonce, kAdditionalData);
56   ASSERT_TRUE(decrypted);
57   ASSERT_EQ(decrypted->size(), sizeof(kPlaintext));
58   ASSERT_EQ(0, memcmp(decrypted->data(), kPlaintext, sizeof(kPlaintext)));
59
60   std::vector<uint8_t> wrong_key(aead.KeyLength(), 1u);
61   crypto::Aead aead_wrong_key(alg);
62   aead_wrong_key.Init(wrong_key);
63   decrypted = aead_wrong_key.Open(ciphertext, nonce, kAdditionalData);
64   EXPECT_FALSE(decrypted);
65 }
66
67 TEST_P(AeadTest, SealOpenWrongKey) {
68   crypto::Aead::AeadAlgorithm alg = GetParam();
69   crypto::Aead aead(alg);
70   std::string key(aead.KeyLength(), 0);
71   std::string wrong_key(aead.KeyLength(), 1);
72   aead.Init(&key);
73   crypto::Aead aead_wrong_key(alg);
74   aead_wrong_key.Init(&wrong_key);
75
76   std::string nonce(aead.NonceLength(), 0);
77   std::string plaintext("this is the plaintext");
78   std::string ad("this is the additional data");
79   std::string ciphertext;
80   EXPECT_TRUE(aead.Seal(plaintext, nonce, ad, &ciphertext));
81   EXPECT_LT(0U, ciphertext.size());
82
83   std::string decrypted;
84   EXPECT_FALSE(aead_wrong_key.Open(ciphertext, nonce, ad, &decrypted));
85   EXPECT_EQ(0U, decrypted.size());
86 }
87
88 }  // namespace