aorecomp

Anarchy Online classic in the browser

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


server/team_chat_test.go (14331B)


      1 package main
      2 
      3 import (
      4 	"bytes"
      5 	"encoding/binary"
      6 	"fmt"
      7 	"io"
      8 	"net"
      9 	"strings"
     10 	"sync"
     11 	"testing"
     12 	"time"
     13 )
     14 
     15 func teamChatWorld(t *testing.T) *server {
     16 	t.Helper()
     17 	world, _ := newTeamWorld(t)
     18 	return world
     19 }
     20 
     21 func loginTeamChat(t *testing.T, world *server, id uint32) net.Conn {
     22 	t.Helper()
     23 	peer, done := startChatTest(t, world, id)
     24 	peer.SetDeadline(time.Now().Add(30 * time.Second))
     25 	t.Cleanup(func() { peer.Close(); <-done })
     26 	if _, err := peer.Write(chatTestAuth(id)); err != nil {
     27 		t.Fatal(err)
     28 	}
     29 	for _, expected := range []uint16{5, 20, 60} {
     30 		kind, _, err := readChatPacket(peer)
     31 		if err != nil || kind != expected {
     32 			t.Fatalf("team chat login: %d %v", kind, err)
     33 		}
     34 	}
     35 	return peer
     36 }
     37 
     38 func expectTeamChatGroup(t *testing.T, peer net.Conn, kind uint16, id uint32) {
     39 	t.Helper()
     40 	expected := binary.BigEndian.AppendUint32([]byte{2}, id)
     41 	if kind == 60 {
     42 		expected = append(expected, 0, 4, 'T', 'e', 'a', 'm', 0, 0, 0, 0, 0, 0)
     43 	}
     44 	actualKind, body, err := readChatPacket(peer)
     45 	if err != nil || actualKind != kind || !bytes.Equal(body, expected) {
     46 		t.Fatalf("team channel: kind=%d body=%x expected=%d/%x error=%v", actualKind, body, kind, expected, err)
     47 	}
     48 }
     49 
     50 func teamChatRequest(id uint32, message string) []byte {
     51 	body := binary.BigEndian.AppendUint32([]byte{2}, id)
     52 	return chatPacket(65, appendChatString(appendChatString(body, message), ""))
     53 }
     54 
     55 func expectTeamChatMessage(t *testing.T, peer net.Conn, team, sender uint32, message string) {
     56 	t.Helper()
     57 	kind, body, err := readChatPacket(peer)
     58 	if err != nil || kind != 20 || binary.BigEndian.Uint32(body) != sender {
     59 		t.Fatalf("team sender name: %d %x %v", kind, body, err)
     60 	}
     61 	kind, body, err = readChatPacket(peer)
     62 	expected := binary.BigEndian.AppendUint32([]byte{2}, team)
     63 	expected = binary.BigEndian.AppendUint32(expected, sender)
     64 	expected = appendChatString(appendChatString(expected, message), "")
     65 	if err != nil || kind != 65 || !bytes.Equal(body, expected) {
     66 		t.Fatalf("team message: %d %x expected=%x error=%v", kind, body, expected, err)
     67 	}
     68 }
     69 
     70 func expectChatPing(t *testing.T, peer net.Conn) {
     71 	t.Helper()
     72 	if _, err := peer.Write(chatPacket(100, []byte{0, 1, 'x'})); err != nil {
     73 		t.Fatal(err)
     74 	}
     75 	kind, body, err := readChatPacket(peer)
     76 	if err != nil || kind != 100 || !bytes.Equal(body, []byte{0, 1, 'x'}) {
     77 		t.Fatalf("unexpected delivery before ping: %d %x %v", kind, body, err)
     78 	}
     79 }
     80 
     81 func TestTeamChatJoinsOnlyAfterAcceptedMembership(t *testing.T) {
     82 	world := teamChatWorld(t)
     83 	alice := loginTeamChat(t, world, 1)
     84 	bob := loginTeamChat(t, world, 2)
     85 	outsider := loginTeamChat(t, world, 3)
     86 
     87 	world.handleTeamAction(world.clients[1], teamRequest(1, 26, 2, 0))
     88 	expectChatPing(t, alice)
     89 	expectChatPing(t, bob)
     90 	world.handleTeamAction(world.clients[2], teamRequest(2, 28, 1, 1))
     91 
     92 	team := world.characters[1].team.id
     93 	expectTeamChatGroup(t, alice, 60, team)
     94 	expectTeamChatGroup(t, bob, 60, team)
     95 	expectChatPing(t, outsider)
     96 }
     97 
     98 func TestTeamChatCrossesZonesAndKeepsHistoryPrivate(t *testing.T) {
     99 	world := teamChatWorld(t)
    100 	formTestTeam(t, world, 1, 2)
    101 	formTestTeam(t, world, 3, 4)
    102 	team := world.characters[1].team.id
    103 	world.characters[2].playfield = 1183
    104 	alice := loginTeamChat(t, world, 1)
    105 	bob := loginTeamChat(t, world, 2)
    106 	outsider := loginTeamChat(t, world, 3)
    107 	expectTeamChatGroup(t, alice, 60, team)
    108 	expectTeamChatGroup(t, bob, 60, team)
    109 	expectTeamChatGroup(t, outsider, 60, world.characters[3].team.id)
    110 
    111 	alice.Write(teamChatRequest(team, "h\xe9 team"))
    112 
    113 	expectTeamChatMessage(t, alice, team, 1, "h\xe9 team")
    114 	expectTeamChatMessage(t, bob, team, 1, "h\xe9 team")
    115 	expectChatPing(t, outsider)
    116 	for _, id := range []uint32{1, 2, 3, 4} {
    117 		history, err := world.chatHistory(id)
    118 		if err != nil {
    119 			t.Fatal(err)
    120 		}
    121 		if id <= 2 {
    122 			if len(history) != 1 || history[0].Channel != "Team" || history[0].SenderID != 1 || history[0].Message != "h\u00e9 team" {
    123 				t.Fatalf("member %d history: %+v", id, history)
    124 			}
    125 		} else if len(history) != 0 {
    126 			t.Fatalf("team history leaked to %d: %+v", id, history)
    127 		}
    128 	}
    129 }
    130 
    131 func TestTeamChatRejectsForgedAndDepartedChannelsWithoutDisconnecting(t *testing.T) {
    132 	world := teamChatWorld(t)
    133 	formTestTeam(t, world, 1, 2)
    134 	team := world.characters[1].team.id
    135 	alice := loginTeamChat(t, world, 1)
    136 	bob := loginTeamChat(t, world, 2)
    137 	outsider := loginTeamChat(t, world, 3)
    138 	expectTeamChatGroup(t, alice, 60, team)
    139 	expectTeamChatGroup(t, bob, 60, team)
    140 
    141 	outsider.Write(teamChatRequest(team, "forged"))
    142 	expectChatPing(t, outsider)
    143 	alice.Write(teamChatRequest(team+1, "wrong team"))
    144 	expectChatPing(t, alice)
    145 	world.handleTeamAction(world.clients[2], teamRequest(2, 24, 0, 0))
    146 	expectTeamChatGroup(t, alice, 61, team)
    147 	expectTeamChatGroup(t, bob, 61, team)
    148 	bob.Write(teamChatRequest(team, "late send"))
    149 
    150 	expectChatPing(t, bob)
    151 	expectChatPing(t, alice)
    152 	expectChatPing(t, outsider)
    153 	for _, id := range []uint32{1, 2, 3} {
    154 		history, err := world.chatHistory(id)
    155 		if err != nil || len(history) != 0 {
    156 			t.Fatalf("rejected message history for %d: %+v %v", id, history, err)
    157 		}
    158 	}
    159 }
    160 
    161 func TestTeamChatMalformedMessagesCloseConnection(t *testing.T) {
    162 	for _, body := range [][]byte{
    163 		{2, 0, 0, 0},
    164 		{2, 0, 0, 0, 1, 0, 2, 'h', 'i'},
    165 		appendChatString(appendChatString([]byte{2, 0, 0, 0, 1}, "bad\x00text"), ""),
    166 		appendChatString(appendChatString([]byte{2, 0, 0, 0, 1}, strings.Repeat("x", 4097)), ""),
    167 	} {
    168 		t.Run(fmt.Sprintf("length_%d", len(body)), func(t *testing.T) {
    169 			world := teamChatWorld(t)
    170 			peer := loginTeamChat(t, world, 1)
    171 
    172 			peer.Write(chatPacket(65, body))
    173 			_, _, err := readChatPacket(peer)
    174 
    175 			if err != io.EOF {
    176 				t.Fatalf("malformed team message did not close promptly: %v", err)
    177 			}
    178 		})
    179 	}
    180 }
    181 
    182 func TestTeamChatRecipientEligibilityFollowsMembershipAndConnection(t *testing.T) {
    183 	for _, scenario := range []string{"dead", "loading", "far away", "world offline", "chat offline"} {
    184 		t.Run(scenario, func(t *testing.T) {
    185 			world := teamChatWorld(t)
    186 			formTestTeam(t, world, 1, 2)
    187 			team := world.characters[1].team.id
    188 			alice := loginTeamChat(t, world, 1)
    189 			bob := loginTeamChat(t, world, 2)
    190 			expectTeamChatGroup(t, alice, 60, team)
    191 			expectTeamChatGroup(t, bob, 60, team)
    192 			switch scenario {
    193 			case "dead":
    194 				world.characters[2].health = 0
    195 			case "loading":
    196 				world.clients[2].ready = false
    197 				world.clients[2].loading = true
    198 			case "far away":
    199 				world.characters[2].position = [3]float32{10000, 0, 10000}
    200 			case "world offline":
    201 				delete(world.clients, 2)
    202 				world.publishChatPerson(world.characters[2])
    203 			case "chat offline":
    204 				world.chatService.call(chatLeave{readChatState(t, world).clients[2]})
    205 			}
    206 
    207 			alice.Write(teamChatRequest(team, scenario))
    208 			expectTeamChatMessage(t, alice, team, 1, scenario)
    209 
    210 			if scenario != "world offline" && scenario != "chat offline" {
    211 				expectTeamChatMessage(t, bob, team, 1, scenario)
    212 			} else {
    213 				bob.SetReadDeadline(time.Now().Add(20 * time.Millisecond))
    214 				kind, body, err := readChatPacket(bob)
    215 				if timeout, ok := err.(net.Error); err != io.EOF && (!ok || !timeout.Timeout()) {
    216 					t.Fatalf("offline recipient delivery: %d %x %v", kind, body, err)
    217 				}
    218 				history, err := world.chatHistory(2)
    219 				if err != nil || len(history) != 0 {
    220 					t.Fatalf("offline recipient history: %+v %v", history, err)
    221 				}
    222 			}
    223 		})
    224 	}
    225 }
    226 
    227 func TestTeamChatDeclineAndFailedAcceptanceDoNotAnnounceChannel(t *testing.T) {
    228 	for _, scenario := range []string{"decline", "failed commit"} {
    229 		t.Run(scenario, func(t *testing.T) {
    230 			world := teamChatWorld(t)
    231 			alice := loginTeamChat(t, world, 1)
    232 			bob := loginTeamChat(t, world, 2)
    233 			world.handleTeamAction(world.clients[1], teamRequest(1, 26, 2, 0))
    234 			accepted := uint32(0)
    235 			if scenario == "failed commit" {
    236 				accepted = 1
    237 				failDatabaseWrites(t, world)
    238 			}
    239 
    240 			err := world.handleTeamAction(world.clients[2], teamRequest(2, 28, 1, accepted))
    241 
    242 			if (err != nil) != (scenario == "failed commit") || world.characters[1].team != nil || world.characters[2].team != nil {
    243 				t.Fatalf("uncommitted team membership: %v", err)
    244 			}
    245 			expectChatPing(t, alice)
    246 			expectChatPing(t, bob)
    247 		})
    248 	}
    249 }
    250 
    251 func TestTeamChatLeadershipKickAndDissolutionUpdateOnlyAffectedChannels(t *testing.T) {
    252 	world := teamChatWorld(t)
    253 	formTestTeam(t, world, 1, 2)
    254 	formTestTeam(t, world, 1, 3)
    255 	team := world.characters[1].team.id
    256 	alice := loginTeamChat(t, world, 1)
    257 	bob := loginTeamChat(t, world, 2)
    258 	carol := loginTeamChat(t, world, 3)
    259 	for _, peer := range []net.Conn{alice, bob, carol} {
    260 		expectTeamChatGroup(t, peer, 60, team)
    261 	}
    262 
    263 	world.handleTeamAction(world.clients[1], teamRequest(1, 25, 2, 0))
    264 	for _, peer := range []net.Conn{alice, bob, carol} {
    265 		expectChatPing(t, peer)
    266 	}
    267 	world.handleTeamAction(world.clients[2], teamRequest(2, 22, 3, 0))
    268 	expectTeamChatGroup(t, carol, 61, team)
    269 	bob.Write(teamChatRequest(team, "remaining pair"))
    270 	expectTeamChatMessage(t, alice, team, 2, "remaining pair")
    271 	expectTeamChatMessage(t, bob, team, 2, "remaining pair")
    272 	expectChatPing(t, carol)
    273 	world.handleTeamAction(world.clients[1], teamRequest(1, 24, 0, 0))
    274 	expectTeamChatGroup(t, alice, 61, team)
    275 	expectTeamChatGroup(t, bob, 61, team)
    276 	formTestTeam(t, world, 1, 3)
    277 
    278 	newTeam := world.characters[1].team.id
    279 	if newTeam == team {
    280 		t.Fatal("dissolved team channel was reused")
    281 	}
    282 	expectTeamChatGroup(t, alice, 60, newTeam)
    283 	expectTeamChatGroup(t, carol, 60, newTeam)
    284 	alice.Write(teamChatRequest(team, "stale old channel"))
    285 	expectChatPing(t, alice)
    286 	expectChatPing(t, bob)
    287 	expectChatPing(t, carol)
    288 }
    289 
    290 func TestTeamChatFailedRosterCommitPreservesChannelAccess(t *testing.T) {
    291 	world := teamChatWorld(t)
    292 	formTestTeam(t, world, 1, 2)
    293 	team := world.characters[1].team.id
    294 	alice := loginTeamChat(t, world, 1)
    295 	bob := loginTeamChat(t, world, 2)
    296 	expectTeamChatGroup(t, alice, 60, team)
    297 	expectTeamChatGroup(t, bob, 60, team)
    298 	failDatabaseWrites(t, world)
    299 
    300 	err := world.handleTeamAction(world.clients[2], teamRequest(2, 24, 0, 0))
    301 	alice.Write(teamChatRequest(team, "still members"))
    302 
    303 	if err == nil || world.characters[2].team == nil {
    304 		t.Fatalf("failed departure changed membership: %v", err)
    305 	}
    306 	expectTeamChatMessage(t, alice, team, 1, "still members")
    307 	expectTeamChatMessage(t, bob, team, 1, "still members")
    308 }
    309 
    310 func TestTeamChatRestartRestoresMembershipAndOnlyReceivedHistory(t *testing.T) {
    311 	world := teamChatWorld(t)
    312 	formTestTeam(t, world, 1, 2)
    313 	team := world.characters[1].team.id
    314 	alice := loginTeamChat(t, world, 1)
    315 	bob := loginTeamChat(t, world, 2)
    316 	expectTeamChatGroup(t, alice, 60, team)
    317 	expectTeamChatGroup(t, bob, 60, team)
    318 	alice.Write(teamChatRequest(team, "before restart"))
    319 	expectTeamChatMessage(t, alice, team, 1, "before restart")
    320 	expectTeamChatMessage(t, bob, team, 1, "before restart")
    321 
    322 	restarted := newTestInventoryServer(t)
    323 	restarted.clients[2] = &worldClient{id: 2, ready: true}
    324 	reconnected := loginTeamChat(t, restarted, 2)
    325 
    326 	expectTeamChatGroup(t, reconnected, 60, team)
    327 	kind, body, err := readChatPacket(reconnected)
    328 	if err != nil || kind != 36 || !bytes.Contains(body, []byte("[Team] Member1: before restart")) {
    329 		t.Fatalf("private restored history: %d %s %v", kind, body, err)
    330 	}
    331 	expectChatPing(t, reconnected)
    332 	history, err := restarted.chatHistory(3)
    333 	if err != nil || len(history) != 0 {
    334 		t.Fatalf("history reached outsider on restart: %+v %v", history, err)
    335 	}
    336 }
    337 
    338 func TestTeamChatNewMemberDoesNotInheritEarlierHistory(t *testing.T) {
    339 	world := teamChatWorld(t)
    340 	formTestTeam(t, world, 1, 2)
    341 	team := world.characters[1].team.id
    342 	alice := loginTeamChat(t, world, 1)
    343 	expectTeamChatGroup(t, alice, 60, team)
    344 	alice.Write(teamChatRequest(team, "before recruitment"))
    345 	expectTeamChatMessage(t, alice, team, 1, "before recruitment")
    346 
    347 	formTestTeam(t, world, 1, 3)
    348 	carol := loginTeamChat(t, world, 3)
    349 
    350 	expectTeamChatGroup(t, carol, 60, team)
    351 	expectChatPing(t, carol)
    352 	for _, id := range []uint32{2, 3} {
    353 		history, err := world.chatHistory(id)
    354 		if err != nil || len(history) != 0 {
    355 			t.Fatalf("unreceived history reached %d: %+v %v", id, history, err)
    356 		}
    357 	}
    358 }
    359 
    360 func TestTeamChatConcurrentDepartureCannotReceiveAfterPart(t *testing.T) {
    361 	world := teamChatWorld(t)
    362 	formTestTeam(t, world, 1, 2)
    363 	formTestTeam(t, world, 1, 3)
    364 	team := world.characters[1].team.id
    365 	clients := []*worldClient{world.clients[1], world.clients[2]}
    366 	startTestService(t, world)
    367 	zone, err := world.world.owner(1)
    368 	if err != nil {
    369 		t.Fatal(err)
    370 	}
    371 	alice := loginTeamChat(t, world, 1)
    372 	bob := loginTeamChat(t, world, 2)
    373 	expectTeamChatGroup(t, alice, 60, team)
    374 	expectTeamChatGroup(t, bob, 60, team)
    375 	for iteration := range 12 {
    376 		if iteration != 0 {
    377 			if err := zone.call(zonePacket{clients[0], teamRequest(1, 26, 2, 0)}); err != nil {
    378 				t.Fatal(err)
    379 			}
    380 			if err := zone.call(zonePacket{clients[1], teamRequest(2, 28, 1, 1)}); err != nil {
    381 				t.Fatal(err)
    382 			}
    383 			expectTeamChatGroup(t, bob, 60, team)
    384 		}
    385 		message := fmt.Sprintf("concurrent %d", iteration)
    386 		start := make(chan struct{})
    387 		var operations sync.WaitGroup
    388 		operations.Add(2)
    389 		errors := make(chan error, 2)
    390 
    391 		go func() {
    392 			defer operations.Done()
    393 			<-start
    394 			_, err := alice.Write(teamChatRequest(team, message))
    395 			errors <- err
    396 		}()
    397 		go func() {
    398 			defer operations.Done()
    399 			<-start
    400 			errors <- zone.call(zonePacket{clients[1], teamRequest(2, 24, 0, 0)})
    401 		}()
    402 		close(start)
    403 		operations.Wait()
    404 		expectTeamChatMessage(t, alice, team, 1, message)
    405 		bob.Write(chatPacket(100, nil))
    406 
    407 		for range 2 {
    408 			if err := <-errors; err != nil {
    409 				t.Fatal(err)
    410 			}
    411 		}
    412 		parted, delivered := false, false
    413 		for {
    414 			kind, body, err := readChatPacket(bob)
    415 			if err != nil {
    416 				t.Fatal(err)
    417 			}
    418 			if kind == 100 {
    419 				break
    420 			}
    421 			switch kind {
    422 			case 20, 65:
    423 				if parted {
    424 					t.Fatal("private delivery followed channel departure")
    425 				}
    426 				delivered = kind == 65
    427 			case 61:
    428 				if parted || !bytes.Equal(body, binary.BigEndian.AppendUint32([]byte{2}, team)) {
    429 					t.Fatalf("unexpected departure: %x", body)
    430 				}
    431 				parted = true
    432 			default:
    433 				t.Fatalf("unexpected packet: %d %x", kind, body)
    434 			}
    435 		}
    436 		if !parted {
    437 			t.Fatal("missing team departure")
    438 		}
    439 		history, err := world.chatHistory(2)
    440 		if err != nil {
    441 			t.Fatal(err)
    442 		}
    443 		found := false
    444 		for _, entry := range history {
    445 			found = found || entry.Message == message
    446 		}
    447 		if found != delivered {
    448 			t.Fatal("history does not match serialized private delivery")
    449 		}
    450 	}
    451 }