aorecomp

Anarchy Online classic in the browser

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


server/inventory_move_test.go (7237B)


      1 package main
      2 
      3 import (
      4 	"reflect"
      5 	"sync"
      6 	"testing"
      7 	"time"
      8 )
      9 
     10 func TestInventoryMoveDoesNotCheckpointNPCs(t *testing.T) {
     11 	world, _ := newInventoryWriteWorld(t)
     12 	for id, npc := range world.characters {
     13 		if npc.npc {
     14 			npc.position[0]++
     15 			saved := world.savedNPCs[id]
     16 			saved.NextSave = time.Time{}
     17 			world.savedNPCs[id] = saved
     18 		}
     19 	}
     20 	auditCheckpointWrites(t, world)
     21 
     22 	err := world.handleInventory(world.clients[1], inventoryMovePacket(1, 64, 65)[20:])
     23 
     24 	if err != nil {
     25 		t.Fatal(err)
     26 	}
     27 	if writes := checkpointWrites(t, world); !reflect.DeepEqual(writes, map[string]int{"item_locations/DELETE": 1, "item_locations/INSERT": 1}) {
     28 		t.Fatalf("item move checkpointed unrelated actors: %v", writes)
     29 	}
     30 }
     31 
     32 func TestInventoryMoveRejectsOccupiedDurableDestination(t *testing.T) {
     33 	world, conn := newInventoryWriteWorld(t)
     34 	if granted, err := world.grantItem(world.characters[1], 121567); err != nil || !granted {
     35 		t.Fatal(granted, err)
     36 	}
     37 	if _, err := world.db.Exec("UPDATE item_locations SET slot=66 WHERE item_id=2"); err != nil {
     38 		t.Fatal(err)
     39 	}
     40 
     41 	err := world.handleInventory(world.clients[1], inventoryMovePacket(1, 64, 66)[20:])
     42 
     43 	var slot uint32
     44 	if queryErr := world.db.QueryRow("SELECT slot FROM item_locations WHERE item_id=1").Scan(&slot); queryErr != nil {
     45 		t.Fatal(queryErr)
     46 	}
     47 	if err == nil || slot != 64 || world.characters[1].inventory.Items[0].Slot != 64 || len(conn.packets) != 0 {
     48 		t.Fatal("occupied destination did not roll back source removal", err, slot)
     49 	}
     50 }
     51 
     52 func TestInventoryMoveCompetingDatabaseConnections(t *testing.T) {
     53 	first, firstConn := newInventoryWriteWorld(t)
     54 	second := newTestInventoryServer(t)
     55 	secondConn := &combatTestConn{}
     56 	second.clients[1] = &worldClient{id: 1, conn: secondConn, target: true, ready: true}
     57 	start := make(chan struct{})
     58 	var errors [2]error
     59 	var done sync.WaitGroup
     60 
     61 	// Separate fixtures share SQLite and retain independent inventory caches.
     62 	for index, world := range []*server{first, second} {
     63 		done.Add(1)
     64 		go func() {
     65 			defer done.Done()
     66 			<-start
     67 			errors[index] = world.handleInventory(world.clients[1], inventoryMovePacket(1, 64, uint32(65+index))[20:])
     68 		}()
     69 	}
     70 	close(start)
     71 	done.Wait()
     72 
     73 	if (errors[0] == nil) == (errors[1] == nil) || len(firstConn.packets)+len(secondConn.packets) != 1 {
     74 		t.Fatal("SQLite did not select exactly one successful move", errors)
     75 	}
     76 	var slot uint32
     77 	if err := first.db.QueryRow("SELECT slot FROM item_locations WHERE item_id=1").Scan(&slot); err != nil {
     78 		t.Fatal(err)
     79 	}
     80 	if errors[0] == nil && slot != 65 || errors[1] == nil && slot != 66 {
     81 		t.Fatal("durable destination differs from the successful move", slot, errors)
     82 	}
     83 }
     84 
     85 func TestBackpackMoveChecksDurableContainerOwnership(t *testing.T) {
     86 	for _, direction := range []string{"deposit", "withdrawal"} {
     87 		t.Run(direction, func(t *testing.T) {
     88 			world, conn := newBackpackWorld(t)
     89 			client := world.clients[1]
     90 			if err := world.handleOwnedItemUse(client, backpackUseRequest(1, 66, 1)[20:]); err != nil {
     91 				t.Fatal(err)
     92 			}
     93 			request := backpackMoveRequest(104, 68, 51017, 3, 111)
     94 			if direction == "withdrawal" {
     95 				if err := world.handleInventory(client, request[20:]); err != nil {
     96 					t.Fatal(err)
     97 				}
     98 				request = backpackMoveRequest(107, 1<<16, 50000, 1, 93)
     99 			}
    100 			if _, err := world.db.Exec("UPDATE item_locations SET player_id=NULL, bank_player_id=1, slot=0 WHERE item_id=3"); err != nil {
    101 				t.Fatal(err)
    102 			}
    103 			conn.packets = nil
    104 			before, err := readTestItemStore(world)
    105 			if err != nil {
    106 				t.Fatal(err)
    107 			}
    108 			inventory := world.characters[1].inventory
    109 
    110 			err = world.handleInventory(client, request[20:])
    111 
    112 			if err == nil || len(conn.packets) != 0 || !reflect.DeepEqual(inventory, world.characters[1].inventory) {
    113 				t.Fatal("move used a backpack that is no longer carried", err)
    114 			}
    115 			after, err := readTestItemStore(world)
    116 			if err != nil || !reflect.DeepEqual(before, after) {
    117 				t.Fatal("refused backpack move changed durable ownership", err)
    118 			}
    119 		})
    120 	}
    121 }
    122 
    123 func TestInventoryMoveFailedCommitRetainsSource(t *testing.T) {
    124 	world, conn := newInventoryWriteWorld(t)
    125 	if _, err := world.db.Exec(`CREATE TABLE move_commit_failure (
    126 		player_id INTEGER REFERENCES players(id) DEFERRABLE INITIALLY DEFERRED);
    127 		CREATE TRIGGER reject_move_commit AFTER INSERT ON item_locations
    128 		BEGIN INSERT INTO move_commit_failure VALUES (99); END;`); err != nil {
    129 		t.Fatal(err)
    130 	}
    131 
    132 	err := world.handleInventory(world.clients[1], inventoryMovePacket(1, 64, 65)[20:])
    133 
    134 	var slot uint32
    135 	if queryErr := world.db.QueryRow("SELECT slot FROM item_locations WHERE item_id=1").Scan(&slot); queryErr != nil {
    136 		t.Fatal(queryErr)
    137 	}
    138 	if err == nil || slot != 64 || world.characters[1].inventory.Items[0].Slot != 64 || len(conn.packets) != 0 {
    139 		t.Fatal("failed commit lost the source or published a move", err, slot)
    140 	}
    141 }
    142 
    143 func TestInventoryMoveChecksDurableSource(t *testing.T) {
    144 	for _, change := range []string{
    145 		"UPDATE item_locations SET slot=66 WHERE item_id=1",
    146 		"UPDATE item_locations SET player_id=2 WHERE item_id=1",
    147 	} {
    148 		t.Run(change, func(t *testing.T) {
    149 			world, conn := newInventoryWriteWorld(t)
    150 			if _, err := world.db.Exec(change); err != nil {
    151 				t.Fatal(err)
    152 			}
    153 			before, err := readTestItemStore(world)
    154 			if err != nil {
    155 				t.Fatal(err)
    156 			}
    157 
    158 			err = world.handleInventory(world.clients[1], inventoryMovePacket(1, 64, 65)[20:])
    159 
    160 			if err == nil || world.characters[1].inventory.Items[0].Slot != 64 || len(conn.packets) != 0 {
    161 				t.Fatal("stale source was published as a successful move", err)
    162 			}
    163 			after, err := readTestItemStore(world)
    164 			if err != nil || !reflect.DeepEqual(before, after) {
    165 				t.Fatal("stale move changed durable ownership", err)
    166 			}
    167 		})
    168 	}
    169 }
    170 
    171 func TestInventoryMoveRollsBackAndRetries(t *testing.T) {
    172 	for _, fault := range []string{
    173 		"BEFORE DELETE ON item_locations BEGIN SELECT RAISE(IGNORE); END",
    174 		"BEFORE INSERT ON item_locations BEGIN SELECT RAISE(IGNORE); END",
    175 		"BEFORE INSERT ON item_locations BEGIN SELECT RAISE(ABORT, 'move failed'); END",
    176 		"AFTER INSERT ON item_locations BEGIN UPDATE item_locations SET slot=66 WHERE item_id=NEW.item_id; END",
    177 	} {
    178 		t.Run(fault, func(t *testing.T) {
    179 			world, conn := newInventoryWriteWorld(t)
    180 			if _, err := world.db.Exec("CREATE TRIGGER reject_move " + fault); err != nil {
    181 				t.Fatal(err)
    182 			}
    183 
    184 			err := world.handleInventory(world.clients[1], inventoryMovePacket(1, 64, 65)[20:])
    185 
    186 			var slot uint32
    187 			if queryErr := world.db.QueryRow("SELECT slot FROM item_locations WHERE item_id=1").Scan(&slot); queryErr != nil {
    188 				t.Fatal(queryErr)
    189 			}
    190 			if err == nil || slot != 64 || world.characters[1].inventory.Items[0].Slot != 64 || len(conn.packets) != 0 {
    191 				t.Fatal("failed move published or retained a partial change", err, slot)
    192 			}
    193 			if _, err := world.db.Exec("DROP TRIGGER reject_move"); err != nil {
    194 				t.Fatal(err)
    195 			}
    196 			if err := world.handleInventory(world.clients[1], inventoryMovePacket(1, 64, 65)[20:]); err != nil {
    197 				t.Fatal(err)
    198 			}
    199 			restored := newTestInventoryServer(t)
    200 			if restored.characters[1].inventory.Items[0].Slot != 65 || world.characters[1].inventory.Items[0].Slot != 65 || len(conn.packets) != 1 {
    201 				t.Fatal("retry did not publish exactly one committed move")
    202 			}
    203 		})
    204 	}
    205 }