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

Side by Side Diff: test/FileSystemJsObject.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 <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 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 { 78 {
79 jsEngine.Evaluate("_fileSystem.read('', function(r) {result = r})"); 79 jsEngine.Evaluate("_fileSystem.read('', function(r) {result = r})");
80 AdblockPlus::Sleep(10); 80 AdblockPlus::Sleep(10);
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 MockFileSystem fileSystem; 88 AdblockPlus::JsEngine jsEngine;
89 fileSystem.contentToRead = "foo"; 89 MockFileSystem* fileSystem = new MockFileSystem();
90 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), &fileSystem, 0, 0); 90 fileSystem->contentToRead = "foo";
91 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));;
91 std::string content; 92 std::string content;
92 std::string error; 93 std::string error;
93 ReadFile(jsEngine, content, error); 94 ReadFile(jsEngine, content, error);
94 ASSERT_EQ("foo", content); 95 ASSERT_EQ("foo", content);
95 ASSERT_EQ("", error); 96 ASSERT_EQ("", error);
96 } 97 }
97 98
98 TEST(FileSystemJsObjectTest, ReadIllegalArguments) 99 TEST(FileSystemJsObjectTest, ReadIllegalArguments)
99 { 100 {
100 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, 0); 101 AdblockPlus::JsEngine jsEngine;
101 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.read()")); 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 } 104 }
104 105
105 TEST(FileSystemJsObjectTest, ReadError) 106 TEST(FileSystemJsObjectTest, ReadError)
106 { 107 {
107 MockFileSystem fileSystem; 108 AdblockPlus::JsEngine jsEngine;
108 fileSystem.success = false; 109 MockFileSystem* fileSystem = new MockFileSystem();
109 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), &fileSystem, 0, 0); 110 fileSystem->success = false;
111 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));;
110 std::string content; 112 std::string content;
111 std::string error; 113 std::string error;
112 ReadFile(jsEngine, content, error); 114 ReadFile(jsEngine, content, error);
113 ASSERT_NE("", error); 115 ASSERT_NE("", error);
114 ASSERT_EQ("", content); 116 ASSERT_EQ("", content);
115 } 117 }
116 118
117 TEST(FileSystemJsObjectTest, Write) 119 TEST(FileSystemJsObjectTest, Write)
118 { 120 {
119 MockFileSystem fileSystem; 121 AdblockPlus::JsEngine jsEngine;
120 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), &fileSystem, 0, 0); 122 MockFileSystem* fileSystem = new MockFileSystem();
123 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));;
121 jsEngine.Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})"); 124 jsEngine.Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})");
122 AdblockPlus::Sleep(10); 125 AdblockPlus::Sleep(10);
123 ASSERT_EQ("foo", fileSystem.lastWrittenPath); 126 ASSERT_EQ("foo", fileSystem->lastWrittenPath);
124 ASSERT_EQ("bar", fileSystem.lastWrittenContent); 127 ASSERT_EQ("bar", fileSystem->lastWrittenContent);
125 ASSERT_EQ("", jsEngine.Evaluate("error")->AsString()); 128 ASSERT_EQ("", jsEngine.Evaluate("error")->AsString());
126 } 129 }
127 130
128 TEST(FileSystemJsObjectTest, WriteIllegalArguments) 131 TEST(FileSystemJsObjectTest, WriteIllegalArguments)
129 { 132 {
130 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, 0); 133 AdblockPlus::JsEngine jsEngine;
131 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.write()")); 134 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.write()"));
132 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.write('', '', '')")); 135 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.write('', '', '')"));
133 } 136 }
134 137
135 TEST(FileSystemJsObjectTest, WriteError) 138 TEST(FileSystemJsObjectTest, WriteError)
136 { 139 {
137 MockFileSystem fileSystem; 140 AdblockPlus::JsEngine jsEngine;
138 fileSystem.success = false; 141 MockFileSystem* fileSystem = new MockFileSystem();
139 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), &fileSystem, 0, 0); 142 fileSystem->success = false;
143 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));;
140 jsEngine.Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})"); 144 jsEngine.Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})");
141 AdblockPlus::Sleep(10); 145 AdblockPlus::Sleep(10);
142 ASSERT_NE("", jsEngine.Evaluate("error")->AsString()); 146 ASSERT_NE("", jsEngine.Evaluate("error")->AsString());
143 } 147 }
144 148
145 TEST(FileSystemJsObjectTest, Move) 149 TEST(FileSystemJsObjectTest, Move)
146 { 150 {
147 MockFileSystem fileSystem; 151 AdblockPlus::JsEngine jsEngine;
148 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), &fileSystem, 0, 0); 152 MockFileSystem* fileSystem = new MockFileSystem();
153 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));;
149 jsEngine.Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); 154 jsEngine.Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})");
150 AdblockPlus::Sleep(10); 155 AdblockPlus::Sleep(10);
151 ASSERT_EQ("foo", fileSystem.movedFrom); 156 ASSERT_EQ("foo", fileSystem->movedFrom);
152 ASSERT_EQ("bar", fileSystem.movedTo); 157 ASSERT_EQ("bar", fileSystem->movedTo);
153 ASSERT_EQ("", jsEngine.Evaluate("error")->AsString()); 158 ASSERT_EQ("", jsEngine.Evaluate("error")->AsString());
154 } 159 }
155 160
156 TEST(FileSystemJsObjectTest, MoveIllegalArguments) 161 TEST(FileSystemJsObjectTest, MoveIllegalArguments)
157 { 162 {
158 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, 0); 163 AdblockPlus::JsEngine jsEngine;
159 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.move()")); 164 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.move()"));
160 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.move('', '', '')")); 165 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.move('', '', '')"));
161 } 166 }
162 167
163 TEST(FileSystemJsObjectTest, MoveError) 168 TEST(FileSystemJsObjectTest, MoveError)
164 { 169 {
165 MockFileSystem fileSystem; 170 AdblockPlus::JsEngine jsEngine;
166 fileSystem.success = false; 171 MockFileSystem* fileSystem = new MockFileSystem();
167 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), &fileSystem, 0, 0); 172 fileSystem->success = false;
173 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));;
168 jsEngine.Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); 174 jsEngine.Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})");
169 AdblockPlus::Sleep(10); 175 AdblockPlus::Sleep(10);
170 ASSERT_NE("", jsEngine.Evaluate("error")->AsString()); 176 ASSERT_NE("", jsEngine.Evaluate("error")->AsString());
171 } 177 }
172 178
173 TEST(FileSystemJsObjectTest, Remove) 179 TEST(FileSystemJsObjectTest, Remove)
174 { 180 {
175 MockFileSystem fileSystem; 181 AdblockPlus::JsEngine jsEngine;
176 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), &fileSystem, 0, 0); 182 MockFileSystem* fileSystem = new MockFileSystem();
183 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));;
177 jsEngine.Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); 184 jsEngine.Evaluate("_fileSystem.remove('foo', function(e) {error = e})");
178 AdblockPlus::Sleep(10); 185 AdblockPlus::Sleep(10);
179 ASSERT_EQ("foo", fileSystem.removedPath); 186 ASSERT_EQ("foo", fileSystem->removedPath);
180 ASSERT_EQ("", jsEngine.Evaluate("error")->AsString()); 187 ASSERT_EQ("", jsEngine.Evaluate("error")->AsString());
181 } 188 }
182 189
183 TEST(FileSystemJsObjectTest, RemoveIllegalArguments) 190 TEST(FileSystemJsObjectTest, RemoveIllegalArguments)
184 { 191 {
185 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, 0); 192 AdblockPlus::JsEngine jsEngine;
186 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.remove()")); 193 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.remove()"));
187 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.remove('', '')")); 194 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.remove('', '')"));
188 } 195 }
189 196
190 TEST(FileSystemJsObjectTest, RemoveError) 197 TEST(FileSystemJsObjectTest, RemoveError)
191 { 198 {
192 MockFileSystem fileSystem; 199 AdblockPlus::JsEngine jsEngine;
193 fileSystem.success = false; 200 MockFileSystem* fileSystem = new MockFileSystem();
194 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), &fileSystem, 0, 0); 201 fileSystem->success = false;
202 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));;
195 jsEngine.Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); 203 jsEngine.Evaluate("_fileSystem.remove('foo', function(e) {error = e})");
196 AdblockPlus::Sleep(10); 204 AdblockPlus::Sleep(10);
197 ASSERT_NE("", jsEngine.Evaluate("error")->AsString()); 205 ASSERT_NE("", jsEngine.Evaluate("error")->AsString());
198 } 206 }
199 207
200 TEST(FileSystemJsObjectTest, Stat) 208 TEST(FileSystemJsObjectTest, Stat)
201 { 209 {
202 MockFileSystem fileSystem; 210 AdblockPlus::JsEngine jsEngine;
203 fileSystem.statExists = true; 211 MockFileSystem* fileSystem = new MockFileSystem();
204 fileSystem.statIsDirectory= false; 212 fileSystem->statExists = true;
205 fileSystem.statIsFile = true; 213 fileSystem->statIsDirectory= false;
206 fileSystem.statLastModified = 1337; 214 fileSystem->statIsFile = true;
207 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), &fileSystem, 0, 0); 215 fileSystem->statLastModified = 1337;
216 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));;
208 jsEngine.Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); 217 jsEngine.Evaluate("_fileSystem.stat('foo', function(r) {result = r})");
209 AdblockPlus::Sleep(10); 218 AdblockPlus::Sleep(10);
210 ASSERT_EQ("foo", fileSystem.statPath); 219 ASSERT_EQ("foo", fileSystem->statPath);
211 ASSERT_EQ("", jsEngine.Evaluate("result.error")->AsString()); 220 ASSERT_EQ("", jsEngine.Evaluate("result.error")->AsString());
212 ASSERT_TRUE(jsEngine.Evaluate("result.exists")->AsBool()); 221 ASSERT_TRUE(jsEngine.Evaluate("result.exists")->AsBool());
213 ASSERT_FALSE(jsEngine.Evaluate("result.isDirectory")->AsBool()); 222 ASSERT_FALSE(jsEngine.Evaluate("result.isDirectory")->AsBool());
214 ASSERT_TRUE(jsEngine.Evaluate("result.isFile")->AsBool()); 223 ASSERT_TRUE(jsEngine.Evaluate("result.isFile")->AsBool());
215 ASSERT_EQ(1337, jsEngine.Evaluate("result.lastModified")->AsInt()); 224 ASSERT_EQ(1337, jsEngine.Evaluate("result.lastModified")->AsInt());
216 } 225 }
217 226
218 TEST(FileSystemJsObjectTest, StatIllegalArguments) 227 TEST(FileSystemJsObjectTest, StatIllegalArguments)
219 { 228 {
220 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, 0); 229 AdblockPlus::JsEngine jsEngine;
221 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.stat()")); 230 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.stat()"));
222 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.stat('', '')")); 231 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.stat('', '')"));
223 } 232 }
224 233
225 TEST(FileSystemJsObjectTest, StatError) 234 TEST(FileSystemJsObjectTest, StatError)
226 { 235 {
227 MockFileSystem fileSystem; 236 AdblockPlus::JsEngine jsEngine;
228 fileSystem.success = false; 237 MockFileSystem* fileSystem = new MockFileSystem();
229 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), &fileSystem, 0, 0); 238 fileSystem->success = false;
239 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));;
230 jsEngine.Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); 240 jsEngine.Evaluate("_fileSystem.stat('foo', function(r) {result = r})");
231 AdblockPlus::Sleep(10); 241 AdblockPlus::Sleep(10);
232 ASSERT_NE("", jsEngine.Evaluate("result.error")->AsString()); 242 ASSERT_NE("", jsEngine.Evaluate("result.error")->AsString());
233 } 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