Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [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 <functional> |
| 18 | |
| 19 | #include <android-base/file.h> |
| 20 | #include <android-base/macros.h> |
| 21 | #include <android-base/unique_fd.h> |
| 22 | #include <gtest/gtest.h> |
| 23 | |
| 24 | #include <modprobe/modprobe.h> |
| 25 | |
| 26 | #include "libmodprobe_test.h" |
| 27 | |
| 28 | // Used by libmodprobe_ext_test to check if requested modules are present. |
| 29 | std::vector<std::string> test_modules; |
| 30 | |
| 31 | // Used by libmodprobe_ext_test to report which modules would have been loaded. |
| 32 | std::vector<std::string> modules_loaded; |
| 33 | |
| 34 | TEST(libmodprobe, Test) { |
| 35 | test_modules = { |
| 36 | "/test1.ko", "/test2.ko", "/test3.ko", "/test4.ko", "/test5.ko", |
| 37 | "/test6.ko", "/test7.ko", "/test8.ko", "/test9.ko", "/test10.ko", |
| 38 | "/test11.ko", "/test12.ko", "/test13.ko", "/test14.ko", "/test15.ko", |
| 39 | }; |
| 40 | |
| 41 | std::vector<std::string> expected_modules_loaded = { |
| 42 | "/test14.ko", |
| 43 | "/test15.ko", |
| 44 | "/test3.ko", |
| 45 | "/test4.ko", |
| 46 | "/test1.ko", |
| 47 | "/test6.ko", |
| 48 | "/test2.ko", |
| 49 | "/test5.ko", |
| 50 | "/test8.ko", |
| 51 | "/test7.ko param1=4", |
| 52 | "/test9.ko param_x=1 param_y=2 param_z=3", |
| 53 | "/test10.ko", |
| 54 | "/test12.ko", |
| 55 | "/test11.ko", |
| 56 | "/test13.ko", |
| 57 | }; |
| 58 | |
Steve Muckle | bb58b01 | 2019-07-30 11:58:11 -0700 | [diff] [blame] | 59 | std::vector<std::string> expected_after_remove = { |
| 60 | "/test14.ko", "/test15.ko", "/test1.ko", |
| 61 | "/test6.ko", "/test2.ko", "/test5.ko", |
| 62 | "/test8.ko", "/test7.ko param1=4", "/test9.ko param_x=1 param_y=2 param_z=3", |
| 63 | "/test10.ko", "/test12.ko", "/test11.ko", |
| 64 | "/test13.ko", |
| 65 | }; |
| 66 | |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 67 | const std::string modules_dep = |
| 68 | "test1.ko:\n" |
| 69 | "test2.ko:\n" |
| 70 | "test3.ko:\n" |
| 71 | "test4.ko: test3.ko\n" |
| 72 | "test5.ko: test2.ko test6.ko\n" |
| 73 | "test6.ko:\n" |
| 74 | "test7.ko:\n" |
| 75 | "test8.ko:\n" |
| 76 | "test9.ko:\n" |
| 77 | "test10.ko:\n" |
| 78 | "test11.ko:\n" |
| 79 | "test12.ko:\n" |
| 80 | "test13.ko:\n" |
| 81 | "test14.ko:\n" |
| 82 | "test15.ko:\n"; |
| 83 | |
| 84 | const std::string modules_softdep = |
| 85 | "softdep test7 pre: test8\n" |
| 86 | "softdep test9 post: test10\n" |
| 87 | "softdep test11 pre: test12 post: test13\n" |
| 88 | "softdep test3 pre: test141516\n"; |
| 89 | |
| 90 | const std::string modules_alias = |
| 91 | "# Aliases extracted from modules themselves.\n" |
| 92 | "\n" |
| 93 | "alias test141516 test14\n" |
| 94 | "alias test141516 test15\n" |
| 95 | "alias test141516 test16\n"; |
| 96 | |
| 97 | const std::string modules_options = |
| 98 | "options test7.ko param1=4\n" |
| 99 | "options test9.ko param_x=1 param_y=2 param_z=3\n" |
| 100 | "options test100.ko param_1=1\n"; |
| 101 | |
Steve Muckle | e31f840 | 2019-07-31 14:34:52 -0700 | [diff] [blame^] | 102 | const std::string modules_blacklist = |
| 103 | "blacklist test9.ko\n" |
| 104 | "blacklist test3.ko\n"; |
| 105 | |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 106 | const std::string modules_load = |
| 107 | "test4.ko\n" |
| 108 | "test1.ko\n" |
| 109 | "test3.ko\n" |
| 110 | "test5.ko\n" |
| 111 | "test7.ko\n" |
| 112 | "test9.ko\n" |
| 113 | "test11.ko\n"; |
| 114 | |
| 115 | TemporaryDir dir; |
Steve Muckle | e31f840 | 2019-07-31 14:34:52 -0700 | [diff] [blame^] | 116 | auto dir_path = std::string(dir.path); |
| 117 | ASSERT_TRUE(android::base::WriteStringToFile(modules_alias, dir_path + "/modules.alias", 0600, |
| 118 | getuid(), getgid())); |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 119 | |
Steve Muckle | e31f840 | 2019-07-31 14:34:52 -0700 | [diff] [blame^] | 120 | ASSERT_TRUE(android::base::WriteStringToFile(modules_dep, dir_path + "/modules.dep", 0600, |
| 121 | getuid(), getgid())); |
| 122 | ASSERT_TRUE(android::base::WriteStringToFile(modules_softdep, dir_path + "/modules.softdep", |
| 123 | 0600, getuid(), getgid())); |
| 124 | ASSERT_TRUE(android::base::WriteStringToFile(modules_options, dir_path + "/modules.options", |
| 125 | 0600, getuid(), getgid())); |
| 126 | ASSERT_TRUE(android::base::WriteStringToFile(modules_load, dir_path + "/modules.load", 0600, |
| 127 | getuid(), getgid())); |
| 128 | ASSERT_TRUE(android::base::WriteStringToFile(modules_blacklist, dir_path + "/modules.blacklist", |
| 129 | 0600, getuid(), getgid())); |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 130 | |
| 131 | for (auto i = test_modules.begin(); i != test_modules.end(); ++i) { |
| 132 | *i = dir.path + *i; |
| 133 | } |
| 134 | |
| 135 | Modprobe m({dir.path}); |
| 136 | EXPECT_TRUE(m.LoadListedModules()); |
| 137 | |
| 138 | GTEST_LOG_(INFO) << "Expected modules loaded (in order):"; |
| 139 | for (auto i = expected_modules_loaded.begin(); i != expected_modules_loaded.end(); ++i) { |
| 140 | *i = dir.path + *i; |
| 141 | GTEST_LOG_(INFO) << "\"" << *i << "\""; |
| 142 | } |
| 143 | GTEST_LOG_(INFO) << "Actual modules loaded (in order):"; |
| 144 | for (auto i = modules_loaded.begin(); i != modules_loaded.end(); ++i) { |
| 145 | GTEST_LOG_(INFO) << "\"" << *i << "\""; |
| 146 | } |
| 147 | |
| 148 | EXPECT_TRUE(modules_loaded == expected_modules_loaded); |
Steve Muckle | bb58b01 | 2019-07-30 11:58:11 -0700 | [diff] [blame] | 149 | |
| 150 | EXPECT_TRUE(m.Remove("test4")); |
| 151 | |
| 152 | GTEST_LOG_(INFO) << "Expected modules loaded after removing test4 (in order):"; |
| 153 | for (auto i = expected_after_remove.begin(); i != expected_after_remove.end(); ++i) { |
| 154 | *i = dir.path + *i; |
| 155 | GTEST_LOG_(INFO) << "\"" << *i << "\""; |
| 156 | } |
| 157 | GTEST_LOG_(INFO) << "Actual modules loaded after removing test4 (in order):"; |
| 158 | for (auto i = modules_loaded.begin(); i != modules_loaded.end(); ++i) { |
| 159 | GTEST_LOG_(INFO) << "\"" << *i << "\""; |
| 160 | } |
| 161 | |
| 162 | EXPECT_TRUE(modules_loaded == expected_after_remove); |
Steve Muckle | e31f840 | 2019-07-31 14:34:52 -0700 | [diff] [blame^] | 163 | |
| 164 | m.EnableBlacklist(true); |
| 165 | EXPECT_FALSE(m.LoadWithAliases("test4", true)); |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 166 | } |