NetworkProxyTest: test more error conditions

PiperOrigin-RevId: 562708702
Change-Id: Ifedcb0eb2bc84396627a0b53828e1e10e4c562ad
This commit is contained in:
Wiktor Garbacz 2023-09-05 02:16:47 -07:00 committed by Copybara-Service
parent b088c01ab2
commit 02d770adcc
3 changed files with 45 additions and 1 deletions

View File

@ -1107,6 +1107,7 @@ cc_test(
"//sandboxed_api:testing",
"//sandboxed_api/sandbox2/network_proxy:testing",
"//sandboxed_api/util:status_matchers",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/time",
"@com_google_googletest//:gtest_main",

View File

@ -1205,7 +1205,8 @@ if(BUILD_TESTING AND SAPI_BUILD_TESTING)
OUTPUT_NAME network_proxy_test
)
target_link_libraries(sandbox2_network_proxy_test
PRIVATE absl::strings
PRIVATE absl::status
absl::strings
absl::time
sandbox2::sandbox2
sandbox2::network_proxy_testing

View File

@ -22,6 +22,7 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/status/status.h"
#include "absl/time/time.h"
#include "sandboxed_api/sandbox2/executor.h"
#include "sandboxed_api/sandbox2/network_proxy/testing.h"
@ -35,8 +36,49 @@ namespace sandbox2 {
namespace {
using ::sapi::GetTestSourcePath;
using ::sapi::StatusIs;
using ::testing::Eq;
TEST(NetworkProxyTest, NoDoublePolicy) {
PolicyBuilder builder;
builder.AddNetworkProxyHandlerPolicy().AddNetworkProxyPolicy();
EXPECT_THAT(builder.TryBuild(),
StatusIs(absl::StatusCode::kFailedPrecondition));
}
TEST(NetworkProxyTest, NoDoublePolicyHandler) {
PolicyBuilder builder;
builder.AddNetworkProxyPolicy().AddNetworkProxyHandlerPolicy();
EXPECT_THAT(builder.TryBuild(),
StatusIs(absl::StatusCode::kFailedPrecondition));
}
TEST(NetworkProxyTest, NoNetworkPolicyIpv4) {
PolicyBuilder builder;
builder.AllowIPv4("127.0.0.1");
EXPECT_THAT(builder.TryBuild(),
StatusIs(absl::StatusCode::kFailedPrecondition));
}
TEST(NetworkProxyTest, NoNetworkPolicyIpv6) {
PolicyBuilder builder;
builder.AllowIPv6("::1");
EXPECT_THAT(builder.TryBuild(),
StatusIs(absl::StatusCode::kFailedPrecondition));
}
TEST(NetworkProxyTest, WrongIPv4) {
PolicyBuilder builder;
builder.AddNetworkProxyPolicy().AllowIPv4("256.256.256.256");
EXPECT_THAT(builder.TryBuild(), StatusIs(absl::StatusCode::kInvalidArgument));
}
TEST(NetworkProxyTest, WrongIPv6) {
PolicyBuilder builder;
builder.AddNetworkProxyPolicy().AllowIPv6("127.0.0.1");
EXPECT_THAT(builder.TryBuild(), StatusIs(absl::StatusCode::kInvalidArgument));
}
using NetworkProxyTest = ::testing::TestWithParam<bool>;
TEST_P(NetworkProxyTest, ProxyWithHandlerAllowed) {