server/social_actions_test.go (8969B)
1 package main 2 3 import ( 4 "bytes" 5 "encoding/binary" 6 "encoding/hex" 7 "net" 8 "testing" 9 "time" 10 ) 11 12 func socialActionRequest(action uint32) []byte { 13 // Captured /wave command with the handler's normalized IIR prefix. 14 body, _ := hex.DecodeString("3b2907710000c350000000010000000100000000000000010000003d") 15 binary.BigEndian.PutUint32(body[24:28], action) 16 return body 17 } 18 19 func TestSocialActionNativeDispatchDoesNotReplayGesturesToLateObservers(t *testing.T) { 20 world := newCheckpointWorld(t) 21 world.newCharacter().playfield = 950 22 world.newCharacter().playfield = 950 23 startTestService(t, world) 24 client := connectZoningClient(t, world, true) 25 observer := &combatTestConn{} 26 observerClient := &worldClient{id: 2, players: map[uint32]bool{1: true}, target: true, ready: true, conn: observer} 27 callTestZone(t, world, 1, zoneTestCommand(func(state *server) error { 28 state.clients[2] = observerClient 29 return nil 30 })) 31 captured, _ := hex.DecodeString("dfdf000a0001002900000001000000023b2907710000c350000000010100000000000000010000003d000000") 32 ping := systemPacket(1, nil) 33 binary.BigEndian.PutUint16(ping[2:4], 11) 34 35 if _, err := client.Write(captured); err != nil { 36 t.Fatal(err) 37 } 38 client.Write(ping) 39 acknowledgement, err := readPacket(client) 40 if err != nil { 41 t.Fatal(err) 42 } 43 reply, err := readPacket(client) 44 if err != nil { 45 t.Fatal(err) 46 } 47 late, peer := net.Pipe() 48 defer late.Close() 49 late.SetDeadline(time.Now().Add(5 * time.Second)) 50 go func() { defer peer.Close(); world.serveWorldConnection(peer) }() 51 cookie, _ := hex.DecodeString("dfdf00010001002000000003000000020000001b000000030000000000000000") 52 late.Write(cookie) 53 var keys []uint32 54 for { 55 packet, err := readPacket(late) 56 if err != nil { 57 t.Fatal(err) 58 } 59 key := binary.BigEndian.Uint32(packet[16:20]) 60 keys = append(keys, key) 61 if key == 0x5f52412e { 62 break 63 } 64 } 65 66 if binary.BigEndian.Uint32(acknowledgement[16:20]) != 0x3b290771 || binary.BigEndian.Uint32(acknowledgement[29:33]) != 1 || binary.BigEndian.Uint32(acknowledgement[33:37]) != 1 { 67 t.Fatalf("owner did not complete the native command: %x", acknowledgement) 68 } 69 if binary.BigEndian.Uint16(reply[2:4]) != 11 { 70 t.Fatalf("owner received an extra gesture response instead of its ping: %x", reply) 71 } 72 observerClient.mutex.Lock() 73 received := len(observer.packets) != 0 && binary.BigEndian.Uint32(observer.packets[0][16:20]) == 0x3b290771 74 observerClient.mutex.Unlock() 75 if !received { 76 t.Fatal("native request did not reach the social-action handler") 77 } 78 for _, key := range keys { 79 if key == 0x3b290771 { 80 t.Fatal("late observer replayed a transient gesture") 81 } 82 } 83 } 84 85 func TestSocialActionCompletesNativeCommandsWithoutDatabaseWrites(t *testing.T) { 86 world := newCheckpointWorld(t) 87 owner, observer := &combatTestConn{}, &combatTestConn{} 88 client := &worldClient{id: 1, target: true, ready: true, conn: owner} 89 world.clients[1] = client 90 world.characters[2] = &character{id: 2, playfield: 950, health: 100} 91 world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, target: true, ready: true, conn: observer} 92 auditCheckpointWrites(t, world) 93 94 for index, action := range []uint32{1, 61, 61, 63} { 95 body := socialActionRequest(action) 96 binary.BigEndian.PutUint32(body[20:24], []uint32{0, 1, 2, 0xffffffff}[index]) 97 if err := world.handleSocialAction(client, body); err != nil { 98 t.Fatal(err) 99 } 100 } 101 102 if len(owner.packets) != 4 || len(observer.packets) != 4 { 103 t.Fatalf("gesture replies owner=%d observer=%d", len(owner.packets), len(observer.packets)) 104 } 105 for index, action := range []uint32{1, 61, 61, 63} { 106 expected := socialActionRequest(action) 107 binary.BigEndian.PutUint32(expected[16:20], 1) 108 binary.BigEndian.PutUint32(expected[20:24], []uint32{0, 1, 2, 0xffffffff}[index]) 109 for _, packet := range [][]byte{owner.packets[index], observer.packets[index]} { 110 if len(packet) != 44 || binary.BigEndian.Uint16(packet[6:8]) != 41 || 111 binary.BigEndian.Uint32(packet[16:20]) != 0x3b290771 || binary.BigEndian.Uint32(packet[24:28]) != 1 || 112 packet[28] != 0 || !bytes.Equal(packet[29:41], expected[16:28]) { 113 t.Fatalf("native gesture fields: %x", packet) 114 } 115 } 116 } 117 if writes := checkpointWrites(t, world); len(writes) != 0 { 118 t.Fatalf("gesture rewrote durable state: %v", writes) 119 } 120 } 121 122 func TestSocialActionRejectsMalformedUnauthorizedAndUnsupportedRequests(t *testing.T) { 123 for _, scenario := range []string{"short prefix", "short command", "trailing data", "foreign owner", "wrong type", "wrong pass-on", "verified response", "unknown verification", "zero action", "unknown action", "replaced", "unready", "loading", "dead", "unsupported client"} { 124 t.Run(scenario, func(t *testing.T) { 125 world, owner := newCombatWorld(t) 126 client := world.clients[1] 127 observer := &combatTestConn{} 128 world.characters[2] = &character{id: 2, playfield: 950, health: 100} 129 world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, target: true, ready: true, conn: observer} 130 body := socialActionRequest(61) 131 malformed := false 132 switch scenario { 133 case "short prefix": 134 body, malformed = body[:12], true 135 case "short command": 136 body, malformed = body[:27], true 137 case "trailing data": 138 body, malformed = append(body, 0), true 139 case "foreign owner": 140 binary.BigEndian.PutUint32(body[8:12], 2) 141 case "wrong type": 142 binary.BigEndian.PutUint32(body[4:8], 51035) 143 case "wrong pass-on": 144 binary.BigEndian.PutUint32(body[12:16], 0) 145 case "verified response": 146 binary.BigEndian.PutUint32(body[16:20], 1) 147 case "unknown verification": 148 binary.BigEndian.PutUint32(body[16:20], 6) 149 case "zero action": 150 binary.BigEndian.PutUint32(body[24:28], 0) 151 case "unknown action": 152 binary.BigEndian.PutUint32(body[24:28], 64) 153 case "replaced": 154 world.clients[1] = &worldClient{id: 1} 155 case "unready": 156 client.ready = false 157 case "loading": 158 client.loading = true 159 case "dead": 160 world.characters[1].health = 0 161 case "unsupported client": 162 client.target = false 163 } 164 before := world.characters[1].vitals() 165 166 err := world.handleSocialAction(client, body) 167 168 rejected := scenario == "unready" || scenario == "loading" || scenario == "dead" 169 expectedReplies := 0 170 if rejected { 171 expectedReplies = 1 172 } 173 if (err != nil) != malformed || len(owner.packets) != expectedReplies || len(observer.packets) != 0 || world.characters[1].vitals() != before { 174 t.Fatalf("invalid gesture changed state: error=%v owner=%d observer=%d", err, len(owner.packets), len(observer.packets)) 175 } 176 if rejected && (binary.BigEndian.Uint32(owner.packets[0][29:33]) != 2 || binary.BigEndian.Uint32(owner.packets[0][33:37]) != 1) { 177 t.Fatal("rejected gesture did not finish its owner's pending command") 178 } 179 }) 180 } 181 } 182 183 func TestSocialActionExcludesOtherPlayfieldsAndUnreadyObservers(t *testing.T) { 184 for _, scenario := range []string{"other playfield", "unready observer"} { 185 t.Run(scenario, func(t *testing.T) { 186 world, owner := newCombatWorld(t) 187 observer := &combatTestConn{} 188 world.characters[2] = &character{id: 2, playfield: 950, health: 100} 189 world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, target: true, ready: true, conn: observer} 190 if scenario == "other playfield" { 191 world.characters[2].playfield = 705 192 } else { 193 world.clients[2].ready = false 194 } 195 196 err := world.handleSocialAction(world.clients[1], socialActionRequest(61)) 197 198 if err != nil || len(owner.packets) != 1 || len(observer.packets) != 0 { 199 t.Fatal("gesture reached an unavailable observer") 200 } 201 }) 202 } 203 } 204 205 func TestSocialActionKeepsPostureAndHeldMovement(t *testing.T) { 206 world, _ := newCombatWorld(t) 207 player, client := world.characters[1], world.clients[1] 208 player.sitting, player.running = true, true 209 world.characters[client.id].movement.forward, world.characters[client.id].movement.strafe, world.characters[client.id].movement.turn = 1, 1, 1 210 211 err := world.handleSocialAction(client, socialActionRequest(61)) 212 213 if err != nil || !player.sitting || !player.running || world.characters[client.id].movement.forward != 1 || world.characters[client.id].movement.strafe == 0 || world.characters[client.id].movement.turn == 0 { 214 t.Fatal("gesture changed posture or held movement") 215 } 216 } 217 218 func TestSocialActionDoesNotInterruptAnAdmittedNano(t *testing.T) { 219 world, _ := newNanoCastingWorld(t) 220 client := world.clients[1] 221 if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1000000, 53019, 29618})[20:]); err != nil { 222 t.Fatal(err) 223 } 224 if client.nanoCast == nil { 225 t.Fatal("cast was not admitted") 226 } 227 deadline := client.nanoCast.readyAt 228 229 if err := world.handleSocialAction(client, socialActionRequest(61)); err != nil { 230 t.Fatal(err) 231 } 232 world.advanceNanos(deadline.Add(time.Nanosecond)) 233 234 if world.characters[1000000].health >= 100 || world.characters[1].nano != 21 || client.nanoCast != nil { 235 t.Fatal("gesture changed the admitted nano outcome") 236 } 237 }