public void EnhancedBlockStream_Int16() { var es = new EnhancedBlockStream(); es.WriteInt16(0); es.WriteInt16(55); es.WriteInt16(0x1234); es.Seek(0, SeekOrigin.Begin); Assert.AreEqual(0, es.ReadInt16()); Assert.AreEqual(55, es.ReadInt16()); Assert.AreEqual(0x1234, es.ReadInt16()); }
/// <summary> /// Renders the packet into a form suitable for transmission via UDP. /// </summary> /// <returns>The raw packet byte array.</returns> public byte[] ToArray() { var bs = new EnhancedBlockStream(0, 2048); bs.WriteByte((byte)this.Code); bs.WriteByte((byte)this.Identifier); bs.WriteInt16(0); // Put a zero in for the length and come back and fill // this in after we know what the actual length is. bs.WriteBytesNoLen(this.Authenticator); for (int i = 0; i < this.Attributes.Count; i++) { var attr = this.Attributes[i]; if (attr.Value.Length > RadiusAttribute.MaxValueLen) { throw new RadiusException("Attribute value size exceeds 253 bytes."); } bs.WriteByte((byte)attr.Type); bs.WriteByte((byte)(attr.Value.Length + 2)); bs.WriteBytesNoLen(attr.Value); } // Go back and write the actual length if (bs.Length > short.MaxValue) { throw new RadiusException("RADIUS packet is too large."); } bs.Position = 2; bs.WriteInt16((int)bs.Length); return(bs.ToArray()); }
public void EnhancedBlockStream_VerifyBufLength() { var es = new EnhancedBlockStream(); es.WriteInt16(5000); es.Seek(0, SeekOrigin.Begin); try { es.ReadBytes16(); Assert.Fail(); } catch { } es.Seek(0, SeekOrigin.Begin); try { es.ReadString16(); Assert.Fail(); } catch { } es.Seek(0, SeekOrigin.Begin); es.WriteInt32(500000); try { es.ReadBytes32(); Assert.Fail(); } catch { } es.Seek(0, SeekOrigin.Begin); try { es.ReadString32(); Assert.Fail(); } catch { } }