aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


server/player_flight_input_test.go (3644B)


      1 package main
      2 
      3 import (
      4 	"encoding/binary"
      5 	"fmt"
      6 	"testing"
      7 	"time"
      8 )
      9 
     10 func TestPlayerMovementRejectsEquipmentAndUnknownEvents(t *testing.T) {
     11 	for _, action := range []uint32{0, 27, 35, 127} {
     12 		t.Run(fmt.Sprint(action), func(t *testing.T) {
     13 			world := newPlayerFormWorld(t, 117322)
     14 			player, client := world.characters[1], world.clients[1]
     15 			observer := &combatTestConn{}
     16 			world.characters[2] = &character{id: 2, playfield: 705, health: 100}
     17 			world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, ready: true, target: true, conn: observer}
     18 			world.characters[1000000].playfield = 705
     19 			player.followTarget = 1000000
     20 			player.nextPositionSave = time.Time{}
     21 			position, rotation, movement, body := player.position, player.rotation, player.movement, player.body
     22 			request := playerMovementRequest(player, action)
     23 			binary.BigEndian.PutUint16(request[17:19], 7200)
     24 			client.conn.(*combatTestConn).packets = nil
     25 			auditCheckpointWrites(t, world)
     26 
     27 			if err := world.handleMovement(client, request); err != nil {
     28 				t.Fatal(err)
     29 			}
     30 
     31 			if player.position != position || player.rotation != rotation || player.movement != movement || player.body != body || player.followTarget != 1000000 || player.inventory.Items[0].Slot != 64 {
     32 				t.Fatal("unsupported events changed authoritative motion or ownership", player.position, player.movement, player.followTarget, player.inventory)
     33 			}
     34 			if len(observer.packets) != 0 || len(client.conn.(*combatTestConn).packets) != 0 || len(checkpointWrites(t, world)) != 0 {
     35 				t.Fatal("unsupported events reached another actor or persisted a forged pose", observer.packets, checkpointWrites(t, world))
     36 			}
     37 		})
     38 	}
     39 }
     40 
     41 func TestPlayerMovementProcessesValidInputAfterAnUnsupportedNativePacket(t *testing.T) {
     42 	world := newCheckpointWorld(t)
     43 	observer := &combatTestConn{}
     44 	player := world.characters[1]
     45 	position := player.position
     46 	world.characters[2] = &character{id: 2, playfield: 950, health: 100}
     47 	world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, ready: true, target: true, conn: observer}
     48 	forged := *player
     49 	forged.position[0] = 70
     50 	invalid := targetPacket(npcMovementPacket(&forged, 27))
     51 	valid := targetPacket(npcMovementPacket(player, 1))
     52 	ping := systemPacket(1, nil)
     53 	binary.BigEndian.PutUint16(ping[2:4], 11)
     54 	shutdown := startTestService(t, world)
     55 	connection := connectZoningClient(t, world, true)
     56 	callTestZone(t, world, 1, zoneTestCommand(func(*server) error {
     57 		observer.packets = nil
     58 		return nil
     59 	}))
     60 
     61 	for _, packet := range [][]byte{invalid, valid, ping} {
     62 		if _, err := connection.Write(packet); err != nil {
     63 			t.Fatal(err)
     64 		}
     65 	}
     66 	reply, err := readPacket(connection)
     67 	if err != nil {
     68 		t.Fatal(err)
     69 	}
     70 	var accepted bool
     71 	callTestZone(t, world, 1, zoneTestCommand(func(state *server) error {
     72 		accepted = state.characters[1].position[0] == position[0] && state.characters[1].movement.forward == 1
     73 		return nil
     74 	}))
     75 	if err := shutdown(); err != nil {
     76 		t.Fatal(err)
     77 	}
     78 	if binary.BigEndian.Uint16(reply[2:4]) != 11 || !accepted {
     79 		t.Fatal("rejected input interrupted the connection or its next valid control", reply, player.position, player.movement)
     80 	}
     81 	// Normal movement ticks can also publish height anchors before shutdown.
     82 	forward := 0
     83 	for _, packet := range observer.packets {
     84 		if binary.BigEndian.Uint32(packet[16:20]) != 0x54111123 {
     85 			continue
     86 		}
     87 		switch packet[29] {
     88 		case 1:
     89 			forward++
     90 		case 0x94:
     91 		default:
     92 			t.Fatal("native observer received an unsupported movement event", packet[29])
     93 		}
     94 	}
     95 	if forward != 1 {
     96 		t.Fatal("native observer lost or duplicated the valid forward input", forward)
     97 	}
     98 }