aorecomp

Anarchy Online classic in the browser

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


server/tradeskills_test.go (14086B)


      1 package main
      2 
      3 import (
      4 	"bytes"
      5 	"encoding/binary"
      6 	"net"
      7 	"reflect"
      8 	"sync"
      9 	"testing"
     10 	"time"
     11 )
     12 
     13 func newCombinationWorld(t *testing.T) (*server, *combatTestConn) {
     14 	t.Helper()
     15 	world, conn := newEquipmentWorld(t)
     16 	player := world.characters[1]
     17 	player.profession = 4
     18 	// Ten in each ability contributes two points to both effective skills.
     19 	player.stats.TradeRepair.NanoProgramming = 23
     20 	player.stats.Spying.BreakingAndEntering = 58
     21 	if err := world.saveUser(player); err != nil {
     22 		t.Fatal(err)
     23 	}
     24 	for _, template := range []uint32{95577, 161699} {
     25 		if granted, err := world.grantItem(player, template); err != nil || !granted {
     26 			t.Fatalf("ingredient: %v %v", granted, err)
     27 		}
     28 	}
     29 	conn.packets = nil
     30 	return world, conn
     31 }
     32 
     33 func combinationRequest(owner, source, target uint32) []byte {
     34 	return characterActionPacket(owner, [5]uint32{81, 104, source, 104, target})[20:]
     35 }
     36 
     37 func TestItemCombinationConsumesBothIngredientsAndPersistsOneResult(t *testing.T) {
     38 	world, conn := newCombinationWorld(t)
     39 	observer := &combatTestConn{}
     40 	world.characters[2] = &character{id: 2, playfield: 950, health: 100}
     41 	world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, conn: observer, target: true, ready: true}
     42 
     43 	if err := world.handleItemCombination(world.clients[1], combinationRequest(1, 65, 66)); err != nil {
     44 		t.Fatal(err)
     45 	}
     46 	restored := newTestInventoryServer(t)
     47 	if err := world.handleItemCombination(world.clients[1], combinationRequest(1, 65, 66)); err != nil {
     48 		t.Fatal(err)
     49 	}
     50 
     51 	expected := []ownedItem{{ID: 1, Type: 51018, Template: 209283, Quality: 1, Slot: 64, Count: 1},
     52 		{ID: 4, Type: 51022, Template: 158416, Quality: 1, Slot: 65, Count: 1}}
     53 	if !reflect.DeepEqual(world.characters[1].inventory.Items, expected) || !reflect.DeepEqual(restored.characters[1].inventory.Items, expected) ||
     54 		world.nextItem != 4 || len(world.items) != 2 || len(observer.packets) != 0 || len(conn.packets) != 3 {
     55 		t.Fatal("combination lost unrelated ownership, retained ingredients or repeated the result")
     56 	}
     57 	for index, slot := range []uint32{49, 50} {
     58 		if binary.BigEndian.Uint32(conn.packets[index][29:33]) != 112 || binary.BigEndian.Uint32(conn.packets[index][37:41]) != slot {
     59 			t.Fatal("ingredient deletion did not precede native first-free result delivery")
     60 		}
     61 	}
     62 	if binary.BigEndian.Uint32(conn.packets[2][16:20]) != 0x052e2f0c ||
     63 		!bytes.Equal(conn.packets[2][29:45], []byte{0, 2, 106, 208, 0, 2, 106, 208, 0, 0, 0, 1, 0, 0, 0, 1}) {
     64 		t.Fatalf("wrong result templates, QL or quantity: %x", conn.packets[2])
     65 	}
     66 }
     67 
     68 func TestItemCombinationRejectsInvalidSourcesAndInsufficientEffectiveSkills(t *testing.T) {
     69 	for _, scenario := range []string{"nano programming", "breaking and entering", "reverse", "personal lock pick", "missing", "same slot", "equipment", "bank", "container", "foreign", "replaced", "loading", "dead", "pending equip", "quality", "type"} {
     70 		t.Run(scenario, func(t *testing.T) {
     71 			world, conn := newCombinationWorld(t)
     72 			player := world.characters[1]
     73 			client := world.clients[1]
     74 			request := combinationRequest(1, 65, 66)
     75 			feedback := uint32(0)
     76 			switch scenario {
     77 			case "nano programming":
     78 				player.stats.TradeRepair.NanoProgramming = 22
     79 				feedback = 160
     80 			case "breaking and entering":
     81 				player.stats.Spying.BreakingAndEntering = 57
     82 				feedback = 165
     83 			case "reverse":
     84 				request = combinationRequest(1, 66, 65)
     85 				feedback = 2
     86 			case "personal lock pick":
     87 				player.inventory.Items[1].Template = 156639
     88 				feedback = 2
     89 			case "missing":
     90 				request = combinationRequest(1, 65, 93)
     91 			case "same slot":
     92 				request = combinationRequest(1, 65, 65)
     93 			case "equipment":
     94 				request = combinationRequest(1, 6, 66)
     95 			case "bank":
     96 				binary.BigEndian.PutUint32(request[20:24], 105)
     97 			case "container":
     98 				binary.BigEndian.PutUint32(request[28:32], 107)
     99 			case "foreign":
    100 				binary.BigEndian.PutUint32(request[8:12], 2)
    101 			case "replaced":
    102 				world.clients[1] = &worldClient{id: 1}
    103 			case "loading":
    104 				world.clients[1].loading = true
    105 			case "dead":
    106 				player.health = 0
    107 			case "pending equip":
    108 				world.clients[1].equipment = &equipmentMove{}
    109 			case "quality":
    110 				player.inventory.Items[1].Quality = 2
    111 			case "type":
    112 				player.inventory.Items[1].Type = 51022
    113 			}
    114 			before, err := readTestItemStore(world)
    115 			if err != nil {
    116 				t.Fatal(err)
    117 			}
    118 			items := append([]ownedItem(nil), player.inventory.Items...)
    119 
    120 			err = world.handleItemCombination(client, request)
    121 			after, readErr := readTestItemStore(world)
    122 
    123 			if err != nil || readErr != nil || !bytes.Equal(before, after) || !reflect.DeepEqual(player.inventory.Items, items) || world.nextItem != 3 {
    124 				t.Fatal("rejected combination changed ownership or allocated an identity")
    125 			}
    126 			if feedback == 0 {
    127 				if len(conn.packets) != 0 {
    128 					t.Fatal("unauthenticated/invalid source received a combination response")
    129 				}
    130 				return
    131 			}
    132 			if len(conn.packets) != 1 || binary.BigEndian.Uint32(conn.packets[0][29:33]) != 84 {
    133 				t.Fatal("missing native combination feedback")
    134 			}
    135 			if feedback == 2 {
    136 				if binary.BigEndian.Uint32(conn.packets[0][33:37]) != 2 {
    137 					t.Fatal("wrong recipe/order feedback")
    138 				}
    139 				return
    140 			}
    141 			required := uint32(25)
    142 			if feedback == 165 {
    143 				required = 60
    144 			}
    145 			if binary.BigEndian.Uint32(conn.packets[0][33:37]) != 32 || binary.BigEndian.Uint32(conn.packets[0][41:45]) != feedback ||
    146 				binary.BigEndian.Uint32(conn.packets[0][45:49]) != required {
    147 				t.Fatal("wrong effective-skill failure or native threshold")
    148 			}
    149 		})
    150 	}
    151 }
    152 
    153 func TestItemCombinationFailureKeepsIngredientsAndAllowsRetry(t *testing.T) {
    154 	for _, trigger := range []string{
    155 		"CREATE TRIGGER fail_combination BEFORE DELETE ON items WHEN OLD.id=3 BEGIN SELECT RAISE(ABORT, 'ingredient'); END",
    156 		"CREATE TRIGGER fail_combination BEFORE INSERT ON items WHEN NEW.template=158416 BEGIN SELECT RAISE(ABORT, 'result'); END",
    157 		"CREATE TRIGGER fail_combination BEFORE INSERT ON item_locations WHEN NEW.item_id=4 BEGIN SELECT RAISE(ABORT, 'placement'); END",
    158 	} {
    159 		t.Run(trigger, func(t *testing.T) {
    160 			world, conn := newCombinationWorld(t)
    161 			before, err := readTestItemStore(world)
    162 			if err != nil {
    163 				t.Fatal(err)
    164 			}
    165 			if _, err := world.db.Exec(trigger); err != nil {
    166 				t.Fatal(err)
    167 			}
    168 
    169 			failed := world.handleItemCombination(world.clients[1], combinationRequest(1, 65, 66))
    170 			after, err := readTestItemStore(world)
    171 			if err != nil {
    172 				t.Fatal(err)
    173 			}
    174 
    175 			if failed == nil || !bytes.Equal(before, after) || len(world.characters[1].inventory.Items) != 3 || world.nextItem != 3 || len(conn.packets) != 0 {
    176 				t.Fatal("failed combination published consumption, allocation or feedback")
    177 			}
    178 			if _, err := world.db.Exec("DROP TRIGGER fail_combination"); err != nil {
    179 				t.Fatal(err)
    180 			}
    181 			if err := world.handleItemCombination(world.clients[1], combinationRequest(1, 65, 66)); err != nil {
    182 				t.Fatal(err)
    183 			}
    184 			if newTestInventoryServer(t).characters[1].inventory.Items[1].Template != 158416 {
    185 				t.Fatal("retry did not persist the result")
    186 			}
    187 		})
    188 	}
    189 }
    190 
    191 func TestItemCombinationConcurrentReplayAndFullInventory(t *testing.T) {
    192 	world, conn := newCombinationWorld(t)
    193 	for range 27 {
    194 		if granted, err := world.grantItem(world.characters[1], 95577); err != nil || !granted {
    195 			t.Fatalf("fill: %v %v", granted, err)
    196 		}
    197 	}
    198 	conn.packets = nil
    199 
    200 	client := world.clients[1]
    201 	shutdown := startTestService(t, world)
    202 	owner, err := world.world.owner(1)
    203 	if err != nil {
    204 		t.Fatal(err)
    205 	}
    206 
    207 	var requests sync.WaitGroup
    208 	for range 8 {
    209 		requests.Go(func() {
    210 			if err := owner.call(zonePacket{client: client, body: combinationRequest(1, 65, 66)}); err != nil {
    211 				t.Error(err)
    212 			}
    213 		})
    214 	}
    215 	requests.Wait()
    216 	if err := shutdown(); err != nil {
    217 		t.Fatal(err)
    218 	}
    219 	restored := newTestInventoryServer(t)
    220 
    221 	if len(restored.characters[1].inventory.Items) != 29 || restored.characters[1].inventory.Items[28] !=
    222 		(ownedItem{ID: 31, Type: 51022, Template: 158416, Quality: 1, Slot: 65, Count: 1}) || restored.nextItem != 31 {
    223 		t.Fatal("full-inventory combination lost value or repeated creation")
    224 	}
    225 	if len(conn.packets) != 3 {
    226 		t.Fatal("replayed completion emitted another result")
    227 	}
    228 }
    229 
    230 func TestItemCombinationUsesTheOriginalNativeInventorySlots(t *testing.T) {
    231 	newCombinationWorld(t)
    232 	world := newTestInventoryServer(t)
    233 	startTestService(t, world)
    234 	client, connection := net.Pipe()
    235 	client.SetDeadline(time.Now().Add(5 * time.Second))
    236 	done := make(chan error, 1)
    237 	go func() { defer connection.Close(); done <- world.serveWorldConnection(connection) }()
    238 	defer func() {
    239 		client.Close()
    240 		if err := <-done; err != nil {
    241 			t.Error(err)
    242 		}
    243 	}()
    244 	cookie := systemPacket(0x1b, []byte{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0})
    245 	binary.BigEndian.PutUint16(cookie[0:2], 0xdfdf)
    246 	client.Write(cookie)
    247 	for {
    248 		packet, err := readPacket(client)
    249 		if err != nil {
    250 			t.Fatal(err)
    251 		}
    252 		if binary.BigEndian.Uint32(packet[16:20]) == 0x5f52412e {
    253 			break
    254 		}
    255 	}
    256 	client.Write(targetPacket(n3Packet(0x570c2039, 50000, 1, nil)))
    257 	if _, err := readPacket(client); err != nil {
    258 		t.Fatal(err)
    259 	}
    260 	request := targetPacket(characterActionPacket(1, [5]uint32{81, 104, 65, 104, 66}))
    261 
    262 	client.Write(request)
    263 	var replies [][]byte
    264 	for range 3 {
    265 		packet, err := readPacket(client)
    266 		if err != nil {
    267 			t.Fatal(err)
    268 		}
    269 		replies = append(replies, packet)
    270 	}
    271 
    272 	if binary.BigEndian.Uint32(request[37:41]) != 49 || binary.BigEndian.Uint32(request[45:49]) != 50 ||
    273 		binary.BigEndian.Uint32(replies[0][37:41]) != 49 || binary.BigEndian.Uint32(replies[1][37:41]) != 50 ||
    274 		binary.BigEndian.Uint32(replies[2][16:20]) != 0x052e2f0c || newTestInventoryServer(t).characters[1].inventory.Items[1].Template != 158416 {
    275 		t.Fatal("native request/reply translated the wrong inventory slots")
    276 	}
    277 }
    278 
    279 func TestCraftedNCUHackerInterfaceCanBeWornAndRemoved(t *testing.T) {
    280 	world, _ := newCombinationWorld(t)
    281 	client, player := world.clients[1], world.characters[1]
    282 	if err := world.handleItemCombination(client, combinationRequest(1, 65, 66)); err != nil {
    283 		t.Fatal(err)
    284 	}
    285 
    286 	if err := world.handleInventory(client, inventoryMovePacket(1, 65, 15)[20:]); err != nil {
    287 		t.Fatal(err)
    288 	}
    289 	if client.equipment == nil {
    290 		t.Fatal("crafted HUD item could not be worn")
    291 	}
    292 	world.advanceEquipment(client.equipment.readyAt)
    293 	worn := newTestInventoryServer(t).characters[1]
    294 	if err := world.handleInventory(client, itemMovePacket(1, 15, 111)[20:]); err != nil {
    295 		t.Fatal(err)
    296 	}
    297 	world.advanceEquipment(client.equipment.readyAt)
    298 
    299 	if worn.inventory.Items[1] != (ownedItem{ID: 4, Type: 51022, Template: 158416, Quality: 1, Slot: 15, Count: 1}) ||
    300 		player.inventory.Items[1].ID != 4 || player.inventory.Items[1].Slot != 65 {
    301 		t.Fatal("wear/remove changed the crafted identity or placement")
    302 	}
    303 	for _, stat := range []uint32{122, 127, 128, 129, 130, 131} {
    304 		if worn.equipmentBonus(stat) != 1 || player.equipmentBonus(stat) != 0 {
    305 			t.Fatal("crafted item did not add/remove its six nano-skill bonuses")
    306 		}
    307 	}
    308 }
    309 
    310 func TestItemCombinationCancelsOpenBasketsWithoutConsumingRestoredItems(t *testing.T) {
    311 	for _, scenario := range []string{"shop", "player"} {
    312 		t.Run(scenario, func(t *testing.T) {
    313 			var world *server
    314 			source := uint32(65)
    315 			if scenario == "shop" {
    316 				world, _ = newShopWorld(t)
    317 			} else {
    318 				world, _ = newPlayerTradeWorld(t)
    319 				playerTradeRequest(t, world, 1, 0, [2]uint32{50000, 2}, [2]uint32{})
    320 				source = 66
    321 			}
    322 			for _, template := range []uint32{95577, 161699} {
    323 				if granted, err := world.grantItem(world.characters[1], template); err != nil || !granted {
    324 					t.Fatalf("ingredient: %v %v", granted, err)
    325 				}
    326 			}
    327 			playerTradeRequest(t, world, 1, 4, [2]uint32{50000, 1}, [2]uint32{104, source})
    328 			before, err := readTestItemStore(world)
    329 			if err != nil {
    330 				t.Fatal(err)
    331 			}
    332 			lastID := world.nextItem
    333 
    334 			err = world.handleItemCombination(world.clients[1], combinationRequest(1, source, source+1))
    335 			after, readErr := readTestItemStore(world)
    336 
    337 			if err != nil || readErr != nil || !bytes.Equal(before, after) || world.nextItem != lastID ||
    338 				world.clients[1].trade != nil || world.clients[1].shop != nil {
    339 				t.Fatal("basket cancellation consumed a restored ingredient or allocated a result")
    340 			}
    341 		})
    342 	}
    343 }
    344 
    345 func TestItemCombinationLostReplyKeepsTheCommittedResult(t *testing.T) {
    346 	world, _ := newCombinationWorld(t)
    347 	world.clients[1].conn = &failingCombatConn{}
    348 
    349 	failed := world.handleItemCombination(world.clients[1], combinationRequest(1, 65, 66))
    350 	if err := world.handleItemCombination(world.clients[1], combinationRequest(1, 65, 66)); err != nil {
    351 		t.Fatal(err)
    352 	}
    353 	restored := newTestInventoryServer(t)
    354 
    355 	if failed == nil || len(restored.characters[1].inventory.Items) != 2 || world.nextItem != 4 ||
    356 		restored.characters[1].inventory.Items[1] != (ownedItem{ID: 4, Type: 51022, Template: 158416, Quality: 1, Slot: 65, Count: 1}) {
    357 		t.Fatal("lost reply reversed consumption or repeated result creation")
    358 	}
    359 }
    360 
    361 func TestCraftedNCUHackerInterfaceRejectsWrongProfessionAndSlot(t *testing.T) {
    362 	for _, scenario := range []string{"profession", "slot"} {
    363 		t.Run(scenario, func(t *testing.T) {
    364 			world, conn := newCombinationWorld(t)
    365 			client := world.clients[1]
    366 			if err := world.handleItemCombination(client, combinationRequest(1, 65, 66)); err != nil {
    367 				t.Fatal(err)
    368 			}
    369 			slot := uint32(15)
    370 			if scenario == "profession" {
    371 				world.characters[1].profession = 3
    372 			} else {
    373 				slot = 1
    374 			}
    375 			before, err := readTestItemStore(world)
    376 			if err != nil {
    377 				t.Fatal(err)
    378 			}
    379 			conn.packets = nil
    380 
    381 			err = world.handleInventory(client, inventoryMovePacket(1, 65, slot)[20:])
    382 			after, readErr := readTestItemStore(world)
    383 
    384 			if err != nil || readErr != nil || !bytes.Equal(before, after) || client.equipment != nil ||
    385 				world.characters[1].inventory.Items[1].Slot != 65 || world.characters[1].equipmentBonus(122) != 0 ||
    386 				len(conn.packets) != 2 || binary.BigEndian.Uint32(conn.packets[0][16:20]) != 0x47537a24 ||
    387 				binary.BigEndian.Uint32(conn.packets[1][16:20]) != 0x47537a24 {
    388 				t.Fatal("crafted item's authored wear rejection changed ownership or effects")
    389 			}
    390 		})
    391 	}
    392 }