]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/netcore/Tests/Thrift.Tests/Protocols/TJsonProtocolHelperTests.cs
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / thrift / lib / netcore / Tests / Thrift.Tests / Protocols / TJsonProtocolHelperTests.cs
1 // Licensed to the Apache Software Foundation(ASF) under one
2 // or more contributor license agreements.See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership.The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17
18 using System;
19 using System.Collections.Generic;
20 using System.Linq;
21 using Microsoft.VisualStudio.TestTools.UnitTesting;
22 using Thrift.Protocols;
23 using Thrift.Protocols.Entities;
24 using Thrift.Protocols.Utilities;
25
26 namespace Thrift.Tests.Protocols
27 {
28 [TestClass]
29 public class TJSONProtocolHelperTests
30 {
31 [TestMethod]
32 public void GetTypeNameForTypeId_Test()
33 {
34 // input/output
35 var sets = new List<Tuple<TType, byte[]>>
36 {
37 new Tuple<TType, byte[]>(TType.Bool, TJSONProtocolConstants.TypeNames.NameBool),
38 new Tuple<TType, byte[]>(TType.Byte, TJSONProtocolConstants.TypeNames.NameByte),
39 new Tuple<TType, byte[]>(TType.I16, TJSONProtocolConstants.TypeNames.NameI16),
40 new Tuple<TType, byte[]>(TType.I32, TJSONProtocolConstants.TypeNames.NameI32),
41 new Tuple<TType, byte[]>(TType.I64, TJSONProtocolConstants.TypeNames.NameI64),
42 new Tuple<TType, byte[]>(TType.Double, TJSONProtocolConstants.TypeNames.NameDouble),
43 new Tuple<TType, byte[]>(TType.String, TJSONProtocolConstants.TypeNames.NameString),
44 new Tuple<TType, byte[]>(TType.Struct, TJSONProtocolConstants.TypeNames.NameStruct),
45 new Tuple<TType, byte[]>(TType.Map, TJSONProtocolConstants.TypeNames.NameMap),
46 new Tuple<TType, byte[]>(TType.Set, TJSONProtocolConstants.TypeNames.NameSet),
47 new Tuple<TType, byte[]>(TType.List, TJSONProtocolConstants.TypeNames.NameList),
48 };
49
50 foreach (var t in sets)
51 {
52 Assert.IsTrue(TJSONProtocolHelper.GetTypeNameForTypeId(t.Item1) == t.Item2, $"Wrong mapping of TypeName {t.Item2} to TType: {t.Item1}");
53 }
54 }
55
56 [TestMethod]
57 [ExpectedException(typeof(TProtocolException))]
58 public void GetTypeNameForTypeId_TStop_Test()
59 {
60 TJSONProtocolHelper.GetTypeNameForTypeId(TType.Stop);
61 }
62
63 [TestMethod]
64 [ExpectedException(typeof(TProtocolException))]
65 public void GetTypeNameForTypeId_NonExistingTType_Test()
66 {
67 TJSONProtocolHelper.GetTypeNameForTypeId((TType)100);
68 }
69
70 [TestMethod]
71 public void GetTypeIdForTypeName_Test()
72 {
73 // input/output
74 var sets = new List<Tuple<TType, byte[]>>
75 {
76 new Tuple<TType, byte[]>(TType.Bool, TJSONProtocolConstants.TypeNames.NameBool),
77 new Tuple<TType, byte[]>(TType.Byte, TJSONProtocolConstants.TypeNames.NameByte),
78 new Tuple<TType, byte[]>(TType.I16, TJSONProtocolConstants.TypeNames.NameI16),
79 new Tuple<TType, byte[]>(TType.I32, TJSONProtocolConstants.TypeNames.NameI32),
80 new Tuple<TType, byte[]>(TType.I64, TJSONProtocolConstants.TypeNames.NameI64),
81 new Tuple<TType, byte[]>(TType.Double, TJSONProtocolConstants.TypeNames.NameDouble),
82 new Tuple<TType, byte[]>(TType.String, TJSONProtocolConstants.TypeNames.NameString),
83 new Tuple<TType, byte[]>(TType.Struct, TJSONProtocolConstants.TypeNames.NameStruct),
84 new Tuple<TType, byte[]>(TType.Map, TJSONProtocolConstants.TypeNames.NameMap),
85 new Tuple<TType, byte[]>(TType.Set, TJSONProtocolConstants.TypeNames.NameSet),
86 new Tuple<TType, byte[]>(TType.List, TJSONProtocolConstants.TypeNames.NameList),
87 };
88
89 foreach (var t in sets)
90 {
91 Assert.IsTrue(TJSONProtocolHelper.GetTypeIdForTypeName(t.Item2) == t.Item1, $"Wrong mapping of TypeName {t.Item2} to TType: {t.Item1}");
92 }
93 }
94
95 [TestMethod]
96 [ExpectedException(typeof(TProtocolException))]
97 public void GetTypeIdForTypeName_TStopTypeName_Test()
98 {
99 TJSONProtocolHelper.GetTypeIdForTypeName(new []{(byte)TType.Stop, (byte)TType.Stop});
100 }
101
102 [TestMethod]
103 [ExpectedException(typeof(TProtocolException))]
104 public void GetTypeIdForTypeName_NonExistingTypeName_Test()
105 {
106 TJSONProtocolHelper.GetTypeIdForTypeName(new byte[]{100});
107 }
108
109 [TestMethod]
110 [ExpectedException(typeof(TProtocolException))]
111 public void GetTypeIdForTypeName_EmptyName_Test()
112 {
113 TJSONProtocolHelper.GetTypeIdForTypeName(new byte[] {});
114 }
115
116 [TestMethod]
117 public void IsJsonNumeric_Test()
118 {
119 // input/output
120 var correctJsonNumeric = "+-.0123456789Ee";
121 var incorrectJsonNumeric = "AaBcDd/*\\";
122
123 var sets = correctJsonNumeric.Select(ch => new Tuple<byte, bool>((byte) ch, true)).ToList();
124 sets.AddRange(incorrectJsonNumeric.Select(ch => new Tuple<byte, bool>((byte) ch, false)));
125
126 foreach (var t in sets)
127 {
128 Assert.IsTrue(TJSONProtocolHelper.IsJsonNumeric(t.Item1) == t.Item2, $"Wrong mapping of Char {t.Item1} to bool: {t.Item2}");
129 }
130 }
131
132 [TestMethod]
133 public void ToHexVal_Test()
134 {
135 // input/output
136 var chars = "0123456789abcdef";
137 var expectedHexValues = new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
138
139 var sets = chars.Select((ch, i) => new Tuple<char, byte>(ch, expectedHexValues[i])).ToList();
140
141 foreach (var t in sets)
142 {
143 var actualResult = TJSONProtocolHelper.ToHexVal((byte)t.Item1);
144 Assert.IsTrue(actualResult == t.Item2, $"Wrong mapping of char byte {t.Item1} to it expected hex value: {t.Item2}. Actual hex value: {actualResult}");
145 }
146 }
147
148 [TestMethod]
149 [ExpectedException(typeof(TProtocolException))]
150 public void ToHexVal_WrongInputChar_Test()
151 {
152 TJSONProtocolHelper.ToHexVal((byte)'s');
153 }
154
155 [TestMethod]
156 public void ToHexChar_Test()
157 {
158 // input/output
159 var hexValues = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
160 var expectedChars = "0123456789abcdef";
161
162
163 var sets = hexValues.Select((hv, i) => new Tuple<byte, char>(hv, expectedChars[i])).ToList();
164
165 foreach (var t in sets)
166 {
167 var actualResult = TJSONProtocolHelper.ToHexChar(t.Item1);
168 Assert.IsTrue(actualResult == t.Item2, $"Wrong mapping of hex value {t.Item1} to it expected char: {t.Item2}. Actual hex value: {actualResult}");
169 }
170 }
171 }
172 }