server/appearance_test.go (7829B)
1 package main 2 3 import ( 4 "bytes" 5 "encoding/binary" 6 "fmt" 7 "net/url" 8 "testing" 9 ) 10 11 func TestPlayerAppearanceIncludesVehicleMaterials(t *testing.T) { 12 player := &character{id: 1, name: "Driver", appearance: 0x515, level: 1} 13 14 packet := targetPacket(characterPacket(player)) 15 16 start := bytes.Index(packet, []byte("arms\x00")) 17 if start < 4 || binary.BigEndian.Uint32(packet[start-4:start]) != 10090 { 18 t.Fatal("player texture list does not contain nine materials") 19 } 20 for _, material := range []struct { 21 name string 22 texture uint32 23 second uint32 24 }{{"Material #2b", 30043, 0}, {"Material #2a", 30044, 0}, 25 {"Material #7", 38586, 38585}, {"Material #8", 38584, 38585}} { 26 name := make([]byte, 32) 27 copy(name, material.name) 28 start := bytes.Index(packet, name) 29 if start < 0 { 30 t.Fatalf("missing vehicle material %s", material.name) 31 } 32 values := packet[start+32:] 33 if binary.BigEndian.Uint32(values) != material.texture || binary.BigEndian.Uint32(values[4:]) != material.second || binary.BigEndian.Uint32(values[8:]) != 0 { 34 t.Fatalf("incorrect vehicle texture for %s: %x", material.name, values[:12]) 35 } 36 } 37 } 38 39 func TestPlayerAppearanceUsesEachNativeBreedAndSexTemplate(t *testing.T) { 40 for _, scenario := range []struct { 41 breed, sex, race uint16 42 head uint32 43 textures [5]uint32 44 }{ 45 {1, 2, 3, 40680, [5]uint32{8714, 8710, 8771, 8713, 8709}}, 46 {1, 3, 3, 40628, [5]uint32{8849, 8850, 8864, 8853, 8848}}, 47 {2, 2, 1, 40284, [5]uint32{8964, 8960, 8963, 8965, 8961}}, 48 {2, 3, 1, 40246, [5]uint32{8885, 8886, 8887, 8884, 8883}}, 49 {3, 2, 1, 40206, [5]uint32{8986, 8983, 8984, 8985, 8982}}, 50 {3, 3, 1, 40169, [5]uint32{9028, 9024, 9023, 9027, 9026}}, 51 {4, 1, 1, 40132, [5]uint32{9233, 9231, 9240, 9234, 9236}}, 52 } { 53 t.Run(fmt.Sprintf("%d/%d", scenario.breed, scenario.sex), func(t *testing.T) { 54 appearance := appearanceFields{Race: scenario.race, Breed: scenario.breed, Sex: scenario.sex} 55 player := &character{id: 1, name: "Visitor", appearance: appearance.packed(), headMesh: scenario.head, level: 1} 56 57 packet := characterPacket(player) 58 59 if binary.BigEndian.Uint32(packet[159:163]) != scenario.head { 60 t.Fatal("explicit head mesh was replaced or truncated") 61 } 62 for index, name := range []string{"arms", "feet", "hands", "legs", "body"} { 63 start := bytes.Index(packet, append([]byte(name), 0)) + len(name) + 1 64 if start < len(name)+1 || binary.BigEndian.Uint32(packet[start:]) != scenario.textures[index] { 65 t.Fatalf("%s did not use its native resolved default material", name) 66 } 67 } 68 }) 69 } 70 } 71 72 func TestPlayerHeightReachesSpawnStatsAndPhysicalExtent(t *testing.T) { 73 for _, scenario := range []struct{ height, native uint16 }{{0, 100}, {1, 20}, {19, 20}, {20, 20}, {90, 90}, {100, 100}, {110, 110}} { 74 t.Run(fmt.Sprint(scenario.height), func(t *testing.T) { 75 player := &character{id: 1, name: "Visitor", height: scenario.height, level: 1} 76 77 spawn, full := characterPacket(player), fullCharacterPacket(player) 78 stats := make(map[uint32]uint32) 79 count := binary.BigEndian.Uint32(full[64:68])/1009 - 1 80 for offset := 68; offset < 68+int(count)*8; offset += 8 { 81 stats[binary.BigEndian.Uint32(full[offset:])] = binary.BigEndian.Uint32(full[offset+4:]) 82 } 83 extent := player.physicalExtent() 84 85 if binary.BigEndian.Uint16(spawn[111:113]) != scenario.native || stats[360] != uint32(scenario.native) || extent != float32(scenario.native*2)*0.01 { 86 t.Fatalf("height disagrees between observer, local stats and extent: %d %d %f", binary.BigEndian.Uint16(spawn[111:113]), stats[360], extent) 87 } 88 }) 89 } 90 } 91 92 func TestPlayerAppearancePersistsThroughControlEditAndRestart(t *testing.T) { 93 world := newTestServer(t) 94 postControlForm(world, "/admin/players", url.Values{"name": {"Visitor"}}) 95 96 response := postControlForm(world, "/admin/players/1", playerFixtureForm("head_mesh: 40132\nheight: 110\nappearance: {race: 1, breed: 4, sex: 1, fatness: 2}\n")) 97 restored := newTestInventoryServer(t).characters[1] 98 spawn := characterPacket(restored) 99 100 if response.Code != 303 || restored.headMesh != 40132 || restored.height != 110 || restored.appearance != 0x2c9 || 101 binary.BigEndian.Uint32(spawn[159:163]) != 40132 || binary.BigEndian.Uint16(spawn[111:113]) != 110 { 102 t.Fatalf("appearance changed across edit/restart: %d head=%d height=%d appearance=%x", response.Code, restored.headMesh, restored.height, restored.appearance) 103 } 104 } 105 106 func TestPlayerAppearanceFailedControlSaveLeavesMemoryAndDatabaseUnchanged(t *testing.T) { 107 for _, failure := range []struct{ name, statement string }{ 108 {"abort", "CREATE TRIGGER deny_appearance BEFORE UPDATE OF head_mesh ON characters BEGIN SELECT RAISE(ABORT, 'appearance'); END"}, 109 {"ignore", "CREATE TRIGGER deny_appearance BEFORE UPDATE OF head_mesh ON characters BEGIN SELECT RAISE(IGNORE); END"}, 110 {"commit", "CREATE TABLE missing_appearance (id INTEGER REFERENCES players(id) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER deny_appearance AFTER UPDATE OF head_mesh ON characters BEGIN INSERT INTO missing_appearance VALUES (999999); END"}, 111 } { 112 t.Run(failure.name, func(t *testing.T) { 113 world := newTestServer(t) 114 postControlForm(world, "/admin/players", url.Values{"name": {"Visitor"}}) 115 if _, err := world.db.Exec(failure.statement); err != nil { 116 t.Fatal(err) 117 } 118 119 response := postControlForm(world, "/admin/players/1", playerFixtureForm("head_mesh: 40680\nheight: 110\n")) 120 restored := newTestInventoryServer(t).characters[1] 121 122 if response.Code != 500 || world.characters[1].headMesh != 0 || world.characters[1].height != 0 || restored.headMesh != 0 || restored.height != 0 { 123 t.Fatal("failed appearance save changed memory or persisted state") 124 } 125 if _, err := world.db.Exec("DROP TRIGGER deny_appearance"); err != nil { 126 t.Fatal(err) 127 } 128 129 response = postControlForm(world, "/admin/players/1", playerFixtureForm("head_mesh: 40680\nheight: 110\n")) 130 restored = newTestInventoryServer(t).characters[1] 131 132 if response.Code != 303 || restored.headMesh != 40680 || restored.height != 110 { 133 t.Fatal("appearance save did not recover after the failed transaction") 134 } 135 }) 136 } 137 } 138 139 func TestPlayerAppearanceRejectsUnrepresentableProfileFields(t *testing.T) { 140 for _, data := range []string{"height: 65536", "height: -1", "head_mesh: 4294967296", "head_mesh: -1"} { 141 t.Run(data, func(t *testing.T) { 142 world := newTestServer(t) 143 postControlForm(world, "/admin/players", url.Values{"name": {"Visitor"}}) 144 145 response := postControlForm(world, "/admin/players/1", playerFixtureForm(data)) 146 restored := newTestInventoryServer(t).characters[1] 147 148 if response.Code != 400 || world.characters[1].headMesh != 0 || world.characters[1].height != 0 || restored.headMesh != 0 || restored.height != 0 { 149 t.Fatal("unrepresentable appearance edit changed the player") 150 } 151 }) 152 } 153 } 154 155 func TestSolitusAppearanceUsesExplicitHeadMeshAndMatchingSkin(t *testing.T) { 156 for _, appearance := range []struct { 157 packed uint16 158 head uint32 159 hands uint32 160 }{ 161 {0x0515, 40682, 8773}, {0x0d15, 40683, 8773}, 162 {0x1515, 40684, 8773}, {0x1d15, 40666, 8773}, 163 {0x2515, 40686, 8773}, {0x2d15, 40687, 8773}, 164 {0x3515, 40688, 8773}, {0x3d15, 40689, 8773}, 165 {0x0595, 40630, 8865}, {0x0d95, 40631, 8865}, 166 {0x1595, 40632, 8865}, {0x1d95, 40633, 8865}, 167 {0x2595, 40634, 8865}, {0x2d95, 40635, 8865}, 168 {0x3595, 40636, 8865}, {0x3d95, 40637, 8865}, 169 } { 170 visitor := &character{id: 1, name: "Visitor", appearance: appearance.packed, headMesh: appearance.head, level: 1} 171 172 packet := characterPacket(visitor) 173 174 if binary.BigEndian.Uint32(packet[159:163]) != appearance.head || string(packet[202:208]) != "hands\x00" || binary.BigEndian.Uint32(packet[208:212]) != appearance.hands { 175 t.Fatalf("appearance %x has incorrect head or skin: %x", appearance.packed, packet) 176 } 177 } 178 }