public void addChild(TLV val) { if (child == null) { child = new List<TLV>(); } child.Add(val); }
public void TLV_Convert__1Level_Nominal() { TLV data = new TLV() { Tag = "AB", Length = 1, Value = "aa" }; TreeNode res = DataConverter.fromTLV(data); Assert.AreEqual("AB", res.Name); Assert.IsInstanceOfType(res.Tag,typeof(string[])); string[] nodeContent = (string[])res.Tag; Assert.AreEqual("01", nodeContent[0]); Assert.AreEqual("aa", nodeContent[1]); }
public void TLV_Normal_2Child() { var testData = "GG0A82088502332285023322"; TLV grandParent = new TLV() { Tag = "GG", Length = 10, Value = "82088502332285023322" }; TLV parent = new TLV() { Tag = "82", Length = 8, Value = "8502332285023322" }; TLV child1 = new TLV() { Tag = "85", Length = 2, Value = "3322" }; TLV child2 = new TLV() { Tag = "85", Length = 2, Value = "3322" }; parent.addChild(child1); parent.addChild(child2); }
public static List<TLV> Parse(string value) { List<TLV> result = null; try { value = StringManipulator.removeSpace(value); result = new List<TLV>(); TLV tlv; int marker = 0; while (marker < value.Length) { //fill up the Tag tlv = new TLV(); tlv.Tag = value.Substring(marker, 2); marker += 2; if (marker + 2 > value.Length) { marker += 2; continue; } // ber-TLV processing tlv.Length = Conversion_BER_TLV.ConvertFromString(value, ref marker); //fill up the value part if (tlv.Length * 2 + marker > value.Length) { marker = value.Length + 1; continue; } //if it's only TL without V then add the TLV immediately if (tlv.Length < 1) { result.Add(tlv); continue; } tlv.Value = value.Substring(marker, tlv.Length * 2); marker += tlv.Length * 2; result.Add(tlv); } } catch (Exception) { } return result; }
public void TLV_Normal_4Childs() { var testData = "D01B81030125008202818205008F0E0143535049524520476C6F62616C"; TLV expected = new TLV() { Tag = "D0", Length = 27, Value = "81030125008202818205008F0E0143535049524520476C6F62616C" }; TLV child1 = new TLV() { Tag = "81", Length = 3, Value = "012500" }; TLV child2 = new TLV() { Tag = "82", Length = 2, Value = "8182" }; TLV child3 = new TLV() { Tag = "05", Length = 0, Value = null }; TLV child4 = new TLV() { Tag = "8F", Length = 14, Value = "0143535049524520476C6F62616C" }; expected.addChild(child1); expected.addChild(child2); expected.addChild(child3); expected.addChild(child4); TLV result = TLV.parse(testData); Helper.compareTLV(expected, result); }
public void TestAPDUin1BERTLV() { //create a big TLV StringBuilder sb = new StringBuilder(); for (int i = 0; i < 200; i++) { sb.Append("BB"); } TLV expected = new TLV() { Tag = "09", // 09 Length = sb.Length / 2, // 100 Value = sb.ToString() // BB repeated 100 times }; List<TLV> result = APDU.Parse("0981C8" + sb.ToString()); Helper.compareTLV(expected, result[0]); }
public void TLV_Normal_2Parent() { var testData = "GG1082048502332283088606004455668877"; TLV grandParent = new TLV() { Tag = "GG", Length = 16, Value = "82048502332283088606004455668877" }; TLV parent1 = new TLV() { Tag = "82", Length = 4, Value = "85023322" }; TLV child1 = new TLV() { Tag = "85", Length = 2, Value = "3322" }; parent1.addChild(child1); TLV parent2 = new TLV { Tag = "83", Length = 8, Value = "8606004455668877" }; TLV child2 = new TLV() { Tag = "86", Length = 6, Value = "004455668877" }; parent2.addChild( child2 ); grandParent.addChild( parent1); grandParent.addChild(parent2); TLV result = TLV.parse(testData); Helper.compareTLV(grandParent, result); }
public void TLV_Convert__2Level_Nominal() { TLV parent= new TLV() { Tag = "AB", Length = 6, Value = "CC01A1CC01A2" }; TLV child1 = new TLV() { Tag = "CC", Length = 1, Value = "A1" }; TLV child2 = new TLV() { Tag = "CC", Length = 1, Value = "A2" }; parent.addChild(child1); parent.addChild(child2); //verify parent TreeNode res = DataConverter.fromTLV(parent); assertTreeNodeAndTLV(parent, res); // verify children Assert.IsNotNull(res.Nodes); Assert.AreEqual(2, res.Nodes.Count); // first chidren assertTreeNodeAndTLV(child1,res.Nodes[0]); // second chidren assertTreeNodeAndTLV(child2, res.Nodes[1]); }
public void TLV_Normal_2GrandChild() { var testData = "GG0C810A98088402889985020032"; TLV grandParent = new TLV() { Tag = "GG", Length = 12, Value = "810A98088402889985020032" }; TLV parent = new TLV() { Tag = "81", Length = 10, Value = "98088402889985020032" }; grandParent.addChild( parent ); TLV child = new TLV() { Tag = "98", Length = 8, Value = "8402889985020032" }; parent.addChild(child); TLV grandChild1= new TLV() { Tag = "84", Length = 2, Value = "8899" }; TLV grandChild2 = new TLV() { Tag = "85", Length = 2, Value = "0032" }; child.addChild(grandChild1); child.addChild(grandChild2); TLV result = TLV.parse(testData); Helper.compareTLV(grandParent, result); }
public void TLV_Convert__3Level_Nominal() { TLV parent = new TLV() { Tag = "AB", Length = 5, Value = "CC03GC01A2" }; TLV child = new TLV() { Tag = "CC", Length = 3, Value = "GC01A2" }; TLV grandChild = new TLV() { Tag = "GC", Length = 1, Value = "A2" }; parent.addChild(child); child.addChild(grandChild); //verify parent TreeNode res = DataConverter.fromTLV(parent); assertTreeNodeAndTLV(parent, res); // verify children Assert.IsNotNull(res.Nodes); Assert.AreEqual(1, res.Nodes.Count); assertTreeNodeAndTLV(child, res.Nodes[0]); // veriy grandchidren Assert.IsNotNull(res.Nodes[0].Nodes); Assert.AreEqual(1,res.Nodes[0].Nodes.Count); assertTreeNodeAndTLV(grandChild, res.Nodes[0].Nodes[0]); }
public static TreeNode fromTLV(TLV data) { if (data == null) { return null; } TreeNode result = new TreeNode(data.Tag); result.Name = data.Tag; // put the Length, Value, Description string[] content = { Conversion_BER_TLV.ConvertFromInt(data.Length), data.Value, data.Description }; result.Tag = content; //traverse each child if (data.child!= null) { foreach(var child in data.child){ if (child != null) { result.Nodes.Add(DataConverter.fromTLV(child)); } } } return result; }
private void assertTreeNodeAndTLV(TLV expected, TreeNode actual) { Assert.AreEqual(expected.Tag, actual.Name); string[] nodeContent3 = (string[])actual.Tag; Assert.IsInstanceOfType(actual.Tag, typeof(string[])); Assert.AreEqual(makeEven(expected.Length.ToString()),nodeContent3[0]); Assert.AreEqual(expected.Value, nodeContent3[1]); }
public void TestAPDUin2BERTLV() { //create a big TLV StringBuilder sb = new StringBuilder(); for (int i = 0; i < 65605; i++) { sb.Append("BB"); } TLV expected = new TLV() { Tag = "09", Length = sb.Length / 2, Value = sb.ToString() }; List<TLV> result = APDU.Parse("0983010045" + sb.ToString()); Helper.compareTLV(expected, result[0]); }
public static void compareTLV(TLV expected, TLV result) { Assert.AreEqual(expected.Length, result.Length); Assert.AreEqual(expected.Tag, result.Tag); Assert.AreEqual(expected.Value, result.Value); // if has a child then traverse to make sure all the child is the same if (expected.child != null) { compareListTLV(expected.child, result.child); } }
public void TLV_Normal_GrandChild() { var testData = "GG088106980484028899"; TLV grandParent = new TLV() { Tag = "GG", Length = 8, Value = "8106980484028899" }; TLV parent = new TLV() { Tag = "81", Length = 6, Value = "980484028899" }; grandParent.addChild(parent); TLV child = new TLV() { Tag = "98", Length = 4, Value = "84028899" }; parent.addChild(child); TLV grandChild = new TLV() { Tag = "84", Length = 2, Value = "8899" }; child.addChild(grandChild); TLV result = TLV.parse(testData); Helper.compareTLV(grandParent, result); }
public void TLV_Normal_NoChild() { var testData = "8010112233445566778899AABBCCDDEEFF00"; TLV expected = new TLV() { Tag = "80", Length = 16, Value = "112233445566778899AABBCCDDEEFF00" }; TLV parent = TLV.parse(testData); Helper.compareTLV(expected, parent); }