aorecomp

Anarchy Online classic in the browser

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


server/stale_implant_move_test.go (5978B)


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