Yifan Hong | 72ff585 | 2019-02-13 14:32:34 -0800 | [diff] [blame^] | 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 | #include <string> |
| 18 | |
| 19 | #include <android-base/file.h> |
| 20 | #include <gmock/gmock.h> |
| 21 | #include <jsonpb/json_schema_test.h> |
| 22 | |
| 23 | #include "cgroups.pb.h" |
| 24 | #include "task_profiles.pb.h" |
| 25 | |
| 26 | using namespace ::android::jsonpb; |
| 27 | using ::android::base::GetExecutableDirectory; |
| 28 | using ::testing::MatchesRegex; |
| 29 | |
| 30 | namespace android { |
| 31 | namespace profiles { |
| 32 | |
| 33 | template <typename T> |
| 34 | JsonSchemaTestConfigFactory MakeTestParam(const std::string& path) { |
| 35 | return jsonpb::MakeTestParam<T>(GetExecutableDirectory() + path); |
| 36 | } |
| 37 | |
| 38 | TEST(LibProcessgroupProto, EmptyMode) { |
| 39 | EXPECT_EQ(0, strtoul("", nullptr, 8)) |
| 40 | << "Empty mode string cannot be silently converted to 0; this should not happen"; |
| 41 | } |
| 42 | |
| 43 | class CgroupsTest : public JsonSchemaTest { |
| 44 | public: |
| 45 | void SetUp() override { |
| 46 | JsonSchemaTest::SetUp(); |
| 47 | cgroups_ = static_cast<Cgroups*>(message()); |
| 48 | } |
| 49 | Cgroups* cgroups_; |
| 50 | }; |
| 51 | |
| 52 | TEST_P(CgroupsTest, CgroupRequiredFields) { |
| 53 | for (int i = 0; i < cgroups_->cgroups_size(); ++i) { |
| 54 | auto&& cgroup = cgroups_->cgroups(i); |
| 55 | EXPECT_FALSE(cgroup.controller().empty()) |
| 56 | << "No controller name for cgroup #" << i << " in " << file_path_; |
| 57 | EXPECT_FALSE(cgroup.path().empty()) << "No path for cgroup #" << i << " in " << file_path_; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | TEST_P(CgroupsTest, Cgroup2RequiredFields) { |
| 62 | if (cgroups_->has_cgroups2()) { |
| 63 | EXPECT_FALSE(cgroups_->cgroups2().path().empty()) |
| 64 | << "No path for cgroup2 in " << file_path_; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // "Mode" field must be in the format of "0xxx". |
| 69 | static constexpr const char* REGEX_MODE = "(0[0-7]{3})?"; |
| 70 | TEST_P(CgroupsTest, CgroupMode) { |
| 71 | for (int i = 0; i < cgroups_->cgroups_size(); ++i) { |
| 72 | EXPECT_THAT(cgroups_->cgroups(i).mode(), MatchesRegex(REGEX_MODE)) |
| 73 | << "For cgroup controller #" << i << " in " << file_path_; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | TEST_P(CgroupsTest, Cgroup2Mode) { |
| 78 | EXPECT_THAT(cgroups_->cgroups2().mode(), MatchesRegex(REGEX_MODE)) |
| 79 | << "For cgroups2 in " << file_path_; |
| 80 | } |
| 81 | |
| 82 | class TaskProfilesTest : public JsonSchemaTest { |
| 83 | public: |
| 84 | void SetUp() override { |
| 85 | JsonSchemaTest::SetUp(); |
| 86 | task_profiles_ = static_cast<TaskProfiles*>(message()); |
| 87 | } |
| 88 | TaskProfiles* task_profiles_; |
| 89 | }; |
| 90 | |
| 91 | TEST_P(TaskProfilesTest, AttributeRequiredFields) { |
| 92 | for (int i = 0; i < task_profiles_->attributes_size(); ++i) { |
| 93 | auto&& attribute = task_profiles_->attributes(i); |
| 94 | EXPECT_FALSE(attribute.name().empty()) |
| 95 | << "No name for attribute #" << i << " in " << file_path_; |
| 96 | EXPECT_FALSE(attribute.controller().empty()) |
| 97 | << "No controller for attribute #" << i << " in " << file_path_; |
| 98 | EXPECT_FALSE(attribute.file().empty()) |
| 99 | << "No file for attribute #" << i << " in " << file_path_; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | TEST_P(TaskProfilesTest, ProfileRequiredFields) { |
| 104 | for (int profile_idx = 0; profile_idx < task_profiles_->profiles_size(); ++profile_idx) { |
| 105 | auto&& profile = task_profiles_->profiles(profile_idx); |
| 106 | EXPECT_FALSE(profile.name().empty()) |
| 107 | << "No name for profile #" << profile_idx << " in " << file_path_; |
| 108 | for (int action_idx = 0; action_idx < profile.actions_size(); ++action_idx) { |
| 109 | auto&& action = profile.actions(action_idx); |
| 110 | EXPECT_FALSE(action.name().empty()) |
| 111 | << "No name for profiles[" << profile_idx << "].actions[" << action_idx |
| 112 | << "] in " << file_path_; |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // Test suite instantiations |
| 118 | |
| 119 | INSTANTIATE_TEST_SUITE_P(, JsonSchemaTest, |
| 120 | ::testing::Values(MakeTestParam<Cgroups>("/cgroups.json"), |
| 121 | MakeTestParam<Cgroups>("/cgroups.recovery.json"), |
| 122 | MakeTestParam<TaskProfiles>("/task_profiles.json"))); |
| 123 | INSTANTIATE_TEST_SUITE_P(, CgroupsTest, |
| 124 | ::testing::Values(MakeTestParam<Cgroups>("/cgroups.json"), |
| 125 | MakeTestParam<Cgroups>("/cgroups.recovery.json"))); |
| 126 | INSTANTIATE_TEST_SUITE_P(, TaskProfilesTest, |
| 127 | ::testing::Values(MakeTestParam<TaskProfiles>("/task_profiles.json"))); |
| 128 | |
| 129 | } // namespace profiles |
| 130 | } // namespace android |
| 131 | |
| 132 | int main(int argc, char** argv) { |
| 133 | ::testing::InitGoogleTest(&argc, argv); |
| 134 | return RUN_ALL_TESTS(); |
| 135 | } |