blob: c05ee4dc97f01d2f9c1211ff49fd739dd575b88f [file] [log] [blame]
Jens Geyer4115e952023-11-21 23:00:01 +01001// Licensed to the Apache Software Foundation(ASF) under one
Jens Geyeraa0c8b32019-01-28 23:27:45 +01002// 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
18using System;
19using System.Collections.Generic;
20using System.Linq;
21using Microsoft.VisualStudio.TestTools.UnitTesting;
22using Thrift.Protocol;
23using Thrift.Protocol.Entities;
24using Thrift.Protocol.Utilities;
25
26namespace 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 {
Jens Geyer4115e952023-11-21 23:00:01 +010037 new(TType.Bool, TJSONProtocolConstants.TypeNames.NameBool),
38 new(TType.Byte, TJSONProtocolConstants.TypeNames.NameByte),
39 new(TType.I16, TJSONProtocolConstants.TypeNames.NameI16),
40 new(TType.I32, TJSONProtocolConstants.TypeNames.NameI32),
41 new(TType.I64, TJSONProtocolConstants.TypeNames.NameI64),
42 new(TType.Double, TJSONProtocolConstants.TypeNames.NameDouble),
43 new(TType.String, TJSONProtocolConstants.TypeNames.NameString),
44 new(TType.Struct, TJSONProtocolConstants.TypeNames.NameStruct),
45 new(TType.Map, TJSONProtocolConstants.TypeNames.NameMap),
46 new(TType.Set, TJSONProtocolConstants.TypeNames.NameSet),
47 new(TType.List, TJSONProtocolConstants.TypeNames.NameList),
Jens Geyeraa0c8b32019-01-28 23:27:45 +010048 };
49
50 foreach (var t in sets)
51 {
Jens Geyerd6fc7912025-11-19 22:04:39 +010052 Assert.AreEqual(t.Item2, TJSONProtocolHelper.GetTypeNameForTypeId(t.Item1), $"Wrong mapping of TypeName {t.Item2} to TType: {t.Item1}");
Jens Geyeraa0c8b32019-01-28 23:27:45 +010053 }
54 }
55
56 [TestMethod]
Jens Geyeraa0c8b32019-01-28 23:27:45 +010057 public void GetTypeNameForTypeId_TStop_Test()
58 {
Jens Geyerd6fc7912025-11-19 22:04:39 +010059 Assert.ThrowsExactly<TProtocolException>(() => TJSONProtocolHelper.GetTypeNameForTypeId(TType.Stop));
Jens Geyeraa0c8b32019-01-28 23:27:45 +010060 }
61
62 [TestMethod]
Jens Geyeraa0c8b32019-01-28 23:27:45 +010063 public void GetTypeNameForTypeId_NonExistingTType_Test()
64 {
Jens Geyerd6fc7912025-11-19 22:04:39 +010065 Assert.ThrowsExactly<TProtocolException>(() => TJSONProtocolHelper.GetTypeNameForTypeId((TType)100));
Jens Geyeraa0c8b32019-01-28 23:27:45 +010066 }
67
68 [TestMethod]
69 public void GetTypeIdForTypeName_Test()
70 {
71 // input/output
72 var sets = new List<Tuple<TType, byte[]>>
73 {
Jens Geyer4115e952023-11-21 23:00:01 +010074 new(TType.Bool, TJSONProtocolConstants.TypeNames.NameBool),
75 new(TType.Byte, TJSONProtocolConstants.TypeNames.NameByte),
76 new(TType.I16, TJSONProtocolConstants.TypeNames.NameI16),
77 new(TType.I32, TJSONProtocolConstants.TypeNames.NameI32),
78 new(TType.I64, TJSONProtocolConstants.TypeNames.NameI64),
79 new(TType.Double, TJSONProtocolConstants.TypeNames.NameDouble),
80 new(TType.String, TJSONProtocolConstants.TypeNames.NameString),
81 new(TType.Struct, TJSONProtocolConstants.TypeNames.NameStruct),
82 new(TType.Map, TJSONProtocolConstants.TypeNames.NameMap),
83 new(TType.Set, TJSONProtocolConstants.TypeNames.NameSet),
84 new(TType.List, TJSONProtocolConstants.TypeNames.NameList),
Jens Geyeraa0c8b32019-01-28 23:27:45 +010085 };
86
87 foreach (var t in sets)
88 {
Jens Geyerd6fc7912025-11-19 22:04:39 +010089 Assert.AreEqual(t.Item1, TJSONProtocolHelper.GetTypeIdForTypeName(t.Item2), $"Wrong mapping of TypeName {t.Item2} to TType: {t.Item1}");
Jens Geyeraa0c8b32019-01-28 23:27:45 +010090 }
91 }
92
93 [TestMethod]
Jens Geyeraa0c8b32019-01-28 23:27:45 +010094 public void GetTypeIdForTypeName_TStopTypeName_Test()
95 {
Jens Geyerd6fc7912025-11-19 22:04:39 +010096 Assert.ThrowsExactly<TProtocolException>(() => TJSONProtocolHelper.GetTypeIdForTypeName([(byte)TType.Stop, (byte)TType.Stop]));
Jens Geyeraa0c8b32019-01-28 23:27:45 +010097 }
98
99 [TestMethod]
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100100 public void GetTypeIdForTypeName_NonExistingTypeName_Test()
101 {
Jens Geyerd6fc7912025-11-19 22:04:39 +0100102 Assert.ThrowsExactly<TProtocolException>(() => TJSONProtocolHelper.GetTypeIdForTypeName([100]));
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100103 }
104
105 [TestMethod]
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100106 public void GetTypeIdForTypeName_EmptyName_Test()
107 {
Jens Geyerd6fc7912025-11-19 22:04:39 +0100108 Assert.ThrowsExactly<TProtocolException>(() => TJSONProtocolHelper.GetTypeIdForTypeName([]));
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100109 }
110
111 [TestMethod]
112 public void IsJsonNumeric_Test()
113 {
114 // input/output
115 var correctJsonNumeric = "+-.0123456789Ee";
116 var incorrectJsonNumeric = "AaBcDd/*\\";
117
118 var sets = correctJsonNumeric.Select(ch => new Tuple<byte, bool>((byte) ch, true)).ToList();
119 sets.AddRange(incorrectJsonNumeric.Select(ch => new Tuple<byte, bool>((byte) ch, false)));
120
121 foreach (var t in sets)
122 {
Jens Geyerd6fc7912025-11-19 22:04:39 +0100123 Assert.AreEqual(t.Item2, TJSONProtocolHelper.IsJsonNumeric(t.Item1), $"Wrong mapping of Char {t.Item1} to bool: {t.Item2}");
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100124 }
125 }
126
127 [TestMethod]
128 public void ToHexVal_Test()
129 {
130 // input/output
131 var chars = "0123456789abcdef";
132 var expectedHexValues = new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
133
134 var sets = chars.Select((ch, i) => new Tuple<char, byte>(ch, expectedHexValues[i])).ToList();
135
136 foreach (var t in sets)
137 {
138 var actualResult = TJSONProtocolHelper.ToHexVal((byte)t.Item1);
Jens Geyerd6fc7912025-11-19 22:04:39 +0100139 Assert.AreEqual(t.Item2, actualResult, $"Wrong mapping of char byte {t.Item1} to it expected hex value: {t.Item2}. Actual hex value: {actualResult}");
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100140 }
141 }
142
143 [TestMethod]
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100144 public void ToHexVal_WrongInputChar_Test()
145 {
Jens Geyerd6fc7912025-11-19 22:04:39 +0100146 Assert.ThrowsExactly<TProtocolException>(() => TJSONProtocolHelper.ToHexVal((byte)'s'));
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100147 }
148
149 [TestMethod]
150 public void ToHexChar_Test()
151 {
152 // input/output
153 var hexValues = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
154 var expectedChars = "0123456789abcdef";
155
156
157 var sets = hexValues.Select((hv, i) => new Tuple<byte, char>(hv, expectedChars[i])).ToList();
158
159 foreach (var t in sets)
160 {
Jens Geyerd6fc7912025-11-19 22:04:39 +0100161 var actualResult = (char)TJSONProtocolHelper.ToHexChar(t.Item1);
162 Assert.AreEqual(t.Item2, actualResult, $"Wrong mapping of hex value {t.Item1} to it expected char: {t.Item2}. Actual hex value: {actualResult}");
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100163 }
164 }
165 }
Jens Geyer4115e952023-11-21 23:00:01 +0100166}