2020-09-22 22:41:06 +08:00
|
|
|
// Copyright 2020 Google LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
2022-01-28 17:38:27 +08:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2020-09-22 22:41:06 +08:00
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include <linux/futex.h>
|
|
|
|
#include <syscall.h>
|
|
|
|
#include <uv.h>
|
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
2022-10-20 21:48:06 +08:00
|
|
|
#include "absl/flags/flag.h"
|
2020-09-22 22:41:06 +08:00
|
|
|
#include "sandboxed_api/util/status_matchers.h"
|
2020-11-19 22:32:44 +08:00
|
|
|
#include "uv_sapi.sapi.h" // NOLINT(build/include)
|
2020-09-22 22:41:06 +08:00
|
|
|
|
2020-09-25 23:06:24 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class UVTestOSSapiSandbox : public uv::UVSandbox {
|
2020-09-22 22:41:06 +08:00
|
|
|
private:
|
|
|
|
std::unique_ptr<sandbox2::Policy> ModifyPolicy(
|
|
|
|
sandbox2::PolicyBuilder*) override {
|
|
|
|
return sandbox2::PolicyBuilder()
|
|
|
|
.AllowDynamicStartup()
|
|
|
|
.AllowExit()
|
|
|
|
.AllowFutexOp(FUTEX_WAKE_PRIVATE)
|
|
|
|
.AllowGetIDs()
|
2023-12-27 18:50:16 +08:00
|
|
|
.AllowMmapWithoutExec()
|
2020-09-22 22:41:06 +08:00
|
|
|
.AllowOpen()
|
|
|
|
.AllowWrite()
|
|
|
|
.AllowSyscalls({__NR_connect, __NR_socket})
|
|
|
|
.DisableNamespaces()
|
|
|
|
.BuildOrDie();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class UVTestOS : public ::testing::Test {
|
|
|
|
protected:
|
|
|
|
void SetUp() override {
|
|
|
|
sandbox_ = std::make_unique<UVTestOSSapiSandbox>();
|
2020-09-25 23:06:24 +08:00
|
|
|
ASSERT_THAT(sandbox_->Init(), sapi::IsOk());
|
|
|
|
api_ = std::make_unique<uv::UVApi>(sandbox_.get());
|
2020-09-22 22:41:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<UVTestOSSapiSandbox> sandbox_;
|
2020-09-25 23:06:24 +08:00
|
|
|
std::unique_ptr<uv::UVApi> api_;
|
2020-09-22 22:41:06 +08:00
|
|
|
|
|
|
|
static constexpr size_t kBigBufLen = 4096;
|
|
|
|
static constexpr size_t kSmallBufLen = 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(UVTestOS, HomeDirBig) {
|
|
|
|
// Get expected home directory
|
|
|
|
char expected_homedir[kBigBufLen];
|
|
|
|
size_t len = sizeof(expected_homedir);
|
|
|
|
ASSERT_GE(uv_os_homedir(expected_homedir, &len), 0);
|
|
|
|
|
|
|
|
// Get home directory from the sandbox
|
|
|
|
sapi::v::Array<char> uv_homedir(kBigBufLen);
|
|
|
|
uv_homedir[0] = '\0';
|
|
|
|
sapi::v::IntBase<size_t> uv_homedir_len(kBigBufLen);
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(
|
|
|
|
int error_code,
|
|
|
|
api_->sapi_uv_os_homedir(uv_homedir.PtrBoth(), uv_homedir_len.PtrBoth()));
|
|
|
|
ASSERT_GE(error_code, 0);
|
|
|
|
|
|
|
|
// Test home directory is as expected
|
|
|
|
ASSERT_EQ(std::string{uv_homedir.GetData()}, std::string{expected_homedir});
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(UVTestOS, HomeDirSmall) {
|
|
|
|
// Try getting expected home directory, error because array is too small
|
|
|
|
char expected_homedir[kSmallBufLen];
|
|
|
|
size_t len = sizeof(expected_homedir);
|
|
|
|
int expected_error_code = uv_os_homedir(expected_homedir, &len);
|
|
|
|
ASSERT_NE(expected_error_code, 0);
|
|
|
|
|
|
|
|
// Try getting home directory from sandbox, error because array is too small
|
|
|
|
sapi::v::Array<char> uv_homedir(kSmallBufLen);
|
|
|
|
uv_homedir[0] = '\0';
|
|
|
|
sapi::v::IntBase<size_t> uv_homedir_len(kSmallBufLen);
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(
|
|
|
|
int error_code,
|
|
|
|
api_->sapi_uv_os_homedir(uv_homedir.PtrBoth(), uv_homedir_len.PtrBoth()));
|
|
|
|
ASSERT_NE(error_code, 0);
|
|
|
|
|
|
|
|
// Test error code is as expected
|
|
|
|
ASSERT_EQ(error_code, expected_error_code);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(UVTestOS, TmpDirBig) {
|
|
|
|
// Get expected tmp directory
|
|
|
|
char expected_tmpdir[kBigBufLen];
|
|
|
|
size_t len = sizeof(expected_tmpdir);
|
|
|
|
ASSERT_GE(uv_os_tmpdir(expected_tmpdir, &len), 0);
|
|
|
|
|
|
|
|
// Get tmp directory from the sandbox
|
|
|
|
sapi::v::Array<char> uv_tmpdir(kBigBufLen);
|
|
|
|
uv_tmpdir[0] = '\0';
|
|
|
|
sapi::v::IntBase<size_t> uv_tmpdir_len(kBigBufLen);
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(
|
|
|
|
int error_code,
|
|
|
|
api_->sapi_uv_os_tmpdir(uv_tmpdir.PtrBoth(), uv_tmpdir_len.PtrBoth()));
|
|
|
|
ASSERT_GE(error_code, 0);
|
|
|
|
|
|
|
|
// Test tmp directory is as expected
|
|
|
|
ASSERT_EQ(std::string{uv_tmpdir.GetData()}, std::string{expected_tmpdir});
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(UVTestOS, TmpDirSmall) {
|
|
|
|
// Try getting expected tmp directory, error because array is too small
|
|
|
|
char expected_tmpdir[kSmallBufLen];
|
|
|
|
size_t len = sizeof(expected_tmpdir);
|
|
|
|
int expected_error_code = uv_os_tmpdir(expected_tmpdir, &len);
|
|
|
|
ASSERT_NE(expected_error_code, 0);
|
|
|
|
|
|
|
|
// Try getting tmp directory from sandbox, error because array is too small
|
|
|
|
sapi::v::Array<char> uv_tmpdir(kSmallBufLen);
|
|
|
|
uv_tmpdir[0] = '\0';
|
|
|
|
sapi::v::IntBase<size_t> uv_tmpdir_len(kSmallBufLen);
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(
|
|
|
|
int error_code,
|
|
|
|
api_->sapi_uv_os_tmpdir(uv_tmpdir.PtrBoth(), uv_tmpdir_len.PtrBoth()));
|
|
|
|
ASSERT_NE(error_code, 0);
|
|
|
|
|
|
|
|
// Test error code is as expected
|
|
|
|
ASSERT_EQ(error_code, expected_error_code);
|
|
|
|
}
|
2020-09-25 23:06:24 +08:00
|
|
|
|
|
|
|
} // namespace
|