Export media_cas_packager_sdk
This commit is contained in:
68
util/BUILD
Normal file
68
util/BUILD
Normal file
@@ -0,0 +1,68 @@
|
||||
################################################################################
|
||||
# Copyright 2017 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.
|
||||
################################################################################
|
||||
|
||||
package(
|
||||
default_visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
|
||||
filegroup(
|
||||
name = "binary_release_files",
|
||||
srcs = [
|
||||
"error_space.h",
|
||||
"status.h",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "status",
|
||||
srcs = [
|
||||
"status.cc",
|
||||
],
|
||||
hdrs = [
|
||||
"status.h",
|
||||
],
|
||||
deps = [
|
||||
":error_space",
|
||||
"//base",
|
||||
"@abseil_repo//absl/strings",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "error_space",
|
||||
hdrs = ["error_space.h"],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "proto_status",
|
||||
hdrs = ["proto_status.h"],
|
||||
deps = [
|
||||
":status",
|
||||
"//external:protobuf",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "status_test",
|
||||
srcs = ["status_test.cc"],
|
||||
deps = [
|
||||
":status",
|
||||
"//testing:gunit_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "error_space_test",
|
||||
srcs = ["error_space_test.cc"],
|
||||
deps = [
|
||||
":error_space",
|
||||
"//testing:gunit_main",
|
||||
],
|
||||
)
|
||||
|
||||
19
util/endian/BUILD
Normal file
19
util/endian/BUILD
Normal file
@@ -0,0 +1,19 @@
|
||||
################################################################################
|
||||
# Copyright 2017 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.
|
||||
################################################################################
|
||||
|
||||
package(
|
||||
default_visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
|
||||
cc_library(
|
||||
name = "endian",
|
||||
hdrs = [
|
||||
"endian.h",
|
||||
],
|
||||
)
|
||||
49
util/endian/endian.h
Normal file
49
util/endian/endian.h
Normal file
@@ -0,0 +1,49 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2017 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 UTIL_ENDIAN_ENDIAN_H_
|
||||
#define UTIL_ENDIAN_ENDIAN_H_
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include <cstdint>
|
||||
|
||||
|
||||
namespace widevine {
|
||||
|
||||
// Utilities to convert numbers between the current hosts's native byte
|
||||
// order and big-endian byte order (same as network byte order)
|
||||
class BigEndian {
|
||||
public:
|
||||
static uint32_t Load32(const char* data) {
|
||||
return (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
|
||||
}
|
||||
|
||||
static uint64_t Load64(const char* data) {
|
||||
return ((static_cast<uint64_t>(Load32(data)) << 32) | Load32(data + 4));
|
||||
}
|
||||
|
||||
static void Store32(void* p, uint32_t data) {
|
||||
for (int i = 0; i < 4; ++i)
|
||||
(reinterpret_cast<uint8_t*>(p))[i] = (data >> ((3 - i) * 8)) & 0xFF;
|
||||
}
|
||||
}; // BigEndian
|
||||
|
||||
inline uint64_t gntohll(uint64_t n) {
|
||||
uint64_t retval;
|
||||
#if __BYTE_ORDER == __BIG_ENDIAN
|
||||
retval = n;
|
||||
#else
|
||||
retval = ((uint64_t) ntohl(n & 0xFFFFFFFFLLU)) << 32;
|
||||
retval |= ntohl((n & 0xFFFFFFFF00000000LLU) >> 32);
|
||||
#endif
|
||||
return(retval);
|
||||
}
|
||||
|
||||
} // namespace widevine
|
||||
|
||||
#endif // UTIL_ENDIAN_ENDIAN_H_
|
||||
83
util/error_space.h
Normal file
83
util/error_space.h
Normal file
@@ -0,0 +1,83 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2017 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 UTIL_ERROR_SPACE_H_
|
||||
#define UTIL_ERROR_SPACE_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace widevine {
|
||||
namespace util {
|
||||
|
||||
class ErrorSpace {
|
||||
public:
|
||||
std::string SpaceName() const { return space_name_func_(this); }
|
||||
std::string String(int code) const { return code_to_string_func_(this, code); }
|
||||
|
||||
protected:
|
||||
// typedef instead of using statements for SWIG compatibility.
|
||||
typedef std::string (*SpaceNameFunc)(const ErrorSpace* space);
|
||||
typedef std::string (*CodeToStringFunc)(const ErrorSpace* space, int code);
|
||||
constexpr ErrorSpace(SpaceNameFunc space_name_func,
|
||||
CodeToStringFunc code_to_string_func)
|
||||
: space_name_func_(space_name_func),
|
||||
code_to_string_func_(code_to_string_func) {}
|
||||
|
||||
private:
|
||||
const SpaceNameFunc space_name_func_;
|
||||
const CodeToStringFunc code_to_string_func_;
|
||||
};
|
||||
|
||||
// Manages creation of error space subclasses.
|
||||
template <typename T>
|
||||
class ErrorSpaceImpl : public ErrorSpace {
|
||||
public:
|
||||
constexpr ErrorSpaceImpl()
|
||||
: ErrorSpace(&ErrorSpaceImpl::SpaceNameImpl,
|
||||
&ErrorSpaceImpl::CodeToStringImpl) {}
|
||||
|
||||
// Returns the canonical instance of the `T` error space.
|
||||
static constexpr const T* Get();
|
||||
|
||||
private:
|
||||
// These functions adapt the stateful implementation that takes a space
|
||||
// pointer to stateless static methods, so that clients of ErrorSpaceImpl are
|
||||
// safe to have constexpr global instances.
|
||||
static std::string SpaceNameImpl(const ErrorSpace* /*space*/) {
|
||||
return T::SpaceName();
|
||||
}
|
||||
|
||||
static std::string CodeToStringImpl(const ErrorSpace* /*space*/, int code) {
|
||||
return T::CodeToString(code);
|
||||
}
|
||||
};
|
||||
|
||||
namespace internal {
|
||||
|
||||
// Provides a global constexpr instance of the error space `T`.
|
||||
// We need the indirection because ErrorSpaceImpl can't declare constexpr
|
||||
// instances of T since it is not yet fully declared.
|
||||
template <typename T>
|
||||
struct ErrorSpaceInstance {
|
||||
static constexpr T value = {};
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
constexpr T ErrorSpaceInstance<T>::value;
|
||||
|
||||
} // namespace internal
|
||||
|
||||
template <typename T>
|
||||
constexpr const T* ErrorSpaceImpl<T>::Get() {
|
||||
return &internal::ErrorSpaceInstance<T>::value;
|
||||
}
|
||||
|
||||
} // namespace util
|
||||
} // namespace widevine
|
||||
|
||||
#endif // UTIL_ERROR_SPACE_H_
|
||||
31
util/error_space_test.cc
Normal file
31
util/error_space_test.cc
Normal file
@@ -0,0 +1,31 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2017 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 "util/error_space.h"
|
||||
|
||||
#include "testing/gunit.h"
|
||||
|
||||
namespace widevine {
|
||||
namespace util {
|
||||
namespace {
|
||||
|
||||
class Space1 : public util::ErrorSpaceImpl<Space1> {
|
||||
public:
|
||||
static std::string SpaceName() { return "Space1"; }
|
||||
static std::string CodeToString(int code) { return "Test" + std::to_string(code); }
|
||||
};
|
||||
|
||||
TEST(ErrorSpaceTest, Basic) {
|
||||
const ErrorSpace* space1 = Space1::Get();
|
||||
EXPECT_EQ("Space1", space1->SpaceName());
|
||||
EXPECT_EQ(Space1::CodeToString(23), space1->String(23));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace util
|
||||
} // namespace widevine
|
||||
19
util/gtl/BUILD
Normal file
19
util/gtl/BUILD
Normal file
@@ -0,0 +1,19 @@
|
||||
################################################################################
|
||||
# Copyright 2017 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.
|
||||
################################################################################
|
||||
|
||||
package(
|
||||
default_visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
|
||||
cc_library(
|
||||
name = "map_util",
|
||||
hdrs = [
|
||||
"map_util.h",
|
||||
],
|
||||
)
|
||||
58
util/gtl/map_util.h
Normal file
58
util/gtl/map_util.h
Normal file
@@ -0,0 +1,58 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2017 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 UTIL_GTL_MAP_UTIL_H_
|
||||
#define UTIL_GTL_MAP_UTIL_H_
|
||||
|
||||
namespace widevine {
|
||||
namespace gtl {
|
||||
|
||||
template <class Collection>
|
||||
const typename Collection::value_type::second_type& FindWithDefault(
|
||||
const Collection& collection,
|
||||
const typename Collection::value_type::first_type& key,
|
||||
const typename Collection::value_type::second_type& value) {
|
||||
typename Collection::const_iterator it = collection.find(key);
|
||||
if (it == collection.end()) {
|
||||
return value;
|
||||
}
|
||||
return it->second;
|
||||
}
|
||||
|
||||
template <class Collection>
|
||||
typename Collection::value_type::second_type* FindOrNull(
|
||||
const Collection& collection,
|
||||
const typename Collection::value_type::first_type& key) {
|
||||
typename Collection::iterator it = collection.find(key);
|
||||
if (it == collection.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
return &it->second;
|
||||
}
|
||||
|
||||
template <class Collection>
|
||||
typename Collection::value_type::second_type* FindOrNull(
|
||||
Collection& collection, // NOLINT
|
||||
const typename Collection::value_type::first_type& key) {
|
||||
typename Collection::iterator it = collection.find(key);
|
||||
if (it == collection.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
return &it->second;
|
||||
}
|
||||
|
||||
template <class C, class K>
|
||||
bool ContainsKey(const C& collection, const K& key) {
|
||||
typename C::const_iterator it = collection.find(key);
|
||||
return it != collection.end();
|
||||
}
|
||||
|
||||
} // namespace gtl
|
||||
} // namespace widevine
|
||||
|
||||
#endif // UTIL_GTL_MAP_UTIL_H_
|
||||
37
util/proto_status.h
Normal file
37
util/proto_status.h
Normal file
@@ -0,0 +1,37 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2017 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 UTIL_PROTO_STATUS_H_
|
||||
#define UTIL_PROTO_STATUS_H_
|
||||
|
||||
#include "google/protobuf/descriptor.h"
|
||||
#include "google/protobuf/generated_enum_reflection.h"
|
||||
#include "util/status.h"
|
||||
|
||||
namespace widevine {
|
||||
namespace util {
|
||||
|
||||
template <typename T>
|
||||
class ProtoEnumErrorSpace : public ErrorSpaceImpl<ProtoEnumErrorSpace<T>> {
|
||||
public:
|
||||
static std::string SpaceName() {
|
||||
return google::protobuf::GetEnumDescriptor<T>()->full_name();
|
||||
}
|
||||
|
||||
static std::string CodeToString(int code) {
|
||||
const google::protobuf::EnumValueDescriptor* v =
|
||||
google::protobuf::GetEnumDescriptor<T>()->FindValueByNumber(code);
|
||||
if (v) return v->name();
|
||||
return std::to_string(code);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace util
|
||||
} // namespace widevine
|
||||
|
||||
#endif // UTIL_PROTO_STATUS_H_
|
||||
55
util/proto_status_test.cc
Normal file
55
util/proto_status_test.cc
Normal file
@@ -0,0 +1,55 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2007 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 "util/proto_status.h"
|
||||
|
||||
#include "testing/gunit.h"
|
||||
#include "protos/public/errors.pb.h"
|
||||
|
||||
namespace widevine {
|
||||
namespace util {
|
||||
|
||||
TEST(StatusTest, PROVIDER_ID_MISMATCH_Status) {
|
||||
Status status(ProtoEnumErrorSpace<Errors>::Get(), PROVIDER_ID_MISMATCH,
|
||||
"provider_id_mismatch");
|
||||
EXPECT_EQ("Errors::PROVIDER_ID_MISMATCH: provider_id_mismatch",
|
||||
status.ToString());
|
||||
}
|
||||
|
||||
TEST(StatusTest, PROVIDER_ID_MISMATCH_Status2) {
|
||||
Status status(static_cast<error::StatusCode>(PROVIDER_ID_MISMATCH),
|
||||
"provider_id_mismatch");
|
||||
EXPECT_EQ("Errors::157: provider_id_mismatch", status.ToString());
|
||||
}
|
||||
|
||||
TEST(StatusTest, Same) {
|
||||
Status status1(ProtoEnumErrorSpace<Errors>::Get(), PROVIDER_ID_MISMATCH,
|
||||
"provider_id_mismatch");
|
||||
Status status2(ProtoEnumErrorSpace<Errors>::Get(), PROVIDER_ID_MISMATCH,
|
||||
"this is a provider_id_mismatch error");
|
||||
EXPECT_EQ(status1, status2);
|
||||
}
|
||||
|
||||
TEST(StatusTest, NotTheSameStatus) {
|
||||
Status status1(ProtoEnumErrorSpace<Errors>::Get(), PROVIDER_ID_MISMATCH,
|
||||
"provider_id_mismatch");
|
||||
Status status2(ProtoEnumErrorSpace<Errors>::Get(), MISSING_CLIENT_ID,
|
||||
"missing client id");
|
||||
EXPECT_NE(status1, status2);
|
||||
}
|
||||
|
||||
TEST(StatusTest, NotTheSameErrorSpace) {
|
||||
Status status1(ProtoEnumErrorSpace<Errors>::Get(), PROVIDER_ID_MISMATCH,
|
||||
"provider_id_mismatch");
|
||||
Status status2(static_cast<error::StatusCode>(PROVIDER_ID_MISMATCH),
|
||||
"this is a provider_id_mismatch error");
|
||||
EXPECT_NE(status1, status2);
|
||||
}
|
||||
|
||||
} // namespace util
|
||||
} // namespace widevine
|
||||
31
util/random/BUILD
Normal file
31
util/random/BUILD
Normal file
@@ -0,0 +1,31 @@
|
||||
################################################################################
|
||||
# Copyright 2017 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.
|
||||
################################################################################
|
||||
|
||||
package(
|
||||
default_visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "global_id",
|
||||
hdrs = [
|
||||
"global_id.h",
|
||||
],
|
||||
deps = [
|
||||
"//external:openssl",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "global_id_test",
|
||||
srcs = ["global_id_test.cc"],
|
||||
deps = [
|
||||
":global_id",
|
||||
"//base",
|
||||
"//testing:gunit_main",
|
||||
],
|
||||
)
|
||||
30
util/random/global_id.h
Normal file
30
util/random/global_id.h
Normal file
@@ -0,0 +1,30 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2017 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 UTIL_RANDOM_GLOBAL_ID_H_
|
||||
#define UTIL_RANDOM_GLOBAL_ID_H_
|
||||
|
||||
#include "openssl/rand.h"
|
||||
|
||||
namespace widevine {
|
||||
namespace util {
|
||||
namespace random {
|
||||
|
||||
static uint64_t NewGlobalID() {
|
||||
static __thread uint64_t val = 0;
|
||||
if (val == 0) {
|
||||
RAND_bytes(reinterpret_cast<uint8_t*>(&val), sizeof(val));
|
||||
}
|
||||
return val++;
|
||||
}
|
||||
|
||||
} // namespace random
|
||||
} // namespace util
|
||||
} // namespace widevine
|
||||
|
||||
#endif // UTIL_RANDOM_GLOBAL_ID_H_
|
||||
27
util/random/global_id_test.cc
Normal file
27
util/random/global_id_test.cc
Normal file
@@ -0,0 +1,27 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2017 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 "util/random/global_id.h"
|
||||
|
||||
#include "glog/logging.h"
|
||||
#include "testing/gunit.h"
|
||||
|
||||
namespace widevine {
|
||||
namespace util {
|
||||
namespace random {
|
||||
|
||||
TEST(GlobalIDTest, NewGlobalID) {
|
||||
uint64_t id = NewGlobalID();
|
||||
LOG(INFO) << "NewGlobalID " << id;
|
||||
EXPECT_EQ(id + 1, NewGlobalID());
|
||||
EXPECT_EQ(id + 2, NewGlobalID());
|
||||
}
|
||||
|
||||
} // namespace random
|
||||
} // namespace util
|
||||
} // namespace widevine
|
||||
62
util/status.cc
Normal file
62
util/status.cc
Normal file
@@ -0,0 +1,62 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2017 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 <string>
|
||||
|
||||
#include "base/macros.h"
|
||||
#include "absl/strings/str_cat.h"
|
||||
#include "util/status.h"
|
||||
|
||||
namespace widevine {
|
||||
namespace util {
|
||||
namespace {
|
||||
|
||||
const char* kLicenseServerStatusMessage[] = {"OK",
|
||||
"UNKNOWN_ERROR",
|
||||
"UNKNOWN_ERROR",
|
||||
"INVALID_ARGUMENT",
|
||||
"UNKNOWN_ERROR",
|
||||
"NOT_FOUND",
|
||||
"ALREADY_EXISTS",
|
||||
"PERMISSION_DENIED",
|
||||
"UNKNOWN_ERROR",
|
||||
"UNKNOWN_ERROR",
|
||||
"UNKNOWN_ERROR",
|
||||
"UNKNOWN_ERROR",
|
||||
"UNIMPLEMENTED",
|
||||
"INTERNAL",
|
||||
"UNAVAILABLE"};
|
||||
|
||||
} // namespace
|
||||
|
||||
std::string GenericErrorSpace::SpaceName() { return "generic"; }
|
||||
|
||||
std::string GenericErrorSpace::CodeToString(int code) {
|
||||
static_assert(
|
||||
arraysize(kLicenseServerStatusMessage) == error::NUM_ERRORS,
|
||||
"mismatching license_server_sdk status message and license_server_sdk "
|
||||
"status.");
|
||||
|
||||
if (code >= 0 && code < error::NUM_ERRORS)
|
||||
return kLicenseServerStatusMessage[code];
|
||||
return std::to_string(code);
|
||||
}
|
||||
|
||||
std::string Status::ToString() const {
|
||||
if (status_code_ == error::OK) return "OK";
|
||||
return absl::StrCat("Errors::", error_space_->String(status_code_), ": ",
|
||||
error_message_);
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const Status& x) {
|
||||
os << x.ToString();
|
||||
return os;
|
||||
}
|
||||
|
||||
} // namespace util
|
||||
} // namespace widevine
|
||||
119
util/status.h
Normal file
119
util/status.h
Normal file
@@ -0,0 +1,119 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2017 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 UTIL_STATUS_H_
|
||||
#define UTIL_STATUS_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "util/error_space.h"
|
||||
|
||||
namespace widevine {
|
||||
namespace util {
|
||||
namespace error {
|
||||
|
||||
enum StatusCode {
|
||||
// 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 must not be used for rejections
|
||||
// caused by exhausting some resource (use RESOURCE_EXHAUSTED
|
||||
// instead for those errors).
|
||||
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 generic (non license related) errors.
|
||||
NUM_ERRORS,
|
||||
};
|
||||
|
||||
} // namespace error
|
||||
|
||||
class GenericErrorSpace : public ErrorSpaceImpl<GenericErrorSpace> {
|
||||
public:
|
||||
static std::string SpaceName();
|
||||
static std::string CodeToString(int code);
|
||||
};
|
||||
|
||||
class Status {
|
||||
public:
|
||||
Status() : status_code_(error::OK) {}
|
||||
~Status() {}
|
||||
explicit Status(error::StatusCode c) : status_code_(c) {}
|
||||
Status(error::StatusCode c, const std::string& error_message)
|
||||
: status_code_(c), error_message_(error_message) {}
|
||||
Status(const ErrorSpace* e, error::StatusCode c,
|
||||
const std::string& error_message) {
|
||||
SetError(e, c, error_message);
|
||||
}
|
||||
Status(const ErrorSpace* e, int error, const std::string& error_message) {
|
||||
SetError(e, error, error_message);
|
||||
}
|
||||
void SetError(const ErrorSpace* e, int c, const std::string& error_message) {
|
||||
error_space_ = e;
|
||||
status_code_ = c;
|
||||
error_message_ = error_message;
|
||||
}
|
||||
|
||||
bool ok() const { return status_code_ == error::OK; }
|
||||
const ErrorSpace* error_space() const { return error_space_; }
|
||||
static const ErrorSpace* canonical_space() {
|
||||
return GenericErrorSpace::Get();
|
||||
}
|
||||
std::string ToString() const;
|
||||
std::string error_message() const { return error_message_; }
|
||||
int error_code() const { return status_code_; }
|
||||
|
||||
private:
|
||||
const ErrorSpace* error_space_ = GenericErrorSpace::Get();
|
||||
int status_code_;
|
||||
std::string error_message_;
|
||||
}; // class Status
|
||||
|
||||
inline Status OkStatus() { return Status(); }
|
||||
|
||||
// Here error_message_ is ignored during comparison.
|
||||
inline bool operator==(const Status& s1, const Status& s2) {
|
||||
return s1.error_space() == s2.error_space() &&
|
||||
s1.error_code() == s2.error_code();
|
||||
}
|
||||
inline bool operator!=(const Status& s1, const Status& s2) {
|
||||
return s1.error_space() != s2.error_space() ||
|
||||
s1.error_code() != s2.error_code();
|
||||
}
|
||||
|
||||
// Prints a human-readable representation of 'x' to 'os'.
|
||||
std::ostream& operator<<(std::ostream& os, const Status& x);
|
||||
|
||||
#define CHECK_OK(expression) CHECK_EQ(util::error::OK, expression.error_code())
|
||||
|
||||
|
||||
} // namespace util
|
||||
} // namespace widevine
|
||||
|
||||
#endif // UTIL_STATUS_H_
|
||||
63
util/status_test.cc
Normal file
63
util/status_test.cc
Normal file
@@ -0,0 +1,63 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2007 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 "util/status.h"
|
||||
|
||||
#include "testing/gunit.h"
|
||||
|
||||
namespace widevine {
|
||||
namespace util {
|
||||
|
||||
TEST(StatusTest, OK_Status) {
|
||||
// test case for ok status.
|
||||
Status status(error::OK);
|
||||
EXPECT_EQ("OK", status.ToString());
|
||||
}
|
||||
|
||||
TEST(StatusTest, OK_Status2) {
|
||||
// test case for ok status.
|
||||
Status status;
|
||||
EXPECT_EQ("OK", status.ToString());
|
||||
}
|
||||
|
||||
TEST(StatusTest, ALREADY_EXISTS_Status) {
|
||||
Status status(error::ALREADY_EXISTS, "it is already exist");
|
||||
EXPECT_EQ("Errors::ALREADY_EXISTS: it is already exist", status.ToString());
|
||||
}
|
||||
|
||||
// test case for status in boundary cases.
|
||||
TEST(StatusTest, UNAVAILABLE_Status) {
|
||||
Status status(error::UNAVAILABLE, "unavailable");
|
||||
EXPECT_EQ("Errors::UNAVAILABLE: unavailable", status.ToString());
|
||||
}
|
||||
|
||||
TEST(StatusTest, NoNameCode) {
|
||||
Status status(static_cast<error::StatusCode>(101), "Unknown error");
|
||||
EXPECT_EQ("Errors::101: Unknown error", status.ToString());
|
||||
}
|
||||
|
||||
TEST(StatusTest, EQUAL_OPERATOR) {
|
||||
Status status1(error::ALREADY_EXISTS, "already exists 1");
|
||||
Status status2(error::ALREADY_EXISTS, "already exists 2");
|
||||
EXPECT_EQ(status1, status2);
|
||||
}
|
||||
|
||||
TEST(StatusTest, NOT_EQUAL_OPERATOR) {
|
||||
Status status1(error::ALREADY_EXISTS, "already exists");
|
||||
Status status2(error::UNAVAILABLE, "unavailable");
|
||||
EXPECT_NE(status1, status2);
|
||||
}
|
||||
|
||||
TEST(StatusTest, NOT_EQUAL_OPERATOR_NONE_MSG) {
|
||||
Status status1(error::ALREADY_EXISTS);
|
||||
Status status2(error::UNAVAILABLE);
|
||||
EXPECT_NE(status1, status2);
|
||||
}
|
||||
|
||||
} // namespace util
|
||||
} // namespace widevine
|
||||
Reference in New Issue
Block a user