| 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 |  | 
|  | 59 | const std::string modules_dep = | 
|  | 60 | "test1.ko:\n" | 
|  | 61 | "test2.ko:\n" | 
|  | 62 | "test3.ko:\n" | 
|  | 63 | "test4.ko: test3.ko\n" | 
|  | 64 | "test5.ko: test2.ko test6.ko\n" | 
|  | 65 | "test6.ko:\n" | 
|  | 66 | "test7.ko:\n" | 
|  | 67 | "test8.ko:\n" | 
|  | 68 | "test9.ko:\n" | 
|  | 69 | "test10.ko:\n" | 
|  | 70 | "test11.ko:\n" | 
|  | 71 | "test12.ko:\n" | 
|  | 72 | "test13.ko:\n" | 
|  | 73 | "test14.ko:\n" | 
|  | 74 | "test15.ko:\n"; | 
|  | 75 |  | 
|  | 76 | const std::string modules_softdep = | 
|  | 77 | "softdep test7 pre: test8\n" | 
|  | 78 | "softdep test9 post: test10\n" | 
|  | 79 | "softdep test11 pre: test12 post: test13\n" | 
|  | 80 | "softdep test3 pre: test141516\n"; | 
|  | 81 |  | 
|  | 82 | const std::string modules_alias = | 
|  | 83 | "# Aliases extracted from modules themselves.\n" | 
|  | 84 | "\n" | 
|  | 85 | "alias test141516 test14\n" | 
|  | 86 | "alias test141516 test15\n" | 
|  | 87 | "alias test141516 test16\n"; | 
|  | 88 |  | 
|  | 89 | const std::string modules_options = | 
|  | 90 | "options test7.ko param1=4\n" | 
|  | 91 | "options test9.ko param_x=1 param_y=2 param_z=3\n" | 
|  | 92 | "options test100.ko param_1=1\n"; | 
|  | 93 |  | 
|  | 94 | const std::string modules_load = | 
|  | 95 | "test4.ko\n" | 
|  | 96 | "test1.ko\n" | 
|  | 97 | "test3.ko\n" | 
|  | 98 | "test5.ko\n" | 
|  | 99 | "test7.ko\n" | 
|  | 100 | "test9.ko\n" | 
|  | 101 | "test11.ko\n"; | 
|  | 102 |  | 
|  | 103 | TemporaryDir dir; | 
|  | 104 | ASSERT_TRUE(android::base::WriteStringToFile( | 
|  | 105 | modules_alias, std::string(dir.path) + "/modules.alias", 0600, getuid(), getgid())); | 
|  | 106 |  | 
|  | 107 | ASSERT_TRUE(android::base::WriteStringToFile( | 
|  | 108 | modules_dep, std::string(dir.path) + "/modules.dep", 0600, getuid(), getgid())); | 
|  | 109 | ASSERT_TRUE(android::base::WriteStringToFile( | 
|  | 110 | modules_softdep, std::string(dir.path) + "/modules.softdep", 0600, getuid(), getgid())); | 
|  | 111 | ASSERT_TRUE(android::base::WriteStringToFile( | 
|  | 112 | modules_options, std::string(dir.path) + "/modules.options", 0600, getuid(), getgid())); | 
|  | 113 | ASSERT_TRUE(android::base::WriteStringToFile( | 
|  | 114 | modules_load, std::string(dir.path) + "/modules.load", 0600, getuid(), getgid())); | 
|  | 115 |  | 
|  | 116 | for (auto i = test_modules.begin(); i != test_modules.end(); ++i) { | 
|  | 117 | *i = dir.path + *i; | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | Modprobe m({dir.path}); | 
|  | 121 | EXPECT_TRUE(m.LoadListedModules()); | 
|  | 122 |  | 
|  | 123 | GTEST_LOG_(INFO) << "Expected modules loaded (in order):"; | 
|  | 124 | for (auto i = expected_modules_loaded.begin(); i != expected_modules_loaded.end(); ++i) { | 
|  | 125 | *i = dir.path + *i; | 
|  | 126 | GTEST_LOG_(INFO) << "\"" << *i << "\""; | 
|  | 127 | } | 
|  | 128 | GTEST_LOG_(INFO) << "Actual modules loaded (in order):"; | 
|  | 129 | for (auto i = modules_loaded.begin(); i != modules_loaded.end(); ++i) { | 
|  | 130 | GTEST_LOG_(INFO) << "\"" << *i << "\""; | 
|  | 131 | } | 
|  | 132 |  | 
|  | 133 | EXPECT_TRUE(modules_loaded == expected_modules_loaded); | 
|  | 134 | } |