server/zone_movement_test.go (7368B)
1 package main 2 3 import ( 4 "bytes" 5 "encoding/binary" 6 "encoding/hex" 7 "reflect" 8 "testing" 9 "time" 10 ) 11 12 func zoneMovementRequest() []byte { 13 // Captured CharZoneMove at [160,14,130.625], with the owned handler prefix. 14 request, _ := hex.DecodeString("2d2b21690000c350000000010000000114050000e004150000114100003da1") 15 return request 16 } 17 18 func TestZoneMovementReplaysShopAndCheckpointsWithoutPerPacketWrites(t *testing.T) { 19 world := newCheckpointWorld(t) 20 player := world.characters[1] 21 player.position, player.rotation = [3]float32{60, 14, 54}, [4]float32{0, 0, 0, 1} 22 if err := world.savePosition(player); err != nil { 23 t.Fatal(err) 24 } 25 owner, observer := &combatTestConn{}, &combatTestConn{} 26 client := &worldClient{id: 1, target: true, ready: true, conn: owner} 27 world.clients[1] = client 28 world.characters[2] = &character{id: 2, playfield: 950, health: 100} 29 world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, target: true, ready: true, conn: observer} 30 now := time.Now() 31 player.nextPositionSave = now.Add(20 * time.Second) 32 before := world.nextItem 33 auditCheckpointWrites(t, world) 34 request := zoneMovementRequest() 35 36 for range 2 { 37 if err := world.handleMovement(client, request); err != nil { 38 t.Fatal(err) 39 } 40 } 41 if writes := checkpointWrites(t, world); len(writes) != 0 { 42 t.Fatal("scene transition rewrote durable state", writes) 43 } 44 if err := world.flushPositions(now.Add(20 * time.Second)); err != nil { 45 t.Fatal(err) 46 } 47 cold := newTestInventoryServer(t).characters[1] 48 49 if len(owner.packets) != 2 || len(observer.packets) != 2 || world.nextItem != before { 50 t.Fatal("scene replay duplicated instances or changed its recipients", len(owner.packets), len(observer.packets)) 51 } 52 for _, packet := range owner.packets { 53 if binary.BigEndian.Uint32(packet[16:20]) != 0x7f544905 || binary.BigEndian.Uint32(packet[20:24]) != 51035 || 54 binary.BigEndian.Uint32(packet[24:28]) != 3221226422 { 55 t.Fatal("scene transition omitted the authored starter vending identity") 56 } 57 } 58 if !bytes.Equal(owner.packets[0], owner.packets[1]) || !bytes.Equal(observer.packets[0][29:], request[16:]) || 59 binary.BigEndian.Uint32(observer.packets[0][16:20]) != 0x2d2b2169 || observer.packets[0][28] != 0 || len(observer.packets[0]) != 44 { 60 t.Fatal("native scene replay or compact pose relay changed its contract") 61 } 62 if player.position != [3]float32{160, 14, 130.625} || cold.position != player.position || cold.rotation != player.rotation || 63 !reflect.DeepEqual(cold.inventory, player.inventory) { 64 t.Fatal("scene transition lost its pose, inventory or checkpoint") 65 } 66 if writes := checkpointWrites(t, world); len(writes) != 1 || writes["characters/UPDATE"] != 1 { 67 t.Fatal("scene transition checkpoint wrote unexpected state", writes) 68 } 69 } 70 71 func TestZoneMovementRejectsInvalidMessagesBeforeSceneReplay(t *testing.T) { 72 for _, scenario := range []string{"short", "long", "foreign owner", "wrong type", "wrong pass-on", "wrong action", "correction", "zero rotation", "replaced", "loading", "unready", "dead", "unsupported client"} { 73 t.Run(scenario, func(t *testing.T) { 74 world, output := newCombatWorld(t) 75 player, client := world.characters[1], world.clients[1] 76 player.position, player.rotation = [3]float32{60, 14, 54}, [4]float32{0, 0, 0, 1} 77 request := zoneMovementRequest() 78 malformed := false 79 switch scenario { 80 case "short": 81 request = request[:30] 82 malformed = true 83 case "long": 84 request = append(request, 0) 85 malformed = true 86 case "foreign owner": 87 binary.BigEndian.PutUint32(request[8:12], 2) 88 case "wrong type": 89 binary.BigEndian.PutUint32(request[4:8], 51035) 90 case "wrong pass-on": 91 binary.BigEndian.PutUint32(request[12:16], 0) 92 case "wrong action": 93 request[16] = 9 94 case "correction": 95 request[16] |= 0x80 96 case "zero rotation": 97 clear(request[23:31]) 98 case "replaced": 99 world.clients[1] = &worldClient{id: 1} 100 case "loading": 101 client.loading = true 102 case "unready": 103 client.ready = false 104 case "dead": 105 player.health = 0 106 case "unsupported client": 107 client.target = false 108 } 109 110 err := world.handleMovement(client, request) 111 112 if (err != nil) != malformed || player.position != [3]float32{60, 14, 54} || player.rotation != [4]float32{0, 0, 0, 1} || 113 len(output.packets) != 0 || !player.movement.updatedAt.IsZero() { 114 t.Fatal("invalid scene transition changed pose or published a scene", err) 115 } 116 }) 117 } 118 } 119 120 func TestZoneMovementReplaysWorldResourcesWithoutAllocatingItems(t *testing.T) { 121 world := newWorldResourceWorld(t) 122 player := world.newCharacter() 123 if err := world.saveUser(player); err != nil { 124 t.Fatal(err) 125 } 126 if err := world.ensureWorldResources(950); err != nil { 127 t.Fatal(err) 128 } 129 output := &combatTestConn{} 130 client := &worldClient{id: 1, target: true, ready: true, conn: output} 131 world.clients[1] = client 132 before, err := readTestItemStore(world) 133 if err != nil { 134 t.Fatal(err) 135 } 136 137 for range 2 { 138 if err := world.handleMovement(client, zoneMovementRequest()); err != nil { 139 t.Fatal(err) 140 } 141 } 142 after, err := readTestItemStore(world) 143 144 if err != nil || !bytes.Equal(before, after) || world.nextItem != 1 || len(output.packets) != 4 { 145 t.Fatal("scene replay changed durable resource ownership or allocated another item", err) 146 } 147 for _, index := range []int{0, 2} { 148 packet := output.packets[index] 149 if binary.BigEndian.Uint32(packet[16:20]) != 0x3b11256f || binary.BigEndian.Uint32(packet[20:24]) != 51005 || 150 binary.BigEndian.Uint32(packet[24:28]) != 1 || !bytes.Equal(packet, output.packets[0]) { 151 t.Fatal("resource replay lost the original physical item") 152 } 153 } 154 } 155 156 func TestZoneMovementReplaysPrivatePassagesAndObjectiveOnly(t *testing.T) { 157 world, client, output := enteredObjectiveWorld(t) 158 mission := world.characters[1].missions[0] 159 request := zoneMovementRequest() 160 binary.BigEndian.PutUint16(request[17:19], 120) 161 binary.BigEndian.PutUint16(request[19:21], 0) 162 binary.BigEndian.PutUint16(request[21:23], 280) 163 before := world.nextItem 164 output.packets = nil 165 166 if err := world.handleMovement(client, request); err != nil { 167 t.Fatal(err) 168 } 169 170 if world.nextItem != before || len(output.packets) != 3 || 171 binary.BigEndian.Uint32(output.packets[0][16:20]) != 0x365a5071 || 172 binary.BigEndian.Uint32(output.packets[0][24:28]) != mission.doors[0].id || 173 binary.BigEndian.Uint32(output.packets[1][16:20]) != 0x4c7d403b || 174 binary.BigEndian.Uint32(output.packets[2][16:20]) != 0x3b11256f || 175 binary.BigEndian.Uint32(output.packets[2][24:28]) != mission.objective { 176 t.Fatal("private scene replay lost its passage/objective or included public machines", len(output.packets)) 177 } 178 } 179 180 func TestZoneMovementNativeSocketDispatchRestoresTheShop(t *testing.T) { 181 world := newCheckpointWorld(t) 182 startTestService(t, world) 183 connection := connectZoningClient(t, world, true) 184 defer connection.Close() 185 captured, _ := hex.DecodeString("dfdf000a0001002c00000005000000022d2b21690000c350000000010114050000e004150000114100003da1") 186 187 if _, err := connection.Write(captured); err != nil { 188 t.Fatal(err) 189 } 190 packet, err := readPacket(connection) 191 192 if err != nil || binary.BigEndian.Uint32(packet[16:20]) != 0x7f544905 || 193 binary.BigEndian.Uint32(packet[20:24]) != 51035 || binary.BigEndian.Uint32(packet[24:28]) != 3221226422 { 194 t.Fatal("native scene transition did not dispatch a vending replay", err) 195 } 196 }