encodedstreamtest.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // Tencent is pleased to support the open source community by making RapidJSON available.
  2. //
  3. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
  4. //
  5. // Licensed under the MIT License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // http://opensource.org/licenses/MIT
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #include "unittest.h"
  15. #include "rapidjson/filereadstream.h"
  16. #include "rapidjson/filewritestream.h"
  17. #include "rapidjson/encodedstream.h"
  18. #include "rapidjson/stringbuffer.h"
  19. #include "rapidjson/memorystream.h"
  20. #include "rapidjson/memorybuffer.h"
  21. using namespace rapidjson;
  22. class EncodedStreamTest : public ::testing::Test {
  23. public:
  24. EncodedStreamTest() : json_(), length_() {}
  25. virtual ~EncodedStreamTest();
  26. virtual void SetUp() {
  27. json_ = ReadFile("utf8.json", true, &length_);
  28. }
  29. virtual void TearDown() {
  30. free(json_);
  31. json_ = 0;
  32. }
  33. private:
  34. EncodedStreamTest(const EncodedStreamTest&);
  35. EncodedStreamTest& operator=(const EncodedStreamTest&);
  36. protected:
  37. static FILE* Open(const char* filename) {
  38. const char *paths[] = {
  39. "encodings",
  40. "bin/encodings",
  41. "../bin/encodings",
  42. "../../bin/encodings",
  43. "../../../bin/encodings"
  44. };
  45. char buffer[1024];
  46. for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) {
  47. sprintf(buffer, "%s/%s", paths[i], filename);
  48. FILE *fp = fopen(buffer, "rb");
  49. if (fp)
  50. return fp;
  51. }
  52. return 0;
  53. }
  54. static char *ReadFile(const char* filename, bool appendPath, size_t* outLength) {
  55. FILE *fp = appendPath ? Open(filename) : fopen(filename, "rb");
  56. if (!fp) {
  57. *outLength = 0;
  58. return 0;
  59. }
  60. fseek(fp, 0, SEEK_END);
  61. *outLength = static_cast<size_t>(ftell(fp));
  62. fseek(fp, 0, SEEK_SET);
  63. char* buffer = static_cast<char*>(malloc(*outLength + 1));
  64. size_t readLength = fread(buffer, 1, *outLength, fp);
  65. buffer[readLength] = '\0';
  66. fclose(fp);
  67. return buffer;
  68. }
  69. template <typename FileEncoding, typename MemoryEncoding>
  70. void TestEncodedInputStream(const char* filename) {
  71. // Test FileReadStream
  72. {
  73. char buffer[16];
  74. FILE *fp = Open(filename);
  75. ASSERT_TRUE(fp != 0);
  76. FileReadStream fs(fp, buffer, sizeof(buffer));
  77. EncodedInputStream<FileEncoding, FileReadStream> eis(fs);
  78. StringStream s(json_);
  79. while (eis.Peek() != '\0') {
  80. unsigned expected, actual;
  81. EXPECT_TRUE(UTF8<>::Decode(s, &expected));
  82. EXPECT_TRUE(MemoryEncoding::Decode(eis, &actual));
  83. EXPECT_EQ(expected, actual);
  84. }
  85. EXPECT_EQ('\0', s.Peek());
  86. fclose(fp);
  87. }
  88. // Test MemoryStream
  89. {
  90. size_t size;
  91. char* data = ReadFile(filename, true, &size);
  92. MemoryStream ms(data, size);
  93. EncodedInputStream<FileEncoding, MemoryStream> eis(ms);
  94. StringStream s(json_);
  95. while (eis.Peek() != '\0') {
  96. unsigned expected, actual;
  97. EXPECT_TRUE(UTF8<>::Decode(s, &expected));
  98. EXPECT_TRUE(MemoryEncoding::Decode(eis, &actual));
  99. EXPECT_EQ(expected, actual);
  100. }
  101. EXPECT_EQ('\0', s.Peek());
  102. free(data);
  103. EXPECT_EQ(size, eis.Tell());
  104. }
  105. }
  106. void TestAutoUTFInputStream(const char *filename, bool expectHasBOM) {
  107. // Test FileReadStream
  108. {
  109. char buffer[16];
  110. FILE *fp = Open(filename);
  111. ASSERT_TRUE(fp != 0);
  112. FileReadStream fs(fp, buffer, sizeof(buffer));
  113. AutoUTFInputStream<unsigned, FileReadStream> eis(fs);
  114. EXPECT_EQ(expectHasBOM, eis.HasBOM());
  115. StringStream s(json_);
  116. while (eis.Peek() != '\0') {
  117. unsigned expected, actual;
  118. EXPECT_TRUE(UTF8<>::Decode(s, &expected));
  119. EXPECT_TRUE(AutoUTF<unsigned>::Decode(eis, &actual));
  120. EXPECT_EQ(expected, actual);
  121. }
  122. EXPECT_EQ('\0', s.Peek());
  123. fclose(fp);
  124. }
  125. // Test MemoryStream
  126. {
  127. size_t size;
  128. char* data = ReadFile(filename, true, &size);
  129. MemoryStream ms(data, size);
  130. AutoUTFInputStream<unsigned, MemoryStream> eis(ms);
  131. EXPECT_EQ(expectHasBOM, eis.HasBOM());
  132. StringStream s(json_);
  133. while (eis.Peek() != '\0') {
  134. unsigned expected, actual;
  135. EXPECT_TRUE(UTF8<>::Decode(s, &expected));
  136. EXPECT_TRUE(AutoUTF<unsigned>::Decode(eis, &actual));
  137. EXPECT_EQ(expected, actual);
  138. }
  139. EXPECT_EQ('\0', s.Peek());
  140. free(data);
  141. EXPECT_EQ(size, eis.Tell());
  142. }
  143. }
  144. template <typename FileEncoding, typename MemoryEncoding>
  145. void TestEncodedOutputStream(const char* expectedFilename, bool putBOM) {
  146. // Test FileWriteStream
  147. {
  148. char filename[L_tmpnam];
  149. FILE* fp = TempFile(filename);
  150. char buffer[16];
  151. FileWriteStream os(fp, buffer, sizeof(buffer));
  152. EncodedOutputStream<FileEncoding, FileWriteStream> eos(os, putBOM);
  153. StringStream s(json_);
  154. while (s.Peek() != '\0') {
  155. bool success = Transcoder<UTF8<>, MemoryEncoding>::Transcode(s, eos);
  156. EXPECT_TRUE(success);
  157. }
  158. eos.Flush();
  159. fclose(fp);
  160. EXPECT_TRUE(CompareFile(filename, expectedFilename));
  161. remove(filename);
  162. }
  163. // Test MemoryBuffer
  164. {
  165. MemoryBuffer mb;
  166. EncodedOutputStream<FileEncoding, MemoryBuffer> eos(mb, putBOM);
  167. StringStream s(json_);
  168. while (s.Peek() != '\0') {
  169. bool success = Transcoder<UTF8<>, MemoryEncoding>::Transcode(s, eos);
  170. EXPECT_TRUE(success);
  171. }
  172. eos.Flush();
  173. EXPECT_TRUE(CompareBufferFile(mb.GetBuffer(), mb.GetSize(), expectedFilename));
  174. }
  175. }
  176. void TestAutoUTFOutputStream(UTFType type, bool putBOM, const char *expectedFilename) {
  177. // Test FileWriteStream
  178. {
  179. char filename[L_tmpnam];
  180. FILE* fp = TempFile(filename);
  181. char buffer[16];
  182. FileWriteStream os(fp, buffer, sizeof(buffer));
  183. AutoUTFOutputStream<unsigned, FileWriteStream> eos(os, type, putBOM);
  184. StringStream s(json_);
  185. while (s.Peek() != '\0') {
  186. bool success = Transcoder<UTF8<>, AutoUTF<unsigned> >::Transcode(s, eos);
  187. EXPECT_TRUE(success);
  188. }
  189. eos.Flush();
  190. fclose(fp);
  191. EXPECT_TRUE(CompareFile(filename, expectedFilename));
  192. remove(filename);
  193. }
  194. // Test MemoryBuffer
  195. {
  196. MemoryBuffer mb;
  197. AutoUTFOutputStream<unsigned, MemoryBuffer> eos(mb, type, putBOM);
  198. StringStream s(json_);
  199. while (s.Peek() != '\0') {
  200. bool success = Transcoder<UTF8<>, AutoUTF<unsigned> >::Transcode(s, eos);
  201. EXPECT_TRUE(success);
  202. }
  203. eos.Flush();
  204. EXPECT_TRUE(CompareBufferFile(mb.GetBuffer(), mb.GetSize(), expectedFilename));
  205. }
  206. }
  207. bool CompareFile(const char* filename, const char* expectedFilename) {
  208. size_t actualLength, expectedLength;
  209. char* actualBuffer = ReadFile(filename, false, &actualLength);
  210. char* expectedBuffer = ReadFile(expectedFilename, true, &expectedLength);
  211. bool ret = (expectedLength == actualLength) && memcmp(expectedBuffer, actualBuffer, actualLength) == 0;
  212. free(actualBuffer);
  213. free(expectedBuffer);
  214. return ret;
  215. }
  216. bool CompareBufferFile(const char* actualBuffer, size_t actualLength, const char* expectedFilename) {
  217. size_t expectedLength;
  218. char* expectedBuffer = ReadFile(expectedFilename, true, &expectedLength);
  219. bool ret = (expectedLength == actualLength) && memcmp(expectedBuffer, actualBuffer, actualLength) == 0;
  220. free(expectedBuffer);
  221. return ret;
  222. }
  223. char *json_;
  224. size_t length_;
  225. };
  226. EncodedStreamTest::~EncodedStreamTest() {}
  227. TEST_F(EncodedStreamTest, EncodedInputStream) {
  228. TestEncodedInputStream<UTF8<>, UTF8<> >("utf8.json");
  229. TestEncodedInputStream<UTF8<>, UTF8<> >("utf8bom.json");
  230. TestEncodedInputStream<UTF16LE<>, UTF16<> >("utf16le.json");
  231. TestEncodedInputStream<UTF16LE<>, UTF16<> >("utf16lebom.json");
  232. TestEncodedInputStream<UTF16BE<>, UTF16<> >("utf16be.json");
  233. TestEncodedInputStream<UTF16BE<>, UTF16<> >("utf16bebom.json");
  234. TestEncodedInputStream<UTF32LE<>, UTF32<> >("utf32le.json");
  235. TestEncodedInputStream<UTF32LE<>, UTF32<> >("utf32lebom.json");
  236. TestEncodedInputStream<UTF32BE<>, UTF32<> >("utf32be.json");
  237. TestEncodedInputStream<UTF32BE<>, UTF32<> >("utf32bebom.json");
  238. }
  239. TEST_F(EncodedStreamTest, AutoUTFInputStream) {
  240. TestAutoUTFInputStream("utf8.json", false);
  241. TestAutoUTFInputStream("utf8bom.json", true);
  242. TestAutoUTFInputStream("utf16le.json", false);
  243. TestAutoUTFInputStream("utf16lebom.json",true);
  244. TestAutoUTFInputStream("utf16be.json", false);
  245. TestAutoUTFInputStream("utf16bebom.json",true);
  246. TestAutoUTFInputStream("utf32le.json", false);
  247. TestAutoUTFInputStream("utf32lebom.json",true);
  248. TestAutoUTFInputStream("utf32be.json", false);
  249. TestAutoUTFInputStream("utf32bebom.json", true);
  250. {
  251. // Auto detection fail, use user defined UTF type
  252. const char json[] = "{ }";
  253. MemoryStream ms(json, sizeof(json));
  254. AutoUTFInputStream<unsigned, MemoryStream> eis(ms, kUTF8);
  255. EXPECT_FALSE(eis.HasBOM());
  256. EXPECT_EQ(kUTF8, eis.GetType());
  257. }
  258. }
  259. TEST_F(EncodedStreamTest, EncodedOutputStream) {
  260. TestEncodedOutputStream<UTF8<>, UTF8<> >("utf8.json", false);
  261. TestEncodedOutputStream<UTF8<>, UTF8<> >("utf8bom.json", true);
  262. TestEncodedOutputStream<UTF16LE<>, UTF16<> >("utf16le.json", false);
  263. TestEncodedOutputStream<UTF16LE<>, UTF16<> >("utf16lebom.json",true);
  264. TestEncodedOutputStream<UTF16BE<>, UTF16<> >("utf16be.json", false);
  265. TestEncodedOutputStream<UTF16BE<>, UTF16<> >("utf16bebom.json",true);
  266. TestEncodedOutputStream<UTF32LE<>, UTF32<> >("utf32le.json", false);
  267. TestEncodedOutputStream<UTF32LE<>, UTF32<> >("utf32lebom.json",true);
  268. TestEncodedOutputStream<UTF32BE<>, UTF32<> >("utf32be.json", false);
  269. TestEncodedOutputStream<UTF32BE<>, UTF32<> >("utf32bebom.json",true);
  270. }
  271. TEST_F(EncodedStreamTest, AutoUTFOutputStream) {
  272. TestAutoUTFOutputStream(kUTF8, false, "utf8.json");
  273. TestAutoUTFOutputStream(kUTF8, true, "utf8bom.json");
  274. TestAutoUTFOutputStream(kUTF16LE, false, "utf16le.json");
  275. TestAutoUTFOutputStream(kUTF16LE, true, "utf16lebom.json");
  276. TestAutoUTFOutputStream(kUTF16BE, false, "utf16be.json");
  277. TestAutoUTFOutputStream(kUTF16BE, true, "utf16bebom.json");
  278. TestAutoUTFOutputStream(kUTF32LE, false, "utf32le.json");
  279. TestAutoUTFOutputStream(kUTF32LE, true, "utf32lebom.json");
  280. TestAutoUTFOutputStream(kUTF32BE, false, "utf32be.json");
  281. TestAutoUTFOutputStream(kUTF32BE, true, "utf32bebom.json");
  282. }