1 // Copyright (c) 2016 The WebM project authors. All Rights Reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the LICENSE file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS.  All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 #include "src/content_encryption_parser.h"
9 
10 #include <cstdint>
11 
12 #include "gtest/gtest.h"
13 
14 #include "test_utils/element_parser_test.h"
15 #include "webm/id.h"
16 
17 using webm::AesSettingsCipherMode;
18 using webm::ContentEncAesSettings;
19 using webm::ContentEncAlgo;
20 using webm::ContentEncryption;
21 using webm::ContentEncryptionParser;
22 using webm::ElementParserTest;
23 using webm::Id;
24 
25 namespace {
26 
27 class ContentEncryptionParserTest
28     : public ElementParserTest<ContentEncryptionParser,
29                                Id::kContentEncryption> {};
30 
TEST_F(ContentEncryptionParserTest,DefaultParse)31 TEST_F(ContentEncryptionParserTest, DefaultParse) {
32   ParseAndVerify();
33 
34   const ContentEncryption content_encryption = parser_.value();
35 
36   EXPECT_FALSE(content_encryption.algorithm.is_present());
37   EXPECT_EQ(ContentEncAlgo::kOnlySigned, content_encryption.algorithm.value());
38 
39   EXPECT_FALSE(content_encryption.key_id.is_present());
40   EXPECT_EQ(std::vector<std::uint8_t>{}, content_encryption.key_id.value());
41 
42   EXPECT_FALSE(content_encryption.aes_settings.is_present());
43   EXPECT_EQ(ContentEncAesSettings{}, content_encryption.aes_settings.value());
44 }
45 
TEST_F(ContentEncryptionParserTest,DefaultValues)46 TEST_F(ContentEncryptionParserTest, DefaultValues) {
47   SetReaderData({
48       0x47, 0xE1,  // ID = 0x47E1 (ContentEncAlgo).
49       0x80,  // Size = 0.
50 
51       0x47, 0xE2,  // ID = 0x47E2 (ContentEncKeyID).
52       0x80,  // Size = 0.
53 
54       0x47, 0xE7,  // ID = 0x47E7 (ContentEncAESSettings).
55       0x80,  // Size = 0.
56   });
57 
58   ParseAndVerify();
59 
60   const ContentEncryption content_encryption = parser_.value();
61 
62   EXPECT_TRUE(content_encryption.algorithm.is_present());
63   EXPECT_EQ(ContentEncAlgo::kOnlySigned, content_encryption.algorithm.value());
64 
65   EXPECT_TRUE(content_encryption.key_id.is_present());
66   EXPECT_EQ(std::vector<std::uint8_t>{}, content_encryption.key_id.value());
67 
68   EXPECT_TRUE(content_encryption.aes_settings.is_present());
69   EXPECT_EQ(ContentEncAesSettings{}, content_encryption.aes_settings.value());
70 }
71 
TEST_F(ContentEncryptionParserTest,CustomValues)72 TEST_F(ContentEncryptionParserTest, CustomValues) {
73   SetReaderData({
74       0x47, 0xE1,  // ID = 0x47E1 (ContentEncAlgo).
75       0x81,  // Size = 1.
76       0x05,  // Body (value = AES).
77 
78       0x47, 0xE2,  // ID = 0x47E2 (ContentEncKeyID).
79       0x81,  // Size = 1.
80       0x00,  // Body.
81 
82       0x47, 0xE7,  // ID = 0x47E7 (ContentEncAESSettings).
83       0x84,  // Size = 4.
84 
85       0x47, 0xE8,  //   ID = 0x47E8 (AESSettingsCipherMode).
86       0x81,  //   Size = 1.
87       0x00,  //   Body (value = 0).
88   });
89 
90   ParseAndVerify();
91 
92   const ContentEncryption content_encryption = parser_.value();
93 
94   EXPECT_TRUE(content_encryption.algorithm.is_present());
95   EXPECT_EQ(ContentEncAlgo::kAes, content_encryption.algorithm.value());
96 
97   EXPECT_TRUE(content_encryption.key_id.is_present());
98   EXPECT_EQ(std::vector<std::uint8_t>{0x00}, content_encryption.key_id.value());
99 
100   ContentEncAesSettings expected;
101   expected.aes_settings_cipher_mode.Set(static_cast<AesSettingsCipherMode>(0),
102                                         true);
103   EXPECT_TRUE(content_encryption.aes_settings.is_present());
104   EXPECT_EQ(expected, content_encryption.aes_settings.value());
105 }
106 
107 }  // namespace
108