public void TestClassAttributeBigEndianWithDefaultLittleEndianAndIndividualLittle() { SerializationDefaults serializationDefaults = new SerializationDefaults(); serializationDefaults.Endianness = Endiannesses.Little; TestEndiannessClass <TestClassClassAttributeBigEndianWithIndividualLittle>(serializationDefaults, new byte[] { 0x01, 0x02, 0x03, 0x07, 0x06, 0x05, 0x04, 0x7F, 0xFF, 0xFF, 0xFD, 0x04, 0x22, 0x11, 0x44, 0x33 }); }
protected void TestEndiannessClass <TTestClass>(SerializationDefaults serializationDefaults, byte[] expectedSerialization) where TTestClass : class, ITestClass, IMessageSerializable, new() { TTestClass testClass = new TTestClass(); testClass.Byte = 0x01; testClass.Short = 0x0203; testClass.Int = 0x04050607; testClass.Enum = TestEnum.High; testClass.ListShort = new List <short>(); testClass.ListShort.Add(0x1122); testClass.ListShort.Add(0x3344); Serializer.Instance.GetClassInfo(typeof(TTestClass), true, serializationDefaults); byte[] serialized = Serializer.Instance.Serialize(testClass); Assert.That(testClass.Length, Is.EqualTo(4), "Length"); Assert.That(serialized, Is.EqualTo(expectedSerialization), "Serialize"); TTestClass testClassDeserialized = Serializer.Instance.Deserialize <TTestClass>(serialized); Assert.That(testClassDeserialized.Byte, Is.EqualTo(testClass.Byte), "Byte"); Assert.That(testClassDeserialized.Short, Is.EqualTo(testClass.Short), "Short"); Assert.That(testClassDeserialized.Int, Is.EqualTo(testClass.Int), "Int"); }
public void TestNoDecorationWithDefaultBigEndian() { SerializationDefaults serializationDefaults = new SerializationDefaults(); serializationDefaults.Endianness = Endiannesses.Big; TestEndiannessClass <TestClassNoDecoration>(serializationDefaults, new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x7F, 0xFF, 0xFF, 0xFD, 0x04, 0x11, 0x22, 0x33, 0x44 }); }
public void TestNoAttributes() { SerializationDefaults serializationDefaults = new SerializationDefaults(); serializationDefaults.PropertyRules.Add(new PropertyRuleTestChecksum5555()); RunTest <TestChecksum5555MessageWithNoAttributes>(serializationDefaults); }
public TestSampleTypeSerializerThreeByteNumeric() { var serializationDefaults = new SerializationDefaults(); // We need to put our TypeSelector first, otherwise a property that matches will get matched by TypeSelectorNumeric first serializationDefaults.TypeSelectors.Insert(0, new TypeSelectorThreeByteNumeric()); _classInfo = Serializer.Instance.GetClassInfo(typeof(TestClass), serializationDefaults); }
public void TestSample() { SerializationDefaults serializationDefaults = new SerializationDefaults(); serializationDefaults.PropertyRules.Add(new PropertyRuleSampleHash()); Serializer.Instance.GetClassInfo(typeof(TestPropertyRuleSampleHash), serializationDefaults); TestPropertyRuleSampleHash testMessage = new TestPropertyRuleSampleHash(); testMessage.Int = 0x01020304; byte[] serialized = Serializer.Instance.Serialize(testMessage); TestPropertyRuleSampleHash deserialized = Serializer.Instance.Deserialize <TestPropertyRuleSampleHash>(serialized); }
protected void RunTest <T>(SerializationDefaults serializationDefaults) where T : class, ITestChecksum5555Message, new() { var testMessage = new T(); testMessage.MessageType = 1; testMessage.SomeNumber = 2; testMessage.NumberNotIncludedInChecksum = 3; testMessage.SomeOtherNumber = 4; // Length will automatically be calculated as 10 // Checksum is total of bytes in MessageType, Length, SomeNumber and SomeOtherNumber so 1 + 10 + 2 + 4 = 17 // 17 = 0x0011 and 0x0011 ^ 0x5555 = 0x5544 so that is what our CalculatorTestChecksum5555 will return byte[] serializedBytes = TestSerialize(testMessage, (bytes, serialized) => { int byteIndex = 0; Assert.That(bytes.Length, Is.EqualTo(14), "Total Length"); Assert.That(serialized.Length, Is.EqualTo(10), "Length Byte"); Assert.That(serialized.Checksum, Is.EqualTo(0x5544), "Calculated Checksum"); byteIndex += CheckNumeric(bytes, byteIndex, "Checksum", serialized.Checksum); Assert.That(bytes[byteIndex++], Is.EqualTo(serialized.MessageType), "MessageType"); Assert.That(bytes[byteIndex++], Is.EqualTo(serialized.Length), "Length"); byteIndex += CheckNumeric(bytes, byteIndex, "SomeNumber", serialized.SomeNumber); byteIndex += CheckNumeric(bytes, byteIndex, "NumberNotIncludedInChecksum", serialized.NumberNotIncludedInChecksum); byteIndex += CheckNumeric(bytes, byteIndex, "SomeOtherNumber", serialized.SomeOtherNumber); }, serializationDefaults); TestDeserialize(serializedBytes, testMessage, (deserializedObject, bytes, originalObject) => { Assert.That(deserializedObject.Checksum, Is.EqualTo(originalObject.Checksum), "Checksum"); Assert.That(deserializedObject.MessageType, Is.EqualTo(originalObject.MessageType), "MessageType"); Assert.That(deserializedObject.Length, Is.EqualTo(originalObject.Length), "Length"); Assert.That(deserializedObject.SomeNumber, Is.EqualTo(originalObject.SomeNumber), "SomeNumber"); Assert.That(deserializedObject.NumberNotIncludedInChecksum, Is.EqualTo(originalObject.NumberNotIncludedInChecksum), "NumberNotIncludedInChecksum"); Assert.That(deserializedObject.SomeOtherNumber, Is.EqualTo(originalObject.SomeOtherNumber), "SomeOtherNumber"); }, serializationDefaults); }
public void Test() { TestPropertyRuleSampleHash testMessage = new TestPropertyRuleSampleHash(); testMessage.Int = 0x01020304; SerializationDefaults serializationDefaults = new SerializationDefaults(); serializationDefaults.PropertyRules.Add(new PropertyRuleSampleHash()); byte[] serializedBytes = TestSerialize(testMessage, (bytes, serialized) => { int byteIndex = 0; Assert.That(bytes.Length, Is.EqualTo(37)); Assert.That(bytes[byteIndex], Is.EqualTo(36), "LengthByte"); byteIndex += CheckNumeric(bytes, byteIndex, "Length", serialized.Length); byteIndex += CheckNumeric(bytes, byteIndex, "Int", serialized.Int); // https://emn178.github.io/online-tools/sha256.html byte[] expectedHash = new byte[] { 0x24, 0x5c, 0x3e, 0x13, 0x49, 0xb2, 0xe4, 0x95, 0x71, 0xd2, 0x54, 0x4d, 0xc8, 0xbe, 0x38, 0x61, 0xb3, 0xf7, 0xb4, 0x91, 0x57, 0x42, 0x22, 0xae, 0x3c, 0xdc, 0x9f, 0x34, 0x52, 0x8c, 0x58, 0xd0 }; CheckMultiByteArray(serialized.Hash, 0, "HashSerialized", expectedHash); byteIndex += CheckMultiByteArray(bytes, byteIndex, "Hash", serialized.Hash); }, serializationDefaults); TestDeserialize(serializedBytes, testMessage, (deserializedObject, bytes, originalObject) => { Assert.That(deserializedObject.Length, Is.EqualTo(originalObject.Length), "Length"); Assert.That(deserializedObject.Int, Is.EqualTo(originalObject.Int), "Int"); Assert.That(deserializedObject.Hash, Is.EqualTo(originalObject.Hash), "Hash"); }, serializationDefaults); }
public void Check(MessageSerializedPropertyInfo messageSerializedPropertyInfo, SerializationDefaults serializationDefaults, MessageClassAttribute classAttribute) { if (!messageSerializedPropertyInfo.ContainsAuthenticationAttribute && messageSerializedPropertyInfo.PropertyInfo.Name.Equals("Hash", StringComparison.InvariantCultureIgnoreCase)) { messageSerializedPropertyInfo.CalculatedFieldAttributes.Add(new CalculatedAuthenticationResultAttribute(typeof(CalculatorAuthenticationSha256))); // Since we know the result of the hash is going to be 32 bytes if the Length property hasn't been set we'll set it. if (!messageSerializedPropertyInfo.MessagePropertyAttribute.IsLengthSpecified) { messageSerializedPropertyInfo.MessagePropertyAttribute.Length = 32; } } }
public void Check(MessageSerializedPropertyInfo messageSerializedPropertyInfo, SerializationDefaults serializationDefaults, MessageClassAttribute classAttribute) { if (!messageSerializedPropertyInfo.ContainsLengthAttribute && messageSerializedPropertyInfo.PropertyInfo.Name == "Length") { messageSerializedPropertyInfo.CalculatedFieldAttributes.Add(new CalculatedLengthResultAttribute()); } }
protected TClassType TestDeserialize <TClassType>(byte[] bytesToDeserialize, TClassType originalObject, VerifyDeserializedObjectDelegate verifyFunction, SerializationDefaults serializationDefaults = null) where TClassType : class, T { Serializer.Instance.GetClassInfo(typeof(TClassType), true, serializationDefaults); TClassType deserializedObject = Serializer.Instance.Deserialize <TClassType>(bytesToDeserialize); verifyFunction(deserializedObject, bytesToDeserialize, originalObject); if (_includeFileTesting) { ConfigMessageSerializerClass.WriteDefaultToFile(typeof(TClassType)); ConfigMessageSerializerClass configMessageSerializerClass = ConfigMessageSerializerClass.ReadFromFile(typeof(TClassType)); List <ConfigMessageSerializerClass> classList = new List <ConfigMessageSerializerClass>() { configMessageSerializerClass }; Serializer.Instance.GetClassInfo(typeof(TClassType), classList, true, serializationDefaults); TClassType deserializedObjectConfig = Serializer.Instance.Deserialize <TClassType>(bytesToDeserialize); verifyFunction(deserializedObjectConfig, bytesToDeserialize, originalObject); } return(deserializedObject); }
protected byte[] TestSerialize <TClassType>(TClassType objectToSerialize, VerifySerializedBytesDelegate verifyFunction, SerializationDefaults serializationDefaults = null) where TClassType : class, T { // We want to test loading by attributes and by file Serializer.Instance.GetClassInfo(typeof(TClassType), true, serializationDefaults); byte[] serializedBytes = Serializer.Instance.Serialize(objectToSerialize); verifyFunction(serializedBytes, objectToSerialize); if (_includeFileTesting) { ConfigMessageSerializerClass.WriteDefaultToFile(typeof(TClassType)); ConfigMessageSerializerClass configMessageSerializerClass = ConfigMessageSerializerClass.ReadFromFile(typeof(TClassType)); List <ConfigMessageSerializerClass> classList = new List <ConfigMessageSerializerClass>() { configMessageSerializerClass }; Serializer.Instance.GetClassInfo(typeof(TClassType), classList, true, serializationDefaults); byte[] serializedBytesConfig = Serializer.Instance.Serialize(objectToSerialize); verifyFunction(serializedBytesConfig, objectToSerialize); Assert.That(serializedBytes.SequenceEqual(serializedBytesConfig), Is.True, "BytesEqual"); } return(serializedBytes); }
public void Check(MessageSerializedPropertyInfo messageSerializedPropertyInfo, SerializationDefaults serializationDefaults, MessageClassAttribute classAttribute) { if (!messageSerializedPropertyInfo.CalculatedFieldAttributes.Any(item => item is CalculatedChecksum5555ResultAttribute || item is CalculatedChecksum5555Attribute)) { if (messageSerializedPropertyInfo.PropertyInfo.Name == "Checksum") { messageSerializedPropertyInfo.CalculatedFieldAttributes.Add(new CalculatedChecksum5555ResultAttribute()); } if (messageSerializedPropertyInfo.PropertyInfo.Name == "NumberNotIncludedInChecksum") { CalculatedChecksum5555Attribute attribute = new CalculatedChecksum5555Attribute(); attribute.Exclude = true; messageSerializedPropertyInfo.CalculatedFieldAttributes.Add(attribute); } } }
public void Check(MessageSerializedPropertyInfo messageSerializedPropertyInfo, SerializationDefaults serializationDefaults, MessageClassAttribute classAttribute) { if (!messageSerializedPropertyInfo.MessagePropertyAttribute.IsPrepadCharacterSpecified) { messageSerializedPropertyInfo.MessagePropertyAttribute.PrepadCharacter = '0'; } }
public void Check(MessageSerializedPropertyInfo messageSerializedPropertyInfo, SerializationDefaults serializationDefaults, MessageClassAttribute classAttribute) { var messagePropertyAttribute = messageSerializedPropertyInfo.MessagePropertyAttribute; if (!messagePropertyAttribute.IsEndiannessSpecified) { if (classAttribute.EndiannessExplicitlySpecified) { messagePropertyAttribute.Endianness = classAttribute.Endianness; } else if (serializationDefaults != null) { messagePropertyAttribute.Endianness = serializationDefaults.Endianness; } else { messagePropertyAttribute.Endianness = Endiannesses.System; } } }