Chromium Code Reviews| 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) { | |
|
ddorwin
2015/02/19 04:45:56
Ideally, we'd have both special characters. We can
jrummell
2015/02/19 18:20:07
Found one.
| |
| 476 const uint8 data1[] = { 0x7E, 0x57, 0x1D, 0x03, 0x7E, 0x57, 0x1D, 0x03, | |
| 477 0x7E, 0x57, 0x1D, 0x03, 0x7E, 0x57, 0x1D, 0x03 }; | |
| 478 | |
| 479 // Verify that |data1| contains invalid base64url characters when converted | |
| 480 // to base64. | |
| 481 std::string encoded_text; | |
| 482 base::Base64Encode( | |
| 483 std::string(reinterpret_cast<const char*>(&data1[0]), arraysize(data1)), | |
| 484 &encoded_text); | |
| 485 EXPECT_NE(encoded_text.find('+'), std::string::npos); | |
|
ddorwin
2015/02/19 04:45:56
Does the encode give us padding? If so, we can che
jrummell
2015/02/19 18:20:07
Added extra case.
| |
| 486 EXPECT_EQ(encoded_text.find('-'), std::string::npos); | |
| 487 | |
| 488 CreateLicenseAndExpect( | |
| 489 data1, arraysize(data1), MediaKeys::TEMPORARY_SESSION, | |
| 490 "{\"kids\":[\"flcdA35XHQN-Vx0DflcdAw\"],\"type\":\"temporary\"}"); | |
| 491 | |
| 492 ExtractKeyFromLicenseAndExpect( | |
| 493 "{\"kids\":[\"flcdA35XHQN-Vx0DflcdAw\"],\"type\":\"temporary\"}", true, | |
| 494 data1, arraysize(data1)); | |
| 495 } | |
| 496 | |
| 474 } // namespace media | 497 } // namespace media |
| 475 | 498 |
| OLD | NEW |