xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/allocation_test.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
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     http://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 "tensorflow/lite/allocation.h"
16 
17 #if defined(__linux__)
18 #include <fcntl.h>
19 #endif
20 
21 #include <sys/stat.h>
22 
23 #include <string>
24 
25 #include <gtest/gtest.h>
26 #include "tensorflow/lite/testing/util.h"
27 
28 namespace tflite {
29 
TEST(MMAPAllocation,TestInvalidFile)30 TEST(MMAPAllocation, TestInvalidFile) {
31   if (!MMAPAllocation::IsSupported()) {
32     return;
33   }
34 
35   TestErrorReporter error_reporter;
36   MMAPAllocation allocation("/tmp/tflite_model_1234", &error_reporter);
37   EXPECT_FALSE(allocation.valid());
38 }
39 
TEST(MMAPAllocation,TestValidFile)40 TEST(MMAPAllocation, TestValidFile) {
41   if (!MMAPAllocation::IsSupported()) {
42     return;
43   }
44 
45   TestErrorReporter error_reporter;
46   MMAPAllocation allocation(
47       "tensorflow/lite/testdata/empty_model.bin", &error_reporter);
48 
49   ASSERT_TRUE(allocation.valid());
50   EXPECT_GT(allocation.fd(), 0);
51   EXPECT_GT(allocation.bytes(), 0);
52   EXPECT_NE(allocation.base(), nullptr);
53 }
54 
55 #if defined(__linux__)
TEST(MMAPAllocation,TestInvalidFileDescriptor)56 TEST(MMAPAllocation, TestInvalidFileDescriptor) {
57   if (!MMAPAllocation::IsSupported()) {
58     return;
59   }
60 
61   TestErrorReporter error_reporter;
62   MMAPAllocation allocation(-1, &error_reporter);
63   EXPECT_FALSE(allocation.valid());
64 }
65 
TEST(MMAPAllocation,TestInvalidSizeAndOffset)66 TEST(MMAPAllocation, TestInvalidSizeAndOffset) {
67   if (!MMAPAllocation::IsSupported()) {
68     return;
69   }
70 
71   int fd =
72       open("tensorflow/lite/testdata/empty_model.bin", O_RDONLY);
73   ASSERT_GT(fd, 0);
74 
75   struct stat fd_stat;
76   ASSERT_EQ(fstat(fd, &fd_stat), 0);
77 
78   size_t file_size = fd_stat.st_size;
79 
80   TestErrorReporter error_reporter;
81   MMAPAllocation allocation_invalid_offset(fd, /*offset=*/file_size + 100,
82                                            /*length=*/1, &error_reporter);
83   EXPECT_FALSE(allocation_invalid_offset.valid());
84 
85   MMAPAllocation allocation_invalid_length(fd, /*offset=*/0, /*length=*/0,
86                                            &error_reporter);
87   EXPECT_FALSE(allocation_invalid_length.valid());
88 
89   MMAPAllocation allocation_excessive_length(fd, /*offset=*/0, /*length=*/0,
90                                              &error_reporter);
91   EXPECT_FALSE(allocation_excessive_length.valid());
92 
93   MMAPAllocation allocation_excessive_length_with_offset(
94       fd, /*offset=*/10, /*length=*/file_size, &error_reporter);
95   EXPECT_FALSE(allocation_excessive_length_with_offset.valid());
96 
97   close(fd);
98 }
99 
TEST(MMAPAllocation,TestValidFileDescriptor)100 TEST(MMAPAllocation, TestValidFileDescriptor) {
101   if (!MMAPAllocation::IsSupported()) {
102     return;
103   }
104 
105   int fd =
106       open("tensorflow/lite/testdata/empty_model.bin", O_RDONLY);
107   ASSERT_GT(fd, 0);
108 
109   TestErrorReporter error_reporter;
110   MMAPAllocation allocation(fd, &error_reporter);
111   EXPECT_TRUE(allocation.valid());
112   EXPECT_GT(allocation.fd(), 0);
113   EXPECT_GT(allocation.bytes(), 0);
114   EXPECT_NE(allocation.base(), nullptr);
115 
116   close(fd);
117 }
118 
TEST(MMAPAllocation,TestValidFileDescriptorWithOffset)119 TEST(MMAPAllocation, TestValidFileDescriptorWithOffset) {
120   if (!MMAPAllocation::IsSupported()) {
121     return;
122   }
123 
124   int fd =
125       open("tensorflow/lite/testdata/empty_model.bin", O_RDONLY);
126   ASSERT_GT(fd, 0);
127 
128   struct stat fd_stat;
129   ASSERT_EQ(fstat(fd, &fd_stat), 0);
130   size_t file_size = fd_stat.st_size;
131 
132   TestErrorReporter error_reporter;
133   MMAPAllocation allocation(fd, /*offset=*/10, /*length=*/file_size - 10,
134                             &error_reporter);
135   EXPECT_TRUE(allocation.valid());
136   EXPECT_GT(allocation.fd(), 0);
137   EXPECT_GT(allocation.bytes(), 0);
138   EXPECT_NE(allocation.base(), nullptr);
139 
140   close(fd);
141 }
142 #endif  // defined(__linux__)
143 
144 }  // namespace tflite
145