1*8d67ca89SAndroid Build Coastguard Worker /*
2*8d67ca89SAndroid Build Coastguard Worker * Copyright (C) 2013 The Android Open Source Project
3*8d67ca89SAndroid Build Coastguard Worker *
4*8d67ca89SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*8d67ca89SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*8d67ca89SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*8d67ca89SAndroid Build Coastguard Worker *
8*8d67ca89SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*8d67ca89SAndroid Build Coastguard Worker *
10*8d67ca89SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*8d67ca89SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*8d67ca89SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8d67ca89SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*8d67ca89SAndroid Build Coastguard Worker * limitations under the License.
15*8d67ca89SAndroid Build Coastguard Worker */
16*8d67ca89SAndroid Build Coastguard Worker
17*8d67ca89SAndroid Build Coastguard Worker #include <gtest/gtest.h>
18*8d67ca89SAndroid Build Coastguard Worker
19*8d67ca89SAndroid Build Coastguard Worker #include <mntent.h>
20*8d67ca89SAndroid Build Coastguard Worker
TEST(mntent,mntent_smoke)21*8d67ca89SAndroid Build Coastguard Worker TEST(mntent, mntent_smoke) {
22*8d67ca89SAndroid Build Coastguard Worker // Read all the entries with getmntent().
23*8d67ca89SAndroid Build Coastguard Worker FILE* fp = setmntent("/proc/mounts", "r");
24*8d67ca89SAndroid Build Coastguard Worker ASSERT_TRUE(fp != nullptr);
25*8d67ca89SAndroid Build Coastguard Worker
26*8d67ca89SAndroid Build Coastguard Worker std::vector<std::string> fsnames;
27*8d67ca89SAndroid Build Coastguard Worker std::vector<std::string> dirs;
28*8d67ca89SAndroid Build Coastguard Worker mntent* me;
29*8d67ca89SAndroid Build Coastguard Worker while ((me = getmntent(fp)) != nullptr) {
30*8d67ca89SAndroid Build Coastguard Worker fsnames.push_back(me->mnt_fsname);
31*8d67ca89SAndroid Build Coastguard Worker dirs.push_back(me->mnt_dir);
32*8d67ca89SAndroid Build Coastguard Worker }
33*8d67ca89SAndroid Build Coastguard Worker
34*8d67ca89SAndroid Build Coastguard Worker ASSERT_EQ(1, endmntent(fp));
35*8d67ca89SAndroid Build Coastguard Worker
36*8d67ca89SAndroid Build Coastguard Worker // Then again with getmntent_r(), checking they match.
37*8d67ca89SAndroid Build Coastguard Worker fp = setmntent("/proc/mounts", "r");
38*8d67ca89SAndroid Build Coastguard Worker ASSERT_TRUE(fp != nullptr);
39*8d67ca89SAndroid Build Coastguard Worker
40*8d67ca89SAndroid Build Coastguard Worker struct mntent entry;
41*8d67ca89SAndroid Build Coastguard Worker char buf[BUFSIZ];
42*8d67ca89SAndroid Build Coastguard Worker size_t i = 0;
43*8d67ca89SAndroid Build Coastguard Worker while (getmntent_r(fp, &entry, buf, sizeof(buf)) != nullptr) {
44*8d67ca89SAndroid Build Coastguard Worker ASSERT_EQ(fsnames[i], entry.mnt_fsname);
45*8d67ca89SAndroid Build Coastguard Worker ASSERT_EQ(dirs[i], entry.mnt_dir);
46*8d67ca89SAndroid Build Coastguard Worker i++;
47*8d67ca89SAndroid Build Coastguard Worker }
48*8d67ca89SAndroid Build Coastguard Worker
49*8d67ca89SAndroid Build Coastguard Worker ASSERT_EQ(1, endmntent(fp));
50*8d67ca89SAndroid Build Coastguard Worker
51*8d67ca89SAndroid Build Coastguard Worker // And just for good measure: we did see a /proc entry, right?
52*8d67ca89SAndroid Build Coastguard Worker auto it = std::find(fsnames.begin(), fsnames.end(), "proc");
53*8d67ca89SAndroid Build Coastguard Worker ASSERT_TRUE(it != fsnames.end());
54*8d67ca89SAndroid Build Coastguard Worker size_t proc_index = it - fsnames.begin();
55*8d67ca89SAndroid Build Coastguard Worker ASSERT_EQ("/proc", dirs[proc_index]);
56*8d67ca89SAndroid Build Coastguard Worker }
57*8d67ca89SAndroid Build Coastguard Worker
TEST(mntent,hasmntopt)58*8d67ca89SAndroid Build Coastguard Worker TEST(mntent, hasmntopt) {
59*8d67ca89SAndroid Build Coastguard Worker // indices 1 1
60*8d67ca89SAndroid Build Coastguard Worker // of keys: 0 5 9 1 4
61*8d67ca89SAndroid Build Coastguard Worker char mnt_opts[]{"aa=b,a=b,b,bb,c=d"};
62*8d67ca89SAndroid Build Coastguard Worker struct mntent ent = {.mnt_opts = mnt_opts};
63*8d67ca89SAndroid Build Coastguard Worker
64*8d67ca89SAndroid Build Coastguard Worker EXPECT_EQ(mnt_opts, hasmntopt(&ent, "aa"));
65*8d67ca89SAndroid Build Coastguard Worker EXPECT_EQ(mnt_opts + 5, hasmntopt(&ent, "a"));
66*8d67ca89SAndroid Build Coastguard Worker EXPECT_EQ(mnt_opts + 9, hasmntopt(&ent, "b"));
67*8d67ca89SAndroid Build Coastguard Worker EXPECT_EQ(mnt_opts + 11, hasmntopt(&ent, "bb"));
68*8d67ca89SAndroid Build Coastguard Worker EXPECT_EQ(mnt_opts + 14, hasmntopt(&ent, "c"));
69*8d67ca89SAndroid Build Coastguard Worker EXPECT_EQ(nullptr, hasmntopt(&ent, "d"));
70*8d67ca89SAndroid Build Coastguard Worker EXPECT_EQ(nullptr, hasmntopt(&ent, "e"));
71*8d67ca89SAndroid Build Coastguard Worker }
72*8d67ca89SAndroid Build Coastguard Worker
TEST(mntent,hasmntopt_no_suffix_match)73*8d67ca89SAndroid Build Coastguard Worker TEST(mntent, hasmntopt_no_suffix_match) {
74*8d67ca89SAndroid Build Coastguard Worker char mnt_opts[]{"noatime"};
75*8d67ca89SAndroid Build Coastguard Worker struct mntent ent = {.mnt_opts = mnt_opts};
76*8d67ca89SAndroid Build Coastguard Worker EXPECT_EQ(nullptr, hasmntopt(&ent, "atime"));
77*8d67ca89SAndroid Build Coastguard Worker }
78