xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/sandbox2/util/maps_parser_test.cc (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "sandboxed_api/sandbox2/util/maps_parser.h"
16 
17 #include <vector>
18 
19 #include "gmock/gmock.h"
20 #include "gtest/gtest.h"
21 #include "absl/status/statusor.h"
22 #include "sandboxed_api/util/status_matchers.h"
23 
24 namespace sandbox2 {
25 namespace {
26 
27 using ::sapi::IsOk;
28 using ::testing::Eq;
29 using ::testing::Not;
30 using ::testing::Test;
31 
TEST(MapsParserTest,ParsesValidFileCorrectly)32 TEST(MapsParserTest, ParsesValidFileCorrectly) {
33   static constexpr char kValidMapsFile[] = R"ValidMapsFile(
34 555555554000-55555555c000 r-xp 00000000 fd:01 3277961                    /bin/cat
35 55555575b000-55555575c000 r--p 00007000 fd:01 3277961                    /bin/cat
36 55555575c000-55555575d000 rw-p 00008000 fd:01 3277961                    /bin/cat
37 55555575d000-55555577e000 rw-p 00000000 00:00 0                          [heap]
38 7ffff7a3a000-7ffff7bcf000 r-xp 00000000 fd:01 916748                     /lib/x86_64-linux-gnu/libc-2.24.so
39 7ffff7bcf000-7ffff7dcf000 ---p 00195000 fd:01 916748                     /lib/x86_64-linux-gnu/libc-2.24.so
40 7ffff7dcf000-7ffff7dd3000 r--p 00195000 fd:01 916748                     /lib/x86_64-linux-gnu/libc-2.24.so
41 7ffff7dd3000-7ffff7dd5000 rw-p 00199000 fd:01 916748                     /lib/x86_64-linux-gnu/libc-2.24.so
42 7ffff7dd5000-7ffff7dd9000 rw-p 00000000 00:00 0
43 7ffff7dd9000-7ffff7dfc000 r-xp 00000000 fd:01 915984                     /lib/x86_64-linux-gnu/ld-2.24.so
44 7ffff7e2b000-7ffff7e7c000 r--p 00000000 fd:01 917362                     /usr/lib/locale/aa_DJ.utf8/LC_CTYPE
45 7ffff7e7c000-7ffff7fac000 r--p 00000000 fd:01 917355                     /usr/lib/locale/aa_DJ.utf8/LC_COLLATE
46 7ffff7fac000-7ffff7fae000 rw-p 00000000 00:00 0
47 7ffff7fc1000-7ffff7fe3000 rw-p 00000000 00:00 0
48 7ffff7fe3000-7ffff7fe4000 r--p 00000000 fd:01 920638                     /usr/lib/locale/aa_ET/LC_NUMERIC
49 7ffff7fe4000-7ffff7fe5000 r--p 00000000 fd:01 932780                     /usr/lib/locale/en_US.utf8/LC_TIME
50 7ffff7fe5000-7ffff7fe6000 r--p 00000000 fd:01 932409                     /usr/lib/locale/chr_US/LC_MONETARY
51 7ffff7fe6000-7ffff7fe7000 r--p 00000000 fd:01 932625                     /usr/lib/locale/en_AG/LC_MESSAGES/SYS_LC_MESSAGES
52 7ffff7fe7000-7ffff7fe8000 r--p 00000000 fd:01 932411                     /usr/lib/locale/chr_US/LC_PAPER
53 7ffff7fe8000-7ffff7fe9000 r--p 00000000 fd:01 932410                     /usr/lib/locale/chr_US/LC_NAME
54 7ffff7fe9000-7ffff7fea000 r--p 00000000 fd:01 932778                     /usr/lib/locale/en_US.utf8/LC_ADDRESS
55 7ffff7fea000-7ffff7feb000 r--p 00000000 fd:01 932412                     /usr/lib/locale/chr_US/LC_TELEPHONE
56 7ffff7feb000-7ffff7fec000 r--p 00000000 fd:01 932407                     /usr/lib/locale/chr_US/LC_MEASUREMENT
57 7ffff7fec000-7ffff7ff3000 r--s 00000000 fd:01 1179918                    /usr/lib/x86_64-linux-gnu/gconv/gconv-modules.cache
58 7ffff7ff3000-7ffff7ff4000 r--p 00000000 fd:01 932779                     /usr/lib/locale/en_US.utf8/LC_IDENTIFICATION
59 7ffff7ff4000-7ffff7ff7000 rw-p 00000000 00:00 0
60 7ffff7ff7000-7ffff7ffa000 r--p 00000000 00:00 0                          [vvar]
61 7ffff7ffa000-7ffff7ffc000 r-xp 00000000 00:00 0                          [vdso]
62 7ffff7ffc000-7ffff7ffd000 r--p 00023000 fd:01 915984                     /lib/x86_64-linux-gnu/ld-2.24.so
63 7ffff7ffd000-7ffff7ffe000 rw-p 00024000 fd:01 915984                     /lib/x86_64-linux-gnu/ld-2.24.so
64 7ffff7ffe000-7ffff7fff000 rw-p 00000000 00:00 0
65 7ffffffde000-7ffffffff000 rw-p 00000000 00:00 0                          [stack]
66 )ValidMapsFile";  // NOLINT
67   SAPI_ASSERT_OK_AND_ASSIGN(std::vector<MapsEntry> entries,
68                             ParseProcMaps(kValidMapsFile));
69   EXPECT_THAT(entries.size(), Eq(32));
70   EXPECT_THAT(entries[0].start, Eq(0x555555554000));
71   EXPECT_THAT(entries[1].start, Eq(0x55555575b000));
72   EXPECT_THAT(entries[1].end, Eq(0x55555575c000));
73   EXPECT_THAT(entries[1].inode, Eq(3277961));
74 
75   EXPECT_THAT(entries[0].is_executable, Eq(true));
76   EXPECT_THAT(entries[1].is_executable, Eq(false));
77 }
78 
TEST(MapsParserTest,FailsOnInvalidFile)79 TEST(MapsParserTest, FailsOnInvalidFile) {
80   static constexpr char kInvalidMapsFile[] = R"InvalidMapsFile(
81 555555554000-55555555c000 r-xp 00000000 fd:01 3277961                    /bin/cat
82 55555575b000-55555575c000 r--p 00007000 fd:01 3277961                    /bin/cat
83 55555575c000-55555575d000 rw-p 00008000 fd:01 3277961                    /bin/cat
84 55555575d000-55555577e000 rw-p 00000000 00:00 0                          [heap]
85 7ffff7fe4000+7ffff7fe5000 r--p 00000000 fdX01 932780                     /usr/lib/locale/en_US.utf8/LC_TIME
86 )InvalidMapsFile";
87   auto status_or = ParseProcMaps(kInvalidMapsFile);
88   ASSERT_THAT(status_or.status(), Not(IsOk()));
89 }
90 
91 }  // namespace
92 }  // namespace sandbox2
93