1 // Copyright 2022 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "util/atomic_write.h" 6 7 #include <string> 8 9 #include "base/files/file_util.h" 10 #include "base/files/scoped_temp_dir.h" 11 #include "util/test/test.h" 12 13 class ImportantFileWriterTest : public testing::Test { 14 public: 15 ImportantFileWriterTest() = default; SetUp()16 void SetUp() override { 17 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 18 file_ = temp_dir_.GetPath().AppendASCII("test-file"); 19 } 20 21 protected: 22 base::FilePath file_; 23 24 private: 25 base::ScopedTempDir temp_dir_; 26 }; 27 28 // Test that WriteFileAtomically works. TEST_F(ImportantFileWriterTest,Basic)29TEST_F(ImportantFileWriterTest, Basic) { 30 const std::string data = "Test string for writing."; 31 EXPECT_FALSE(base::PathExists(file_)); 32 EXPECT_TRUE(util::WriteFileAtomically(file_, data.data(), data.size())); 33 std::string actual; 34 EXPECT_TRUE(ReadFileToString(file_, &actual)); 35 EXPECT_EQ(data, actual); 36 } 37