Elliott Hughes | eb664e2 | 2014-05-13 10:44:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 <gtest/gtest.h> |
| 18 | |
| 19 | #include <mntent.h> |
| 20 | |
| 21 | TEST(mntent, mntent_smoke) { |
Elliott Hughes | 1e393b0 | 2022-05-13 16:06:54 -0700 | [diff] [blame] | 22 | // Read all the entries with getmntent(). |
Elliott Hughes | e3c4acf | 2014-11-13 14:27:25 -0800 | [diff] [blame] | 23 | FILE* fp = setmntent("/proc/mounts", "r"); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 24 | ASSERT_TRUE(fp != nullptr); |
Elliott Hughes | eb664e2 | 2014-05-13 10:44:07 -0700 | [diff] [blame] | 25 | |
Elliott Hughes | 1e393b0 | 2022-05-13 16:06:54 -0700 | [diff] [blame] | 26 | std::vector<std::string> fsnames; |
| 27 | std::vector<std::string> dirs; |
| 28 | mntent* me; |
| 29 | while ((me = getmntent(fp)) != nullptr) { |
| 30 | fsnames.push_back(me->mnt_fsname); |
| 31 | dirs.push_back(me->mnt_dir); |
| 32 | } |
Elliott Hughes | eb664e2 | 2014-05-13 10:44:07 -0700 | [diff] [blame] | 33 | |
Elliott Hughes | 1e393b0 | 2022-05-13 16:06:54 -0700 | [diff] [blame] | 34 | ASSERT_EQ(1, endmntent(fp)); |
| 35 | |
| 36 | // Then again with getmntent_r(), checking they match. |
| 37 | fp = setmntent("/proc/mounts", "r"); |
| 38 | ASSERT_TRUE(fp != nullptr); |
Elliott Hughes | e3c4acf | 2014-11-13 14:27:25 -0800 | [diff] [blame] | 39 | |
| 40 | struct mntent entry; |
| 41 | char buf[BUFSIZ]; |
Elliott Hughes | 1e393b0 | 2022-05-13 16:06:54 -0700 | [diff] [blame] | 42 | size_t i = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 43 | while (getmntent_r(fp, &entry, buf, sizeof(buf)) != nullptr) { |
Elliott Hughes | 1e393b0 | 2022-05-13 16:06:54 -0700 | [diff] [blame] | 44 | ASSERT_EQ(fsnames[i], entry.mnt_fsname); |
| 45 | ASSERT_EQ(dirs[i], entry.mnt_dir); |
| 46 | i++; |
Elliott Hughes | e3c4acf | 2014-11-13 14:27:25 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Elliott Hughes | eb664e2 | 2014-05-13 10:44:07 -0700 | [diff] [blame] | 49 | ASSERT_EQ(1, endmntent(fp)); |
Elliott Hughes | 1e393b0 | 2022-05-13 16:06:54 -0700 | [diff] [blame] | 50 | |
| 51 | // And just for good measure: we did see a /proc entry, right? |
| 52 | auto it = std::find(fsnames.begin(), fsnames.end(), "proc"); |
| 53 | ASSERT_TRUE(it != fsnames.end()); |
| 54 | size_t proc_index = it - fsnames.begin(); |
| 55 | ASSERT_EQ("/proc", dirs[proc_index]); |
Elliott Hughes | eb664e2 | 2014-05-13 10:44:07 -0700 | [diff] [blame] | 56 | } |
Greg Hackmann | 3679ade | 2016-03-04 10:07:35 -0800 | [diff] [blame] | 57 | |
| 58 | TEST(mntent, hasmntopt) { |
| 59 | // indices 1 1 |
| 60 | // of keys: 0 5 9 1 4 |
| 61 | char mnt_opts[]{"aa=b,a=b,b,bb,c=d"}; |
| 62 | struct mntent ent; |
| 63 | memset(&ent, 0, sizeof(ent)); |
| 64 | ent.mnt_opts = mnt_opts; |
| 65 | |
| 66 | EXPECT_EQ(mnt_opts, hasmntopt(&ent, "aa")); |
| 67 | EXPECT_EQ(mnt_opts + 5, hasmntopt(&ent, "a")); |
| 68 | EXPECT_EQ(mnt_opts + 9, hasmntopt(&ent, "b")); |
| 69 | EXPECT_EQ(mnt_opts + 11, hasmntopt(&ent, "bb")); |
| 70 | EXPECT_EQ(mnt_opts + 14, hasmntopt(&ent, "c")); |
| 71 | EXPECT_EQ(nullptr, hasmntopt(&ent, "d")); |
| 72 | EXPECT_EQ(nullptr, hasmntopt(&ent, "e")); |
| 73 | } |