public void TestDecode() { ShortHeaderPacket shp = new ShortHeaderPacket(); shp.PacketNumber = 42; shp.DCID = new byte[] { 0x00, 0x00, 0x00, 0x7b }; shp.AddFrame(new DebugFrame { Message = "Message" }); byte[] pack = shp.Encode(); Packet p = Packet.Unpack(pack); Assert.AreEqual(p.GetType(), typeof(ShortHeaderPacket)); ShortHeaderPacket sh = p as ShortHeaderPacket; CollectionAssert.AreEqual(sh.DCID, new byte[] { 0x00, 0x00, 0x00, 0x7b }); Assert.AreEqual(sh.PacketNumber, (UInt64)42); Assert.AreEqual(sh.Spin, false); Assert.AreEqual(sh.KeyPhase, false); Assert.AreEqual(sh.PacketNumberLength, (UInt32)4); foreach (Frame f in p.Frames) { Assert.AreEqual(f.Type, 0x1e); DebugFrame fd = f as DebugFrame; Assert.AreEqual(fd.Message, "Message"); } }
static void Main(string[] args) { byte[] bytes = new VariableInteger(12345); VariableInteger integer = bytes; UInt64 uinteger = integer; int size = VariableInteger.Size(bytes[0]); InitialPacket packet = new InitialPacket() { Version = 16, SourceConnectionId = 124, DestinationConnectionId = 0, PacketNumber = 777521, TokenLength = 0 }; packet = new PacketCreator().CreateInitialPacket(124, 0); ConnectionCloseFrame frame = new ConnectionCloseFrame(ErrorCode.SERVER_BUSY, "The server is too busy to process your request."); MaxStreamIdFrame msidframe = new MaxStreamIdFrame(144123, StreamType.ClientUnidirectional); //packet.AttachFrame(frame); packet.AttachFrame(msidframe); byte[] data = packet.Encode(); string b64 = ToBase64(data); byte[] shpdata1 = new byte[] { 1, 1, 2, 3, 5, 8 }; byte[] shpdata2 = new byte[] { 13, 21, 34, 55, 89, 144 }; ShortHeaderPacket shp = new ShortHeaderPacket(); shp.DestinationConnectionId = 124; shp.PacketNumber = 2; shp.AttachFrame(new StreamFrame() { StreamId = 1, Length = new VariableInteger((UInt64)shpdata2.Length), StreamData = shpdata2, Offset = 6, EndOfStream = true }); shp.AttachFrame(new StreamFrame() { StreamId = 1, Length = new VariableInteger((UInt64)shpdata1.Length), StreamData = shpdata1, Offset = 0 }); string shpb64 = ToBase64(shp.Encode()); packet.Decode(data); byte[] ccfData = frame.Encode(); frame.Decode(new ByteArray(ccfData)); byte[] streamIdData = new StreamId(123, StreamType.ClientUnidirectional); StreamId streamId = streamIdData; QuicListener listener = new QuicListener(11000); listener.OnClientConnected += Listener_OnClientConnected; listener.Start(); }
public void TestEncode() { ShortHeaderPacket shp = new ShortHeaderPacket(); shp.PacketNumber = 42; shp.DCID = new byte[] { 0x00, 0x00, 0x00, 0x7b }; shp.AddFrame(new DebugFrame { Message = "Message" }); byte[] pack = shp.Encode(); /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 0 | 1 | S | R | R | K | P P | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Destination Connection ID(0..160) ... +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Packet Number(8 / 16 / 24 / 32)... +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Protected Payload(*)... +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ */ Assert.AreEqual(pack.Length, 17); Assert.AreEqual(pack[0] & 0x80, 0); // First bit Assert.AreEqual(pack[0] & 0x40, 0x40); // Second bit Assert.AreEqual(pack[0] & 0x20, 0); Assert.AreEqual(pack[0] & 0x04, 0); Assert.AreEqual(pack[0] & 0x02, 0x02); Assert.AreEqual(pack[0] & 0x01, 0x01); Assert.AreEqual(pack[1], 0); Assert.AreEqual(pack[2], 0); Assert.AreEqual(pack[3], 0); Assert.AreEqual(pack[4], 0x7b); Assert.AreEqual(pack[5], 0); Assert.AreEqual(pack[6], 0); Assert.AreEqual(pack[7], 0); Assert.AreEqual(pack[8], 42); // Payload Assert.AreEqual(pack[9], 0x1e); // Type Debug Assert.AreEqual(pack[10], Convert.ToByte('M')); Assert.AreEqual(pack[11], Convert.ToByte('e')); Assert.AreEqual(pack[12], Convert.ToByte('s')); Assert.AreEqual(pack[13], Convert.ToByte('s')); Assert.AreEqual(pack[14], Convert.ToByte('a')); Assert.AreEqual(pack[15], Convert.ToByte('g')); Assert.AreEqual(pack[16], Convert.ToByte('e')); }