blob: 7266ed5fc006ed901c563256368432df3364d2fd [file] [log] [blame]
henrike@webrtc.org0e118e72013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2010, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <string>
29
30#include "talk/base/gunit.h"
31#include "talk/base/helpers.h"
32#include "talk/base/logging.h"
33#include "talk/base/pathutils.h"
34#include "talk/base/scoped_ptr.h"
35#include "talk/base/multipart.h"
36
37namespace talk_base {
38
39static const std::string kTestMultipartBoundary = "123456789987654321";
40static const std::string kTestContentType =
41 "multipart/form-data; boundary=123456789987654321";
42static const char kTestData[] = "This is a test.";
43static const char kTestStreamContent[] = "This is a test stream.";
44
45TEST(MultipartTest, TestBasicOperations) {
46 MultipartStream multipart("multipart/form-data", kTestMultipartBoundary);
47 std::string content_type;
48 multipart.GetContentType(&content_type);
49 EXPECT_EQ(kTestContentType, content_type);
50
51 EXPECT_EQ(talk_base::SS_OPENING, multipart.GetState());
52
53 // The multipart stream contains only --boundary--\r\n
54 size_t end_part_size = multipart.GetEndPartSize();
55 multipart.EndParts();
56 EXPECT_EQ(talk_base::SS_OPEN, multipart.GetState());
57 size_t size;
58 EXPECT_TRUE(multipart.GetSize(&size));
59 EXPECT_EQ(end_part_size, size);
60
61 // Write is not supported.
62 EXPECT_EQ(talk_base::SR_ERROR,
63 multipart.Write(kTestData, sizeof(kTestData), NULL, NULL));
64
65 multipart.Close();
66 EXPECT_EQ(talk_base::SS_CLOSED, multipart.GetState());
67 EXPECT_TRUE(multipart.GetSize(&size));
68 EXPECT_EQ(0U, size);
69}
70
71TEST(MultipartTest, TestAddAndRead) {
72 MultipartStream multipart("multipart/form-data", kTestMultipartBoundary);
73
74 size_t part_size =
75 multipart.GetPartSize(kTestData, "form-data; name=\"text\"", "text");
76 EXPECT_TRUE(multipart.AddPart(kTestData, "form-data; name=\"text\"", "text"));
77 size_t size;
78 EXPECT_TRUE(multipart.GetSize(&size));
79 EXPECT_EQ(part_size, size);
80
henrike@webrtc.orga113c082013-07-12 16:04:50 +000081 talk_base::scoped_ptr<talk_base::MemoryStream> stream(
82 new talk_base::MemoryStream(kTestStreamContent));
henrike@webrtc.org0e118e72013-07-10 00:45:36 +000083 size_t stream_size = 0;
84 EXPECT_TRUE(stream->GetSize(&stream_size));
85 part_size +=
86 multipart.GetPartSize("", "form-data; name=\"stream\"", "stream");
87 part_size += stream_size;
88
89 EXPECT_TRUE(multipart.AddPart(
90 new talk_base::MemoryStream(kTestStreamContent),
91 "form-data; name=\"stream\"",
92 "stream"));
93 EXPECT_TRUE(multipart.GetSize(&size));
94 EXPECT_EQ(part_size, size);
95
96 // In adding state, block read.
97 char buffer[1024];
98 EXPECT_EQ(talk_base::SR_BLOCK,
99 multipart.Read(buffer, sizeof(buffer), NULL, NULL));
100 // Write is not supported.
101 EXPECT_EQ(talk_base::SR_ERROR,
102 multipart.Write(buffer, sizeof(buffer), NULL, NULL));
103
104 part_size += multipart.GetEndPartSize();
105 multipart.EndParts();
106 EXPECT_TRUE(multipart.GetSize(&size));
107 EXPECT_EQ(part_size, size);
108
109 // Read the multipart stream into StringStream
110 std::string str;
111 talk_base::StringStream str_stream(str);
112 EXPECT_EQ(talk_base::SR_SUCCESS,
113 Flow(&multipart, buffer, sizeof(buffer), &str_stream));
114 EXPECT_EQ(size, str.length());
115
116 // Search three boundaries and two parts in the order.
117 size_t pos = 0;
118 pos = str.find(kTestMultipartBoundary);
119 EXPECT_NE(std::string::npos, pos);
120 pos += kTestMultipartBoundary.length();
121
122 pos = str.find(kTestData, pos);
123 EXPECT_NE(std::string::npos, pos);
124 pos += sizeof(kTestData);
125
126 pos = str.find(kTestMultipartBoundary, pos);
127 EXPECT_NE(std::string::npos, pos);
128 pos += kTestMultipartBoundary.length();
129
130 pos = str.find(kTestStreamContent, pos);
131 EXPECT_NE(std::string::npos, pos);
132 pos += sizeof(kTestStreamContent);
133
134 pos = str.find(kTestMultipartBoundary, pos);
135 EXPECT_NE(std::string::npos, pos);
136 pos += kTestMultipartBoundary.length();
137
138 pos = str.find(kTestMultipartBoundary, pos);
139 EXPECT_EQ(std::string::npos, pos);
140}
141
142} // namespace talk_base