Ejemplo n.º 1
0
        /// <summary>
        /// The QldbHash of an IonValue is just the IonHash of that value.
        /// </summary>
        ///
        /// <param name="value">Ion Value to be hashed.</param>
        ///
        /// <returns>Hashed result.</returns>
        internal static QldbHash ToQldbHash(IIonValue value)
        {
            IIonReader     reader     = IonReaderBuilder.Build(value);
            IIonHashReader hashReader = IonHashReaderBuilder.Standard()
                                        .WithHasherProvider(HasherProvider)
                                        .WithReader(reader)
                                        .Build();

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

            return(new QldbHash(hashReader.Digest()));
        }
Ejemplo n.º 2
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.º 3
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.º 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");
            }
        }
Ejemplo n.º 5
0
        public void Blob_PartialRead(int size, int step)
        {
            var blob = new byte[size];
            for (var i = 0; i < size; i++)
            {
                blob[i] = (byte)i;
            }

            var memStream = new MemoryStream();
            using (var writer = IonBinaryWriterBuilder.Build(memStream))
            {
                writer.WriteBlob(blob);
                writer.Finish();
            }
            var output = memStream.ToArray();
            var reader = IonReaderBuilder.Build(new MemoryStream(output));
            ReaderTestCommon.Blob_PartialRead(size, step, reader);
        }
Ejemplo n.º 6
0
        public IonSystemLite(IonTextWriterBuilder textWriterBuilder, PrivateIonBinaryWriterBuilder binaryWriterBuilder,
                             IonReaderBuilder readerBuilder)
        {
            _readerBuilder = readerBuilder;
            var catalog = textWriterBuilder.Catalog;

            //make sure we're on the same catalog here
            Debug.Assert(catalog == binaryWriterBuilder.Catalog);
            Debug.Assert(catalog == readerBuilder.Catalog);

            Context            = new ContainerlessContext(this);
            Catalog            = catalog;
            _systemSymbolTable = binaryWriterBuilder.InitialSymbolTable;
            Debug.Assert(_systemSymbolTable.IsSystem);
            binaryWriterBuilder.SymtabValueFactory = this;

            _textWriterBuilder   = textWriterBuilder.Immutable();
            _binaryWriterBuilder = binaryWriterBuilder.Immutable();
        }
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");
        }
        private List <List <KeyValuePair <string, string> > > BuildData(IResult result)
        {
            List <List <KeyValuePair <string, string> > > keyValuePairListList = new List <List <KeyValuePair <string, string> > >();
            IEnumerator <IIonValue> enumerator = result.GetEnumerator();

            while (enumerator.MoveNext())
            {
                List <KeyValuePair <string, string> > document = new List <KeyValuePair <string, string> >();
                using (IIonReader reader = IonReaderBuilder.Build(enumerator.Current, new ReaderOptions()))
                {
                    IonType type;
                    while ((type = reader.MoveNext()) != IonType.None)
                    {
                        ConnectionService.buildData1(document, reader, type);
                    }
                }
                keyValuePairListList.Add(document);
            }
            return(keyValuePairListList);
        }
Ejemplo n.º 9
0
        private void LargeIntTest(string path, string expectedStr)
        {
            var expected = BigInteger.Parse(expectedStr);
            var bytes    = DirStructure.IonTestFileAsBytes(path);
            var reader   = IonReaderBuilder.Build(bytes);

            try
            {
                reader.MoveNext();
                reader.StepIn();
                reader.MoveNext();
                Assert.AreEqual(expected, reader.BigIntegerValue());
                reader.MoveNext();
                Assert.AreEqual(expected, reader.BigIntegerValue());
                reader.StepOut();
            }
            finally
            {
                reader.Dispose();
            }
        }
Ejemplo n.º 10
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.º 11
0
        /// <summary>
        /// Apply the same writing logic to binary and text writers and assert the accuracy
        /// </summary>
        protected void AssertReaderWriter(Action <IIonReader> assertReader, Action <IIonWriter> writerFunc)
        {
            //bin
            using (var s = new MemoryStream())
            {
                var binWriter = IonBinaryWriterBuilder.Build(s);
                writerFunc(binWriter);
                s.Seek(0, SeekOrigin.Begin);
                var binReader = IonReaderBuilder.Build(s);
                assertReader(binReader);
            }

            //text
            var sw         = new StringWriter();
            var textWriter = IonTextWriterBuilder.Build(sw);

            writerFunc(textWriter);
            var str        = sw.ToString();
            var textReader = IonReaderBuilder.Build(str);

            assertReader(textReader);
        }
Ejemplo n.º 12
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.º 13
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.º 14
0
        public void MultipleBlobs()
        {
            IIonReader binReader;

            using (var ms = new MemoryStream())
            {
                ReadOnlySpan <byte> firstBlob  = new ReadOnlySpan <byte>(new byte[] { 68, 65, 72, 72, 75 });
                ReadOnlySpan <byte> secondBlob = new ReadOnlySpan <byte>(new byte[] { 68, 69 });
                ReadOnlySpan <byte> thirdBlob  = new ReadOnlySpan <byte>(new byte[] { 68, 65, 85 });

                var binWriter = IonBinaryWriterBuilder.Build(ms);
                binWriter.WriteBlob(firstBlob);
                binWriter.WriteBlob(secondBlob);
                binWriter.WriteBlob(thirdBlob);
                binWriter.Finish();

                ms.Seek(0, SeekOrigin.Begin);
                binReader = IonReaderBuilder.Build(ms);


                var type   = binReader.MoveNext();
                var buffer = binReader.NewByteArray();
                Assert.AreEqual(IonType.Blob, type);
                Assert.AreEqual("68 65 72 72 75 ", BytesToHex(buffer));

                type   = binReader.MoveNext();
                buffer = binReader.NewByteArray();
                Assert.AreEqual(IonType.Blob, type);
                Assert.AreEqual("68 69 ", BytesToHex(buffer));

                type   = binReader.MoveNext();
                buffer = binReader.NewByteArray();
                Assert.AreEqual(IonType.Blob, type);
                Assert.AreEqual("68 65 85 ", BytesToHex(buffer));
            }
        }
        public void Scratch()
        {
            string ionText = "{iRoNiCcAsE: 3}";

            IonSerializer   ionSerializer;
            IIonReader      reader;
            NormalCaseClass result;

            ionSerializer = new IonSerializer(new IonSerializationOptions {
                PropertyNameCaseInsensitive = false
            });
            reader = IonReaderBuilder.Build(ionText);
            result = ionSerializer.Deserialize <NormalCaseClass>(reader);
            Console.WriteLine(result.ironicCase == null); // could not deserialize differently-cased text
            // True

            ionSerializer = new IonSerializer(new IonSerializationOptions {
                PropertyNameCaseInsensitive = true
            });
            reader = IonReaderBuilder.Build(ionText);
            result = ionSerializer.Deserialize <NormalCaseClass>(reader);
            Console.WriteLine(result.ironicCase); // successfully deserializes differently-cased text
            // 3
        }
Ejemplo n.º 16
0
        public void MultipleClobs()
        {
            IIonReader binReader;

            using (var ms = new MemoryStream())
            {
                ReadOnlySpan <byte> firstClob  = new ReadOnlySpan <byte>(new byte[] { 97, 98, 99, 32, 100, 101 });
                ReadOnlySpan <byte> secondClob = new ReadOnlySpan <byte>(new byte[] { 65, 32, 66, 32, 67 });
                ReadOnlySpan <byte> thirdClob  = new ReadOnlySpan <byte>(new byte[] { 50, 51, 32, 54, 55 });

                var binWriter = IonBinaryWriterBuilder.Build(ms);
                binWriter.WriteClob(firstClob);  // {{ "abc de" }}
                binWriter.WriteClob(secondClob); // {{ "A B\tC" }}
                binWriter.WriteClob(thirdClob);  // {{ "23 56" }}
                binWriter.Finish();

                ms.Seek(0, SeekOrigin.Begin);
                binReader = IonReaderBuilder.Build(ms);


                var type   = binReader.MoveNext();
                var buffer = binReader.NewByteArray();
                Assert.AreEqual(IonType.Clob, type);
                Assert.AreEqual("abc de", Encoding.ASCII.GetString(buffer));

                type   = binReader.MoveNext();
                buffer = binReader.NewByteArray();
                Assert.AreEqual(IonType.Clob, type);
                Assert.AreEqual("A B C", Encoding.ASCII.GetString(buffer));

                type   = binReader.MoveNext();
                buffer = binReader.NewByteArray();
                Assert.AreEqual(IonType.Clob, type);
                Assert.AreEqual("23 67", Encoding.ASCII.GetString(buffer));
            }
        }
Ejemplo n.º 17
0
 /// <inheritdoc/>
 public IIonReader Create(Stream stream)
 {
     return(IonReaderBuilder.Build(stream, new ReaderOptions {
         Format = ReaderFormat.Detect
     }));
 }
Ejemplo n.º 18
0
 internal override IIonReader GetIonReader(IIonValue ionValue)
 {
     return(IonReaderBuilder.Build(ionValue.ToPrettyString()));
 }
Ejemplo n.º 19
0
        public void SerializesAndDeserializePropertiesWhenConflictingFieldNamesArePresent()
        {
            var stream = defaultSerializer.Serialize(new PropertyNameConflictsWithIonPropertyField
            {
                firstString          = "ignored",
                SomeOtherFirstString = "expected"
            });

            var serialized = StreamToIonValue(stream);

            Assert.IsFalse(serialized.ContainsField("SomeOtherFirstString"));
            Assert.IsTrue(serialized.ContainsField("firstString"));
            Assert.AreEqual(serialized.GetField("firstString").StringValue, "expected");

            var deserialized = defaultSerializer.Deserialize <PropertyNameConflictsWithIonPropertyField>(IonReaderBuilder.Build(serialized));

            Assert.AreEqual(deserialized.SomeOtherFirstString, "expected");
        }
Ejemplo n.º 20
0
 internal virtual IIonReader GetIonReader(IIonValue ionValue)
 {
     return(IonReaderBuilder.Build(ionValue));
 }
Ejemplo n.º 21
0
 internal virtual IIonReader GetIonReader(byte[] ionBinary)
 {
     return(IonReaderBuilder.Build(ionBinary));
 }
Ejemplo n.º 22
0
 /// <summary>
 /// The QldbHash of an IonValue is just the IonHash of that value.
 /// </summary>
 ///
 /// <param name="value">Ion Value to be hashed.</param>
 ///
 /// <returns>Hashed result.</returns>
 internal static QldbHash ToQldbHash(IIonValue value)
 {
     return(ToQldbHash(IonReaderBuilder.Build(value)));
 }
Ejemplo n.º 23
0
 internal static QldbHash ToQldbHash(ValueHolder value)
 {
     return(ToQldbHash(IonReaderBuilder.Build(value.IonBinary)));
 }
 public IIonReader Create(Stream stream)
 {
     return(IonReaderBuilder.Build(stream, new ReaderOptions {
         Format = ReaderFormat.Detect, Encoding = Encoding.Unicode
     }));
 }
Ejemplo n.º 25
0
        public void WriterReader()
        {
            var catalog = new SimpleCatalog();
            var table1  = SharedSymbolTable.NewSharedSymbolTable("table1", 1, null, new[] { "s1", "s2" });
            var table2  = SharedSymbolTable.NewSharedSymbolTable("table2", 1, null, new[] { "s3", "s4" });

            catalog.PutTable(table1);
            catalog.PutTable(table2);

            var stream = new MemoryStream();

            byte[] output;
            using (var binWriter = new ManagedBinaryWriter(stream, new[] { table1, table2 }))
            {
                binWriter.StepIn(IonType.Struct);
                binWriter.SetFieldName("s1");
                binWriter.WriteSymbol("s2");
                binWriter.SetFieldName("s3");
                binWriter.WriteSymbol("s4");
                binWriter.StepOut();
                binWriter.Finish();
                Assert.AreEqual(binWriter.SymbolTable.GetImportedMaxId(), binWriter.SymbolTable.MaxId);

                output = stream.ToArray();
            }

            var reader = IonReaderBuilder.Build(new MemoryStream(output));

            Assert.AreEqual(IonType.Struct, reader.MoveNext());
            reader.StepIn();
            Assert.AreEqual(IonType.Symbol, reader.MoveNext());
            var fns = reader.GetFieldNameSymbol();

            Assert.AreEqual(10, fns.Sid);
            Assert.IsNull(fns.Text);
            Assert.ThrowsException <UnknownSymbolException>(() => reader.CurrentFieldName);
            Assert.AreEqual(11, reader.SymbolValue().Sid);

            Assert.AreEqual(IonType.Symbol, reader.MoveNext());
            fns = reader.GetFieldNameSymbol();
            Assert.AreEqual(12, fns.Sid);
            Assert.IsNull(fns.Text);
            Assert.ThrowsException <UnknownSymbolException>(() => reader.CurrentFieldName);
            Assert.AreEqual(13, reader.SymbolValue().Sid);

            //make sure that a reader with the correct imports can read it
            var reader2 = new UserBinaryReader(new MemoryStream(output), catalog);

            Assert.AreEqual(IonType.Struct, reader2.MoveNext());
            var localTable = reader2.GetSymbolTable();

            Console.WriteLine(localTable.Find("s1").ToString());
            reader2.StepIn();
            Assert.AreEqual(IonType.Symbol, reader2.MoveNext());
            Assert.AreEqual("s1", reader2.CurrentFieldName);
            Assert.AreEqual("s2", reader2.SymbolValue().Text);

            Assert.AreEqual(IonType.Symbol, reader2.MoveNext());
            Assert.AreEqual("s3", reader2.CurrentFieldName);
            Assert.AreEqual("s4", reader2.SymbolValue().Text);
        }
Ejemplo n.º 26
0
 internal override IIonReader GetIonReader(IIonValue ionValue)
 {
     return(IonReaderBuilder.Build(ionValue));
 }