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

Side by Side Diff: test/WebRequest.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
« src/JsEngine.cpp ('K') | « test/JsValue.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #include <sstream> 1 #include <sstream>
2 #include <AdblockPlus.h> 2 #include <AdblockPlus.h>
3 #include <gtest/gtest.h> 3 #include <gtest/gtest.h>
4 4
5 #include "../src/Thread.h" 5 #include "../src/Thread.h"
6 6
7 class TestWebRequest : public AdblockPlus::WebRequest 7 class TestWebRequest : public AdblockPlus::WebRequest
8 { 8 {
9 public: 9 public:
10 AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::Hea derList& requestHeaders) const 10 AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::Hea derList& requestHeaders) const
(...skipping 11 matching lines...) Expand all
22 22
23 std::string ToString(unsigned int i) 23 std::string ToString(unsigned int i)
24 { 24 {
25 std::stringstream stream; 25 std::stringstream stream;
26 stream << i; 26 stream << i;
27 return stream.str(); 27 return stream.str();
28 } 28 }
29 29
30 TEST(WebRequestTest, BadCall) 30 TEST(WebRequestTest, BadCall)
31 { 31 {
32 AdblockPlus::JsEngine jsEngine; 32 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
33 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET()")); 33 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET()"));
34 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('', {}, function(){})")); 34 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('', {}, function(){})"));
35 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET({toString: false}, {}, fun ction(){})")); 35 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET({toString: false}, {}, fu nction(){})"));
36 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('http://example.com/', nul l, function(){})")); 36 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', nu ll, function(){})"));
37 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('http://example.com/', {}, null)")); 37 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', {} , null)"));
38 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('http://example.com/', {}, function(){}, 0)")); 38 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', {} , function(){}, 0)"));
39 } 39 }
40 40
41 TEST(WebRequestTest, TestWebRequest) 41 TEST(WebRequestTest, TestWebRequest)
42 { 42 {
43 AdblockPlus::JsEngine jsEngine; 43 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
44 jsEngine.SetWebRequest(AdblockPlus::WebRequestPtr(new TestWebRequest())); 44 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new TestWebRequest()));
45 jsEngine.Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function(r esult) {foo = result;} )"); 45 jsEngine->Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function( result) {foo = result;} )");
46 ASSERT_TRUE(jsEngine.Evaluate("this.foo")->IsUndefined()); 46 ASSERT_TRUE(jsEngine->Evaluate("this.foo")->IsUndefined());
47 AdblockPlus::Sleep(200); 47 AdblockPlus::Sleep(200);
48 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine.Evaluate("foo.status")->AsI nt()); 48 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("foo.status")->As Int());
49 ASSERT_EQ(123, jsEngine.Evaluate("foo.responseStatus")->AsInt()); 49 ASSERT_EQ(123, jsEngine->Evaluate("foo.responseStatus")->AsInt());
50 ASSERT_EQ("http://example.com/\nX\nY", jsEngine.Evaluate("foo.responseText")-> AsString()); 50 ASSERT_EQ("http://example.com/\nX\nY", jsEngine->Evaluate("foo.responseText")- >AsString());
51 ASSERT_EQ("{\"Foo\":\"Bar\"}", jsEngine.Evaluate("JSON.stringify(foo.responseH eaders)")->AsString()); 51 ASSERT_EQ("{\"Foo\":\"Bar\"}", jsEngine->Evaluate("JSON.stringify(foo.response Headers)")->AsString());
52 } 52 }
53 53
54 #if defined(HAVE_CURL) or defined(HAVE_WININET) 54 #if defined(HAVE_CURL) or defined(HAVE_WININET)
55 TEST(WebRequestTest, RealWebRequest) 55 TEST(WebRequestTest, RealWebRequest)
56 { 56 {
57 AdblockPlus::JsEngine jsEngine; 57 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
58 58
59 // This URL should redirect to easylist-downloads.adblockplus.org and we 59 // This URL should redirect to easylist-downloads.adblockplus.org and we
60 // should get the actual filter list back. 60 // should get the actual filter list back.
61 jsEngine.Evaluate("_webRequest.GET('https://easylist.adblockplus.org/easylist. txt', {}, function(result) {foo = result;} )"); 61 jsEngine->Evaluate("_webRequest.GET('https://easylist.adblockplus.org/easylist .txt', {}, function(result) {foo = result;} )");
62 do 62 do
63 { 63 {
64 AdblockPlus::Sleep(200); 64 AdblockPlus::Sleep(200);
65 } while (jsEngine.Evaluate("this.foo")->IsUndefined()); 65 } while (jsEngine->Evaluate("this.foo")->IsUndefined());
66 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine.Evaluate("foo.status")->AsI nt()); 66 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("foo.status")->As Int());
67 ASSERT_EQ(200, jsEngine.Evaluate("foo.responseStatus")->AsInt()); 67 ASSERT_EQ(200, jsEngine->Evaluate("foo.responseStatus")->AsInt());
68 ASSERT_EQ("[Adblock Plus ", jsEngine.Evaluate("foo.responseText.substr(0, 14)" )->AsString()); 68 ASSERT_EQ("[Adblock Plus ", jsEngine->Evaluate("foo.responseText.substr(0, 14) ")->AsString());
69 ASSERT_EQ("text/plain", jsEngine.Evaluate("foo.responseHeaders['content-type'] .substr(0, 10)")->AsString()); 69 ASSERT_EQ("text/plain", jsEngine->Evaluate("foo.responseHeaders['content-type' ].substr(0, 10)")->AsString());
70 ASSERT_TRUE(jsEngine.Evaluate("foo.responseHeaders['location']")->IsUndefined( )); 70 ASSERT_TRUE(jsEngine->Evaluate("foo.responseHeaders['location']")->IsUndefined ());
71 } 71 }
72 72
73 #if !FILTER_ENGINE_STUBS 73 #if !FILTER_ENGINE_STUBS
74 TEST(WebRequestTest, XMLHttpRequest) 74 TEST(WebRequestTest, XMLHttpRequest)
75 { 75 {
76 AdblockPlus::JsEngine jsEngine; 76 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
77 AdblockPlus::FilterEngine filterEngine(jsEngine); 77 AdblockPlus::FilterEngine filterEngine(jsEngine);
78 78
79 jsEngine.Evaluate("\ 79 jsEngine->Evaluate("\
80 var result;\ 80 var result;\
81 var request = new XMLHttpRequest();\ 81 var request = new XMLHttpRequest();\
82 request.open('GET', 'https://easylist.adblockplus.org/easylist.txt');\ 82 request.open('GET', 'https://easylist.adblockplus.org/easylist.txt');\
83 request.setRequestHeader('X', 'Y');\ 83 request.setRequestHeader('X', 'Y');\
84 request.overrideMimeType('text/plain');\ 84 request.overrideMimeType('text/plain');\
85 request.addEventListener('load', function() {result = request.responseText;} , false);\ 85 request.addEventListener('load', function() {result = request.responseText;} , false);\
86 request.addEventListener('error', function() {result = 'error';}, false);\ 86 request.addEventListener('error', function() {result = 'error';}, false);\
87 request.send(null);"); 87 request.send(null);");
88 do 88 do
89 { 89 {
90 AdblockPlus::Sleep(200); 90 AdblockPlus::Sleep(200);
91 } while (jsEngine.Evaluate("result")->IsUndefined()); 91 } while (jsEngine->Evaluate("result")->IsUndefined());
92 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine.Evaluate("request.channel.s tatus")->AsInt()); 92 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("request.channel. status")->AsInt());
93 ASSERT_EQ(200, jsEngine.Evaluate("request.status")->AsInt()); 93 ASSERT_EQ(200, jsEngine->Evaluate("request.status")->AsInt());
94 ASSERT_EQ("[Adblock Plus ", jsEngine.Evaluate("result.substr(0, 14)")->AsStrin g()); 94 ASSERT_EQ("[Adblock Plus ", jsEngine->Evaluate("result.substr(0, 14)")->AsStri ng());
95 ASSERT_EQ("text/plain", jsEngine.Evaluate("request.getResponseHeader('Content- Type').substr(0, 10)")->AsString()); 95 ASSERT_EQ("text/plain", jsEngine->Evaluate("request.getResponseHeader('Content -Type').substr(0, 10)")->AsString());
96 ASSERT_TRUE(jsEngine.Evaluate("request.getResponseHeader('Location')")->IsNull ()); 96 ASSERT_TRUE(jsEngine->Evaluate("request.getResponseHeader('Location')")->IsNul l());
97 } 97 }
98 #endif 98 #endif
99 99
100 #else 100 #else
101 TEST(WebRequestTest, DummyWebRequest) 101 TEST(WebRequestTest, DummyWebRequest)
102 { 102 {
103 AdblockPlus::JsEngine jsEngine; 103 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
104 jsEngine.Evaluate("_webRequest.GET('https://easylist.adblockplus.org/easylist. txt', {}, function(result) {foo = result;} )"); 104 jsEngine->Evaluate("_webRequest.GET('https://easylist.adblockplus.org/easylist .txt', {}, function(result) {foo = result;} )");
105 do 105 do
106 { 106 {
107 AdblockPlus::Sleep(200); 107 AdblockPlus::Sleep(200);
108 } while (jsEngine.Evaluate("this.foo")->IsUndefined()); 108 } while (jsEngine->Evaluate("this.foo")->IsUndefined());
109 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine.Evaluate("foo.st atus")->AsInt()); 109 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine->Evaluate("foo.s tatus")->AsInt());
110 ASSERT_EQ(0, jsEngine.Evaluate("foo.responseStatus")->AsInt()); 110 ASSERT_EQ(0, jsEngine->Evaluate("foo.responseStatus")->AsInt());
111 ASSERT_EQ("", jsEngine.Evaluate("foo.responseText")->AsString()); 111 ASSERT_EQ("", jsEngine->Evaluate("foo.responseText")->AsString());
112 ASSERT_EQ("{}", jsEngine.Evaluate("JSON.stringify(foo.responseHeaders)")->AsSt ring()); 112 ASSERT_EQ("{}", jsEngine->Evaluate("JSON.stringify(foo.responseHeaders)")->AsS tring());
113 } 113 }
114 114
115 #if !FILTER_ENGINE_STUBS 115 #if !FILTER_ENGINE_STUBS
116 TEST(WebRequestTest, XMLHttpRequest) 116 TEST(WebRequestTest, XMLHttpRequest)
117 { 117 {
118 AdblockPlus::JsEngine jsEngine; 118 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
119 AdblockPlus::FilterEngine filterEngine(jsEngine); 119 AdblockPlus::FilterEngine filterEngine(jsEngine);
120 120
121 jsEngine.Evaluate("\ 121 jsEngine->Evaluate("\
122 var result;\ 122 var result;\
123 var request = new XMLHttpRequest();\ 123 var request = new XMLHttpRequest();\
124 request.open('GET', 'https://easylist.adblockplus.org/easylist.txt');\ 124 request.open('GET', 'https://easylist.adblockplus.org/easylist.txt');\
125 request.setRequestHeader('X', 'Y');\ 125 request.setRequestHeader('X', 'Y');\
126 request.overrideMimeType('text/plain');\ 126 request.overrideMimeType('text/plain');\
127 request.addEventListener('load', function() {result = request.responseText;} , false);\ 127 request.addEventListener('load', function() {result = request.responseText;} , false);\
128 request.addEventListener('error', function() {result = 'error';}, false);\ 128 request.addEventListener('error', function() {result = 'error';}, false);\
129 request.send(null);"); 129 request.send(null);");
130 do 130 do
131 { 131 {
132 AdblockPlus::Sleep(200); 132 AdblockPlus::Sleep(200);
133 } while (jsEngine.Evaluate("result")->IsUndefined()); 133 } while (jsEngine->Evaluate("result")->IsUndefined());
134 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine.Evaluate("reques t.channel.status")->AsInt()); 134 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine->Evaluate("reque st.channel.status")->AsInt());
135 ASSERT_EQ(0, jsEngine.Evaluate("request.status")->AsInt()); 135 ASSERT_EQ(0, jsEngine->Evaluate("request.status")->AsInt());
136 ASSERT_EQ("error", jsEngine.Evaluate("result")->AsString()); 136 ASSERT_EQ("error", jsEngine->Evaluate("result")->AsString());
137 ASSERT_TRUE(jsEngine.Evaluate("request.getResponseHeader('Content-Type')")->Is Null()); 137 ASSERT_TRUE(jsEngine->Evaluate("request.getResponseHeader('Content-Type')")->I sNull());
138 } 138 }
139 #endif 139 #endif
140 140
141 #endif 141 #endif
OLDNEW
« src/JsEngine.cpp ('K') | « test/JsValue.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld