1 // Copyright 2014 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 <stdint.h>
6
7 #include "base/files/file.h"
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "gn/functions.h"
11 #include "gn/scheduler.h"
12 #include "gn/test_with_scheduler.h"
13 #include "gn/test_with_scope.h"
14 #include "util/test/test.h"
15
16 #if defined(OS_LINUX) || defined(OS_MACOSX) || defined(OS_HAIKU)
17 #include <sys/time.h>
18 #endif
19
20 #if defined(OS_WIN)
21 #include <windows.h>
22 #endif
23
24 namespace {
25
26 // Returns true on success, false if write_file signaled an error.
CallWriteFile(Scope * scope,const std::string & filename,const Value & data)27 bool CallWriteFile(Scope* scope,
28 const std::string& filename,
29 const Value& data) {
30 Err err;
31
32 std::vector<Value> args;
33 args.push_back(Value(nullptr, filename));
34 args.push_back(data);
35
36 FunctionCallNode function_call;
37 Value result = functions::RunWriteFile(scope, &function_call, args, &err);
38 EXPECT_EQ(Value::NONE, result.type()); // Should always return none.
39
40 return !err.has_error();
41 }
42
43 } // namespace
44
45 using WriteFileTest = TestWithScheduler;
46
TEST_F(WriteFileTest,WithData)47 TEST_F(WriteFileTest, WithData) {
48 TestWithScope setup;
49
50 // Make a real directory for writing the files.
51 base::ScopedTempDir temp_dir;
52 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
53 setup.build_settings()->SetRootPath(temp_dir.GetPath());
54 setup.build_settings()->SetBuildDir(SourceDir("//out/"));
55
56 Value some_string(nullptr, "some string contents");
57
58 // Should refuse to write files outside of the output dir.
59 EXPECT_FALSE(CallWriteFile(setup.scope(), "//in_root.txt", some_string));
60 EXPECT_FALSE(
61 CallWriteFile(setup.scope(), "//other_dir/foo.txt", some_string));
62
63 // Should be able to write to a new dir inside the out dir.
64 EXPECT_TRUE(CallWriteFile(setup.scope(), "//out/foo.txt", some_string));
65 base::FilePath foo_name = temp_dir.GetPath()
66 .Append(FILE_PATH_LITERAL("out"))
67 .Append(FILE_PATH_LITERAL("foo.txt"));
68 std::string result_contents;
69 EXPECT_TRUE(base::ReadFileToString(foo_name, &result_contents));
70 EXPECT_EQ(some_string.string_value(), result_contents);
71
72 // Update the contents with a list of a string and a number.
73 Value some_list(nullptr, Value::LIST);
74 some_list.list_value().push_back(Value(nullptr, "line 1"));
75 some_list.list_value().push_back(Value(nullptr, static_cast<int64_t>(2)));
76 EXPECT_TRUE(CallWriteFile(setup.scope(), "//out/foo.txt", some_list));
77 EXPECT_TRUE(base::ReadFileToString(foo_name, &result_contents));
78 EXPECT_EQ("line 1\n2\n", result_contents);
79
80 // Test that the file is not rewritten if the contents are not changed.
81 base::File foo_file(foo_name, base::File::FLAG_OPEN | base::File::FLAG_READ |
82 base::File::FLAG_WRITE);
83 ASSERT_TRUE(foo_file.IsValid());
84
85 // Start by setting the modified time to something old to avoid clock
86 // resolution issues.
87 #if defined(OS_WIN)
88 FILETIME last_access_filetime = {};
89 FILETIME last_modified_filetime = {};
90 ASSERT_TRUE(::SetFileTime(foo_file.GetPlatformFile(), nullptr,
91 &last_access_filetime, &last_modified_filetime));
92 #elif defined(OS_AIX) || defined(OS_HAIKU)
93 struct timeval times[2] = {};
94 ASSERT_EQ(utimes(foo_name.value().c_str(), times), 0);
95 #else
96 struct timeval times[2] = {};
97 ASSERT_EQ(futimes(foo_file.GetPlatformFile(), times), 0);
98 #endif
99
100 // Read the current time to avoid timer resolution issues when comparing
101 // below.
102 base::File::Info original_info;
103 foo_file.GetInfo(&original_info);
104
105 EXPECT_TRUE(CallWriteFile(setup.scope(), "//out/foo.txt", some_list));
106
107 // Verify that the last modified time is the same as before.
108 base::File::Info new_info;
109 foo_file.GetInfo(&new_info);
110 EXPECT_EQ(original_info.last_modified, new_info.last_modified);
111 }
112