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_encoding_parser.h"
9 
10 #include "gtest/gtest.h"
11 
12 #include "test_utils/element_parser_test.h"
13 #include "webm/id.h"
14 
15 using webm::ContentEncAlgo;
16 using webm::ContentEncoding;
17 using webm::ContentEncodingParser;
18 using webm::ContentEncodingType;
19 using webm::ContentEncryption;
20 using webm::ElementParserTest;
21 using webm::Id;
22 
23 namespace {
24 
25 class ContentEncodingParserTest
26     : public ElementParserTest<ContentEncodingParser, Id::kContentEncoding> {};
27 
TEST_F(ContentEncodingParserTest,DefaultParse)28 TEST_F(ContentEncodingParserTest, DefaultParse) {
29   ParseAndVerify();
30 
31   const ContentEncoding content_encoding = parser_.value();
32 
33   EXPECT_FALSE(content_encoding.order.is_present());
34   EXPECT_EQ(static_cast<std::uint64_t>(0), content_encoding.order.value());
35 
36   EXPECT_FALSE(content_encoding.scope.is_present());
37   EXPECT_EQ(static_cast<std::uint64_t>(1), content_encoding.scope.value());
38 
39   EXPECT_FALSE(content_encoding.type.is_present());
40   EXPECT_EQ(ContentEncodingType::kCompression, content_encoding.type.value());
41 
42   EXPECT_FALSE(content_encoding.encryption.is_present());
43   EXPECT_EQ(ContentEncryption{}, content_encoding.encryption.value());
44 }
45 
TEST_F(ContentEncodingParserTest,DefaultValues)46 TEST_F(ContentEncodingParserTest, DefaultValues) {
47   SetReaderData({
48       0x50, 0x31,  // ID = 0x5031 (ContentEncodingOrder).
49       0x80,  // Size = 0.
50 
51       0x50, 0x32,  // ID = 0x5032 (ContentEncodingScope).
52       0x80,  // Size = 0.
53 
54       0x50, 0x33,  // ID = 0x5033 (ContentEncodingType).
55       0x80,  // Size = 0.
56 
57       0x50, 0x35,  // ID = 0x5035 (ContentEncryption).
58       0x80,  // Size = 0.
59   });
60 
61   ParseAndVerify();
62 
63   const ContentEncoding content_encoding = parser_.value();
64 
65   EXPECT_TRUE(content_encoding.order.is_present());
66   EXPECT_EQ(static_cast<std::uint64_t>(0), content_encoding.order.value());
67 
68   EXPECT_TRUE(content_encoding.scope.is_present());
69   EXPECT_EQ(static_cast<std::uint64_t>(1), content_encoding.scope.value());
70 
71   EXPECT_TRUE(content_encoding.type.is_present());
72   EXPECT_EQ(ContentEncodingType::kCompression, content_encoding.type.value());
73 
74   EXPECT_TRUE(content_encoding.encryption.is_present());
75   EXPECT_EQ(ContentEncryption{}, content_encoding.encryption.value());
76 }
77 
TEST_F(ContentEncodingParserTest,CustomValues)78 TEST_F(ContentEncodingParserTest, CustomValues) {
79   SetReaderData({
80       0x50, 0x31,  // ID = 0x5031 (ContentEncodingOrder).
81       0x81,  // Size = 1.
82       0x01,  // Body (value = 1).
83 
84       0x50, 0x32,  // ID = 0x5032 (ContentEncodingScope).
85       0x81,  // Size = 1.
86       0x02,  // Body (value = 2).
87 
88       0x50, 0x33,  // ID = 0x5033 (ContentEncodingType).
89       0x81,  // Size = 1.
90       0x01,  // Body (value = encryption).
91 
92       0x50, 0x35,  // ID = 0x5035 (ContentEncryption).
93       0x84,  // Size = 4.
94 
95       0x47, 0xE1,  //   ID = 0x47E1 (ContentEncAlgo).
96       0x81,  //   Size = 1.
97       0x05,  //   Body (value = AES).
98   });
99 
100   ParseAndVerify();
101 
102   const ContentEncoding content_encoding = parser_.value();
103 
104   EXPECT_TRUE(content_encoding.order.is_present());
105   EXPECT_EQ(static_cast<std::uint64_t>(1), content_encoding.order.value());
106 
107   EXPECT_TRUE(content_encoding.scope.is_present());
108   EXPECT_EQ(static_cast<std::uint64_t>(2), content_encoding.scope.value());
109 
110   EXPECT_TRUE(content_encoding.type.is_present());
111   EXPECT_EQ(ContentEncodingType::kEncryption, content_encoding.type.value());
112 
113   ContentEncryption expected;
114   expected.algorithm.Set(ContentEncAlgo::kAes, true);
115 
116   EXPECT_TRUE(content_encoding.encryption.is_present());
117   EXPECT_EQ(expected, content_encoding.encryption.value());
118 }
119 
120 }  // namespace
121