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/base64.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 } | 53 } |
54 | 54 |
55 void ExtractKeyFromLicenseAndExpect(const std::string& license, | 55 void ExtractKeyFromLicenseAndExpect(const std::string& license, |
56 bool expected_result, | 56 bool expected_result, |
57 const uint8* expected_key, | 57 const uint8* expected_key, |
58 int expected_key_length) { | 58 int expected_key_length) { |
59 std::vector<uint8> license_vector(license.begin(), license.end()); | 59 std::vector<uint8> license_vector(license.begin(), license.end()); |
60 std::vector<uint8> key; | 60 std::vector<uint8> key; |
61 EXPECT_EQ(expected_result, | 61 EXPECT_EQ(expected_result, |
62 ExtractFirstKeyIdFromLicenseRequest(license_vector, &key)); | 62 ExtractFirstKeyIdFromLicenseRequest(license_vector, &key)); |
63 if (expected_result) { | 63 if (expected_result) |
64 std::vector<uint8> key_result(expected_key, | 64 VerifyKeyId(key, expected_key, expected_key_length); |
65 expected_key + expected_key_length); | 65 } |
66 EXPECT_EQ(key_result, key); | 66 |
67 } | 67 void VerifyKeyId(std::vector<uint8> key, |
| 68 const uint8* expected_key, |
| 69 int expected_key_length) { |
| 70 std::vector<uint8> key_result(expected_key, |
| 71 expected_key + expected_key_length); |
| 72 EXPECT_EQ(key_result, key); |
68 } | 73 } |
69 }; | 74 }; |
70 | 75 |
71 TEST_F(JSONWebKeyTest, GenerateJWKSet) { | 76 TEST_F(JSONWebKeyTest, GenerateJWKSet) { |
72 const uint8 data1[] = { 0x01, 0x02 }; | 77 const uint8 data1[] = { 0x01, 0x02 }; |
73 const uint8 data2[] = { 0x01, 0x02, 0x03, 0x04 }; | 78 const uint8 data2[] = { 0x01, 0x02, 0x03, 0x04 }; |
74 const uint8 data3[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, | 79 const uint8 data3[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
75 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10 }; | 80 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10 }; |
76 | 81 |
77 EXPECT_EQ( | 82 EXPECT_EQ( |
(...skipping 434 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
512 key_ids.push_back(std::vector<uint8>(data2, data2 + arraysize(data2))); | 517 key_ids.push_back(std::vector<uint8>(data2, data2 + arraysize(data2))); |
513 key_ids.push_back(std::vector<uint8>(data3, data3 + arraysize(data3))); | 518 key_ids.push_back(std::vector<uint8>(data3, data3 + arraysize(data3))); |
514 CreateLicenseRequest(key_ids, MediaKeys::TEMPORARY_SESSION, &result); | 519 CreateLicenseRequest(key_ids, MediaKeys::TEMPORARY_SESSION, &result); |
515 std::string s(result.begin(), result.end()); | 520 std::string s(result.begin(), result.end()); |
516 EXPECT_EQ( | 521 EXPECT_EQ( |
517 "{\"kids\":[\"AQI\",\"AQIDBA\",\"AQIDBAUGBwgJCgsMDQ4PEA\"],\"type\":" | 522 "{\"kids\":[\"AQI\",\"AQIDBA\",\"AQIDBAUGBwgJCgsMDQ4PEA\"],\"type\":" |
518 "\"temporary\"}", | 523 "\"temporary\"}", |
519 s); | 524 s); |
520 } | 525 } |
521 | 526 |
| 527 TEST_F(JSONWebKeyTest, ExtractKeyIds) { |
| 528 const uint8 data1[] = { 0x01, 0x02 }; |
| 529 const uint8 data2[] = { 0x01, 0x02, 0x03, 0x04 }; |
| 530 const uint8 data3[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 531 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10 }; |
| 532 |
| 533 KeyIdList key_ids; |
| 534 std::string error_message; |
| 535 |
| 536 EXPECT_TRUE(ExtractKeyIdsFromKeyIdsInitData("{\"kids\":[\"AQI\"]}", &key_ids, |
| 537 &error_message)); |
| 538 EXPECT_EQ(1u, key_ids.size()); |
| 539 EXPECT_EQ(0u, error_message.length()); |
| 540 VerifyKeyId(key_ids[0], data1, arraysize(data1)); |
| 541 |
| 542 EXPECT_TRUE(ExtractKeyIdsFromKeyIdsInitData( |
| 543 "{\"kids\":[\"AQI\",\"AQIDBA\",\"AQIDBAUGBwgJCgsMDQ4PEA\"]}", &key_ids, |
| 544 &error_message)); |
| 545 EXPECT_EQ(3u, key_ids.size()); |
| 546 EXPECT_EQ(0u, error_message.length()); |
| 547 VerifyKeyId(key_ids[0], data1, arraysize(data1)); |
| 548 VerifyKeyId(key_ids[1], data2, arraysize(data2)); |
| 549 VerifyKeyId(key_ids[2], data3, arraysize(data3)); |
| 550 |
| 551 // Expect failure when non-ascii. |
| 552 EXPECT_FALSE(ExtractKeyIdsFromKeyIdsInitData( |
| 553 "This is not ASCII due to \xff\xfe\x0a in it.", &key_ids, |
| 554 &error_message)); |
| 555 EXPECT_EQ(3u, key_ids.size()); // |key_ids| should be unchanged. |
| 556 EXPECT_EQ(error_message, |
| 557 "Non ASCII: This is not ASCII due to \\u00FF\\u00FE\\n in it."); |
| 558 |
| 559 // Expect failure when not JSON or not a dictionary. |
| 560 EXPECT_FALSE(ExtractKeyIdsFromKeyIdsInitData("This is invalid.", &key_ids, |
| 561 &error_message)); |
| 562 EXPECT_EQ(3u, key_ids.size()); // |key_ids| should be unchanged. |
| 563 EXPECT_EQ(error_message, "Not valid JSON: This is invalid."); |
| 564 EXPECT_FALSE(ExtractKeyIdsFromKeyIdsInitData("6", &key_ids, &error_message)); |
| 565 EXPECT_EQ(3u, key_ids.size()); // |key_ids| should be unchanged. |
| 566 EXPECT_EQ(error_message, "Not valid JSON: 6"); |
| 567 EXPECT_FALSE(ExtractKeyIdsFromKeyIdsInitData( |
| 568 "This is a very long string that is longer than 64 characters and is " |
| 569 "invalid.", |
| 570 &key_ids, &error_message)); |
| 571 EXPECT_EQ(3u, key_ids.size()); // |key_ids| should be unchanged. |
| 572 EXPECT_EQ(error_message, |
| 573 "Not valid JSON: This is a very long string that is longer than 64 " |
| 574 "characters ..."); |
| 575 |
| 576 // Expect failure when "kids" not specified. |
| 577 EXPECT_FALSE(ExtractKeyIdsFromKeyIdsInitData("{\"keys\":[\"AQI\"]}", &key_ids, |
| 578 &error_message)); |
| 579 EXPECT_EQ(3u, key_ids.size()); // |key_ids| should be unchanged. |
| 580 EXPECT_EQ(error_message, "Missing 'kids' parameter or not a list"); |
| 581 |
| 582 // Expect failure when invalid key_ids specified. |
| 583 EXPECT_FALSE(ExtractKeyIdsFromKeyIdsInitData("{\"kids\":[1]}", &key_ids, |
| 584 &error_message)); |
| 585 EXPECT_EQ(3u, key_ids.size()); // |key_ids| should be unchanged. |
| 586 EXPECT_EQ(error_message, "'kids'[0] is not string."); |
| 587 EXPECT_FALSE(ExtractKeyIdsFromKeyIdsInitData("{\"kids\": {\"id\":\"AQI\" }}", |
| 588 &key_ids, &error_message)); |
| 589 EXPECT_EQ(3u, key_ids.size()); // |key_ids| should be unchanged. |
| 590 EXPECT_EQ(error_message, "Missing 'kids' parameter or not a list"); |
| 591 |
| 592 // Expect failure when non-base64 key_ids specified. |
| 593 EXPECT_FALSE(ExtractKeyIdsFromKeyIdsInitData("{\"kids\":[\"AQI+\"]}", |
| 594 &key_ids, &error_message)); |
| 595 EXPECT_EQ(3u, key_ids.size()); // |key_ids| should be unchanged. |
| 596 EXPECT_EQ(error_message, |
| 597 "'kids'[0] is not valid base64url encoded. Value: AQI+"); |
| 598 EXPECT_FALSE(ExtractKeyIdsFromKeyIdsInitData("{\"kids\":[\"AQI\",\"AQI/\"]}", |
| 599 &key_ids, &error_message)); |
| 600 EXPECT_EQ(3u, key_ids.size()); // |key_ids| should be unchanged. |
| 601 EXPECT_EQ(error_message, |
| 602 "'kids'[1] is not valid base64url encoded. Value: AQI/"); |
| 603 } |
| 604 |
522 } // namespace media | 605 } // namespace media |
523 | 606 |
OLD | NEW |