Jens Geyer | 4115e95 | 2023-11-21 23:00:01 +0100 | [diff] [blame^] | 1 | // Licensed to the Apache Software Foundation(ASF) under one |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 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.Protocol; |
| 23 | using Thrift.Protocol.Entities; |
| 24 | using Thrift.Protocol.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 | { |
Jens Geyer | 4115e95 | 2023-11-21 23:00:01 +0100 | [diff] [blame^] | 37 | 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 Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 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 | { |
Jens Geyer | 4115e95 | 2023-11-21 23:00:01 +0100 | [diff] [blame^] | 76 | new(TType.Bool, TJSONProtocolConstants.TypeNames.NameBool), |
| 77 | new(TType.Byte, TJSONProtocolConstants.TypeNames.NameByte), |
| 78 | new(TType.I16, TJSONProtocolConstants.TypeNames.NameI16), |
| 79 | new(TType.I32, TJSONProtocolConstants.TypeNames.NameI32), |
| 80 | new(TType.I64, TJSONProtocolConstants.TypeNames.NameI64), |
| 81 | new(TType.Double, TJSONProtocolConstants.TypeNames.NameDouble), |
| 82 | new(TType.String, TJSONProtocolConstants.TypeNames.NameString), |
| 83 | new(TType.Struct, TJSONProtocolConstants.TypeNames.NameStruct), |
| 84 | new(TType.Map, TJSONProtocolConstants.TypeNames.NameMap), |
| 85 | new(TType.Set, TJSONProtocolConstants.TypeNames.NameSet), |
| 86 | new(TType.List, TJSONProtocolConstants.TypeNames.NameList), |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 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 | { |
Jens Geyer | 4115e95 | 2023-11-21 23:00:01 +0100 | [diff] [blame^] | 99 | TJSONProtocolHelper.GetTypeIdForTypeName([(byte)TType.Stop, (byte)TType.Stop]); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | [TestMethod] |
| 103 | [ExpectedException(typeof(TProtocolException))] |
| 104 | public void GetTypeIdForTypeName_NonExistingTypeName_Test() |
| 105 | { |
Jens Geyer | 4115e95 | 2023-11-21 23:00:01 +0100 | [diff] [blame^] | 106 | TJSONProtocolHelper.GetTypeIdForTypeName([100]); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | [TestMethod] |
| 110 | [ExpectedException(typeof(TProtocolException))] |
| 111 | public void GetTypeIdForTypeName_EmptyName_Test() |
| 112 | { |
Jens Geyer | 4115e95 | 2023-11-21 23:00:01 +0100 | [diff] [blame^] | 113 | TJSONProtocolHelper.GetTypeIdForTypeName([]); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 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 | } |
Jens Geyer | 4115e95 | 2023-11-21 23:00:01 +0100 | [diff] [blame^] | 172 | } |