xref: /aosp_15_r20/external/sandboxed-api/oss-internship-2020/libuv/tests/test_callback.cc (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
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 <syscall.h>
17 #include <uv.h>
18 
19 #include "gtest/gtest.h"
20 #include "absl/flags/flag.h"
21 #include "sandboxed_api/util/status_matchers.h"
22 #include "uv_sapi.sapi.h"  // NOLINT(build/include)
23 
24 namespace {
25 
26 class UVTestCallbackSapiSandbox : public uv::UVSandbox {
27  private:
ModifyPolicy(sandbox2::PolicyBuilder *)28   std::unique_ptr<sandbox2::Policy> ModifyPolicy(
29       sandbox2::PolicyBuilder*) override {
30     return sandbox2::PolicyBuilder()
31         .AllowDynamicStartup()
32         .AllowExit()
33         .AllowFutexOp(FUTEX_WAKE_PRIVATE)
34         .AllowSyscalls({__NR_epoll_create1, __NR_eventfd2, __NR_pipe2})
35         .AllowWrite()
36         .BuildOrDie();
37   }
38 };
39 
40 class UVTestCallback : public ::testing::Test {
41  protected:
SetUp()42   void SetUp() override {
43     sandbox_ = std::make_unique<UVTestCallbackSapiSandbox>();
44     ASSERT_THAT(sandbox_->Init(), sapi::IsOk());
45     api_ = std::make_unique<uv::UVApi>(sandbox_.get());
46   }
47 
48   // Check sapi_uv_timer_init
UVTimerInit(sapi::v::Ptr * loop,sapi::v::Ptr * timer)49   void UVTimerInit(sapi::v::Ptr* loop, sapi::v::Ptr* timer) {
50     SAPI_ASSERT_OK_AND_ASSIGN(int error_code,
51                               api_->sapi_uv_timer_init(loop, timer));
52     ASSERT_EQ(error_code, 0);
53   }
54 
55   // Check sapi_uv_timer_start
56   // (actual time is ignored because timeout and repeat are 0)
UVTimerStart(sapi::v::Ptr * timer)57   void UVTimerStart(sapi::v::Ptr* timer) {
58     // Get the TimerCallback callback from the sandbox
59     void* timer_cb_voidptr;
60     ASSERT_THAT(
61         sandbox_->rpc_channel()->Symbol("TimerCallback", &timer_cb_voidptr),
62         sapi::IsOk());
63     sapi::v::RemotePtr timer_cb(timer_cb_voidptr);
64 
65     // Set the timer's callback, timeout and repeat
66     SAPI_ASSERT_OK_AND_ASSIGN(
67         int error_code, api_->sapi_uv_timer_start(timer, &timer_cb, 0, 0));
68     ASSERT_EQ(error_code, 0);
69   }
70 
71   // Check sapi_uv_run
UVRun(sapi::v::Ptr * loop)72   void UVRun(sapi::v::Ptr* loop) {
73     SAPI_ASSERT_OK_AND_ASSIGN(int error_code,
74                               api_->sapi_uv_run(loop, UV_RUN_DEFAULT));
75     ASSERT_EQ(error_code, 0);
76   }
77 
78   // Check sapi_uv_loop_close
UVLoopClose(sapi::v::Ptr * loop)79   void UVLoopClose(sapi::v::Ptr* loop) {
80     SAPI_ASSERT_OK_AND_ASSIGN(int error_code, api_->sapi_uv_loop_close(loop));
81     ASSERT_EQ(error_code, 0);
82   }
83 
84   // Check sapi_uv_default_loop, set loop to default loop
UVDefaultLoop(sapi::v::Ptr * loop)85   void UVDefaultLoop(sapi::v::Ptr* loop) {
86     SAPI_ASSERT_OK_AND_ASSIGN(void* loop_voidptr, api_->sapi_uv_default_loop());
87     loop->SetRemote(loop_voidptr);
88   }
89 
90   std::unique_ptr<UVTestCallbackSapiSandbox> sandbox_;
91   std::unique_ptr<uv::UVApi> api_;
92 
93   static constexpr int kData = 1729;
94 };
95 
TEST_F(UVTestCallback,TimerCallback)96 TEST_F(UVTestCallback, TimerCallback) {
97   // Initialize loop
98   sapi::v::RemotePtr loop(nullptr);
99 
100   // Allocate memory for timer
101   void* timer_voidptr;
102   ASSERT_THAT(
103       sandbox_->rpc_channel()->Allocate(sizeof(uv_timer_t), &timer_voidptr),
104       sapi::IsOk());
105   sapi::v::RemotePtr timer(timer_voidptr);
106 
107   // Initialize timer and add it to default loop
108   UVDefaultLoop(&loop);
109   UVTimerInit(loop.PtrNone(), timer.PtrBoth());
110 
111   // Set timer data to kData
112   sapi::v::Int data(kData);
113   void* data_voidptr;
114   ASSERT_THAT(sandbox_->rpc_channel()->Allocate(sizeof(int), &data_voidptr),
115               sapi::IsOk());
116   data.SetRemote(data_voidptr);
117   ASSERT_THAT(api_->sapi_uv_handle_set_data(timer.PtrBoth(), data.PtrBefore()),
118               sapi::IsOk());
119 
120   // Start the timer
121   UVTimerStart(timer.PtrBoth());
122 
123   // Check that data has not changed (because the loop is not running yet)
124   // This is done by resetting the local value and then getting the remote one
125   data.SetValue(0);
126   ASSERT_THAT(sandbox_->TransferFromSandboxee(&data), sapi::IsOk());
127   ASSERT_EQ(data.GetValue(), kData);
128 
129   // Run the loop
130   UVDefaultLoop(&loop);
131   UVRun(loop.PtrNone());
132 
133   // Check that data has changed (and therefore callback was called correctly)
134   ASSERT_THAT(sandbox_->TransferFromSandboxee(&data), sapi::IsOk());
135   ASSERT_EQ(data.GetValue(), kData + 1);
136 
137   // Close the loop
138   UVDefaultLoop(&loop);
139   UVLoopClose(loop.PtrNone());
140 }
141 
142 }  // namespace
143