server/mission_boundaries_test.go (18248B)
1 package main 2 3 import ( 4 "bytes" 5 "encoding/binary" 6 "fmt" 7 "reflect" 8 "testing" 9 "time" 10 ) 11 12 func newMissionBoundaryWorld(t *testing.T) (*server, [3]*combatTestConn) { 13 t.Helper() 14 world, owner := newMissionEncounterOfferWorld(t) 15 outputs := [3]*combatTestConn{owner, {}, {}} 16 for _, id := range []uint32{2, 3} { 17 data := world.characters[1].userData() 18 data.ID, data.Name = id, fmt.Sprintf("Mission player %d", id) 19 world.characters[id] = data.character() 20 world.clients[id] = &worldClient{id: id, target: true, ready: true, conn: outputs[id-1], npcs: make(map[uint32]bool)} 21 if err := world.saveUser(world.characters[id]); err != nil { 22 t.Fatal(err) 23 } 24 if err := world.saveInventory(world.characters[id], playerInventory{Credits: 5}); err != nil { 25 t.Fatal(err) 26 } 27 } 28 request := missionOffersRequest() 29 binary.BigEndian.PutUint32(request[8:12], 2) 30 if err := world.handleMissionOffers(world.clients[2], request); err != nil { 31 t.Fatal(err) 32 } 33 for _, id := range []uint32{1, 2} { 34 request := missionAcceptanceRequest(*world.clients[id].missionOffer) 35 binary.BigEndian.PutUint32(request[8:12], id) 36 if err := world.handleMissionAcceptance(world.clients[id], request); err != nil { 37 t.Fatal(err) 38 } 39 } 40 41 // A copied key admits the guest to the first owner's world, not the second's. 42 if granted, err := world.grantItem(world.characters[3], 28577); err != nil || !granted { 43 t.Fatal("copy mission key", granted, err) 44 } 45 if _, err := world.db.Exec("INSERT INTO mission_keys VALUES (?,?)", world.nextItem, world.characters[1].missions[0].id); err != nil { 46 t.Fatal(err) 47 } 48 for _, id := range []uint32{1, 2, 3} { 49 client, player := world.clients[id], world.characters[id] 50 player.geometry = world.geometry 51 player.position, player.rotation = [3]float32{89.30216, 115.75, 152.5}, [4]float32{0, 1, 0, 0} 52 if err := world.handleMovement(client, playerMovementRequest(player, 1)); err != nil { 53 t.Fatal(err) 54 } 55 expected := uint32(65536) 56 if id == 2 { 57 expected = 65537 58 } 59 if player.playfield != expected { 60 t.Fatal("keyed entry selected a different assignment", id, player.playfield) 61 } 62 world.clients[id], client.ready, client.loading = client, true, false 63 player.position, player.rotation = [3]float32{15, .011, 43}, [4]float32{0, 0, 0, 1} 64 player.movement = playerMovement{} 65 if err := world.savePosition(player); err != nil { 66 t.Fatal(err) 67 } 68 world.updateNPCInterest(client, time.Now()) 69 } 70 world.clients[1].players = map[uint32]bool{3: true} 71 world.clients[3].players = map[uint32]bool{1: true} 72 for _, output := range outputs { 73 output.packets = nil 74 } 75 return world, outputs 76 } 77 78 func TestMissionBoundariesInspectOnlyRepresentedActorsInTheSameWorld(t *testing.T) { 79 world, outputs := newMissionBoundaryWorld(t) 80 client := world.clients[1] 81 client.npcs[2000006] = true // A stale interest entry must not bypass the world check. 82 83 for _, id := range []uint32{2, 2000006, 3, 2000001} { 84 if err := world.handleCharacterInfo(client, teamRequest(1, 105, id, 0)); err != nil { 85 t.Fatal(err) 86 } 87 } 88 89 if len(outputs[0].packets) != 2 || len(outputs[1].packets) != 0 || len(outputs[2].packets) != 0 || 90 binary.BigEndian.Uint32(outputs[0].packets[0][24:28]) != 3 || binary.BigEndian.Uint32(outputs[0].packets[1][24:28]) != 2000001 || 91 len(client.absorbedBonuses) != 2 { 92 t.Fatal("inspection crossed a runtime world or failed for its guest", outputs[0].packets, client.absorbedBonuses) 93 } 94 cold := reloadMissionEntranceWorld(t) 95 if cold.characters[1].playfield != 65536 || cold.characters[2].playfield != 65537 || cold.characters[3].playfield != 65536 { 96 t.Fatal("cold loading merged owner and guest worlds") 97 } 98 } 99 100 func TestMissionBoundariesNanosRejectForeignWorldAndRefundDepartedTargets(t *testing.T) { 101 for _, friendly := range []bool{false, true} { 102 t.Run(map[bool]string{false: "harmful", true: "friendly"}[friendly], func(t *testing.T) { 103 world, outputs := newMissionBoundaryWorld(t) 104 player, client := world.characters[1], world.clients[1] 105 player.profession, player.nano = 8, 100 106 player.stats.Body.NanoPool = 200 107 player.stats.NanoAiding.PsychologicalModification, player.stats.NanoAiding.MatterCreation = 7, 6 108 player.stats.NanoAiding.MatterMetamorphosis, player.stats.NanoAiding.BiologicalMetamorphosis = 39, 33 109 player.inventory.LearnedNanos = []uint32{29618, 27169} 110 if err := world.saveUser(player); err != nil { 111 t.Fatal(err) 112 } 113 program, foreign, local := uint32(29618), uint32(2000006), uint32(2000001) 114 if friendly { 115 program, foreign, local = 27169, 2, 3 116 } 117 client.npcs[foreign] = true 118 before := world.characters[foreign].health 119 120 if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, foreign, 53019, program})[20:]); err != nil { 121 t.Fatal(err) 122 } 123 124 if client.nanoCast != nil || player.nano != 100 || player.nanoReserved != 0 || world.characters[foreign].health != before || 125 len(world.characters[foreign].activeNanos) != 0 || len(outputs[1].packets) != 0 || len(outputs[2].packets) != 0 { 126 t.Fatal("foreign-world cast changed resources, effects or observer state") 127 } 128 if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, local, 53019, program})[20:]); err != nil { 129 t.Fatal(err) 130 } 131 if client.nanoCast == nil || player.nanoReserved == 0 { 132 t.Fatal("same-world cast was not admitted") 133 } 134 deadline := client.nanoCast.readyAt.Add(time.Nanosecond) 135 target := world.characters[local] 136 health := target.health 137 target.playfield = 65537 138 world.advanceNanos(deadline) 139 world.advanceNanos(deadline.Add(time.Second)) 140 cold := reloadMissionEntranceWorld(t) 141 if client.nanoCast != nil || player.nano != 100 || player.nanoReserved != 0 || !player.nanoRechargeUntil.IsZero() || 142 cold.characters[1].nano != 100 || cold.characters[1].nanoReserved != 0 || target.health != health || len(target.activeNanos) != 0 || 143 len(outputs[1].packets) != 0 { 144 t.Fatal("departed cast target received an outcome or refund was not durable") 145 } 146 target.playfield = 65536 147 outputs[0].packets, outputs[2].packets = nil, nil 148 if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, local, 53019, program})[20:]); err != nil { 149 t.Fatal(err) 150 } 151 if client.nanoCast == nil { 152 t.Fatal("same-world retry was not admitted") 153 } 154 world.advanceNanos(client.nanoCast.readyAt.Add(time.Nanosecond)) 155 if player.nano >= 100 || len(outputs[1].packets) != 0 || len(outputs[2].packets) == 0 || 156 friendly && len(target.activeNanos) != 1 || !friendly && target.health >= 20 { 157 t.Fatal("clear same-world retry failed or broadcast into the other instance") 158 } 159 }) 160 } 161 } 162 163 func TestMissionBoundariesTradeAdmissionAndDepartureConserveOwnership(t *testing.T) { 164 world, outputs := newMissionBoundaryWorld(t) 165 before, err := readTestItemStore(world) 166 if err != nil { 167 t.Fatal(err) 168 } 169 170 playerTradeRequest(t, world, 1, 0, [2]uint32{50000, 2}, [2]uint32{}) 171 after, err := readTestItemStore(world) 172 173 if err != nil || !bytes.Equal(before, after) || world.clients[1].trade != nil || world.clients[2].trade != nil || 174 len(outputs[0].packets) != 0 || len(outputs[1].packets) != 0 || len(outputs[2].packets) != 0 { 175 t.Fatal("foreign-world trade changed ownership or opened a window", err) 176 } 177 playerTradeRequest(t, world, 1, 0, [2]uint32{50000, 3}, [2]uint32{}) 178 playerTradeRequest(t, world, 1, 4, [2]uint32{50000, 1}, [2]uint32{104, 64}) 179 playerTradeRequest(t, world, 1, 1, [2]uint32{}, [2]uint32{}) 180 world.characters[3].playfield = 65537 181 playerTradeRequest(t, world, 3, 1, [2]uint32{}, [2]uint32{}) 182 cold := reloadMissionEntranceWorld(t) 183 if world.clients[1].trade != nil || world.clients[3].trade != nil || len(outputs[1].packets) != 0 || 184 !reflect.DeepEqual(cold.characters[1].inventory, world.characters[1].inventory) || 185 !reflect.DeepEqual(cold.characters[3].inventory, world.characters[3].inventory) || 186 world.characters[1].inventory.Items[0].ID != 1 || world.characters[1].inventory.Items[0].Slot != 64 || len(world.characters[3].inventory.Items) != 1 { 187 t.Fatal("late trade acceptance transferred an item across worlds or lost the restored axe") 188 } 189 } 190 191 func TestMissionBoundariesVicinityAndHistoryStayInsideTheRuntimeWorld(t *testing.T) { 192 for kind := range uint32(4) { 193 t.Run(fmt.Sprint(kind), func(t *testing.T) { 194 world, _ := newMissionBoundaryWorld(t) 195 outputs := [3]*chatRecordingConn{{}, {}, {}} 196 setChatClients(t, world, map[uint32]*chatClient{1: {id: 1, conn: outputs[0]}, 2: {id: 2, conn: outputs[1]}, 3: {id: 3, conn: outputs[2]}}) 197 198 world.sendVicinity(world.clients[1], kind, "private mission message", nil) 199 200 if len(outputs[0].packets) != 2 || len(outputs[1].packets) != 0 || len(outputs[2].packets) != 2 || 201 !bytes.Equal(outputs[0].packets[1], outputs[2].packets[1]) || binary.BigEndian.Uint16(outputs[0].packets[1]) != 34 { 202 t.Fatal("vicinity delivery crossed a runtime world or omitted its guest") 203 } 204 cold := reloadMissionEntranceWorld(t) 205 for _, id := range []uint32{1, 2, 3} { 206 history, err := cold.chatHistory(id) 207 if err != nil { 208 t.Fatal(err) 209 } 210 if id == 2 { 211 if len(history) != 0 { 212 t.Fatal("private vicinity history leaked after restart", history) 213 } 214 } else if len(history) != 1 || history[0].Playfield != 65536 || history[0].Message != "private mission message" { 215 t.Fatal("same-world chat history lost its exact runtime world", id, history) 216 } 217 } 218 }) 219 } 220 } 221 222 func TestMissionBoundariesCombatAndLootCannotReachAnotherWorld(t *testing.T) { 223 world, outputs := newMissionBoundaryWorld(t) 224 player, client, npc := world.characters[1], world.clients[1], world.characters[2000001] 225 profile := *npc.combatProfile 226 profile.Loot = profile.Loot[:1] 227 npc.combatProfile = &profile 228 client.npcs[2000006] = true 229 world.clients[2].npcs[npc.id] = true 230 world.combatRoll = func(uint32) uint32 { return 0 } 231 232 if err := world.handleCombat(client, attackPacket(1, 2000006)[20:]); err != nil { 233 t.Fatal(err) 234 } 235 236 if player.fighting != 0 || world.characters[2000006].health != 20 || len(world.fighting) != 0 || len(outputs[1].packets) != 0 || len(outputs[2].packets) != 0 { 237 t.Fatal("forged known-actor attack crossed the runtime world") 238 } 239 npc.health = 1 240 if err := world.handleCombat(client, attackPacket(1, npc.id)[20:]); err != nil { 241 t.Fatal(err) 242 } 243 world.advanceCombat(player.nextAttack) 244 if npc.corpse == nil || npc.corpse.looter != 1 || len(npc.corpse.items) != 1 || player.experience != 100 || len(outputs[1].packets) != 0 || len(outputs[2].packets) == 0 { 245 t.Fatal("same-world kill did not isolate damage, rewards and corpse creation") 246 } 247 item := npc.corpse.items[0].ID 248 request := lootUseRequest(1) 249 binary.BigEndian.PutUint32(request[44:48], npc.id) 250 player.playfield = 65537 251 if err := world.handleLootUse(client, request); err != nil { 252 t.Fatal(err) 253 } 254 if client.lootCorpse != 0 || npc.corpse.credits != 1 || len(npc.corpse.items) != 1 || player.inventory.Credits != 4 { 255 t.Fatal("entitled player collected a corpse from a different world") 256 } 257 player.playfield = 65536 258 binary.BigEndian.PutUint32(request[20:24], 2) 259 if err := world.handleLootUse(client, request); err != nil { 260 t.Fatal(err) 261 } 262 if client.lootCorpse != npc.id || npc.corpse.credits != 0 || player.inventory.Credits != 5 { 263 t.Fatal("same-world corpse opening failed to collect its cash") 264 } 265 player.playfield = 65537 266 if err := world.handleInventory(client, lootTakeRequest()); err != nil { 267 t.Fatal(err) 268 } 269 if client.lootCorpse != 0 || len(npc.corpse.items) != 1 || world.items[item].Template != 42640 { 270 t.Fatal("stale loot window transferred its item across worlds or remained open") 271 } 272 273 // Reopen the closed source before retrying from its owning world. 274 player.playfield = 65536 275 binary.BigEndian.PutUint32(request[20:24], 3) 276 if err := world.handleLootUse(client, request); err != nil { 277 t.Fatal(err) 278 } 279 if client.lootCorpse != npc.id || npc.corpse.credits != 0 || player.inventory.Credits != 5 { 280 t.Fatal("same-world corpse reopening failed or paid its cash twice") 281 } 282 if err := world.handleInventory(client, lootTakeRequest()); err != nil { 283 t.Fatal(err) 284 } 285 cold := reloadMissionEntranceWorld(t) 286 if len(npc.corpse.items) != 0 || len(cold.characters[1].inventory.Items) != 3 || cold.characters[1].inventory.Items[2].ID != item || 287 cold.characters[1].experience != 100 || cold.characters[1].inventory.Credits != 5 || cold.characters[2].experience != 0 || 288 len(cold.characters[2].inventory.Items) != 1 || len(outputs[1].packets) != 0 { 289 t.Fatalf("same-world pickup: corpse=%v inventory=%v xp=%d cash=%d foreignXP=%d foreignItems=%v foreignPackets=%x", npc.corpse.items, cold.characters[1].inventory.Items, cold.characters[1].experience, cold.characters[1].inventory.Credits, cold.characters[2].experience, cold.characters[2].inventory.Items, outputs[1].packets) 290 } 291 } 292 293 func TestMissionBoundariesInterestMovementAndObjectivePickupIncludeOnlyGuests(t *testing.T) { 294 world, outputs := newMissionBoundaryWorld(t) 295 first, second := world.characters[1].missions[0], world.characters[2].missions[0] 296 objective := first.objective 297 for _, id := range []uint32{1, 2, 3} { 298 local, foreign := uint32(2000001), uint32(2000006) 299 if id == 2 { 300 local, foreign = foreign, local 301 } 302 if !world.clients[id].npcs[local] || world.clients[id].npcs[foreign] { 303 t.Fatal("initial interest merged identical-coordinate populations", id, world.clients[id].npcs) 304 } 305 } 306 307 if err := world.handleMovement(world.clients[1], playerMovementRequest(world.characters[1], 9)); err != nil { 308 t.Fatal(err) 309 } 310 if err := world.handleInventory(world.clients[2], worldPickupRequest(2, objective)); err != nil { 311 t.Fatal(err) 312 } 313 314 if len(outputs[2].packets) != 1 || binary.BigEndian.Uint32(outputs[2].packets[0][24:28]) != 1 || 315 len(outputs[1].packets) != 1 || binary.BigEndian.Uint32(outputs[1].packets[0][29:33]) != 146 || 316 world.worldItems[objective].mission != first.id || len(world.characters[2].inventory.Items) != 1 { 317 t.Fatal("movement or objective ownership crossed the world boundary") 318 } 319 for _, output := range outputs { 320 output.packets = nil 321 } 322 world.characters[3].position = world.worldItems[objective].position 323 if err := world.handleInventory(world.clients[3], worldPickupRequest(3, objective)); err != nil { 324 t.Fatal(err) 325 } 326 cold := reloadMissionEntranceWorld(t) 327 if len(outputs[0].packets) != 1 || binary.BigEndian.Uint32(outputs[0].packets[0][24:28]) != objective || len(outputs[1].packets) != 0 || 328 len(outputs[2].packets) != 2 || binary.BigEndian.Uint32(outputs[2].packets[0][16:20]) != 0x47537a24 || 329 binary.BigEndian.Uint32(outputs[2].packets[1][16:20]) != 0x36510078 || 330 len(cold.characters[3].inventory.Items) != 2 || cold.characters[3].inventory.Items[1].ID != objective || 331 cold.worldItems[second.objective].mission != second.id || len(cold.worldItems) != 1 { 332 t.Fatal("guest pickup lost the exact objective or removed another world's item") 333 } 334 } 335 336 func TestMissionBoundariesGuestTradeTransfersTheSameInstanceOnce(t *testing.T) { 337 world, outputs := newMissionBoundaryWorld(t) 338 playerTradeRequest(t, world, 1, 0, [2]uint32{50000, 3}, [2]uint32{}) 339 playerTradeRequest(t, world, 1, 4, [2]uint32{50000, 1}, [2]uint32{104, 64}) 340 341 playerTradeRequest(t, world, 1, 1, [2]uint32{}, [2]uint32{}) 342 playerTradeRequest(t, world, 3, 1, [2]uint32{}, [2]uint32{}) 343 playerTradeRequest(t, world, 3, 1, [2]uint32{}, [2]uint32{}) 344 cold := reloadMissionEntranceWorld(t) 345 346 if world.clients[1].trade != nil || world.clients[3].trade != nil || len(outputs[1].packets) != 0 || len(cold.characters[1].inventory.Items) != 1 || 347 cold.characters[1].inventory.Items[0].ID != 2 || len(cold.characters[3].inventory.Items) != 2 || cold.characters[3].inventory.Items[0].ID != 1 || 348 cold.characters[3].inventory.Items[0].Slot != 65 || cold.characters[1].inventory.Credits != 4 || cold.characters[3].inventory.Credits != 5 { 349 t.Fatal("same-world guest trade lost an instance or repeated settlement", cold.characters[1].inventory, cold.characters[3].inventory) 350 } 351 } 352 353 func TestMissionBoundariesRetirementEvacuatesGuestsAndPreservesTheOtherWorld(t *testing.T) { 354 world, outputs := newMissionBoundaryWorld(t) 355 first, second := world.characters[1].missions[0], world.characters[2].missions[0] 356 otherItems := world.characters[2].inventory.Items 357 358 if err := world.handleMissionRemoval(world.clients[1], missionRemovalPacket(1, first.id)[20:]); err != nil { 359 t.Fatal(err) 360 } 361 if err := world.flushWorld(); err != nil { 362 t.Fatal(err) 363 } 364 cold := reloadMissionEntranceWorld(t) 365 366 if cold.characters[1].playfield != 3000 || cold.characters[3].playfield != 3000 || cold.characters[2].playfield != 65537 || 367 len(cold.characters[1].missions) != 0 || len(cold.characters[3].inventory.Items) != 0 || len(cold.characters[2].missions) != 1 || 368 !reflect.DeepEqual(cold.characters[2].inventory.Items, otherItems) || cold.characters[2].inventory.Credits != 4 || len(outputs[1].packets) != 0 || 369 len(cold.missionExpiry) != 1 || cold.missionExpiry[0].id != second.id || cold.missionExpiry[0].doors[0].id != 2 || 370 len(cold.worldItems) != 1 || cold.worldItems[second.objective].mission != second.id || 371 world.geometry.mission(65536) != nil || world.geometry.mission(65537) == nil { 372 t.Fatal("retirement revoked another assignment or a checkpoint restored the retired world") 373 } 374 for id := uint32(2000000); id < 2000010; id++ { 375 if id < 2000005 { 376 if world.characters[id] != nil || cold.characters[id] != nil { 377 t.Fatal("retired mission actor survived a checkpoint", id) 378 } 379 } else if cold.characters[id] == nil || cold.characters[id].health != 20 || cold.characters[id].playfield != 65537 { 380 t.Fatal("another world's original population changed", id) 381 } 382 } 383 } 384 385 // Chat uses a different stream header from world packets. 386 type chatRecordingConn struct{ combatTestConn } 387 388 func (conn *chatRecordingConn) Write(packet []byte) (int, error) { 389 conn.pending = append(conn.pending, packet...) 390 for len(conn.pending) >= 4 { 391 length := int(binary.BigEndian.Uint16(conn.pending[2:4])) + 4 392 if len(conn.pending) < length { 393 break 394 } 395 conn.packets = append(conn.packets, append([]byte(nil), conn.pending[:length]...)) 396 conn.pending = conn.pending[length:] 397 } 398 return len(packet), nil 399 }