OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/cdm/json_web_key.h" | 5 #include "media/cdm/json_web_key.h" |
6 | 6 |
| 7 #include "base/base64.h" |
7 #include "base/logging.h" | 8 #include "base/logging.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
9 | 10 |
10 namespace media { | 11 namespace media { |
11 | 12 |
12 class JSONWebKeyTest : public testing::Test { | 13 class JSONWebKeyTest : public testing::Test { |
13 public: | 14 public: |
14 JSONWebKeyTest() {} | 15 JSONWebKeyTest() {} |
15 | 16 |
16 protected: | 17 protected: |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 " {" | 119 " {" |
119 " \"kty\": \"oct\"," | 120 " \"kty\": \"oct\"," |
120 " \"alg\": \"A128KW\"," | 121 " \"alg\": \"A128KW\"," |
121 " \"kid\": \"AAECAwQFBgcICQoLDA0ODxAREhM\"," | 122 " \"kid\": \"AAECAwQFBgcICQoLDA0ODxAREhM\"," |
122 " \"k\": \"FBUWFxgZGhscHR4fICEiIw\"" | 123 " \"k\": \"FBUWFxgZGhscHR4fICEiIw\"" |
123 " }," | 124 " }," |
124 " {" | 125 " {" |
125 " \"kty\": \"oct\"," | 126 " \"kty\": \"oct\"," |
126 " \"alg\": \"A128KW\"," | 127 " \"alg\": \"A128KW\"," |
127 " \"kid\": \"JCUmJygpKissLS4vMA\"," | 128 " \"kid\": \"JCUmJygpKissLS4vMA\"," |
128 " \"k\":\"MTIzNDU2Nzg5Ojs8PT4/QA\"" | 129 " \"k\":\"MTIzNDU2Nzg5Ojs8PT4_QA\"" |
129 " }" | 130 " }" |
130 " ]" | 131 " ]" |
131 "}"; | 132 "}"; |
132 ExtractJWKKeysAndExpect(kJwksMultipleEntries, true, 2); | 133 ExtractJWKKeysAndExpect(kJwksMultipleEntries, true, 2); |
133 | 134 |
134 // Try a key with no spaces and some \n plus additional fields. | 135 // Try a key with no spaces and some \n plus additional fields. |
135 const std::string kJwksNoSpaces = | 136 const std::string kJwksNoSpaces = |
136 "\n\n{\"something\":1,\"keys\":[{\n\n\"kty\":\"oct\",\"alg\":\"A128KW\"," | 137 "\n\n{\"something\":1,\"keys\":[{\n\n\"kty\":\"oct\",\"alg\":\"A128KW\"," |
137 "\"kid\":\"AAECAwQFBgcICQoLDA0ODxAREhM\",\"k\":\"GawgguFyGrWKav7AX4VKUg" | 138 "\"kid\":\"AAECAwQFBgcICQoLDA0ODxAREhM\",\"k\":\"GawgguFyGrWKav7AX4VKUg" |
138 "\",\"foo\":\"bar\"}]}\n\n"; | 139 "\",\"foo\":\"bar\"}]}\n\n"; |
139 ExtractJWKKeysAndExpect(kJwksNoSpaces, true, 1); | 140 ExtractJWKKeysAndExpect(kJwksNoSpaces, true, 1); |
140 | 141 |
141 // Try a list with multiple keys with the same kid. | 142 // Try a list with multiple keys with the same kid. |
142 const std::string kJwksDuplicateKids = | 143 const std::string kJwksDuplicateKids = |
143 "{" | 144 "{" |
144 " \"keys\": [" | 145 " \"keys\": [" |
145 " {" | 146 " {" |
146 " \"kty\": \"oct\"," | 147 " \"kty\": \"oct\"," |
147 " \"alg\": \"A128KW\"," | 148 " \"alg\": \"A128KW\"," |
148 " \"kid\": \"JCUmJygpKissLS4vMA\"," | 149 " \"kid\": \"JCUmJygpKissLS4vMA\"," |
149 " \"k\": \"FBUWFxgZGhscHR4fICEiIw\"" | 150 " \"k\": \"FBUWFxgZGhscHR4fICEiIw\"" |
150 " }," | 151 " }," |
151 " {" | 152 " {" |
152 " \"kty\": \"oct\"," | 153 " \"kty\": \"oct\"," |
153 " \"alg\": \"A128KW\"," | 154 " \"alg\": \"A128KW\"," |
154 " \"kid\": \"JCUmJygpKissLS4vMA\"," | 155 " \"kid\": \"JCUmJygpKissLS4vMA\"," |
155 " \"k\":\"MTIzNDU2Nzg5Ojs8PT4/QA\"" | 156 " \"k\":\"MTIzNDU2Nzg5Ojs8PT4_QA\"" |
156 " }" | 157 " }" |
157 " ]" | 158 " ]" |
158 "}"; | 159 "}"; |
159 ExtractJWKKeysAndExpect(kJwksDuplicateKids, true, 2); | 160 ExtractJWKKeysAndExpect(kJwksDuplicateKids, true, 2); |
160 } | 161 } |
161 | 162 |
162 TEST_F(JSONWebKeyTest, ExtractInvalidJWKKeys) { | 163 TEST_F(JSONWebKeyTest, ExtractInvalidJWKKeys) { |
163 // Try a simple JWK key (i.e. not in a set) | 164 // Try a simple JWK key (i.e. not in a set) |
164 const std::string kJwkSimple = | 165 const std::string kJwkSimple = |
165 "{" | 166 "{" |
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
464 // Correct tag, but empty list. | 465 // Correct tag, but empty list. |
465 ExtractKeyFromLicenseAndExpect("{\"kids\":[]}", false, NULL, 0); | 466 ExtractKeyFromLicenseAndExpect("{\"kids\":[]}", false, NULL, 0); |
466 | 467 |
467 // Correct tag, but list doesn't contain a string. | 468 // Correct tag, but list doesn't contain a string. |
468 ExtractKeyFromLicenseAndExpect("{\"kids\":[[\"AQI\"]]}", false, NULL, 0); | 469 ExtractKeyFromLicenseAndExpect("{\"kids\":[[\"AQI\"]]}", false, NULL, 0); |
469 | 470 |
470 // Correct tag, but invalid base64 encoding. | 471 // Correct tag, but invalid base64 encoding. |
471 ExtractKeyFromLicenseAndExpect("{\"kids\":[\"!@#$%^&*()\"]}", false, NULL, 0); | 472 ExtractKeyFromLicenseAndExpect("{\"kids\":[\"!@#$%^&*()\"]}", false, NULL, 0); |
472 } | 473 } |
473 | 474 |
| 475 TEST_F(JSONWebKeyTest, Base64UrlEncoding) { |
| 476 const uint8 data1[] = { 0xfb, 0xfd, 0xfb, 0xfd, 0xfb, 0xfd, 0xfb }; |
| 477 |
| 478 // Verify that |data1| contains invalid base64url characters '+' and '/' |
| 479 // and is padded with = when converted to base64. |
| 480 std::string encoded_text; |
| 481 base::Base64Encode( |
| 482 std::string(reinterpret_cast<const char*>(&data1[0]), arraysize(data1)), |
| 483 &encoded_text); |
| 484 EXPECT_EQ(encoded_text, "+/37/fv9+w=="); |
| 485 EXPECT_NE(encoded_text.find('+'), std::string::npos); |
| 486 EXPECT_NE(encoded_text.find('/'), std::string::npos); |
| 487 EXPECT_NE(encoded_text.find('='), std::string::npos); |
| 488 |
| 489 // base64url characters '-' and '_' not in base64 encoding. |
| 490 EXPECT_EQ(encoded_text.find('-'), std::string::npos); |
| 491 EXPECT_EQ(encoded_text.find('_'), std::string::npos); |
| 492 |
| 493 CreateLicenseAndExpect(data1, arraysize(data1), MediaKeys::TEMPORARY_SESSION, |
| 494 "{\"kids\":[\"-_37_fv9-w\"],\"type\":\"temporary\"}"); |
| 495 |
| 496 ExtractKeyFromLicenseAndExpect( |
| 497 "{\"kids\":[\"-_37_fv9-w\"],\"type\":\"temporary\"}", true, data1, |
| 498 arraysize(data1)); |
| 499 } |
| 500 |
474 } // namespace media | 501 } // namespace media |
475 | 502 |
OLD | NEW |