server/ncu_hacker_interface_test.go (7031B)
1 package main 2 3 import ( 4 "bytes" 5 "encoding/binary" 6 "fmt" 7 "net/url" 8 "testing" 9 ) 10 11 func TestNCUHackerInterfaceQualityWearRemoveAndRestart(t *testing.T) { 12 for _, template := range []uint32{158416, 158417} { 13 for _, scenario := range []struct { 14 quality uint32 15 bonus int32 16 low uint32 17 high uint32 18 }{ 19 {1, 1, 158416, 158416}, {2, 1, 158416, 158417}, 20 {3, 2, 158416, 158417}, {50, 16, 158416, 158417}, 21 {100, 30, 158416, 158417}, {101, 31, 158416, 158417}, 22 {199, 60, 158416, 158417}, {200, 60, 158417, 158417}, 23 } { 24 t.Run(fmt.Sprintf("%d/QL%d", template, scenario.quality), func(t *testing.T) { 25 world, owner := newEquipmentWorld(t) 26 player, client := world.characters[1], world.clients[1] 27 player.level, player.profession = uint16(scenario.quality), 4 28 if err := world.saveUser(player); err != nil { 29 t.Fatal(err) 30 } 31 player.inventory.Items[0] = ownedItem{ID: 1, Type: 51022, Template: template, Quality: scenario.quality, Slot: 64, Count: 1} 32 if err := world.saveInventory(player, player.inventory); err != nil { 33 t.Fatal(err) 34 } 35 observer := &combatTestConn{} 36 world.characters[2] = &character{id: 2, playfield: player.playfield, health: 100} 37 world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, conn: observer, target: true, ready: true} 38 39 if err := world.handleInventory(client, inventoryMovePacket(1, 64, 15)[20:]); err != nil || client.equipment == nil { 40 t.Fatal("HUD2 wear rejected", err) 41 } 42 world.advanceEquipment(client.equipment.readyAt) 43 worn := newTestInventoryServer(t).characters[1] 44 snapshot := targetPacket(fullCharacterPacket(worn)) 45 46 expected := ownedItem{ID: 1, Type: 51022, Template: template, Quality: scenario.quality, Slot: 15, Count: 1} 47 if worn.inventory.Items[0] != expected || len(owner.packets) != 2 || len(observer.packets) != 1 || 48 !bytes.Equal(owner.packets[1][16:], observer.packets[0][16:]) { 49 t.Fatal("wear changed identity, quality or observer replication") 50 } 51 for _, stat := range []uint32{122, 127, 128, 129, 130, 131} { 52 if worn.equipmentBonus(stat) != scenario.bonus || player.equipmentBonus(stat) != scenario.bonus { 53 t.Fatalf("stat%d: got %d, want %d", stat, worn.equipmentBonus(stat), scenario.bonus) 54 } 55 } 56 var action, entry []byte 57 for _, value := range []uint32{scenario.low, scenario.high, scenario.quality, 1, 6, 101, 15, 0, 0} { 58 action = binary.BigEndian.AppendUint32(action, value) 59 } 60 for _, value := range []uint32{1, scenario.low, scenario.high, scenario.quality, 0} { 61 entry = binary.BigEndian.AppendUint32(entry, value) 62 } 63 if !bytes.Equal(owner.packets[1][29:65], action) || !bytes.Contains(snapshot, entry) || len(equippedItemPackets(worn)) != 1 { 64 t.Fatal("wear or cold snapshot lost the original endpoint pair") 65 } 66 67 if err := world.handleInventory(client, itemMovePacket(1, 15, 111)[20:]); err != nil || client.equipment == nil { 68 t.Fatal("HUD2 removal rejected", err) 69 } 70 world.advanceEquipment(client.equipment.readyAt) 71 removed := newTestInventoryServer(t).characters[1] 72 73 expected.Slot = 64 74 if removed.inventory.Items[0] != expected || len(owner.packets) != 4 || len(observer.packets) != 2 { 75 t.Fatal("removal changed ownership or replication") 76 } 77 for _, stat := range []uint32{122, 127, 128, 129, 130, 131} { 78 if removed.equipmentBonus(stat) != 0 || player.equipmentBonus(stat) != 0 { 79 t.Fatal("removal retained a nano-skill modifier") 80 } 81 } 82 }) 83 } 84 } 85 } 86 87 func TestNCUHackerInterfaceWearRejectionPreservesItems(t *testing.T) { 88 for _, scenario := range []string{"low level", "wrong profession", "wrong slot", "quality zero", "quality 201", "source interface", "wrong type", "changed level", "failed save"} { 89 t.Run(scenario, func(t *testing.T) { 90 world, owner := newEquipmentWorld(t) 91 player, client := world.characters[1], world.clients[1] 92 player.level, player.profession = 50, 4 93 player.inventory.Items[0] = ownedItem{ID: 1, Type: 51022, Template: 158416, Quality: 50, Slot: 64, Count: 1} 94 destination := uint32(15) 95 switch scenario { 96 case "low level": 97 player.level = 49 98 case "wrong profession": 99 player.profession = 1 100 case "wrong slot": 101 destination = 1 102 case "quality zero": 103 player.inventory.Items[0].Quality = 0 104 case "quality 201": 105 player.inventory.Items[0].Quality = 201 106 case "source interface": 107 player.inventory.Items[0].Template = 161699 108 case "wrong type": 109 player.inventory.Items[0].Type = 51005 110 } 111 if err := world.saveInventory(player, player.inventory); err != nil { 112 t.Fatal(err) 113 } 114 before, err := readTestItemStore(world) 115 if err != nil { 116 t.Fatal(err) 117 } 118 if scenario == "failed save" { 119 if _, err := world.db.Exec("CREATE TRIGGER fail_ncu_wear BEFORE INSERT ON item_locations BEGIN SELECT RAISE(ABORT, 'move failure'); END"); err != nil { 120 t.Fatal(err) 121 } 122 } 123 124 err = world.handleInventory(client, inventoryMovePacket(1, 64, destination)[20:]) 125 if (scenario == "changed level" || scenario == "failed save") && client.equipment == nil { 126 t.Fatal("valid wear did not reach the delayed completion") 127 } 128 if scenario == "changed level" { 129 player.level = 49 130 } 131 if client.equipment != nil { 132 world.advanceEquipment(client.equipment.readyAt) 133 } 134 after, readErr := readTestItemStore(world) 135 136 if err != nil || readErr != nil || !bytes.Equal(before, after) || client.equipment != nil || player.inventory.Items[0].Slot != 64 { 137 t.Fatal("rejected wear changed item state", err, readErr) 138 } 139 for _, packet := range owner.packets { 140 if binary.BigEndian.Uint32(packet[16:20]) == 0x35505644 { 141 t.Fatal("rejected wear published native effects") 142 } 143 } 144 }) 145 } 146 } 147 148 func TestControlNCUHackerInterfaceQualityBounds(t *testing.T) { 149 for _, scenario := range []struct { 150 template, quality uint32 151 status int 152 }{ 153 {158416, 1, 303}, {158416, 50, 303}, {158416, 200, 303}, 154 {158417, 1, 303}, {158417, 101, 303}, {158417, 200, 303}, 155 {158416, 0, 400}, {158416, 201, 400}, {158417, 0, 400}, {158417, 201, 400}, 156 {161699, 50, 400}, 157 } { 158 t.Run(fmt.Sprintf("%d/QL%d", scenario.template, scenario.quality), func(t *testing.T) { 159 useNPCData(t, testNPCData()) 160 world := newTestServer(t) 161 postControlForm(world, "/admin/items", url.Values{"template": {fmt.Sprint(scenario.template)}}) 162 before, _ := readTestItemStore(world) 163 fields := url.Values{"quality": {fmt.Sprint(scenario.quality)}} 164 165 response := postControlForm(world, "/admin/items/1", fields) 166 after, err := readTestItemStore(world) 167 168 if response.Code != scenario.status || err != nil { 169 t.Fatal(response.Code, response.Body.String(), err) 170 } 171 if scenario.status == 400 { 172 if !bytes.Equal(before, after) { 173 t.Fatal("rejected QL changed durable data") 174 } 175 } else if newTestInventoryServer(t).items[1].Quality != scenario.quality { 176 t.Fatal("supported QL did not survive restart") 177 } 178 }) 179 } 180 }