You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
5.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  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. #include <string>
  28. #include "talk/base/gunit.h"
  29. #include "talk/base/helpers.h"
  30. #include "talk/base/logging.h"
  31. #include "talk/base/pathutils.h"
  32. #include "talk/base/scoped_ptr.h"
  33. #include "talk/base/multipart.h"
  34. namespace talk_base {
  35. static const std::string kTestMultipartBoundary = "123456789987654321";
  36. static const std::string kTestContentType =
  37. "multipart/form-data; boundary=123456789987654321";
  38. static const char kTestData[] = "This is a test.";
  39. static const char kTestStreamContent[] = "This is a test stream.";
  40. TEST(MultipartTest, TestBasicOperations) {
  41. MultipartStream multipart("multipart/form-data", kTestMultipartBoundary);
  42. std::string content_type;
  43. multipart.GetContentType(&content_type);
  44. EXPECT_EQ(kTestContentType, content_type);
  45. EXPECT_EQ(talk_base::SS_OPENING, multipart.GetState());
  46. // The multipart stream contains only --boundary--\r\n
  47. size_t end_part_size = multipart.GetEndPartSize();
  48. multipart.EndParts();
  49. EXPECT_EQ(talk_base::SS_OPEN, multipart.GetState());
  50. size_t size;
  51. EXPECT_TRUE(multipart.GetSize(&size));
  52. EXPECT_EQ(end_part_size, size);
  53. // Write is not supported.
  54. EXPECT_EQ(talk_base::SR_ERROR,
  55. multipart.Write(kTestData, sizeof(kTestData), NULL, NULL));
  56. multipart.Close();
  57. EXPECT_EQ(talk_base::SS_CLOSED, multipart.GetState());
  58. EXPECT_TRUE(multipart.GetSize(&size));
  59. EXPECT_EQ(0U, size);
  60. }
  61. TEST(MultipartTest, TestAddAndRead) {
  62. MultipartStream multipart("multipart/form-data", kTestMultipartBoundary);
  63. size_t part_size =
  64. multipart.GetPartSize(kTestData, "form-data; name=\"text\"", "text");
  65. EXPECT_TRUE(multipart.AddPart(kTestData, "form-data; name=\"text\"", "text"));
  66. size_t size;
  67. EXPECT_TRUE(multipart.GetSize(&size));
  68. EXPECT_EQ(part_size, size);
  69. talk_base::MemoryStream* stream =
  70. new talk_base::MemoryStream(kTestStreamContent);
  71. size_t stream_size = 0;
  72. EXPECT_TRUE(stream->GetSize(&stream_size));
  73. part_size +=
  74. multipart.GetPartSize("", "form-data; name=\"stream\"", "stream");
  75. part_size += stream_size;
  76. EXPECT_TRUE(multipart.AddPart(
  77. new talk_base::MemoryStream(kTestStreamContent),
  78. "form-data; name=\"stream\"",
  79. "stream"));
  80. EXPECT_TRUE(multipart.GetSize(&size));
  81. EXPECT_EQ(part_size, size);
  82. // In adding state, block read.
  83. char buffer[1024];
  84. EXPECT_EQ(talk_base::SR_BLOCK,
  85. multipart.Read(buffer, sizeof(buffer), NULL, NULL));
  86. // Write is not supported.
  87. EXPECT_EQ(talk_base::SR_ERROR,
  88. multipart.Write(buffer, sizeof(buffer), NULL, NULL));
  89. part_size += multipart.GetEndPartSize();
  90. multipart.EndParts();
  91. EXPECT_TRUE(multipart.GetSize(&size));
  92. EXPECT_EQ(part_size, size);
  93. // Read the multipart stream into StringStream
  94. std::string str;
  95. talk_base::StringStream str_stream(str);
  96. EXPECT_EQ(talk_base::SR_SUCCESS,
  97. Flow(&multipart, buffer, sizeof(buffer), &str_stream));
  98. EXPECT_EQ(size, str.length());
  99. // Search three boundaries and two parts in the order.
  100. size_t pos = 0;
  101. pos = str.find(kTestMultipartBoundary);
  102. EXPECT_NE(std::string::npos, pos);
  103. pos += kTestMultipartBoundary.length();
  104. pos = str.find(kTestData, pos);
  105. EXPECT_NE(std::string::npos, pos);
  106. pos += sizeof(kTestData);
  107. pos = str.find(kTestMultipartBoundary, pos);
  108. EXPECT_NE(std::string::npos, pos);
  109. pos += kTestMultipartBoundary.length();
  110. pos = str.find(kTestStreamContent, pos);
  111. EXPECT_NE(std::string::npos, pos);
  112. pos += sizeof(kTestStreamContent);
  113. pos = str.find(kTestMultipartBoundary, pos);
  114. EXPECT_NE(std::string::npos, pos);
  115. pos += kTestMultipartBoundary.length();
  116. pos = str.find(kTestMultipartBoundary, pos);
  117. EXPECT_EQ(std::string::npos, pos);
  118. }
  119. } // namespace talk_base