| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/bind.h" |
| 6 #include "base/strings/string_piece.h" |
| 5 #include "components/keyed_service/core/dependency_graph.h" | 7 #include "components/keyed_service/core/dependency_graph.h" |
| 6 #include "components/keyed_service/core/dependency_node.h" | 8 #include "components/keyed_service/core/dependency_node.h" |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "third_party/re2/re2/re2.h" |
| 8 | 11 |
| 9 namespace { | 12 namespace { |
| 10 | 13 |
| 11 class DependencyGraphTest : public testing::Test {}; | 14 class DependencyGraphTest : public testing::Test {}; |
| 12 | 15 |
| 13 class DummyNode : public DependencyNode { | 16 class DummyNode : public DependencyNode { |
| 14 public: | 17 public: |
| 15 explicit DummyNode(DependencyGraph* graph) : dependency_graph_(graph) { | 18 explicit DummyNode(DependencyGraph* graph) : dependency_graph_(graph) { |
| 16 dependency_graph_->AddNode(this); | 19 dependency_graph_->AddNode(this); |
| 17 } | 20 } |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 | 151 |
| 149 std::vector<DependencyNode*> destruction_order; | 152 std::vector<DependencyNode*> destruction_order; |
| 150 EXPECT_TRUE(graph.GetDestructionOrder(&destruction_order)); | 153 EXPECT_TRUE(graph.GetDestructionOrder(&destruction_order)); |
| 151 ASSERT_EQ(4U, destruction_order.size()); | 154 ASSERT_EQ(4U, destruction_order.size()); |
| 152 EXPECT_EQ(&bottom, destruction_order[0]); | 155 EXPECT_EQ(&bottom, destruction_order[0]); |
| 153 EXPECT_EQ(&middle2, destruction_order[1]); | 156 EXPECT_EQ(&middle2, destruction_order[1]); |
| 154 EXPECT_EQ(&middle1, destruction_order[2]); | 157 EXPECT_EQ(&middle1, destruction_order[2]); |
| 155 EXPECT_EQ(&parent, destruction_order[3]); | 158 EXPECT_EQ(&parent, destruction_order[3]); |
| 156 } | 159 } |
| 157 | 160 |
| 161 std::string NodeNameProvider(const std::string& name, DependencyNode* node) { |
| 162 return name; |
| 163 } |
| 164 |
| 165 // When this returns true, then |name| is a valid ID according to the DOT |
| 166 // language specification [1]. Note that DOT language also allows HTML strings |
| 167 // as valid identifiers, but those are not used in the production code calling |
| 168 // the tested DumpAsGraphviz. |
| 169 // [1] http://www.graphviz.org/content/dot-language |
| 170 bool IsValidDotId(base::StringPiece name) { |
| 171 static const char pattern[] = |
| 172 "[a-zA-Z\\200-\\377_][0-9a-zA-Z\\200-\\377_]*" |
| 173 "|-?(?:\\.[0-9]+|[0-9]+(\\.[0-9]*)?)" |
| 174 "|\"(?:[^\"]|\\\")*\""; |
| 175 return RE2::FullMatch(re2::StringPiece(name.data(), name.size()), pattern); |
| 176 } |
| 177 |
| 178 // Returns the source name of the first edge of the graphstr described in DOT |
| 179 // format in |graphstr|. |
| 180 base::StringPiece LocateNodeNameInGraph(base::StringPiece graphstr) { |
| 181 re2::StringPiece name; |
| 182 EXPECT_TRUE(RE2::FullMatch( |
| 183 re2::StringPiece(graphstr.data(), graphstr.size()), |
| 184 "(?sm).*^[ \\t]*([^ \\t]*(?:[ \\t]+[^ \\t]+)*)[ \\t]*->.*", &name)) |
| 185 << "graphstr=" << graphstr; |
| 186 return base::StringPiece(name.data(), name.size()); |
| 187 } |
| 188 |
| 189 // Node names in the dependency graph should be properly escaped. |
| 190 TEST_F(DependencyGraphTest, DumpAsGraphviz_Escaping) { |
| 191 const char* names[] = { |
| 192 "namespace::Service", "justAlphabeticService", |
| 193 "Service_with_underscores", "AlphaNumeric123Service", |
| 194 "123ServiceStartingWithDigits", "service with spaces", |
| 195 "ServiceWith\"Quotes"}; |
| 196 |
| 197 DependencyGraph graph; |
| 198 DummyNode parent(&graph); |
| 199 DummyNode child(&graph); |
| 200 |
| 201 graph.AddEdge(&parent, &child); |
| 202 |
| 203 for (const char* name : names) { |
| 204 SCOPED_TRACE(testing::Message("name=") << name); |
| 205 std::string graph_str = |
| 206 graph.DumpAsGraphviz("Test", base::Bind(&NodeNameProvider, name)); |
| 207 base::StringPiece dumped_name(LocateNodeNameInGraph(graph_str)); |
| 208 EXPECT_TRUE(IsValidDotId(dumped_name)) << "dumped_name=" << dumped_name; |
| 209 } |
| 210 } |
| 211 |
| 158 } // namespace | 212 } // namespace |
| OLD | NEW |