aorecomp

Anarchy Online classic in the browser

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


server/remote_reload_test.go (5894B)


      1 package main
      2 
      3 import (
      4 	"bytes"
      5 	"encoding/binary"
      6 	"fmt"
      7 	"slices"
      8 	"testing"
      9 )
     10 
     11 func newRemoteReloadWorld(t *testing.T) (*server, [3]*combatTestConn) {
     12 	t.Helper()
     13 	world, owner := newRangedWorld(t)
     14 	world.characters[1].inventory.Items[0].Ammo = 0
     15 	if err := world.saveInventory(world.characters[1], world.characters[1].inventory); err != nil {
     16 		t.Fatal(err)
     17 	}
     18 	world.nextCharacter = 1
     19 	observer := world.newCharacter()
     20 	if err := world.saveUser(observer); err != nil {
     21 		t.Fatal(err)
     22 	}
     23 	inventory := playerInventory{LastID: 4, Items: []ownedItem{
     24 		{ID: 3, Type: 51018, Template: 123742, Quality: 1, Slot: 6, Count: 1, Ammo: 9},
     25 		{ID: 4, Type: 51005, Template: 21605, Quality: 1, Slot: 64, Count: 7},
     26 	}}
     27 	if err := world.saveInventory(observer, inventory); err != nil {
     28 		t.Fatal(err)
     29 	}
     30 	outsider := world.newCharacter()
     31 	outsider.playfield = 952
     32 	connections := [3]*combatTestConn{owner, {}, {}}
     33 	world.clients[2] = &worldClient{id: 2, conn: connections[1], target: true, ready: true, players: map[uint32]bool{1: true}}
     34 	world.clients[3] = &worldClient{id: 3, conn: connections[2], target: true, ready: true}
     35 	owner.packets = nil
     36 	return world, connections
     37 }
     38 
     39 func TestReloadPublishesCommittedStateToVisibleObservers(t *testing.T) {
     40 	for _, row := range []struct{ source, loaded, remaining, removed uint32 }{{100, 20, 80, 0}, {5, 5, 0, 1}} {
     41 		t.Run(fmt.Sprint(row.source), func(t *testing.T) {
     42 			world, connections := newRemoteReloadWorld(t)
     43 			owner, observer := world.characters[1], world.characters[2]
     44 			owner.inventory.Items[1].Count = uint16(row.source)
     45 			if err := world.saveInventory(owner, owner.inventory); err != nil {
     46 				t.Fatal(err)
     47 			}
     48 			other := slices.Clone(observer.inventory.Items)
     49 			expected := []ownedItem{{ID: 1, Type: 51018, Template: 123742, Quality: 1, Slot: 6, Count: 1, Ammo: row.loaded}}
     50 			if row.remaining != 0 {
     51 				expected = append(expected, ownedItem{ID: 2, Type: 51005, Template: 21605, Quality: 1, Slot: 64, Count: uint16(row.remaining)})
     52 			}
     53 
     54 			for range 2 {
     55 				if err := world.handleReload(world.clients[1], explicitReloadRequest(1, 64, 1)); err != nil {
     56 					t.Fatal(err)
     57 				}
     58 			}
     59 			cold := newTestInventoryServer(t)
     60 
     61 			if len(connections[0].packets) != 2 || len(connections[1].packets) != 1 || len(connections[2].packets) != 0 {
     62 				t.Fatal("reload did not publish once to its observer while retaining the private acknowledgement")
     63 			}
     64 			reply := connections[1].packets[0]
     65 			if !bytes.Equal(reply[16:], connections[0].packets[1][16:]) || binary.BigEndian.Uint32(reply[16:20]) != 0x26515e61 ||
     66 				binary.BigEndian.Uint32(reply[24:28]) != 1 || binary.BigEndian.Uint32(reply[29:33]) != 104 ||
     67 				binary.BigEndian.Uint32(reply[33:37]) != 48 || binary.BigEndian.Uint32(reply[41:45]) != 1 ||
     68 				binary.BigEndian.Uint32(reply[45:49]) != row.remaining || binary.BigEndian.Uint32(reply[49:53]) != row.loaded ||
     69 				binary.BigEndian.Uint32(reply[53:57]) != row.removed {
     70 				t.Fatal("observer reload changed its subject, source, weapon or committed round counts")
     71 			}
     72 			for _, current := range []*server{world, cold} {
     73 				if !slices.Equal(current.characters[1].inventory.Items, expected) ||
     74 					!slices.Equal(current.characters[2].inventory.Items, other) || current.nextItem != 4 {
     75 					t.Fatal("reload changed another owner's items or lost a committed round")
     76 				}
     77 			}
     78 		})
     79 	}
     80 }
     81 
     82 func TestReloadFailureDoesNotPublishRemoteSuccess(t *testing.T) {
     83 	for _, fault := range []struct{ name, statement string }{
     84 		{"failed update", "CREATE TRIGGER reject_reload BEFORE UPDATE OF ammo ON items WHEN NEW.id=1 BEGIN SELECT RAISE(ABORT, 'reload fault'); END"},
     85 		{"ignored update", "CREATE TRIGGER reject_reload BEFORE UPDATE OF ammo ON items WHEN NEW.id=1 BEGIN SELECT RAISE(IGNORE); END"},
     86 		{"failed commit", "CREATE TABLE pending_reload (id INTEGER REFERENCES players(id) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER reject_reload AFTER UPDATE OF ammo ON items WHEN NEW.id=1 BEGIN INSERT INTO pending_reload VALUES(999999); END"},
     87 	} {
     88 		t.Run(fault.name, func(t *testing.T) {
     89 			world, connections := newRemoteReloadWorld(t)
     90 			before, err := readTestItemStore(world)
     91 			if err != nil {
     92 				t.Fatal(err)
     93 			}
     94 			if _, err := world.db.Exec(fault.statement); err != nil {
     95 				t.Fatal(err)
     96 			}
     97 
     98 			failed := world.handleReload(world.clients[1], explicitReloadRequest(1, 64, 1))
     99 			after, err := readTestItemStore(world)
    100 
    101 			if failed == nil || err != nil || !bytes.Equal(before, after) || world.characters[1].inventory.Items[0].Ammo != 0 ||
    102 				world.characters[1].inventory.Items[1].Count != 100 || !world.clients[1].reloadUntil.IsZero() ||
    103 				len(connections[0].packets)+len(connections[1].packets)+len(connections[2].packets) != 0 {
    104 				t.Fatal("failed reload published success or changed either side of the transfer", failed, err)
    105 			}
    106 
    107 			if _, err := world.db.Exec("DROP TRIGGER reject_reload"); err != nil {
    108 				t.Fatal(err)
    109 			}
    110 			if err := world.handleReload(world.clients[1], explicitReloadRequest(2, 64, 1)); err != nil {
    111 				t.Fatal(err)
    112 			}
    113 
    114 			if len(connections[0].packets) != 2 || len(connections[1].packets) != 1 || len(connections[2].packets) != 0 {
    115 				t.Fatal("a normal retry did not publish one committed reload")
    116 			}
    117 		})
    118 	}
    119 }
    120 
    121 func TestReloadLostOwnerReplyStillUpdatesItsObserver(t *testing.T) {
    122 	world, connections := newRemoteReloadWorld(t)
    123 	world.clients[1].conn = &shortWorldConn{}
    124 
    125 	err := world.handleReload(world.clients[1], explicitReloadRequest(1, 64, 1))
    126 	cold := newTestInventoryServer(t)
    127 
    128 	if err == nil || len(connections[1].packets) != 1 || len(connections[2].packets) != 0 ||
    129 		cold.characters[1].inventory.Items[0].Ammo != 20 || cold.characters[1].inventory.Items[1].Count != 80 ||
    130 		cold.characters[2].inventory.Items[0].Ammo != 9 || cold.characters[2].inventory.Items[1].Count != 7 {
    131 		t.Fatal("lost owner output hid the committed reload from its observer or changed ownership", err)
    132 	}
    133 }