Ejemplo n.º 1
0
        public void TestEmptyString()
        {
            IIonHashReader ihr = IonHashReaderBuilder
                                 .Standard()
                                 .WithHasherProvider(new IdentityIonHasherProvider())
                                 .WithReader(IonReaderBuilder.Build(""))
                                 .Build();

            Assert.AreEqual(IonType.None, ihr.MoveNext());
            TestUtil.AssertEquals(new byte[] { }, ihr.Digest(), "Digests don't match.");
            Assert.AreEqual(IonType.None, ihr.MoveNext());
            TestUtil.AssertEquals(new byte[] { }, ihr.Digest(), "Digests don't match.");
        }
Ejemplo n.º 2
0
        internal static QldbHash ToQldbHash(IIonReader reader)
        {
            IIonHashReader hashReader = IonHashReaderBuilder.Standard()
                                        .WithHasherProvider(HasherProvider)
                                        .WithReader(reader)
                                        .Build();

            while (hashReader.MoveNext() != IonType.None)
            {
            }

            return(new QldbHash(hashReader.Digest()));
        }
Ejemplo n.º 3
0
            internal virtual void Traverse(IIonHashReader reader)
            {
                IonType iType;

                while ((iType = reader.MoveNext()) != IonType.None)
                {
                    if (!reader.CurrentIsNull && IonTypeExtensions.IsContainer(iType))
                    {
                        reader.StepIn();
                        Traverse(reader);
                        reader.StepOut();
                    }
                }
            }
Ejemplo n.º 4
0
        public void TestIonReaderContract()
        {
            FileInfo  file         = DirStructure.IonHashDotnetTestFile("ion_hash_tests.ion");
            IIonValue ionHashTests = loader.Load(file);

            IIonReader ir = IonReaderBuilder.Build(ionHashTests);

            IIonHashReader ihr = IonHashReaderBuilder
                                 .Standard()
                                 .WithHasherProvider(new IdentityIonHasherProvider())
                                 .WithReader(IonReaderBuilder.Build(ionHashTests))
                                 .Build();

            ReaderCompare.Compare(ir, ihr);
        }
Ejemplo n.º 5
0
        public void TestUnresolvedSid()
        {
            // unresolved SIDs (such as SID 10 here) should result in an exception
            IIonValue ionContainer = loader.Load("(0xd3 0x8a 0x21 0x01)");

            byte[] ionBinary = IonHashTest.ContainerToBytes(ionContainer.GetElementAt(0));

            IIonHashReader ihr = IonHashReaderBuilder
                                 .Standard()
                                 .WithHasherProvider(new IdentityIonHasherProvider())
                                 .WithReader(IonReaderBuilder.Build(ionBinary))
                                 .Build();

            ihr.MoveNext();
            Assert.ThrowsException <UnknownSymbolException>(() => ihr.MoveNext());
        }
Ejemplo n.º 6
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");
            }
        }
Ejemplo n.º 7
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");
        }
Ejemplo n.º 8
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");
        }
Ejemplo n.º 9
0
        public void TestTopLevelValues()
        {
            IIonHashReader ihr = IonHashReaderBuilder
                                 .Standard()
                                 .WithHasherProvider(new IdentityIonHasherProvider())
                                 .WithReader(IonReaderBuilder.Build("1 2 3"))
                                 .Build();

            Assert.AreEqual(IonType.Int, ihr.MoveNext());
            TestUtil.AssertEquals(new byte[] { }, ihr.Digest(), "Digests don't match.");

            Assert.AreEqual(IonType.Int, ihr.MoveNext());
            TestUtil.AssertEquals(new byte[] { 0x0b, 0x20, 0x01, 0x0e }, ihr.Digest(), "Digests don't match.");

            Assert.AreEqual(IonType.Int, ihr.MoveNext());
            TestUtil.AssertEquals(new byte[] { 0x0b, 0x20, 0x02, 0x0e }, ihr.Digest(), "Digests don't match.");

            Assert.AreEqual(IonType.None, ihr.MoveNext());
            TestUtil.AssertEquals(new byte[] { 0x0b, 0x20, 0x03, 0x0e }, ihr.Digest(), "Digests don't match.");

            Assert.AreEqual(IonType.None, ihr.MoveNext());
            TestUtil.AssertEquals(new byte[] { }, ihr.Digest(), "Digests don't match.");
        }
Ejemplo n.º 10
0
        private void Consume(Action <IIonHashReader> lambda)
        {
            IIonHashReader ihr = IonHashReaderBuilder
                                 .Standard()
                                 .WithHasherProvider(new IdentityIonHasherProvider())
                                 .WithReader(IonReaderBuilder.Build("[1,2,{a:3,b:4},5]"))
                                 .Build();

            TestUtil.AssertEquals(new byte[] { }, ihr.Digest(), "Digests don't match.");
            lambda(ihr);
            TestUtil.AssertEquals(new byte[] { 0x0b, 0xb0,
                                               0x0b, 0x20, 0x01, 0x0e,
                                               0x0b, 0x20, 0x02, 0x0e,
                                               0x0b, 0xd0,
                                               0x0c, 0x0b, 0x70, 0x61, 0x0c, 0x0e, 0x0c, 0x0b, 0x20, 0x03, 0x0c, 0x0e,
                                               0x0c, 0x0b, 0x70, 0x62, 0x0c, 0x0e, 0x0c, 0x0b, 0x20, 0x04, 0x0c, 0x0e,
                                               0x0e,
                                               0x0b, 0x20, 0x05, 0x0e,
                                               0x0e }, ihr.Digest(), "Digests don't match.");

            Assert.AreEqual(IonType.None, ihr.MoveNext());
            TestUtil.AssertEquals(new byte[] { }, ihr.Digest(), "Digests don't match.");
        }
Ejemplo n.º 11
0
 internal override void Traverse(IIonHashReader reader)
 {
     while (reader.MoveNext() != IonType.None)
     {
     }
 }