server/stale_armor_move_test.go (6150B)
1 package main 2 3 import ( 4 "bytes" 5 "encoding/binary" 6 "fmt" 7 "reflect" 8 "slices" 9 "testing" 10 ) 11 12 func TestStaleArmorMoveCorrectsItsEmptySource(t *testing.T) { 13 for _, source := range []uint32{16, 18, 21, 31} { 14 t.Run(fmt.Sprint(source), func(t *testing.T) { 15 world, output := newEquipmentWorld(t) 16 client := world.clients[1] 17 request := itemMovePacket(1, source, 111)[20:] 18 auditCheckpointWrites(t, world) 19 output.packets = nil 20 21 err := world.handleInventory(client, request) 22 23 if err != nil || len(output.packets) != 2 || len(checkpointWrites(t, world)) != 0 { 24 t.Fatal("empty worn armor source did not receive cancellation and clearing", err, len(output.packets)) 25 } 26 for index, action := range []uint32{146, 112} { 27 packet := output.packets[index] 28 if binary.BigEndian.Uint32(packet[29:33]) != action || binary.BigEndian.Uint32(packet[33:37]) != 102 || 29 binary.BigEndian.Uint32(packet[37:41]) != source { 30 t.Fatalf("stale armor correction addressed another row: %x", packet) 31 } 32 } 33 }) 34 } 35 } 36 37 func newStaleArmorMoveWorld(t *testing.T) (*server, *combatTestConn) { 38 t.Helper() 39 world, output := newEquipmentWorld(t) 40 player, client := world.characters[1], world.clients[1] 41 if granted, err := world.grantItem(player, 85557); err != nil || !granted { 42 t.Fatal(granted, err) 43 } 44 if err := world.handleInventory(client, inventoryMovePacket(1, 65, 18)[20:]); err != nil || client.equipment == nil { 45 t.Fatal("fixture helmet did not start wear", err) 46 } 47 world.advanceEquipment(client.equipment.readyAt) 48 49 // Complete a bank transfer while retaining the already published native armor. 50 inventory := player.inventory 51 index := slices.IndexFunc(inventory.Items, func(item ownedItem) bool { return item.Slot == 18 }) 52 helmet := inventory.Items[index] 53 helmet.Slot = 0 54 inventory.Bank = []ownedItem{helmet} 55 inventory.Items = slices.Delete(slices.Clone(inventory.Items), index, index+1) 56 if err := world.saveInventory(player, inventory); err != nil { 57 t.Fatal(err) 58 } 59 output.packets = nil 60 return world, output 61 } 62 63 func TestStaleArmorMoveUnwearsOnlyItsPublishedTemplateOnce(t *testing.T) { 64 for _, destination := range []uint32{64, 93, 111} { 65 t.Run(fmt.Sprint(destination), func(t *testing.T) { 66 world, output := newStaleArmorMoveWorld(t) 67 player, client := world.characters[1], world.clients[1] 68 inventory := player.inventory 69 before, err := readTestItemStore(world) 70 if err != nil { 71 t.Fatal(err) 72 } 73 auditCheckpointWrites(t, world) 74 75 for range 2 { 76 if err := world.handleInventory(client, itemMovePacket(1, 18, destination)[20:]); err != nil { 77 t.Fatal(err) 78 } 79 } 80 after, err := readTestItemStore(world) 81 cold := newTestInventoryServer(t) 82 83 if err != nil || !bytes.Equal(before, after) || len(checkpointWrites(t, world)) != 0 || 84 !reflect.DeepEqual(player.inventory, inventory) || !reflect.DeepEqual(cold.characters[1].inventory, inventory) || 85 world.nextItem != 2 || cold.nextItem != 2 || len(output.packets) != 10 || client.wornRemoval[18] != nil { 86 t.Fatal("stale armor correction changed ownership, allocation or repeated its unwear", err, len(output.packets)) 87 } 88 for index, action := range map[int]uint32{0: 146, 2: 112, 8: 146, 9: 112} { 89 packet := output.packets[index] 90 if binary.BigEndian.Uint32(packet[16:20]) != 0x5e477770 || binary.BigEndian.Uint32(packet[29:33]) != action || 91 binary.BigEndian.Uint32(packet[33:37]) != 102 || binary.BigEndian.Uint32(packet[37:41]) != 18 { 92 t.Fatalf("stale armor did not cancel/clear its own row: %x", packet) 93 } 94 } 95 unwear := output.packets[1] 96 words := []uint32{} 97 for offset := 29; offset < 65; offset += 4 { 98 words = append(words, binary.BigEndian.Uint32(unwear[offset:])) 99 } 100 if binary.BigEndian.Uint32(unwear[16:20]) != 0x35505644 || 101 !slices.Equal(words, []uint32{85557, 85557, 1, 1, 7, 102, 18, 0, 0}) { 102 t.Fatal("stale armor did not use its exact last-published removal contract", words) 103 } 104 }) 105 } 106 } 107 108 func TestStaleArmorMovePreservesInvalidAndPendingRequests(t *testing.T) { 109 for _, scenario := range []string{"foreign actor", "foreign target", "target type", "wrong page", "source before", "source after", "invalid destination", "pending destination"} { 110 t.Run(scenario, func(t *testing.T) { 111 world, output := newStaleArmorMoveWorld(t) 112 client := world.clients[1] 113 request := itemMovePacket(1, 18, 111)[20:] 114 switch scenario { 115 case "foreign actor": 116 binary.BigEndian.PutUint32(request[8:], 2) 117 case "foreign target": 118 binary.BigEndian.PutUint32(request[28:], 2) 119 case "target type": 120 binary.BigEndian.PutUint32(request[24:], 51017) 121 case "wrong page": 122 binary.BigEndian.PutUint32(request[16:], 103) 123 case "source before": 124 binary.BigEndian.PutUint32(request[20:], 15) 125 case "source after": 126 binary.BigEndian.PutUint32(request[20:], 32) 127 case "invalid destination": 128 binary.BigEndian.PutUint32(request[32:], 94) 129 case "pending destination": 130 player := world.characters[1] 131 inventory := player.inventory 132 item := inventory.Bank[0] 133 item.Slot = 65 134 inventory.Items = append(slices.Clone(inventory.Items), item) 135 inventory.Bank = nil 136 if err := world.saveInventory(player, inventory); err != nil { 137 t.Fatal(err) 138 } 139 if err := world.handleInventory(client, inventoryMovePacket(1, 65, 18)[20:]); err != nil || client.equipment == nil { 140 t.Fatal("fixture helmet did not start wear into the protected row", err) 141 } 142 } 143 pending := client.equipment 144 removal := slices.Clone(client.wornRemoval[18]) 145 before, err := readTestItemStore(world) 146 if err != nil { 147 t.Fatal(err) 148 } 149 output.packets = nil 150 auditCheckpointWrites(t, world) 151 152 err = world.handleInventory(client, request) 153 after, readErr := readTestItemStore(world) 154 155 if err != nil || readErr != nil || !bytes.Equal(before, after) || len(checkpointWrites(t, world)) != 0 || 156 len(output.packets) != 0 || !bytes.Equal(removal, client.wornRemoval[18]) || client.equipment != pending { 157 t.Fatal("invalid stale armor request changed ownership, pending wear or published metadata", err, readErr) 158 } 159 }) 160 } 161 }