xref: /aosp_15_r20/external/sandboxed-api/oss-internship-2020/libuv/tests/test_callback.cc (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1*ec63e07aSXin Li // Copyright 2020 Google LLC
2*ec63e07aSXin Li //
3*ec63e07aSXin Li // Licensed under the Apache License, Version 2.0 (the "License");
4*ec63e07aSXin Li // you may not use this file except in compliance with the License.
5*ec63e07aSXin Li // You may obtain a copy of the License at
6*ec63e07aSXin Li //
7*ec63e07aSXin Li //     https://www.apache.org/licenses/LICENSE-2.0
8*ec63e07aSXin Li //
9*ec63e07aSXin Li // Unless required by applicable law or agreed to in writing, software
10*ec63e07aSXin Li // distributed under the License is distributed on an "AS IS" BASIS,
11*ec63e07aSXin Li // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*ec63e07aSXin Li // See the License for the specific language governing permissions and
13*ec63e07aSXin Li // limitations under the License.
14*ec63e07aSXin Li 
15*ec63e07aSXin Li #include <linux/futex.h>
16*ec63e07aSXin Li #include <syscall.h>
17*ec63e07aSXin Li #include <uv.h>
18*ec63e07aSXin Li 
19*ec63e07aSXin Li #include "gtest/gtest.h"
20*ec63e07aSXin Li #include "absl/flags/flag.h"
21*ec63e07aSXin Li #include "sandboxed_api/util/status_matchers.h"
22*ec63e07aSXin Li #include "uv_sapi.sapi.h"  // NOLINT(build/include)
23*ec63e07aSXin Li 
24*ec63e07aSXin Li namespace {
25*ec63e07aSXin Li 
26*ec63e07aSXin Li class UVTestCallbackSapiSandbox : public uv::UVSandbox {
27*ec63e07aSXin Li  private:
ModifyPolicy(sandbox2::PolicyBuilder *)28*ec63e07aSXin Li   std::unique_ptr<sandbox2::Policy> ModifyPolicy(
29*ec63e07aSXin Li       sandbox2::PolicyBuilder*) override {
30*ec63e07aSXin Li     return sandbox2::PolicyBuilder()
31*ec63e07aSXin Li         .AllowDynamicStartup()
32*ec63e07aSXin Li         .AllowExit()
33*ec63e07aSXin Li         .AllowFutexOp(FUTEX_WAKE_PRIVATE)
34*ec63e07aSXin Li         .AllowSyscalls({__NR_epoll_create1, __NR_eventfd2, __NR_pipe2})
35*ec63e07aSXin Li         .AllowWrite()
36*ec63e07aSXin Li         .BuildOrDie();
37*ec63e07aSXin Li   }
38*ec63e07aSXin Li };
39*ec63e07aSXin Li 
40*ec63e07aSXin Li class UVTestCallback : public ::testing::Test {
41*ec63e07aSXin Li  protected:
SetUp()42*ec63e07aSXin Li   void SetUp() override {
43*ec63e07aSXin Li     sandbox_ = std::make_unique<UVTestCallbackSapiSandbox>();
44*ec63e07aSXin Li     ASSERT_THAT(sandbox_->Init(), sapi::IsOk());
45*ec63e07aSXin Li     api_ = std::make_unique<uv::UVApi>(sandbox_.get());
46*ec63e07aSXin Li   }
47*ec63e07aSXin Li 
48*ec63e07aSXin Li   // Check sapi_uv_timer_init
UVTimerInit(sapi::v::Ptr * loop,sapi::v::Ptr * timer)49*ec63e07aSXin Li   void UVTimerInit(sapi::v::Ptr* loop, sapi::v::Ptr* timer) {
50*ec63e07aSXin Li     SAPI_ASSERT_OK_AND_ASSIGN(int error_code,
51*ec63e07aSXin Li                               api_->sapi_uv_timer_init(loop, timer));
52*ec63e07aSXin Li     ASSERT_EQ(error_code, 0);
53*ec63e07aSXin Li   }
54*ec63e07aSXin Li 
55*ec63e07aSXin Li   // Check sapi_uv_timer_start
56*ec63e07aSXin Li   // (actual time is ignored because timeout and repeat are 0)
UVTimerStart(sapi::v::Ptr * timer)57*ec63e07aSXin Li   void UVTimerStart(sapi::v::Ptr* timer) {
58*ec63e07aSXin Li     // Get the TimerCallback callback from the sandbox
59*ec63e07aSXin Li     void* timer_cb_voidptr;
60*ec63e07aSXin Li     ASSERT_THAT(
61*ec63e07aSXin Li         sandbox_->rpc_channel()->Symbol("TimerCallback", &timer_cb_voidptr),
62*ec63e07aSXin Li         sapi::IsOk());
63*ec63e07aSXin Li     sapi::v::RemotePtr timer_cb(timer_cb_voidptr);
64*ec63e07aSXin Li 
65*ec63e07aSXin Li     // Set the timer's callback, timeout and repeat
66*ec63e07aSXin Li     SAPI_ASSERT_OK_AND_ASSIGN(
67*ec63e07aSXin Li         int error_code, api_->sapi_uv_timer_start(timer, &timer_cb, 0, 0));
68*ec63e07aSXin Li     ASSERT_EQ(error_code, 0);
69*ec63e07aSXin Li   }
70*ec63e07aSXin Li 
71*ec63e07aSXin Li   // Check sapi_uv_run
UVRun(sapi::v::Ptr * loop)72*ec63e07aSXin Li   void UVRun(sapi::v::Ptr* loop) {
73*ec63e07aSXin Li     SAPI_ASSERT_OK_AND_ASSIGN(int error_code,
74*ec63e07aSXin Li                               api_->sapi_uv_run(loop, UV_RUN_DEFAULT));
75*ec63e07aSXin Li     ASSERT_EQ(error_code, 0);
76*ec63e07aSXin Li   }
77*ec63e07aSXin Li 
78*ec63e07aSXin Li   // Check sapi_uv_loop_close
UVLoopClose(sapi::v::Ptr * loop)79*ec63e07aSXin Li   void UVLoopClose(sapi::v::Ptr* loop) {
80*ec63e07aSXin Li     SAPI_ASSERT_OK_AND_ASSIGN(int error_code, api_->sapi_uv_loop_close(loop));
81*ec63e07aSXin Li     ASSERT_EQ(error_code, 0);
82*ec63e07aSXin Li   }
83*ec63e07aSXin Li 
84*ec63e07aSXin Li   // Check sapi_uv_default_loop, set loop to default loop
UVDefaultLoop(sapi::v::Ptr * loop)85*ec63e07aSXin Li   void UVDefaultLoop(sapi::v::Ptr* loop) {
86*ec63e07aSXin Li     SAPI_ASSERT_OK_AND_ASSIGN(void* loop_voidptr, api_->sapi_uv_default_loop());
87*ec63e07aSXin Li     loop->SetRemote(loop_voidptr);
88*ec63e07aSXin Li   }
89*ec63e07aSXin Li 
90*ec63e07aSXin Li   std::unique_ptr<UVTestCallbackSapiSandbox> sandbox_;
91*ec63e07aSXin Li   std::unique_ptr<uv::UVApi> api_;
92*ec63e07aSXin Li 
93*ec63e07aSXin Li   static constexpr int kData = 1729;
94*ec63e07aSXin Li };
95*ec63e07aSXin Li 
TEST_F(UVTestCallback,TimerCallback)96*ec63e07aSXin Li TEST_F(UVTestCallback, TimerCallback) {
97*ec63e07aSXin Li   // Initialize loop
98*ec63e07aSXin Li   sapi::v::RemotePtr loop(nullptr);
99*ec63e07aSXin Li 
100*ec63e07aSXin Li   // Allocate memory for timer
101*ec63e07aSXin Li   void* timer_voidptr;
102*ec63e07aSXin Li   ASSERT_THAT(
103*ec63e07aSXin Li       sandbox_->rpc_channel()->Allocate(sizeof(uv_timer_t), &timer_voidptr),
104*ec63e07aSXin Li       sapi::IsOk());
105*ec63e07aSXin Li   sapi::v::RemotePtr timer(timer_voidptr);
106*ec63e07aSXin Li 
107*ec63e07aSXin Li   // Initialize timer and add it to default loop
108*ec63e07aSXin Li   UVDefaultLoop(&loop);
109*ec63e07aSXin Li   UVTimerInit(loop.PtrNone(), timer.PtrBoth());
110*ec63e07aSXin Li 
111*ec63e07aSXin Li   // Set timer data to kData
112*ec63e07aSXin Li   sapi::v::Int data(kData);
113*ec63e07aSXin Li   void* data_voidptr;
114*ec63e07aSXin Li   ASSERT_THAT(sandbox_->rpc_channel()->Allocate(sizeof(int), &data_voidptr),
115*ec63e07aSXin Li               sapi::IsOk());
116*ec63e07aSXin Li   data.SetRemote(data_voidptr);
117*ec63e07aSXin Li   ASSERT_THAT(api_->sapi_uv_handle_set_data(timer.PtrBoth(), data.PtrBefore()),
118*ec63e07aSXin Li               sapi::IsOk());
119*ec63e07aSXin Li 
120*ec63e07aSXin Li   // Start the timer
121*ec63e07aSXin Li   UVTimerStart(timer.PtrBoth());
122*ec63e07aSXin Li 
123*ec63e07aSXin Li   // Check that data has not changed (because the loop is not running yet)
124*ec63e07aSXin Li   // This is done by resetting the local value and then getting the remote one
125*ec63e07aSXin Li   data.SetValue(0);
126*ec63e07aSXin Li   ASSERT_THAT(sandbox_->TransferFromSandboxee(&data), sapi::IsOk());
127*ec63e07aSXin Li   ASSERT_EQ(data.GetValue(), kData);
128*ec63e07aSXin Li 
129*ec63e07aSXin Li   // Run the loop
130*ec63e07aSXin Li   UVDefaultLoop(&loop);
131*ec63e07aSXin Li   UVRun(loop.PtrNone());
132*ec63e07aSXin Li 
133*ec63e07aSXin Li   // Check that data has changed (and therefore callback was called correctly)
134*ec63e07aSXin Li   ASSERT_THAT(sandbox_->TransferFromSandboxee(&data), sapi::IsOk());
135*ec63e07aSXin Li   ASSERT_EQ(data.GetValue(), kData + 1);
136*ec63e07aSXin Li 
137*ec63e07aSXin Li   // Close the loop
138*ec63e07aSXin Li   UVDefaultLoop(&loop);
139*ec63e07aSXin Li   UVLoopClose(loop.PtrNone());
140*ec63e07aSXin Li }
141*ec63e07aSXin Li 
142*ec63e07aSXin Li }  // namespace
143