blob: fd69ae1b0372021fea6aa71299d4f4e0b93ba016 [file] [log] [blame]
Elliott Hugheseb664e22014-05-13 10:44:07 -07001/*
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
21TEST(mntent, mntent_smoke) {
Elliott Hughes1e393b02022-05-13 16:06:54 -070022 // Read all the entries with getmntent().
Elliott Hughese3c4acf2014-11-13 14:27:25 -080023 FILE* fp = setmntent("/proc/mounts", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -070024 ASSERT_TRUE(fp != nullptr);
Elliott Hugheseb664e22014-05-13 10:44:07 -070025
Elliott Hughes1e393b02022-05-13 16:06:54 -070026 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 Hugheseb664e22014-05-13 10:44:07 -070033
Elliott Hughes1e393b02022-05-13 16:06:54 -070034 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 Hughese3c4acf2014-11-13 14:27:25 -080039
40 struct mntent entry;
41 char buf[BUFSIZ];
Elliott Hughes1e393b02022-05-13 16:06:54 -070042 size_t i = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -070043 while (getmntent_r(fp, &entry, buf, sizeof(buf)) != nullptr) {
Elliott Hughes1e393b02022-05-13 16:06:54 -070044 ASSERT_EQ(fsnames[i], entry.mnt_fsname);
45 ASSERT_EQ(dirs[i], entry.mnt_dir);
46 i++;
Elliott Hughese3c4acf2014-11-13 14:27:25 -080047 }
48
Elliott Hugheseb664e22014-05-13 10:44:07 -070049 ASSERT_EQ(1, endmntent(fp));
Elliott Hughes1e393b02022-05-13 16:06:54 -070050
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 Hugheseb664e22014-05-13 10:44:07 -070056}
Greg Hackmann3679ade2016-03-04 10:07:35 -080057
58TEST(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"};
Elliott Hughesc0ce06f2024-08-19 17:06:00 +000062 struct mntent ent = {.mnt_opts = mnt_opts};
Greg Hackmann3679ade2016-03-04 10:07:35 -080063
64 EXPECT_EQ(mnt_opts, hasmntopt(&ent, "aa"));
65 EXPECT_EQ(mnt_opts + 5, hasmntopt(&ent, "a"));
66 EXPECT_EQ(mnt_opts + 9, hasmntopt(&ent, "b"));
67 EXPECT_EQ(mnt_opts + 11, hasmntopt(&ent, "bb"));
68 EXPECT_EQ(mnt_opts + 14, hasmntopt(&ent, "c"));
69 EXPECT_EQ(nullptr, hasmntopt(&ent, "d"));
70 EXPECT_EQ(nullptr, hasmntopt(&ent, "e"));
71}
Elliott Hughesc0ce06f2024-08-19 17:06:00 +000072
73TEST(mntent, hasmntopt_no_suffix_match) {
74 char mnt_opts[]{"noatime"};
75 struct mntent ent = {.mnt_opts = mnt_opts};
76 EXPECT_EQ(nullptr, hasmntopt(&ent, "atime"));
77}