Imported Upstream version 1.10.1
[platform/upstream/ninja.git] / src / build_log_test.cc
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "build_log.h"
16
17 #include "util.h"
18 #include "test.h"
19
20 #include <sys/stat.h>
21 #ifdef _WIN32
22 #include <fcntl.h>
23 #include <share.h>
24 #else
25 #include <sys/types.h>
26 #include <unistd.h>
27 #endif
28 #include <cassert>
29
30 namespace {
31
32 const char kTestFilename[] = "BuildLogTest-tempfile";
33
34 struct BuildLogTest : public StateTestWithBuiltinRules, public BuildLogUser {
35   virtual void SetUp() {
36     // In case a crashing test left a stale file behind.
37     unlink(kTestFilename);
38   }
39   virtual void TearDown() {
40     unlink(kTestFilename);
41   }
42   virtual bool IsPathDead(StringPiece s) const { return false; }
43 };
44
45 TEST_F(BuildLogTest, WriteRead) {
46   AssertParse(&state_,
47 "build out: cat mid\n"
48 "build mid: cat in\n");
49
50   BuildLog log1;
51   string err;
52   EXPECT_TRUE(log1.OpenForWrite(kTestFilename, *this, &err));
53   ASSERT_EQ("", err);
54   log1.RecordCommand(state_.edges_[0], 15, 18);
55   log1.RecordCommand(state_.edges_[1], 20, 25);
56   log1.Close();
57
58   BuildLog log2;
59   EXPECT_TRUE(log2.Load(kTestFilename, &err));
60   ASSERT_EQ("", err);
61
62   ASSERT_EQ(2u, log1.entries().size());
63   ASSERT_EQ(2u, log2.entries().size());
64   BuildLog::LogEntry* e1 = log1.LookupByOutput("out");
65   ASSERT_TRUE(e1);
66   BuildLog::LogEntry* e2 = log2.LookupByOutput("out");
67   ASSERT_TRUE(e2);
68   ASSERT_TRUE(*e1 == *e2);
69   ASSERT_EQ(15, e1->start_time);
70   ASSERT_EQ("out", e1->output);
71 }
72
73 TEST_F(BuildLogTest, FirstWriteAddsSignature) {
74   const char kExpectedVersion[] = "# ninja log vX\n";
75   const size_t kVersionPos = strlen(kExpectedVersion) - 2;  // Points at 'X'.
76
77   BuildLog log;
78   string contents, err;
79
80   EXPECT_TRUE(log.OpenForWrite(kTestFilename, *this, &err));
81   ASSERT_EQ("", err);
82   log.Close();
83
84   ASSERT_EQ(0, ReadFile(kTestFilename, &contents, &err));
85   ASSERT_EQ("", err);
86   if (contents.size() >= kVersionPos)
87     contents[kVersionPos] = 'X';
88   EXPECT_EQ(kExpectedVersion, contents);
89
90   // Opening the file anew shouldn't add a second version string.
91   EXPECT_TRUE(log.OpenForWrite(kTestFilename, *this, &err));
92   ASSERT_EQ("", err);
93   log.Close();
94
95   contents.clear();
96   ASSERT_EQ(0, ReadFile(kTestFilename, &contents, &err));
97   ASSERT_EQ("", err);
98   if (contents.size() >= kVersionPos)
99     contents[kVersionPos] = 'X';
100   EXPECT_EQ(kExpectedVersion, contents);
101 }
102
103 TEST_F(BuildLogTest, DoubleEntry) {
104   FILE* f = fopen(kTestFilename, "wb");
105   fprintf(f, "# ninja log v4\n");
106   fprintf(f, "0\t1\t2\tout\tcommand abc\n");
107   fprintf(f, "3\t4\t5\tout\tcommand def\n");
108   fclose(f);
109
110   string err;
111   BuildLog log;
112   EXPECT_TRUE(log.Load(kTestFilename, &err));
113   ASSERT_EQ("", err);
114
115   BuildLog::LogEntry* e = log.LookupByOutput("out");
116   ASSERT_TRUE(e);
117   ASSERT_NO_FATAL_FAILURE(AssertHash("command def", e->command_hash));
118 }
119
120 TEST_F(BuildLogTest, Truncate) {
121   AssertParse(&state_,
122 "build out: cat mid\n"
123 "build mid: cat in\n");
124
125   {
126     BuildLog log1;
127     string err;
128     EXPECT_TRUE(log1.OpenForWrite(kTestFilename, *this, &err));
129     ASSERT_EQ("", err);
130     log1.RecordCommand(state_.edges_[0], 15, 18);
131     log1.RecordCommand(state_.edges_[1], 20, 25);
132     log1.Close();
133   }
134
135   struct stat statbuf;
136   ASSERT_EQ(0, stat(kTestFilename, &statbuf));
137   ASSERT_GT(statbuf.st_size, 0);
138
139   // For all possible truncations of the input file, assert that we don't
140   // crash when parsing.
141   for (off_t size = statbuf.st_size; size > 0; --size) {
142     BuildLog log2;
143     string err;
144     EXPECT_TRUE(log2.OpenForWrite(kTestFilename, *this, &err));
145     ASSERT_EQ("", err);
146     log2.RecordCommand(state_.edges_[0], 15, 18);
147     log2.RecordCommand(state_.edges_[1], 20, 25);
148     log2.Close();
149
150     ASSERT_TRUE(Truncate(kTestFilename, size, &err));
151
152     BuildLog log3;
153     err.clear();
154     ASSERT_TRUE(log3.Load(kTestFilename, &err) == LOAD_SUCCESS || !err.empty());
155   }
156 }
157
158 TEST_F(BuildLogTest, ObsoleteOldVersion) {
159   FILE* f = fopen(kTestFilename, "wb");
160   fprintf(f, "# ninja log v3\n");
161   fprintf(f, "123 456 0 out command\n");
162   fclose(f);
163
164   string err;
165   BuildLog log;
166   EXPECT_TRUE(log.Load(kTestFilename, &err));
167   ASSERT_NE(err.find("version"), string::npos);
168 }
169
170 TEST_F(BuildLogTest, SpacesInOutputV4) {
171   FILE* f = fopen(kTestFilename, "wb");
172   fprintf(f, "# ninja log v4\n");
173   fprintf(f, "123\t456\t456\tout with space\tcommand\n");
174   fclose(f);
175
176   string err;
177   BuildLog log;
178   EXPECT_TRUE(log.Load(kTestFilename, &err));
179   ASSERT_EQ("", err);
180
181   BuildLog::LogEntry* e = log.LookupByOutput("out with space");
182   ASSERT_TRUE(e);
183   ASSERT_EQ(123, e->start_time);
184   ASSERT_EQ(456, e->end_time);
185   ASSERT_EQ(456, e->mtime);
186   ASSERT_NO_FATAL_FAILURE(AssertHash("command", e->command_hash));
187 }
188
189 TEST_F(BuildLogTest, DuplicateVersionHeader) {
190   // Old versions of ninja accidentally wrote multiple version headers to the
191   // build log on Windows. This shouldn't crash, and the second version header
192   // should be ignored.
193   FILE* f = fopen(kTestFilename, "wb");
194   fprintf(f, "# ninja log v4\n");
195   fprintf(f, "123\t456\t456\tout\tcommand\n");
196   fprintf(f, "# ninja log v4\n");
197   fprintf(f, "456\t789\t789\tout2\tcommand2\n");
198   fclose(f);
199
200   string err;
201   BuildLog log;
202   EXPECT_TRUE(log.Load(kTestFilename, &err));
203   ASSERT_EQ("", err);
204
205   BuildLog::LogEntry* e = log.LookupByOutput("out");
206   ASSERT_TRUE(e);
207   ASSERT_EQ(123, e->start_time);
208   ASSERT_EQ(456, e->end_time);
209   ASSERT_EQ(456, e->mtime);
210   ASSERT_NO_FATAL_FAILURE(AssertHash("command", e->command_hash));
211
212   e = log.LookupByOutput("out2");
213   ASSERT_TRUE(e);
214   ASSERT_EQ(456, e->start_time);
215   ASSERT_EQ(789, e->end_time);
216   ASSERT_EQ(789, e->mtime);
217   ASSERT_NO_FATAL_FAILURE(AssertHash("command2", e->command_hash));
218 }
219
220 struct TestDiskInterface : public DiskInterface {
221   virtual TimeStamp Stat(const string& path, string* err) const {
222     return 4;
223   }
224   virtual bool WriteFile(const string& path, const string& contents) {
225     assert(false);
226     return true;
227   }
228   virtual bool MakeDir(const string& path) {
229     assert(false);
230     return false;
231   }
232   virtual Status ReadFile(const string& path, string* contents, string* err) {
233     assert(false);
234     return NotFound;
235   }
236   virtual int RemoveFile(const string& path) {
237     assert(false);
238     return 0;
239   }
240 };
241
242 TEST_F(BuildLogTest, Restat) {
243   FILE* f = fopen(kTestFilename, "wb");
244   fprintf(f, "# ninja log v4\n"
245              "1\t2\t3\tout\tcommand\n");
246   fclose(f);
247   std::string err;
248   BuildLog log;
249   EXPECT_TRUE(log.Load(kTestFilename, &err));
250   ASSERT_EQ("", err);
251   BuildLog::LogEntry* e = log.LookupByOutput("out");
252   ASSERT_EQ(3, e->mtime);
253
254   TestDiskInterface testDiskInterface;
255   char out2[] = { 'o', 'u', 't', '2', 0 };
256   char* filter2[] = { out2 };
257   EXPECT_TRUE(log.Restat(kTestFilename, testDiskInterface, 1, filter2, &err));
258   ASSERT_EQ("", err);
259   e = log.LookupByOutput("out");
260   ASSERT_EQ(3, e->mtime); // unchanged, since the filter doesn't match
261
262   EXPECT_TRUE(log.Restat(kTestFilename, testDiskInterface, 0, NULL, &err));
263   ASSERT_EQ("", err);
264   e = log.LookupByOutput("out");
265   ASSERT_EQ(4, e->mtime);
266 }
267
268 TEST_F(BuildLogTest, VeryLongInputLine) {
269   // Ninja's build log buffer is currently 256kB. Lines longer than that are
270   // silently ignored, but don't affect parsing of other lines.
271   FILE* f = fopen(kTestFilename, "wb");
272   fprintf(f, "# ninja log v4\n");
273   fprintf(f, "123\t456\t456\tout\tcommand start");
274   for (size_t i = 0; i < (512 << 10) / strlen(" more_command"); ++i)
275     fputs(" more_command", f);
276   fprintf(f, "\n");
277   fprintf(f, "456\t789\t789\tout2\tcommand2\n");
278   fclose(f);
279
280   string err;
281   BuildLog log;
282   EXPECT_TRUE(log.Load(kTestFilename, &err));
283   ASSERT_EQ("", err);
284
285   BuildLog::LogEntry* e = log.LookupByOutput("out");
286   ASSERT_EQ(NULL, e);
287
288   e = log.LookupByOutput("out2");
289   ASSERT_TRUE(e);
290   ASSERT_EQ(456, e->start_time);
291   ASSERT_EQ(789, e->end_time);
292   ASSERT_EQ(789, e->mtime);
293   ASSERT_NO_FATAL_FAILURE(AssertHash("command2", e->command_hash));
294 }
295
296 TEST_F(BuildLogTest, MultiTargetEdge) {
297   AssertParse(&state_,
298 "build out out.d: cat\n");
299
300   BuildLog log;
301   log.RecordCommand(state_.edges_[0], 21, 22);
302
303   ASSERT_EQ(2u, log.entries().size());
304   BuildLog::LogEntry* e1 = log.LookupByOutput("out");
305   ASSERT_TRUE(e1);
306   BuildLog::LogEntry* e2 = log.LookupByOutput("out.d");
307   ASSERT_TRUE(e2);
308   ASSERT_EQ("out", e1->output);
309   ASSERT_EQ("out.d", e2->output);
310   ASSERT_EQ(21, e1->start_time);
311   ASSERT_EQ(21, e2->start_time);
312   ASSERT_EQ(22, e2->end_time);
313   ASSERT_EQ(22, e2->end_time);
314 }
315
316 struct BuildLogRecompactTest : public BuildLogTest {
317   virtual bool IsPathDead(StringPiece s) const { return s == "out2"; }
318 };
319
320 TEST_F(BuildLogRecompactTest, Recompact) {
321   AssertParse(&state_,
322 "build out: cat in\n"
323 "build out2: cat in\n");
324
325   BuildLog log1;
326   string err;
327   EXPECT_TRUE(log1.OpenForWrite(kTestFilename, *this, &err));
328   ASSERT_EQ("", err);
329   // Record the same edge several times, to trigger recompaction
330   // the next time the log is opened.
331   for (int i = 0; i < 200; ++i)
332     log1.RecordCommand(state_.edges_[0], 15, 18 + i);
333   log1.RecordCommand(state_.edges_[1], 21, 22);
334   log1.Close();
335
336   // Load...
337   BuildLog log2;
338   EXPECT_TRUE(log2.Load(kTestFilename, &err));
339   ASSERT_EQ("", err);
340   ASSERT_EQ(2u, log2.entries().size());
341   ASSERT_TRUE(log2.LookupByOutput("out"));
342   ASSERT_TRUE(log2.LookupByOutput("out2"));
343   // ...and force a recompaction.
344   EXPECT_TRUE(log2.OpenForWrite(kTestFilename, *this, &err));
345   log2.Close();
346
347   // "out2" is dead, it should've been removed.
348   BuildLog log3;
349   EXPECT_TRUE(log2.Load(kTestFilename, &err));
350   ASSERT_EQ("", err);
351   ASSERT_EQ(1u, log2.entries().size());
352   ASSERT_TRUE(log2.LookupByOutput("out"));
353   ASSERT_FALSE(log2.LookupByOutput("out2"));
354 }
355
356 }  // anonymous namespace