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 UVTestArraySapiSandbox : 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 .AllowOpen()
35 .AllowSyscall(__NR_sysinfo)
36 .AllowWrite()
37 .BuildOrDie();
38 }
39 };
40
41 class UVTestArray : public ::testing::Test {
42 protected:
SetUp()43 void SetUp() override {
44 sandbox_ = std::make_unique<UVTestArraySapiSandbox>();
45 ASSERT_THAT(sandbox_->Init(), sapi::IsOk());
46 api_ = std::make_unique<uv::UVApi>(sandbox_.get());
47 }
48
49 std::unique_ptr<UVTestArraySapiSandbox> sandbox_;
50 std::unique_ptr<uv::UVApi> api_;
51 };
52
TEST_F(UVTestArray,LoadAvg)53 TEST_F(UVTestArray, LoadAvg) {
54 double avg_buf[] = {-1, -1, -1};
55 sapi::v::Array<double> avg(avg_buf, 3);
56
57 // Check that loadavg is as initialized before call
58 ASSERT_EQ(avg_buf[0], -1);
59 ASSERT_EQ(avg_buf[1], -1);
60 ASSERT_EQ(avg_buf[2], -1);
61
62 // Get loadavg
63 ASSERT_THAT(api_->sapi_uv_loadavg(avg.PtrBoth()), sapi::IsOk());
64
65 // Check that loadavg values are positive
66 ASSERT_GE(avg_buf[0], 0);
67 ASSERT_GE(avg_buf[1], 0);
68 ASSERT_GE(avg_buf[2], 0);
69 }
70
71 } // namespace
72