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

Side by Side Diff: test/WebRequest.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
« 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 #ifdef HAVE_WININET
8 #include "../src/WebRequestWinInet.h"
9 #endif
10
11 class TestWebRequest : public AdblockPlus::WebRequest 7 class TestWebRequest : public AdblockPlus::WebRequest
12 { 8 {
13 public: 9 public:
14 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
15 { 11 {
16 AdblockPlus::Sleep(50); 12 AdblockPlus::Sleep(50);
17 13
18 AdblockPlus::ServerResponse result; 14 AdblockPlus::ServerResponse result;
19 result.status = NS_OK; 15 result.status = NS_OK;
20 result.responseStatus = 123; 16 result.responseStatus = 123;
21 result.responseHeaders.push_back(std::pair<std::string, std::string>("Foo", "Bar")); 17 result.responseHeaders.push_back(std::pair<std::string, std::string>("Foo", "Bar"));
22 result.responseText = url + "\n" + requestHeaders[0].first + "\n" + requestH eaders[0].second; 18 result.responseText = url + "\n" + requestHeaders[0].first + "\n" + requestH eaders[0].second;
23 return result; 19 return result;
24 } 20 }
25 }; 21 };
26 22
27 std::string ToString(unsigned int i) 23 std::string ToString(unsigned int i)
28 { 24 {
29 std::stringstream stream; 25 std::stringstream stream;
30 stream << i; 26 stream << i;
31 return stream.str(); 27 return stream.str();
32 } 28 }
33 29
34 TEST(WebRequestTest, BadCall) 30 TEST(WebRequestTest, BadCall)
35 { 31 {
36 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, 0); 32 AdblockPlus::JsEngine jsEngine;
37 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET()")); 33 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET()"));
38 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('', {}, function(){})")); 34 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('', {}, function(){})"));
39 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET({toString: false}, {}, fun ction(){})")); 35 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET({toString: false}, {}, fun ction(){})"));
40 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('http://example.com/', nul l, function(){})")); 36 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('http://example.com/', nul l, function(){})"));
41 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('http://example.com/', {}, null)")); 37 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('http://example.com/', {}, null)"));
42 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)"));
43 } 39 }
44 40
45 TEST(WebRequestTest, TestWebRequest) 41 TEST(WebRequestTest, TestWebRequest)
46 { 42 {
47 TestWebRequest webRequest; 43 AdblockPlus::JsEngine jsEngine;
48 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, &webRequest, 0); 44 jsEngine.SetWebRequest(AdblockPlus::WebRequestPtr(new TestWebRequest()));
49 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(r esult) {foo = result;} )");
50 ASSERT_TRUE(jsEngine.Evaluate("this.foo")->IsUndefined()); 46 ASSERT_TRUE(jsEngine.Evaluate("this.foo")->IsUndefined());
51 AdblockPlus::Sleep(200); 47 AdblockPlus::Sleep(200);
52 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine.Evaluate("foo.status")->AsI nt()); 48 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine.Evaluate("foo.status")->AsI nt());
53 ASSERT_EQ(123, jsEngine.Evaluate("foo.responseStatus")->AsInt()); 49 ASSERT_EQ(123, jsEngine.Evaluate("foo.responseStatus")->AsInt());
54 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());
55 ASSERT_EQ("{\"Foo\":\"Bar\"}", jsEngine.Evaluate("JSON.stringify(foo.responseH eaders)")->AsString()); 51 ASSERT_EQ("{\"Foo\":\"Bar\"}", jsEngine.Evaluate("JSON.stringify(foo.responseH eaders)")->AsString());
56 } 52 }
57 53
58 #if defined(HAVE_CURL) 54 #if defined(HAVE_CURL) or defined(HAVE_WININET)
59 TEST(WebRequestTest, RealWebRequest) 55 TEST(WebRequestTest, RealWebRequest)
60 { 56 {
61 AdblockPlus::DefaultWebRequest webRequest; 57 AdblockPlus::JsEngine jsEngine;
62 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, &webRequest, 0);
63 58
64 // This URL should redirect to easylist-downloads.adblockplus.org and we 59 // This URL should redirect to easylist-downloads.adblockplus.org and we
65 // should get the actual filter list back. 60 // should get the actual filter list back.
66 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;} )");
67 do 62 do
68 { 63 {
69 AdblockPlus::Sleep(200); 64 AdblockPlus::Sleep(200);
70 } while (jsEngine.Evaluate("this.foo")->IsUndefined()); 65 } while (jsEngine.Evaluate("this.foo")->IsUndefined());
71 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine.Evaluate("foo.status")->AsI nt()); 66 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine.Evaluate("foo.status")->AsI nt());
72 ASSERT_EQ(200, jsEngine.Evaluate("foo.responseStatus")->AsInt()); 67 ASSERT_EQ(200, jsEngine.Evaluate("foo.responseStatus")->AsInt());
73 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());
74 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());
75 ASSERT_TRUE(jsEngine.Evaluate("foo.responseHeaders['location']")->IsUndefined( )); 70 ASSERT_TRUE(jsEngine.Evaluate("foo.responseHeaders['location']")->IsUndefined( ));
76 } 71 }
77 #elif defined(HAVE_WININET)
78 TEST(WebRequestTest, RealWebRequest)
79 {
80 AdblockPlus::WebRequestWinInet webRequest;
81 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, &webRequest, 0);
82 jsEngine.Evaluate("_webRequest.GET('https://easylist.adblockplus.org/easylist. txt', {}, function(result) {foo = result;} )");
83 do
84 {
85 AdblockPlus::Sleep(200);
86 } while (jsEngine.Evaluate("typeof foo") == "undefined");
87 ASSERT_EQ(ToString(AdblockPlus::WebRequest::NS_OK), jsEngine.Evaluate("foo.sta tus"));
88 ASSERT_EQ("200", jsEngine.Evaluate("foo.responseStatus"));
89 ASSERT_EQ("[Adblock Plus ", jsEngine.Evaluate("foo.responseText.substr(0, 14)" ));
90 //TODO: Shall we be strict with letter casing here? Content-Type vs content-ty pe
91 ASSERT_EQ("text/plain", jsEngine.Evaluate("foo.responseHeaders['Content-Type'] .substr(0, 10)"));
92 ASSERT_EQ("undefined", jsEngine.Evaluate("typeof foo.responseHeaders['location ']"));
93 }
94 72
73 #if !FILTER_ENGINE_STUBS
95 TEST(WebRequestTest, XMLHttpRequest) 74 TEST(WebRequestTest, XMLHttpRequest)
96 { 75 {
97 AdblockPlus::DefaultWebRequest webRequest; 76 AdblockPlus::JsEngine jsEngine;
98 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, &webRequest, 0);
99 AdblockPlus::FilterEngine filterEngine(jsEngine); 77 AdblockPlus::FilterEngine filterEngine(jsEngine);
100 78
101 jsEngine.Evaluate("\ 79 jsEngine.Evaluate("\
102 var result;\ 80 var result;\
103 var request = new XMLHttpRequest();\ 81 var request = new XMLHttpRequest();\
104 request.open('GET', 'https://easylist.adblockplus.org/easylist.txt');\ 82 request.open('GET', 'https://easylist.adblockplus.org/easylist.txt');\
105 request.setRequestHeader('X', 'Y');\ 83 request.setRequestHeader('X', 'Y');\
106 request.overrideMimeType('text/plain');\ 84 request.overrideMimeType('text/plain');\
107 request.addEventListener('load', function() {result = request.responseText;} , false);\ 85 request.addEventListener('load', function() {result = request.responseText;} , false);\
108 request.addEventListener('error', function() {result = 'error';}, false);\ 86 request.addEventListener('error', function() {result = 'error';}, false);\
109 request.send(null);"); 87 request.send(null);");
110 do 88 do
111 { 89 {
112 AdblockPlus::Sleep(200); 90 AdblockPlus::Sleep(200);
113 } while (jsEngine.Evaluate("result")->IsUndefined()); 91 } while (jsEngine.Evaluate("result")->IsUndefined());
114 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine.Evaluate("request.channel.s tatus")->AsInt()); 92 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine.Evaluate("request.channel.s tatus")->AsInt());
115 ASSERT_EQ(200, jsEngine.Evaluate("request.status")->AsInt()); 93 ASSERT_EQ(200, jsEngine.Evaluate("request.status")->AsInt());
116 ASSERT_EQ("[Adblock Plus ", jsEngine.Evaluate("result.substr(0, 14)")->AsStrin g()); 94 ASSERT_EQ("[Adblock Plus ", jsEngine.Evaluate("result.substr(0, 14)")->AsStrin g());
117 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());
118 ASSERT_TRUE(jsEngine.Evaluate("request.getResponseHeader('Location')")->IsNull ()); 96 ASSERT_TRUE(jsEngine.Evaluate("request.getResponseHeader('Location')")->IsNull ());
119 } 97 }
98 #endif
99
120 #else 100 #else
121 TEST(WebRequestTest, DummyWebRequest) 101 TEST(WebRequestTest, DummyWebRequest)
122 { 102 {
123 AdblockPlus::DefaultWebRequest webRequest; 103 AdblockPlus::JsEngine jsEngine;
124 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, &webRequest, 0);
125 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;} )");
126 do 105 do
127 { 106 {
128 AdblockPlus::Sleep(200); 107 AdblockPlus::Sleep(200);
129 } while (jsEngine.Evaluate("this.foo")->IsUndefined()); 108 } while (jsEngine.Evaluate("this.foo")->IsUndefined());
130 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine.Evaluate("foo.st atus")->AsInt()); 109 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine.Evaluate("foo.st atus")->AsInt());
131 ASSERT_EQ(0, jsEngine.Evaluate("foo.responseStatus")->AsInt()); 110 ASSERT_EQ(0, jsEngine.Evaluate("foo.responseStatus")->AsInt());
132 ASSERT_EQ("", jsEngine.Evaluate("foo.responseText")->AsString()); 111 ASSERT_EQ("", jsEngine.Evaluate("foo.responseText")->AsString());
133 ASSERT_EQ("{}", jsEngine.Evaluate("JSON.stringify(foo.responseHeaders)")->AsSt ring()); 112 ASSERT_EQ("{}", jsEngine.Evaluate("JSON.stringify(foo.responseHeaders)")->AsSt ring());
134 } 113 }
135 114
115 #if !FILTER_ENGINE_STUBS
136 TEST(WebRequestTest, XMLHttpRequest) 116 TEST(WebRequestTest, XMLHttpRequest)
137 { 117 {
138 AdblockPlus::DefaultWebRequest webRequest; 118 AdblockPlus::JsEngine jsEngine;
139 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, &webRequest, 0);
140 AdblockPlus::FilterEngine filterEngine(jsEngine); 119 AdblockPlus::FilterEngine filterEngine(jsEngine);
141 120
142 jsEngine.Evaluate("\ 121 jsEngine.Evaluate("\
143 var result;\ 122 var result;\
144 var request = new XMLHttpRequest();\ 123 var request = new XMLHttpRequest();\
145 request.open('GET', 'https://easylist.adblockplus.org/easylist.txt');\ 124 request.open('GET', 'https://easylist.adblockplus.org/easylist.txt');\
146 request.setRequestHeader('X', 'Y');\ 125 request.setRequestHeader('X', 'Y');\
147 request.overrideMimeType('text/plain');\ 126 request.overrideMimeType('text/plain');\
148 request.addEventListener('load', function() {result = request.responseText;} , false);\ 127 request.addEventListener('load', function() {result = request.responseText;} , false);\
149 request.addEventListener('error', function() {result = 'error';}, false);\ 128 request.addEventListener('error', function() {result = 'error';}, false);\
150 request.send(null);"); 129 request.send(null);");
151 do 130 do
152 { 131 {
153 AdblockPlus::Sleep(200); 132 AdblockPlus::Sleep(200);
154 } while (jsEngine.Evaluate("result")->IsUndefined()); 133 } while (jsEngine.Evaluate("result")->IsUndefined());
155 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine.Evaluate("reques t.channel.status")->AsInt()); 134 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine.Evaluate("reques t.channel.status")->AsInt());
156 ASSERT_EQ(0, jsEngine.Evaluate("request.status")->AsInt()); 135 ASSERT_EQ(0, jsEngine.Evaluate("request.status")->AsInt());
157 ASSERT_EQ("error", jsEngine.Evaluate("result")->AsString()); 136 ASSERT_EQ("error", jsEngine.Evaluate("result")->AsString());
158 ASSERT_TRUE(jsEngine.Evaluate("request.getResponseHeader('Content-Type')")->Is Null()); 137 ASSERT_TRUE(jsEngine.Evaluate("request.getResponseHeader('Content-Type')")->Is Null());
159 } 138 }
160 #endif 139 #endif
140
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