Fix crash in attempting to canonicalize paths longer than _MAX_PATH
[platform/upstream/ninja.git] / src / includes_normalize_test.cc
1 // Copyright 2012 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 "includes_normalize.h"
16
17 #include <algorithm>
18
19 #include <direct.h>
20
21 #include "test.h"
22 #include "util.h"
23
24 TEST(IncludesNormalize, Simple) {
25   EXPECT_EQ("b", IncludesNormalize::Normalize("a\\..\\b", NULL));
26   EXPECT_EQ("b", IncludesNormalize::Normalize("a\\../b", NULL));
27   EXPECT_EQ("a/b", IncludesNormalize::Normalize("a\\.\\b", NULL));
28   EXPECT_EQ("a/b", IncludesNormalize::Normalize("a\\./b", NULL));
29 }
30
31 namespace {
32
33 string GetCurDir() {
34   char buf[_MAX_PATH];
35   _getcwd(buf, sizeof(buf));
36   vector<string> parts = IncludesNormalize::Split(string(buf), '\\');
37   return parts[parts.size() - 1];
38 }
39
40 }  // namespace
41
42 TEST(IncludesNormalize, WithRelative) {
43   string currentdir = GetCurDir();
44   EXPECT_EQ("c", IncludesNormalize::Normalize("a/b/c", "a/b"));
45   EXPECT_EQ("a", IncludesNormalize::Normalize(IncludesNormalize::AbsPath("a"),
46                                               NULL));
47   EXPECT_EQ(string("../") + currentdir + string("/a"),
48             IncludesNormalize::Normalize("a", "../b"));
49   EXPECT_EQ(string("../") + currentdir + string("/a/b"),
50             IncludesNormalize::Normalize("a/b", "../c"));
51   EXPECT_EQ("../../a", IncludesNormalize::Normalize("a", "b/c"));
52   EXPECT_EQ(".", IncludesNormalize::Normalize("a", "a"));
53 }
54
55 TEST(IncludesNormalize, Case) {
56   EXPECT_EQ("b", IncludesNormalize::Normalize("Abc\\..\\b", NULL));
57   EXPECT_EQ("BdEf", IncludesNormalize::Normalize("Abc\\..\\BdEf", NULL));
58   EXPECT_EQ("A/b", IncludesNormalize::Normalize("A\\.\\b", NULL));
59   EXPECT_EQ("a/b", IncludesNormalize::Normalize("a\\./b", NULL));
60   EXPECT_EQ("A/B", IncludesNormalize::Normalize("A\\.\\B", NULL));
61   EXPECT_EQ("A/B", IncludesNormalize::Normalize("A\\./B", NULL));
62 }
63
64 TEST(IncludesNormalize, Join) {
65   vector<string> x;
66   EXPECT_EQ("", IncludesNormalize::Join(x, ':'));
67   x.push_back("alpha");
68   EXPECT_EQ("alpha", IncludesNormalize::Join(x, ':'));
69   x.push_back("beta");
70   x.push_back("gamma");
71   EXPECT_EQ("alpha:beta:gamma", IncludesNormalize::Join(x, ':'));
72 }
73
74 TEST(IncludesNormalize, Split) {
75   EXPECT_EQ("", IncludesNormalize::Join(IncludesNormalize::Split("", '/'),
76                                         ':'));
77   EXPECT_EQ("a", IncludesNormalize::Join(IncludesNormalize::Split("a", '/'),
78                                          ':'));
79   EXPECT_EQ("a:b:c",
80             IncludesNormalize::Join(
81                 IncludesNormalize::Split("a/b/c", '/'), ':'));
82 }
83
84 TEST(IncludesNormalize, ToLower) {
85   EXPECT_EQ("", IncludesNormalize::ToLower(""));
86   EXPECT_EQ("stuff", IncludesNormalize::ToLower("Stuff"));
87   EXPECT_EQ("stuff and things", IncludesNormalize::ToLower("Stuff AND thINGS"));
88   EXPECT_EQ("stuff 3and thin43gs",
89             IncludesNormalize::ToLower("Stuff 3AND thIN43GS"));
90 }
91
92 TEST(IncludesNormalize, DifferentDrive) {
93   EXPECT_EQ("stuff.h",
94       IncludesNormalize::Normalize("p:\\vs08\\stuff.h", "p:\\vs08"));
95   EXPECT_EQ("stuff.h",
96       IncludesNormalize::Normalize("P:\\Vs08\\stuff.h", "p:\\vs08"));
97   EXPECT_EQ("p:/vs08/stuff.h",
98       IncludesNormalize::Normalize("p:\\vs08\\stuff.h", "c:\\vs08"));
99   EXPECT_EQ("P:/vs08/stufF.h",
100       IncludesNormalize::Normalize("P:\\vs08\\stufF.h", "D:\\stuff/things"));
101   EXPECT_EQ("P:/vs08/stuff.h",
102       IncludesNormalize::Normalize("P:/vs08\\stuff.h", "D:\\stuff/things"));
103   EXPECT_EQ("P:/wee/stuff.h",
104             IncludesNormalize::Normalize("P:/vs08\\../wee\\stuff.h",
105                                          "D:\\stuff/things"));
106 }
107
108 TEST(IncludesNormalize, LongInvalidPath) {
109   const char kLongInputString[] =
110       "C:\\Program Files (x86)\\Microsoft Visual Studio "
111       "12.0\\VC\\INCLUDEwarning #31001: The dll for reading and writing the "
112       "pdb (for example, mspdb110.dll) could not be found on your path. This "
113       "is usually a configuration error. Compilation will continue using /Z7 "
114       "instead of /Zi, but expect a similar error when you link your program.";
115   // Too long, won't be canonicalized, but just don't crash.
116   EXPECT_EQ(kLongInputString,
117             IncludesNormalize::Normalize(kLongInputString, NULL));
118
119   const char kExactlyMaxPath[] =
120       "012345678\\"
121       "012345678\\"
122       "012345678\\"
123       "012345678\\"
124       "012345678\\"
125       "012345678\\"
126       "012345678\\"
127       "012345678\\"
128       "012345678\\"
129       "012345678\\"
130       "012345678\\"
131       "012345678\\"
132       "012345678\\"
133       "012345678\\"
134       "012345678\\"
135       "012345678\\"
136       "012345678\\"
137       "012345678\\"
138       "012345678\\"
139       "012345678\\"
140       "012345678\\"
141       "012345678\\"
142       "012345678\\"
143       "012345678\\"
144       "012345678\\"
145       "0123456789";
146   std::string forward_slashes(kExactlyMaxPath);
147   std::replace(forward_slashes.begin(), forward_slashes.end(), '\\', '/');
148   // Make sure a path that's exactly _MAX_PATH long is canonicalized.
149   EXPECT_EQ(forward_slashes,
150             IncludesNormalize::Normalize(kExactlyMaxPath, NULL));
151 }