forked from youtube/cobalt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_atomic_replace_test.cc
104 lines (83 loc) · 3.68 KB
/
file_atomic_replace_test.cc
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
// Copyright 2019 The Cobalt Authors. All Rights Reserved.
//
// 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 <fcntl.h>
#include <sys/stat.h>
#include "starboard/common/file.h"
#include "starboard/file.h"
#include "starboard/nplb/file_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace starboard {
namespace nplb {
namespace {
static const char kTestContents[] =
"The quick brown fox jumps over the lazy dog.";
static const int kTestContentsLength = sizeof(kTestContents);
bool CompareFileContentsToString(const char* filename,
const char* str,
int size) {
char result[kTestContentsLength] = {'\0'};
int file = open(filename, O_RDONLY, S_IRUSR | S_IWUSR);
EXPECT_TRUE(starboard::IsValid(file));
// We always try to read kTestContentsLength since the data will at most be
// this long. There are test cases where the number of bytes read will be
// less.
EXPECT_EQ(size, starboard::ReadAll(file, result, kTestContentsLength));
EXPECT_TRUE(!close(file));
return strncmp(str, result, kTestContentsLength) == 0;
}
bool FileExists(const char* path) {
struct stat info;
return stat(path, &info) == 0;
}
TEST(SbFileAtomicReplaceTest, ReplacesValidFile) {
ScopedRandomFile random_file(ScopedRandomFile::kDefaultLength,
ScopedRandomFile::kCreate);
const std::string& filename = random_file.filename();
EXPECT_TRUE(FileExists(filename.c_str()));
EXPECT_TRUE(SbFileAtomicReplace(filename.c_str(), kTestContents,
kTestContentsLength));
EXPECT_TRUE(CompareFileContentsToString(filename.c_str(), kTestContents,
kTestContentsLength));
}
TEST(SbFileAtomicReplaceTest, ReplacesNonExistentFile) {
ScopedRandomFile random_file(ScopedRandomFile::kDontCreate);
const std::string& filename = random_file.filename();
EXPECT_FALSE(FileExists(filename.c_str()));
EXPECT_TRUE(SbFileAtomicReplace(filename.c_str(), kTestContents,
kTestContentsLength));
EXPECT_TRUE(CompareFileContentsToString(filename.c_str(), kTestContents,
kTestContentsLength));
}
TEST(SbFileAtomicReplaceTest, ReplacesWithNoData) {
ScopedRandomFile random_file(ScopedRandomFile::kCreate);
const std::string& filename = random_file.filename();
EXPECT_TRUE(FileExists(filename.c_str()));
EXPECT_TRUE(SbFileAtomicReplace(filename.c_str(), nullptr, 0));
EXPECT_TRUE(CompareFileContentsToString(filename.c_str(), "\0", 0));
}
TEST(SbFileAtomicReplaceTest, FailsWithNoDataButLength) {
ScopedRandomFile random_file(ScopedRandomFile::kCreate);
const std::string& filename = random_file.filename();
EXPECT_TRUE(FileExists(filename.c_str()));
EXPECT_FALSE(SbFileAtomicReplace(filename.c_str(), nullptr, 1));
}
TEST(SbFileAtomicReplaceTest, FailsWithInvalidLength) {
ScopedRandomFile random_file(ScopedRandomFile::kCreate);
const std::string& filename = random_file.filename();
EXPECT_TRUE(FileExists(filename.c_str()));
EXPECT_FALSE(SbFileAtomicReplace(filename.c_str(), kTestContents, -1));
}
} // namespace
} // namespace nplb
} // namespace starboard