File: file_test.cpp

package info (click to toggle)
android-platform-tools 35.0.2-1~exp6
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 211,716 kB
  • sloc: cpp: 995,749; java: 290,495; ansic: 145,647; xml: 58,531; python: 39,608; sh: 14,500; javascript: 5,198; asm: 4,866; makefile: 3,115; yacc: 769; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (431 lines) | stat: -rw-r--r-- 13,568 bytes parent folder | download | duplicates (3)
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*
 * Copyright (C) 2013 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 "android-base/file.h"

#include "android-base/utf8.h"

#include <gtest/gtest.h>

#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <wchar.h>

#include <string>

#if !defined(_WIN32)
#include <pwd.h>
#else
#include <windows.h>
#endif

#include "android-base/logging.h"  // and must be after windows.h for ERROR

TEST(file, ReadFileToString_ENOENT) {
  std::string s("hello");
  errno = 0;
  ASSERT_FALSE(android::base::ReadFileToString("/proc/does-not-exist", &s));
  EXPECT_EQ(ENOENT, errno);
  EXPECT_EQ("", s);  // s was cleared.
}

TEST(file, ReadFileToString_WriteStringToFile) {
  TemporaryFile tf;
  ASSERT_NE(tf.fd, -1) << tf.path;
  ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.path))
    << strerror(errno);
  std::string s;
  ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s))
    << strerror(errno);
  EXPECT_EQ("abc", s);
}

// symlinks require elevated privileges on Windows.
#if !defined(_WIN32)
TEST(file, ReadFileToString_WriteStringToFile_symlink) {
  TemporaryFile target, link;
  ASSERT_EQ(0, unlink(link.path));
  ASSERT_EQ(0, symlink(target.path, link.path));
  ASSERT_FALSE(android::base::WriteStringToFile("foo", link.path, false));
  ASSERT_EQ(ELOOP, errno);
  ASSERT_TRUE(android::base::WriteStringToFile("foo", link.path, true));

  std::string s;
  ASSERT_FALSE(android::base::ReadFileToString(link.path, &s));
  ASSERT_EQ(ELOOP, errno);
  ASSERT_TRUE(android::base::ReadFileToString(link.path, &s, true));
  ASSERT_EQ("foo", s);
}
#endif

// WriteStringToFile2 is explicitly for setting Unix permissions, which make no
// sense on Windows.
#if !defined(_WIN32)
TEST(file, WriteStringToFile2) {
  TemporaryFile tf;
  ASSERT_NE(tf.fd, -1) << tf.path;
  ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.path, 0660,
                                               getuid(), getgid()))
      << strerror(errno);
  struct stat sb;
  ASSERT_EQ(0, stat(tf.path, &sb));
  ASSERT_EQ(0660U, static_cast<unsigned int>(sb.st_mode & ~S_IFMT));
  ASSERT_EQ(getuid(), sb.st_uid);
  ASSERT_EQ(getgid(), sb.st_gid);
  std::string s;
  ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s))
    << strerror(errno);
  EXPECT_EQ("abc", s);
}
#endif

#if defined(_WIN32)
TEST(file, NonUnicodeCharsWindows) {
  constexpr auto kMaxEnvVariableValueSize = 32767;
  std::wstring old_tmp;
  old_tmp.resize(kMaxEnvVariableValueSize);
  old_tmp.resize(GetEnvironmentVariableW(L"TMP", old_tmp.data(), old_tmp.size()));
  if (old_tmp.empty()) {
    // Can't continue with empty TMP folder.
    return;
  }

  std::wstring new_tmp = old_tmp;
  if (new_tmp.back() != L'\\') {
    new_tmp.push_back(L'\\');
  }

  {
    auto path(new_tmp + L"锦绣成都\\");
    _wmkdir(path.c_str());
    ASSERT_TRUE(SetEnvironmentVariableW(L"TMP", path.c_str()));

    TemporaryFile tf;
    ASSERT_NE(tf.fd, -1) << tf.path;
    ASSERT_TRUE(android::base::WriteStringToFd("abc", tf.fd));

    ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno);

    std::string s;
    ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)) << strerror(errno);
    EXPECT_EQ("abc", s);
  }
  {
    auto path(new_tmp + L"директория с длинным именем\\");
    _wmkdir(path.c_str());
    ASSERT_TRUE(SetEnvironmentVariableW(L"TMP", path.c_str()));

    TemporaryFile tf;
    ASSERT_NE(tf.fd, -1) << tf.path;
    ASSERT_TRUE(android::base::WriteStringToFd("abc", tf.fd));

    ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno);

    std::string s;
    ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)) << strerror(errno);
    EXPECT_EQ("abc", s);
  }
  {
    auto path(new_tmp + L"äüöß weiß\\");
    _wmkdir(path.c_str());
    ASSERT_TRUE(SetEnvironmentVariableW(L"TMP", path.c_str()));

    TemporaryFile tf;
    ASSERT_NE(tf.fd, -1) << tf.path;
    ASSERT_TRUE(android::base::WriteStringToFd("abc", tf.fd));

    ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno);

    std::string s;
    ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)) << strerror(errno);
    EXPECT_EQ("abc", s);
  }

  SetEnvironmentVariableW(L"TMP", old_tmp.c_str());
}

TEST(file, RootDirectoryWindows) {
  constexpr auto kMaxEnvVariableValueSize = 32767;
  std::wstring old_tmp;
  bool tmp_is_empty = false;
  old_tmp.resize(kMaxEnvVariableValueSize);
  old_tmp.resize(GetEnvironmentVariableW(L"TMP", old_tmp.data(), old_tmp.size()));
  if (old_tmp.empty()) {
    tmp_is_empty = (GetLastError() == ERROR_ENVVAR_NOT_FOUND);
  }
  SetEnvironmentVariableW(L"TMP", L"C:");

  TemporaryFile tf;
  ASSERT_NE(tf.fd, -1) << tf.path;

  SetEnvironmentVariableW(L"TMP", tmp_is_empty ? nullptr : old_tmp.c_str());
}
#endif

TEST(file, WriteStringToFd_StringLiteral) {
  TemporaryFile tf;
  ASSERT_NE(tf.fd, -1) << tf.path;
  ASSERT_TRUE(android::base::WriteStringToFd("abc", tf.fd));

  ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno);

  std::string s;
  ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)) << strerror(errno);
  EXPECT_EQ("abc", s);
}

TEST(file, WriteStringToFd_String) {
  std::string testStr = "def";
  TemporaryFile tf;
  ASSERT_NE(tf.fd, -1) << tf.path;
  ASSERT_TRUE(android::base::WriteStringToFd(testStr, tf.fd));

  ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno);

  std::string s;
  ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)) << strerror(errno);
  EXPECT_EQ(testStr, s);
}

TEST(file, WriteStringToFd_StringView) {
  std::string_view testStrView = "ghi";
  TemporaryFile tf;
  ASSERT_NE(tf.fd, -1) << tf.path;
  ASSERT_TRUE(android::base::WriteStringToFd(testStrView, tf.fd));

  ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno);

  std::string s;
  ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)) << strerror(errno);
  EXPECT_EQ(testStrView, s);
}

TEST(file, WriteFully) {
  TemporaryFile tf;
  ASSERT_NE(tf.fd, -1) << tf.path;
  ASSERT_TRUE(android::base::WriteFully(tf.fd, "abc", 3));

  ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno);

  std::string s;
  s.resize(3);
  ASSERT_TRUE(android::base::ReadFully(tf.fd, &s[0], s.size()))
    << strerror(errno);
  EXPECT_EQ("abc", s);

  ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno);

  s.resize(1024);
  ASSERT_FALSE(android::base::ReadFully(tf.fd, &s[0], s.size()));
}

TEST(file, RemoveFileIfExists) {
  TemporaryFile tf;
  ASSERT_NE(tf.fd, -1) << tf.path;
  close(tf.fd);
  tf.fd = -1;
  std::string err;
  ASSERT_TRUE(android::base::RemoveFileIfExists(tf.path, &err)) << err;
  ASSERT_TRUE(android::base::RemoveFileIfExists(tf.path));
  TemporaryDir td;
  ASSERT_FALSE(android::base::RemoveFileIfExists(td.path));
  ASSERT_FALSE(android::base::RemoveFileIfExists(td.path, &err));
  ASSERT_EQ("is not a regular file or symbolic link", err);
}

TEST(file, RemoveFileIfExists_ENOTDIR) {
  TemporaryFile tf;
  close(tf.fd);
  tf.fd = -1;
  std::string err{"xxx"};
  ASSERT_TRUE(android::base::RemoveFileIfExists(std::string{tf.path} + "/abc", &err));
  ASSERT_EQ("xxx", err);
}

#if !defined(_WIN32)
TEST(file, RemoveFileIfExists_EACCES) {
  // EACCES -- one of the directories in the path has no search permission
  // root can bypass permission restrictions, so drop root.
  if (getuid() == 0) {
    passwd* shell = getpwnam("shell");
    setgid(shell->pw_gid);
    setuid(shell->pw_uid);
  }

  TemporaryDir td;
  TemporaryFile tf(td.path);
  close(tf.fd);
  tf.fd = -1;
  std::string err{"xxx"};
  // Remove dir's search permission.
  ASSERT_TRUE(chmod(td.path, S_IRUSR | S_IWUSR) == 0);
  ASSERT_FALSE(android::base::RemoveFileIfExists(tf.path, &err));
  ASSERT_EQ("Permission denied", err);
  // Set dir's search permission again.
  ASSERT_TRUE(chmod(td.path, S_IRWXU) == 0);
  ASSERT_TRUE(android::base::RemoveFileIfExists(tf.path, &err));
}
#endif

TEST(file, Readlink) {
#if !defined(_WIN32)
  // Linux doesn't allow empty symbolic links.
  std::string min("x");
  // ext2 and ext4 both have PAGE_SIZE limits.
  // If file encryption is enabled, there's extra overhead to store the
  // size of the encrypted symlink target. There's also an off-by-one
  // in current kernels (and marlin/sailfish where we're seeing this
  // failure are still on 3.18, far from current). http://b/33306057.
  std::string max(static_cast<size_t>(4096 - 2 - 1 - 1), 'x');

  TemporaryDir td;
  std::string min_path{std::string(td.path) + "/" + "min"};
  std::string max_path{std::string(td.path) + "/" + "max"};

  ASSERT_EQ(0, symlink(min.c_str(), min_path.c_str()));
  ASSERT_EQ(0, symlink(max.c_str(), max_path.c_str()));

  std::string result;

  result = "wrong";
  ASSERT_TRUE(android::base::Readlink(min_path, &result));
  ASSERT_EQ(min, result);

  result = "wrong";
  ASSERT_TRUE(android::base::Readlink(max_path, &result));
  ASSERT_EQ(max, result);
#endif
}

TEST(file, Realpath) {
#if !defined(_WIN32)
  TemporaryDir td;
  std::string basename = android::base::Basename(td.path);
  std::string dir_name = android::base::Dirname(td.path);
  std::string base_dir_name = android::base::Basename(dir_name);

  {
    std::string path = dir_name + "/../" + base_dir_name + "/" + basename;
    std::string result;
    ASSERT_TRUE(android::base::Realpath(path, &result));
    ASSERT_EQ(td.path, result);
  }

  {
    std::string path = std::string(td.path) + "/..";
    std::string result;
    ASSERT_TRUE(android::base::Realpath(path, &result));
    ASSERT_EQ(dir_name, result);
  }

  {
    errno = 0;
    std::string path = std::string(td.path) + "/foo.noent";
    std::string result = "wrong";
    ASSERT_TRUE(!android::base::Realpath(path, &result));
    ASSERT_TRUE(result.empty());
    ASSERT_EQ(ENOENT, errno);
  }
#endif
}

TEST(file, GetExecutableDirectory) {
  std::string path = android::base::GetExecutableDirectory();
  ASSERT_NE("", path);
  ASSERT_NE(android::base::GetExecutablePath(), path);
  ASSERT_EQ('/', path[0]);
  ASSERT_NE('/', path[path.size() - 1]);
}

TEST(file, GetExecutablePath) {
  ASSERT_NE("", android::base::GetExecutablePath());
}

TEST(file, Basename) {
  EXPECT_EQ("sh", android::base::Basename("/system/bin/sh"));
  EXPECT_EQ("sh", android::base::Basename("sh"));
  EXPECT_EQ("sh", android::base::Basename("/system/bin/sh/"));

  // Since we've copy & pasted bionic's implementation, copy & paste the tests.
  EXPECT_EQ(".",   android::base::Basename(""));
  EXPECT_EQ("lib", android::base::Basename("/usr/lib"));
  EXPECT_EQ("usr", android::base::Basename("/usr/"));
  EXPECT_EQ("usr", android::base::Basename("usr"));
  EXPECT_EQ("/",   android::base::Basename("/"));
  EXPECT_EQ(".",   android::base::Basename("."));
  EXPECT_EQ("..",  android::base::Basename(".."));
  EXPECT_EQ("/",   android::base::Basename("///"));
  EXPECT_EQ("lib", android::base::Basename("//usr//lib//"));
}

TEST(file, Dirname) {
  EXPECT_EQ("/system/bin", android::base::Dirname("/system/bin/sh"));
  EXPECT_EQ(".", android::base::Dirname("sh"));
  EXPECT_EQ("/system/bin", android::base::Dirname("/system/bin/sh/"));

  // Since we've copy & pasted bionic's implementation, copy & paste the tests.
  EXPECT_EQ(".", android::base::Dirname(""));
  EXPECT_EQ("/usr", android::base::Dirname("/usr/lib"));
  EXPECT_EQ("/", android::base::Dirname("/usr/"));
  EXPECT_EQ(".", android::base::Dirname("usr"));
  EXPECT_EQ(".", android::base::Dirname("."));
  EXPECT_EQ(".", android::base::Dirname(".."));
  EXPECT_EQ("/", android::base::Dirname("/"));
}

TEST(file, ReadFileToString_capacity) {
  TemporaryFile tf;
  ASSERT_NE(tf.fd, -1) << tf.path;

  // For a huge file, the overhead should still be small.
  std::string s;
  size_t size = 16 * 1024 * 1024;
  ASSERT_TRUE(android::base::WriteStringToFile(std::string(size, 'x'), tf.path));
  ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s));
  EXPECT_EQ(size, s.size());
  EXPECT_LT(s.capacity(), size + 16);

  // Even for weird badly-aligned sizes.
  size += 12345;
  ASSERT_TRUE(android::base::WriteStringToFile(std::string(size, 'x'), tf.path));
  ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s));
  EXPECT_EQ(size, s.size());
  EXPECT_LT(s.capacity(), size + 16);

  // We'll shrink an enormous string if you read a small file into it.
  size = 64;
  ASSERT_TRUE(android::base::WriteStringToFile(std::string(size, 'x'), tf.path));
  ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s));
  EXPECT_EQ(size, s.size());
  EXPECT_LT(s.capacity(), size + 16);
}

TEST(file, ReadFileToString_capacity_0) {
  TemporaryFile tf;
  ASSERT_NE(tf.fd, -1) << tf.path;

  // Because /proc reports its files as zero-length, we don't actually trust
  // any file that claims to be zero-length. Rather than add increasingly
  // complex heuristics for shrinking the passed-in string in that case, we
  // currently leave it alone.
  std::string s;
  size_t initial_capacity = s.capacity();
  ASSERT_TRUE(android::base::WriteStringToFile("", tf.path));
  ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s));
  EXPECT_EQ(0U, s.size());
  EXPECT_EQ(initial_capacity, s.capacity());
}