server/character_info_test.go (10527B)
1 package main 2 3 import ( 4 "bytes" 5 "encoding/binary" 6 "encoding/hex" 7 "fmt" 8 "testing" 9 ) 10 11 func TestCharacterInfoReturnsOnlyTheRequestedPlayerWithoutWrites(t *testing.T) { 12 world, connections := newTeamWorld(t) 13 world.characters[2].health = 15 14 auditCheckpointWrites(t, world) 15 16 for range 2 { 17 if err := world.handleCharacterInfo(world.clients[1], teamRequest(1, 105, 2, 0)); err != nil { 18 t.Fatal(err) 19 } 20 } 21 22 if len(connections[0].packets) != 2 || !bytes.Equal(connections[0].packets[0], connections[0].packets[1]) { 23 t.Fatal("repeated inspection did not return the same snapshot") 24 } 25 packet := connections[0].packets[0] 26 if len(packet) != 84 || binary.BigEndian.Uint32(packet[16:20]) != 0x4d38242e || 27 binary.BigEndian.Uint32(packet[12:16]) != 1 || binary.BigEndian.Uint32(packet[20:24]) != 50000 || binary.BigEndian.Uint32(packet[24:28]) != 2 { 28 t.Fatalf("native info envelope: %x", packet) 29 } 30 if got := hex.EncodeToString(packet[29:81]); got != "0001010102010000000000000000000f00000025000000030000001d000000030000001d00000000000000000000000000000000" { 31 t.Fatalf("native info fields: %s", got) 32 } 33 for _, connection := range connections[1:] { 34 if len(connection.packets) != 0 { 35 t.Fatal("inspection reached another player") 36 } 37 } 38 if writes := checkpointWrites(t, world); len(writes) != 0 { 39 t.Fatalf("inspection wrote durable state: %v", writes) 40 } 41 } 42 43 func TestCharacterInfoSelfUsesProfileStatsWithoutAbsorbingModifiers(t *testing.T) { 44 world, connections := newTeamWorld(t) 45 player := world.characters[1] 46 player.level, player.profession, player.appearance = 150, 12, 0x1a9 47 player.sitting = true 48 player.fighting = 2 49 50 err := world.handleCharacterInfo(world.clients[1], teamRequest(1, 105, 1, 0)) 51 52 if err != nil || len(connections[0].packets) != 1 { 53 t.Fatalf("self inspection: %v", err) 54 } 55 packet := connections[0].packets[0] 56 if !bytes.Equal(packet[29:35], []byte{0, 12, 1, 5, 3, 12}) || 57 binary.BigEndian.Uint32(packet[53:57]) != 59 || binary.BigEndian.Uint32(packet[61:65]) != 59 || len(world.clients[1].absorbedBonuses) != 0 { 58 t.Fatalf("self profile, fighting intervals or modifier basis: %x", packet) 59 } 60 } 61 62 func TestCharacterInfoRetainsVisitorTitleSeparatelyFromName(t *testing.T) { 63 world, connections := newTeamWorld(t) 64 world.characters[2].title = "Visitor" 65 world.characters[2].name = "Samplevisitor" 66 67 if err := world.handleCharacterInfo(world.clients[1], teamRequest(1, 105, 2, 0)); err != nil { 68 t.Fatal(err) 69 } 70 71 packet := connections[0].packets[0] 72 if !bytes.Equal(packet[73:88], []byte{0, 0, 0, 0, 0, 7, 'V', 'i', 's', 'i', 't', 'o', 'r', 0, 0}) || 73 world.characters[2].name != "Samplevisitor" { 74 t.Fatal("inspection lost the visitor title or changed the player name") 75 } 76 } 77 78 func TestCharacterInfoRejectsUnrepresentedOrUnauthenticatedRequests(t *testing.T) { 79 for _, scenario := range []string{"missing", "unseen npc", "unseen player", "other zone", "offline", "foreign owner", "sender type", "target type", "argument", "action", "key", "legacy", "not ready", "loading", "target not ready", "target loading", "replaced"} { 80 t.Run(scenario, func(t *testing.T) { 81 world, connections := newTeamWorld(t) 82 client := world.clients[1] 83 request := teamRequest(1, 105, 2, 0) 84 switch scenario { 85 case "unseen player": 86 delete(client.players, 2) 87 case "missing": 88 binary.BigEndian.PutUint32(request[24:28], 90) 89 case "unseen npc": 90 world.characters[2].npc = true 91 case "other zone": 92 world.characters[2].playfield = 705 93 case "offline": 94 delete(world.clients, 2) 95 case "foreign owner": 96 binary.BigEndian.PutUint32(request[8:12], 2) 97 case "sender type": 98 binary.BigEndian.PutUint32(request[4:8], 0) 99 case "target type": 100 binary.BigEndian.PutUint32(request[20:24], 51005) 101 case "argument": 102 binary.BigEndian.PutUint32(request[28:32], 1) 103 case "action": 104 binary.BigEndian.PutUint32(request[16:20], 106) 105 case "key": 106 binary.BigEndian.PutUint32(request, 0) 107 case "legacy": 108 client.target = false 109 case "not ready": 110 client.ready = false 111 case "loading": 112 client.loading = true 113 case "target not ready": 114 world.clients[2].ready = false 115 case "target loading": 116 world.clients[2].loading = true 117 case "replaced": 118 world.clients[1] = &worldClient{id: 1, ready: true} 119 } 120 121 err := world.handleCharacterInfo(client, request) 122 123 if err != nil || len(client.absorbedBonuses) != 0 { 124 t.Fatalf("invalid inspection changed observer state: %v", err) 125 } 126 for _, connection := range connections { 127 if len(connection.packets) != 0 { 128 t.Fatal("invalid inspection sent a packet") 129 } 130 } 131 }) 132 } 133 } 134 135 func TestCharacterInfoRejectsMalformedLengths(t *testing.T) { 136 world, connections := newTeamWorld(t) 137 request := teamRequest(1, 105, 2, 0) 138 139 for _, body := range [][]byte{nil, request[:35], append(request, 0)} { 140 if err := world.handleCharacterInfo(world.clients[1], body); err == nil { 141 t.Fatal("malformed inspection accepted") 142 } 143 } 144 145 if len(connections[0].packets) != 0 { 146 t.Fatal("malformed inspection sent a reply") 147 } 148 } 149 150 func TestCharacterInfoAbsorbsOnlyHealthBeforeEquipmentRemoval(t *testing.T) { 151 world, connections := newResourceBackpackTeam(t) 152 if granted, err := world.grantItem(world.characters[1], 152039); err != nil || !granted { 153 t.Fatal("Pioneer grant failed", err) 154 } 155 client := world.clients[2] 156 client.absorbedBonuses = map[uint32]resourceBonuses{1: {nano: 123}} 157 for _, connection := range connections { 158 connection.packets = nil 159 } 160 161 info := world.handleCharacterInfo(client, teamRequest(2, 105, 1, 0)) 162 remove := world.handleInventory(world.clients[1], itemMovePacket(1, 19, 111)[20:]) 163 world.advanceEquipment(world.clients[1].equipment.readyAt) 164 wear := world.handleInventory(world.clients[1], inventoryMovePacket(1, 64, 19)[20:]) 165 world.advanceEquipment(world.clients[1].equipment.readyAt) 166 167 if info != nil || remove != nil || wear != nil || client.absorbedBonuses[1] != (resourceBonuses{health: 90, nano: 123}) { 168 t.Fatalf("info modifier basis: %v %v %v %+v", info, remove, wear, client.absorbedBonuses) 169 } 170 packet := connections[1].packets[0] 171 if binary.BigEndian.Uint32(packet[45:49]) != 139 { 172 t.Fatalf("inspection did not report the effective maximum: %x", packet) 173 } 174 var maxima []uint32 175 for _, packet := range connections[1].packets[1:] { 176 if binary.BigEndian.Uint32(packet[16:20]) == 0x2b333d6e && binary.BigEndian.Uint32(packet[29:33]) == 1 { 177 maxima = append(maxima, binary.BigEndian.Uint32(packet[33:37])) 178 } 179 } 180 if len(maxima) != 1 || maxima[0] != 142 || world.characters[1].maximumHealth() != 52 { 181 t.Fatalf("observer must apply OnRemove once after the absorbed snapshot: %v", maxima) 182 } 183 } 184 185 func TestCharacterInfoNativeWireDispatch(t *testing.T) { 186 world := newCheckpointWorld(t) 187 startTestService(t, world) 188 client := connectZoningClient(t, world, true) 189 request := targetPacket(characterActionPacket(1, [5]uint32{105, 50000, 1, 0, 0})) 190 191 if _, err := client.Write(request); err != nil { 192 t.Fatal(err) 193 } 194 reply, err := readPacket(client) 195 196 if err != nil || len(reply) != 81 || binary.BigEndian.Uint32(reply[16:20]) != 0x4d38242e || binary.BigEndian.Uint32(reply[24:28]) != 1 { 197 t.Fatalf("native inspection dispatch: %v %x", err, reply) 198 } 199 } 200 201 func TestCharacterInfoNPCUsesNativeDefaultsWithoutChangingRecovery(t *testing.T) { 202 for _, fighting := range []uint32{0, 1} { 203 if !t.Run(fmt.Sprint(fighting), func(t *testing.T) { 204 world, conn := newRangedWorld(t) 205 npc := world.characters[1000000] 206 npc.health, npc.level, npc.fighting = 20, 200, fighting 207 npc.stats.Abilities.Stamina, npc.stats.Abilities.Psychic = 900, 900 208 auditCheckpointWrites(t, world) 209 210 for range 2 { 211 if err := world.handleCharacterInfo(world.clients[1], teamRequest(1, 105, 1000000, 0)); err != nil { 212 t.Fatal(err) 213 } 214 } 215 216 if len(conn.packets) != 2 || !bytes.Equal(conn.packets[0], conn.packets[1]) { 217 t.Fatal("represented NPC did not return a repeatable inspection reply") 218 } 219 packet := conn.packets[0] 220 if binary.BigEndian.Uint32(packet[24:28]) != 1000000 { 221 t.Fatal("NPC reply used the requester's identity") 222 } 223 if got := hex.EncodeToString(packet[29:81]); got != "00ff010101000000000000000000001400000064000000030000001e000000030000001e00000000000000000000000000000000" { 224 t.Fatal("NPC metadata or raw regeneration inputs changed", got) 225 } 226 if npc.health != 20 || npc.fighting != fighting || npc.regenerationSeconds != 0 || !npc.regenerationAt.IsZero() { 227 t.Fatal("inspection changed the NPC's health or recovery clock") 228 } 229 if writes := checkpointWrites(t, world); len(writes) != 0 { 230 t.Fatal("NPC inspection wrote durable state", writes) 231 } 232 }) { 233 return 234 } 235 } 236 } 237 238 func TestCharacterInfoNPCRejectsTargetsWithoutASimpleCharacter(t *testing.T) { 239 for _, scenario := range []string{"unseen", "other zone", "removed", "corpse", "foreign owner", "loading", "not ready", "replaced"} { 240 t.Run(scenario, func(t *testing.T) { 241 world, conn := newCombatWorld(t) 242 npc, client := world.characters[1000000], world.clients[1] 243 request := teamRequest(1, 105, 1000000, 0) 244 switch scenario { 245 case "unseen": 246 delete(client.npcs, npc.id) 247 case "other zone": 248 npc.playfield = 705 249 case "removed": 250 delete(world.characters, npc.id) 251 case "corpse": 252 npc.health, npc.corpse = 0, &npcCorpse{} 253 case "foreign owner": 254 binary.BigEndian.PutUint32(request[8:12], 2) 255 case "loading": 256 client.loading = true 257 case "not ready": 258 client.ready = false 259 case "replaced": 260 world.clients[1] = &worldClient{id: 1, ready: true} 261 } 262 263 err := world.handleCharacterInfo(client, request) 264 265 if err != nil || len(conn.packets) != 0 || len(client.absorbedBonuses) != 0 { 266 t.Fatal("an unrepresented NPC received an unsafe SimpleChar reply", err) 267 } 268 }) 269 } 270 } 271 272 func TestCharacterInfoNPCNativeWireDispatch(t *testing.T) { 273 world := newCheckpointWorld(t) 274 startTestService(t, world) 275 client := connectZoningClient(t, world, true) 276 request := targetPacket(characterActionPacket(1, [5]uint32{105, 50000, 1000000, 0, 0})) 277 278 if _, err := client.Write(request); err != nil { 279 t.Fatal(err) 280 } 281 reply, err := readPacket(client) 282 283 if err != nil || len(reply) != 81 || binary.BigEndian.Uint32(reply[16:20]) != 0x4d38242e || binary.BigEndian.Uint32(reply[24:28]) != 1000000 { 284 t.Fatalf("native NPC inspection dispatch: %v %x", err, reply) 285 } 286 if !bytes.Equal(reply[49:65], []byte{0, 0, 0, 3, 0, 0, 0, 30, 0, 0, 0, 3, 0, 0, 0, 30}) { 287 t.Fatal("native NPC inspection changed raw recovery inputs") 288 } 289 }