blob: d50c10d8f5fd0691e9eae1f8d7e126797803ae23 [file] [log] [blame]
Steve Muckle18b981e2019-04-15 17:43:02 -07001/*
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.
29std::vector<std::string> test_modules;
30
31// Used by libmodprobe_ext_test to report which modules would have been loaded.
32std::vector<std::string> modules_loaded;
33
Steve Muckle373a3ca2019-12-06 17:08:09 -080034// Used by libmodprobe_ext_test to fake a kernel commandline
35std::string kernel_cmdline;
36
Steve Muckle18b981e2019-04-15 17:43:02 -070037TEST(libmodprobe, Test) {
Steve Muckle373a3ca2019-12-06 17:08:09 -080038 kernel_cmdline =
39 "flag1 flag2 test1.option1=50 test4.option3=\"set x\" test1.option2=60 "
40 "test8. test5.option1= test10.option1=1";
Steve Muckle18b981e2019-04-15 17:43:02 -070041 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 Muckle373a3ca2019-12-06 17:08:09 -080051 "/test4.ko option3=\"set x\"",
52 "/test1.ko option1=50 option2=60",
Steve Muckle18b981e2019-04-15 17:43:02 -070053 "/test6.ko",
54 "/test2.ko",
Steve Muckle373a3ca2019-12-06 17:08:09 -080055 "/test5.ko option1=",
Steve Muckle18b981e2019-04-15 17:43:02 -070056 "/test8.ko",
57 "/test7.ko param1=4",
58 "/test9.ko param_x=1 param_y=2 param_z=3",
Steve Muckle373a3ca2019-12-06 17:08:09 -080059 "/test10.ko option1=1",
Steve Muckle18b981e2019-04-15 17:43:02 -070060 "/test12.ko",
61 "/test11.ko",
62 "/test13.ko",
63 };
64
Steve Mucklebb58b012019-07-30 11:58:11 -070065 std::vector<std::string> expected_after_remove = {
Steve Muckle373a3ca2019-12-06 17:08:09 -080066 "/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 Mucklebb58b012019-07-30 11:58:11 -070078 "/test13.ko",
79 };
80
Steve Muckle18b981e2019-04-15 17:43:02 -070081 const std::string modules_dep =
82 "test1.ko:\n"
83 "test2.ko:\n"
84 "test3.ko:\n"
85 "test4.ko: test3.ko\n"
86 "test5.ko: test2.ko test6.ko\n"
87 "test6.ko:\n"
88 "test7.ko:\n"
89 "test8.ko:\n"
90 "test9.ko:\n"
91 "test10.ko:\n"
92 "test11.ko:\n"
93 "test12.ko:\n"
94 "test13.ko:\n"
95 "test14.ko:\n"
96 "test15.ko:\n";
97
98 const std::string modules_softdep =
99 "softdep test7 pre: test8\n"
100 "softdep test9 post: test10\n"
101 "softdep test11 pre: test12 post: test13\n"
102 "softdep test3 pre: test141516\n";
103
104 const std::string modules_alias =
105 "# Aliases extracted from modules themselves.\n"
106 "\n"
107 "alias test141516 test14\n"
108 "alias test141516 test15\n"
109 "alias test141516 test16\n";
110
111 const std::string modules_options =
112 "options test7.ko param1=4\n"
113 "options test9.ko param_x=1 param_y=2 param_z=3\n"
114 "options test100.ko param_1=1\n";
115
Mark Salyzyn703fb742020-06-15 11:51:59 -0700116 const std::string modules_blocklist =
117 "blocklist test9.ko\n"
118 "blocklist test3.ko\n";
Steve Mucklee31f8402019-07-31 14:34:52 -0700119
Steve Muckle18b981e2019-04-15 17:43:02 -0700120 const std::string modules_load =
121 "test4.ko\n"
122 "test1.ko\n"
123 "test3.ko\n"
124 "test5.ko\n"
125 "test7.ko\n"
126 "test9.ko\n"
127 "test11.ko\n";
128
129 TemporaryDir dir;
Steve Mucklee31f8402019-07-31 14:34:52 -0700130 auto dir_path = std::string(dir.path);
131 ASSERT_TRUE(android::base::WriteStringToFile(modules_alias, dir_path + "/modules.alias", 0600,
132 getuid(), getgid()));
Steve Muckle18b981e2019-04-15 17:43:02 -0700133
Steve Mucklee31f8402019-07-31 14:34:52 -0700134 ASSERT_TRUE(android::base::WriteStringToFile(modules_dep, dir_path + "/modules.dep", 0600,
135 getuid(), getgid()));
136 ASSERT_TRUE(android::base::WriteStringToFile(modules_softdep, dir_path + "/modules.softdep",
137 0600, getuid(), getgid()));
138 ASSERT_TRUE(android::base::WriteStringToFile(modules_options, dir_path + "/modules.options",
139 0600, getuid(), getgid()));
140 ASSERT_TRUE(android::base::WriteStringToFile(modules_load, dir_path + "/modules.load", 0600,
141 getuid(), getgid()));
Mark Salyzyn703fb742020-06-15 11:51:59 -0700142 ASSERT_TRUE(android::base::WriteStringToFile(modules_blocklist, dir_path + "/modules.blocklist",
Steve Mucklee31f8402019-07-31 14:34:52 -0700143 0600, getuid(), getgid()));
Steve Muckle18b981e2019-04-15 17:43:02 -0700144
145 for (auto i = test_modules.begin(); i != test_modules.end(); ++i) {
146 *i = dir.path + *i;
147 }
148
149 Modprobe m({dir.path});
150 EXPECT_TRUE(m.LoadListedModules());
151
152 GTEST_LOG_(INFO) << "Expected modules loaded (in order):";
153 for (auto i = expected_modules_loaded.begin(); i != expected_modules_loaded.end(); ++i) {
154 *i = dir.path + *i;
155 GTEST_LOG_(INFO) << "\"" << *i << "\"";
156 }
157 GTEST_LOG_(INFO) << "Actual modules loaded (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_modules_loaded);
Steve Mucklebb58b012019-07-30 11:58:11 -0700163
Steve Muckleb0c48812020-05-29 16:30:33 -0700164 EXPECT_TRUE(m.GetModuleCount() == 15);
Steve Mucklebb58b012019-07-30 11:58:11 -0700165 EXPECT_TRUE(m.Remove("test4"));
166
167 GTEST_LOG_(INFO) << "Expected modules loaded after removing test4 (in order):";
168 for (auto i = expected_after_remove.begin(); i != expected_after_remove.end(); ++i) {
169 *i = dir.path + *i;
170 GTEST_LOG_(INFO) << "\"" << *i << "\"";
171 }
172 GTEST_LOG_(INFO) << "Actual modules loaded after removing test4 (in order):";
173 for (auto i = modules_loaded.begin(); i != modules_loaded.end(); ++i) {
174 GTEST_LOG_(INFO) << "\"" << *i << "\"";
175 }
176
177 EXPECT_TRUE(modules_loaded == expected_after_remove);
Steve Mucklee31f8402019-07-31 14:34:52 -0700178
Mark Salyzyn703fb742020-06-15 11:51:59 -0700179 m.EnableBlocklist(true);
Steve Mucklee31f8402019-07-31 14:34:52 -0700180 EXPECT_FALSE(m.LoadWithAliases("test4", true));
Steve Muckle18b981e2019-04-15 17:43:02 -0700181}
Andrew Scullfb18f6e2020-10-18 17:37:27 +0100182
183TEST(libmodprobe, ModuleDepLineWithoutColonIsSkipped) {
184 TemporaryDir dir;
185 auto dir_path = std::string(dir.path);
186 ASSERT_TRUE(android::base::WriteStringToFile(
187 "no_colon.ko no_colon.ko\n", dir_path + "/modules.dep", 0600, getuid(), getgid()));
188
189 kernel_cmdline = "";
190 test_modules = {dir_path + "/no_colon.ko"};
191
192 Modprobe m({dir.path});
193 EXPECT_FALSE(m.LoadWithAliases("no_colon", true));
194}