Files
whitebox/api/license_whitebox_get_secret_string_test.cc
Aaron Vaage 77f7ef98c0 Initial Code Drop
This is the initial code drop of the reference implementation and
test cases for the Widevine Whitebox API.

In this drop, the full reference implementation for the AEAD
white-box is provided and all test cases verifying the top-level
behave have are enabled. Since the implementations can vary so much
the testing is mostly left to verifying the return codes for specific
parameter conditions.

A full reference implementation for the license white-box is provided,
however not all tests are implemented or enabled. A number of tests
have been disabled as they required a loaded license and test licenses
are still being worked on.

The two license white-box API functions that are the further from
competition are ProcessLicenseResponse() and MaskedDecryt().
ProcessLicenseResponse() is still being worked on and MaskedDecrypt()
is waiting on Decrypt() to be fully functional.

Most tests focus on verifying return code for specific parameter
conditions, but as test licenses are created, tests looking to test
the internal behaviour of license management will be added to
ProcessLicenseResponse(), Decrypt(), and MaskedDecrypt().
2020-05-18 19:45:53 -07:00

181 lines
6.4 KiB
C++

// Copyright 2020 Google LLC. All Rights Reserved.
#include "api/license_whitebox.h"
#include <string>
#include <vector>
#include "api/test_data.h"
#include "testing/include/gtest/gtest.h"
namespace {
class LicenseWhiteboxGetSecretStringTest : public ::testing::Test {
protected:
void SetUp() override {
// The secret string size is implementation specific, so this number just
// needs to be reasonably large to accommodate different implementations.
secret_string_size_ = 256;
secret_string_.resize(secret_string_size_);
const std::vector<uint8_t> init_data = GetLicenseInitData();
ASSERT_EQ(WB_License_Create(init_data.data(), init_data.size(), &whitebox_),
WB_RESULT_OK);
ASSERT_TRUE(whitebox_);
}
void TearDown() override { WB_License_Delete(whitebox_); }
void LoadLicense() {
// TODO: We will need to load a license for most tests as they will require
// a key id look-up and we get keys from licenses.
}
// TODO: These needs to be replaced with the actual keys in the loaded
// license.
const std::vector<uint8_t> crypto_key_id_ = {1, 2, 3};
const std::vector<uint8_t> hardware_key_id_ = {2, 3, 4};
const std::vector<uint8_t> bad_key_id_ = {3, 4, 5};
const std::vector<uint8_t> non_content_key_id_ = {4, 5, 6};
size_t secret_string_size_;
std::vector<uint8_t> secret_string_;
WB_License_Whitebox* whitebox_;
};
TEST_F(LicenseWhiteboxGetSecretStringTest, Success) {
// TODO: This test needs to be skipped right now because we can't load a
// license and we need a license to have key ids.
GTEST_SKIP();
LoadLicense();
ASSERT_EQ(
WB_License_GetSecretString(whitebox_, WB_CIPHER_MODE_CBC,
crypto_key_id_.data(), crypto_key_id_.size(),
secret_string_.data(), &secret_string_size_),
WB_RESULT_OK);
secret_string_.resize(secret_string_size_);
ASSERT_GT(secret_string_.size(), 0);
}
TEST_F(LicenseWhiteboxGetSecretStringTest, InvalidParameterForNullWhitebox) {
LoadLicense();
ASSERT_EQ(
WB_License_GetSecretString(nullptr, WB_CIPHER_MODE_CBC,
crypto_key_id_.data(), crypto_key_id_.size(),
secret_string_.data(), &secret_string_size_),
WB_RESULT_INVALID_PARAMETER);
}
TEST_F(LicenseWhiteboxGetSecretStringTest, InvalidParameterForNullKeyId) {
LoadLicense();
ASSERT_EQ(WB_License_GetSecretString(
whitebox_, WB_CIPHER_MODE_CBC, nullptr, crypto_key_id_.size(),
secret_string_.data(), &secret_string_size_),
WB_RESULT_INVALID_PARAMETER);
}
TEST_F(LicenseWhiteboxGetSecretStringTest, InvalidParameterForZeroKeyIdSize) {
LoadLicense();
ASSERT_EQ(WB_License_GetSecretString(
whitebox_, WB_CIPHER_MODE_CBC, crypto_key_id_.data(), 0,
secret_string_.data(), &secret_string_size_),
WB_RESULT_INVALID_PARAMETER);
}
TEST_F(LicenseWhiteboxGetSecretStringTest,
InvalidParameterForNullSecretString) {
LoadLicense();
ASSERT_EQ(WB_License_GetSecretString(
whitebox_, WB_CIPHER_MODE_CBC, crypto_key_id_.data(),
crypto_key_id_.size(), nullptr, &secret_string_size_),
WB_RESULT_INVALID_PARAMETER);
}
TEST_F(LicenseWhiteboxGetSecretStringTest,
InvalidParameterForNullSecretStringSize) {
LoadLicense();
ASSERT_EQ(WB_License_GetSecretString(
whitebox_, WB_CIPHER_MODE_CBC, crypto_key_id_.data(),
crypto_key_id_.size(), secret_string_.data(), nullptr),
WB_RESULT_INVALID_PARAMETER);
}
TEST_F(LicenseWhiteboxGetSecretStringTest, NoSuchKey) {
// TODO: This test needs to be skipped right now because we can't load a
// license and we need a license to have key ids.
GTEST_SKIP();
LoadLicense();
ASSERT_EQ(
WB_License_GetSecretString(whitebox_, WB_CIPHER_MODE_CBC,
bad_key_id_.data(), bad_key_id_.size(),
secret_string_.data(), &secret_string_size_),
WB_RESULT_NO_SUCH_KEY);
}
TEST_F(LicenseWhiteboxGetSecretStringTest, WrongKeyType) {
// TODO: This test needs to be skipped right now because we can't load a
// license and we need a license to have key ids.
GTEST_SKIP();
LoadLicense();
ASSERT_EQ(WB_License_GetSecretString(
whitebox_, WB_CIPHER_MODE_CBC, non_content_key_id_.data(),
non_content_key_id_.size(), secret_string_.data(),
&secret_string_size_),
WB_RESULT_NO_SUCH_KEY);
}
TEST_F(LicenseWhiteboxGetSecretStringTest, InsufficientSecurityLevel) {
// TODO: This test needs to be skipped right now because we can't load a
// license and we need a license to have key ids.
GTEST_SKIP();
LoadLicense();
ASSERT_EQ(
WB_License_GetSecretString(
whitebox_, WB_CIPHER_MODE_CBC, hardware_key_id_.data(),
hardware_key_id_.size(), secret_string_.data(), &secret_string_size_),
WB_RESULT_NO_SUCH_KEY);
}
TEST_F(LicenseWhiteboxGetSecretStringTest, BufferTooSmall) {
// TODO: This test needs to be skipped right now because we can't load a
// license and we need a license to have key ids.
GTEST_SKIP();
LoadLicense();
// Since the secret string is implementation specific, we don't want to make
// any big assumptions about what would be too small. Using 1 is fairly safe
// as it would not introduce enough variation to be effective. We avoid using
// zero here so that we can verify that we are not just checking for zero.
secret_string_size_ = 1;
ASSERT_EQ(
WB_License_GetSecretString(whitebox_, WB_CIPHER_MODE_CBC,
crypto_key_id_.data(), crypto_key_id_.size(),
secret_string_.data(), &secret_string_size_),
WB_RESULT_BUFFER_TOO_SMALL);
// Make sure that the output included the required size. We don't know what
// it is, so we rely on checking that it is just bigger than the "too small"
// size.
ASSERT_GT(secret_string_size_, 1);
}
TEST_F(LicenseWhiteboxGetSecretStringTest, InvalidState) {
// Purposely do not load a license so that we won't have any keys, causing
// use to be in an invalid state.
ASSERT_EQ(
WB_License_GetSecretString(whitebox_, WB_CIPHER_MODE_CBC,
crypto_key_id_.data(), crypto_key_id_.size(),
secret_string_.data(), &secret_string_size_),
WB_RESULT_INVALID_STATE);
}
} // namespace