aorecomp

Anarchy Online classic in the browser

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


server/player_trade_stale_offer_test.go (3158B)


      1 package main
      2 
      3 import (
      4 	"bytes"
      5 	"encoding/binary"
      6 	"reflect"
      7 	"testing"
      8 )
      9 
     10 func TestPlayerTradeStaleOfferRefreshesOnlyOwnerWithoutWrites(t *testing.T) {
     11 	world, output := newPlayerTradeWorld(t)
     12 	owner, peer := world.characters[1], world.characters[2]
     13 	inventory := owner.inventory
     14 	inventory.LastID = 4
     15 	inventory.Items = []ownedItem{
     16 		{ID: 1, Type: 51018, Template: 209283, Quality: 1, Slot: 67, Count: 1},
     17 		{ID: 2, Type: 51005, Template: 25812, Quality: 2, Slot: 65, Count: 8},
     18 		{ID: 4, Type: 51005, Template: 25812, Quality: 2, Slot: 66, Count: 7}}
     19 	if err := world.saveInventory(owner, inventory); err != nil {
     20 		t.Fatal(err)
     21 	}
     22 	peerItems, peerCash := peer.inventory.Items, peer.inventory.Credits
     23 	playerTradeRequest(t, world, 1, 0, [2]uint32{50000, 2}, [2]uint32{})
     24 	before, err := readTestItemStore(world)
     25 	if err != nil {
     26 		t.Fatal(err)
     27 	}
     28 	auditCheckpointWrites(t, world)
     29 	output[0].packets, output[1].packets = nil, nil
     30 
     31 	for range 2 {
     32 		playerTradeRequest(t, world, 1, 4, [2]uint32{50000, 1}, [2]uint32{104, 64})
     33 	}
     34 	after, err := readTestItemStore(world)
     35 	cold := newTestInventoryServer(t)
     36 
     37 	if err != nil || !bytes.Equal(before, after) || len(checkpointWrites(t, world)) != 0 ||
     38 		world.clients[1].trade != nil || world.clients[2].trade != nil ||
     39 		world.nextItem != 4 || cold.nextItem != 4 {
     40 		t.Fatal("stale player offer wrote state, retained a draft or changed allocation", err, bytes.Equal(before, after), checkpointWrites(t, world), world.nextItem, cold.nextItem)
     41 	}
     42 	if !reflect.DeepEqual(owner.inventory, inventory) || !reflect.DeepEqual(cold.characters[1].inventory, inventory) {
     43 		t.Fatal("stale offer changed the owner's inventory", owner.inventory, cold.characters[1].inventory, inventory)
     44 	}
     45 	if !reflect.DeepEqual(peer.inventory.Items, peerItems) || !reflect.DeepEqual(cold.characters[2].inventory.Items, peerItems) ||
     46 		peer.inventory.Credits != peerCash || cold.characters[2].inventory.Credits != peerCash {
     47 		t.Fatal("stale offer changed the peer's items/cash", peer.inventory, cold.characters[2].inventory)
     48 	}
     49 	for _, conn := range output {
     50 		if len(conn.packets) == 0 || binary.BigEndian.Uint32(conn.packets[0][16:20]) != 0x36284f6e ||
     51 			binary.BigEndian.Uint32(conn.packets[0][33:37]) != 2 {
     52 			t.Fatal("participant did not receive cancellation before correction")
     53 		}
     54 	}
     55 	if len(output[1].packets) != 1 {
     56 		t.Fatal("stale owner offer disturbed the peer's inventory")
     57 	}
     58 	var additions [][4]uint32
     59 	for _, packet := range output[0].packets[1:] {
     60 		if binary.BigEndian.Uint32(packet[16:20]) == 0x052e2f0c {
     61 			additions = append(additions, [4]uint32{binary.BigEndian.Uint32(packet[29:33]), binary.BigEndian.Uint32(packet[33:37]),
     62 				binary.BigEndian.Uint32(packet[37:41]), binary.BigEndian.Uint32(packet[41:45])})
     63 		}
     64 		if binary.BigEndian.Uint32(packet[16:20]) == 0x5e477770 &&
     65 			binary.BigEndian.Uint32(packet[29:33]) == 112 && binary.BigEndian.Uint32(packet[37:41]) == 51 {
     66 			t.Fatal("refresh retired the unrelated live weapon")
     67 		}
     68 	}
     69 	if !reflect.DeepEqual(additions, [][4]uint32{{25812, 25811, 2, 8}, {25812, 25811, 2, 7}}) {
     70 		t.Fatal("stale offer omitted authoritative selected-quality stacks", additions)
     71 	}
     72 }