func Test_ReadAndWrite(t *testing.T) { compound := &Compound{ map[string]ITag{ "Data": &Compound{ map[string]ITag{ "Byte": &Byte{5}, }, }, }, } serialized := []byte("" + "\x0a\x00\x00" + // Empty name containing Compound "\x0a\x00\x04Data" + // "Data" Compound "\x01\x00\x04Byte\x05" + // Byte{5} "\x00\x00") // End of both compounds. reader := bytes.NewBuffer(serialized) result, err := Read(reader) if err != nil { t.Fatalf("Got Read error: %v", err) } if !reflect.DeepEqual(compound, result) { t.Errorf("Got unexpected result: %#v", result) } // Test writing back out writer := new(bytes.Buffer) if err = Write(writer, compound); err != nil { t.Fatalf("Got Write error: %v", err) return } matcher := &te.BytesLiteral{serialized} if err = te.Matches(matcher, writer.Bytes()); err != nil { t.Errorf("Got unexpected output from Write: %v", err) } }
func testPacketSerial(t *testing.T, fromClient bool, outputPkt IPacket, expectedSerialization te.IBytesMatcher) { ps := new(PacketSerializer) // Test reading. input := new(bytes.Buffer) expectedSerialization.Write(input) if inputPkt, err := ps.ReadPacket(input, fromClient); err != nil { t.Errorf("Unexpected error reading packet: %v", err) } else { if !reflect.DeepEqual(outputPkt, inputPkt) { t.Errorf("Packet did not read expected value:\n expected: %#v\n result: %#v", outputPkt, inputPkt) } } // Test writing. output := new(bytes.Buffer) if err := ps.WritePacket(output, outputPkt); err != nil { t.Errorf("Unexpected error writing packet: %v\n %#v\v", err, outputPkt) } else { if err := te.Matches(expectedSerialization, output.Bytes()); err != nil { t.Errorf("Output of writing packet did not match: %v\n%#v", err, outputPkt) } } }
func TestMobSpawn(t *testing.T) { tests := []testCase{ { "pig", func(writer *bytes.Buffer) os.Error { m := NewPig().(*Pig) m.PointObject.Init(&types.AbsXyz{11, 70, -172}, &types.AbsVelocity{0, 0, 0}) m.Mob.EntityId = 0x1234 m.SetBurning(true) m.SetBurning(false) m.SetLook(types.LookDegrees{10, 20}) return m.SendSpawn(writer) }, te.InOrder( // packetIdEntitySpawn te.LiteralString("\x18"+ // Packet ID "\x00\x00\x12\x34"+ // EntityId "Z"+ // EntityMobType "\x00\x00\x01`\x00\x00\b\xc0\xff\xff\xea\x80"+ // X, Y, Z "\a\x0e", // Yaw, Pitch ), te.AnyOrder( te.LiteralString("\x00\x00"), // burning=false te.LiteralString("\x10\x00"), // 16=0 (?) ), te.LiteralString("\x7f"), // 127 = end of metadata // packetIdEntityVelocity te.LiteralString("\x1c\x00\x00\x12\x34\x00\x00\x00\x00\x00\x00"), ), }, { "creeper", func(writer *bytes.Buffer) os.Error { // Bogus position, changing below. m := NewCreeper().(*Creeper) m.PointObject.Init(&types.AbsXyz{11, 70, -172}, &types.AbsVelocity{}) m.Mob.EntityId = 0x5678 m.CreeperSetBlueAura() m.SetBurning(true) m.SetLook(types.LookDegrees{0, 199}) return m.SendSpawn(writer) }, te.InOrder( // packetIdEntitySpawn te.LiteralString("\x18"+ // Packet ID "\x00\x00\x56\x78"+ // EntityId "2"+ // EntityMobType "\x00\x00\x01\x60\x00\x00\x08\xc0\xff\xff\xea\x80"+ // X, Y, Z "\x00\x8d", // Yaw, Pitch ), te.AnyOrder( te.LiteralString("\x00\x01"), // burning=true te.LiteralString("\x10\xff"), // 16=255 (?) te.LiteralString("\x11\x01"), // blue aura=true ), te.LiteralString("\x7f"), // 127 = end of metadata // packetIdEntityVelocity te.LiteralString("\x1c\x00\x00\x56\x78\x00\x00\x00\x00\x00\x00"), ), }, } for _, x := range tests { buf := new(bytes.Buffer) want, err := x.want, x.result(buf) if err != nil { t.Errorf("Error when writing in test %s: %v", x.name, err) continue } result := buf.Bytes() if err = te.Matches(want, result); err != nil { t.Errorf("Resulting raw data mismatch for %s spawn: %v\nGot bytes: %x", x.name, err, result) } } }