1 : // Copyright 2014 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #include "syzygy/kasko/internet_helpers.h"
16 :
17 : #include <string>
18 :
19 : #include "base/strings/string16.h"
20 : #include "base/strings/utf_string_conversions.h"
21 : #include "gtest/gtest.h"
22 : #include "syzygy/kasko/internet_unittest_helpers.h"
23 :
24 m : namespace kasko {
25 :
26 m : TEST(InternetHelpersTest, ParseContentType) {
27 m : const struct {
28 m : const base::char16* content_type;
29 m : const base::char16* expected_mime_type;
30 m : const base::char16* expected_charset;
31 m : const bool expected_had_charset;
32 m : const base::char16* expected_boundary;
33 m : } tests[] = {
34 m : { L"text/html; charset=utf-8",
35 m : L"text/html",
36 m : L"utf-8",
37 m : true,
38 m : L""
39 m : },
40 m : { L"text/html; charset=",
41 m : L"text/html",
42 m : L"",
43 m : true,
44 m : L""
45 m : },
46 m : { L"text/html; charset",
47 m : L"text/html",
48 m : L"",
49 m : false,
50 m : L""
51 m : },
52 m : { L"text/html; charset='",
53 m : L"text/html",
54 m : L"",
55 m : true,
56 m : L""
57 m : },
58 m : { L"text/html; charset='utf-8'",
59 m : L"text/html",
60 m : L"utf-8",
61 m : true,
62 m : L""
63 m : },
64 m : { L"text/html; charset=\"utf-8\"",
65 m : L"text/html",
66 m : L"utf-8",
67 m : true,
68 m : L""
69 m : },
70 m : { L"text/html; charset =utf-8",
71 m : L"text/html",
72 m : L"utf-8",
73 m : true,
74 m : L""
75 m : },
76 m : { L"text/html; charset= utf-8",
77 m : L"text/html",
78 m : L"utf-8",
79 m : true,
80 m : L""
81 m : },
82 m : { L"text/html; charset=utf-8 ",
83 m : L"text/html",
84 m : L"utf-8",
85 m : true,
86 m : L""
87 m : },
88 m : { L"text/html; boundary=\"WebKit-ada-df-dsf-adsfadsfs\"",
89 m : L"text/html",
90 m : L"",
91 m : false,
92 m : L"\"WebKit-ada-df-dsf-adsfadsfs\""
93 m : },
94 m : { L"text/html; boundary =\"WebKit-ada-df-dsf-adsfadsfs\"",
95 m : L"text/html",
96 m : L"",
97 m : false,
98 m : L"\"WebKit-ada-df-dsf-adsfadsfs\""
99 m : },
100 m : { L"text/html; boundary= \"WebKit-ada-df-dsf-adsfadsfs\"",
101 m : L"text/html",
102 m : L"",
103 m : false,
104 m : L"\"WebKit-ada-df-dsf-adsfadsfs\""
105 m : },
106 m : { L"text/html; boundary= \"WebKit-ada-df-dsf-adsfadsfs\" ",
107 m : L"text/html",
108 m : L"",
109 m : false,
110 m : L"\"WebKit-ada-df-dsf-adsfadsfs\""
111 m : },
112 m : { L"text/html; boundary=\"WebKit-ada-df-dsf-adsfadsfs \"",
113 m : L"text/html",
114 m : L"",
115 m : false,
116 m : L"\"WebKit-ada-df-dsf-adsfadsfs \""
117 m : },
118 m : { L"text/html; boundary=WebKit-ada-df-dsf-adsfadsfs",
119 m : L"text/html",
120 m : L"",
121 m : false,
122 m : L"WebKit-ada-df-dsf-adsfadsfs"
123 m : },
124 m : };
125 m : for (size_t i = 0; i < arraysize(tests); ++i) {
126 m : base::string16 mime_type;
127 m : base::string16 charset;
128 m : bool had_charset = false;
129 m : base::string16 boundary;
130 m : ParseContentType(tests[i].content_type, &mime_type, &charset, &had_charset,
131 m : &boundary);
132 m : EXPECT_EQ(tests[i].expected_mime_type, mime_type) << "i=" << i;
133 m : EXPECT_EQ(tests[i].expected_charset, charset) << "i=" << i;
134 m : EXPECT_EQ(tests[i].expected_had_charset, had_charset) << "i=" << i;
135 m : EXPECT_EQ(tests[i].expected_boundary, boundary) << "i=" << i;
136 m : }
137 m : }
138 :
139 m : TEST(InternetHelpersTest, ComposeAndDecomposeUrl) {
140 m : const struct {
141 m : const base::char16* url;
142 m : const base::char16* scheme;
143 m : const base::char16* host;
144 m : uint16_t port;
145 m : const base::char16* path;
146 m : } tests[] = {
147 m : {L"http://example.com/", L"http", L"example.com", 80, L"/"},
148 m : {L"https://example.com/", L"https", L"example.com", 443, L"/"},
149 m : {L"https://sub.example.com/", L"https", L"sub.example.com", 443, L"/"},
150 m : {L"https://example.com:9999/", L"https", L"example.com", 9999, L"/"},
151 m : {L"http://example.com/a/b/c", L"http", L"example.com", 80, L"/a/b/c"},
152 m : };
153 m : for (size_t i = 0; i < arraysize(tests); ++i) {
154 m : base::string16 scheme, host, path;
155 m : uint16_t port = 0;
156 m : EXPECT_TRUE(DecomposeUrl(tests[i].url, &scheme, &host, &port, &path))
157 m : << "i=" << i;
158 m : EXPECT_EQ(tests[i].scheme, scheme) << "i=" << i;
159 m : EXPECT_EQ(tests[i].host, host) << "i=" << i;
160 m : EXPECT_EQ(tests[i].port, port) << "i=" << i;
161 m : EXPECT_EQ(tests[i].path, path) << "i=" << i;
162 m : EXPECT_EQ(tests[i].url,
163 m : ComposeUrl(tests[i].host, tests[i].port, tests[i].path,
164 m : tests[i].scheme == base::string16(L"https")));
165 m : }
166 :
167 m : const base::char16* invalid_urls[] = {L"",
168 m : L"example.com",
169 m : L"example.com/foo",
170 m : L"/foo/bar",
171 m : L"example.com:80",
172 m : L"http://",
173 m : L"http:",
174 m : L"http:/example.com",
175 m : L"http:example.com"};
176 :
177 m : for (size_t i = 0; i < arraysize(invalid_urls); ++i) {
178 m : base::string16 scheme, host, path;
179 m : uint16_t port = 0;
180 m : EXPECT_FALSE(DecomposeUrl(invalid_urls[i], &scheme, &host, &port, &path))
181 m : << "i=" << i;
182 m : }
183 m : }
184 :
185 m : TEST(InternetHelpersTest, GenerateMultipartHttpRequestBoundary) {
186 m : base::string16 boundary1 = GenerateMultipartHttpRequestBoundary();
187 m : base::string16 boundary2 = GenerateMultipartHttpRequestBoundary();
188 m : EXPECT_FALSE(boundary1.empty());
189 m : EXPECT_FALSE(boundary2.empty());
190 m : EXPECT_NE(boundary1, boundary2);
191 m : ASSERT_EQ(base::string16::npos,
192 m : boundary1.find_first_not_of(L"-0123456789abcdefABCDEF"));
193 m : }
194 :
195 :
196 m : TEST(InternetHelpersTest, GenerateMultipartHttpRequestContentTypeHeader) {
197 m : base::string16 boundary = GenerateMultipartHttpRequestBoundary();
198 m : base::string16 content_type_header =
199 m : GenerateMultipartHttpRequestContentTypeHeader(boundary);
200 :
201 m : size_t semicolon = content_type_header.find(L':');
202 m : ASSERT_NE(base::string16::npos, semicolon);
203 :
204 m : base::string16 mime_type, charset, parsed_boundary;
205 m : bool had_charset = false;
206 m : ParseContentType(base::string16(content_type_header.begin() + semicolon + 1,
207 m : content_type_header.end()),
208 m : &mime_type, &charset, &had_charset, &parsed_boundary);
209 m : EXPECT_EQ(boundary, parsed_boundary);
210 m : EXPECT_TRUE(charset.empty());
211 m : EXPECT_FALSE(had_charset);
212 m : EXPECT_EQ(L"multipart/form-data", mime_type);
213 m : }
214 :
215 m : TEST(InternetHelpersTest, GenerateMultipartHttpRequestBody) {
216 m : std::map<base::string16, base::string16> parameters;
217 m : parameters[L"param"] = L"value";
218 m : base::string16 boundary = GenerateMultipartHttpRequestBoundary();
219 m : std::string file = "file contents";
220 m : base::string16 file_part_name = L"file_name";
221 :
222 m : std::string body = GenerateMultipartHttpRequestBody(parameters, file,
223 m : file_part_name, boundary);
224 m : ExpectMultipartMimeMessageIsPlausible(boundary, parameters, file,
225 m : base::WideToUTF8(file_part_name), body);
226 m : }
227 :
228 m : } // namespace kasko
|