Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / net / filter / sdch_filter_unittest.cc
index 28da615..2263001 100644 (file)
@@ -63,9 +63,33 @@ class SdchFilterTest : public testing::Test {
     url_request_context->set_sdch_manager(sdch_manager_.get());
   }
 
+  // Attempt to add a dictionary to the manager; returns whether or not
+  // the attempt succeeded.
+  bool AddSdchDictionary(const std::string& dictionary_text,
+                         const GURL& gurl) {
+    std::string list;
+    sdch_manager_->GetAvailDictionaryList(gurl, &list);
+    sdch_manager_->AddSdchDictionary(dictionary_text, gurl);
+    std::string list2;
+    sdch_manager_->GetAvailDictionaryList(gurl, &list2);
+
+    // The list of hashes should change iff the addition succeeds.
+    return (list != list2);
+  }
+
   MockFilterContext* filter_context() { return filter_context_.get(); }
 
-  std::string NewSdchCompressedData(const std::string dictionary);
+  std::string NewSdchCompressedData(const std::string dictionary) {
+    std::string client_hash;
+    std::string server_hash;
+    SdchManager::GenerateHash(dictionary, &client_hash, &server_hash);
+
+    // Build compressed data that refers to our dictionary.
+    std::string compressed(server_hash);
+    compressed.append("\0", 1);
+    compressed.append(vcdiff_compressed_data_);
+    return compressed;
+  }
 
   const std::string test_vcdiff_dictionary_;
   const std::string vcdiff_compressed_data_;
@@ -75,19 +99,6 @@ class SdchFilterTest : public testing::Test {
   scoped_ptr<MockFilterContext> filter_context_;
 };
 
-std::string SdchFilterTest::NewSdchCompressedData(
-    const std::string dictionary) {
-  std::string client_hash;
-  std::string server_hash;
-  SdchManager::GenerateHash(dictionary, &client_hash, &server_hash);
-
-  // Build compressed data that refers to our dictionary.
-  std::string compressed(server_hash);
-  compressed.append("\0", 1);
-  compressed.append(vcdiff_compressed_data_);
-  return compressed;
-}
-
 //------------------------------------------------------------------------------
 
 
@@ -165,6 +176,25 @@ TEST_F(SdchFilterTest, EmptyInputOk) {
   filter_context()->SetURL(GURL(url_string));
   scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context()));
 
+  // With no input data, try to read output.
+  int output_bytes_or_buffer_size = sizeof(output_buffer);
+  Filter::FilterStatus status = filter->ReadData(output_buffer,
+                                                 &output_bytes_or_buffer_size);
+
+  EXPECT_EQ(0, output_bytes_or_buffer_size);
+  EXPECT_EQ(Filter::FILTER_NEED_MORE_DATA, status);
+}
+
+// Make sure that the filter context has everything that might be
+// nuked from it during URLRequest teardown before the SdchFilter
+// destructor.
+TEST_F(SdchFilterTest, SparseContextOk) {
+  std::vector<Filter::FilterType> filter_types;
+  filter_types.push_back(Filter::FILTER_TYPE_SDCH);
+  char output_buffer[20];
+  std::string url_string("http://ignore.com");
+  filter_context()->SetURL(GURL(url_string));
+  scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context()));
 
   // With no input data, try to read output.
   int output_bytes_or_buffer_size = sizeof(output_buffer);
@@ -173,6 +203,13 @@ TEST_F(SdchFilterTest, EmptyInputOk) {
 
   EXPECT_EQ(0, output_bytes_or_buffer_size);
   EXPECT_EQ(Filter::FILTER_NEED_MORE_DATA, status);
+
+  // Partially tear down context.  Anything that goes through request()
+  // without checking it for null in the URLRequestJob::HttpFilterContext
+  // implementation is suspect.  Everything that does check it for null should
+  // return null.  This is to test for incorrectly relying on filter_context()
+  // from the SdchFilter destructor.
+  filter_context()->NukeUnstableInterfaces();
 }
 
 TEST_F(SdchFilterTest, PassThroughWhenTentative) {
@@ -387,10 +424,10 @@ TEST_F(SdchFilterTest, DictionaryAddOnce) {
 
   std::string url_string = "http://" + kSampleDomain;
   GURL url(url_string);
-  EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary, url));
+  EXPECT_TRUE(AddSdchDictionary(dictionary, url));
 
   // Check we can't add it twice.
-  EXPECT_FALSE(sdch_manager_->AddSdchDictionary(dictionary, url));
+  EXPECT_FALSE(AddSdchDictionary(dictionary, url));
 
   const std::string kSampleDomain2 = "sdchtest2.com";
 
@@ -401,7 +438,7 @@ TEST_F(SdchFilterTest, DictionaryAddOnce) {
 
     std::string url_string2 = "http://" + kSampleDomain2;
     GURL url2(url_string2);
-    EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary2, url2));
+    EXPECT_TRUE(AddSdchDictionary(dictionary2, url2));
   }
 }
 
@@ -413,7 +450,7 @@ TEST_F(SdchFilterTest, BasicDictionary) {
   std::string url_string = "http://" + kSampleDomain;
 
   GURL url(url_string);
-  EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary, url));
+  EXPECT_TRUE(AddSdchDictionary(dictionary, url));
 
   std::string compressed(NewSdchCompressedData(dictionary));
 
@@ -450,7 +487,7 @@ TEST_F(SdchFilterTest, NoDecodeHttps) {
   std::string url_string = "http://" + kSampleDomain;
 
   GURL url(url_string);
-  EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary, url));
+  EXPECT_TRUE(AddSdchDictionary(dictionary, url));
 
   std::string compressed(NewSdchCompressedData(dictionary));
 
@@ -480,7 +517,7 @@ TEST_F(SdchFilterTest, NoDecodeFtp) {
   std::string url_string = "http://" + kSampleDomain;
 
   GURL url(url_string);
-  EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary, url));
+  EXPECT_TRUE(AddSdchDictionary(dictionary, url));
 
   std::string compressed(NewSdchCompressedData(dictionary));
 
@@ -506,7 +543,7 @@ TEST_F(SdchFilterTest, NoDecodeFileColon) {
   std::string url_string = "http://" + kSampleDomain;
 
   GURL url(url_string);
-  EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary, url));
+  EXPECT_TRUE(AddSdchDictionary(dictionary, url));
 
   std::string compressed(NewSdchCompressedData(dictionary));
 
@@ -532,7 +569,7 @@ TEST_F(SdchFilterTest, NoDecodeAboutColon) {
   std::string url_string = "http://" + kSampleDomain;
 
   GURL url(url_string);
-  EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary, url));
+  EXPECT_TRUE(AddSdchDictionary(dictionary, url));
 
   std::string compressed(NewSdchCompressedData(dictionary));
 
@@ -558,7 +595,7 @@ TEST_F(SdchFilterTest, NoDecodeJavaScript) {
   std::string url_string = "http://" + kSampleDomain;
 
   GURL url(url_string);
-  EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary, url));
+  EXPECT_TRUE(AddSdchDictionary(dictionary, url));
 
   std::string compressed(NewSdchCompressedData(dictionary));
 
@@ -584,7 +621,7 @@ TEST_F(SdchFilterTest, CanStillDecodeHttp) {
   std::string url_string = "http://" + kSampleDomain;
 
   GURL url(url_string);
-  EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary, url));
+  EXPECT_TRUE(AddSdchDictionary(dictionary, url));
 
   std::string compressed(NewSdchCompressedData(dictionary));
 
@@ -610,7 +647,7 @@ TEST_F(SdchFilterTest, CrossDomainDictionaryUse) {
   std::string url_string = "http://" + kSampleDomain;
 
   GURL url(url_string);
-  EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary, url));
+  EXPECT_TRUE(AddSdchDictionary(dictionary, url));
 
   std::string compressed(NewSdchCompressedData(dictionary));
 
@@ -649,13 +686,14 @@ TEST_F(SdchFilterTest, DictionaryPathValidation) {
   std::string url_string = "http://" + kSampleDomain;
 
   GURL url(url_string);
-  EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary, url));
+  EXPECT_TRUE(AddSdchDictionary(dictionary, url));
 
   // Create a dictionary with a path restriction, by prefixing dictionary.
   const std::string path("/special_path/bin");
   std::string dictionary_with_path("Path: " + path + "\n");
   dictionary_with_path.append(dictionary);
-  EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_with_path, url));
+  GURL url2(url_string + path);
+  EXPECT_TRUE(AddSdchDictionary(dictionary_with_path, url2));
 
   std::string compressed_for_path(NewSdchCompressedData(dictionary_with_path));
 
@@ -703,16 +741,15 @@ TEST_F(SdchFilterTest, DictionaryPortValidation) {
   std::string url_string = "http://" + kSampleDomain;
 
   GURL url(url_string);
-  EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary, url));
-
+  EXPECT_TRUE(AddSdchDictionary(dictionary, url));
 
   // Create a dictionary with a port restriction, by prefixing old dictionary.
   const std::string port("502");
   std::string dictionary_with_port("Port: " + port + "\n");
   dictionary_with_port.append("Port: 80\n");  // Add default port.
   dictionary_with_port.append(dictionary);
-  EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_with_port,
-                                            GURL(url_string + ":" + port)));
+  EXPECT_TRUE(AddSdchDictionary(dictionary_with_port,
+                                GURL(url_string + ":" + port)));
 
   std::string compressed_for_port(NewSdchCompressedData(dictionary_with_port));
 
@@ -833,7 +870,7 @@ TEST_F(SdchFilterTest, FilterChaining) {
   std::string url_string = "http://" + kSampleDomain;
 
   GURL url(url_string);
-  EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary, url));
+  EXPECT_TRUE(AddSdchDictionary(dictionary, url));
 
   std::string sdch_compressed(NewSdchCompressedData(dictionary));
 
@@ -915,7 +952,7 @@ TEST_F(SdchFilterTest, DefaultGzipIfSdch) {
   std::string url_string = "http://" + kSampleDomain;
 
   GURL url(url_string);
-  EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary, url));
+  EXPECT_TRUE(AddSdchDictionary(dictionary, url));
 
   std::string sdch_compressed(NewSdchCompressedData(dictionary));
 
@@ -971,7 +1008,7 @@ TEST_F(SdchFilterTest, AcceptGzipSdchIfGzip) {
   std::string url_string = "http://" + kSampleDomain;
 
   GURL url(url_string);
-  EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary, url));
+  EXPECT_TRUE(AddSdchDictionary(dictionary, url));
 
   std::string sdch_compressed(NewSdchCompressedData(dictionary));
 
@@ -1030,7 +1067,7 @@ TEST_F(SdchFilterTest, DefaultSdchGzipIfEmpty) {
   std::string url_string = "http://" + kSampleDomain;
 
   GURL url(url_string);
-  EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary, url));
+  EXPECT_TRUE(AddSdchDictionary(dictionary, url));
 
   std::string sdch_compressed(NewSdchCompressedData(dictionary));
 
@@ -1086,7 +1123,7 @@ TEST_F(SdchFilterTest, AcceptGzipGzipSdchIfGzip) {
   std::string url_string = "http://" + kSampleDomain;
 
   GURL url(url_string);
-  EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary, url));
+  EXPECT_TRUE(AddSdchDictionary(dictionary, url));
 
   std::string sdch_compressed(NewSdchCompressedData(dictionary));