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
|
/*
* libjingle
* Copyright 2010, Google Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <string>
#include "talk/base/gunit.h"
#include "talk/base/helpers.h"
#include "talk/base/logging.h"
#include "talk/base/pathutils.h"
#include "talk/base/scoped_ptr.h"
#include "talk/base/multipart.h"
namespace talk_base {
static const std::string kTestMultipartBoundary = "123456789987654321";
static const std::string kTestContentType =
"multipart/form-data; boundary=123456789987654321";
static const char kTestData[] = "This is a test.";
static const char kTestStreamContent[] = "This is a test stream.";
TEST(MultipartTest, TestBasicOperations) {
MultipartStream multipart("multipart/form-data", kTestMultipartBoundary);
std::string content_type;
multipart.GetContentType(&content_type);
EXPECT_EQ(kTestContentType, content_type);
EXPECT_EQ(talk_base::SS_OPENING, multipart.GetState());
// The multipart stream contains only --boundary--\r\n
size_t end_part_size = multipart.GetEndPartSize();
multipart.EndParts();
EXPECT_EQ(talk_base::SS_OPEN, multipart.GetState());
size_t size;
EXPECT_TRUE(multipart.GetSize(&size));
EXPECT_EQ(end_part_size, size);
// Write is not supported.
EXPECT_EQ(talk_base::SR_ERROR,
multipart.Write(kTestData, sizeof(kTestData), NULL, NULL));
multipart.Close();
EXPECT_EQ(talk_base::SS_CLOSED, multipart.GetState());
EXPECT_TRUE(multipart.GetSize(&size));
EXPECT_EQ(0U, size);
}
TEST(MultipartTest, TestAddAndRead) {
MultipartStream multipart("multipart/form-data", kTestMultipartBoundary);
size_t part_size =
multipart.GetPartSize(kTestData, "form-data; name=\"text\"", "text");
EXPECT_TRUE(multipart.AddPart(kTestData, "form-data; name=\"text\"", "text"));
size_t size;
EXPECT_TRUE(multipart.GetSize(&size));
EXPECT_EQ(part_size, size);
talk_base::scoped_ptr<talk_base::MemoryStream> stream(
new talk_base::MemoryStream(kTestStreamContent));
size_t stream_size = 0;
EXPECT_TRUE(stream->GetSize(&stream_size));
part_size +=
multipart.GetPartSize("", "form-data; name=\"stream\"", "stream");
part_size += stream_size;
EXPECT_TRUE(multipart.AddPart(
new talk_base::MemoryStream(kTestStreamContent),
"form-data; name=\"stream\"",
"stream"));
EXPECT_TRUE(multipart.GetSize(&size));
EXPECT_EQ(part_size, size);
// In adding state, block read.
char buffer[1024];
EXPECT_EQ(talk_base::SR_BLOCK,
multipart.Read(buffer, sizeof(buffer), NULL, NULL));
// Write is not supported.
EXPECT_EQ(talk_base::SR_ERROR,
multipart.Write(buffer, sizeof(buffer), NULL, NULL));
part_size += multipart.GetEndPartSize();
multipart.EndParts();
EXPECT_TRUE(multipart.GetSize(&size));
EXPECT_EQ(part_size, size);
// Read the multipart stream into StringStream
std::string str;
talk_base::StringStream str_stream(str);
EXPECT_EQ(talk_base::SR_SUCCESS,
Flow(&multipart, buffer, sizeof(buffer), &str_stream));
EXPECT_EQ(size, str.length());
// Search three boundaries and two parts in the order.
size_t pos = 0;
pos = str.find(kTestMultipartBoundary);
EXPECT_NE(std::string::npos, pos);
pos += kTestMultipartBoundary.length();
pos = str.find(kTestData, pos);
EXPECT_NE(std::string::npos, pos);
pos += sizeof(kTestData);
pos = str.find(kTestMultipartBoundary, pos);
EXPECT_NE(std::string::npos, pos);
pos += kTestMultipartBoundary.length();
pos = str.find(kTestStreamContent, pos);
EXPECT_NE(std::string::npos, pos);
pos += sizeof(kTestStreamContent);
pos = str.find(kTestMultipartBoundary, pos);
EXPECT_NE(std::string::npos, pos);
pos += kTestMultipartBoundary.length();
pos = str.find(kTestMultipartBoundary, pos);
EXPECT_EQ(std::string::npos, pos);
}
} // namespace talk_base
|