xref: /aosp_15_r20/system/libbase/mapped_file_test.cpp (revision 8f0ba417480079999ba552f1087ae592091b9d02)
1*8f0ba417SAndroid Build Coastguard Worker /*
2*8f0ba417SAndroid Build Coastguard Worker  * Copyright (C) 2018 The Android Open Source Project
3*8f0ba417SAndroid Build Coastguard Worker  *
4*8f0ba417SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*8f0ba417SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*8f0ba417SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*8f0ba417SAndroid Build Coastguard Worker  *
8*8f0ba417SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*8f0ba417SAndroid Build Coastguard Worker  *
10*8f0ba417SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*8f0ba417SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*8f0ba417SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8f0ba417SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*8f0ba417SAndroid Build Coastguard Worker  * limitations under the License.
15*8f0ba417SAndroid Build Coastguard Worker  */
16*8f0ba417SAndroid Build Coastguard Worker 
17*8f0ba417SAndroid Build Coastguard Worker #include "android-base/mapped_file.h"
18*8f0ba417SAndroid Build Coastguard Worker 
19*8f0ba417SAndroid Build Coastguard Worker #include <gtest/gtest.h>
20*8f0ba417SAndroid Build Coastguard Worker 
21*8f0ba417SAndroid Build Coastguard Worker #include <errno.h>
22*8f0ba417SAndroid Build Coastguard Worker #include <fcntl.h>
23*8f0ba417SAndroid Build Coastguard Worker #include <unistd.h>
24*8f0ba417SAndroid Build Coastguard Worker 
25*8f0ba417SAndroid Build Coastguard Worker #include <string>
26*8f0ba417SAndroid Build Coastguard Worker 
27*8f0ba417SAndroid Build Coastguard Worker #include "android-base/file.h"
28*8f0ba417SAndroid Build Coastguard Worker 
TEST(mapped_file,smoke)29*8f0ba417SAndroid Build Coastguard Worker TEST(mapped_file, smoke) {
30*8f0ba417SAndroid Build Coastguard Worker   TemporaryFile tf;
31*8f0ba417SAndroid Build Coastguard Worker   ASSERT_TRUE(tf.fd != -1);
32*8f0ba417SAndroid Build Coastguard Worker   ASSERT_TRUE(android::base::WriteStringToFd("hello world", tf.fd));
33*8f0ba417SAndroid Build Coastguard Worker 
34*8f0ba417SAndroid Build Coastguard Worker   auto m = android::base::MappedFile::FromFd(tf.fd, 3, 2, PROT_READ);
35*8f0ba417SAndroid Build Coastguard Worker   ASSERT_EQ(2u, m->size());
36*8f0ba417SAndroid Build Coastguard Worker   ASSERT_EQ('l', m->data()[0]);
37*8f0ba417SAndroid Build Coastguard Worker   ASSERT_EQ('o', m->data()[1]);
38*8f0ba417SAndroid Build Coastguard Worker }
39*8f0ba417SAndroid Build Coastguard Worker 
TEST(mapped_file,zero_length_mapping)40*8f0ba417SAndroid Build Coastguard Worker TEST(mapped_file, zero_length_mapping) {
41*8f0ba417SAndroid Build Coastguard Worker   // http://b/119818070 "app crashes when reading asset of zero length".
42*8f0ba417SAndroid Build Coastguard Worker   // mmap fails with EINVAL for a zero length region.
43*8f0ba417SAndroid Build Coastguard Worker   TemporaryFile tf;
44*8f0ba417SAndroid Build Coastguard Worker   ASSERT_TRUE(tf.fd != -1);
45*8f0ba417SAndroid Build Coastguard Worker 
46*8f0ba417SAndroid Build Coastguard Worker   auto m = android::base::MappedFile::FromFd(tf.fd, 4096, 0, PROT_READ);
47*8f0ba417SAndroid Build Coastguard Worker   EXPECT_EQ(0u, m->size());
48*8f0ba417SAndroid Build Coastguard Worker   EXPECT_NE(nullptr, m->data());
49*8f0ba417SAndroid Build Coastguard Worker }
50