| 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 |  | 
| Steve Muckle | 373a3ca | 2019-12-06 17:08:09 -0800 | [diff] [blame] | 34 | // Used by libmodprobe_ext_test to fake a kernel commandline | 
|  | 35 | std::string kernel_cmdline; | 
|  | 36 |  | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 37 | TEST(libmodprobe, Test) { | 
| Steve Muckle | 373a3ca | 2019-12-06 17:08:09 -0800 | [diff] [blame] | 38 | kernel_cmdline = | 
|  | 39 | "flag1 flag2 test1.option1=50 test4.option3=\"set x\" test1.option2=60 " | 
|  | 40 | "test8. test5.option1= test10.option1=1"; | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 41 | test_modules = { | 
|  | 42 | "/test1.ko",  "/test2.ko",  "/test3.ko",  "/test4.ko",  "/test5.ko", | 
|  | 43 | "/test6.ko",  "/test7.ko",  "/test8.ko",  "/test9.ko",  "/test10.ko", | 
|  | 44 | "/test11.ko", "/test12.ko", "/test13.ko", "/test14.ko", "/test15.ko", | 
|  | 45 | }; | 
|  | 46 |  | 
|  | 47 | std::vector<std::string> expected_modules_loaded = { | 
|  | 48 | "/test14.ko", | 
|  | 49 | "/test15.ko", | 
|  | 50 | "/test3.ko", | 
| Steve Muckle | 373a3ca | 2019-12-06 17:08:09 -0800 | [diff] [blame] | 51 | "/test4.ko option3=\"set x\"", | 
|  | 52 | "/test1.ko option1=50 option2=60", | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 53 | "/test6.ko", | 
|  | 54 | "/test2.ko", | 
| Steve Muckle | 373a3ca | 2019-12-06 17:08:09 -0800 | [diff] [blame] | 55 | "/test5.ko option1=", | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 56 | "/test8.ko", | 
|  | 57 | "/test7.ko param1=4", | 
|  | 58 | "/test9.ko param_x=1 param_y=2 param_z=3", | 
| Steve Muckle | 373a3ca | 2019-12-06 17:08:09 -0800 | [diff] [blame] | 59 | "/test10.ko option1=1", | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 60 | "/test12.ko", | 
|  | 61 | "/test11.ko", | 
|  | 62 | "/test13.ko", | 
|  | 63 | }; | 
|  | 64 |  | 
| Steve Muckle | bb58b01 | 2019-07-30 11:58:11 -0700 | [diff] [blame] | 65 | std::vector<std::string> expected_after_remove = { | 
| Steve Muckle | 373a3ca | 2019-12-06 17:08:09 -0800 | [diff] [blame] | 66 | "/test14.ko", | 
|  | 67 | "/test15.ko", | 
|  | 68 | "/test1.ko option1=50 option2=60", | 
|  | 69 | "/test6.ko", | 
|  | 70 | "/test2.ko", | 
|  | 71 | "/test5.ko option1=", | 
|  | 72 | "/test8.ko", | 
|  | 73 | "/test7.ko param1=4", | 
|  | 74 | "/test9.ko param_x=1 param_y=2 param_z=3", | 
|  | 75 | "/test10.ko option1=1", | 
|  | 76 | "/test12.ko", | 
|  | 77 | "/test11.ko", | 
| Steve Muckle | bb58b01 | 2019-07-30 11:58:11 -0700 | [diff] [blame] | 78 | "/test13.ko", | 
|  | 79 | }; | 
|  | 80 |  | 
| Will McVicker | 87b2ef0 | 2021-03-12 11:11:37 -0800 | [diff] [blame] | 81 | std::vector<std::string> expected_modules_blocklist_enabled = { | 
|  | 82 | "/test1.ko option1=50 option2=60", | 
|  | 83 | "/test6.ko", | 
|  | 84 | "/test2.ko", | 
|  | 85 | "/test5.ko option1=", | 
|  | 86 | "/test8.ko", | 
|  | 87 | "/test7.ko param1=4", | 
|  | 88 | "/test12.ko", | 
|  | 89 | "/test11.ko", | 
|  | 90 | "/test13.ko", | 
|  | 91 | }; | 
|  | 92 |  | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 93 | const std::string modules_dep = | 
|  | 94 | "test1.ko:\n" | 
|  | 95 | "test2.ko:\n" | 
|  | 96 | "test3.ko:\n" | 
|  | 97 | "test4.ko: test3.ko\n" | 
|  | 98 | "test5.ko: test2.ko test6.ko\n" | 
|  | 99 | "test6.ko:\n" | 
|  | 100 | "test7.ko:\n" | 
|  | 101 | "test8.ko:\n" | 
|  | 102 | "test9.ko:\n" | 
|  | 103 | "test10.ko:\n" | 
|  | 104 | "test11.ko:\n" | 
|  | 105 | "test12.ko:\n" | 
|  | 106 | "test13.ko:\n" | 
|  | 107 | "test14.ko:\n" | 
|  | 108 | "test15.ko:\n"; | 
|  | 109 |  | 
|  | 110 | const std::string modules_softdep = | 
|  | 111 | "softdep test7 pre: test8\n" | 
|  | 112 | "softdep test9 post: test10\n" | 
|  | 113 | "softdep test11 pre: test12 post: test13\n" | 
|  | 114 | "softdep test3 pre: test141516\n"; | 
|  | 115 |  | 
|  | 116 | const std::string modules_alias = | 
|  | 117 | "# Aliases extracted from modules themselves.\n" | 
|  | 118 | "\n" | 
|  | 119 | "alias test141516 test14\n" | 
|  | 120 | "alias test141516 test15\n" | 
|  | 121 | "alias test141516 test16\n"; | 
|  | 122 |  | 
|  | 123 | const std::string modules_options = | 
|  | 124 | "options test7.ko param1=4\n" | 
|  | 125 | "options test9.ko param_x=1 param_y=2 param_z=3\n" | 
|  | 126 | "options test100.ko param_1=1\n"; | 
|  | 127 |  | 
| Mark Salyzyn | 703fb74 | 2020-06-15 11:51:59 -0700 | [diff] [blame] | 128 | const std::string modules_blocklist = | 
|  | 129 | "blocklist test9.ko\n" | 
|  | 130 | "blocklist test3.ko\n"; | 
| Steve Muckle | e31f840 | 2019-07-31 14:34:52 -0700 | [diff] [blame] | 131 |  | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 132 | const std::string modules_load = | 
|  | 133 | "test4.ko\n" | 
|  | 134 | "test1.ko\n" | 
|  | 135 | "test3.ko\n" | 
|  | 136 | "test5.ko\n" | 
|  | 137 | "test7.ko\n" | 
|  | 138 | "test9.ko\n" | 
|  | 139 | "test11.ko\n"; | 
|  | 140 |  | 
|  | 141 | TemporaryDir dir; | 
| Steve Muckle | e31f840 | 2019-07-31 14:34:52 -0700 | [diff] [blame] | 142 | auto dir_path = std::string(dir.path); | 
|  | 143 | ASSERT_TRUE(android::base::WriteStringToFile(modules_alias, dir_path + "/modules.alias", 0600, | 
|  | 144 | getuid(), getgid())); | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 145 |  | 
| Steve Muckle | e31f840 | 2019-07-31 14:34:52 -0700 | [diff] [blame] | 146 | ASSERT_TRUE(android::base::WriteStringToFile(modules_dep, dir_path + "/modules.dep", 0600, | 
|  | 147 | getuid(), getgid())); | 
|  | 148 | ASSERT_TRUE(android::base::WriteStringToFile(modules_softdep, dir_path + "/modules.softdep", | 
|  | 149 | 0600, getuid(), getgid())); | 
|  | 150 | ASSERT_TRUE(android::base::WriteStringToFile(modules_options, dir_path + "/modules.options", | 
|  | 151 | 0600, getuid(), getgid())); | 
|  | 152 | ASSERT_TRUE(android::base::WriteStringToFile(modules_load, dir_path + "/modules.load", 0600, | 
|  | 153 | getuid(), getgid())); | 
| Mark Salyzyn | 703fb74 | 2020-06-15 11:51:59 -0700 | [diff] [blame] | 154 | ASSERT_TRUE(android::base::WriteStringToFile(modules_blocklist, dir_path + "/modules.blocklist", | 
| Steve Muckle | e31f840 | 2019-07-31 14:34:52 -0700 | [diff] [blame] | 155 | 0600, getuid(), getgid())); | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 156 |  | 
|  | 157 | for (auto i = test_modules.begin(); i != test_modules.end(); ++i) { | 
|  | 158 | *i = dir.path + *i; | 
|  | 159 | } | 
|  | 160 |  | 
| Will McVicker | 87b2ef0 | 2021-03-12 11:11:37 -0800 | [diff] [blame] | 161 | Modprobe m({dir.path}, "modules.load", false); | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 162 | EXPECT_TRUE(m.LoadListedModules()); | 
|  | 163 |  | 
|  | 164 | GTEST_LOG_(INFO) << "Expected modules loaded (in order):"; | 
|  | 165 | for (auto i = expected_modules_loaded.begin(); i != expected_modules_loaded.end(); ++i) { | 
|  | 166 | *i = dir.path + *i; | 
|  | 167 | GTEST_LOG_(INFO) << "\"" << *i << "\""; | 
|  | 168 | } | 
|  | 169 | GTEST_LOG_(INFO) << "Actual modules loaded (in order):"; | 
|  | 170 | for (auto i = modules_loaded.begin(); i != modules_loaded.end(); ++i) { | 
|  | 171 | GTEST_LOG_(INFO) << "\"" << *i << "\""; | 
|  | 172 | } | 
|  | 173 |  | 
|  | 174 | EXPECT_TRUE(modules_loaded == expected_modules_loaded); | 
| Steve Muckle | bb58b01 | 2019-07-30 11:58:11 -0700 | [diff] [blame] | 175 |  | 
| Steve Muckle | b0c4881 | 2020-05-29 16:30:33 -0700 | [diff] [blame] | 176 | EXPECT_TRUE(m.GetModuleCount() == 15); | 
| Steve Muckle | bb58b01 | 2019-07-30 11:58:11 -0700 | [diff] [blame] | 177 | EXPECT_TRUE(m.Remove("test4")); | 
|  | 178 |  | 
|  | 179 | GTEST_LOG_(INFO) << "Expected modules loaded after removing test4 (in order):"; | 
|  | 180 | for (auto i = expected_after_remove.begin(); i != expected_after_remove.end(); ++i) { | 
|  | 181 | *i = dir.path + *i; | 
|  | 182 | GTEST_LOG_(INFO) << "\"" << *i << "\""; | 
|  | 183 | } | 
|  | 184 | GTEST_LOG_(INFO) << "Actual modules loaded after removing test4 (in order):"; | 
|  | 185 | for (auto i = modules_loaded.begin(); i != modules_loaded.end(); ++i) { | 
|  | 186 | GTEST_LOG_(INFO) << "\"" << *i << "\""; | 
|  | 187 | } | 
|  | 188 |  | 
|  | 189 | EXPECT_TRUE(modules_loaded == expected_after_remove); | 
| Steve Muckle | e31f840 | 2019-07-31 14:34:52 -0700 | [diff] [blame] | 190 |  | 
| Chungkai | c60300a | 2021-02-03 20:30:07 -0800 | [diff] [blame] | 191 | Modprobe m2({dir.path}); | 
|  | 192 |  | 
|  | 193 | EXPECT_FALSE(m2.LoadWithAliases("test4", true)); | 
|  | 194 | while (modules_loaded.size() > 0) EXPECT_TRUE(m2.Remove(modules_loaded.front())); | 
|  | 195 | EXPECT_TRUE(m2.LoadListedModules()); | 
| Will McVicker | 87b2ef0 | 2021-03-12 11:11:37 -0800 | [diff] [blame] | 196 |  | 
|  | 197 | GTEST_LOG_(INFO) << "Expected modules loaded after enabling blocklist (in order):"; | 
|  | 198 | for (auto i = expected_modules_blocklist_enabled.begin(); | 
|  | 199 | i != expected_modules_blocklist_enabled.end(); ++i) { | 
|  | 200 | *i = dir.path + *i; | 
|  | 201 | GTEST_LOG_(INFO) << "\"" << *i << "\""; | 
|  | 202 | } | 
|  | 203 | GTEST_LOG_(INFO) << "Actual modules loaded with blocklist enabled (in order):"; | 
|  | 204 | for (auto i = modules_loaded.begin(); i != modules_loaded.end(); ++i) { | 
|  | 205 | GTEST_LOG_(INFO) << "\"" << *i << "\""; | 
|  | 206 | } | 
|  | 207 | EXPECT_TRUE(modules_loaded == expected_modules_blocklist_enabled); | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 208 | } | 
| Andrew Scull | fb18f6e | 2020-10-18 17:37:27 +0100 | [diff] [blame] | 209 |  | 
|  | 210 | TEST(libmodprobe, ModuleDepLineWithoutColonIsSkipped) { | 
|  | 211 | TemporaryDir dir; | 
|  | 212 | auto dir_path = std::string(dir.path); | 
|  | 213 | ASSERT_TRUE(android::base::WriteStringToFile( | 
|  | 214 | "no_colon.ko no_colon.ko\n", dir_path + "/modules.dep", 0600, getuid(), getgid())); | 
|  | 215 |  | 
|  | 216 | kernel_cmdline = ""; | 
|  | 217 | test_modules = {dir_path + "/no_colon.ko"}; | 
|  | 218 |  | 
|  | 219 | Modprobe m({dir.path}); | 
|  | 220 | EXPECT_FALSE(m.LoadWithAliases("no_colon", true)); | 
|  | 221 | } |