From 051a5207763942d481c6d16e5c050d906baae24d Mon Sep 17 00:00:00 2001 From: Fang Yu Date: Fri, 26 Oct 2018 14:56:05 -0700 Subject: [PATCH] Create a WvCasStatus to be used as return type of libraries in media_cas_packager_sdk. Following example: []/video/widevine/export/provisioning_sdk/public/provisioning_status.h and []/video/widevine/export/exported_root/util/status.h ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=218918745 --- example/BUILD | 3 +- example/wv_cas_ecm_example.cc | 15 +- media_cas_packager_sdk/public/BUILD | 25 ++- media_cas_packager_sdk/public/wv_cas_ecm.cc | 71 ++++--- media_cas_packager_sdk/public/wv_cas_ecm.h | 34 ++-- .../public/wv_cas_ecm_test.cc | 173 +++++++++--------- .../public/wv_cas_status.cc | 41 +++++ media_cas_packager_sdk/public/wv_cas_status.h | 56 ++++++ .../public/wv_cas_status_test.cc | 28 +++ 9 files changed, 305 insertions(+), 141 deletions(-) create mode 100644 media_cas_packager_sdk/public/wv_cas_status.cc create mode 100644 media_cas_packager_sdk/public/wv_cas_status.h create mode 100644 media_cas_packager_sdk/public/wv_cas_status_test.cc diff --git a/example/BUILD b/example/BUILD index f0514df..fe847b8 100644 --- a/example/BUILD +++ b/example/BUILD @@ -47,7 +47,8 @@ cc_binary( name = "wv_cas_ecm_example", srcs = ["wv_cas_ecm_example.cc"], deps = [ - "//base", + "@abseil_repo//absl/base:core_headers", "//media_cas_packager_sdk/public:wv_cas_ecm", + "//media_cas_packager_sdk/public:wv_cas_status", ], ) diff --git a/example/wv_cas_ecm_example.cc b/example/wv_cas_ecm_example.cc index 2d94154..4d65e34 100644 --- a/example/wv_cas_ecm_example.cc +++ b/example/wv_cas_ecm_example.cc @@ -12,6 +12,7 @@ #include #include "media_cas_packager_sdk/public/wv_cas_ecm.h" +#include "media_cas_packager_sdk/public/wv_cas_status.h" const int kContentIvSize = 16; const bool kKeyRotationEnabled = true; @@ -27,17 +28,21 @@ const char kEntitlementKey[] = "entitlement_key................."; // 32 bytes int main(int argc, char **argv) { widevine::cas::WvCasEcm wv_cas_ecm; - bool status = + widevine::cas::WvCasStatus status = wv_cas_ecm.Initialize(kContentIvSize, kKeyRotationEnabled, kCryptoMode); - if (!status) { - std::cerr << "Failed to initialize WV CAS ECM, error." << std::endl; + if (status != widevine::cas::OK) { + std::cerr << "Failed to initialize WV CAS ECM, error: " + << widevine::cas::GetWvCasStatusMessage(status) + << std::endl; } std::string ecm; status = wv_cas_ecm.GenerateEcm(kEvenKey, kEvenContentIv16Bytes, kOddKey, kOddContentIv16Bytes, kEntitlementKeyId, kEntitlementKey, &ecm); - if (!status) { - std::cerr << "Failed to generate WV CAS ECM, error" << std::endl; + if (status != widevine::cas::OK) { + std::cerr << "Failed to generate WV CAS ECM, error: " + << widevine::cas::GetWvCasStatusMessage(status) + << std::endl; } else { std::cout << "ECM size: " << ecm.size() << std::endl; std::cout << "ECM bytes: "; diff --git a/media_cas_packager_sdk/public/BUILD b/media_cas_packager_sdk/public/BUILD index 49e7b94..a1e3e29 100644 --- a/media_cas_packager_sdk/public/BUILD +++ b/media_cas_packager_sdk/public/BUILD @@ -28,7 +28,10 @@ filegroup( cc_binary( name = "libmedia_cas_packager_sdk.so", linkshared = 1, - deps = [":wv_cas_ecm"], + deps = [ + ":wv_cas_ecm", + ":wv_cas_status", + ], ) cc_library( @@ -60,6 +63,7 @@ cc_library( hdrs = ["wv_cas_ecm.h"], copts = PUBLIC_COPTS, deps = [ + ":wv_cas_status", "//base", "@abseil_repo//absl/base:core_headers", "@abseil_repo//absl/memory", @@ -80,7 +84,26 @@ cc_test( srcs = ["wv_cas_ecm_test.cc"], deps = [ ":wv_cas_ecm", + ":wv_cas_status", "//testing:gunit_main", "@abseil_repo//absl/strings", ], ) + +cc_library( + name = "wv_cas_status", + srcs = ["wv_cas_status.cc"], + hdrs = ["wv_cas_status.h"], + copts = PUBLIC_COPTS, + deps = ["//base"], +) + +cc_test( + name = "wv_cas_status_test", + size = "small", + srcs = ["wv_cas_status_test.cc"], + deps = [ + ":wv_cas_status", + "//testing:gunit_main", + ], +) diff --git a/media_cas_packager_sdk/public/wv_cas_ecm.cc b/media_cas_packager_sdk/public/wv_cas_ecm.cc index eb4606a..f8fffdb 100644 --- a/media_cas_packager_sdk/public/wv_cas_ecm.cc +++ b/media_cas_packager_sdk/public/wv_cas_ecm.cc @@ -8,21 +8,21 @@ #include "media_cas_packager_sdk/public/wv_cas_ecm.h" -#include -#include +#include #include #include #include #include "glog/logging.h" +#include "util/status.h" #include "absl/memory/memory.h" #include "absl/strings/str_cat.h" -#include "util/status.h" #include "common/crypto_util.h" #include "example/constants.h" #include "media_cas_packager_sdk/internal/ecm.h" #include "media_cas_packager_sdk/internal/ecm_generator.h" #include "media_cas_packager_sdk/internal/fixed_key_fetcher.h" +#include "media_cas_packager_sdk/public/wv_cas_status.h" #include "protos/public/media_cas.pb.h" namespace widevine { @@ -52,52 +52,51 @@ EcmInitParameters CreateEcmInitParameters(int content_iv_size, } // namespace -// TODO(user): Return a status class like -// instead of boolean. - -bool WvCasEcm::Initialize(int content_iv_size, bool key_rotation_enabled, - int crypto_mode) { +WvCasStatus WvCasEcm::Initialize(int content_iv_size, bool key_rotation_enabled, + int crypto_mode) { if (initialized_) { LOG(ERROR) << "Cannot initialize an instance of WvCasEcm more than once"; - return false; + return UNAVAILABLE; } if (content_iv_size != 8 && content_iv_size != 16) { LOG(ERROR) << "Only support content_iv_size being 8 now"; - return false; + return INVALID_ARGUMENT; } content_iv_size_ = content_iv_size; key_rotation_enabled_ = key_rotation_enabled; if (crypto_mode != kCryptoModeCtr) { LOG(ERROR) << "Only CTR crypto mode is supported by Widevine plugin for " "content encryption"; - return false; + return INVALID_ARGUMENT; } crypto_mode_ = crypto_mode; initialized_ = true; - return true; + return OK; } -bool WvCasEcm::GenerateEcm(const std::string& even_key, - const std::string& even_content_iv, const std::string& odd_key, - const std::string& odd_content_iv, - const std::string& entitlement_key_id, - const std::string& entitlement_key, std::string* ecm) const { +WvCasStatus WvCasEcm::GenerateEcm(const std::string& even_key, + const std::string& even_content_iv, + const std::string& odd_key, + const std::string& odd_content_iv, + const std::string& entitlement_key_id, + const std::string& entitlement_key, + std::string* ecm) const { DCHECK(ecm); if (!initialized_) { LOG(ERROR) << "WvCasEcm has not been properly initialized"; - return false; + return UNAVAILABLE; } if (!key_rotation_enabled_) { LOG(ERROR) << "Please call GenerateSingleKeyEcm() instead when key " "rotation is disabled"; - return false; + return UNAVAILABLE; } if (even_content_iv.size() != content_iv_size_ || odd_content_iv.size() != content_iv_size_) { LOG(ERROR) << "Size of content IV is incorrect"; - return false; + return INVALID_ARGUMENT; } // Create an instance of CasEcm in order to set the entitlement keys. @@ -117,7 +116,7 @@ bool WvCasEcm::GenerateEcm(const std::string& even_key, ecm_init_params, &entitlement_request)) .ok()) { LOG(ERROR) << "Failed to initialize ECM class."; - return false; + return INTERNAL; } FixedKeyFetcher fixed_key_fetcher( /* even_entitlement_key_id= */ entitlement_key_id, @@ -128,12 +127,12 @@ bool WvCasEcm::GenerateEcm(const std::string& even_key, &entitlement_response)) .ok()) { LOG(ERROR) << "Failed to get entitlement key."; - return false; + return INTERNAL; } if (!(status = cas_ecm->ProcessCasEncryptionResponse(entitlement_response)) .ok()) { LOG(ERROR) << "Failed to process entitlement key."; - return false; + return INTERNAL; } // Generate ECM. @@ -155,27 +154,27 @@ bool WvCasEcm::GenerateEcm(const std::string& even_key, ecm_param.key_params[1].content_ivs.push_back(odd_content_iv); *ecm = ecm_generator.GenerateEcm(ecm_param); - return true; + return OK; } -bool WvCasEcm::GenerateSingleKeyEcm(const std::string& even_key, - const std::string& even_content_iv, - const std::string& entitlement_key_id, - const std::string& entitlement_key, - std::string* ecm) const { +WvCasStatus WvCasEcm::GenerateSingleKeyEcm(const std::string& even_key, + const std::string& even_content_iv, + const std::string& entitlement_key_id, + const std::string& entitlement_key, + std::string* ecm) const { DCHECK(ecm); if (!initialized_) { LOG(ERROR) << "WvCasEcm has not been properly initialized"; - return false; + return UNAVAILABLE; } if (key_rotation_enabled_) { LOG(ERROR) << "Please call GenerateEcm() instead when key rotation is enabled"; - return false; + return UNAVAILABLE; } if (even_content_iv.size() != content_iv_size_) { LOG(ERROR) << "Size of content IV is incorrect"; - return false; + return INVALID_ARGUMENT; } // Create an instance of CasEcm in order to set the entitlement keys. @@ -195,7 +194,7 @@ bool WvCasEcm::GenerateSingleKeyEcm(const std::string& even_key, ecm_init_params, &entitlement_request)) .ok()) { LOG(ERROR) << "Failed to get initialize ECM class."; - return false; + return INTERNAL; } FixedKeyFetcher fixed_key_fetcher( /* even_entitlement_key_id= */ entitlement_key_id, @@ -206,12 +205,12 @@ bool WvCasEcm::GenerateSingleKeyEcm(const std::string& even_key, &entitlement_response)) .ok()) { LOG(ERROR) << "Failed to get entitlement key."; - return false; + return INTERNAL; } if (!(status = cas_ecm->ProcessCasEncryptionResponse(entitlement_response)) .ok()) { LOG(ERROR) << "Failed to process entitlement key."; - return false; + return INTERNAL; } // Generate ECM. @@ -227,7 +226,7 @@ bool WvCasEcm::GenerateSingleKeyEcm(const std::string& even_key, ecm_param.key_params[0].content_ivs.push_back(even_content_iv); *ecm = ecm_generator.GenerateEcm(ecm_param); - return true; + return OK; } } // namespace cas diff --git a/media_cas_packager_sdk/public/wv_cas_ecm.h b/media_cas_packager_sdk/public/wv_cas_ecm.h index 020bffd..5b089b8 100644 --- a/media_cas_packager_sdk/public/wv_cas_ecm.h +++ b/media_cas_packager_sdk/public/wv_cas_ecm.h @@ -16,7 +16,7 @@ #include #include -#include "util/status.h" +#include "media_cas_packager_sdk/public/wv_cas_status.h" namespace widevine { namespace cas { @@ -47,12 +47,12 @@ class WvCasEcm { // only CTR is supported by Widevine CAS plugin for now // // Returns: - // - A boolean indicating whether there was any error during initialization + // - A status indicating whether there was any error during initialization // // Note: // - 'even'/'odd' key in the ECM will be be encrypted using AEC_CBC - virtual bool Initialize(int content_iv_size, bool key_rotation_enabled, - int crypto_mode); + virtual WvCasStatus Initialize(int content_iv_size, bool key_rotation_enabled, + int crypto_mode); // Generate an ECM containing two keys (even and odd). Can be called when // |key_rotation_enabled| is initialized to 'true'. @@ -67,18 +67,20 @@ class WvCasEcm { // - |ecm| for returning the generated ECM, must not be nullptr // // Returns: - // - A boolean indicating whether there was any error during processing + // - A status indicating whether there was any error during processing // // Note: // - The same |entitlement_key| will be used to encrypt both |even_key| // and |odd_key| in the ECM // - Size of |even_content_iv| and |odd_content_iv| must match // |content_iv_size| set during initialization - virtual bool GenerateEcm(const std::string& even_key, - const std::string& even_content_iv, const std::string& odd_key, - const std::string& odd_content_iv, - const std::string& entitlement_key_id, - const std::string& entitlement_key, std::string* ecm) const; + virtual WvCasStatus GenerateEcm(const std::string& even_key, + const std::string& even_content_iv, + const std::string& odd_key, + const std::string& odd_content_iv, + const std::string& entitlement_key_id, + const std::string& entitlement_key, + std::string* ecm) const; // Generate an ECM containing only a singe even key. Can be called when // |key_rotation_enabled| is initialized to 'false'. @@ -91,16 +93,16 @@ class WvCasEcm { // - |ecm| for returning the generated ECM, must not be nullptr // // Returns: - // - A boolean indicating whether there was any error during processing + // - A status indicating whether there was any error during processing // // Note: // - Size of |even_content_iv| and |odd_content_iv| must match // |content_iv_size| set during initialization - virtual bool GenerateSingleKeyEcm(const std::string& even_key, - const std::string& even_content_iv, - const std::string& entitlement_key_id, - const std::string& entitlement_key, - std::string* ecm) const; + virtual WvCasStatus GenerateSingleKeyEcm(const std::string& even_key, + const std::string& even_content_iv, + const std::string& entitlement_key_id, + const std::string& entitlement_key, + std::string* ecm) const; private: bool initialized_ = false; diff --git a/media_cas_packager_sdk/public/wv_cas_ecm_test.cc b/media_cas_packager_sdk/public/wv_cas_ecm_test.cc index 3d7b8fe..1ad8da4 100644 --- a/media_cas_packager_sdk/public/wv_cas_ecm_test.cc +++ b/media_cas_packager_sdk/public/wv_cas_ecm_test.cc @@ -7,9 +7,11 @@ //////////////////////////////////////////////////////////////////////////////// #include "media_cas_packager_sdk/public/wv_cas_ecm.h" -#include "testing/gmock.h" + #include "testing/gunit.h" #include "absl/strings/escaping.h" +#include "absl/strings/str_cat.h" +#include "media_cas_packager_sdk/public/wv_cas_status.h" using ::testing::Test; @@ -30,81 +32,88 @@ class WvCasEcmTest : public Test { }; TEST_F(WvCasEcmTest, InitializeTwice) { - EXPECT_TRUE(wv_cas_ecm_.Initialize(/* content_iv_size= */ 8, - /* key_rotation_enabled= */ true, - /* crypto_mode= */ 1)); - EXPECT_FALSE(wv_cas_ecm_.Initialize(/* content_iv_size= */ 8, - /* key_rotation_enabled= */ true, - /* crypto_mode= */ 1)); + EXPECT_EQ(OK, wv_cas_ecm_.Initialize(/* content_iv_size= */ 8, + /* key_rotation_enabled= */ true, + /* crypto_mode= */ 1)); + EXPECT_EQ(UNAVAILABLE, + wv_cas_ecm_.Initialize(/* content_iv_size= */ 8, + /* key_rotation_enabled= */ true, + /* crypto_mode= */ 1)); } -TEST_F(WvCasEcmTest, InitializeContentIvSize) { - EXPECT_FALSE(wv_cas_ecm_.Initialize(/* content_iv_size= */ 4, - /* key_rotation_enabled= */ true, - /* crypto_mode= */ 1)); +TEST_F(WvCasEcmTest, InitializeInvalidContentIvSize) { + EXPECT_EQ(INVALID_ARGUMENT, + wv_cas_ecm_.Initialize(/* content_iv_size= */ 4, + /* key_rotation_enabled= */ true, + /* crypto_mode= */ 1)); } -TEST_F(WvCasEcmTest, InitializeCryptoMode) { - EXPECT_FALSE(wv_cas_ecm_.Initialize(/* content_iv_size= */ 8, - /* key_rotation_enabled= */ true, - /* crypto_mode= */ 0)); +TEST_F(WvCasEcmTest, InitializeUnsupportedCryptoMode) { + EXPECT_EQ(INVALID_ARGUMENT, + wv_cas_ecm_.Initialize(/* content_iv_size= */ 8, + /* key_rotation_enabled= */ true, + /* crypto_mode= */ 0)); } -TEST_F(WvCasEcmTest, InitializeKeyRotationEnabled) { - EXPECT_TRUE(wv_cas_ecm_.Initialize(/* content_iv_size= */ 8, - /* key_rotation_enabled= */ true, - /* crypto_mode= */ 1)); +TEST_F(WvCasEcmTest, InitializeKeyRotationEnabledThenGenerateSingleKeyEcm) { + EXPECT_EQ(OK, wv_cas_ecm_.Initialize(/* content_iv_size= */ 8, + /* key_rotation_enabled= */ true, + /* crypto_mode= */ 1)); std::string actual_ecm; - EXPECT_FALSE(wv_cas_ecm_.GenerateSingleKeyEcm("", "", "", "", &actual_ecm)); + EXPECT_EQ(UNAVAILABLE, + wv_cas_ecm_.GenerateSingleKeyEcm("", "", "", "", &actual_ecm)); } -TEST_F(WvCasEcmTest, InitializeKeyRotationDisabled) { - EXPECT_TRUE(wv_cas_ecm_.Initialize(/* content_iv_size= */ 8, - /* key_rotation_enabled= */ false, - /* crypto_mode= */ 1)); +TEST_F(WvCasEcmTest, InitializeKeyRotationDisabledThenGenerateEcm) { + EXPECT_EQ(OK, wv_cas_ecm_.Initialize(/* content_iv_size= */ 8, + /* key_rotation_enabled= */ false, + /* crypto_mode= */ 1)); std::string actual_ecm; - EXPECT_FALSE(wv_cas_ecm_.GenerateEcm("", "", "", "", "", "", &actual_ecm)); + EXPECT_EQ(UNAVAILABLE, + wv_cas_ecm_.GenerateEcm("", "", "", "", "", "", &actual_ecm)); } TEST_F(WvCasEcmTest, GenerateEcmInvalidContentIv) { - EXPECT_TRUE(wv_cas_ecm_.Initialize(/* content_iv_size= */ 8, - /* key_rotation_enabled= */ true, - /* crypto_mode= */ 1)); + EXPECT_EQ(OK, wv_cas_ecm_.Initialize(/* content_iv_size= */ 8, + /* key_rotation_enabled= */ true, + /* crypto_mode= */ 1)); std::string actual_ecm; - EXPECT_FALSE(wv_cas_ecm_.GenerateEcm( - /* even_key= */ kEvenKey, - /* even_content_iv= */ kEvenContentIv8Bytes, - /* odd_key= */ kOddKey, - /* odd_content_iv= */ "123456789", - /* entitlement_key_id= */ kEntitlementKeyId, - /* entitlement_key= */ kEntitlementKey, &actual_ecm)); + EXPECT_EQ(INVALID_ARGUMENT, + wv_cas_ecm_.GenerateEcm( + /* even_key= */ kEvenKey, + /* even_content_iv= */ kEvenContentIv8Bytes, + /* odd_key= */ kOddKey, + /* odd_content_iv= */ "123456789", + /* entitlement_key_id= */ kEntitlementKeyId, + /* entitlement_key= */ kEntitlementKey, &actual_ecm)); } TEST_F(WvCasEcmTest, GenerateSingleKeyEcmInvalidContentIv) { - EXPECT_TRUE(wv_cas_ecm_.Initialize(/* content_iv_size= */ 8, - /* key_rotation_enabled= */ false, - /* crypto_mode= */ 1)); + EXPECT_EQ(OK, wv_cas_ecm_.Initialize(/* content_iv_size= */ 8, + /* key_rotation_enabled= */ false, + /* crypto_mode= */ 1)); std::string actual_ecm; - EXPECT_FALSE(wv_cas_ecm_.GenerateSingleKeyEcm( - /* even_key= */ kEvenKey, - /* even_content_iv= */ "1234", - /* entitlement_key_id= */ kEntitlementKeyId, - /* entitlement_key= */ kEntitlementKey, &actual_ecm)); + EXPECT_EQ(INVALID_ARGUMENT, + wv_cas_ecm_.GenerateSingleKeyEcm( + /* even_key= */ kEvenKey, + /* even_content_iv= */ "1234", + /* entitlement_key_id= */ kEntitlementKeyId, + /* entitlement_key= */ kEntitlementKey, &actual_ecm)); } TEST_F(WvCasEcmTest, GenerateEcm8BytesContentIvSuccess) { - EXPECT_TRUE(wv_cas_ecm_.Initialize(/* content_iv_size= */ 8, - /* key_rotation_enabled= */ true, - /* crypto_mode= */ 1)); + EXPECT_EQ(OK, wv_cas_ecm_.Initialize(/* content_iv_size= */ 8, + /* key_rotation_enabled= */ true, + /* crypto_mode= */ 1)); std::string actual_ecm; - EXPECT_TRUE(wv_cas_ecm_.GenerateEcm( - /* even_key= */ kEvenKey, - /* even_content_iv= */ kEvenContentIv8Bytes, - /* odd_key= */ kOddKey, - /* odd_content_iv= */ kOddContentIv8Bytes, - /* entitlement_key_id= */ kEntitlementKeyId, - /* entitlement_key= */ kEntitlementKey, &actual_ecm)); + EXPECT_EQ(OK, wv_cas_ecm_.GenerateEcm( + /* even_key= */ kEvenKey, + /* even_content_iv= */ kEvenContentIv8Bytes, + /* odd_key= */ kOddKey, + /* odd_content_iv= */ kOddContentIv8Bytes, + /* entitlement_key_id= */ kEntitlementKeyId, + /* entitlement_key= */ kEntitlementKey, &actual_ecm)); EXPECT_EQ( "4ad4010380656e745f6b65795f69642e2e2e2e2e2ea5693deeba52b4cb27e7021eefa2f8" @@ -116,16 +125,16 @@ TEST_F(WvCasEcmTest, GenerateEcm8BytesContentIvSuccess) { } TEST_F(WvCasEcmTest, GenerateSingleKeyEcm8BytesContentIvSuccess) { - EXPECT_TRUE(wv_cas_ecm_.Initialize(/* content_iv_size= */ 8, - /* key_rotation_enabled= */ false, - /* crypto_mode= */ 1)); + EXPECT_EQ(OK, wv_cas_ecm_.Initialize(/* content_iv_size= */ 8, + /* key_rotation_enabled= */ false, + /* crypto_mode= */ 1)); std::string actual_ecm; - EXPECT_TRUE(wv_cas_ecm_.GenerateSingleKeyEcm( - /* even_key= */ kEvenKey, - /* even_content_iv= */ kEvenContentIv8Bytes, - /* entitlement_key_id= */ kEntitlementKeyId, - /* entitlement_key= */ kEntitlementKey, &actual_ecm)); + EXPECT_EQ(OK, wv_cas_ecm_.GenerateSingleKeyEcm( + /* even_key= */ kEvenKey, + /* even_content_iv= */ kEvenContentIv8Bytes, + /* entitlement_key_id= */ kEntitlementKeyId, + /* entitlement_key= */ kEntitlementKey, &actual_ecm)); EXPECT_EQ( "4ad4010280656e745f6b65795f69642e2e2e2e2e2ea5693deeba52b4cb27e7021eefa2f8" @@ -135,20 +144,20 @@ TEST_F(WvCasEcmTest, GenerateSingleKeyEcm8BytesContentIvSuccess) { } TEST_F(WvCasEcmTest, GenerateEcm16BytesContentIvSuccess) { - EXPECT_TRUE(wv_cas_ecm_.Initialize(/* content_iv_size= */ 16, - /* key_rotation_enabled= */ true, - /* crypto_mode= */ 1)); + EXPECT_EQ(OK, wv_cas_ecm_.Initialize(/* content_iv_size= */ 16, + /* key_rotation_enabled= */ true, + /* crypto_mode= */ 1)); std::string actual_ecm; - EXPECT_TRUE(wv_cas_ecm_.GenerateEcm( - /* even_key= */ kEvenKey, - /* even_content_iv= */ - absl::StrCat(kEvenContentIv8Bytes, kEvenContentIv8Bytes), - /* odd_key= */ kOddKey, - /* odd_content_iv= */ - absl::StrCat(kOddContentIv8Bytes, kOddContentIv8Bytes), - /* entitlement_key_id= */ kEntitlementKeyId, - /* entitlement_key= */ kEntitlementKey, &actual_ecm)); + EXPECT_EQ(OK, wv_cas_ecm_.GenerateEcm( + /* even_key= */ kEvenKey, + /* even_content_iv= */ + absl::StrCat(kEvenContentIv8Bytes, kEvenContentIv8Bytes), + /* odd_key= */ kOddKey, + /* odd_content_iv= */ + absl::StrCat(kOddContentIv8Bytes, kOddContentIv8Bytes), + /* entitlement_key_id= */ kEntitlementKeyId, + /* entitlement_key= */ kEntitlementKey, &actual_ecm)); EXPECT_EQ( "4ad40103c0656e745f6b65795f69642e2e2e2e2e2ea5693deeba52b4cb27e7021eefa2f8" @@ -160,17 +169,17 @@ TEST_F(WvCasEcmTest, GenerateEcm16BytesContentIvSuccess) { } TEST_F(WvCasEcmTest, GenerateSingleKeyEcm16BytesContentIvSuccess) { - EXPECT_TRUE(wv_cas_ecm_.Initialize(/* content_iv_size= */ 16, - /* key_rotation_enabled= */ false, - /* crypto_mode= */ 1)); + EXPECT_EQ(OK, wv_cas_ecm_.Initialize(/* content_iv_size= */ 16, + /* key_rotation_enabled= */ false, + /* crypto_mode= */ 1)); std::string actual_ecm; - EXPECT_TRUE(wv_cas_ecm_.GenerateSingleKeyEcm( - /* even_key= */ kEvenKey, - /* even_content_iv= */ - absl::StrCat(kEvenContentIv8Bytes, kEvenContentIv8Bytes), - /* entitlement_key_id= */ kEntitlementKeyId, - /* entitlement_key= */ kEntitlementKey, &actual_ecm)); + EXPECT_EQ(OK, wv_cas_ecm_.GenerateSingleKeyEcm( + /* even_key= */ kEvenKey, + /* even_content_iv= */ + absl::StrCat(kEvenContentIv8Bytes, kEvenContentIv8Bytes), + /* entitlement_key_id= */ kEntitlementKeyId, + /* entitlement_key= */ kEntitlementKey, &actual_ecm)); EXPECT_EQ( "4ad40102c0656e745f6b65795f69642e2e2e2e2e2ea5693deeba52b4cb27e7021eefa2f8" diff --git a/media_cas_packager_sdk/public/wv_cas_status.cc b/media_cas_packager_sdk/public/wv_cas_status.cc new file mode 100644 index 0000000..984632f --- /dev/null +++ b/media_cas_packager_sdk/public/wv_cas_status.cc @@ -0,0 +1,41 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright 2018 Google LLC. +// +// This software is licensed under the terms defined in the Widevine Master +// License Agreement. For a copy of this agreement, please contact +// widevine-licensing@google.com. +//////////////////////////////////////////////////////////////////////////////// + +#include "media_cas_packager_sdk/public/wv_cas_status.h" + +#include "base/macros.h" + +namespace widevine { +namespace cas { + +static const char* kWvCasStatusMessage[] = { + "OK", // OK = 0, + "", + "", + "Invalid argument", // INVALID_ARGUMENT = 3, + "", + "Not found", // NOT_FOUND = 5, + "Already exists", // ALREADY_EXISTS = 6, + "Permission denied", // PERMISSION_DENIED = 7, + "", + "", + "", + "", + "Unimplemented", // UNIMPLEMENTED = 12, + "Internal", // INTERNAL = 13, + "Unavailable", // UNAVAILABLE = 14, +}; + +std::string GetWvCasStatusMessage(WvCasStatus status) { + static_assert(arraysize(kWvCasStatusMessage) == NUM_WV_CAS_STATUS, + "mismatching status message and status."); + return kWvCasStatusMessage[status]; +} + +} // namespace cas +} // namespace widevine diff --git a/media_cas_packager_sdk/public/wv_cas_status.h b/media_cas_packager_sdk/public/wv_cas_status.h new file mode 100644 index 0000000..99e49f6 --- /dev/null +++ b/media_cas_packager_sdk/public/wv_cas_status.h @@ -0,0 +1,56 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright 2018 Google LLC. +// +// This software is licensed under the terms defined in the Widevine Master +// License Agreement. For a copy of this agreement, please contact +// widevine-licensing@google.com. +//////////////////////////////////////////////////////////////////////////////// + +#ifndef MEDIA_CAS_PACKAGER_SDK_PUBLIC_WV_CAS_STATUS_H_ +#define MEDIA_CAS_PACKAGER_SDK_PUBLIC_WV_CAS_STATUS_H_ + +#include + +namespace widevine { +namespace cas { + +enum WvCasStatus { + // Success. + OK = 0, + + // Client specified an invalid argument. + INVALID_ARGUMENT = 3, + + // Some requested entity (e.g., file or directory) was not found. + NOT_FOUND = 5, + + // Some entity that we attempted to create (e.g., file or directory) + // already exists. + ALREADY_EXISTS = 6, + + // The caller does not have permission to execute the specified + // operation. + PERMISSION_DENIED = 7, + + // Operation is not implemented or not supported/enabled in this service. + UNIMPLEMENTED = 12, + + // Internal errors. Means some invariants expected by underlying + // system has been broken. If you see one of these errors, + // something is very broken. + INTERNAL = 13, + + // Operation is not implemented or not supported/enabled in this service. + UNAVAILABLE = 14, + + // Number of errors. + NUM_WV_CAS_STATUS, +}; + +// Returns the message std::string for the given WvCasStatus. +std::string GetWvCasStatusMessage(WvCasStatus status); + +} // namespace cas +} // namespace widevine + +#endif // MEDIA_CAS_PACKAGER_SDK_PUBLIC_WV_CAS_STATUS_H_ diff --git a/media_cas_packager_sdk/public/wv_cas_status_test.cc b/media_cas_packager_sdk/public/wv_cas_status_test.cc new file mode 100644 index 0000000..511310e --- /dev/null +++ b/media_cas_packager_sdk/public/wv_cas_status_test.cc @@ -0,0 +1,28 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright 2018 Google LLC. +// +// This software is licensed under the terms defined in the Widevine Master +// License Agreement. For a copy of this agreement, please contact +// widevine-licensing@google.com. +//////////////////////////////////////////////////////////////////////////////// + +#include "media_cas_packager_sdk/public/wv_cas_status.h" + +#include "testing/gunit.h" + +namespace widevine { +namespace cas { + +TEST(WvCasStatusTest, GetWvCasStatusMessage) { + EXPECT_EQ("OK", GetWvCasStatusMessage(OK)); + EXPECT_EQ("Invalid argument", GetWvCasStatusMessage(INVALID_ARGUMENT)); + EXPECT_EQ("Not found", GetWvCasStatusMessage(NOT_FOUND)); + EXPECT_EQ("Already exists", GetWvCasStatusMessage(ALREADY_EXISTS)); + EXPECT_EQ("Permission denied", GetWvCasStatusMessage(PERMISSION_DENIED)); + EXPECT_EQ("Unimplemented", GetWvCasStatusMessage(UNIMPLEMENTED)); + EXPECT_EQ("Internal", GetWvCasStatusMessage(INTERNAL)); + EXPECT_EQ("Unavailable", GetWvCasStatusMessage(UNAVAILABLE)); +} + +} // namespace cas +} // namespace widevine