blob: 98232210ddb192f99073966c893b4f9c31c97965 [file] [log] [blame]
Jens Geyer06ad7212014-02-16 15:48:57 +01001/**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20using System;
21using System.Collections.Generic;
22using System.Diagnostics;
23using System.IO;
24using System.Linq;
25using System.Text;
26using Thrift.Protocol;
27using Thrift.Transport;
28
29namespace JSONTest
30{
Jens Geyerd5436f52014-10-03 19:50:38 +020031 class Program
32 {
33 static void Main(string[] args)
34 {
35 TestThrift2365(); // JSON binary decodes too much data
36 TestThrift2336(); // hex encoding using \uXXXX where 0xXXXX > 0xFF
37 }
Jens Geyer06ad7212014-02-16 15:48:57 +010038
39
Jens Geyerd5436f52014-10-03 19:50:38 +020040 public static void TestThrift2365()
41 {
42 var rnd = new Random();
43 for (var len = 0; len < 10; ++len)
44 {
45 byte[] dataWritten = new byte[len];
46 rnd.NextBytes(dataWritten);
Jens Geyer06ad7212014-02-16 15:48:57 +010047
Jens Geyerd5436f52014-10-03 19:50:38 +020048 Stream stm = new MemoryStream();
49 TTransport trans = new TStreamTransport(null, stm);
50 TProtocol prot = new TJSONProtocol(trans);
51 prot.WriteBinary(dataWritten);
Jens Geyer06ad7212014-02-16 15:48:57 +010052
Jens Geyerd5436f52014-10-03 19:50:38 +020053 stm.Position = 0;
54 trans = new TStreamTransport(stm, null);
55 prot = new TJSONProtocol(trans);
56 byte[] dataRead = prot.ReadBinary();
Jens Geyer06ad7212014-02-16 15:48:57 +010057
Jens Geyerd5436f52014-10-03 19:50:38 +020058 Debug.Assert(dataRead.Length == dataWritten.Length);
59 for (var i = 0; i < dataRead.Length; ++i)
60 Debug.Assert(dataRead[i] == dataWritten[i]);
61 }
62 }
Jens Geyer06ad7212014-02-16 15:48:57 +010063
64
Jens Geyerd5436f52014-10-03 19:50:38 +020065 public static void TestThrift2336()
66 {
67 const string RUSSIAN_TEXT = "\u0420\u0443\u0441\u0441\u043a\u043e\u0435 \u041d\u0430\u0437\u0432\u0430\u043d\u0438\u0435";
68 const string RUSSIAN_JSON = "\"\\u0420\\u0443\\u0441\\u0441\\u043a\\u043e\\u0435 \\u041d\\u0430\\u0437\\u0432\\u0430\\u043d\\u0438\\u0435\"";
Jens Geyer06ad7212014-02-16 15:48:57 +010069
Jens Geyerd5436f52014-10-03 19:50:38 +020070 // prepare buffer with JSON data
71 byte[] rawBytes = new byte[RUSSIAN_JSON.Length];
72 for (var i = 0; i < RUSSIAN_JSON.Length; ++i)
73 rawBytes[i] = (byte)(RUSSIAN_JSON[i] & (char)0xFF); // only low bytes
74
75 // parse and check
76 var stm = new MemoryStream(rawBytes);
77 var trans = new TStreamTransport(stm, null);
78 var prot = new TJSONProtocol(trans);
79 Debug.Assert(prot.ReadString() == RUSSIAN_TEXT, "reading JSON with hex-encoded chars > 8 bit");
80 }
81 }
Jens Geyer06ad7212014-02-16 15:48:57 +010082}