Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: test/FilterEngineStubs.cpp

Issue 10310030: Convert references to FileSystem & Co. into shared pointers (avoid use after free) (Closed)
Patch Set: Created April 18, 2013, 11:59 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 #include <iostream> 1 #include <iostream>
2 #include <AdblockPlus.h> 2 #include <AdblockPlus.h>
3 #include <gtest/gtest.h> 3 #include <gtest/gtest.h>
4 4
5 class DummyFileSystem : public AdblockPlus::FileSystem 5 class DummyFileSystem : public AdblockPlus::FileSystem
6 { 6 {
7 std::tr1::shared_ptr<std::istream> Read(const std::string& path) const 7 std::tr1::shared_ptr<std::istream> Read(const std::string& path) const
8 { 8 {
9 throw std::runtime_error("Not implemented"); 9 throw std::runtime_error("Not implemented");
10 } 10 }
(...skipping 13 matching lines...) Expand all
24 { 24 {
25 throw std::runtime_error("Not implemented"); 25 throw std::runtime_error("Not implemented");
26 } 26 }
27 27
28 StatResult Stat(const std::string& path) const 28 StatResult Stat(const std::string& path) const
29 { 29 {
30 throw std::runtime_error("Not implemented"); 30 throw std::runtime_error("Not implemented");
31 } 31 }
32 }; 32 };
33 33
34 class DummyErrorCallback : public AdblockPlus::ErrorCallback
35 {
36 public:
37 void operator()(const std::string& message)
38 {
39 std::cout << message << std::endl;
40 }
41 };
42
43 TEST(FilterEngineStubsTest, FilterCreation) 34 TEST(FilterEngineStubsTest, FilterCreation)
44 { 35 {
45 DummyFileSystem fileSystem; 36 AdblockPlus::JsEngine jsEngine;
46 DummyErrorCallback errorCallback; 37 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(new DummyFileSystem()));
47 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), &fileSystem, 0,
48 &errorCallback);
49 AdblockPlus::FilterEngine filterEngine(jsEngine); 38 AdblockPlus::FilterEngine filterEngine(jsEngine);
50 39
51 AdblockPlus::FilterPtr filter1 = filterEngine.GetFilter("foo"); 40 AdblockPlus::FilterPtr filter1 = filterEngine.GetFilter("foo");
52 ASSERT_EQ(AdblockPlus::Filter::TYPE_BLOCKING, filter1->GetProperty("type", -1) ); 41 ASSERT_EQ(AdblockPlus::Filter::TYPE_BLOCKING, filter1->GetProperty("type", -1) );
53 AdblockPlus::FilterPtr filter2 = filterEngine.GetFilter("@@foo"); 42 AdblockPlus::FilterPtr filter2 = filterEngine.GetFilter("@@foo");
54 ASSERT_EQ(AdblockPlus::Filter::TYPE_EXCEPTION, filter2->GetProperty("type", -1 )); 43 ASSERT_EQ(AdblockPlus::Filter::TYPE_EXCEPTION, filter2->GetProperty("type", -1 ));
55 AdblockPlus::FilterPtr filter3 = filterEngine.GetFilter("example.com##foo"); 44 AdblockPlus::FilterPtr filter3 = filterEngine.GetFilter("example.com##foo");
56 ASSERT_EQ(AdblockPlus::Filter::TYPE_ELEMHIDE, filter3->GetProperty("type", -1) ); 45 ASSERT_EQ(AdblockPlus::Filter::TYPE_ELEMHIDE, filter3->GetProperty("type", -1) );
57 AdblockPlus::FilterPtr filter4 = filterEngine.GetFilter("example.com#@#foo"); 46 AdblockPlus::FilterPtr filter4 = filterEngine.GetFilter("example.com#@#foo");
58 ASSERT_EQ(AdblockPlus::Filter::TYPE_ELEMHIDE_EXCEPTION, filter4->GetProperty(" type", -1)); 47 ASSERT_EQ(AdblockPlus::Filter::TYPE_ELEMHIDE_EXCEPTION, filter4->GetProperty(" type", -1));
59 AdblockPlus::FilterPtr filter5 = filterEngine.GetFilter(" foo "); 48 AdblockPlus::FilterPtr filter5 = filterEngine.GetFilter(" foo ");
60 ASSERT_EQ(*filter1, *filter5); 49 ASSERT_EQ(*filter1, *filter5);
61 } 50 }
62 51
63 TEST(FilterEngineStubsTest, FilterProperties) 52 TEST(FilterEngineStubsTest, FilterProperties)
64 { 53 {
65 DummyFileSystem fileSystem; 54 AdblockPlus::JsEngine jsEngine;
66 DummyErrorCallback errorCallback; 55 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(new DummyFileSystem()));
67 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), &fileSystem, 0,
68 &errorCallback);
69 AdblockPlus::FilterEngine filterEngine(jsEngine); 56 AdblockPlus::FilterEngine filterEngine(jsEngine);
70 AdblockPlus::FilterPtr filter = filterEngine.GetFilter("foo"); 57 AdblockPlus::FilterPtr filter = filterEngine.GetFilter("foo");
71 58
72 ASSERT_EQ("x", filter->GetProperty("stringFoo", "x")); 59 ASSERT_EQ("x", filter->GetProperty("stringFoo", "x"));
73 ASSERT_EQ(42, filter->GetProperty("intFoo", 42)); 60 ASSERT_EQ(42, filter->GetProperty("intFoo", 42));
74 ASSERT_FALSE(filter->GetProperty("boolFoo", false)); 61 ASSERT_FALSE(filter->GetProperty("boolFoo", false));
75 62
76 filter->SetProperty("stringFoo", "y"); 63 filter->SetProperty("stringFoo", "y");
77 filter->SetProperty("intFoo", 24); 64 filter->SetProperty("intFoo", 24);
78 filter->SetProperty("boolFoo", true); 65 filter->SetProperty("boolFoo", true);
79 ASSERT_EQ("y", filter->GetProperty("stringFoo", "x")); 66 ASSERT_EQ("y", filter->GetProperty("stringFoo", "x"));
80 ASSERT_EQ(24, filter->GetProperty("intFoo", 42)); 67 ASSERT_EQ(24, filter->GetProperty("intFoo", 42));
81 ASSERT_TRUE(filter->GetProperty("boolFoo", false)); 68 ASSERT_TRUE(filter->GetProperty("boolFoo", false));
82 } 69 }
83 70
84 TEST(FilterEngineStubsTest, AddRemoveFilters) 71 TEST(FilterEngineStubsTest, AddRemoveFilters)
85 { 72 {
86 DummyFileSystem fileSystem; 73 AdblockPlus::JsEngine jsEngine;
87 DummyErrorCallback errorCallback; 74 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(new DummyFileSystem()));
88 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), &fileSystem, 0,
89 &errorCallback);
90 AdblockPlus::FilterEngine filterEngine(jsEngine); 75 AdblockPlus::FilterEngine filterEngine(jsEngine);
91 ASSERT_EQ(0u, filterEngine.GetListedFilters().size()); 76 ASSERT_EQ(0u, filterEngine.GetListedFilters().size());
92 AdblockPlus::FilterPtr filter = filterEngine.GetFilter("foo"); 77 AdblockPlus::FilterPtr filter = filterEngine.GetFilter("foo");
93 ASSERT_EQ(0u, filterEngine.GetListedFilters().size()); 78 ASSERT_EQ(0u, filterEngine.GetListedFilters().size());
94 filter->AddToList(); 79 filter->AddToList();
95 ASSERT_EQ(1u, filterEngine.GetListedFilters().size()); 80 ASSERT_EQ(1u, filterEngine.GetListedFilters().size());
96 ASSERT_EQ(*filter, *filterEngine.GetListedFilters()[0]); 81 ASSERT_EQ(*filter, *filterEngine.GetListedFilters()[0]);
97 filter->AddToList(); 82 filter->AddToList();
98 ASSERT_EQ(1u, filterEngine.GetListedFilters().size()); 83 ASSERT_EQ(1u, filterEngine.GetListedFilters().size());
99 ASSERT_EQ(*filter, *filterEngine.GetListedFilters()[0]); 84 ASSERT_EQ(*filter, *filterEngine.GetListedFilters()[0]);
100 filter->RemoveFromList(); 85 filter->RemoveFromList();
101 ASSERT_EQ(0u, filterEngine.GetListedFilters().size()); 86 ASSERT_EQ(0u, filterEngine.GetListedFilters().size());
102 filter->RemoveFromList(); 87 filter->RemoveFromList();
103 ASSERT_EQ(0u, filterEngine.GetListedFilters().size()); 88 ASSERT_EQ(0u, filterEngine.GetListedFilters().size());
104 } 89 }
105 90
106 TEST(FilterEngineStubsTest, SubscriptionProperties) 91 TEST(FilterEngineStubsTest, SubscriptionProperties)
107 { 92 {
108 DummyFileSystem fileSystem; 93 AdblockPlus::JsEngine jsEngine;
109 DummyErrorCallback errorCallback; 94 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(new DummyFileSystem()));
110 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), &fileSystem, 0,
111 &errorCallback);
112 AdblockPlus::FilterEngine filterEngine(jsEngine); 95 AdblockPlus::FilterEngine filterEngine(jsEngine);
113 AdblockPlus::SubscriptionPtr subscription = filterEngine.GetSubscription("foo" ); 96 AdblockPlus::SubscriptionPtr subscription = filterEngine.GetSubscription("foo" );
114 97
115 ASSERT_EQ("x", subscription->GetProperty("stringFoo", "x")); 98 ASSERT_EQ("x", subscription->GetProperty("stringFoo", "x"));
116 ASSERT_EQ(42, subscription->GetProperty("intFoo", 42)); 99 ASSERT_EQ(42, subscription->GetProperty("intFoo", 42));
117 ASSERT_FALSE(subscription->GetProperty("boolFoo", false)); 100 ASSERT_FALSE(subscription->GetProperty("boolFoo", false));
118 101
119 subscription->SetProperty("stringFoo", "y"); 102 subscription->SetProperty("stringFoo", "y");
120 subscription->SetProperty("intFoo", 24); 103 subscription->SetProperty("intFoo", 24);
121 subscription->SetProperty("boolFoo", true); 104 subscription->SetProperty("boolFoo", true);
122 ASSERT_EQ("y", subscription->GetProperty("stringFoo", "x")); 105 ASSERT_EQ("y", subscription->GetProperty("stringFoo", "x"));
123 ASSERT_EQ(24, subscription->GetProperty("intFoo", 42)); 106 ASSERT_EQ(24, subscription->GetProperty("intFoo", 42));
124 ASSERT_TRUE(subscription->GetProperty("boolFoo", false)); 107 ASSERT_TRUE(subscription->GetProperty("boolFoo", false));
125 } 108 }
126 109
127 TEST(FilterEngineStubsTest, AddRemoveSubscriptions) 110 TEST(FilterEngineStubsTest, AddRemoveSubscriptions)
128 { 111 {
129 DummyFileSystem fileSystem; 112 AdblockPlus::JsEngine jsEngine;
130 DummyErrorCallback errorCallback; 113 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(new DummyFileSystem()));
131 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), &fileSystem, 0,
132 &errorCallback);
133 AdblockPlus::FilterEngine filterEngine(jsEngine); 114 AdblockPlus::FilterEngine filterEngine(jsEngine);
134 ASSERT_EQ(0u, filterEngine.GetListedSubscriptions().size()); 115 ASSERT_EQ(0u, filterEngine.GetListedSubscriptions().size());
135 AdblockPlus::SubscriptionPtr subscription = filterEngine.GetSubscription("foo" ); 116 AdblockPlus::SubscriptionPtr subscription = filterEngine.GetSubscription("foo" );
136 ASSERT_EQ(0u, filterEngine.GetListedSubscriptions().size()); 117 ASSERT_EQ(0u, filterEngine.GetListedSubscriptions().size());
137 subscription->AddToList(); 118 subscription->AddToList();
138 ASSERT_EQ(1u, filterEngine.GetListedSubscriptions().size()); 119 ASSERT_EQ(1u, filterEngine.GetListedSubscriptions().size());
139 ASSERT_EQ(*subscription, *filterEngine.GetListedSubscriptions()[0]); 120 ASSERT_EQ(*subscription, *filterEngine.GetListedSubscriptions()[0]);
140 subscription->AddToList(); 121 subscription->AddToList();
141 ASSERT_EQ(1u, filterEngine.GetListedSubscriptions().size()); 122 ASSERT_EQ(1u, filterEngine.GetListedSubscriptions().size());
142 ASSERT_EQ(*subscription, *filterEngine.GetListedSubscriptions()[0]); 123 ASSERT_EQ(*subscription, *filterEngine.GetListedSubscriptions()[0]);
143 subscription->RemoveFromList(); 124 subscription->RemoveFromList();
144 ASSERT_EQ(0u, filterEngine.GetListedSubscriptions().size()); 125 ASSERT_EQ(0u, filterEngine.GetListedSubscriptions().size());
145 subscription->RemoveFromList(); 126 subscription->RemoveFromList();
146 ASSERT_EQ(0u, filterEngine.GetListedSubscriptions().size()); 127 ASSERT_EQ(0u, filterEngine.GetListedSubscriptions().size());
147 } 128 }
148 129
149 TEST(FilterEngineStubsTest, SubscriptionUpdates) 130 TEST(FilterEngineStubsTest, SubscriptionUpdates)
150 { 131 {
151 DummyFileSystem fileSystem; 132 AdblockPlus::JsEngine jsEngine;
152 DummyErrorCallback errorCallback; 133 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(new DummyFileSystem()));
153 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), &fileSystem, 0,
154 &errorCallback);
155 AdblockPlus::FilterEngine filterEngine(jsEngine); 134 AdblockPlus::FilterEngine filterEngine(jsEngine);
156 AdblockPlus::SubscriptionPtr subscription = filterEngine.GetSubscription("foo" ); 135 AdblockPlus::SubscriptionPtr subscription = filterEngine.GetSubscription("foo" );
157 ASSERT_FALSE(subscription->IsUpdating()); 136 ASSERT_FALSE(subscription->IsUpdating());
158 137
159 //TODO: This currently crashes due to errors reported on a different thread 138 //TODO: This currently crashes due to errors reported on a different thread
160 //subscription->UpdateFilters(); 139 //subscription->UpdateFilters();
161 } 140 }
162 141
163 TEST(FilterEngineStubsTest, Matches) 142 TEST(FilterEngineStubsTest, Matches)
164 { 143 {
165 DummyFileSystem fileSystem; 144 AdblockPlus::JsEngine jsEngine;
166 DummyErrorCallback errorCallback; 145 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(new DummyFileSystem()));
167 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), &fileSystem, 0,
168 &errorCallback);
169 AdblockPlus::FilterEngine filterEngine(jsEngine); 146 AdblockPlus::FilterEngine filterEngine(jsEngine);
170 147
171 filterEngine.GetFilter("adbanner.gif")->AddToList(); 148 filterEngine.GetFilter("adbanner.gif")->AddToList();
172 filterEngine.GetFilter("@@notbanner.gif")->AddToList(); 149 filterEngine.GetFilter("@@notbanner.gif")->AddToList();
173 150
174 AdblockPlus::FilterPtr match1 = filterEngine.Matches("http://example.org/fooba r.gif", "IMAGE", ""); 151 AdblockPlus::FilterPtr match1 = filterEngine.Matches("http://example.org/fooba r.gif", "IMAGE", "");
175 ASSERT_FALSE(match1); 152 ASSERT_FALSE(match1);
176 153
177 AdblockPlus::FilterPtr match2 = filterEngine.Matches("http://example.org/adban ner.gif", "IMAGE", ""); 154 AdblockPlus::FilterPtr match2 = filterEngine.Matches("http://example.org/adban ner.gif", "IMAGE", "");
178 ASSERT_TRUE(match2); 155 ASSERT_TRUE(match2);
179 ASSERT_EQ(AdblockPlus::Filter::TYPE_BLOCKING, match2->GetProperty("type", -1)) ; 156 ASSERT_EQ(AdblockPlus::Filter::TYPE_BLOCKING, match2->GetProperty("type", -1)) ;
180 157
181 AdblockPlus::FilterPtr match3 = filterEngine.Matches("http://example.org/notba nner.gif", "IMAGE", ""); 158 AdblockPlus::FilterPtr match3 = filterEngine.Matches("http://example.org/notba nner.gif", "IMAGE", "");
182 ASSERT_TRUE(match3); 159 ASSERT_TRUE(match3);
183 ASSERT_EQ(AdblockPlus::Filter::TYPE_EXCEPTION, match3->GetProperty("type", -1) ); 160 ASSERT_EQ(AdblockPlus::Filter::TYPE_EXCEPTION, match3->GetProperty("type", -1) );
184 161
185 AdblockPlus::FilterPtr match4 = filterEngine.Matches("http://example.org/notba nner.gif", "IMAGE", ""); 162 AdblockPlus::FilterPtr match4 = filterEngine.Matches("http://example.org/notba nner.gif", "IMAGE", "");
186 ASSERT_TRUE(match4); 163 ASSERT_TRUE(match4);
187 ASSERT_EQ(AdblockPlus::Filter::TYPE_EXCEPTION, match4->GetProperty("type", -1) ); 164 ASSERT_EQ(AdblockPlus::Filter::TYPE_EXCEPTION, match4->GetProperty("type", -1) );
188 } 165 }
OLDNEW
« src/JsEngine.cpp ('K') | « test/FileSystemJsObject.cpp ('k') | test/GlobalJsObject.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld