1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203

media / base / decrypt_config_unittest.cc [blame]

// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/base/decrypt_config.h"

#include <sstream>

#include "media/base/encryption_pattern.h"
#include "media/base/subsample_entry.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace media {

namespace {

const char kDefaultKeyId[] = "key_id";
const char kDefaultIV[] = "1234567890123456";
const char kAlternateKeyId[] = "alternate";
const char kAlternateIV[] = "abcdefghijklmnop";

}  // namespace

TEST(DecryptConfigTest, CencConstruction) {
  auto config = DecryptConfig::CreateCencConfig(kDefaultKeyId, kDefaultIV, {});
  EXPECT_EQ(config->key_id(), kDefaultKeyId);
  EXPECT_EQ(config->iv(), kDefaultIV);
  EXPECT_EQ(config->subsamples().size(), 0u);
  EXPECT_EQ(config->encryption_scheme(), EncryptionScheme::kCenc);

  // Now with single subsample entry.
  config =
      DecryptConfig::CreateCencConfig(kAlternateKeyId, kDefaultIV, {{1, 2}});
  EXPECT_EQ(config->key_id(), kAlternateKeyId);
  EXPECT_EQ(config->iv(), kDefaultIV);
  EXPECT_EQ(config->subsamples().size(), 1u);
  EXPECT_EQ(config->subsamples()[0].clear_bytes, 1u);
  EXPECT_EQ(config->subsamples()[0].cypher_bytes, 2u);
  EXPECT_EQ(config->encryption_scheme(), EncryptionScheme::kCenc);

  // Now with multiple subsample entries.
  config = DecryptConfig::CreateCencConfig(kDefaultKeyId, kAlternateIV,
                                           {{1, 2}, {3, 4}, {5, 6}, {7, 8}});
  EXPECT_EQ(config->key_id(), kDefaultKeyId);
  EXPECT_EQ(config->iv(), kAlternateIV);
  EXPECT_EQ(config->subsamples().size(), 4u);
  EXPECT_EQ(config->subsamples()[1].clear_bytes, 3u);
  EXPECT_EQ(config->subsamples()[3].cypher_bytes, 8u);
  EXPECT_EQ(config->encryption_scheme(), EncryptionScheme::kCenc);
}

TEST(DecryptConfigTest, CbcsConstruction) {
  auto config = DecryptConfig::CreateCbcsConfig(kDefaultKeyId, kDefaultIV, {},
                                                EncryptionPattern(1, 2));
  EXPECT_EQ(config->key_id(), kDefaultKeyId);
  EXPECT_EQ(config->iv(), kDefaultIV);
  EXPECT_EQ(config->subsamples().size(), 0u);
  EXPECT_EQ(config->encryption_scheme(), EncryptionScheme::kCbcs);
  EXPECT_TRUE(config->HasPattern());
  EXPECT_EQ(config->encryption_pattern()->crypt_byte_block(), 1u);
  EXPECT_EQ(config->encryption_pattern()->skip_byte_block(), 2u);

  // Now with multiple subsample entries.
  config = DecryptConfig::CreateCbcsConfig(kDefaultKeyId, kAlternateIV,
                                           {{1, 2}, {3, 4}, {5, 6}, {7, 8}},
                                           EncryptionPattern(1, 0));
  EXPECT_EQ(config->key_id(), kDefaultKeyId);
  EXPECT_EQ(config->iv(), kAlternateIV);
  EXPECT_EQ(config->subsamples().size(), 4u);
  EXPECT_EQ(config->subsamples()[0].clear_bytes, 1u);
  EXPECT_EQ(config->subsamples()[0].cypher_bytes, 2u);
  EXPECT_EQ(config->subsamples()[3].clear_bytes, 7u);
  EXPECT_EQ(config->subsamples()[3].cypher_bytes, 8u);
  EXPECT_EQ(config->encryption_scheme(), EncryptionScheme::kCbcs);
  EXPECT_TRUE(config->HasPattern());
  EXPECT_EQ(config->encryption_pattern()->crypt_byte_block(), 1u);
  EXPECT_EQ(config->encryption_pattern()->skip_byte_block(), 0u);

  // Now without pattern.
  config = DecryptConfig::CreateCbcsConfig(kAlternateKeyId, kDefaultIV,
                                           {{1, 2}}, std::nullopt);
  EXPECT_EQ(config->key_id(), kAlternateKeyId);
  EXPECT_EQ(config->iv(), kDefaultIV);
  EXPECT_EQ(config->subsamples().size(), 1u);
  EXPECT_EQ(config->subsamples()[0].clear_bytes, 1u);
  EXPECT_EQ(config->subsamples()[0].cypher_bytes, 2u);
  EXPECT_EQ(config->encryption_scheme(), EncryptionScheme::kCbcs);
  EXPECT_FALSE(config->HasPattern());
}

TEST(DecryptConfigTest, Matches) {
  auto config1 = DecryptConfig::CreateCencConfig(kDefaultKeyId, kDefaultIV, {});
  EXPECT_TRUE(config1->Matches(*config1));

  auto config2 = DecryptConfig::CreateCbcsConfig(kDefaultKeyId, kDefaultIV, {},
                                                 EncryptionPattern(1, 2));
  EXPECT_TRUE(config2->Matches(*config2));

  EXPECT_FALSE(config1->Matches(*config2));
  EXPECT_FALSE(config2->Matches(*config1));
}

TEST(DecryptConfigTest, CencMatches) {
  auto config1 = DecryptConfig::CreateCencConfig(kDefaultKeyId, kDefaultIV, {});
  EXPECT_TRUE(config1->Matches(*config1));

  // Different key_id.
  auto config2 =
      DecryptConfig::CreateCencConfig(kAlternateKeyId, kDefaultIV, {});
  EXPECT_FALSE(config1->Matches(*config2));
  EXPECT_FALSE(config2->Matches(*config1));

  // Different IV.
  auto config3 =
      DecryptConfig::CreateCencConfig(kDefaultKeyId, kAlternateIV, {});
  EXPECT_FALSE(config1->Matches(*config3));
  EXPECT_FALSE(config2->Matches(*config3));
  EXPECT_FALSE(config3->Matches(*config1));
  EXPECT_FALSE(config3->Matches(*config2));

  // Different subsamples.
  auto config4 = DecryptConfig::CreateCencConfig(
      kDefaultKeyId, kDefaultIV, {{1, 2}, {3, 4}, {5, 6}, {7, 8}});
  EXPECT_FALSE(config1->Matches(*config4));
  EXPECT_FALSE(config2->Matches(*config4));
  EXPECT_FALSE(config3->Matches(*config4));
  EXPECT_FALSE(config4->Matches(*config1));
  EXPECT_FALSE(config4->Matches(*config2));
  EXPECT_FALSE(config4->Matches(*config3));
}

TEST(DecryptConfigTest, CbcsMatches) {
  auto config1 = DecryptConfig::CreateCbcsConfig(kDefaultKeyId, kDefaultIV, {},
                                                 EncryptionPattern(1, 2));
  EXPECT_TRUE(config1->Matches(*config1));

  // Different key_id.
  auto config2 = DecryptConfig::CreateCbcsConfig(kAlternateKeyId, kDefaultIV,
                                                 {}, EncryptionPattern(1, 2));
  EXPECT_FALSE(config1->Matches(*config2));
  EXPECT_FALSE(config2->Matches(*config1));

  // Different IV.
  auto config3 = DecryptConfig::CreateCbcsConfig(kDefaultKeyId, kAlternateIV,
                                                 {}, EncryptionPattern(1, 2));
  EXPECT_FALSE(config1->Matches(*config3));
  EXPECT_FALSE(config2->Matches(*config3));
  EXPECT_FALSE(config3->Matches(*config1));
  EXPECT_FALSE(config3->Matches(*config2));

  // Different subsamples.
  auto config4 = DecryptConfig::CreateCbcsConfig(kDefaultKeyId, kDefaultIV,
                                                 {{1, 2}, {3, 4}, {5, 6}},
                                                 EncryptionPattern(1, 2));
  EXPECT_FALSE(config1->Matches(*config4));
  EXPECT_FALSE(config2->Matches(*config4));
  EXPECT_FALSE(config3->Matches(*config4));
  EXPECT_FALSE(config4->Matches(*config1));
  EXPECT_FALSE(config4->Matches(*config2));
  EXPECT_FALSE(config4->Matches(*config3));

  // Different pattern.
  auto config5 = DecryptConfig::CreateCbcsConfig(kDefaultKeyId, kDefaultIV, {},
                                                 EncryptionPattern(5, 6));
  EXPECT_FALSE(config1->Matches(*config5));
  EXPECT_FALSE(config2->Matches(*config5));
  EXPECT_FALSE(config3->Matches(*config5));
  EXPECT_FALSE(config4->Matches(*config5));
  EXPECT_FALSE(config5->Matches(*config1));
  EXPECT_FALSE(config5->Matches(*config2));
  EXPECT_FALSE(config5->Matches(*config3));
  EXPECT_FALSE(config5->Matches(*config4));

  // Without pattern.
  auto config6 = DecryptConfig::CreateCbcsConfig(kDefaultKeyId, kDefaultIV, {},
                                                 std::nullopt);
  EXPECT_FALSE(config1->Matches(*config6));
  EXPECT_FALSE(config5->Matches(*config6));
  EXPECT_FALSE(config6->Matches(*config1));
  EXPECT_FALSE(config6->Matches(*config5));
}

TEST(DecryptConfigTest, Output) {
  std::ostringstream stream;

  // Simple 'cenc' config.
  stream << *DecryptConfig::CreateCencConfig(kDefaultKeyId, kDefaultIV, {});

  // Try with subsamples.
  stream << *DecryptConfig::CreateCencConfig(kAlternateKeyId, kAlternateIV,
                                             {{1, 2}, {3, 4}, {5, 6}});

  // Simple 'cbcs' config.
  stream << *DecryptConfig::CreateCbcsConfig(kDefaultKeyId, kDefaultIV, {},
                                             std::nullopt);

  // 'cbcs' config with subsamples and pattern.
  stream << *DecryptConfig::CreateCbcsConfig(kAlternateKeyId, kAlternateIV,
                                             {{1, 2}, {3, 4}, {5, 6}, {7, 8}},
                                             EncryptionPattern(1, 2));
}

}  // namespace media