summaryrefslogtreecommitdiff
path: root/chromeos/http/http_form_data_unittest.cc
blob: fccef009caebab98853f8e0ee21a74128c1d5b8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright 2014 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <chromeos/http/http_form_data.h>

#include <set>

#include <base/files/file_util.h>
#include <base/files/scoped_temp_dir.h>
#include <chromeos/mime_utils.h>
#include <chromeos/streams/file_stream.h>
#include <chromeos/streams/input_stream_set.h>
#include <gtest/gtest.h>

namespace chromeos {
namespace http {
namespace {
std::string GetFormFieldData(FormField* field) {
  std::vector<StreamPtr> streams;
  CHECK(field->ExtractDataStreams(&streams));
  StreamPtr stream = InputStreamSet::Create(std::move(streams), nullptr);

  std::vector<uint8_t> data(stream->GetSize());
  EXPECT_TRUE(stream->ReadAllBlocking(data.data(), data.size(), nullptr));
  return std::string{data.begin(), data.end()};
}
}  // anonymous namespace

TEST(HttpFormData, TextFormField) {
  TextFormField form_field{"field1", "abcdefg", mime::text::kPlain, "7bit"};
  const char expected_header[] =
      "Content-Disposition: form-data; name=\"field1\"\r\n"
      "Content-Type: text/plain\r\n"
      "Content-Transfer-Encoding: 7bit\r\n"
      "\r\n";
  EXPECT_EQ(expected_header, form_field.GetContentHeader());
  EXPECT_EQ("abcdefg", GetFormFieldData(&form_field));
}

TEST(HttpFormData, FileFormField) {
  base::ScopedTempDir dir;
  ASSERT_TRUE(dir.CreateUniqueTempDir());
  std::string file_content{"text line1\ntext line2\n"};
  base::FilePath file_name = dir.path().Append("sample.txt");
  ASSERT_EQ(file_content.size(),
            static_cast<size_t>(base::WriteFile(
                file_name, file_content.data(), file_content.size())));

  StreamPtr stream = FileStream::Open(file_name, Stream::AccessMode::READ,
                                      FileStream::Disposition::OPEN_EXISTING,
                                      nullptr);
  ASSERT_NE(nullptr, stream);
  FileFormField form_field{"test_file",
                           std::move(stream),
                           "sample.txt",
                           content_disposition::kFormData,
                           mime::text::kPlain,
                           ""};
  const char expected_header[] =
      "Content-Disposition: form-data; name=\"test_file\";"
      " filename=\"sample.txt\"\r\n"
      "Content-Type: text/plain\r\n"
      "\r\n";
  EXPECT_EQ(expected_header, form_field.GetContentHeader());
  EXPECT_EQ(file_content, GetFormFieldData(&form_field));
}

TEST(HttpFormData, MultiPartFormField) {
  base::ScopedTempDir dir;
  ASSERT_TRUE(dir.CreateUniqueTempDir());
  std::string file1{"text line1\ntext line2\n"};
  base::FilePath filename1 = dir.path().Append("sample.txt");
  ASSERT_EQ(file1.size(),
            static_cast<size_t>(
                base::WriteFile(filename1, file1.data(), file1.size())));
  std::string file2{"\x01\x02\x03\x04\x05"};
  base::FilePath filename2 = dir.path().Append("test.bin");
  ASSERT_EQ(file2.size(),
            static_cast<size_t>(
                base::WriteFile(filename2, file2.data(), file2.size())));

  MultiPartFormField form_field{"foo", mime::multipart::kFormData, "Delimiter"};
  form_field.AddTextField("name", "John Doe");
  EXPECT_TRUE(form_field.AddFileField("file1",
                                      filename1,
                                      content_disposition::kFormData,
                                      mime::text::kPlain,
                                      nullptr));
  EXPECT_TRUE(form_field.AddFileField("file2",
                                      filename2,
                                      content_disposition::kFormData,
                                      mime::application::kOctet_stream,
                                      nullptr));
  const char expected_header[] =
      "Content-Disposition: form-data; name=\"foo\"\r\n"
      "Content-Type: multipart/form-data; boundary=\"Delimiter\"\r\n"
      "\r\n";
  EXPECT_EQ(expected_header, form_field.GetContentHeader());
  const char expected_data[] =
      "--Delimiter\r\n"
      "Content-Disposition: form-data; name=\"name\"\r\n"
      "\r\n"
      "John Doe\r\n"
      "--Delimiter\r\n"
      "Content-Disposition: form-data; name=\"file1\";"
      " filename=\"sample.txt\"\r\n"
      "Content-Type: text/plain\r\n"
      "Content-Transfer-Encoding: binary\r\n"
      "\r\n"
      "text line1\ntext line2\n\r\n"
      "--Delimiter\r\n"
      "Content-Disposition: form-data; name=\"file2\";"
      " filename=\"test.bin\"\r\n"
      "Content-Type: application/octet-stream\r\n"
      "Content-Transfer-Encoding: binary\r\n"
      "\r\n"
      "\x01\x02\x03\x04\x05\r\n"
      "--Delimiter--";
  EXPECT_EQ(expected_data, GetFormFieldData(&form_field));
}

TEST(HttpFormData, MultiPartBoundary) {
  const int count = 10;
  std::set<std::string> boundaries;
  for (int i = 0; i < count; i++) {
    MultiPartFormField field{""};
    std::string boundary = field.GetBoundary();
    boundaries.insert(boundary);
    // Our generated boundary must be 16 character long and contain lowercase
    // hexadecimal digits only.
    EXPECT_EQ(16u, boundary.size());
    EXPECT_EQ(std::string::npos,
              boundary.find_first_not_of("0123456789abcdef"));
  }
  // Now make sure the boundary strings were generated at random, so we should
  // get |count| unique boundary strings. However since the strings are random,
  // there is a very slim change of generating the same string twice, so
  // expect at least 90% of unique strings. 90% is picked arbitrarily here.
  int expected_min_unique = count * 9 / 10;
  EXPECT_GE(boundaries.size(), expected_min_unique);
}

TEST(HttpFormData, FormData) {
  base::ScopedTempDir dir;
  ASSERT_TRUE(dir.CreateUniqueTempDir());
  std::string file1{"text line1\ntext line2\n"};
  base::FilePath filename1 = dir.path().Append("sample.txt");
  ASSERT_EQ(file1.size(),
            static_cast<size_t>(
                base::WriteFile(filename1, file1.data(), file1.size())));
  std::string file2{"\x01\x02\x03\x04\x05"};
  base::FilePath filename2 = dir.path().Append("test.bin");
  ASSERT_EQ(file2.size(),
            static_cast<size_t>(
                base::WriteFile(filename2, file2.data(), file2.size())));

  FormData form_data{"boundary1"};
  form_data.AddTextField("name", "John Doe");
  std::unique_ptr<MultiPartFormField> files{
      new MultiPartFormField{"files", "", "boundary2"}};
  EXPECT_TRUE(files->AddFileField(
      "", filename1, content_disposition::kFile, mime::text::kPlain, nullptr));
  EXPECT_TRUE(files->AddFileField("",
                                  filename2,
                                  content_disposition::kFile,
                                  mime::application::kOctet_stream,
                                  nullptr));
  form_data.AddCustomField(std::move(files));
  EXPECT_EQ("multipart/form-data; boundary=\"boundary1\"",
            form_data.GetContentType());

  StreamPtr stream = form_data.ExtractDataStream();
  std::vector<uint8_t> data(stream->GetSize());
  EXPECT_TRUE(stream->ReadAllBlocking(data.data(), data.size(), nullptr));
  const char expected_data[] =
      "--boundary1\r\n"
      "Content-Disposition: form-data; name=\"name\"\r\n"
      "\r\n"
      "John Doe\r\n"
      "--boundary1\r\n"
      "Content-Disposition: form-data; name=\"files\"\r\n"
      "Content-Type: multipart/mixed; boundary=\"boundary2\"\r\n"
      "\r\n"
      "--boundary2\r\n"
      "Content-Disposition: file; filename=\"sample.txt\"\r\n"
      "Content-Type: text/plain\r\n"
      "Content-Transfer-Encoding: binary\r\n"
      "\r\n"
      "text line1\ntext line2\n\r\n"
      "--boundary2\r\n"
      "Content-Disposition: file; filename=\"test.bin\"\r\n"
      "Content-Type: application/octet-stream\r\n"
      "Content-Transfer-Encoding: binary\r\n"
      "\r\n"
      "\x01\x02\x03\x04\x05\r\n"
      "--boundary2--\r\n"
      "--boundary1--";
  EXPECT_EQ(expected_data, (std::string{data.begin(), data.end()}));
}
}  // namespace http
}  // namespace chromeos