Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: net/filter/filter_unittest.cc

Issue 754433003: Update from https://crrev.com/305340 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/filter/filter.cc ('k') | net/filter/mock_filter_context.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/base/io_buffer.h" 5 #include "net/base/io_buffer.h"
6 #include "net/filter/filter.h" 6 #include "net/filter/filter.h"
7 #include "net/filter/mock_filter_context.h" 7 #include "net/filter/mock_filter_context.h"
8 #include "testing/gtest/include/gtest/gtest.h" 8 #include "testing/gtest/include/gtest/gtest.h"
9 9
10 namespace net { 10 namespace net {
11 11
12 namespace { 12 namespace {
13 13
14 class PassThroughFilter : public Filter { 14 class PassThroughFilter : public Filter {
15 public: 15 public:
16 PassThroughFilter() : Filter(FILTER_TYPE_UNSUPPORTED) {} 16 PassThroughFilter() : Filter(FILTER_TYPE_UNSUPPORTED) {}
17 17
18 FilterStatus ReadFilteredData(char* dest_buffer, int* dest_len) override { 18 FilterStatus ReadFilteredData(char* dest_buffer, int* dest_len) override {
19 return CopyOut(dest_buffer, dest_len); 19 return CopyOut(dest_buffer, dest_len);
20 } 20 }
21 21
22 DISALLOW_COPY_AND_ASSIGN(PassThroughFilter); 22 DISALLOW_COPY_AND_ASSIGN(PassThroughFilter);
23 }; 23 };
24 24
25 } // namespace 25 } // namespace
26 26
27 class FilterTest : public testing::Test {
28 };
29
30 TEST(FilterTest, ContentTypeId) { 27 TEST(FilterTest, ContentTypeId) {
31 // Check for basic translation of Content-Encoding, including case variations. 28 // Check for basic translation of Content-Encoding, including case variations.
32 EXPECT_EQ(Filter::FILTER_TYPE_DEFLATE, 29 EXPECT_EQ(Filter::FILTER_TYPE_DEFLATE,
33 Filter::ConvertEncodingToType("deflate")); 30 Filter::ConvertEncodingToType("deflate"));
34 EXPECT_EQ(Filter::FILTER_TYPE_DEFLATE, 31 EXPECT_EQ(Filter::FILTER_TYPE_DEFLATE,
35 Filter::ConvertEncodingToType("deflAte")); 32 Filter::ConvertEncodingToType("deflAte"));
36 EXPECT_EQ(Filter::FILTER_TYPE_GZIP, 33 EXPECT_EQ(Filter::FILTER_TYPE_GZIP,
37 Filter::ConvertEncodingToType("gzip")); 34 Filter::ConvertEncodingToType("gzip"));
38 EXPECT_EQ(Filter::FILTER_TYPE_GZIP, 35 EXPECT_EQ(Filter::FILTER_TYPE_GZIP,
39 Filter::ConvertEncodingToType("GzIp")); 36 Filter::ConvertEncodingToType("GzIp"));
40 EXPECT_EQ(Filter::FILTER_TYPE_GZIP, 37 EXPECT_EQ(Filter::FILTER_TYPE_GZIP,
41 Filter::ConvertEncodingToType("x-gzip")); 38 Filter::ConvertEncodingToType("x-gzip"));
42 EXPECT_EQ(Filter::FILTER_TYPE_GZIP, 39 EXPECT_EQ(Filter::FILTER_TYPE_GZIP,
43 Filter::ConvertEncodingToType("X-GzIp")); 40 Filter::ConvertEncodingToType("X-GzIp"));
44 EXPECT_EQ(Filter::FILTER_TYPE_SDCH, 41 EXPECT_EQ(Filter::FILTER_TYPE_SDCH,
45 Filter::ConvertEncodingToType("sdch")); 42 Filter::ConvertEncodingToType("sdch"));
46 EXPECT_EQ(Filter::FILTER_TYPE_SDCH, 43 EXPECT_EQ(Filter::FILTER_TYPE_SDCH,
47 Filter::ConvertEncodingToType("sDcH")); 44 Filter::ConvertEncodingToType("sDcH"));
48 EXPECT_EQ(Filter::FILTER_TYPE_UNSUPPORTED, 45 EXPECT_EQ(Filter::FILTER_TYPE_UNSUPPORTED,
49 Filter::ConvertEncodingToType("weird")); 46 Filter::ConvertEncodingToType("weird"));
50 EXPECT_EQ(Filter::FILTER_TYPE_UNSUPPORTED, 47 EXPECT_EQ(Filter::FILTER_TYPE_UNSUPPORTED,
51 Filter::ConvertEncodingToType("strange")); 48 Filter::ConvertEncodingToType("strange"));
52 } 49 }
53 50
54 // Check various fixups that modify content encoding lists. 51 // Check various fixups that modify content encoding lists.
55 TEST(FilterTest, ApacheGzip) { 52 TEST(FilterTest, ApacheGzip) {
56 MockFilterContext filter_context; 53 MockFilterContext filter_context;
57 filter_context.SetSdchResponse(false); 54 filter_context.SetSdchResponse(NULL);
58 55
59 // Check that redundant gzip mime type removes only solo gzip encoding. 56 // Check that redundant gzip mime type removes only solo gzip encoding.
60 const std::string kGzipMime1("application/x-gzip"); 57 const std::string kGzipMime1("application/x-gzip");
61 const std::string kGzipMime2("application/gzip"); 58 const std::string kGzipMime2("application/gzip");
62 const std::string kGzipMime3("application/x-gunzip"); 59 const std::string kGzipMime3("application/x-gunzip");
63 std::vector<Filter::FilterType> encoding_types; 60 std::vector<Filter::FilterType> encoding_types;
64 61
65 // First show it removes the gzip, given any gzip style mime type. 62 // First show it removes the gzip, given any gzip style mime type.
66 encoding_types.clear(); 63 encoding_types.clear();
67 encoding_types.push_back(Filter::FILTER_TYPE_GZIP); 64 encoding_types.push_back(Filter::FILTER_TYPE_GZIP);
(...skipping 25 matching lines...) Expand all
93 encoding_types.clear(); 90 encoding_types.clear();
94 encoding_types.push_back(Filter::FILTER_TYPE_GZIP); 91 encoding_types.push_back(Filter::FILTER_TYPE_GZIP);
95 filter_context.SetMimeType("other/mime"); 92 filter_context.SetMimeType("other/mime");
96 Filter::FixupEncodingTypes(filter_context, &encoding_types); 93 Filter::FixupEncodingTypes(filter_context, &encoding_types);
97 ASSERT_EQ(1U, encoding_types.size()); 94 ASSERT_EQ(1U, encoding_types.size());
98 EXPECT_EQ(Filter::FILTER_TYPE_GZIP, encoding_types.front()); 95 EXPECT_EQ(Filter::FILTER_TYPE_GZIP, encoding_types.front());
99 } 96 }
100 97
101 TEST(FilterTest, GzipContentDispositionFilename) { 98 TEST(FilterTest, GzipContentDispositionFilename) {
102 MockFilterContext filter_context; 99 MockFilterContext filter_context;
103 filter_context.SetSdchResponse(false); 100 filter_context.SetSdchResponse(NULL);
104 101
105 const std::string kGzipMime("application/x-tar"); 102 const std::string kGzipMime("application/x-tar");
106 const std::string kContentDisposition("attachment; filename=\"foo.tgz\""); 103 const std::string kContentDisposition("attachment; filename=\"foo.tgz\"");
107 const std::string kURL("http://foo.com/getfoo.php"); 104 const std::string kURL("http://foo.com/getfoo.php");
108 std::vector<Filter::FilterType> encoding_types; 105 std::vector<Filter::FilterType> encoding_types;
109 106
110 encoding_types.push_back(Filter::FILTER_TYPE_GZIP); 107 encoding_types.push_back(Filter::FILTER_TYPE_GZIP);
111 filter_context.SetMimeType(kGzipMime); 108 filter_context.SetMimeType(kGzipMime);
112 filter_context.SetURL(GURL(kURL)); 109 filter_context.SetURL(GURL(kURL));
113 filter_context.SetContentDisposition(kContentDisposition); 110 filter_context.SetContentDisposition(kContentDisposition);
114 Filter::FixupEncodingTypes(filter_context, &encoding_types); 111 Filter::FixupEncodingTypes(filter_context, &encoding_types);
115 ASSERT_EQ(0U, encoding_types.size()); 112 ASSERT_EQ(0U, encoding_types.size());
116 } 113 }
117 114
118 TEST(FilterTest, SdchEncoding) { 115 TEST(FilterTest, SdchEncoding) {
119 // Handle content encodings including SDCH. 116 // Handle content encodings including SDCH.
120 const std::string kTextHtmlMime("text/html"); 117 const std::string kTextHtmlMime("text/html");
121 MockFilterContext filter_context; 118 MockFilterContext filter_context;
122 filter_context.SetSdchResponse(true); 119 // Empty handle indicates to filter that SDCH is active.
120 filter_context.SetSdchResponse(
121 SdchManager::CreateEmptyDictionarySetForTesting().Pass());
123 122
124 std::vector<Filter::FilterType> encoding_types; 123 std::vector<Filter::FilterType> encoding_types;
125 124
126 // Check for most common encoding, and verify it survives unchanged. 125 // Check for most common encoding, and verify it survives unchanged.
127 encoding_types.clear(); 126 encoding_types.clear();
128 encoding_types.push_back(Filter::FILTER_TYPE_SDCH); 127 encoding_types.push_back(Filter::FILTER_TYPE_SDCH);
129 encoding_types.push_back(Filter::FILTER_TYPE_GZIP); 128 encoding_types.push_back(Filter::FILTER_TYPE_GZIP);
130 filter_context.SetMimeType(kTextHtmlMime); 129 filter_context.SetMimeType(kTextHtmlMime);
131 Filter::FixupEncodingTypes(filter_context, &encoding_types); 130 Filter::FixupEncodingTypes(filter_context, &encoding_types);
132 ASSERT_EQ(2U, encoding_types.size()); 131 ASSERT_EQ(2U, encoding_types.size());
(...skipping 16 matching lines...) Expand all
149 Filter::FixupEncodingTypes(filter_context, &encoding_types); 148 Filter::FixupEncodingTypes(filter_context, &encoding_types);
150 ASSERT_EQ(2U, encoding_types.size()); 149 ASSERT_EQ(2U, encoding_types.size());
151 EXPECT_EQ(Filter::FILTER_TYPE_SDCH, encoding_types[0]); 150 EXPECT_EQ(Filter::FILTER_TYPE_SDCH, encoding_types[0]);
152 EXPECT_EQ(Filter::FILTER_TYPE_GZIP_HELPING_SDCH, encoding_types[1]); 151 EXPECT_EQ(Filter::FILTER_TYPE_GZIP_HELPING_SDCH, encoding_types[1]);
153 } 152 }
154 153
155 TEST(FilterTest, MissingSdchEncoding) { 154 TEST(FilterTest, MissingSdchEncoding) {
156 // Handle interesting case where entire SDCH encoding assertion "got lost." 155 // Handle interesting case where entire SDCH encoding assertion "got lost."
157 const std::string kTextHtmlMime("text/html"); 156 const std::string kTextHtmlMime("text/html");
158 MockFilterContext filter_context; 157 MockFilterContext filter_context;
159 filter_context.SetSdchResponse(true); 158 filter_context.SetSdchResponse(
159 SdchManager::CreateEmptyDictionarySetForTesting().Pass());
160 160
161 std::vector<Filter::FilterType> encoding_types; 161 std::vector<Filter::FilterType> encoding_types;
162 162
163 // Loss of encoding, but it was an SDCH response with html type. 163 // Loss of encoding, but it was an SDCH response with html type.
164 encoding_types.clear(); 164 encoding_types.clear();
165 filter_context.SetMimeType(kTextHtmlMime); 165 filter_context.SetMimeType(kTextHtmlMime);
166 Filter::FixupEncodingTypes(filter_context, &encoding_types); 166 Filter::FixupEncodingTypes(filter_context, &encoding_types);
167 ASSERT_EQ(2U, encoding_types.size()); 167 ASSERT_EQ(2U, encoding_types.size());
168 EXPECT_EQ(Filter::FILTER_TYPE_SDCH_POSSIBLE, encoding_types[0]); 168 EXPECT_EQ(Filter::FILTER_TYPE_SDCH_POSSIBLE, encoding_types[0]);
169 EXPECT_EQ(Filter::FILTER_TYPE_GZIP_HELPING_SDCH, encoding_types[1]); 169 EXPECT_EQ(Filter::FILTER_TYPE_GZIP_HELPING_SDCH, encoding_types[1]);
170 170
171 // Loss of encoding, but it was an SDCH response with a prefix that says it 171 // Loss of encoding, but it was an SDCH response with a prefix that says it
172 // was an html type. Note that it *should* be the case that a precise match 172 // was an html type. Note that it *should* be the case that a precise match
173 // with "text/html" we be collected by GetMimeType() and passed in, but we 173 // with "text/html" we be collected by GetMimeType() and passed in, but we
174 // coded the fixup defensively (scanning for a prefix of "text/html", so this 174 // coded the fixup defensively (scanning for a prefix of "text/html", so this
175 // is an example which could survive such confusion in the caller). 175 // is an example which could survive such confusion in the caller).
176 encoding_types.clear(); 176 encoding_types.clear();
177 filter_context.SetMimeType("text/html; charset=UTF-8"); 177 filter_context.SetMimeType("text/html; charset=UTF-8");
178 Filter::FixupEncodingTypes(filter_context, &encoding_types); 178 Filter::FixupEncodingTypes(filter_context, &encoding_types);
179 ASSERT_EQ(2U, encoding_types.size()); 179 ASSERT_EQ(2U, encoding_types.size());
180 EXPECT_EQ(Filter::FILTER_TYPE_SDCH_POSSIBLE, encoding_types[0]); 180 EXPECT_EQ(Filter::FILTER_TYPE_SDCH_POSSIBLE, encoding_types[0]);
181 EXPECT_EQ(Filter::FILTER_TYPE_GZIP_HELPING_SDCH, encoding_types[1]); 181 EXPECT_EQ(Filter::FILTER_TYPE_GZIP_HELPING_SDCH, encoding_types[1]);
182 182
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after
438 amount_to_copy); 438 amount_to_copy);
439 filter1->FlushStreamBuffer(amount_to_copy); 439 filter1->FlushStreamBuffer(amount_to_copy);
440 read_array_index += amount_to_copy; 440 read_array_index += amount_to_copy;
441 } while (true); 441 } while (true);
442 442
443 EXPECT_EQ(read_array_index, input_array_size); 443 EXPECT_EQ(read_array_index, input_array_size);
444 EXPECT_EQ(compare_array_index, input_array_size); 444 EXPECT_EQ(compare_array_index, input_array_size);
445 } 445 }
446 446
447 } // Namespace net 447 } // Namespace net
OLDNEW
« no previous file with comments | « net/filter/filter.cc ('k') | net/filter/mock_filter_context.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698