1ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch// Copyright 2013 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind.h"
1003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "base/json/json_reader.h"
1103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "base/values.h"
12f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "media/base/cdm_promise.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "media/base/decoder_buffer.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "media/base/decrypt_config.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "media/base/mock_filters.h"
16ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "media/cdm/aes_decryptor.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gmock/include/gmock/gmock.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using ::testing::_;
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using ::testing::Gt;
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using ::testing::IsNull;
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using ::testing::NotNull;
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using ::testing::SaveArg;
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using ::testing::StrNe;
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
27eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochMATCHER(IsEmpty, "") { return arg.empty(); }
28f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)MATCHER(IsNotEmpty, "") { return !arg.empty(); }
2903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)MATCHER(IsJSONDictionary, "") {
3003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  std::string result(arg.begin(), arg.end());
3103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  scoped_ptr<base::Value> root(base::JSONReader().ReadToValue(result));
3203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  return (root.get() && root->GetType() == base::Value::TYPE_DICTIONARY);
3303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)}
34eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
35cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class GURL;
36cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace media {
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)const uint8 kOriginalData[] = "Original subsample data.";
4068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)const int kOriginalDataSize = 24;
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
428bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// In the examples below, 'k'(key) has to be 16 bytes, and will always require
438bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// 2 bytes of padding. 'kid'(keyid) is variable length, and may require 0, 1,
448bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// or 2 bytes of padding.
458bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
4668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)const uint8 kKeyId[] = {
478bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // base64 equivalent is AAECAw
4858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    0x00, 0x01, 0x02, 0x03
4958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)};
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
51f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Key is 0x0405060708090a0b0c0d0e0f10111213,
52f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// base64 equivalent is BAUGBwgJCgsMDQ4PEBESEw.
5368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)const char kKeyAsJWK[] =
5458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    "{"
5558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    "  \"keys\": ["
5658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    "    {"
5758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    "      \"kty\": \"oct\","
588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    "      \"kid\": \"AAECAw\","
598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    "      \"k\": \"BAUGBwgJCgsMDQ4PEBESEw\""
6058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    "    }"
6103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    "  ],"
6203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    "  \"type\": \"temporary\""
6358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    "}";
6458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
65a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// Same kid as kKeyAsJWK, key to decrypt kEncryptedData2
66a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)const char kKeyAlternateAsJWK[] =
67a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    "{"
68a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    "  \"keys\": ["
69a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    "    {"
70a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    "      \"kty\": \"oct\","
71a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    "      \"kid\": \"AAECAw\","
72a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    "      \"k\": \"FBUWFxgZGhscHR4fICEiIw\""
73a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    "    }"
74a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    "  ]"
75a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    "}";
76a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
7768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)const char kWrongKeyAsJWK[] =
7858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    "{"
7958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    "  \"keys\": ["
8058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    "    {"
8158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    "      \"kty\": \"oct\","
828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    "      \"kid\": \"AAECAw\","
838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    "      \"k\": \"7u7u7u7u7u7u7u7u7u7u7g\""
8458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    "    }"
8558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    "  ]"
8658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    "}";
8758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
8868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)const char kWrongSizedKeyAsJWK[] =
8958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    "{"
9058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    "  \"keys\": ["
9158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    "    {"
9258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    "      \"kty\": \"oct\","
938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    "      \"kid\": \"AAECAw\","
948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    "      \"k\": \"AAECAw\""
9558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    "    }"
9658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    "  ]"
9758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    "}";
9858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
9968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)const uint8 kIv[] = {
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// kOriginalData encrypted with kKey and kIv but without any subsamples (or
10558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// equivalently using kSubsampleEntriesCypherOnly).
10668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)const uint8 kEncryptedData[] = {
10758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  0x2f, 0x03, 0x09, 0xef, 0x71, 0xaf, 0x31, 0x16,
10858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  0xfa, 0x9d, 0x18, 0x43, 0x1e, 0x96, 0x71, 0xb5,
10958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  0xbf, 0xf5, 0x30, 0x53, 0x9a, 0x20, 0xdf, 0x95
11058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)};
11158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
11258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// kOriginalData encrypted with kSubsampleKey and kSubsampleIv using
113a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)// kSubsampleEntriesNormal.
11468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)const uint8 kSubsampleEncryptedData[] = {
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  0x4f, 0x72, 0x09, 0x16, 0x09, 0xe6, 0x79, 0xad,
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  0x70, 0x73, 0x75, 0x62, 0x09, 0xbb, 0x83, 0x1d,
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  0x4d, 0x08, 0xd7, 0x78, 0xa4, 0xa7, 0xf1, 0x2e
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)const uint8 kOriginalData2[] = "Changed Original data.";
12158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
12268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)const uint8 kIv2[] = {
12358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)const uint8 kKeyId2[] = {
12858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    // base64 equivalent is AAECAwQFBgcICQoLDA0ODxAREhM=
12958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
13058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
13158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    0x10, 0x11, 0x12, 0x13
13258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)};
13358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
13468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)const char kKey2AsJWK[] =
13558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    "{"
13658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    "  \"keys\": ["
13758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    "    {"
13858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    "      \"kty\": \"oct\","
1398bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    "      \"kid\": \"AAECAwQFBgcICQoLDA0ODxAREhM\","
1408bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    "      \"k\": \"FBUWFxgZGhscHR4fICEiIw\""
14158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    "    }"
14258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    "  ]"
14358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    "}";
14458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
14558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// 'k' in bytes is x14x15x16x17x18x19x1ax1bx1cx1dx1ex1fx20x21x22x23
14658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
14768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)const uint8 kEncryptedData2[] = {
14858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  0x57, 0x66, 0xf4, 0x12, 0x1a, 0xed, 0xb5, 0x79,
14958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  0x1c, 0x8e, 0x25, 0xd7, 0x17, 0xe7, 0x5e, 0x16,
15058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  0xe3, 0x40, 0x08, 0x27, 0x11, 0xe9
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
153a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)// Subsample entries for testing. The sum of |cypher_bytes| and |clear_bytes| of
15458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// all entries must be equal to kOriginalDataSize to make the subsample entries
15558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// valid.
156a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
15768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)const SubsampleEntry kSubsampleEntriesNormal[] = {
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  { 2, 7 },
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  { 3, 11 },
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  { 1, 0 }
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)const SubsampleEntry kSubsampleEntriesWrongSize[] = {
16458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  { 3, 6 }, // This entry doesn't match the correct entry.
16558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  { 3, 11 },
16658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  { 1, 0 }
16758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)};
16858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
16968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)const SubsampleEntry kSubsampleEntriesInvalidTotalSize[] = {
17058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  { 1, 1000 }, // This entry is too large.
17158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  { 3, 11 },
17258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  { 1, 0 }
17358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)};
17458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
17568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)const SubsampleEntry kSubsampleEntriesClearOnly[] = {
176a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  { 7, 0 },
177a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  { 8, 0 },
178a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  { 9, 0 }
179a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)};
180a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
18168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)const SubsampleEntry kSubsampleEntriesCypherOnly[] = {
182a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  { 0, 6 },
183a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  { 0, 8 },
184a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  { 0, 10 }
185a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)};
186a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
18758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)static scoped_refptr<DecoderBuffer> CreateEncryptedBuffer(
18858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    const std::vector<uint8>& data,
18958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    const std::vector<uint8>& key_id,
19058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    const std::vector<uint8>& iv,
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::vector<SubsampleEntry>& subsample_entries) {
19258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DCHECK(!data.empty());
1935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_refptr<DecoderBuffer> encrypted_buffer(new DecoderBuffer(data.size()));
1945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  memcpy(encrypted_buffer->writable_data(), &data[0], data.size());
195868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  CHECK(encrypted_buffer.get());
19658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  std::string key_id_string(
19758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      reinterpret_cast<const char*>(key_id.empty() ? NULL : &key_id[0]),
19858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      key_id.size());
19958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  std::string iv_string(
20058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      reinterpret_cast<const char*>(iv.empty() ? NULL : &iv[0]), iv.size());
20158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  encrypted_buffer->set_decrypt_config(scoped_ptr<DecryptConfig>(
2025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      new DecryptConfig(key_id_string, iv_string, subsample_entries)));
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return encrypted_buffer;
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
206f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)enum PromiseResult { RESOLVED, REJECTED };
207f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class AesDecryptorTest : public testing::Test {
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AesDecryptorTest()
211f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      : decryptor_(base::Bind(&AesDecryptorTest::OnSessionMessage,
212a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                              base::Unretained(this)),
213a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                   base::Bind(&AesDecryptorTest::OnSessionClosed,
2141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                              base::Unretained(this)),
2151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                   base::Bind(&AesDecryptorTest::OnSessionKeysChange,
216a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                              base::Unretained(this))),
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        decrypt_cb_(base::Bind(&AesDecryptorTest::BufferDecrypted,
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               base::Unretained(this))),
21958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        original_data_(kOriginalData, kOriginalData + kOriginalDataSize),
22058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        encrypted_data_(kEncryptedData,
22158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                        kEncryptedData + arraysize(kEncryptedData)),
22258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        subsample_encrypted_data_(
22358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)            kSubsampleEncryptedData,
22458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)            kSubsampleEncryptedData + arraysize(kSubsampleEncryptedData)),
22558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        key_id_(kKeyId, kKeyId + arraysize(kKeyId)),
22658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        iv_(kIv, kIv + arraysize(kIv)),
22758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        normal_subsample_entries_(
228a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)            kSubsampleEntriesNormal,
229f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)            kSubsampleEntriesNormal + arraysize(kSubsampleEntriesNormal)) {
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
2336e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  void OnResolveWithSession(PromiseResult expected_result,
234f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                            const std::string& web_session_id) {
2356e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    EXPECT_EQ(expected_result, RESOLVED) << "Unexpectedly resolved.";
236f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    EXPECT_GT(web_session_id.length(), 0ul);
237f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    web_session_id_ = web_session_id;
238f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  }
239f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
2406e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  void OnResolve(PromiseResult expected_result) {
2416e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    EXPECT_EQ(expected_result, RESOLVED) << "Unexpectedly resolved.";
242f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  }
243f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
2446e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  void OnResolveWithUsableKeyIds(PromiseResult expected_result,
2456e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                 uint32 expected_count,
2466e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                 const KeyIdsVector& useable_key_ids) {
2476e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    EXPECT_EQ(expected_result, RESOLVED) << "Unexpectedly resolved.";
2486e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    EXPECT_EQ(expected_count, useable_key_ids.size());
2496e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    useable_key_ids_ = useable_key_ids;
2506e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  }
2516e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
2526e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  void OnReject(PromiseResult expected_result,
253f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                MediaKeys::Exception exception_code,
254f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                uint32 system_code,
255f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                const std::string& error_message) {
2566e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    EXPECT_EQ(expected_result, REJECTED) << "Unexpectedly rejected.";
257f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  }
258f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
2596e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  scoped_ptr<SimpleCdmPromise> CreatePromise(PromiseResult expected_result) {
2606e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    scoped_ptr<SimpleCdmPromise> promise(
2616e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        new SimpleCdmPromise(base::Bind(&AesDecryptorTest::OnResolve,
2626e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                        base::Unretained(this),
2636e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                        expected_result),
2646e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                             base::Bind(&AesDecryptorTest::OnReject,
2656e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                        base::Unretained(this),
2666e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                        expected_result)));
267f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    return promise.Pass();
268f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  }
269f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
270f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  scoped_ptr<NewSessionCdmPromise> CreateSessionPromise(
2716e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      PromiseResult expected_result) {
272f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    scoped_ptr<NewSessionCdmPromise> promise(new NewSessionCdmPromise(
273f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        base::Bind(&AesDecryptorTest::OnResolveWithSession,
274f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                   base::Unretained(this),
2756e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                   expected_result),
2766e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        base::Bind(&AesDecryptorTest::OnReject,
2776e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                   base::Unretained(this),
2786e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                   expected_result)));
2796e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    return promise.Pass();
2806e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  }
2816e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
2826e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  scoped_ptr<KeyIdsPromise> CreateUsableKeyIdsPromise(
2836e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      PromiseResult expected_result,
2846e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      uint32 expected_count) {
2856e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    scoped_ptr<KeyIdsPromise> promise(new KeyIdsPromise(
2866e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        base::Bind(&AesDecryptorTest::OnResolveWithUsableKeyIds,
2876e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                   base::Unretained(this),
2886e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                   expected_result,
2896e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                   expected_count),
2906e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        base::Bind(&AesDecryptorTest::OnReject,
2916e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                   base::Unretained(this),
2926e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                   expected_result)));
293f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    return promise.Pass();
294f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  }
295f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
296a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Creates a new session using |key_id|. Returns the session ID.
297f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::string CreateSession(const std::vector<uint8>& key_id) {
29858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    DCHECK(!key_id.empty());
299f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    EXPECT_CALL(*this,
30003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                OnSessionMessage(
30103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                    IsNotEmpty(), IsJSONDictionary(), GURL::EmptyGURL()));
302f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    decryptor_.CreateSession(std::string(),
303f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                             &key_id[0],
304f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                             key_id.size(),
305f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                             MediaKeys::TEMPORARY_SESSION,
306f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                             CreateSessionPromise(RESOLVED));
307f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    // This expects the promise to be called synchronously, which is the case
308f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    // for AesDecryptor.
309f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    return web_session_id_;
310a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
311a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
3121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Closes the session specified by |session_id|.
3131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void CloseSession(const std::string& session_id) {
314a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    EXPECT_CALL(*this, OnSessionClosed(session_id));
3151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    decryptor_.CloseSession(session_id, CreatePromise(RESOLVED));
3161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
3171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
3181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Removes the session specified by |session_id|. This should simply do a
3191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // CloseSession().
3201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // TODO(jrummell): Clean this up when the prefixed API is removed.
3211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // http://crbug.com/249976.
3221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void RemoveSession(const std::string& session_id) {
3231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    EXPECT_CALL(*this, OnSessionClosed(session_id));
3241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    decryptor_.RemoveSession(session_id, CreatePromise(RESOLVED));
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
327a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Updates the session specified by |session_id| with |key|. |result|
328a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // tests that the update succeeds or generates an error.
329f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  void UpdateSessionAndExpect(std::string session_id,
330a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                              const std::string& key,
3311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                              PromiseResult expected_result) {
33258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    DCHECK(!key.empty());
33358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
3341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    if (expected_result == RESOLVED) {
3351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      EXPECT_CALL(*this, OnSessionKeysChange(session_id, true));
3361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    } else {
3371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      EXPECT_CALL(*this, OnSessionKeysChange(_, _)).Times(0);
3381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    }
3391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
340f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    decryptor_.UpdateSession(session_id,
341f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                             reinterpret_cast<const uint8*>(key.c_str()),
342f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                             key.length(),
3431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                             CreatePromise(expected_result));
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3466e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  void GetUsableKeyIdsAndExpect(const std::string& session_id,
3476e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                PromiseResult expected_result,
3486e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                uint32 expected_count) {
3496e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    decryptor_.GetUsableKeyIds(
3506e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        session_id, CreateUsableKeyIdsPromise(expected_result, expected_count));
3516e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  }
3526e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
3536e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  bool UsableKeyIdsContains(std::vector<uint8> expected) {
3546e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    for (KeyIdsVector::iterator it = useable_key_ids_.begin();
3556e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)         it != useable_key_ids_.end();
3566e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)         ++it) {
3576e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      if (*it == expected)
3586e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        return true;
3596e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    }
3606e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    return false;
3616e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  }
3626e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MOCK_METHOD2(BufferDecrypted, void(Decryptor::Status,
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     const scoped_refptr<DecoderBuffer>&));
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
36658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  enum DecryptExpectation {
36758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    SUCCESS,
36858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    DATA_MISMATCH,
36958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    DATA_AND_SIZE_MISMATCH,
370a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    DECRYPT_ERROR,
371a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    NO_KEY
37258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  };
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
37458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  void DecryptAndExpect(const scoped_refptr<DecoderBuffer>& encrypted,
37558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                        const std::vector<uint8>& plain_text,
37658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                        DecryptExpectation result) {
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scoped_refptr<DecoderBuffer> decrypted;
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    switch (result) {
3805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      case SUCCESS:
3815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      case DATA_MISMATCH:
3825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      case DATA_AND_SIZE_MISMATCH:
3835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        EXPECT_CALL(*this, BufferDecrypted(Decryptor::kSuccess, NotNull()))
3845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            .WillOnce(SaveArg<1>(&decrypted));
3855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        break;
3865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      case DECRYPT_ERROR:
3875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        EXPECT_CALL(*this, BufferDecrypted(Decryptor::kError, IsNull()))
3885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            .WillOnce(SaveArg<1>(&decrypted));
3895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        break;
3905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      case NO_KEY:
3915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        EXPECT_CALL(*this, BufferDecrypted(Decryptor::kNoKey, IsNull()))
3925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            .WillOnce(SaveArg<1>(&decrypted));
3935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        break;
39458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    }
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    decryptor_.Decrypt(Decryptor::kVideo, encrypted, decrypt_cb_);
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
39858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    std::vector<uint8> decrypted_text;
3991320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    if (decrypted.get() && decrypted->data_size()) {
40058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      decrypted_text.assign(
40158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        decrypted->data(), decrypted->data() + decrypted->data_size());
40258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    }
40358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
40458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    switch (result) {
40558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      case SUCCESS:
40658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        EXPECT_EQ(plain_text, decrypted_text);
40758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        break;
40858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      case DATA_MISMATCH:
40958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        EXPECT_EQ(plain_text.size(), decrypted_text.size());
41058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        EXPECT_NE(plain_text, decrypted_text);
41158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        break;
41258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      case DATA_AND_SIZE_MISMATCH:
41358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        EXPECT_NE(plain_text.size(), decrypted_text.size());
41458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        break;
41558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      case DECRYPT_ERROR:
416a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      case NO_KEY:
41758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        EXPECT_TRUE(decrypted_text.empty());
41858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        break;
41958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    }
4205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
422a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  MOCK_METHOD3(OnSessionMessage,
423f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)               void(const std::string& web_session_id,
424f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                    const std::vector<uint8>& message,
42546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                    const GURL& destination_url));
4261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  MOCK_METHOD2(OnSessionKeysChange,
4271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci               void(const std::string& web_session_id,
4281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                    bool has_additional_usable_key));
429f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  MOCK_METHOD1(OnSessionClosed, void(const std::string& web_session_id));
4302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AesDecryptor decryptor_;
4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AesDecryptor::DecryptCB decrypt_cb_;
433f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::string web_session_id_;
43458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
4356e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // Copy of the vector from the last successful call to
4366e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // OnResolveWithUsableKeyIds().
4376e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  KeyIdsVector useable_key_ids_;
4386e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
43958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Constants for testing.
44058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  const std::vector<uint8> original_data_;
44158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  const std::vector<uint8> encrypted_data_;
44258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  const std::vector<uint8> subsample_encrypted_data_;
44358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  const std::vector<uint8> key_id_;
44458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  const std::vector<uint8> iv_;
44558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  const std::vector<SubsampleEntry> normal_subsample_entries_;
44658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  const std::vector<SubsampleEntry> no_subsample_entries_;
4475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
449a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)TEST_F(AesDecryptorTest, CreateSessionWithNullInitData) {
450cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_CALL(*this,
451f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)              OnSessionMessage(IsNotEmpty(), IsEmpty(), GURL::EmptyGURL()));
452f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  decryptor_.CreateSession(std::string(),
453f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                           NULL,
454f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                           0,
455f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                           MediaKeys::TEMPORARY_SESSION,
456f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                           CreateSessionPromise(RESOLVED));
457f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
458f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
459a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)TEST_F(AesDecryptorTest, MultipleCreateSession) {
460cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_CALL(*this,
461f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)              OnSessionMessage(IsNotEmpty(), IsEmpty(), GURL::EmptyGURL()));
462f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  decryptor_.CreateSession(std::string(),
463f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                           NULL,
464f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                           0,
465f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                           MediaKeys::TEMPORARY_SESSION,
466f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                           CreateSessionPromise(RESOLVED));
467a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
468cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_CALL(*this,
469f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)              OnSessionMessage(IsNotEmpty(), IsEmpty(), GURL::EmptyGURL()));
470f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  decryptor_.CreateSession(std::string(),
471f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                           NULL,
472f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                           0,
473f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                           MediaKeys::TEMPORARY_SESSION,
474f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                           CreateSessionPromise(RESOLVED));
475a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
476cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_CALL(*this,
477f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)              OnSessionMessage(IsNotEmpty(), IsEmpty(), GURL::EmptyGURL()));
478f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  decryptor_.CreateSession(std::string(),
479f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                           NULL,
480f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                           0,
481f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                           MediaKeys::TEMPORARY_SESSION,
482f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                           CreateSessionPromise(RESOLVED));
4832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
48558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)TEST_F(AesDecryptorTest, NormalDecryption) {
486f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::string session_id = CreateSession(key_id_);
487f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED);
48858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
4895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      encrypted_data_, key_id_, iv_, no_subsample_entries_);
49058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS);
49158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
49258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
49358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)TEST_F(AesDecryptorTest, UnencryptedFrame) {
49458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // An empty iv string signals that the frame is unencrypted.
49558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
4965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      original_data_, key_id_, std::vector<uint8>(), no_subsample_entries_);
49758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS);
4985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(AesDecryptorTest, WrongKey) {
501f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::string session_id = CreateSession(key_id_);
502f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(session_id, kWrongKeyAsJWK, RESOLVED);
50358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
5045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      encrypted_data_, key_id_, iv_, no_subsample_entries_);
50558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH);
5065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(AesDecryptorTest, NoKey) {
50958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
5105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      encrypted_data_, key_id_, iv_, no_subsample_entries_);
5115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_CALL(*this, BufferDecrypted(AesDecryptor::kNoKey, IsNull()));
51258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  decryptor_.Decrypt(Decryptor::kVideo, encrypted_buffer, decrypt_cb_);
5135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(AesDecryptorTest, KeyReplacement) {
516f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::string session_id = CreateSession(key_id_);
51758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
5185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      encrypted_data_, key_id_, iv_, no_subsample_entries_);
51958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
520f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(session_id, kWrongKeyAsJWK, RESOLVED);
52158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ASSERT_NO_FATAL_FAILURE(DecryptAndExpect(
52258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      encrypted_buffer, original_data_, DATA_MISMATCH));
52358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
524f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED);
52558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ASSERT_NO_FATAL_FAILURE(
52658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS));
5275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(AesDecryptorTest, WrongSizedKey) {
530f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::string session_id = CreateSession(key_id_);
531f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(session_id, kWrongSizedKeyAsJWK, REJECTED);
5325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(AesDecryptorTest, MultipleKeysAndFrames) {
535f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::string session_id = CreateSession(key_id_);
536f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED);
53758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
5385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      encrypted_data_, key_id_, iv_, no_subsample_entries_);
53958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ASSERT_NO_FATAL_FAILURE(
54058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS));
54158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
542f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(session_id, kKey2AsJWK, RESOLVED);
54358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
54458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // The first key is still available after we added a second key.
54558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ASSERT_NO_FATAL_FAILURE(
54658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS));
54758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
54858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // The second key is also available.
54958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  encrypted_buffer = CreateEncryptedBuffer(
55058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      std::vector<uint8>(kEncryptedData2,
55158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                         kEncryptedData2 + arraysize(kEncryptedData2)),
55258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      std::vector<uint8>(kKeyId2, kKeyId2 + arraysize(kKeyId2)),
55358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      std::vector<uint8>(kIv2, kIv2 + arraysize(kIv2)),
55458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      no_subsample_entries_);
55558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ASSERT_NO_FATAL_FAILURE(DecryptAndExpect(
55658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      encrypted_buffer,
55758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      std::vector<uint8>(kOriginalData2,
55858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                         kOriginalData2 + arraysize(kOriginalData2) - 1),
55958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      SUCCESS));
5605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(AesDecryptorTest, CorruptedIv) {
563f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::string session_id = CreateSession(key_id_);
564f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED);
56558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
56658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  std::vector<uint8> bad_iv = iv_;
56758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  bad_iv[1]++;
56858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
56958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
5705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      encrypted_data_, key_id_, bad_iv, no_subsample_entries_);
57158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
57258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH);
5735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(AesDecryptorTest, CorruptedData) {
576f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::string session_id = CreateSession(key_id_);
577f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED);
5785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
57958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  std::vector<uint8> bad_data = encrypted_data_;
58058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  bad_data[1]++;
58158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
58258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
5835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      bad_data, key_id_, iv_, no_subsample_entries_);
58458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH);
5855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
58758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)TEST_F(AesDecryptorTest, EncryptedAsUnencryptedFailure) {
588f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::string session_id = CreateSession(key_id_);
589f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED);
59058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
5915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      encrypted_data_, key_id_, std::vector<uint8>(), no_subsample_entries_);
59258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH);
5935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(AesDecryptorTest, SubsampleDecryption) {
596f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::string session_id = CreateSession(key_id_);
597f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED);
59858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
5995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      subsample_encrypted_data_, key_id_, iv_, normal_subsample_entries_);
60058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS);
6015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Ensures noninterference of data offset and subsample mechanisms. We never
6045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// expect to encounter this in the wild, but since the DecryptConfig doesn't
6055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// disallow such a configuration, it should be covered.
6065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(AesDecryptorTest, SubsampleDecryptionWithOffset) {
607f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::string session_id = CreateSession(key_id_);
608f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED);
60958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
6105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      subsample_encrypted_data_, key_id_, iv_, normal_subsample_entries_);
61158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS);
6125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
61458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)TEST_F(AesDecryptorTest, SubsampleWrongSize) {
615f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::string session_id = CreateSession(key_id_);
616f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED);
61758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
61858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  std::vector<SubsampleEntry> subsample_entries_wrong_size(
61958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      kSubsampleEntriesWrongSize,
62058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      kSubsampleEntriesWrongSize + arraysize(kSubsampleEntriesWrongSize));
62158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
62258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
6235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      subsample_encrypted_data_, key_id_, iv_, subsample_entries_wrong_size);
62458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH);
6255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
62758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)TEST_F(AesDecryptorTest, SubsampleInvalidTotalSize) {
628f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::string session_id = CreateSession(key_id_);
629f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED);
63058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
63158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  std::vector<SubsampleEntry> subsample_entries_invalid_total_size(
63258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      kSubsampleEntriesInvalidTotalSize,
63358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      kSubsampleEntriesInvalidTotalSize +
63458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          arraysize(kSubsampleEntriesInvalidTotalSize));
63558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
63658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
6375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      subsample_encrypted_data_, key_id_, iv_,
63858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      subsample_entries_invalid_total_size);
63958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DecryptAndExpect(encrypted_buffer, original_data_, DECRYPT_ERROR);
6405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
642a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)// No cypher bytes in any of the subsamples.
643a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)TEST_F(AesDecryptorTest, SubsampleClearBytesOnly) {
644f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::string session_id = CreateSession(key_id_);
645f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED);
64658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
64758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  std::vector<SubsampleEntry> clear_only_subsample_entries(
648a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      kSubsampleEntriesClearOnly,
649a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      kSubsampleEntriesClearOnly + arraysize(kSubsampleEntriesClearOnly));
65058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
65158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
6525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      original_data_, key_id_, iv_, clear_only_subsample_entries);
65358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS);
654a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
655a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
656a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)// No clear bytes in any of the subsamples.
657a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)TEST_F(AesDecryptorTest, SubsampleCypherBytesOnly) {
658f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::string session_id = CreateSession(key_id_);
659f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED);
66058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
66158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  std::vector<SubsampleEntry> cypher_only_subsample_entries(
662a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      kSubsampleEntriesCypherOnly,
663a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      kSubsampleEntriesCypherOnly + arraysize(kSubsampleEntriesCypherOnly));
66458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
66558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
6665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      encrypted_data_, key_id_, iv_, cypher_only_subsample_entries);
66758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS);
66858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
66958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
6701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciTEST_F(AesDecryptorTest, CloseSession) {
6711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  std::string session_id = CreateSession(key_id_);
6721320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
6731320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      encrypted_data_, key_id_, iv_, no_subsample_entries_);
6741320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
6751320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED);
6761320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  ASSERT_NO_FATAL_FAILURE(
6771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS));
6781320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
6791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  CloseSession(session_id);
6801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
6811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
6821320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciTEST_F(AesDecryptorTest, RemoveSession) {
6831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // TODO(jrummell): Clean this up when the prefixed API is removed.
6841320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // http://crbug.com/249976.
685f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::string session_id = CreateSession(key_id_);
686a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
6875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      encrypted_data_, key_id_, iv_, no_subsample_entries_);
688a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
689f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED);
690a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  ASSERT_NO_FATAL_FAILURE(
691a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS));
692a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
6931320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  RemoveSession(session_id);
694a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
695a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
6961320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciTEST_F(AesDecryptorTest, NoKeyAfterCloseSession) {
697f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::string session_id = CreateSession(key_id_);
698a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
6995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      encrypted_data_, key_id_, iv_, no_subsample_entries_);
700a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
701f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED);
702a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  ASSERT_NO_FATAL_FAILURE(
703a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS));
704a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
7051320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  CloseSession(session_id);
706a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  ASSERT_NO_FATAL_FAILURE(
707a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      DecryptAndExpect(encrypted_buffer, original_data_, NO_KEY));
708a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
709a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
710a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)TEST_F(AesDecryptorTest, LatestKeyUsed) {
711f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::string session_id1 = CreateSession(key_id_);
712a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
7135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      encrypted_data_, key_id_, iv_, no_subsample_entries_);
714a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
715a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Add alternate key, buffer should not be decoded properly.
716f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(session_id1, kKeyAlternateAsJWK, RESOLVED);
717a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  ASSERT_NO_FATAL_FAILURE(
718a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH));
719a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
720a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Create a second session with a correct key value for key_id_.
721f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::string session_id2 = CreateSession(key_id_);
722f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(session_id2, kKeyAsJWK, RESOLVED);
723a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
724a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Should be able to decode with latest key.
725a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  ASSERT_NO_FATAL_FAILURE(
726a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS));
727a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
728a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
7291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciTEST_F(AesDecryptorTest, LatestKeyUsedAfterCloseSession) {
730f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::string session_id1 = CreateSession(key_id_);
731a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
7325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      encrypted_data_, key_id_, iv_, no_subsample_entries_);
733f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(session_id1, kKeyAsJWK, RESOLVED);
734a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  ASSERT_NO_FATAL_FAILURE(
735a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS));
736a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
737a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Create a second session with a different key value for key_id_.
738f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::string session_id2 = CreateSession(key_id_);
739f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(session_id2, kKeyAlternateAsJWK, RESOLVED);
740a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
741a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Should not be able to decode with new key.
742a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  ASSERT_NO_FATAL_FAILURE(
743a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH));
744a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
745a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Close second session, should revert to original key.
7461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  CloseSession(session_id2);
747a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  ASSERT_NO_FATAL_FAILURE(
748a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS));
749a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
750a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
75158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)TEST_F(AesDecryptorTest, JWKKey) {
752f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::string session_id = CreateSession(key_id_);
753a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
75458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Try a simple JWK key (i.e. not in a set)
755f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  const std::string kJwkSimple =
75658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "{"
75758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "  \"kty\": \"oct\","
7588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "  \"kid\": \"AAECAwQFBgcICQoLDA0ODxAREhM\","
7598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "  \"k\": \"FBUWFxgZGhscHR4fICEiIw\""
76058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "}";
761f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(session_id, kJwkSimple, REJECTED);
76258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
76358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Try a key list with multiple entries.
764f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  const std::string kJwksMultipleEntries =
76558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "{"
76658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "  \"keys\": ["
76758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "    {"
76858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "      \"kty\": \"oct\","
7698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "      \"kid\": \"AAECAwQFBgcICQoLDA0ODxAREhM\","
7708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "      \"k\": \"FBUWFxgZGhscHR4fICEiIw\""
77158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "    },"
77258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "    {"
77358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "      \"kty\": \"oct\","
7748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "      \"kid\": \"JCUmJygpKissLS4vMA\","
7758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "      \"k\":\"MTIzNDU2Nzg5Ojs8PT4/QA\""
77658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "    }"
77758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "  ]"
77858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "}";
779f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(session_id, kJwksMultipleEntries, RESOLVED);
78058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
78158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Try a key with no spaces and some \n plus additional fields.
782f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  const std::string kJwksNoSpaces =
78358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "\n\n{\"something\":1,\"keys\":[{\n\n\"kty\":\"oct\",\"alg\":\"A128KW\","
7848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "\"kid\":\"AAECAwQFBgcICQoLDA0ODxAREhM\",\"k\":\"GawgguFyGrWKav7AX4VKUg"
7858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "\",\"foo\":\"bar\"}]}\n\n";
786f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(session_id, kJwksNoSpaces, RESOLVED);
78758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
78858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Try some non-ASCII characters.
789f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(
790f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      session_id, "This is not ASCII due to \xff\xfe\xfd in it.", REJECTED);
79158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
79258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Try a badly formatted key. Assume that the JSON parser is fully tested,
79358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // so we won't try a lot of combinations. However, need a test to ensure
79458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // that the code doesn't crash if invalid JSON received.
795f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(session_id, "This is not a JSON key.", REJECTED);
79658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
79758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Try passing some valid JSON that is not a dictionary at the top level.
798f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(session_id, "40", REJECTED);
79958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
80058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Try an empty dictionary.
801f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(session_id, "{ }", REJECTED);
80258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
80358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Try an empty 'keys' dictionary.
804f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(session_id, "{ \"keys\": [] }", REJECTED);
80558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
80658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Try with 'keys' not a dictionary.
807f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(session_id, "{ \"keys\":\"1\" }", REJECTED);
80858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
80958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Try with 'keys' a list of integers.
810f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(session_id, "{ \"keys\": [ 1, 2, 3 ] }", REJECTED);
81158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
812f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Try padding(=) at end of 'k' base64 string.
813f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  const std::string kJwksWithPaddedKey =
81458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "{"
81558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "  \"keys\": ["
81658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "    {"
81758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "      \"kty\": \"oct\","
8188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "      \"kid\": \"AAECAw\","
8198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "      \"k\": \"BAUGBwgJCgsMDQ4PEBESEw==\""
82058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "    }"
82158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "  ]"
82258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "}";
823f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(session_id, kJwksWithPaddedKey, REJECTED);
82458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
825f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Try padding(=) at end of 'kid' base64 string.
826f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  const std::string kJwksWithPaddedKeyId =
82758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "{"
82858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "  \"keys\": ["
82958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "    {"
83058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "      \"kty\": \"oct\","
8318bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "      \"kid\": \"AAECAw==\","
8328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "      \"k\": \"BAUGBwgJCgsMDQ4PEBESEw\""
83358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "    }"
83458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "  ]"
83558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "}";
836f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(session_id, kJwksWithPaddedKeyId, REJECTED);
83758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
83858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Try a key with invalid base64 encoding.
839f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  const std::string kJwksWithInvalidBase64 =
84058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "{"
84158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "  \"keys\": ["
84258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "    {"
84358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "      \"kty\": \"oct\","
8448bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "      \"kid\": \"!@#$%^&*()\","
8458bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "      \"k\": \"BAUGBwgJCgsMDQ4PEBESEw\""
84658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "    }"
84758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "  ]"
84858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "}";
849f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(session_id, kJwksWithInvalidBase64, REJECTED);
8508bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
851f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Try a 3-byte 'kid' where no base64 padding is required.
852f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // |kJwksMultipleEntries| above has 2 'kid's that require 1 and 2 padding
853f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // bytes. Note that 'k' has to be 16 bytes, so it will always require padding.
854f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  const std::string kJwksWithNoPadding =
8558bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "{"
8568bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "  \"keys\": ["
8578bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "    {"
8588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "      \"kty\": \"oct\","
8598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "      \"kid\": \"Kiss\","
8608bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "      \"k\": \"BAUGBwgJCgsMDQ4PEBESEw\""
8618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "    }"
8628bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "  ]"
8638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "}";
864f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(session_id, kJwksWithNoPadding, RESOLVED);
8658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
8668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Empty key id.
867f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  const std::string kJwksWithEmptyKeyId =
8688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "{"
8698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "  \"keys\": ["
8708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "    {"
8718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "      \"kty\": \"oct\","
8728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "      \"kid\": \"\","
8738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "      \"k\": \"BAUGBwgJCgsMDQ4PEBESEw\""
8748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "    }"
8758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "  ]"
8768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "}";
877f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  UpdateSessionAndExpect(session_id, kJwksWithEmptyKeyId, REJECTED);
8781320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  CloseSession(session_id);
879a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
880a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
8816e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)TEST_F(AesDecryptorTest, GetKeyIds) {
8826e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  std::vector<uint8> key_id1(kKeyId, kKeyId + arraysize(kKeyId));
8836e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  std::vector<uint8> key_id2(kKeyId2, kKeyId2 + arraysize(kKeyId2));
8846e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
8856e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  std::string session_id = CreateSession(key_id_);
8866e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  GetUsableKeyIdsAndExpect(session_id, RESOLVED, 0);
8876e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_FALSE(UsableKeyIdsContains(key_id1));
8886e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_FALSE(UsableKeyIdsContains(key_id2));
8896e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
8906e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // Add 1 key, verify ID is returned.
8916e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED);
8926e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  GetUsableKeyIdsAndExpect(session_id, RESOLVED, 1);
8936e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_TRUE(UsableKeyIdsContains(key_id1));
8946e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_FALSE(UsableKeyIdsContains(key_id2));
8956e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
8966e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // Add second key, verify both IDs returned.
8976e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  UpdateSessionAndExpect(session_id, kKey2AsJWK, RESOLVED);
8986e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  GetUsableKeyIdsAndExpect(session_id, RESOLVED, 2);
8996e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_TRUE(UsableKeyIdsContains(key_id1));
9006e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_TRUE(UsableKeyIdsContains(key_id2));
9016e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
9026e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
9035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace media
904