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
TEST(mntent,mntent_smoke)21 TEST(mntent, mntent_smoke) {
22 // Read all the entries with getmntent().
23 FILE* fp = setmntent("/proc/mounts", "r");
24 ASSERT_TRUE(fp != nullptr);
25
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 }
33
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);
39
40 struct mntent entry;
41 char buf[BUFSIZ];
42 size_t i = 0;
43 while (getmntent_r(fp, &entry, buf, sizeof(buf)) != nullptr) {
44 ASSERT_EQ(fsnames[i], entry.mnt_fsname);
45 ASSERT_EQ(dirs[i], entry.mnt_dir);
46 i++;
47 }
48
49 ASSERT_EQ(1, endmntent(fp));
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]);
56 }
57
TEST(mntent,hasmntopt)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 }
74