public void ExceptionTest() { AssertThrow<ArgumentNullException>(() => new EmberWriter(null, 1).Dispose()); AssertThrow<ArgumentException>(() => new EmberWriter(new MemoryStream(), 0).Dispose()); using (var writer = new EmberWriter(new MemoryStream(), 1)) { var outer = EmberId.CreateApplication(0); AssertThrow<ArgumentNullException>( () => writer.WriteValue(outer, (byte[])null), () => writer.WriteValue(outer, (int[])null), () => writer.WriteValue(outer, (string)null)); AssertThrow<ArgumentOutOfRangeException>( () => writer.WriteStartApplicationDefinedType(outer, InnerNumber.FirstApplication - 1)); writer.Dispose(); AssertThrow<ObjectDisposedException>( () => writer.WriteValue(outer, true), () => writer.WriteValue(outer, 0), () => writer.WriteValue(outer, new byte[] { }), () => writer.WriteValue(outer, 0.0), () => writer.WriteValue(outer, string.Empty), () => writer.WriteValue(outer, new int[] { }), () => writer.WriteStartSequence(outer), () => writer.WriteStartSet(outer), () => writer.WriteStartApplicationDefinedType(outer, InnerNumber.FirstApplication), () => writer.WriteEndContainer()); } }
public void ExceptionTest() { AssertThrow <ArgumentNullException>(() => new EmberWriter(null, 1).Dispose()); AssertThrow <ArgumentException>(() => new EmberWriter(new MemoryStream(), 0).Dispose()); using (var writer = new EmberWriter(new MemoryStream(), 1)) { var outer = EmberId.CreateApplication(0); AssertThrow <ArgumentNullException>( () => writer.WriteValue(outer, (byte[])null), () => writer.WriteValue(outer, (int[])null), () => writer.WriteValue(outer, (string)null)); AssertThrow <ArgumentOutOfRangeException>( () => writer.WriteStartApplicationDefinedType(outer, InnerNumber.FirstApplication - 1)); writer.Dispose(); AssertThrow <ObjectDisposedException>( () => writer.WriteValue(outer, true), () => writer.WriteValue(outer, 0), () => writer.WriteValue(outer, new byte[] { }), () => writer.WriteValue(outer, 0.0), () => writer.WriteValue(outer, string.Empty), () => writer.WriteValue(outer, new int[] { }), () => writer.WriteStartSequence(outer), () => writer.WriteStartSet(outer), () => writer.WriteStartApplicationDefinedType(outer, InnerNumber.FirstApplication), () => writer.WriteEndContainer()); } }
private static void WriteRelativeObjectIdentifier(XmlReader reader, EmberWriter writer, EmberId fieldId) { var pathElements = ReadValue(reader, r => r.ReadContentAsString()).Split('.'); var value = (pathElements.Length == 1) && string.IsNullOrEmpty(pathElements[0]) ? new int[0] : pathElements.Select(s => int.Parse(s, InvariantCulture)).ToArray(); writer.WriteValue(fieldId, value); }
public void SkipContentsTest() { using (var stream = new MemoryStream(new byte[] { 0x60, 0x03, 0x01, 0x01, 0xFF, 0x60, 0x03, 0x01, 0x01, 0x00 })) using (var reader = new EmberReader(stream, 1)) { Assert.IsTrue(reader.Read()); Assert.IsTrue(reader.Read()); Assert.IsFalse(reader.ReadContentsAsBoolean()); } var original = new byte[64]; this.Random.NextBytes(original); byte[] encoded; using (var stream = new MemoryStream()) { using (var writer = new EmberWriter(stream)) { writer.WriteValue(EmberId.CreateApplication(0), original); writer.WriteValue(EmberId.CreateApplication(1), true); } encoded = stream.ToArray(); } using (var stream = new MemoryStream(encoded)) using (var reader = new EmberReader(stream, 1)) { Assert.IsTrue(reader.Read()); Assert.AreEqual(InnerNumber.Octetstring, reader.InnerNumber); Assert.IsTrue(reader.Read()); Assert.AreEqual(InnerNumber.Boolean, reader.InnerNumber); Assert.AreEqual(true, reader.ReadContentsAsBoolean()); Assert.IsFalse(reader.Read()); } }
private static void WriteOctetstring(XmlReader reader, EmberWriter writer, EmberId fieldId) { var buffer = new byte[1024]; using (var stream = new MemoryStream()) { var read = ReadValue(reader, r => (int?)r.ReadContentAsBinHex(buffer, 0, buffer.Length), 0); while (read > 0) { stream.Write(buffer, 0, read.Value); read = reader.ReadContentAsBinHex(buffer, 0, buffer.Length); } writer.WriteValue(fieldId, stream.ToArray()); } }
public void InvalidXmlCharactersTest() { using (var stream = new MemoryStream()) { using (var writer = new EmberWriter(stream)) { writer.WriteValue(EmberId.CreateContextSpecific(0), "\0"); } var builder = new StringBuilder(); using (var xmlWriter = XmlWriter.Create(builder)) { var converter = new EmberConverter(); converter.ToXml(stream.ToArray(), xmlWriter); } } }
private object CopyCore(EmberWriter writer, int inner) { switch (inner) { case Ember.InnerNumber.Boolean: var boolean = this.ReadContentsAsBoolean(); writer.WriteValue(this.outer.GetValueOrDefault(), boolean); return(boolean); case Ember.InnerNumber.Integer: var int64 = this.ReadContentsAsInt64(); writer.WriteValue(this.outer.GetValueOrDefault(), int64); return(int64); case Ember.InnerNumber.Octetstring: var byteArray = this.ReadContentsAsByteArray(); writer.WriteValue(this.outer.GetValueOrDefault(), byteArray); return(byteArray); case Ember.InnerNumber.Real: var dbl = this.ReadContentsAsDouble(); writer.WriteValue(this.outer.GetValueOrDefault(), dbl); return(dbl); case Ember.InnerNumber.Utf8String: var str = this.ReadContentsAsString(); writer.WriteValue(this.outer.GetValueOrDefault(), str); return(str); case Ember.InnerNumber.RelativeObjectIdentifier: var int32Array = this.ReadContentsAsInt32Array(); writer.WriteValue(this.outer.GetValueOrDefault(), int32Array); return(int32Array); case Ember.InnerNumber.Sequence: writer.WriteStartSequence(this.outer.GetValueOrDefault()); this.CopyToEndContainer(writer, null); return(null); case Ember.InnerNumber.Set: writer.WriteStartSet(this.outer.GetValueOrDefault()); this.CopyToEndContainer(writer, null); return(null); default: writer.WriteStartApplicationDefinedType(this.outer.GetValueOrDefault(), inner); this.CopyToEndContainer(writer, null); return(null); } }
private object CopyCore(EmberWriter writer, int inner) { switch (inner) { case Ember.InnerNumber.Boolean: var boolean = this.ReadContentsAsBoolean(); writer.WriteValue(this.outer.GetValueOrDefault(), boolean); return boolean; case Ember.InnerNumber.Integer: var int64 = this.ReadContentsAsInt64(); writer.WriteValue(this.outer.GetValueOrDefault(), int64); return int64; case Ember.InnerNumber.Octetstring: var byteArray = this.ReadContentsAsByteArray(); writer.WriteValue(this.outer.GetValueOrDefault(), byteArray); return byteArray; case Ember.InnerNumber.Real: var dbl = this.ReadContentsAsDouble(); writer.WriteValue(this.outer.GetValueOrDefault(), dbl); return dbl; case Ember.InnerNumber.Utf8String: var str = this.ReadContentsAsString(); writer.WriteValue(this.outer.GetValueOrDefault(), str); return str; case Ember.InnerNumber.RelativeObjectIdentifier: var int32Array = this.ReadContentsAsInt32Array(); writer.WriteValue(this.outer.GetValueOrDefault(), int32Array); return int32Array; case Ember.InnerNumber.Sequence: writer.WriteStartSequence(this.outer.GetValueOrDefault()); this.CopyToEndContainer(writer, null); return null; case Ember.InnerNumber.Set: writer.WriteStartSet(this.outer.GetValueOrDefault()); this.CopyToEndContainer(writer, null); return null; default: writer.WriteStartApplicationDefinedType(this.outer.GetValueOrDefault(), inner); this.CopyToEndContainer(writer, null); return null; } }
private void FromXmlCore( XmlReader reader, EmberWriter writer, FieldPath <string, string> previousPath, string currentType) { if (reader.IsEmptyElement) { writer.WriteEndContainer(); return; } while (ReadNext(reader)) { if (reader.NodeType == XmlNodeType.EndElement) { writer.WriteEndContainer(); return; } if (reader.NodeType != XmlNodeType.Element) { const string Format = "Unexpected Node Type: Encountered {0} while looking for {1}."; throw new XmlException( string.Format(InvariantCulture, Format, reader.NodeType, XmlNodeType.Element)); } var currentPath = Combine(previousPath, new Field <string, string>(currentType, reader.Name)); var fieldId = this.GetFieldId(currentPath); if (reader.AttributeCount != 1) { throw new XmlException( "Unexpected Attribute Count: Each element must have exactly one type attribute."); } var nextType = reader.GetAttribute(0); switch (nextType) { case BerBoolean.Name: writer.WriteValue(fieldId, ReadValue(reader, r => r.ReadContentAsBoolean())); break; case BerInteger.Name: writer.WriteValue(fieldId, ReadValue(reader, r => r.ReadContentAsLong())); break; case BerOctetstring.Name: WriteOctetstring(reader, writer, fieldId); break; case BerReal.Name: writer.WriteValue(fieldId, ReadValue(reader, r => r.ReadContentAsDouble())); break; case BerUtf8String.Name: var value = ReadValue(reader, r => r.ReadContentAsString(), string.Empty); switch (currentPath.ToString()) { case "Parameter.contents.formula": case "Parameter.contents.enumeration": case "Parameter.contents.schemaIdentifiers": case "QualifiedParameter.contents.formula": case "QualifiedParameter.contents.enumeration": case "QualifiedParameter.contents.schemaIdentifiers": case "Node.contents.schemaIdentifiers": case "QualifiedNode.contents.schemaIdentifiers": case "Matrix.contents.schemaIdentifiers": case "QualifiedMatrix.contents.schemaIdentifiers": value = value.Replace(Environment.NewLine, "\n"); break; default: // Intentionally do nothing break; } writer.WriteValue(fieldId, value); break; case BerRelativeObjectIdentifier.Name: WriteRelativeObjectIdentifier(reader, writer, fieldId); break; case BerSequence.Name: writer.WriteStartSequence(fieldId); this.FromXmlCore(reader, writer, currentPath, nextType); break; case BerSet.Name: writer.WriteStartSet(fieldId); this.FromXmlCore(reader, writer, currentPath, nextType); break; default: writer.WriteStartApplicationDefinedType(fieldId, this.GetInnerNumber(nextType)); this.FromXmlCore(reader, writer, currentPath, nextType); break; } } }