1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <linux/futex.h>
16 #include <uv.h>
17
18 #include "gtest/gtest.h"
19 #include "absl/flags/flag.h"
20 #include "sandboxed_api/util/status_matchers.h"
21 #include "uv_sapi.sapi.h" // NOLINT(build/include)
22
23 namespace {
24
25 class UVTestErrorSapiSandbox : public uv::UVSandbox {
26 private:
ModifyPolicy(sandbox2::PolicyBuilder *)27 std::unique_ptr<sandbox2::Policy> ModifyPolicy(
28 sandbox2::PolicyBuilder*) override {
29 return sandbox2::PolicyBuilder()
30 .AllowDynamicStartup()
31 .AllowExit()
32 .AllowFutexOp(FUTEX_WAKE_PRIVATE)
33 .AllowWrite()
34 .BuildOrDie();
35 }
36 };
37
38 class UVTestError : public ::testing::Test {
39 protected:
SetUp()40 void SetUp() override {
41 sandbox_ = std::make_unique<UVTestErrorSapiSandbox>();
42 ASSERT_THAT(sandbox_->Init(), sapi::IsOk());
43 api_ = std::make_unique<uv::UVApi>(sandbox_.get());
44 }
45
46 // Check sapi_uv_strerror on error
UVStrerror(int error)47 void UVStrerror(int error) {
48 // Call sapi_uv_strerror
49 absl::StatusOr<void*> error_message_ptr = api_->sapi_uv_strerror(error);
50 ASSERT_THAT(error_message_ptr, sapi::IsOk());
51
52 // Get error message from the sandboxee
53 SAPI_ASSERT_OK_AND_ASSIGN(
54 std::string error_message,
55 sandbox_->GetCString(sapi::v::RemotePtr{error_message_ptr.value()}));
56
57 // Check that it is equal to expected error message
58 ASSERT_EQ(error_message, std::string{uv_strerror(error)});
59 }
60
61 // Check sapi_uv_translate_sys_error on error
UVTranslateSysError(int error)62 void UVTranslateSysError(int error) {
63 // Call sapi_uv_translate_sys_error and get error code
64 SAPI_ASSERT_OK_AND_ASSIGN(int error_code,
65 api_->sapi_uv_translate_sys_error(error));
66
67 // Check that it is equal to expected error code
68 ASSERT_EQ(error_code, uv_translate_sys_error(error));
69 }
70
71 std::unique_ptr<UVTestErrorSapiSandbox> sandbox_;
72 std::unique_ptr<uv::UVApi> api_;
73 };
74
TEST_F(UVTestError,ErrorMessage)75 TEST_F(UVTestError, ErrorMessage) {
76 // Test sapi_uv_strerror method
77 UVStrerror(0);
78 UVStrerror(UV_EINVAL);
79 UVStrerror(1337);
80 UVStrerror(-1337);
81 }
82
TEST_F(UVTestError,SystemError)83 TEST_F(UVTestError, SystemError) {
84 // Test sapi_uv_translate_sys_error method
85 UVTranslateSysError(EPERM);
86 UVTranslateSysError(EPIPE);
87 UVTranslateSysError(EINVAL);
88 UVTranslateSysError(UV_EINVAL);
89 UVTranslateSysError(UV_ERANGE);
90 UVTranslateSysError(UV_EACCES);
91 UVTranslateSysError(0);
92 }
93
94 } // namespace
95