public void TestMiscMethods() { // coverage for Digest(), IsInStruct, SetFieldName(), AddTypeAnnotation() StringWriter stringWriter = new StringWriter(); IIonHashWriter ihw = IonHashWriterBuilder .Standard() .WithHasherProvider(new IdentityIonHasherProvider()) .WithWriter(IonTextWriterBuilder.Build(stringWriter)) .Build(); TestUtil.AssertEquals(new byte[] { }, ihw.Digest(), "Digests don't match."); ihw.WriteNull(); TestUtil.AssertEquals(new byte[] { 0x0b, 0x0f, 0x0e }, ihw.Digest(), "Digests don't match."); ihw.StepIn(IonType.List); Assert.ThrowsException <InvalidOperationException>(ihw.Digest); ihw.WriteInt(5); Assert.ThrowsException <InvalidOperationException>(ihw.Digest); ihw.StepOut(); TestUtil.AssertEquals(new byte[] { 0x0b, 0xb0, 0x0b, 0x20, 0x05, 0x0e, 0x0e }, ihw.Digest(), "Digests don't match."); ihw.WriteNull(); TestUtil.AssertEquals(new byte[] { 0x0b, 0x0f, 0x0e }, ihw.Digest(), "Digests don't match."); Assert.IsFalse(ihw.IsInStruct); ihw.StepIn(IonType.Struct); Assert.IsTrue(ihw.IsInStruct); ihw.SetFieldName("hello"); ihw.AddTypeAnnotation("ion"); ihw.AddTypeAnnotation("hash"); ihw.WriteSymbol("world"); ihw.StepOut(); Assert.IsFalse(ihw.IsInStruct); TestUtil.AssertEquals(new byte[] { 0x0b, 0xd0, 0x0c, 0x0b, 0x70, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x0c, 0x0e, // hello: 0x0c, 0x0b, 0xe0, 0x0c, 0x0b, 0x70, 0x69, 0x6f, 0x6e, 0x0c, 0x0e, // ion:: 0x0c, 0x0b, 0x70, 0x68, 0x61, 0x73, 0x68, 0x0c, 0x0e, // hash:: 0x0c, 0x0b, 0x70, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x0c, 0x0e, // world 0x0c, 0x0e, 0x0e }, ihw.Digest(), "Digests don't match."); ihw.Finish(); //Commented out following assertion because it is failing //https://github.com/amzn/ion-hash-dotnet/issues/5 //Assert.AreEqual("null [5] null {hello:ion::hash::world}", stringWriter); }
public void FieldNameAsymmetry() { var memoryStream = new MemoryStream(); var writer = IonBinaryWriterBuilder.Build(memoryStream); IIonHashWriter ihw = IonHashWriterBuilder .Standard() .WithHasherProvider(new IdentityIonHasherProvider()) .WithWriter(writer) .Build(); // A nested struct: {a:{b:1}} writer.StepIn(IonType.Struct); writer.SetFieldName("a"); ihw.StepIn(IonType.Struct); ihw.SetFieldName("b"); ihw.WriteInt(1); ihw.StepOut(); byte[] writeHash = ihw.Digest(); ihw.Flush(); writer.StepOut(); writer.Flush(); var loader = IonLoader.Default; var ionValue = loader.Load(memoryStream.ToArray()) //Datagram .GetElementAt(0) //first struct .GetField("a"); //inner struct IIonReader reader = IonReaderBuilder.Build(ionValue); IIonHashReader ihr = IonHashReaderBuilder .Standard() .WithReader(reader) .WithHasherProvider(new IdentityIonHasherProvider()) .Build(); ihr.MoveNext(); // struct ihr.MoveNext(); // none TestUtil.AssertEquals(writeHash, ihr.Digest(), "Digest arrays mismatch"); }