server/player_trade_feedback_test.go (4556B)
1 package main 2 3 import ( 4 "bytes" 5 "encoding/binary" 6 "fmt" 7 "testing" 8 ) 9 10 func TestPlayerTradeRejectionExplainsEachParticipantsFailure(t *testing.T) { 11 for _, reason := range []string{"full inventory", "duplicate unique"} { 12 for _, recipient := range []uint32{1, 2} { 13 t.Run(fmt.Sprintf("%s/recipient%d", reason, recipient), func(t *testing.T) { 14 world, conns := newPlayerTradeWorld(t) 15 donor := 3 - recipient 16 source := uint32(64) 17 if donor == 1 { 18 source = 65 19 } 20 if reason == "full inventory" { 21 for len(world.characters[recipient].inventory.Items) < 30 { 22 if granted, err := world.grantItem(world.characters[recipient], 95577); err != nil || !granted { 23 t.Fatalf("fill: %v %v", granted, err) 24 } 25 } 26 } else { 27 for _, id := range []uint32{1, 2} { 28 if granted, err := world.grantItem(world.characters[id], 161699); err != nil || !granted { 29 t.Fatalf("unique: %v %v", granted, err) 30 } 31 } 32 source++ 33 } 34 before, err := readTestItemStore(world) 35 if err != nil { 36 t.Fatal(err) 37 } 38 playerTradeRequest(t, world, 1, 0, [2]uint32{50000, 2}, [2]uint32{}) 39 playerTradeRequest(t, world, donor, 4, [2]uint32{50000, donor}, [2]uint32{104, source}) 40 playerTradeRequest(t, world, donor, 1, [2]uint32{}, [2]uint32{}) 41 conns[0].packets, conns[1].packets = nil, nil 42 43 playerTradeRequest(t, world, recipient, 1, [2]uint32{}, [2]uint32{}) 44 playerTradeRequest(t, world, recipient, 1, [2]uint32{}, [2]uint32{}) 45 after, err := readTestItemStore(world) 46 if err != nil { 47 t.Fatal(err) 48 } 49 50 if !bytes.Equal(before, after) || world.clients[1].trade != nil || world.clients[2].trade != nil { 51 t.Fatal("rejection changed ownership/cash or retained the draft") 52 } 53 for side, conn := range conns { 54 code := uint32(21) 55 if uint32(side+1) == recipient { 56 code = 24 57 } 58 if reason == "duplicate unique" { 59 code = 47 60 if uint32(side+1) == recipient { 61 code = 48 62 } 63 } 64 feedback := 0 65 for _, packet := range conn.packets { 66 if binary.BigEndian.Uint32(packet[16:20]) == 0x50544d19 { 67 feedback++ 68 if binary.BigEndian.Uint32(packet[24:28]) != uint32(side+1) || binary.BigEndian.Uint32(packet[29:33]) != code { 69 t.Fatalf("wrong feedback perspective: %x", packet) 70 } 71 } 72 } 73 if feedback != 1 || binary.BigEndian.Uint32(conn.packets[0][33:37]) != 2 || 74 binary.BigEndian.Uint32(conn.packets[len(conn.packets)-1][16:20]) != 0x50544d19 { 75 t.Fatal("feedback was repeated, missing or sent before cancellation") 76 } 77 } 78 }) 79 } 80 } 81 } 82 83 func TestPlayerTradeRejectionWaitsForDurableRestoration(t *testing.T) { 84 world, conns := newPlayerTradeWorld(t) 85 for _, id := range []uint32{1, 2} { 86 if granted, err := world.grantItem(world.characters[id], 161699); err != nil || !granted { 87 t.Fatalf("unique: %v %v", granted, err) 88 } 89 } 90 inventory := world.characters[1].inventory 91 inventory.Items[2].Slot = 70 92 if err := world.saveInventory(world.characters[1], inventory); err != nil { 93 t.Fatal(err) 94 } 95 playerTradeRequest(t, world, 1, 0, [2]uint32{50000, 2}, [2]uint32{}) 96 playerTradeRequest(t, world, 1, 4, [2]uint32{50000, 1}, [2]uint32{104, 70}) 97 playerTradeRequest(t, world, 1, 1, [2]uint32{}, [2]uint32{}) 98 before, err := readTestItemStore(world) 99 if err != nil { 100 t.Fatal(err) 101 } 102 if _, err := world.db.Exec("CREATE TRIGGER fail_restore BEFORE INSERT ON item_locations WHEN NEW.item_id=4 BEGIN SELECT RAISE(ABORT, 'restoration'); END"); err != nil { 103 t.Fatal(err) 104 } 105 conns[0].packets, conns[1].packets = nil, nil 106 request := shopTradePacket([2]uint32{50000, 2}, 1, [2]uint32{}, [2]uint32{})[20:] 107 108 failed := world.handleTrade(world.clients[2], request) 109 after, err := readTestItemStore(world) 110 if err != nil { 111 t.Fatal(err) 112 } 113 114 if failed == nil || !bytes.Equal(before, after) || world.clients[1].trade == nil || world.clients[2].trade == nil || 115 len(conns[0].packets) != 0 || len(conns[1].packets) != 0 { 116 t.Fatal("failed restoration published rejection or changed ownership") 117 } 118 if _, err := world.db.Exec("DROP TRIGGER fail_restore"); err != nil { 119 t.Fatal(err) 120 } 121 playerTradeRequest(t, world, 2, 1, [2]uint32{}, [2]uint32{}) 122 if world.clients[1].trade != nil || world.clients[2].trade != nil || 123 binary.BigEndian.Uint32(conns[0].packets[len(conns[0].packets)-1][29:33]) != 47 || 124 binary.BigEndian.Uint32(conns[1].packets[len(conns[1].packets)-1][29:33]) != 48 { 125 t.Fatal("successful retry did not cancel with the Unique explanation") 126 } 127 }