OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 "media/formats/webm/webm_crypto_helpers.h" | |
chcunningham
2016/07/29 22:26:22
Thanks for writing great tests.
kqyang
2016/08/02 00:07:23
:)
| |
6 | |
7 #include "testing/gmock/include/gmock/gmock.h" | |
8 #include "testing/gtest/include/gtest/gtest.h" | |
9 | |
10 using ::testing::ElementsAre; | |
11 | |
12 namespace { | |
13 | |
14 const uint8_t kKeyId[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; | |
15 | |
16 } // namespace | |
17 | |
18 namespace media { | |
19 | |
20 inline bool operator==(const SubsampleEntry& lhs, const SubsampleEntry& rhs) { | |
21 return lhs.clear_bytes == rhs.clear_bytes && | |
22 lhs.cypher_bytes == rhs.cypher_bytes; | |
23 } | |
24 | |
25 TEST(WebMCryptoHelpersTest, EmptyData) { | |
26 std::unique_ptr<DecryptConfig> decrypt_config; | |
27 int data_offset; | |
28 ASSERT_FALSE(WebMCreateDecryptConfig(nullptr, 0, kKeyId, sizeof(kKeyId), | |
29 &decrypt_config, &data_offset)); | |
30 } | |
31 | |
32 TEST(WebMCryptoHelpersTest, ClearData) { | |
33 const uint8_t kData[] = {0x00, 0x0d, 0x0a, 0x0d, 0x0a}; | |
34 std::unique_ptr<DecryptConfig> decrypt_config; | |
35 int data_offset; | |
36 ASSERT_TRUE(WebMCreateDecryptConfig(kData, sizeof(kData), kKeyId, | |
37 sizeof(kKeyId), &decrypt_config, | |
38 &data_offset)); | |
39 EXPECT_EQ(1, data_offset); | |
40 EXPECT_FALSE(decrypt_config->is_encrypted()); | |
41 } | |
42 | |
43 TEST(WebMCryptoHelpersTest, EncryptedButNotEnoughBytes) { | |
44 const uint8_t kData[] = {0x01, 0x0d, 0x0a, 0x0d, 0x0a}; | |
45 std::unique_ptr<DecryptConfig> decrypt_config; | |
46 int data_offset; | |
47 ASSERT_FALSE(WebMCreateDecryptConfig(kData, sizeof(kData), kKeyId, | |
48 sizeof(kKeyId), &decrypt_config, | |
49 &data_offset)); | |
50 } | |
51 | |
52 TEST(WebMCryptoHelpersTest, EncryptedNotPartitioned) { | |
53 const uint8_t kData[] = { | |
54 // Encrypted | |
55 0x01, | |
56 // IV | |
57 0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a, | |
58 // Data | |
59 0x01, 0x02, | |
60 }; | |
61 // Extracted from kData and zero extended to 16 bytes. | |
62 const uint8_t kExpectedIv[] = { | |
63 0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a, | |
64 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
chcunningham
2016/07/29 21:32:42
Just curious, why do we extend to 16 bytes?
kqyang
2016/08/02 00:07:23
Some CDMs expect IV to be 16 bytes: https://cs.chr
| |
65 }; | |
66 std::unique_ptr<DecryptConfig> decrypt_config; | |
67 int data_offset; | |
68 ASSERT_TRUE(WebMCreateDecryptConfig(kData, sizeof(kData), kKeyId, | |
69 sizeof(kKeyId), &decrypt_config, | |
70 &data_offset)); | |
71 EXPECT_TRUE(decrypt_config->is_encrypted()); | |
72 EXPECT_EQ(std::string(kKeyId, kKeyId + sizeof(kKeyId)), | |
73 decrypt_config->key_id()); | |
74 EXPECT_EQ(std::string(kExpectedIv, kExpectedIv + sizeof(kExpectedIv)), | |
75 decrypt_config->iv()); | |
76 EXPECT_TRUE(decrypt_config->subsamples().empty()); | |
77 } | |
78 | |
79 TEST(WebMCryptoHelpersTest, EncryptedPartitionedMissingNumPartitionField) { | |
80 const uint8_t kData[] = { | |
81 // Encrypted and Partitioned | |
82 0x03, | |
83 // IV | |
84 0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a, | |
85 }; | |
86 std::unique_ptr<DecryptConfig> decrypt_config; | |
87 int data_offset; | |
88 ASSERT_FALSE(WebMCreateDecryptConfig(kData, sizeof(kData), kKeyId, | |
89 sizeof(kKeyId), &decrypt_config, | |
90 &data_offset)); | |
91 } | |
92 | |
93 TEST(WebMCryptoHelpersTest, EncryptedPartitionedNotEnoughBytesForOffsets) { | |
94 const uint8_t kData[] = { | |
95 // Encrypted and Partitioned | |
96 0x03, | |
97 // IV | |
98 0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a, | |
99 // Num partitions = 2 | |
100 0x02, | |
101 // Partition 0 @ offset 3 | |
102 0x00, 0x00, 0x00, 0x03, | |
103 }; | |
104 std::unique_ptr<DecryptConfig> decrypt_config; | |
105 int data_offset; | |
106 ASSERT_FALSE(WebMCreateDecryptConfig(kData, sizeof(kData), kKeyId, | |
107 sizeof(kKeyId), &decrypt_config, | |
108 &data_offset)); | |
109 } | |
110 | |
111 TEST(WebMCryptoHelpersTest, EncryptedPartitionedNotEnoughBytesForData) { | |
112 const uint8_t kData[] = { | |
113 // Encrypted and Partitioned | |
114 0x03, | |
115 // IV | |
116 0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a, | |
117 // Num partitions = 2 | |
118 0x02, | |
119 // Partition 0 @ offset 3, partition 2 @ offset 5 | |
120 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x05, | |
121 // Should have more than 5 bytes of data | |
122 0x00, 0x01, 0x02, 0x03, | |
123 }; | |
124 std::unique_ptr<DecryptConfig> decrypt_config; | |
125 int data_offset; | |
126 ASSERT_FALSE(WebMCreateDecryptConfig(kData, sizeof(kData), kKeyId, | |
127 sizeof(kKeyId), &decrypt_config, | |
128 &data_offset)); | |
129 } | |
130 | |
131 TEST(WebMCryptoHelpersTest, EncryptedPartitionedNotEnoughBytesForData2) { | |
132 const uint8_t kData[] = { | |
133 // Encrypted and Partitioned | |
134 0x03, | |
135 // IV | |
136 0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a, | |
137 // Num partitions = 2 | |
138 0x02, | |
139 // Partition 0 @ offset 3, partition 1 @ offset 5 | |
140 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x05, | |
141 // Should have more than 5 bytes of data | |
142 0x00, 0x01, 0x02, 0x03, 0x04, | |
143 }; | |
144 std::unique_ptr<DecryptConfig> decrypt_config; | |
145 int data_offset; | |
146 ASSERT_FALSE(WebMCreateDecryptConfig(kData, sizeof(kData), kKeyId, | |
147 sizeof(kKeyId), &decrypt_config, | |
148 &data_offset)); | |
149 } | |
150 | |
151 TEST(WebMCryptoHelpersTest, EncryptedPartitionedDecreasingOffsets) { | |
152 const uint8_t kData[] = { | |
153 // Encrypted and Partitioned | |
154 0x03, | |
155 // IV | |
156 0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a, | |
157 // Num partitions = 2 | |
158 0x02, | |
159 // Partition 0 @ offset 3, partition 1 @ offset 2 | |
160 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x02, | |
161 // Should have more than 5 bytes of data | |
162 0x00, 0x01, 0x02, 0x03, 0x04, | |
163 }; | |
164 std::unique_ptr<DecryptConfig> decrypt_config; | |
165 int data_offset; | |
166 ASSERT_FALSE(WebMCreateDecryptConfig(kData, sizeof(kData), kKeyId, | |
167 sizeof(kKeyId), &decrypt_config, | |
168 &data_offset)); | |
169 } | |
170 | |
171 TEST(WebMCryptoHelpersTest, EncryptedPartitionedEvenNumberOfPartitions) { | |
172 const uint8_t kData[] = { | |
173 // Encrypted and Partitioned | |
174 0x03, | |
175 // IV | |
176 0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a, | |
177 // Num partitions = 2 | |
178 0x02, | |
179 // Partition 0 @ offset 3, partition 1 @ offset 5 | |
180 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x05, | |
181 // Should have more than 5 bytes of data | |
182 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, | |
183 }; | |
184 // Extracted from kData and zero extended to 16 bytes. | |
185 const uint8_t kExpectedIv[] = { | |
186 0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a, | |
187 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
188 }; | |
189 std::unique_ptr<DecryptConfig> decrypt_config; | |
190 int data_offset; | |
191 ASSERT_TRUE(WebMCreateDecryptConfig(kData, sizeof(kData), kKeyId, | |
192 sizeof(kKeyId), &decrypt_config, | |
193 &data_offset)); | |
194 EXPECT_TRUE(decrypt_config->is_encrypted()); | |
195 EXPECT_EQ(std::string(kKeyId, kKeyId + sizeof(kKeyId)), | |
196 decrypt_config->key_id()); | |
197 EXPECT_EQ(std::string(kExpectedIv, kExpectedIv + sizeof(kExpectedIv)), | |
198 decrypt_config->iv()); | |
199 EXPECT_THAT(decrypt_config->subsamples(), | |
200 ElementsAre(SubsampleEntry(3, 2), SubsampleEntry(1, 0))); | |
201 EXPECT_EQ(18, data_offset); | |
202 } | |
203 | |
204 TEST(WebMCryptoHelpersTest, EncryptedPartitionedOddNumberOfPartitions) { | |
205 const uint8_t kData[] = { | |
206 // Encrypted and Partitioned | |
207 0x03, | |
208 // IV | |
209 0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a, | |
210 // Num partitions = 1 | |
211 0x01, | |
212 // Partition 0 @ offset 3, | |
213 0x00, 0x00, 0x00, 0x03, | |
214 // Should have more than 3 bytes of data | |
215 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, | |
216 }; | |
217 // Extracted from kData and zero extended to 16 bytes. | |
218 const uint8_t kExpectedIv[] = { | |
219 0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a, | |
220 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
221 }; | |
222 std::unique_ptr<DecryptConfig> decrypt_config; | |
223 int data_offset; | |
224 ASSERT_TRUE(WebMCreateDecryptConfig(kData, sizeof(kData), kKeyId, | |
225 sizeof(kKeyId), &decrypt_config, | |
226 &data_offset)); | |
227 EXPECT_TRUE(decrypt_config->is_encrypted()); | |
228 EXPECT_EQ(std::string(kKeyId, kKeyId + sizeof(kKeyId)), | |
229 decrypt_config->key_id()); | |
230 EXPECT_EQ(std::string(kExpectedIv, kExpectedIv + sizeof(kExpectedIv)), | |
231 decrypt_config->iv()); | |
232 EXPECT_THAT(decrypt_config->subsamples(), ElementsAre(SubsampleEntry(3, 3))); | |
233 EXPECT_EQ(14, data_offset); | |
234 } | |
235 | |
236 } // namespace media | |
OLD | NEW |