File: MemoryFileTest.cpp

package info (click to toggle)
android-platform-system-core 1%3A10.0.0%2Br36-7
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 123,760 kB
  • sloc: cpp: 197,034; ansic: 18,211; asm: 3,606; sh: 3,180; python: 2,671; java: 693; xml: 266; makefile: 237
file content (276 lines) | stat: -rw-r--r-- 9,301 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <string>
#include <vector>

#include <android-base/test_utils.h>
#include <android-base/file.h>
#include <gtest/gtest.h>

#include <unwindstack/Memory.h>

namespace unwindstack {

class MemoryFileTest : public ::testing::Test {
 protected:
  void SetUp() override {
    tf_ = new TemporaryFile;
  }

  void TearDown() override {
    delete tf_;
  }

  void WriteTestData() {
    ASSERT_TRUE(android::base::WriteStringToFd("0123456789abcdefghijklmnopqrstuvxyz", tf_->fd));
  }

  MemoryFileAtOffset memory_;

  TemporaryFile* tf_ = nullptr;
};

TEST_F(MemoryFileTest, init_offset_0) {
  WriteTestData();

  ASSERT_TRUE(memory_.Init(tf_->path, 0));
  std::vector<char> buffer(11);
  ASSERT_TRUE(memory_.ReadFully(0, buffer.data(), 10));
  buffer[10] = '\0';
  ASSERT_STREQ("0123456789", buffer.data());
}

TEST_F(MemoryFileTest, init_offset_non_zero) {
  WriteTestData();

  ASSERT_TRUE(memory_.Init(tf_->path, 10));
  std::vector<char> buffer(11);
  ASSERT_TRUE(memory_.ReadFully(0, buffer.data(), 10));
  buffer[10] = '\0';
  ASSERT_STREQ("abcdefghij", buffer.data());
}

TEST_F(MemoryFileTest, init_offset_non_zero_larger_than_pagesize) {
  size_t pagesize = getpagesize();
  std::string large_string;
  for (size_t i = 0; i < pagesize; i++) {
    large_string += '1';
  }
  large_string += "012345678901234abcdefgh";
  ASSERT_TRUE(android::base::WriteStringToFd(large_string, tf_->fd));

  ASSERT_TRUE(memory_.Init(tf_->path, pagesize + 15));
  std::vector<char> buffer(9);
  ASSERT_TRUE(memory_.ReadFully(0, buffer.data(), 8));
  buffer[8] = '\0';
  ASSERT_STREQ("abcdefgh", buffer.data());
}

TEST_F(MemoryFileTest, init_offset_pagesize_aligned) {
  size_t pagesize = getpagesize();
  std::string data;
  for (size_t i = 0; i < 2 * pagesize; i++) {
    data += static_cast<char>((i / pagesize) + '0');
    data += static_cast<char>((i % 10) + '0');
  }
  ASSERT_TRUE(android::base::WriteStringToFd(data, tf_->fd));

  ASSERT_TRUE(memory_.Init(tf_->path, 2 * pagesize));
  std::vector<char> buffer(11);
  ASSERT_TRUE(memory_.ReadFully(0, buffer.data(), 10));
  buffer[10] = '\0';
  std::string expected_str;
  for (size_t i = 0; i < 5; i++) {
    expected_str += '1';
    expected_str += static_cast<char>(((i + pagesize) % 10) + '0');
  }
  ASSERT_STREQ(expected_str.c_str(), buffer.data());
}

TEST_F(MemoryFileTest, init_offset_pagesize_aligned_plus_extra) {
  size_t pagesize = getpagesize();
  std::string data;
  for (size_t i = 0; i < 2 * pagesize; i++) {
    data += static_cast<char>((i / pagesize) + '0');
    data += static_cast<char>((i % 10) + '0');
  }
  ASSERT_TRUE(android::base::WriteStringToFd(data, tf_->fd));

  ASSERT_TRUE(memory_.Init(tf_->path, 2 * pagesize + 10));
  std::vector<char> buffer(11);
  ASSERT_TRUE(memory_.ReadFully(0, buffer.data(), 10));
  buffer[10] = '\0';
  std::string expected_str;
  for (size_t i = 0; i < 5; i++) {
    expected_str += '1';
    expected_str += static_cast<char>(((i + pagesize + 5) % 10) + '0');
  }
  ASSERT_STREQ(expected_str.c_str(), buffer.data());
}

TEST_F(MemoryFileTest, init_offset_greater_than_filesize) {
  size_t pagesize = getpagesize();
  std::string data;
  uint64_t file_size = 2 * pagesize + pagesize / 2;
  for (size_t i = 0; i < file_size; i++) {
    data += static_cast<char>((i / pagesize) + '0');
  }
  ASSERT_TRUE(android::base::WriteStringToFd(data, tf_->fd));

  // Check offset > file size fails and aligned_offset > file size.
  ASSERT_FALSE(memory_.Init(tf_->path, file_size + 2 * pagesize));
  // Check offset == filesize fails.
  ASSERT_FALSE(memory_.Init(tf_->path, file_size));
  // Check aligned_offset < filesize, but offset > filesize fails.
  ASSERT_FALSE(memory_.Init(tf_->path, 2 * pagesize + pagesize / 2 + pagesize / 4));
}

TEST_F(MemoryFileTest, read_error) {
  std::string data;
  for (size_t i = 0; i < 5000; i++) {
    data += static_cast<char>((i % 10) + '0');
  }
  ASSERT_TRUE(android::base::WriteStringToFd(data, tf_->fd));

  std::vector<char> buffer(100);

  // Read before init.
  ASSERT_FALSE(memory_.ReadFully(0, buffer.data(), 10));

  ASSERT_TRUE(memory_.Init(tf_->path, 0));

  ASSERT_FALSE(memory_.ReadFully(10000, buffer.data(), 10));
  ASSERT_FALSE(memory_.ReadFully(5000, buffer.data(), 10));
  ASSERT_FALSE(memory_.ReadFully(4990, buffer.data(), 11));
  ASSERT_TRUE(memory_.ReadFully(4990, buffer.data(), 10));
  ASSERT_FALSE(memory_.ReadFully(4999, buffer.data(), 2));
  ASSERT_TRUE(memory_.ReadFully(4999, buffer.data(), 1));

  // Check that overflow fails properly.
  ASSERT_FALSE(memory_.ReadFully(UINT64_MAX - 100, buffer.data(), 200));
}

TEST_F(MemoryFileTest, read_past_file_within_mapping) {
  size_t pagesize = getpagesize();

  ASSERT_TRUE(pagesize > 100);
  std::vector<uint8_t> buffer(pagesize - 100);
  for (size_t i = 0; i < pagesize - 100; i++) {
    buffer[i] = static_cast<uint8_t>((i % 0x5e) + 0x20);
  }
  ASSERT_TRUE(android::base::WriteFully(tf_->fd, buffer.data(), buffer.size()));

  ASSERT_TRUE(memory_.Init(tf_->path, 0));

  for (size_t i = 0; i < 100; i++) {
    uint8_t value;
    ASSERT_FALSE(memory_.ReadFully(buffer.size() + i, &value, 1))
        << "Should have failed at value " << i;
  }
}

TEST_F(MemoryFileTest, map_partial_offset_aligned) {
  size_t pagesize = getpagesize();
  std::vector<uint8_t> buffer(pagesize * 10);
  for (size_t i = 0; i < pagesize * 10; i++) {
    buffer[i] = i / pagesize + 1;
  }
  ASSERT_TRUE(android::base::WriteFully(tf_->fd, buffer.data(), buffer.size()));

  // Map in only two pages of the data, and after the first page.
  ASSERT_TRUE(memory_.Init(tf_->path, pagesize, pagesize * 2));

  std::vector<uint8_t> read_buffer(pagesize * 2);
  // Make sure that reading after mapped data is a failure.
  ASSERT_FALSE(memory_.ReadFully(pagesize * 2, read_buffer.data(), 1));
  ASSERT_TRUE(memory_.ReadFully(0, read_buffer.data(), pagesize * 2));
  for (size_t i = 0; i < pagesize; i++) {
    ASSERT_EQ(2, read_buffer[i]) << "Failed at byte " << i;
  }
  for (size_t i = pagesize; i < pagesize * 2; i++) {
    ASSERT_EQ(3, read_buffer[i]) << "Failed at byte " << i;
  }
}

TEST_F(MemoryFileTest, map_partial_offset_unaligned) {
  size_t pagesize = getpagesize();
  ASSERT_TRUE(pagesize > 0x100);
  std::vector<uint8_t> buffer(pagesize * 10);
  for (size_t i = 0; i < buffer.size(); i++) {
    buffer[i] = i / pagesize + 1;
  }
  ASSERT_TRUE(android::base::WriteFully(tf_->fd, buffer.data(), buffer.size()));

  // Map in only two pages of the data, and after the first page.
  ASSERT_TRUE(memory_.Init(tf_->path, pagesize + 0x100, pagesize * 2));

  std::vector<uint8_t> read_buffer(pagesize * 2);
  // Make sure that reading after mapped data is a failure.
  ASSERT_FALSE(memory_.ReadFully(pagesize * 2, read_buffer.data(), 1));
  ASSERT_TRUE(memory_.ReadFully(0, read_buffer.data(), pagesize * 2));
  for (size_t i = 0; i < pagesize - 0x100; i++) {
    ASSERT_EQ(2, read_buffer[i]) << "Failed at byte " << i;
  }
  for (size_t i = pagesize - 0x100; i < 2 * pagesize - 0x100; i++) {
    ASSERT_EQ(3, read_buffer[i]) << "Failed at byte " << i;
  }
  for (size_t i = 2 * pagesize - 0x100; i < pagesize * 2; i++) {
    ASSERT_EQ(4, read_buffer[i]) << "Failed at byte " << i;
  }
}

TEST_F(MemoryFileTest, map_overflow) {
  size_t pagesize = getpagesize();
  ASSERT_TRUE(pagesize > 0x100);
  std::vector<uint8_t> buffer(pagesize * 10);
  for (size_t i = 0; i < buffer.size(); i++) {
    buffer[i] = i / pagesize + 1;
  }
  ASSERT_TRUE(android::base::WriteFully(tf_->fd, buffer.data(), buffer.size()));

  // Map in only two pages of the data, and after the first page.
  ASSERT_TRUE(memory_.Init(tf_->path, pagesize + 0x100, UINT64_MAX));

  std::vector<uint8_t> read_buffer(pagesize * 10);
  ASSERT_FALSE(memory_.ReadFully(pagesize * 9 - 0x100 + 1, read_buffer.data(), 1));
  ASSERT_TRUE(memory_.ReadFully(0, read_buffer.data(), pagesize * 9 - 0x100));
}

TEST_F(MemoryFileTest, init_reinit) {
  size_t pagesize = getpagesize();
  std::vector<uint8_t> buffer(pagesize * 2);
  for (size_t i = 0; i < buffer.size(); i++) {
    buffer[i] = i / pagesize + 1;
  }
  ASSERT_TRUE(android::base::WriteFully(tf_->fd, buffer.data(), buffer.size()));

  ASSERT_TRUE(memory_.Init(tf_->path, 0));
  std::vector<uint8_t> read_buffer(buffer.size());
  ASSERT_TRUE(memory_.ReadFully(0, read_buffer.data(), pagesize));
  for (size_t i = 0; i < pagesize; i++) {
    ASSERT_EQ(1, read_buffer[i]) << "Failed at byte " << i;
  }

  // Now reinit.
  ASSERT_TRUE(memory_.Init(tf_->path, pagesize));
  ASSERT_TRUE(memory_.ReadFully(0, read_buffer.data(), pagesize));
  for (size_t i = 0; i < pagesize; i++) {
    ASSERT_EQ(2, read_buffer[i]) << "Failed at byte " << i;
  }
}

}  // namespace unwindstack