• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that
3 // can be found in the LICENSE file.
4 
5 #include "tests/ceftests/test_util.h"
6 
7 #include <algorithm>
8 
9 #include "include/cef_command_line.h"
10 #include "include/cef_request_context_handler.h"
11 #include "tests/gtest/include/gtest/gtest.h"
12 
TestMapEqual(const CefRequest::HeaderMap & map1,const CefRequest::HeaderMap & map2,bool allowExtras)13 void TestMapEqual(const CefRequest::HeaderMap& map1,
14                   const CefRequest::HeaderMap& map2,
15                   bool allowExtras) {
16   if (!allowExtras) {
17     EXPECT_EQ(map1.size(), map2.size());
18   }
19 
20   TestMapNoDuplicates(map1);
21   TestMapNoDuplicates(map2);
22 
23   CefRequest::HeaderMap::const_iterator it1, it2;
24 
25   for (it1 = map1.begin(); it1 != map1.end(); ++it1) {
26     bool found = false;
27     std::string name1 = it1->first;
28     std::transform(name1.begin(), name1.end(), name1.begin(), ::tolower);
29     for (it2 = map2.begin(); it2 != map2.end(); ++it2) {
30       std::string name2 = it2->first;
31       std::transform(name2.begin(), name2.end(), name2.begin(), ::tolower);
32       if (name1 == name2 && it1->second == it2->second) {
33         found = true;
34         break;
35       }
36     }
37     EXPECT_TRUE(found) << "No entry for " << it1->first.ToString() << ": "
38                        << it1->second.ToString();
39   }
40 }
41 
TestMapNoDuplicates(const CefRequest::HeaderMap & map)42 void TestMapNoDuplicates(const CefRequest::HeaderMap& map) {
43   CefRequest::HeaderMap::const_iterator it1 = map.begin();
44   for (; it1 != map.end(); ++it1) {
45     CefRequest::HeaderMap::const_iterator it2 = it1;
46     for (++it2; it2 != map.end(); ++it2) {
47       EXPECT_FALSE(it1->first == it2->first && it1->second == it2->second)
48           << "Duplicate entry for " << it1->first.ToString() << ": "
49           << it1->second.ToString();
50     }
51   }
52 }
53 
TestPostDataElementEqual(CefRefPtr<CefPostDataElement> elem1,CefRefPtr<CefPostDataElement> elem2)54 void TestPostDataElementEqual(CefRefPtr<CefPostDataElement> elem1,
55                               CefRefPtr<CefPostDataElement> elem2) {
56   EXPECT_TRUE(elem1.get());
57   EXPECT_TRUE(elem2.get());
58 
59   EXPECT_EQ(elem1->GetType(), elem2->GetType());
60   switch (elem1->GetType()) {
61     case PDE_TYPE_BYTES: {
62       EXPECT_EQ(elem1->GetBytesCount(), elem2->GetBytesCount());
63       size_t bytesCt = elem1->GetBytesCount();
64       char* buff1 = new char[bytesCt];
65       char* buff2 = new char[bytesCt];
66       elem1->GetBytes(bytesCt, buff1);
67       elem2->GetBytes(bytesCt, buff2);
68       EXPECT_TRUE(!memcmp(buff1, buff2, bytesCt));
69       delete[] buff1;
70       delete[] buff2;
71     } break;
72     case PDE_TYPE_FILE:
73       EXPECT_EQ(elem1->GetFile(), elem2->GetFile());
74       break;
75     default:
76       break;
77   }
78 }
79 
TestPostDataEqual(CefRefPtr<CefPostData> postData1,CefRefPtr<CefPostData> postData2)80 void TestPostDataEqual(CefRefPtr<CefPostData> postData1,
81                        CefRefPtr<CefPostData> postData2) {
82   EXPECT_TRUE(postData1.get());
83   EXPECT_TRUE(postData2.get());
84 
85   EXPECT_EQ(postData1->GetElementCount(), postData2->GetElementCount());
86 
87   CefPostData::ElementVector ev1, ev2;
88   postData1->GetElements(ev1);
89   postData1->GetElements(ev2);
90   ASSERT_EQ(ev1.size(), ev2.size());
91 
92   CefPostData::ElementVector::const_iterator it1 = ev1.begin();
93   CefPostData::ElementVector::const_iterator it2 = ev2.begin();
94   for (; it1 != ev1.end() && it2 != ev2.end(); ++it1, ++it2)
95     TestPostDataElementEqual((*it1), (*it2));
96 }
97 
TestRequestEqual(CefRefPtr<CefRequest> request1,CefRefPtr<CefRequest> request2,bool allowExtras)98 void TestRequestEqual(CefRefPtr<CefRequest> request1,
99                       CefRefPtr<CefRequest> request2,
100                       bool allowExtras) {
101   EXPECT_TRUE(request1.get());
102   EXPECT_TRUE(request2.get());
103 
104   EXPECT_STREQ(request1->GetURL().ToString().c_str(),
105                request2->GetURL().ToString().c_str());
106   EXPECT_STREQ(request1->GetMethod().ToString().c_str(),
107                request2->GetMethod().ToString().c_str());
108 
109   EXPECT_STREQ(request1->GetReferrerURL().ToString().c_str(),
110                request2->GetReferrerURL().ToString().c_str());
111   EXPECT_EQ(request1->GetReferrerPolicy(), request2->GetReferrerPolicy());
112 
113   CefRequest::HeaderMap headers1, headers2;
114   request1->GetHeaderMap(headers1);
115   request2->GetHeaderMap(headers2);
116   TestMapEqual(headers1, headers2, allowExtras);
117 
118   CefRefPtr<CefPostData> postData1 = request1->GetPostData();
119   CefRefPtr<CefPostData> postData2 = request2->GetPostData();
120   EXPECT_EQ(!!(postData1.get()), !!(postData2.get()));
121   if (postData1.get() && postData2.get())
122     TestPostDataEqual(postData1, postData2);
123 }
124 
TestResponseEqual(CefRefPtr<CefResponse> response1,CefRefPtr<CefResponse> response2,bool allowExtras)125 void TestResponseEqual(CefRefPtr<CefResponse> response1,
126                        CefRefPtr<CefResponse> response2,
127                        bool allowExtras) {
128   EXPECT_TRUE(response1.get());
129   EXPECT_TRUE(response2.get());
130 
131   EXPECT_EQ(response1->GetStatus(), response2->GetStatus());
132   EXPECT_STREQ(response1->GetStatusText().ToString().c_str(),
133                response2->GetStatusText().ToString().c_str());
134   EXPECT_STREQ(response1->GetMimeType().ToString().c_str(),
135                response2->GetMimeType().ToString().c_str());
136 
137   CefRequest::HeaderMap headers1, headers2;
138   response1->GetHeaderMap(headers1);
139   response2->GetHeaderMap(headers2);
140   TestMapEqual(headers1, headers2, allowExtras);
141 }
142 
TestBinaryEqual(CefRefPtr<CefBinaryValue> val1,CefRefPtr<CefBinaryValue> val2)143 void TestBinaryEqual(CefRefPtr<CefBinaryValue> val1,
144                      CefRefPtr<CefBinaryValue> val2) {
145   EXPECT_TRUE(val1.get());
146   EXPECT_TRUE(val2.get());
147 
148   EXPECT_TRUE(val1->IsEqual(val2));
149   EXPECT_TRUE(val2->IsEqual(val1));
150 
151   size_t data_size = val1->GetSize();
152   EXPECT_EQ(data_size, val2->GetSize());
153 
154   EXPECT_GT(data_size, (size_t)0);
155 
156   char* data1 = new char[data_size + 1];
157   char* data2 = new char[data_size + 1];
158 
159   EXPECT_EQ(data_size, val1->GetData(data1, data_size, 0));
160   data1[data_size] = 0;
161   EXPECT_EQ(data_size, val2->GetData(data2, data_size, 0));
162   data2[data_size] = 0;
163 
164   EXPECT_STREQ(data1, data2);
165 
166   delete[] data1;
167   delete[] data2;
168 }
169 
TestDictionaryEqual(CefRefPtr<CefDictionaryValue> val1,CefRefPtr<CefDictionaryValue> val2)170 void TestDictionaryEqual(CefRefPtr<CefDictionaryValue> val1,
171                          CefRefPtr<CefDictionaryValue> val2) {
172   EXPECT_TRUE(val1.get());
173   EXPECT_TRUE(val2.get());
174 
175   EXPECT_TRUE(val1->IsEqual(val2));
176   EXPECT_TRUE(val2->IsEqual(val1));
177 
178   EXPECT_EQ(val1->GetSize(), val2->GetSize());
179 
180   CefDictionaryValue::KeyList keys;
181   EXPECT_TRUE(val1->GetKeys(keys));
182 
183   CefDictionaryValue::KeyList::const_iterator it = keys.begin();
184   for (; it != keys.end(); ++it) {
185     CefString key = *it;
186     EXPECT_TRUE(val2->HasKey(key));
187     CefValueType type = val1->GetType(key);
188     EXPECT_EQ(type, val2->GetType(key));
189     switch (type) {
190       case VTYPE_INVALID:
191       case VTYPE_NULL:
192         break;
193       case VTYPE_BOOL:
194         EXPECT_EQ(val1->GetBool(key), val2->GetBool(key));
195         break;
196       case VTYPE_INT:
197         EXPECT_EQ(val1->GetInt(key), val2->GetInt(key));
198         break;
199       case VTYPE_DOUBLE:
200         EXPECT_EQ(val1->GetDouble(key), val2->GetDouble(key));
201         break;
202       case VTYPE_STRING:
203         EXPECT_EQ(val1->GetString(key), val2->GetString(key));
204         break;
205       case VTYPE_BINARY:
206         TestBinaryEqual(val1->GetBinary(key), val2->GetBinary(key));
207         break;
208       case VTYPE_DICTIONARY:
209         TestDictionaryEqual(val1->GetDictionary(key), val2->GetDictionary(key));
210         break;
211       case VTYPE_LIST:
212         TestListEqual(val1->GetList(key), val2->GetList(key));
213         break;
214     }
215   }
216 }
217 
TestListEqual(CefRefPtr<CefListValue> val1,CefRefPtr<CefListValue> val2)218 void TestListEqual(CefRefPtr<CefListValue> val1, CefRefPtr<CefListValue> val2) {
219   EXPECT_TRUE(val1.get());
220   EXPECT_TRUE(val2.get());
221 
222   EXPECT_TRUE(val1->IsEqual(val2));
223   EXPECT_TRUE(val2->IsEqual(val1));
224 
225   size_t size = val1->GetSize();
226   EXPECT_EQ(size, val2->GetSize());
227 
228   for (size_t i = 0; i < size; ++i) {
229     CefValueType type = val1->GetType(i);
230     EXPECT_EQ(type, val2->GetType(i));
231     switch (type) {
232       case VTYPE_INVALID:
233       case VTYPE_NULL:
234         break;
235       case VTYPE_BOOL:
236         EXPECT_EQ(val1->GetBool(i), val2->GetBool(i));
237         break;
238       case VTYPE_INT:
239         EXPECT_EQ(val1->GetInt(i), val2->GetInt(i));
240         break;
241       case VTYPE_DOUBLE:
242         EXPECT_EQ(val1->GetDouble(i), val2->GetDouble(i));
243         break;
244       case VTYPE_STRING:
245         EXPECT_EQ(val1->GetString(i), val2->GetString(i));
246         break;
247       case VTYPE_BINARY:
248         TestBinaryEqual(val1->GetBinary(i), val2->GetBinary(i));
249         break;
250       case VTYPE_DICTIONARY:
251         TestDictionaryEqual(val1->GetDictionary(i), val2->GetDictionary(i));
252         break;
253       case VTYPE_LIST:
254         TestListEqual(val1->GetList(i), val2->GetList(i));
255         break;
256     }
257   }
258 }
259 
TestProcessMessageEqual(CefRefPtr<CefProcessMessage> val1,CefRefPtr<CefProcessMessage> val2)260 void TestProcessMessageEqual(CefRefPtr<CefProcessMessage> val1,
261                              CefRefPtr<CefProcessMessage> val2) {
262   EXPECT_TRUE(val1.get());
263   EXPECT_TRUE(val2.get());
264   EXPECT_EQ(val1->GetName(), val2->GetName());
265 
266   TestListEqual(val1->GetArgumentList(), val2->GetArgumentList());
267 }
268 
TestStringVectorEqual(const std::vector<CefString> & val1,const std::vector<CefString> & val2)269 void TestStringVectorEqual(const std::vector<CefString>& val1,
270                            const std::vector<CefString>& val2) {
271   EXPECT_EQ(val1.size(), val2.size());
272 
273   for (size_t i = 0; i < val1.size(); ++i)
274     EXPECT_STREQ(val1[i].ToString().c_str(), val2[i].ToString().c_str());
275 }
276 
TestOldResourceAPI()277 bool TestOldResourceAPI() {
278   static int state = -1;
279   if (state == -1) {
280     CefRefPtr<CefCommandLine> command_line =
281         CefCommandLine::GetGlobalCommandLine();
282     state = command_line->HasSwitch("test-old-resource-api") ? 1 : 0;
283   }
284   return state ? true : false;
285 }
286 
IsChromeRuntimeEnabled()287 bool IsChromeRuntimeEnabled() {
288   static int state = -1;
289   if (state == -1) {
290     CefRefPtr<CefCommandLine> command_line =
291         CefCommandLine::GetGlobalCommandLine();
292     state = command_line->HasSwitch("enable-chrome-runtime") ? 1 : 0;
293   }
294   return state ? true : false;
295 }
296 
IgnoreURL(const std::string & url)297 bool IgnoreURL(const std::string& url) {
298   return IsChromeRuntimeEnabled() &&
299          url.find("/favicon.ico") != std::string::npos;
300 }
301 
CreateTestRequestContext(TestRequestContextMode mode,const std::string & cache_path)302 CefRefPtr<CefRequestContext> CreateTestRequestContext(
303     TestRequestContextMode mode,
304     const std::string& cache_path) {
305   EXPECT_TRUE(cache_path.empty() || mode == TEST_RC_MODE_CUSTOM ||
306               mode == TEST_RC_MODE_CUSTOM_WITH_HANDLER);
307 
308   if (mode == TEST_RC_MODE_NONE)
309     return nullptr;
310   if (mode == TEST_RC_MODE_GLOBAL)
311     return CefRequestContext::GetGlobalContext();
312 
313   CefRefPtr<CefRequestContextHandler> rc_handler;
314   if (mode == TEST_RC_MODE_GLOBAL_WITH_HANDLER ||
315       mode == TEST_RC_MODE_CUSTOM_WITH_HANDLER) {
316     class Handler : public CefRequestContextHandler {
317      public:
318       Handler() {}
319 
320      private:
321       IMPLEMENT_REFCOUNTING(Handler);
322     };
323     rc_handler = new Handler();
324   }
325 
326   if (mode == TEST_RC_MODE_CUSTOM || mode == TEST_RC_MODE_CUSTOM_WITH_HANDLER) {
327     CefRequestContextSettings settings;
328     if (!cache_path.empty())
329       CefString(&settings.cache_path) = cache_path;
330     return CefRequestContext::CreateContext(settings, rc_handler);
331   }
332 
333   EXPECT_EQ(mode, TEST_RC_MODE_GLOBAL_WITH_HANDLER);
334   return CefRequestContext::CreateContext(CefRequestContext::GetGlobalContext(),
335                                           rc_handler);
336 }
337