|
| 1 | +package packet |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + "go.minekube.com/gate/pkg/edition/java/proto/util" |
| 10 | + "go.minekube.com/gate/pkg/edition/java/proto/version" |
| 11 | + "go.minekube.com/gate/pkg/gate/proto" |
| 12 | +) |
| 13 | + |
| 14 | +// TestLoginPluginMessage_WireFormat verifies that LoginPluginMessage.Data is encoded |
| 15 | +// as raw remaining bytes (NOT length-prefixed), matching the Minecraft protocol spec |
| 16 | +// and consistent with LoginPluginResponse which already uses raw bytes. |
| 17 | +func TestLoginPluginMessage_WireFormat(t *testing.T) { |
| 18 | + msg := &LoginPluginMessage{ |
| 19 | + ID: 42, |
| 20 | + Channel: "velocity:player_info", |
| 21 | + Data: []byte{0x04}, // e.g. forwarding version 4 |
| 22 | + } |
| 23 | + |
| 24 | + ctx := &proto.PacketContext{ |
| 25 | + Direction: proto.ClientBound, |
| 26 | + Protocol: version.Minecraft_1_20_2.Protocol, |
| 27 | + } |
| 28 | + |
| 29 | + // Encode |
| 30 | + var buf bytes.Buffer |
| 31 | + require.NoError(t, msg.Encode(ctx, &buf)) |
| 32 | + encoded := buf.Bytes() |
| 33 | + |
| 34 | + // Manually build the expected wire format: |
| 35 | + // VarInt(42) + String("velocity:player_info") + raw byte 0x04 |
| 36 | + var expected bytes.Buffer |
| 37 | + util.PanicWriter(&expected).VarInt(42) |
| 38 | + util.PanicWriter(&expected).String("velocity:player_info") |
| 39 | + expected.WriteByte(0x04) // raw data, no length prefix |
| 40 | + assert.Equal(t, expected.Bytes(), encoded, |
| 41 | + "LoginPluginMessage data must be written as raw bytes, not length-prefixed") |
| 42 | + |
| 43 | + // Verify decode reads raw bytes (no length prefix) |
| 44 | + decoded := &LoginPluginMessage{} |
| 45 | + require.NoError(t, decoded.Decode(ctx, bytes.NewReader(encoded))) |
| 46 | + assert.Equal(t, 42, decoded.ID) |
| 47 | + assert.Equal(t, "velocity:player_info", decoded.Channel) |
| 48 | + assert.Equal(t, []byte{0x04}, decoded.Data, |
| 49 | + "Decoded data should be the raw remaining bytes") |
| 50 | +} |
| 51 | + |
| 52 | +// TestLoginPluginMessage_MultiByteData tests with larger data payloads. |
| 53 | +func TestLoginPluginMessage_MultiByteData(t *testing.T) { |
| 54 | + data := []byte("hello forge handshake data") |
| 55 | + original := &LoginPluginMessage{ |
| 56 | + ID: 1, |
| 57 | + Channel: "fml:loginwrapper", |
| 58 | + Data: data, |
| 59 | + } |
| 60 | + |
| 61 | + ctx := &proto.PacketContext{ |
| 62 | + Direction: proto.ClientBound, |
| 63 | + Protocol: version.Minecraft_1_20.Protocol, |
| 64 | + } |
| 65 | + |
| 66 | + var buf bytes.Buffer |
| 67 | + require.NoError(t, original.Encode(ctx, &buf)) |
| 68 | + |
| 69 | + // Build expected wire format manually |
| 70 | + var expected bytes.Buffer |
| 71 | + util.PanicWriter(&expected).VarInt(1) |
| 72 | + util.PanicWriter(&expected).String("fml:loginwrapper") |
| 73 | + expected.Write(data) // raw data |
| 74 | + assert.Equal(t, expected.Bytes(), buf.Bytes(), |
| 75 | + "Multi-byte data must be written as raw bytes") |
| 76 | + |
| 77 | + decoded := &LoginPluginMessage{} |
| 78 | + require.NoError(t, decoded.Decode(ctx, bytes.NewReader(buf.Bytes()))) |
| 79 | + assert.Equal(t, original.ID, decoded.ID) |
| 80 | + assert.Equal(t, original.Channel, decoded.Channel) |
| 81 | + assert.Equal(t, data, decoded.Data) |
| 82 | +} |
| 83 | + |
| 84 | +// TestLoginPluginMessage_EmptyData tests with no data payload. |
| 85 | +func TestLoginPluginMessage_EmptyData(t *testing.T) { |
| 86 | + original := &LoginPluginMessage{ |
| 87 | + ID: 5, |
| 88 | + Channel: "test:channel", |
| 89 | + Data: nil, |
| 90 | + } |
| 91 | + |
| 92 | + ctx := &proto.PacketContext{ |
| 93 | + Direction: proto.ClientBound, |
| 94 | + Protocol: version.Minecraft_1_20.Protocol, |
| 95 | + } |
| 96 | + |
| 97 | + var buf bytes.Buffer |
| 98 | + require.NoError(t, original.Encode(ctx, &buf)) |
| 99 | + |
| 100 | + decoded := &LoginPluginMessage{} |
| 101 | + require.NoError(t, decoded.Decode(ctx, &buf)) |
| 102 | + |
| 103 | + assert.Equal(t, original.ID, decoded.ID) |
| 104 | + assert.Equal(t, original.Channel, decoded.Channel) |
| 105 | + assert.Empty(t, decoded.Data) |
| 106 | +} |
0 commit comments