aorecomp

Anarchy Online classic in the browser

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


server/stale_non_hand_move_test.go (6600B)


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