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