示例#1
0
        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);
        }
示例#2
0
        public void TestExtraStepOut()
        {
            IIonHashWriter ihw = IonHashWriterBuilder
                                 .Standard()
                                 .WithHasherProvider(new IdentityIonHasherProvider())
                                 .WithWriter(IonTextWriterBuilder.Build(new StringWriter()))
                                 .Build();

            Assert.ThrowsException <InvalidOperationException>(ihw.StepOut);
        }
示例#3
0
        public void TestUnresolvedSid()
        {
            // unresolved SIDs (such as SID 10 here) should result in an exception
            SymbolToken symbolUnresolvedSid = new SymbolToken(null, 10);

            using (MemoryStream memoryStream = new MemoryStream())
            {
                IIonHashWriter writer = IonHashWriterBuilder
                                        .Standard()
                                        .WithHasherProvider(new IdentityIonHasherProvider())
                                        .WithWriter(IonBinaryWriterBuilder.Build(memoryStream))
                                        .Build();
                Assert.ThrowsException <UnknownSymbolException>(() => writer.WriteSymbolToken(symbolUnresolvedSid));
            }
        }
示例#4
0
        public void Test(TestValue tv, string s)
        {
            IIonHashWriter hashWriter = null;

            try
            {
                hashWriter = IonHashWriterBuilder.Standard()
                             .WithWriter(IonTextWriterBuilder.Build(new StringWriter()))
                             .WithHasherProvider(HasherProvider)
                             .Build();
                hashWriter.WriteValues(IonReaderBuilder.Build(s));
            }
            catch (IonException e)
            {
                if (tv.IsValidIon())
                {
                    throw e;
                }
            }

            IIonHashReader hashReader = null;

            try
            {
                hashReader = IonHashReaderBuilder.Standard()
                             .WithReader(IonReaderBuilder.Build(s))
                             .WithHasherProvider(HasherProvider)
                             .Build();
                hashReader.MoveNext();
                hashReader.MoveNext();
            }
            catch (IonException e)
            {
                if (tv.IsValidIon())
                {
                    throw e;
                }
            }

            if (tv.validIon == null || tv.validIon.Value == true)
            {
                TestUtil.AssertEquals(
                    hashWriter.Digest(),
                    hashReader.Digest(),
                    "Reader/writer hashes for line |" + tv.AsIon() + "| as |" + s + "| don't match");
            }
        }
示例#5
0
        private void AssertNoFieldnameInCurrentHash(string value, byte[] expectedBytes)
        {
            var reader = IonReaderBuilder.Build(value);

            reader.MoveNext();

            MemoryStream memoryStream = new MemoryStream();
            var          writer       = IonBinaryWriterBuilder.Build(memoryStream);

            writer.StepIn(IonType.Struct);

            IIonHashWriter ihw = IonHashWriterBuilder
                                 .Standard()
                                 .WithHasherProvider(new IdentityIonHasherProvider())
                                 .WithWriter(writer)
                                 .Build();

            ihw.SetFieldName("field_name");
            ihw.WriteValue(reader);
            byte[] actual = ihw.Digest();
            TestUtil.AssertEquals(expectedBytes, actual, "Digest arrays mismatch");
            writer.StepOut();

            ihw.Finish();
            writer.Finish();
            memoryStream.Flush();
            byte[] bytes = memoryStream.ToArray();
            memoryStream.Close();

            reader = IonReaderBuilder.Build(bytes);
            reader.MoveNext();
            reader.StepIn();
            IIonHashReader ihr = IonHashReaderBuilder
                                 .Standard()
                                 .WithHasherProvider(new IdentityIonHasherProvider())
                                 .WithReader(reader)
                                 .Build();

            ihr.MoveNext();   //List
            ihr.MoveNext();   //none
            actual = ihr.Digest();
            TestUtil.AssertEquals(expectedBytes, actual, "Digest arrays mismatch");
        }
示例#6
0
        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");
        }