Jens Geyer | 06ad721 | 2014-02-16 15:48:57 +0100 | [diff] [blame] | 1 | /** |
| 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 | |
| 20 | using System; |
| 21 | using System.Collections.Generic; |
| 22 | using System.Diagnostics; |
| 23 | using System.IO; |
| 24 | using System.Linq; |
| 25 | using System.Text; |
| 26 | using Thrift.Protocol; |
| 27 | using Thrift.Transport; |
| 28 | |
| 29 | namespace JSONTest |
| 30 | { |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 31 | 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 Geyer | 06ad721 | 2014-02-16 15:48:57 +0100 | [diff] [blame] | 38 | |
| 39 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 40 | 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 Geyer | 06ad721 | 2014-02-16 15:48:57 +0100 | [diff] [blame] | 47 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 48 | Stream stm = new MemoryStream(); |
| 49 | TTransport trans = new TStreamTransport(null, stm); |
| 50 | TProtocol prot = new TJSONProtocol(trans); |
| 51 | prot.WriteBinary(dataWritten); |
Jens Geyer | 06ad721 | 2014-02-16 15:48:57 +0100 | [diff] [blame] | 52 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 53 | stm.Position = 0; |
| 54 | trans = new TStreamTransport(stm, null); |
| 55 | prot = new TJSONProtocol(trans); |
| 56 | byte[] dataRead = prot.ReadBinary(); |
Jens Geyer | 06ad721 | 2014-02-16 15:48:57 +0100 | [diff] [blame] | 57 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 58 | 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 Geyer | 06ad721 | 2014-02-16 15:48:57 +0100 | [diff] [blame] | 63 | |
| 64 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 65 | 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 Geyer | 06ad721 | 2014-02-16 15:48:57 +0100 | [diff] [blame] | 69 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 70 | // 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 Geyer | 06ad721 | 2014-02-16 15:48:57 +0100 | [diff] [blame] | 82 | } |