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

Side by Side Diff: test/FilterEngineStubs.cpp

Issue 10173031: Don`t use references to JsEngine to avoid use-after-free errors,switch to shared_ptr instead (Closed)
Patch Set: Created April 18, 2013, 4:15 p.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 15 matching lines...) Expand all
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 TEST(FilterEngineStubsTest, FilterCreation) 34 TEST(FilterEngineStubsTest, FilterCreation)
35 { 35 {
36 AdblockPlus::JsEngine jsEngine; 36 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
37 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(new DummyFileSystem())); 37 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new DummyFileSystem()));
38 AdblockPlus::FilterEngine filterEngine(jsEngine); 38 AdblockPlus::FilterEngine filterEngine(jsEngine);
39 39
40 AdblockPlus::FilterPtr filter1 = filterEngine.GetFilter("foo"); 40 AdblockPlus::FilterPtr filter1 = filterEngine.GetFilter("foo");
41 ASSERT_EQ(AdblockPlus::Filter::TYPE_BLOCKING, filter1->GetProperty("type", -1) ); 41 ASSERT_EQ(AdblockPlus::Filter::TYPE_BLOCKING, filter1->GetProperty("type", -1) );
42 AdblockPlus::FilterPtr filter2 = filterEngine.GetFilter("@@foo"); 42 AdblockPlus::FilterPtr filter2 = filterEngine.GetFilter("@@foo");
43 ASSERT_EQ(AdblockPlus::Filter::TYPE_EXCEPTION, filter2->GetProperty("type", -1 )); 43 ASSERT_EQ(AdblockPlus::Filter::TYPE_EXCEPTION, filter2->GetProperty("type", -1 ));
44 AdblockPlus::FilterPtr filter3 = filterEngine.GetFilter("example.com##foo"); 44 AdblockPlus::FilterPtr filter3 = filterEngine.GetFilter("example.com##foo");
45 ASSERT_EQ(AdblockPlus::Filter::TYPE_ELEMHIDE, filter3->GetProperty("type", -1) ); 45 ASSERT_EQ(AdblockPlus::Filter::TYPE_ELEMHIDE, filter3->GetProperty("type", -1) );
46 AdblockPlus::FilterPtr filter4 = filterEngine.GetFilter("example.com#@#foo"); 46 AdblockPlus::FilterPtr filter4 = filterEngine.GetFilter("example.com#@#foo");
47 ASSERT_EQ(AdblockPlus::Filter::TYPE_ELEMHIDE_EXCEPTION, filter4->GetProperty(" type", -1)); 47 ASSERT_EQ(AdblockPlus::Filter::TYPE_ELEMHIDE_EXCEPTION, filter4->GetProperty(" type", -1));
48 AdblockPlus::FilterPtr filter5 = filterEngine.GetFilter(" foo "); 48 AdblockPlus::FilterPtr filter5 = filterEngine.GetFilter(" foo ");
49 ASSERT_EQ(*filter1, *filter5); 49 ASSERT_EQ(*filter1, *filter5);
50 } 50 }
51 51
52 TEST(FilterEngineStubsTest, FilterProperties) 52 TEST(FilterEngineStubsTest, FilterProperties)
53 { 53 {
54 AdblockPlus::JsEngine jsEngine; 54 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
55 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(new DummyFileSystem())); 55 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new DummyFileSystem()));
56 AdblockPlus::FilterEngine filterEngine(jsEngine); 56 AdblockPlus::FilterEngine filterEngine(jsEngine);
57 AdblockPlus::FilterPtr filter = filterEngine.GetFilter("foo"); 57 AdblockPlus::FilterPtr filter = filterEngine.GetFilter("foo");
58 58
59 ASSERT_EQ("x", filter->GetProperty("stringFoo", "x")); 59 ASSERT_EQ("x", filter->GetProperty("stringFoo", "x"));
60 ASSERT_EQ(42, filter->GetProperty("intFoo", 42)); 60 ASSERT_EQ(42, filter->GetProperty("intFoo", 42));
61 ASSERT_FALSE(filter->GetProperty("boolFoo", false)); 61 ASSERT_FALSE(filter->GetProperty("boolFoo", false));
62 62
63 filter->SetProperty("stringFoo", "y"); 63 filter->SetProperty("stringFoo", "y");
64 filter->SetProperty("intFoo", 24); 64 filter->SetProperty("intFoo", 24);
65 filter->SetProperty("boolFoo", true); 65 filter->SetProperty("boolFoo", true);
66 ASSERT_EQ("y", filter->GetProperty("stringFoo", "x")); 66 ASSERT_EQ("y", filter->GetProperty("stringFoo", "x"));
67 ASSERT_EQ(24, filter->GetProperty("intFoo", 42)); 67 ASSERT_EQ(24, filter->GetProperty("intFoo", 42));
68 ASSERT_TRUE(filter->GetProperty("boolFoo", false)); 68 ASSERT_TRUE(filter->GetProperty("boolFoo", false));
69 } 69 }
70 70
71 TEST(FilterEngineStubsTest, AddRemoveFilters) 71 TEST(FilterEngineStubsTest, AddRemoveFilters)
72 { 72 {
73 AdblockPlus::JsEngine jsEngine; 73 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
74 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(new DummyFileSystem())); 74 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new DummyFileSystem()));
75 AdblockPlus::FilterEngine filterEngine(jsEngine); 75 AdblockPlus::FilterEngine filterEngine(jsEngine);
76 ASSERT_EQ(0u, filterEngine.GetListedFilters().size()); 76 ASSERT_EQ(0u, filterEngine.GetListedFilters().size());
77 AdblockPlus::FilterPtr filter = filterEngine.GetFilter("foo"); 77 AdblockPlus::FilterPtr filter = filterEngine.GetFilter("foo");
78 ASSERT_EQ(0u, filterEngine.GetListedFilters().size()); 78 ASSERT_EQ(0u, filterEngine.GetListedFilters().size());
79 filter->AddToList(); 79 filter->AddToList();
80 ASSERT_EQ(1u, filterEngine.GetListedFilters().size()); 80 ASSERT_EQ(1u, filterEngine.GetListedFilters().size());
81 ASSERT_EQ(*filter, *filterEngine.GetListedFilters()[0]); 81 ASSERT_EQ(*filter, *filterEngine.GetListedFilters()[0]);
82 filter->AddToList(); 82 filter->AddToList();
83 ASSERT_EQ(1u, filterEngine.GetListedFilters().size()); 83 ASSERT_EQ(1u, filterEngine.GetListedFilters().size());
84 ASSERT_EQ(*filter, *filterEngine.GetListedFilters()[0]); 84 ASSERT_EQ(*filter, *filterEngine.GetListedFilters()[0]);
85 filter->RemoveFromList(); 85 filter->RemoveFromList();
86 ASSERT_EQ(0u, filterEngine.GetListedFilters().size()); 86 ASSERT_EQ(0u, filterEngine.GetListedFilters().size());
87 filter->RemoveFromList(); 87 filter->RemoveFromList();
88 ASSERT_EQ(0u, filterEngine.GetListedFilters().size()); 88 ASSERT_EQ(0u, filterEngine.GetListedFilters().size());
89 } 89 }
90 90
91 TEST(FilterEngineStubsTest, SubscriptionProperties) 91 TEST(FilterEngineStubsTest, SubscriptionProperties)
92 { 92 {
93 AdblockPlus::JsEngine jsEngine; 93 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
94 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(new DummyFileSystem())); 94 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new DummyFileSystem()));
95 AdblockPlus::FilterEngine filterEngine(jsEngine); 95 AdblockPlus::FilterEngine filterEngine(jsEngine);
96 AdblockPlus::SubscriptionPtr subscription = filterEngine.GetSubscription("foo" ); 96 AdblockPlus::SubscriptionPtr subscription = filterEngine.GetSubscription("foo" );
97 97
98 ASSERT_EQ("x", subscription->GetProperty("stringFoo", "x")); 98 ASSERT_EQ("x", subscription->GetProperty("stringFoo", "x"));
99 ASSERT_EQ(42, subscription->GetProperty("intFoo", 42)); 99 ASSERT_EQ(42, subscription->GetProperty("intFoo", 42));
100 ASSERT_FALSE(subscription->GetProperty("boolFoo", false)); 100 ASSERT_FALSE(subscription->GetProperty("boolFoo", false));
101 101
102 subscription->SetProperty("stringFoo", "y"); 102 subscription->SetProperty("stringFoo", "y");
103 subscription->SetProperty("intFoo", 24); 103 subscription->SetProperty("intFoo", 24);
104 subscription->SetProperty("boolFoo", true); 104 subscription->SetProperty("boolFoo", true);
105 ASSERT_EQ("y", subscription->GetProperty("stringFoo", "x")); 105 ASSERT_EQ("y", subscription->GetProperty("stringFoo", "x"));
106 ASSERT_EQ(24, subscription->GetProperty("intFoo", 42)); 106 ASSERT_EQ(24, subscription->GetProperty("intFoo", 42));
107 ASSERT_TRUE(subscription->GetProperty("boolFoo", false)); 107 ASSERT_TRUE(subscription->GetProperty("boolFoo", false));
108 } 108 }
109 109
110 TEST(FilterEngineStubsTest, AddRemoveSubscriptions) 110 TEST(FilterEngineStubsTest, AddRemoveSubscriptions)
111 { 111 {
112 AdblockPlus::JsEngine jsEngine; 112 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
113 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(new DummyFileSystem())); 113 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new DummyFileSystem()));
114 AdblockPlus::FilterEngine filterEngine(jsEngine); 114 AdblockPlus::FilterEngine filterEngine(jsEngine);
115 ASSERT_EQ(0u, filterEngine.GetListedSubscriptions().size()); 115 ASSERT_EQ(0u, filterEngine.GetListedSubscriptions().size());
116 AdblockPlus::SubscriptionPtr subscription = filterEngine.GetSubscription("foo" ); 116 AdblockPlus::SubscriptionPtr subscription = filterEngine.GetSubscription("foo" );
117 ASSERT_EQ(0u, filterEngine.GetListedSubscriptions().size()); 117 ASSERT_EQ(0u, filterEngine.GetListedSubscriptions().size());
118 subscription->AddToList(); 118 subscription->AddToList();
119 ASSERT_EQ(1u, filterEngine.GetListedSubscriptions().size()); 119 ASSERT_EQ(1u, filterEngine.GetListedSubscriptions().size());
120 ASSERT_EQ(*subscription, *filterEngine.GetListedSubscriptions()[0]); 120 ASSERT_EQ(*subscription, *filterEngine.GetListedSubscriptions()[0]);
121 subscription->AddToList(); 121 subscription->AddToList();
122 ASSERT_EQ(1u, filterEngine.GetListedSubscriptions().size()); 122 ASSERT_EQ(1u, filterEngine.GetListedSubscriptions().size());
123 ASSERT_EQ(*subscription, *filterEngine.GetListedSubscriptions()[0]); 123 ASSERT_EQ(*subscription, *filterEngine.GetListedSubscriptions()[0]);
124 subscription->RemoveFromList(); 124 subscription->RemoveFromList();
125 ASSERT_EQ(0u, filterEngine.GetListedSubscriptions().size()); 125 ASSERT_EQ(0u, filterEngine.GetListedSubscriptions().size());
126 subscription->RemoveFromList(); 126 subscription->RemoveFromList();
127 ASSERT_EQ(0u, filterEngine.GetListedSubscriptions().size()); 127 ASSERT_EQ(0u, filterEngine.GetListedSubscriptions().size());
128 } 128 }
129 129
130 TEST(FilterEngineStubsTest, SubscriptionUpdates) 130 TEST(FilterEngineStubsTest, SubscriptionUpdates)
131 { 131 {
132 AdblockPlus::JsEngine jsEngine; 132 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
133 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(new DummyFileSystem())); 133 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new DummyFileSystem()));
134 AdblockPlus::FilterEngine filterEngine(jsEngine); 134 AdblockPlus::FilterEngine filterEngine(jsEngine);
135 AdblockPlus::SubscriptionPtr subscription = filterEngine.GetSubscription("foo" ); 135 AdblockPlus::SubscriptionPtr subscription = filterEngine.GetSubscription("foo" );
136 ASSERT_FALSE(subscription->IsUpdating()); 136 ASSERT_FALSE(subscription->IsUpdating());
137 137
138 //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
139 //subscription->UpdateFilters(); 139 //subscription->UpdateFilters();
140 } 140 }
141 141
142 TEST(FilterEngineStubsTest, Matches) 142 TEST(FilterEngineStubsTest, Matches)
143 { 143 {
144 AdblockPlus::JsEngine jsEngine; 144 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
145 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(new DummyFileSystem())); 145 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new DummyFileSystem()));
146 AdblockPlus::FilterEngine filterEngine(jsEngine); 146 AdblockPlus::FilterEngine filterEngine(jsEngine);
147 147
148 filterEngine.GetFilter("adbanner.gif")->AddToList(); 148 filterEngine.GetFilter("adbanner.gif")->AddToList();
149 filterEngine.GetFilter("@@notbanner.gif")->AddToList(); 149 filterEngine.GetFilter("@@notbanner.gif")->AddToList();
150 150
151 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", "");
152 ASSERT_FALSE(match1); 152 ASSERT_FALSE(match1);
153 153
154 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", "");
155 ASSERT_TRUE(match2); 155 ASSERT_TRUE(match2);
156 ASSERT_EQ(AdblockPlus::Filter::TYPE_BLOCKING, match2->GetProperty("type", -1)) ; 156 ASSERT_EQ(AdblockPlus::Filter::TYPE_BLOCKING, match2->GetProperty("type", -1)) ;
157 157
158 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", "");
159 ASSERT_TRUE(match3); 159 ASSERT_TRUE(match3);
160 ASSERT_EQ(AdblockPlus::Filter::TYPE_EXCEPTION, match3->GetProperty("type", -1) ); 160 ASSERT_EQ(AdblockPlus::Filter::TYPE_EXCEPTION, match3->GetProperty("type", -1) );
161 161
162 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", "");
163 ASSERT_TRUE(match4); 163 ASSERT_TRUE(match4);
164 ASSERT_EQ(AdblockPlus::Filter::TYPE_EXCEPTION, match4->GetProperty("type", -1) ); 164 ASSERT_EQ(AdblockPlus::Filter::TYPE_EXCEPTION, match4->GetProperty("type", -1) );
165 } 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