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

Side by Side Diff: test/FileSystemJsObject.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 <AdblockPlus.h> 1 #include <AdblockPlus.h>
2 #include <gtest/gtest.h> 2 #include <gtest/gtest.h>
3 3
4 #include "../src/Thread.h" 4 #include "../src/Thread.h"
5 #include "../src/Utils.h" 5 #include "../src/Utils.h"
6 6
7 namespace 7 namespace
8 { 8 {
9 class MockFileSystem : public AdblockPlus::FileSystem 9 class MockFileSystem : public AdblockPlus::FileSystem
10 { 10 {
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 statPath = path; 66 statPath = path;
67 StatResult result; 67 StatResult result;
68 result.exists = statExists; 68 result.exists = statExists;
69 result.isDirectory = statIsDirectory; 69 result.isDirectory = statIsDirectory;
70 result.isFile = statIsFile; 70 result.isFile = statIsFile;
71 result.lastModified = statLastModified; 71 result.lastModified = statLastModified;
72 return result; 72 return result;
73 } 73 }
74 }; 74 };
75 75
76 void ReadFile(AdblockPlus::JsEngine& jsEngine, std::string& content, 76 void ReadFile(AdblockPlus::JsEnginePtr jsEngine, std::string& content,
77 std::string& error) 77 std::string& error)
78 { 78 {
79 jsEngine.Evaluate("_fileSystem.read('', function(r) {result = r})"); 79 jsEngine->Evaluate("_fileSystem.read('', function(r) {result = r})");
80 AdblockPlus::Sleep(50); 80 AdblockPlus::Sleep(50);
81 content = jsEngine.Evaluate("result.content")->AsString(); 81 content = jsEngine->Evaluate("result.content")->AsString();
82 error = jsEngine.Evaluate("result.error")->AsString(); 82 error = jsEngine->Evaluate("result.error")->AsString();
83 } 83 }
84 } 84 }
85 85
86 TEST(FileSystemJsObjectTest, Read) 86 TEST(FileSystemJsObjectTest, Read)
87 { 87 {
88 AdblockPlus::JsEngine jsEngine; 88 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
89 MockFileSystem* fileSystem = new MockFileSystem(); 89 MockFileSystem* fileSystem = new MockFileSystem();
90 fileSystem->contentToRead = "foo"; 90 fileSystem->contentToRead = "foo";
91 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));; 91 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));;
92 std::string content; 92 std::string content;
93 std::string error; 93 std::string error;
94 ReadFile(jsEngine, content, error); 94 ReadFile(jsEngine, content, error);
95 ASSERT_EQ("foo", content); 95 ASSERT_EQ("foo", content);
96 ASSERT_EQ("", error); 96 ASSERT_EQ("", error);
97 } 97 }
98 98
99 TEST(FileSystemJsObjectTest, ReadIllegalArguments) 99 TEST(FileSystemJsObjectTest, ReadIllegalArguments)
100 { 100 {
101 AdblockPlus::JsEngine jsEngine; 101 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
102 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.read()")); 102 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.read()"));
103 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.read('', '')")); 103 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.read('', '')"));
104 } 104 }
105 105
106 TEST(FileSystemJsObjectTest, ReadError) 106 TEST(FileSystemJsObjectTest, ReadError)
107 { 107 {
108 AdblockPlus::JsEngine jsEngine; 108 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
109 MockFileSystem* fileSystem = new MockFileSystem(); 109 MockFileSystem* fileSystem = new MockFileSystem();
110 fileSystem->success = false; 110 fileSystem->success = false;
111 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));; 111 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));;
112 std::string content; 112 std::string content;
113 std::string error; 113 std::string error;
114 ReadFile(jsEngine, content, error); 114 ReadFile(jsEngine, content, error);
115 ASSERT_NE("", error); 115 ASSERT_NE("", error);
116 ASSERT_EQ("", content); 116 ASSERT_EQ("", content);
117 } 117 }
118 118
119 TEST(FileSystemJsObjectTest, Write) 119 TEST(FileSystemJsObjectTest, Write)
120 { 120 {
121 AdblockPlus::JsEngine jsEngine; 121 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
122 MockFileSystem* fileSystem = new MockFileSystem(); 122 MockFileSystem* fileSystem = new MockFileSystem();
123 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));; 123 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));;
124 jsEngine.Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})"); 124 jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})") ;
125 AdblockPlus::Sleep(50); 125 AdblockPlus::Sleep(50);
126 ASSERT_EQ("foo", fileSystem->lastWrittenPath); 126 ASSERT_EQ("foo", fileSystem->lastWrittenPath);
127 ASSERT_EQ("bar", fileSystem->lastWrittenContent); 127 ASSERT_EQ("bar", fileSystem->lastWrittenContent);
128 ASSERT_EQ("", jsEngine.Evaluate("error")->AsString()); 128 ASSERT_EQ("", jsEngine->Evaluate("error")->AsString());
129 } 129 }
130 130
131 TEST(FileSystemJsObjectTest, WriteIllegalArguments) 131 TEST(FileSystemJsObjectTest, WriteIllegalArguments)
132 { 132 {
133 AdblockPlus::JsEngine jsEngine; 133 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
134 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.write()")); 134 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write()"));
135 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.write('', '', '')")); 135 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write('', '', '')"));
136 } 136 }
137 137
138 TEST(FileSystemJsObjectTest, WriteError) 138 TEST(FileSystemJsObjectTest, WriteError)
139 { 139 {
140 AdblockPlus::JsEngine jsEngine; 140 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
141 MockFileSystem* fileSystem = new MockFileSystem(); 141 MockFileSystem* fileSystem = new MockFileSystem();
142 fileSystem->success = false; 142 fileSystem->success = false;
143 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));; 143 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));;
144 jsEngine.Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})"); 144 jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})") ;
145 AdblockPlus::Sleep(50); 145 AdblockPlus::Sleep(50);
146 ASSERT_NE("", jsEngine.Evaluate("error")->AsString()); 146 ASSERT_NE("", jsEngine->Evaluate("error")->AsString());
147 } 147 }
148 148
149 TEST(FileSystemJsObjectTest, Move) 149 TEST(FileSystemJsObjectTest, Move)
150 { 150 {
151 AdblockPlus::JsEngine jsEngine; 151 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
152 MockFileSystem* fileSystem = new MockFileSystem(); 152 MockFileSystem* fileSystem = new MockFileSystem();
153 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));; 153 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));;
154 jsEngine.Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); 154 jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})");
155 AdblockPlus::Sleep(50); 155 AdblockPlus::Sleep(50);
156 ASSERT_EQ("foo", fileSystem->movedFrom); 156 ASSERT_EQ("foo", fileSystem->movedFrom);
157 ASSERT_EQ("bar", fileSystem->movedTo); 157 ASSERT_EQ("bar", fileSystem->movedTo);
158 ASSERT_EQ("", jsEngine.Evaluate("error")->AsString()); 158 ASSERT_EQ("", jsEngine->Evaluate("error")->AsString());
159 } 159 }
160 160
161 TEST(FileSystemJsObjectTest, MoveIllegalArguments) 161 TEST(FileSystemJsObjectTest, MoveIllegalArguments)
162 { 162 {
163 AdblockPlus::JsEngine jsEngine; 163 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
164 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.move()")); 164 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.move()"));
165 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.move('', '', '')")); 165 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.move('', '', '')"));
166 } 166 }
167 167
168 TEST(FileSystemJsObjectTest, MoveError) 168 TEST(FileSystemJsObjectTest, MoveError)
169 { 169 {
170 AdblockPlus::JsEngine jsEngine; 170 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
171 MockFileSystem* fileSystem = new MockFileSystem(); 171 MockFileSystem* fileSystem = new MockFileSystem();
172 fileSystem->success = false; 172 fileSystem->success = false;
173 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));; 173 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));;
174 jsEngine.Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); 174 jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})");
175 AdblockPlus::Sleep(50); 175 AdblockPlus::Sleep(50);
176 ASSERT_NE("", jsEngine.Evaluate("error")->AsString()); 176 ASSERT_NE("", jsEngine->Evaluate("error")->AsString());
177 } 177 }
178 178
179 TEST(FileSystemJsObjectTest, Remove) 179 TEST(FileSystemJsObjectTest, Remove)
180 { 180 {
181 AdblockPlus::JsEngine jsEngine; 181 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
182 MockFileSystem* fileSystem = new MockFileSystem(); 182 MockFileSystem* fileSystem = new MockFileSystem();
183 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));; 183 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));;
184 jsEngine.Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); 184 jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e})");
185 AdblockPlus::Sleep(50); 185 AdblockPlus::Sleep(50);
186 ASSERT_EQ("foo", fileSystem->removedPath); 186 ASSERT_EQ("foo", fileSystem->removedPath);
187 ASSERT_EQ("", jsEngine.Evaluate("error")->AsString()); 187 ASSERT_EQ("", jsEngine->Evaluate("error")->AsString());
188 } 188 }
189 189
190 TEST(FileSystemJsObjectTest, RemoveIllegalArguments) 190 TEST(FileSystemJsObjectTest, RemoveIllegalArguments)
191 { 191 {
192 AdblockPlus::JsEngine jsEngine; 192 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
193 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.remove()")); 193 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.remove()"));
194 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.remove('', '')")); 194 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.remove('', '')"));
195 } 195 }
196 196
197 TEST(FileSystemJsObjectTest, RemoveError) 197 TEST(FileSystemJsObjectTest, RemoveError)
198 { 198 {
199 AdblockPlus::JsEngine jsEngine; 199 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
200 MockFileSystem* fileSystem = new MockFileSystem(); 200 MockFileSystem* fileSystem = new MockFileSystem();
201 fileSystem->success = false; 201 fileSystem->success = false;
202 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));; 202 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));;
203 jsEngine.Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); 203 jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e})");
204 AdblockPlus::Sleep(50); 204 AdblockPlus::Sleep(50);
205 ASSERT_NE("", jsEngine.Evaluate("error")->AsString()); 205 ASSERT_NE("", jsEngine->Evaluate("error")->AsString());
206 } 206 }
207 207
208 TEST(FileSystemJsObjectTest, Stat) 208 TEST(FileSystemJsObjectTest, Stat)
209 { 209 {
210 AdblockPlus::JsEngine jsEngine; 210 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
211 MockFileSystem* fileSystem = new MockFileSystem(); 211 MockFileSystem* fileSystem = new MockFileSystem();
212 fileSystem->statExists = true; 212 fileSystem->statExists = true;
213 fileSystem->statIsDirectory= false; 213 fileSystem->statIsDirectory= false;
214 fileSystem->statIsFile = true; 214 fileSystem->statIsFile = true;
215 fileSystem->statLastModified = 1337; 215 fileSystem->statLastModified = 1337;
216 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));; 216 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));;
217 jsEngine.Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); 217 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})");
218 AdblockPlus::Sleep(50); 218 AdblockPlus::Sleep(50);
219 ASSERT_EQ("foo", fileSystem->statPath); 219 ASSERT_EQ("foo", fileSystem->statPath);
220 ASSERT_EQ("", jsEngine.Evaluate("result.error")->AsString()); 220 ASSERT_EQ("", jsEngine->Evaluate("result.error")->AsString());
221 ASSERT_TRUE(jsEngine.Evaluate("result.exists")->AsBool()); 221 ASSERT_TRUE(jsEngine->Evaluate("result.exists")->AsBool());
222 ASSERT_FALSE(jsEngine.Evaluate("result.isDirectory")->AsBool()); 222 ASSERT_FALSE(jsEngine->Evaluate("result.isDirectory")->AsBool());
223 ASSERT_TRUE(jsEngine.Evaluate("result.isFile")->AsBool()); 223 ASSERT_TRUE(jsEngine->Evaluate("result.isFile")->AsBool());
224 ASSERT_EQ(1337, jsEngine.Evaluate("result.lastModified")->AsInt()); 224 ASSERT_EQ(1337, jsEngine->Evaluate("result.lastModified")->AsInt());
225 } 225 }
226 226
227 TEST(FileSystemJsObjectTest, StatIllegalArguments) 227 TEST(FileSystemJsObjectTest, StatIllegalArguments)
228 { 228 {
229 AdblockPlus::JsEngine jsEngine; 229 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
230 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.stat()")); 230 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat()"));
231 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.stat('', '')")); 231 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat('', '')"));
232 } 232 }
233 233
234 TEST(FileSystemJsObjectTest, StatError) 234 TEST(FileSystemJsObjectTest, StatError)
235 { 235 {
236 AdblockPlus::JsEngine jsEngine; 236 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
237 MockFileSystem* fileSystem = new MockFileSystem(); 237 MockFileSystem* fileSystem = new MockFileSystem();
238 fileSystem->success = false; 238 fileSystem->success = false;
239 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));; 239 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));;
240 jsEngine.Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); 240 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})");
241 AdblockPlus::Sleep(50); 241 AdblockPlus::Sleep(50);
242 ASSERT_NE("", jsEngine.Evaluate("result.error")->AsString()); 242 ASSERT_NE("", jsEngine->Evaluate("result.error")->AsString());
243 } 243 }
OLDNEW
« src/JsEngine.cpp ('K') | « test/ConsoleJsObject.cpp ('k') | test/FilterEngineStubs.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld