mirror of
https://github.com/google/sandboxed-api.git
synced 2024-03-22 13:11:30 +08:00
Migrate to open-source absl::StatusOr<>
This removes our own fork of `absl::StatusOr<>`. Sandboxed API still includes a custom matcher for Googletest, as that is not open source yet. For compatibility, the `statusor.h` header is still retained and now aliases `sapi::StatusOr<>` to `absl::StatusOr<>`. PiperOrigin-RevId: 329916309 Change-Id: I0544b73a9e312dce499bc4128c28457e04ab9929
This commit is contained in:
parent
23da55c19a
commit
fdf0483ca0
|
@ -18,7 +18,7 @@ project(absl-download NONE)
|
||||||
include(ExternalProject)
|
include(ExternalProject)
|
||||||
ExternalProject_Add(absl
|
ExternalProject_Add(absl
|
||||||
GIT_REPOSITORY https://github.com/abseil/abseil-cpp
|
GIT_REPOSITORY https://github.com/abseil/abseil-cpp
|
||||||
GIT_TAG 6e18c7115df9b7ca0987cc346b1b1d4b3cc829b3 # 2020-04-28
|
GIT_TAG 0e9921b75a0fdd639a504ec8443fc1fe801becd7 # 2020-09-02
|
||||||
SOURCE_DIR "${CMAKE_BINARY_DIR}/absl-src"
|
SOURCE_DIR "${CMAKE_BINARY_DIR}/absl-src"
|
||||||
BINARY_DIR "${CMAKE_BINARY_DIR}/absl-build"
|
BINARY_DIR "${CMAKE_BINARY_DIR}/absl-build"
|
||||||
CONFIGURE_COMMAND ""
|
CONFIGURE_COMMAND ""
|
||||||
|
|
|
@ -43,6 +43,8 @@ add_library(sapi_embed_file STATIC
|
||||||
add_library(sapi::embed_file ALIAS sapi_embed_file)
|
add_library(sapi::embed_file ALIAS sapi_embed_file)
|
||||||
target_link_libraries(sapi_embed_file PRIVATE
|
target_link_libraries(sapi_embed_file PRIVATE
|
||||||
absl::flat_hash_map
|
absl::flat_hash_map
|
||||||
|
absl::status
|
||||||
|
absl::statusor
|
||||||
absl::strings
|
absl::strings
|
||||||
absl::synchronization
|
absl::synchronization
|
||||||
glog::glog
|
glog::glog
|
||||||
|
@ -65,6 +67,8 @@ add_library(sapi::sapi ALIAS sapi_sapi)
|
||||||
target_link_libraries(sapi_sapi
|
target_link_libraries(sapi_sapi
|
||||||
PRIVATE absl::flat_hash_map
|
PRIVATE absl::flat_hash_map
|
||||||
absl::memory
|
absl::memory
|
||||||
|
absl::status
|
||||||
|
absl::statusor
|
||||||
absl::str_format
|
absl::str_format
|
||||||
absl::strings
|
absl::strings
|
||||||
absl::synchronization
|
absl::synchronization
|
||||||
|
@ -76,7 +80,6 @@ target_link_libraries(sapi_sapi
|
||||||
sandbox2::strerror
|
sandbox2::strerror
|
||||||
sandbox2::util
|
sandbox2::util
|
||||||
sapi::embed_file
|
sapi::embed_file
|
||||||
sapi::status
|
|
||||||
sapi::vars
|
sapi::vars
|
||||||
PUBLIC absl::core_headers
|
PUBLIC absl::core_headers
|
||||||
sandbox2::client
|
sandbox2::client
|
||||||
|
@ -137,6 +140,8 @@ add_library(sapi_vars STATIC
|
||||||
add_library(sapi::vars ALIAS sapi_vars)
|
add_library(sapi::vars ALIAS sapi_vars)
|
||||||
target_link_libraries(sapi_vars PRIVATE
|
target_link_libraries(sapi_vars PRIVATE
|
||||||
absl::core_headers
|
absl::core_headers
|
||||||
|
absl::status
|
||||||
|
absl::statusor
|
||||||
absl::str_format
|
absl::str_format
|
||||||
absl::strings
|
absl::strings
|
||||||
absl::synchronization
|
absl::synchronization
|
||||||
|
@ -147,7 +152,6 @@ target_link_libraries(sapi_vars PRIVATE
|
||||||
sapi::lenval_core
|
sapi::lenval_core
|
||||||
sapi::proto_arg_proto
|
sapi::proto_arg_proto
|
||||||
sapi::status
|
sapi::status
|
||||||
sapi::statusor
|
|
||||||
sapi::var_type
|
sapi::var_type
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -178,6 +182,7 @@ if(SAPI_ENABLE_TESTS)
|
||||||
)
|
)
|
||||||
target_link_libraries(sapi_test PRIVATE
|
target_link_libraries(sapi_test PRIVATE
|
||||||
absl::memory
|
absl::memory
|
||||||
|
absl::status
|
||||||
benchmark
|
benchmark
|
||||||
sapi::sapi
|
sapi::sapi
|
||||||
sapi::status
|
sapi::status
|
||||||
|
|
|
@ -34,9 +34,9 @@ def sapi_deps():
|
||||||
maybe(
|
maybe(
|
||||||
http_archive,
|
http_archive,
|
||||||
name = "com_google_absl",
|
name = "com_google_absl",
|
||||||
sha256 = "6668ada01192e2b95b42bb3668cfa5282c047de5176f5e567028e12f8bfb8aef", # 2020-04-28
|
sha256 = "8061df0ebbd3f599bcd3f5e57fb8003564d50a9b6a81a7f968fb0196b952365d", # 2020-09-02
|
||||||
strip_prefix = "abseil-cpp-6e18c7115df9b7ca0987cc346b1b1d4b3cc829b3",
|
strip_prefix = "abseil-cpp-0e9921b75a0fdd639a504ec8443fc1fe801becd7",
|
||||||
urls = ["https://github.com/abseil/abseil-cpp/archive/6e18c7115df9b7ca0987cc346b1b1d4b3cc829b3.zip"],
|
urls = ["https://github.com/abseil/abseil-cpp/archive/0e9921b75a0fdd639a504ec8443fc1fe801becd7.zip"],
|
||||||
)
|
)
|
||||||
maybe(
|
maybe(
|
||||||
http_archive,
|
http_archive,
|
||||||
|
|
|
@ -32,11 +32,11 @@ add_executable(main_zlib
|
||||||
main_zlib.cc
|
main_zlib.cc
|
||||||
)
|
)
|
||||||
target_link_libraries(main_zlib PRIVATE
|
target_link_libraries(main_zlib PRIVATE
|
||||||
|
absl::status
|
||||||
sapi::base
|
sapi::base
|
||||||
glog::glog
|
glog::glog
|
||||||
sapi::flags
|
sapi::flags
|
||||||
sapi::sapi
|
sapi::sapi
|
||||||
sapi::status
|
sapi::status
|
||||||
sapi::statusor
|
|
||||||
sapi::zlib_sapi
|
sapi::zlib_sapi
|
||||||
)
|
)
|
||||||
|
|
|
@ -271,6 +271,8 @@ target_link_libraries(sandbox2_sandbox2
|
||||||
absl::flat_hash_set
|
absl::flat_hash_set
|
||||||
absl::memory
|
absl::memory
|
||||||
absl::optional
|
absl::optional
|
||||||
|
absl::status
|
||||||
|
absl::statusor
|
||||||
absl::str_format
|
absl::str_format
|
||||||
absl::strings
|
absl::strings
|
||||||
absl::synchronization
|
absl::synchronization
|
||||||
|
@ -300,7 +302,6 @@ target_link_libraries(sandbox2_sandbox2
|
||||||
sandbox2::util
|
sandbox2::util
|
||||||
sandbox2::violation_proto
|
sandbox2::violation_proto
|
||||||
sapi::base
|
sapi::base
|
||||||
sapi::statusor
|
|
||||||
PUBLIC sapi::flags
|
PUBLIC sapi::flags
|
||||||
sapi::status
|
sapi::status
|
||||||
sandbox2::logsink
|
sandbox2::logsink
|
||||||
|
@ -351,6 +352,8 @@ add_library(sandbox2_forkserver STATIC
|
||||||
add_library(sandbox2::forkserver ALIAS sandbox2_forkserver)
|
add_library(sandbox2::forkserver ALIAS sandbox2_forkserver)
|
||||||
target_link_libraries(sandbox2_forkserver PRIVATE
|
target_link_libraries(sandbox2_forkserver PRIVATE
|
||||||
absl::memory
|
absl::memory
|
||||||
|
absl::status
|
||||||
|
absl::statusor
|
||||||
absl::str_format
|
absl::str_format
|
||||||
absl::strings
|
absl::strings
|
||||||
libcap::libcap
|
libcap::libcap
|
||||||
|
@ -369,7 +372,6 @@ target_link_libraries(sandbox2_forkserver PRIVATE
|
||||||
sandbox2::util
|
sandbox2::util
|
||||||
sapi::base
|
sapi::base
|
||||||
sapi::raw_logging
|
sapi::raw_logging
|
||||||
sapi::statusor
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# sandboxed_api/sandbox2:fork_client
|
# sandboxed_api/sandbox2:fork_client
|
||||||
|
@ -397,6 +399,7 @@ target_link_libraries(sandbox2_mounts PRIVATE
|
||||||
absl::core_headers
|
absl::core_headers
|
||||||
absl::flat_hash_set
|
absl::flat_hash_set
|
||||||
absl::status
|
absl::status
|
||||||
|
absl::statusor
|
||||||
absl::str_format
|
absl::str_format
|
||||||
absl::strings
|
absl::strings
|
||||||
protobuf::libprotobuf
|
protobuf::libprotobuf
|
||||||
|
@ -408,7 +411,6 @@ target_link_libraries(sandbox2_mounts PRIVATE
|
||||||
sapi::base
|
sapi::base
|
||||||
sapi::raw_logging
|
sapi::raw_logging
|
||||||
sapi::status
|
sapi::status
|
||||||
sapi::statusor
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# sandboxed_api/sandbox2:namespace
|
# sandboxed_api/sandbox2:namespace
|
||||||
|
@ -463,8 +465,8 @@ target_link_libraries(sandbox2_util
|
||||||
sandbox2::strerror
|
sandbox2::strerror
|
||||||
sapi::base
|
sapi::base
|
||||||
sapi::raw_logging
|
sapi::raw_logging
|
||||||
sapi::statusor
|
|
||||||
PUBLIC absl::status
|
PUBLIC absl::status
|
||||||
|
absl::statusor
|
||||||
)
|
)
|
||||||
target_compile_options(sandbox2_util PRIVATE
|
target_compile_options(sandbox2_util PRIVATE
|
||||||
# The default is 16384, however we need to do a clone with a
|
# The default is 16384, however we need to do a clone with a
|
||||||
|
@ -482,12 +484,13 @@ add_library(sandbox2::buffer ALIAS sandbox2_buffer)
|
||||||
target_link_libraries(sandbox2_buffer PRIVATE
|
target_link_libraries(sandbox2_buffer PRIVATE
|
||||||
absl::core_headers
|
absl::core_headers
|
||||||
absl::memory
|
absl::memory
|
||||||
|
absl::status
|
||||||
|
absl::statusor
|
||||||
absl::strings
|
absl::strings
|
||||||
sandbox2::strerror
|
sandbox2::strerror
|
||||||
sandbox2::util
|
sandbox2::util
|
||||||
sapi::base
|
sapi::base
|
||||||
sapi::status
|
sapi::status
|
||||||
sapi::statusor
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# sandboxed_api/sandbox2:forkserver_proto
|
# sandboxed_api/sandbox2:forkserver_proto
|
||||||
|
@ -527,6 +530,8 @@ add_library(sandbox2_comms STATIC
|
||||||
add_library(sandbox2::comms ALIAS sandbox2_comms)
|
add_library(sandbox2::comms ALIAS sandbox2_comms)
|
||||||
target_link_libraries(sandbox2_comms
|
target_link_libraries(sandbox2_comms
|
||||||
PRIVATE absl::memory
|
PRIVATE absl::memory
|
||||||
|
absl::status
|
||||||
|
absl::statusor
|
||||||
absl::str_format
|
absl::str_format
|
||||||
absl::strings
|
absl::strings
|
||||||
sandbox2::strerror
|
sandbox2::strerror
|
||||||
|
@ -534,7 +539,6 @@ target_link_libraries(sandbox2_comms
|
||||||
sapi::base
|
sapi::base
|
||||||
sapi::raw_logging
|
sapi::raw_logging
|
||||||
sapi::status_proto
|
sapi::status_proto
|
||||||
sapi::statusor
|
|
||||||
PUBLIC absl::core_headers
|
PUBLIC absl::core_headers
|
||||||
absl::status
|
absl::status
|
||||||
absl::synchronization
|
absl::synchronization
|
||||||
|
@ -832,6 +836,7 @@ if(SAPI_ENABLE_TESTS)
|
||||||
)
|
)
|
||||||
target_link_libraries(stack_trace_test PRIVATE
|
target_link_libraries(stack_trace_test PRIVATE
|
||||||
absl::memory
|
absl::memory
|
||||||
|
absl::status
|
||||||
absl::strings
|
absl::strings
|
||||||
sandbox2::bpf_helper
|
sandbox2::bpf_helper
|
||||||
sandbox2::fileops
|
sandbox2::fileops
|
||||||
|
|
|
@ -27,7 +27,6 @@ cc_binary(
|
||||||
deps = [
|
deps = [
|
||||||
"//sandboxed_api/sandbox2",
|
"//sandboxed_api/sandbox2",
|
||||||
"//sandboxed_api/sandbox2:comms",
|
"//sandboxed_api/sandbox2:comms",
|
||||||
"//sandboxed_api/sandbox2/network_proxy:filtering",
|
|
||||||
"//sandboxed_api/sandbox2/util:bpf_helper",
|
"//sandboxed_api/sandbox2/util:bpf_helper",
|
||||||
"//sandboxed_api/sandbox2/util:fileops",
|
"//sandboxed_api/sandbox2/util:fileops",
|
||||||
"//sandboxed_api/sandbox2/util:runfiles",
|
"//sandboxed_api/sandbox2/util:runfiles",
|
||||||
|
@ -50,6 +49,7 @@ cc_binary(
|
||||||
"//sandboxed_api/util:status",
|
"//sandboxed_api/util:status",
|
||||||
"@com_google_absl//absl/status",
|
"@com_google_absl//absl/status",
|
||||||
"@com_google_absl//absl/status:statusor",
|
"@com_google_absl//absl/status:statusor",
|
||||||
|
"@com_google_absl//absl/strings",
|
||||||
"@com_google_absl//absl/strings:str_format",
|
"@com_google_absl//absl/strings:str_format",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
|
@ -31,13 +31,14 @@ target_link_libraries(sandbox2_networkproxy_sandbox PRIVATE
|
||||||
sapi::flags
|
sapi::flags
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
# sandboxed_api/sandbox2/examples/networkproxy:networkproxy_bin
|
# sandboxed_api/sandbox2/examples/networkproxy:networkproxy_bin
|
||||||
add_executable(sandbox2_networkproxy_bin
|
add_executable(sandbox2_networkproxy_bin
|
||||||
networkproxy_bin.cc
|
networkproxy_bin.cc
|
||||||
)
|
)
|
||||||
add_executable(sandbox2::networkproxy_bin ALIAS sandbox2_networkproxy_bin)
|
add_executable(sandbox2::networkproxy_bin ALIAS sandbox2_networkproxy_bin)
|
||||||
target_link_libraries(sandbox2_networkproxy_bin PRIVATE
|
target_link_libraries(sandbox2_networkproxy_bin PRIVATE
|
||||||
|
absl::status
|
||||||
|
absl::statusor
|
||||||
absl::str_format
|
absl::str_format
|
||||||
glog::glog
|
glog::glog
|
||||||
gflags::gflags
|
gflags::gflags
|
||||||
|
@ -48,6 +49,4 @@ target_link_libraries(sandbox2_networkproxy_bin PRIVATE
|
||||||
sapi::base
|
sapi::base
|
||||||
sapi::flags
|
sapi::flags
|
||||||
sapi::status
|
sapi::status
|
||||||
sapi::statusor
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
#include "sandboxed_api/util/flag.h"
|
#include "sandboxed_api/util/flag.h"
|
||||||
#include "absl/status/status.h"
|
#include "absl/status/status.h"
|
||||||
#include "absl/status/statusor.h"
|
#include "absl/status/statusor.h"
|
||||||
|
#include "absl/strings/str_cat.h"
|
||||||
#include "absl/strings/str_format.h"
|
#include "absl/strings/str_format.h"
|
||||||
#include "sandboxed_api/sandbox2/client.h"
|
#include "sandboxed_api/sandbox2/client.h"
|
||||||
#include "sandboxed_api/sandbox2/comms.h"
|
#include "sandboxed_api/sandbox2/comms.h"
|
||||||
|
|
|
@ -33,14 +33,14 @@ add_library(sandbox2_network_proxy_filtering STATIC
|
||||||
filtering.h
|
filtering.h
|
||||||
)
|
)
|
||||||
add_library(sandbox2::network_proxy_filtering ALIAS sandbox2_network_proxy_filtering)
|
add_library(sandbox2::network_proxy_filtering ALIAS sandbox2_network_proxy_filtering)
|
||||||
target_link_libraries(sandbox2_network_proxy_filtering PRIVATE
|
target_link_libraries(sandbox2_network_proxy_filtering
|
||||||
absl::memory
|
PRIVATE absl::memory
|
||||||
glog::glog
|
absl::status
|
||||||
sandbox2::comms
|
glog::glog
|
||||||
sandbox2::fileops
|
sandbox2::comms
|
||||||
sapi::base
|
sandbox2::fileops
|
||||||
|
sapi::base
|
||||||
PUBLIC sapi::status
|
PUBLIC sapi::status
|
||||||
sapi::statusor
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# sandboxed_api/sandbox2/network_proxy:client
|
# sandboxed_api/sandbox2/network_proxy:client
|
||||||
|
|
|
@ -41,7 +41,12 @@ STATIC_LINKOPTS = [
|
||||||
]
|
]
|
||||||
|
|
||||||
# TODO(https://github.com/bazelbuild/bazel/issues/8672): Remove this workaround
|
# TODO(https://github.com/bazelbuild/bazel/issues/8672): Remove this workaround
|
||||||
EXTRA_FULLY_STATIC_LINKOPTS = ["-l:libstdc++.a"]
|
# Change is scheduled for Bazel 4.0. Specifying
|
||||||
|
# `--incompatible_linkopts_to_linklibs` also works
|
||||||
|
EXTRA_FULLY_STATIC_LINKOPTS = [
|
||||||
|
"-l:libstdc++.a",
|
||||||
|
"-l:libm.a",
|
||||||
|
]
|
||||||
|
|
||||||
cc_binary(
|
cc_binary(
|
||||||
name = "abort",
|
name = "abort",
|
||||||
|
|
|
@ -80,7 +80,6 @@ target_link_libraries(sandbox2_util_minielf PRIVATE
|
||||||
sandbox2::util
|
sandbox2::util
|
||||||
sapi::base
|
sapi::base
|
||||||
sapi::raw_logging
|
sapi::raw_logging
|
||||||
sapi::statusor
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# sandboxed_api/sandbox2/util:temp_file
|
# sandboxed_api/sandbox2/util:temp_file
|
||||||
|
@ -95,7 +94,7 @@ target_link_libraries(sandbox2_util_temp_file
|
||||||
sandbox2::strerror
|
sandbox2::strerror
|
||||||
sapi::base
|
sapi::base
|
||||||
PUBLIC absl::status
|
PUBLIC absl::status
|
||||||
sapi::statusor
|
absl::statusor
|
||||||
)
|
)
|
||||||
|
|
||||||
# sandboxed_api/sandbox2/util:maps_parser
|
# sandboxed_api/sandbox2/util:maps_parser
|
||||||
|
@ -106,9 +105,9 @@ add_library(sandbox2_util_maps_parser STATIC
|
||||||
add_library(sandbox2::maps_parser ALIAS sandbox2_util_maps_parser)
|
add_library(sandbox2::maps_parser ALIAS sandbox2_util_maps_parser)
|
||||||
target_link_libraries(sandbox2_util_maps_parser PRIVATE
|
target_link_libraries(sandbox2_util_maps_parser PRIVATE
|
||||||
absl::status
|
absl::status
|
||||||
|
absl::statusor
|
||||||
absl::strings
|
absl::strings
|
||||||
sapi::base
|
sapi::base
|
||||||
sapi::statusor
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# sandboxed_api/sandbox2/util:runfiles
|
# sandboxed_api/sandbox2/util:runfiles
|
||||||
|
|
|
@ -23,8 +23,8 @@
|
||||||
#include "sandboxed_api/sandbox2/testing.h"
|
#include "sandboxed_api/sandbox2/testing.h"
|
||||||
#include "sandboxed_api/sandbox2/util/path.h"
|
#include "sandboxed_api/sandbox2/util/path.h"
|
||||||
|
|
||||||
using testing::Gt;
|
using ::testing::Gt;
|
||||||
using testing::IsTrue;
|
using ::testing::IsTrue;
|
||||||
|
|
||||||
namespace sandbox2 {
|
namespace sandbox2 {
|
||||||
namespace util {
|
namespace util {
|
||||||
|
|
|
@ -45,20 +45,15 @@ cc_library(
|
||||||
cc_library(
|
cc_library(
|
||||||
name = "statusor",
|
name = "statusor",
|
||||||
hdrs = ["statusor.h"],
|
hdrs = ["statusor.h"],
|
||||||
copts = sapi_platform_copts(),
|
deprecation = "Migrate to `absl::StatusOr<T>`",
|
||||||
visibility = ["//visibility:public"],
|
|
||||||
deps = [
|
deps = [
|
||||||
":raw_logging",
|
|
||||||
":status",
|
|
||||||
"@com_google_absl//absl/base:core_headers",
|
"@com_google_absl//absl/base:core_headers",
|
||||||
"@com_google_absl//absl/base:log_severity",
|
"@com_google_absl//absl/status:statusor",
|
||||||
"@com_google_absl//absl/status",
|
|
||||||
"@com_google_absl//absl/types:variant",
|
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
# gMock matchers for absl::Status and sapi::StatusOr<T> and a gUnit printer
|
# gMock matchers for absl::Status and absl::StatusOr<T> and a gUnit printer
|
||||||
# extension for sapi::StatusOr<T>.
|
# extension. Adapted from the version in Asylo.
|
||||||
cc_library(
|
cc_library(
|
||||||
name = "status_matchers",
|
name = "status_matchers",
|
||||||
testonly = 1,
|
testonly = 1,
|
||||||
|
@ -67,8 +62,8 @@ cc_library(
|
||||||
visibility = ["//visibility:public"],
|
visibility = ["//visibility:public"],
|
||||||
deps = [
|
deps = [
|
||||||
":status",
|
":status",
|
||||||
":statusor",
|
|
||||||
"@com_google_absl//absl/status",
|
"@com_google_absl//absl/status",
|
||||||
|
"@com_google_absl//absl/status:statusor",
|
||||||
"@com_google_absl//absl/types:optional",
|
"@com_google_absl//absl/types:optional",
|
||||||
"@com_google_googletest//:gtest",
|
"@com_google_googletest//:gtest",
|
||||||
],
|
],
|
||||||
|
@ -87,19 +82,6 @@ cc_test(
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
# Tests for the StatusOr template class.
|
|
||||||
cc_test(
|
|
||||||
name = "statusor_test",
|
|
||||||
srcs = ["statusor_test.cc"],
|
|
||||||
copts = sapi_platform_copts(),
|
|
||||||
deps = [
|
|
||||||
":status",
|
|
||||||
":status_matchers",
|
|
||||||
":statusor",
|
|
||||||
"@com_google_googletest//:gtest_main",
|
|
||||||
],
|
|
||||||
)
|
|
||||||
|
|
||||||
# Tests for the Status macros.
|
# Tests for the Status macros.
|
||||||
cc_test(
|
cc_test(
|
||||||
name = "status_macros_test",
|
name = "status_macros_test",
|
||||||
|
@ -108,9 +90,9 @@ cc_test(
|
||||||
deps = [
|
deps = [
|
||||||
":status",
|
":status",
|
||||||
":status_matchers",
|
":status_matchers",
|
||||||
":statusor",
|
|
||||||
"@com_google_absl//absl/memory",
|
"@com_google_absl//absl/memory",
|
||||||
"@com_google_absl//absl/status",
|
"@com_google_absl//absl/status",
|
||||||
|
"@com_google_absl//absl/status:statusor",
|
||||||
"@com_google_absl//absl/strings",
|
"@com_google_absl//absl/strings",
|
||||||
"@com_google_googletest//:gtest_main",
|
"@com_google_googletest//:gtest_main",
|
||||||
],
|
],
|
||||||
|
|
|
@ -48,11 +48,10 @@ add_library(sapi_util_statusor STATIC
|
||||||
)
|
)
|
||||||
add_library(sapi::statusor ALIAS sapi_util_statusor)
|
add_library(sapi::statusor ALIAS sapi_util_statusor)
|
||||||
target_link_libraries(sapi_util_statusor PRIVATE
|
target_link_libraries(sapi_util_statusor PRIVATE
|
||||||
absl::base
|
|
||||||
absl::core_headers
|
absl::core_headers
|
||||||
absl::variant
|
absl::status
|
||||||
sapi::raw_logging
|
absl::statusor
|
||||||
sapi::status
|
sapi::base
|
||||||
)
|
)
|
||||||
|
|
||||||
# sandboxed_api/util:flag
|
# sandboxed_api/util:flag
|
||||||
|
@ -90,7 +89,6 @@ if(SAPI_ENABLE_TESTS)
|
||||||
sapi::base
|
sapi::base
|
||||||
PUBLIC absl::status
|
PUBLIC absl::status
|
||||||
sapi::status
|
sapi::status
|
||||||
sapi::statusor
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# sandboxed_api/util:status_test
|
# sandboxed_api/util:status_test
|
||||||
|
@ -98,20 +96,24 @@ if(SAPI_ENABLE_TESTS)
|
||||||
status_test.cc
|
status_test.cc
|
||||||
)
|
)
|
||||||
target_link_libraries(status_test PRIVATE
|
target_link_libraries(status_test PRIVATE
|
||||||
|
sapi::status
|
||||||
sapi::status_matchers
|
sapi::status_matchers
|
||||||
sapi::test_main
|
sapi::test_main
|
||||||
|
absl::status
|
||||||
absl::type_traits
|
absl::type_traits
|
||||||
)
|
)
|
||||||
gtest_discover_tests(status_test)
|
gtest_discover_tests(status_test)
|
||||||
|
|
||||||
# sandboxed_api/util:statusor_test
|
# sandboxed_api/util:status_macros_test
|
||||||
add_executable(statusor_test
|
add_executable(status_macros_test
|
||||||
statusor_test.cc
|
status_macros_test.cc
|
||||||
)
|
)
|
||||||
target_link_libraries(statusor_test PRIVATE
|
target_link_libraries(status_macros_test PRIVATE
|
||||||
sapi::status_matchers
|
sapi::status_matchers
|
||||||
sapi::test_main
|
sapi::test_main
|
||||||
|
absl::status
|
||||||
|
absl::statusor
|
||||||
absl::type_traits
|
absl::type_traits
|
||||||
)
|
)
|
||||||
gtest_discover_tests(statusor_test)
|
gtest_discover_tests(status_macros_test)
|
||||||
endif()
|
endif()
|
||||||
|
|
|
@ -42,6 +42,6 @@
|
||||||
if (ABSL_PREDICT_FALSE(!statusor.ok())) { \
|
if (ABSL_PREDICT_FALSE(!statusor.ok())) { \
|
||||||
return statusor.status(); \
|
return statusor.status(); \
|
||||||
} \
|
} \
|
||||||
lhs = std::move(statusor).ValueOrDie();
|
lhs = std::move(statusor).value();
|
||||||
|
|
||||||
#endif // THIRD_PARTY_SAPI_UTIL_STATUS_MACROS_H_
|
#endif // THIRD_PARTY_SAPI_UTIL_STATUS_MACROS_H_
|
||||||
|
|
|
@ -20,10 +20,10 @@
|
||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
#include "absl/memory/memory.h"
|
#include "absl/memory/memory.h"
|
||||||
#include "absl/status/status.h"
|
#include "absl/status/status.h"
|
||||||
|
#include "absl/status/statusor.h"
|
||||||
#include "absl/strings/str_cat.h"
|
#include "absl/strings/str_cat.h"
|
||||||
#include "sandboxed_api/util/status.h"
|
#include "sandboxed_api/util/status.h"
|
||||||
#include "sandboxed_api/util/status_matchers.h"
|
#include "sandboxed_api/util/status_matchers.h"
|
||||||
#include "sandboxed_api/util/statusor.h"
|
|
||||||
|
|
||||||
namespace sapi {
|
namespace sapi {
|
||||||
namespace {
|
namespace {
|
||||||
|
@ -52,17 +52,17 @@ TEST(ReturnIfError, ReturnsOnErrorFromLambda) {
|
||||||
TEST(AssignOrReturn, AssignsMultipleVariablesInSequence) {
|
TEST(AssignOrReturn, AssignsMultipleVariablesInSequence) {
|
||||||
auto func = []() -> absl::Status {
|
auto func = []() -> absl::Status {
|
||||||
int value1;
|
int value1;
|
||||||
SAPI_ASSIGN_OR_RETURN(value1, StatusOr<int>(1));
|
SAPI_ASSIGN_OR_RETURN(value1, absl::StatusOr<int>(1));
|
||||||
EXPECT_EQ(1, value1);
|
EXPECT_EQ(1, value1);
|
||||||
int value2;
|
int value2;
|
||||||
SAPI_ASSIGN_OR_RETURN(value2, StatusOr<int>(2));
|
SAPI_ASSIGN_OR_RETURN(value2, absl::StatusOr<int>(2));
|
||||||
EXPECT_EQ(2, value2);
|
EXPECT_EQ(2, value2);
|
||||||
int value3;
|
int value3;
|
||||||
SAPI_ASSIGN_OR_RETURN(value3, StatusOr<int>(3));
|
SAPI_ASSIGN_OR_RETURN(value3, absl::StatusOr<int>(3));
|
||||||
EXPECT_EQ(3, value3);
|
EXPECT_EQ(3, value3);
|
||||||
int value4;
|
int value4;
|
||||||
SAPI_ASSIGN_OR_RETURN(value4,
|
SAPI_ASSIGN_OR_RETURN(value4,
|
||||||
StatusOr<int>(absl::UnknownError("EXPECTED")));
|
absl::StatusOr<int>(absl::UnknownError("EXPECTED")));
|
||||||
return absl::UnknownError(absl::StrCat("ERROR: assigned value ", value4));
|
return absl::UnknownError(absl::StrCat("ERROR: assigned value ", value4));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -72,11 +72,12 @@ TEST(AssignOrReturn, AssignsMultipleVariablesInSequence) {
|
||||||
TEST(AssignOrReturn, AssignsRepeatedlyToSingleVariable) {
|
TEST(AssignOrReturn, AssignsRepeatedlyToSingleVariable) {
|
||||||
auto func = []() -> absl::Status {
|
auto func = []() -> absl::Status {
|
||||||
int value = 1;
|
int value = 1;
|
||||||
SAPI_ASSIGN_OR_RETURN(value, StatusOr<int>(2));
|
SAPI_ASSIGN_OR_RETURN(value, absl::StatusOr<int>(2));
|
||||||
EXPECT_EQ(2, value);
|
EXPECT_EQ(2, value);
|
||||||
SAPI_ASSIGN_OR_RETURN(value, StatusOr<int>(3));
|
SAPI_ASSIGN_OR_RETURN(value, absl::StatusOr<int>(3));
|
||||||
EXPECT_EQ(3, value);
|
EXPECT_EQ(3, value);
|
||||||
SAPI_ASSIGN_OR_RETURN(value, StatusOr<int>(absl::UnknownError("EXPECTED")));
|
SAPI_ASSIGN_OR_RETURN(value,
|
||||||
|
absl::StatusOr<int>(absl::UnknownError("EXPECTED")));
|
||||||
return absl::UnknownError("ERROR");
|
return absl::UnknownError("ERROR");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -87,7 +88,7 @@ TEST(AssignOrReturn, MovesUniquePtr) {
|
||||||
auto func = []() -> absl::Status {
|
auto func = []() -> absl::Status {
|
||||||
std::unique_ptr<int> ptr;
|
std::unique_ptr<int> ptr;
|
||||||
SAPI_ASSIGN_OR_RETURN(
|
SAPI_ASSIGN_OR_RETURN(
|
||||||
ptr, StatusOr<std::unique_ptr<int>>(absl::make_unique<int>(1)));
|
ptr, absl::StatusOr<std::unique_ptr<int>>(absl::make_unique<int>(1)));
|
||||||
EXPECT_EQ(*ptr, 1);
|
EXPECT_EQ(*ptr, 1);
|
||||||
return absl::UnknownError("EXPECTED");
|
return absl::UnknownError("EXPECTED");
|
||||||
};
|
};
|
||||||
|
@ -98,8 +99,8 @@ TEST(AssignOrReturn, MovesUniquePtr) {
|
||||||
TEST(AssignOrReturn, DoesNotAssignUniquePtrOnErrorStatus) {
|
TEST(AssignOrReturn, DoesNotAssignUniquePtrOnErrorStatus) {
|
||||||
auto func = []() -> absl::Status {
|
auto func = []() -> absl::Status {
|
||||||
std::unique_ptr<int> ptr;
|
std::unique_ptr<int> ptr;
|
||||||
SAPI_ASSIGN_OR_RETURN(
|
SAPI_ASSIGN_OR_RETURN(ptr, absl::StatusOr<std::unique_ptr<int>>(
|
||||||
ptr, StatusOr<std::unique_ptr<int>>(absl::UnknownError("EXPECTED")));
|
absl::UnknownError("EXPECTED")));
|
||||||
EXPECT_EQ(ptr, nullptr);
|
EXPECT_EQ(ptr, nullptr);
|
||||||
return absl::OkStatus();
|
return absl::OkStatus();
|
||||||
};
|
};
|
||||||
|
@ -111,10 +112,10 @@ TEST(AssignOrReturn, MovesUniquePtrRepeatedlyToSingleVariable) {
|
||||||
auto func = []() -> absl::Status {
|
auto func = []() -> absl::Status {
|
||||||
std::unique_ptr<int> ptr;
|
std::unique_ptr<int> ptr;
|
||||||
SAPI_ASSIGN_OR_RETURN(
|
SAPI_ASSIGN_OR_RETURN(
|
||||||
ptr, StatusOr<std::unique_ptr<int>>(absl::make_unique<int>(1)));
|
ptr, absl::StatusOr<std::unique_ptr<int>>(absl::make_unique<int>(1)));
|
||||||
EXPECT_EQ(*ptr, 1);
|
EXPECT_EQ(*ptr, 1);
|
||||||
SAPI_ASSIGN_OR_RETURN(
|
SAPI_ASSIGN_OR_RETURN(
|
||||||
ptr, StatusOr<std::unique_ptr<int>>(absl::make_unique<int>(2)));
|
ptr, absl::StatusOr<std::unique_ptr<int>>(absl::make_unique<int>(2)));
|
||||||
EXPECT_EQ(*ptr, 2);
|
EXPECT_EQ(*ptr, 2);
|
||||||
return absl::UnknownError("EXPECTED");
|
return absl::UnknownError("EXPECTED");
|
||||||
};
|
};
|
||||||
|
|
|
@ -19,9 +19,9 @@
|
||||||
|
|
||||||
#include "gmock/gmock.h"
|
#include "gmock/gmock.h"
|
||||||
#include "absl/status/status.h"
|
#include "absl/status/status.h"
|
||||||
|
#include "absl/status/statusor.h"
|
||||||
#include "absl/types/optional.h"
|
#include "absl/types/optional.h"
|
||||||
#include "sandboxed_api/util/status_macros.h"
|
#include "sandboxed_api/util/status_macros.h"
|
||||||
#include "sandboxed_api/util/statusor.h"
|
|
||||||
|
|
||||||
#define SAPI_ASSERT_OK_AND_ASSIGN(lhs, rexpr) \
|
#define SAPI_ASSERT_OK_AND_ASSIGN(lhs, rexpr) \
|
||||||
SAPI_ASSERT_OK_AND_ASSIGN_IMPL( \
|
SAPI_ASSERT_OK_AND_ASSIGN_IMPL( \
|
||||||
|
|
|
@ -12,220 +12,17 @@
|
||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
// This file and it's implementation provide a custom fork of
|
|
||||||
// util/task/statusor.h. This will become obsolete and will be replaced once
|
|
||||||
// Abseil releases absl::Status.
|
|
||||||
|
|
||||||
#ifndef THIRD_PARTY_SAPI_UTIL_STATUSOR_H_
|
#ifndef THIRD_PARTY_SAPI_UTIL_STATUSOR_H_
|
||||||
#define THIRD_PARTY_SAPI_UTIL_STATUSOR_H_
|
#define THIRD_PARTY_SAPI_UTIL_STATUSOR_H_
|
||||||
|
|
||||||
#include <initializer_list>
|
|
||||||
#include <utility>
|
|
||||||
|
|
||||||
#include "absl/base/internal/raw_logging.h"
|
|
||||||
#include "absl/base/attributes.h"
|
#include "absl/base/attributes.h"
|
||||||
#include "absl/base/log_severity.h"
|
#include "absl/status/statusor.h"
|
||||||
#include "absl/status/status.h"
|
|
||||||
#include "absl/types/variant.h"
|
|
||||||
#include "sandboxed_api/util/raw_logging.h"
|
|
||||||
|
|
||||||
namespace sapi {
|
namespace sapi {
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class ABSL_MUST_USE_RESULT StatusOr {
|
using StatusOr ABSL_DEPRECATED("Use absl::StatusOr instead") =
|
||||||
template <typename U>
|
absl::StatusOr<T>;
|
||||||
friend class StatusOr;
|
|
||||||
|
|
||||||
public:
|
|
||||||
using element_type = T;
|
|
||||||
|
|
||||||
explicit StatusOr() : variant_(absl::UnknownError("")) {}
|
|
||||||
|
|
||||||
StatusOr(const StatusOr&) = default;
|
|
||||||
StatusOr& operator=(const StatusOr&) = default;
|
|
||||||
|
|
||||||
StatusOr(StatusOr&&) = default;
|
|
||||||
StatusOr& operator=(StatusOr&&) = default;
|
|
||||||
|
|
||||||
// Not implemented:
|
|
||||||
// template <typename U> StatusOr(const StatusOr<U>& other)
|
|
||||||
// template <typename U> StatusOr(StatusOr<U>&& other)
|
|
||||||
|
|
||||||
template <typename U>
|
|
||||||
StatusOr& operator=(const StatusOr<U>& other) {
|
|
||||||
if (other.ok()) {
|
|
||||||
variant_ = other.value();
|
|
||||||
} else {
|
|
||||||
variant_ = other.status();
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename U>
|
|
||||||
StatusOr& operator=(StatusOr<U>&& other) {
|
|
||||||
if (other.ok()) {
|
|
||||||
variant_ = std::move(other).value();
|
|
||||||
} else {
|
|
||||||
variant_ = std::move(other).status();
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
StatusOr(const T& value) : variant_(value) {}
|
|
||||||
|
|
||||||
StatusOr(const absl::Status& status) : variant_(status) { EnsureNotOk(); }
|
|
||||||
|
|
||||||
// Not implemented:
|
|
||||||
// template <typename U = T> StatusOr& operator=(U&& value)
|
|
||||||
|
|
||||||
StatusOr(T&& value) : variant_(std::move(value)) {}
|
|
||||||
|
|
||||||
StatusOr(absl::Status&& value) : variant_(std::move(value)) {}
|
|
||||||
|
|
||||||
StatusOr& operator=(absl::Status&& status) {
|
|
||||||
variant_ = std::move(status);
|
|
||||||
EnsureNotOk();
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename... Args>
|
|
||||||
explicit StatusOr(absl::in_place_t, Args&&... args)
|
|
||||||
: StatusOr(T(std::forward<Args>(args)...)) {}
|
|
||||||
|
|
||||||
template <typename U, typename... Args>
|
|
||||||
explicit StatusOr(absl::in_place_t, std::initializer_list<U> ilist,
|
|
||||||
Args&&... args)
|
|
||||||
: StatusOr(ilist, U(std::forward<Args>(args)...)) {}
|
|
||||||
|
|
||||||
explicit operator bool() const { return ok(); }
|
|
||||||
|
|
||||||
ABSL_MUST_USE_RESULT bool ok() const {
|
|
||||||
return absl::holds_alternative<T>(variant_);
|
|
||||||
}
|
|
||||||
|
|
||||||
const absl::Status& status() const& {
|
|
||||||
static const auto* ok_status = new absl::Status();
|
|
||||||
return ok() ? *ok_status : absl::get<absl::Status>(variant_);
|
|
||||||
}
|
|
||||||
|
|
||||||
absl::Status status() && {
|
|
||||||
return ok() ? absl::OkStatus()
|
|
||||||
: std::move(absl::get<absl::Status>(variant_));
|
|
||||||
}
|
|
||||||
|
|
||||||
const T& value() const& {
|
|
||||||
EnsureOk();
|
|
||||||
return absl::get<T>(variant_);
|
|
||||||
}
|
|
||||||
|
|
||||||
T& value() & {
|
|
||||||
EnsureOk();
|
|
||||||
return absl::get<T>(variant_);
|
|
||||||
}
|
|
||||||
|
|
||||||
const T&& value() const&& {
|
|
||||||
EnsureOk();
|
|
||||||
return absl::get<T>(std::move(variant_));
|
|
||||||
}
|
|
||||||
|
|
||||||
T&& value() && {
|
|
||||||
EnsureOk();
|
|
||||||
return absl::get<T>(std::move(variant_));
|
|
||||||
}
|
|
||||||
|
|
||||||
const T& ValueOrDie() const& {
|
|
||||||
EnsureOk();
|
|
||||||
return absl::get<T>(variant_);
|
|
||||||
}
|
|
||||||
|
|
||||||
T& ValueOrDie() & {
|
|
||||||
EnsureOk();
|
|
||||||
return absl::get<T>(variant_);
|
|
||||||
}
|
|
||||||
|
|
||||||
T&& ValueOrDie() && {
|
|
||||||
EnsureOk();
|
|
||||||
return absl::get<T>(std::move(variant_));
|
|
||||||
}
|
|
||||||
|
|
||||||
const T& operator*() const& {
|
|
||||||
EnsureOk();
|
|
||||||
return absl::get<T>(variant_);
|
|
||||||
}
|
|
||||||
|
|
||||||
T& operator*() & {
|
|
||||||
EnsureOk();
|
|
||||||
return absl::get<T>(variant_);
|
|
||||||
}
|
|
||||||
|
|
||||||
const T&& operator*() const&& {
|
|
||||||
EnsureOk();
|
|
||||||
return absl::get<T>(std::move(variant_));
|
|
||||||
}
|
|
||||||
|
|
||||||
T&& operator*() && {
|
|
||||||
EnsureOk();
|
|
||||||
return absl::get<T>(std::move(variant_));
|
|
||||||
}
|
|
||||||
|
|
||||||
const T* operator->() const {
|
|
||||||
EnsureOk();
|
|
||||||
return &absl::get<T>(variant_);
|
|
||||||
}
|
|
||||||
|
|
||||||
T* operator->() {
|
|
||||||
EnsureOk();
|
|
||||||
return &absl::get<T>(variant_);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename U>
|
|
||||||
T value_or(U&& default_value) const& {
|
|
||||||
if (ok()) {
|
|
||||||
return absl::get<T>(variant_);
|
|
||||||
}
|
|
||||||
return std::forward<U>(default_value);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename U>
|
|
||||||
T value_or(U&& default_value) && {
|
|
||||||
if (ok()) {
|
|
||||||
return absl::get<T>(std::move(variant_));
|
|
||||||
}
|
|
||||||
return std::forward<U>(default_value);
|
|
||||||
}
|
|
||||||
|
|
||||||
void IgnoreError() const { /* no-op */
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename... Args>
|
|
||||||
T& emplace(Args&&... args) {
|
|
||||||
return variant_.template emplace<T>(std::forward<Args>(args)...);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename U, typename... Args>
|
|
||||||
T& emplace(std::initializer_list<U> ilist, Args&&... args) {
|
|
||||||
return variant_.template emplace<T>(ilist, std::forward<Args>(args)...);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
void EnsureOk() const {
|
|
||||||
if (!ok()) {
|
|
||||||
// GoogleTest needs this exact error message for death tests to work.
|
|
||||||
SAPI_RAW_LOG(FATAL,
|
|
||||||
"Attempting to fetch value instead of handling error %s",
|
|
||||||
status().message());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void EnsureNotOk() const {
|
|
||||||
if (ok()) {
|
|
||||||
SAPI_RAW_LOG(
|
|
||||||
FATAL,
|
|
||||||
"An OK status is not a valid constructor argument to StatusOr<T>");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
absl::variant<absl::Status, T> variant_;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace sapi
|
} // namespace sapi
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user