1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <string>
20 
21 #include <gmock/gmock.h>
22 #include <jsonpb/json_schema_test.h>
23 
24 #include "cgroups.pb.h"
25 
26 using ::testing::MatchesRegex;
27 
28 namespace android {
29 namespace profiles {
30 
31 class CgroupsTest : public jsonpb::JsonSchemaTest {
32   public:
SetUp()33     void SetUp() override {
34         JsonSchemaTest::SetUp();
35         cgroups_ = static_cast<Cgroups*>(message());
36     }
37     Cgroups* cgroups_;
38 };
39 
TEST_P(CgroupsTest,CgroupRequiredFields)40 TEST_P(CgroupsTest, CgroupRequiredFields) {
41     for (int i = 0; i < cgroups_->cgroups_size(); ++i) {
42         auto&& cgroup = cgroups_->cgroups(i);
43         EXPECT_FALSE(cgroup.controller().empty())
44                 << "No controller name for cgroup #" << i << " in " << file_path_;
45         EXPECT_FALSE(cgroup.path().empty()) << "No path for cgroup #" << i << " in " << file_path_;
46     }
47 }
48 
TEST_P(CgroupsTest,Cgroup2RequiredFields)49 TEST_P(CgroupsTest, Cgroup2RequiredFields) {
50     if (cgroups_->has_cgroups2()) {
51         EXPECT_FALSE(cgroups_->cgroups2().path().empty())
52                 << "No path for cgroup2 in " << file_path_;
53     }
54 }
55 
56 // "Mode" field must be in the format of "0xxx".
57 static inline constexpr const char* REGEX_MODE = "(0[0-7]{3})?";
TEST_P(CgroupsTest,CgroupMode)58 TEST_P(CgroupsTest, CgroupMode) {
59     for (int i = 0; i < cgroups_->cgroups_size(); ++i) {
60         EXPECT_THAT(cgroups_->cgroups(i).mode(), MatchesRegex(REGEX_MODE))
61                 << "For cgroup controller #" << i << " in " << file_path_;
62     }
63 }
64 
TEST_P(CgroupsTest,Cgroup2Mode)65 TEST_P(CgroupsTest, Cgroup2Mode) {
66     EXPECT_THAT(cgroups_->cgroups2().mode(), MatchesRegex(REGEX_MODE))
67             << "For cgroups2 in " << file_path_;
68 }
69 
70 }  // namespace profiles
71 }  // namespace android
72