| 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 | { |
| Jens Geyer | d6fc791 | 2025-11-19 22:04:39 +0100 | [diff] [blame^] | 52 | Assert.AreEqual(t.Item2, TJSONProtocolHelper.GetTypeNameForTypeId(t.Item1), $"Wrong mapping of TypeName {t.Item2} to TType: {t.Item1}"); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
| 56 | [TestMethod] |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 57 | public void GetTypeNameForTypeId_TStop_Test() |
| 58 | { |
| Jens Geyer | d6fc791 | 2025-11-19 22:04:39 +0100 | [diff] [blame^] | 59 | Assert.ThrowsExactly<TProtocolException>(() => TJSONProtocolHelper.GetTypeNameForTypeId(TType.Stop)); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | [TestMethod] |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 63 | public void GetTypeNameForTypeId_NonExistingTType_Test() |
| 64 | { |
| Jens Geyer | d6fc791 | 2025-11-19 22:04:39 +0100 | [diff] [blame^] | 65 | Assert.ThrowsExactly<TProtocolException>(() => TJSONProtocolHelper.GetTypeNameForTypeId((TType)100)); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | [TestMethod] |
| 69 | public void GetTypeIdForTypeName_Test() |
| 70 | { |
| 71 | // input/output |
| 72 | var sets = new List<Tuple<TType, byte[]>> |
| 73 | { |
| Jens Geyer | 4115e95 | 2023-11-21 23:00:01 +0100 | [diff] [blame] | 74 | 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 Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | foreach (var t in sets) |
| 88 | { |
| Jens Geyer | d6fc791 | 2025-11-19 22:04:39 +0100 | [diff] [blame^] | 89 | Assert.AreEqual(t.Item1, TJSONProtocolHelper.GetTypeIdForTypeName(t.Item2), $"Wrong mapping of TypeName {t.Item2} to TType: {t.Item1}"); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | [TestMethod] |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 94 | public void GetTypeIdForTypeName_TStopTypeName_Test() |
| 95 | { |
| Jens Geyer | d6fc791 | 2025-11-19 22:04:39 +0100 | [diff] [blame^] | 96 | Assert.ThrowsExactly<TProtocolException>(() => TJSONProtocolHelper.GetTypeIdForTypeName([(byte)TType.Stop, (byte)TType.Stop])); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | [TestMethod] |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 100 | public void GetTypeIdForTypeName_NonExistingTypeName_Test() |
| 101 | { |
| Jens Geyer | d6fc791 | 2025-11-19 22:04:39 +0100 | [diff] [blame^] | 102 | Assert.ThrowsExactly<TProtocolException>(() => TJSONProtocolHelper.GetTypeIdForTypeName([100])); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | [TestMethod] |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 106 | public void GetTypeIdForTypeName_EmptyName_Test() |
| 107 | { |
| Jens Geyer | d6fc791 | 2025-11-19 22:04:39 +0100 | [diff] [blame^] | 108 | Assert.ThrowsExactly<TProtocolException>(() => TJSONProtocolHelper.GetTypeIdForTypeName([])); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 109 | } |
| 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 Geyer | d6fc791 | 2025-11-19 22:04:39 +0100 | [diff] [blame^] | 123 | Assert.AreEqual(t.Item2, TJSONProtocolHelper.IsJsonNumeric(t.Item1), $"Wrong mapping of Char {t.Item1} to bool: {t.Item2}"); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 124 | } |
| 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 Geyer | d6fc791 | 2025-11-19 22:04:39 +0100 | [diff] [blame^] | 139 | Assert.AreEqual(t.Item2, actualResult, $"Wrong mapping of char byte {t.Item1} to it expected hex value: {t.Item2}. Actual hex value: {actualResult}"); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
| 143 | [TestMethod] |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 144 | public void ToHexVal_WrongInputChar_Test() |
| 145 | { |
| Jens Geyer | d6fc791 | 2025-11-19 22:04:39 +0100 | [diff] [blame^] | 146 | Assert.ThrowsExactly<TProtocolException>(() => TJSONProtocolHelper.ToHexVal((byte)'s')); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 147 | } |
| 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 Geyer | d6fc791 | 2025-11-19 22:04:39 +0100 | [diff] [blame^] | 161 | 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 Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | } |
| Jens Geyer | 4115e95 | 2023-11-21 23:00:01 +0100 | [diff] [blame] | 166 | } |