aorecomp

Anarchy Online classic in the browser

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


server/chat_metadata_test.go (9476B)


      1 package main
      2 
      3 import (
      4 	"bytes"
      5 	"fmt"
      6 	"io"
      7 	"strings"
      8 	"testing"
      9 )
     10 
     11 func TestChatRelaysNativeMetadata(t *testing.T) {
     12 	reference := []byte{0, 0, 0, 195, 80, 0, 0, 0, 2, 0, 0, 0, 0, 0, 3, 'B', 'o', 'b', 0, 4, 'i', 'n', 'f', 'o'}
     13 	voice := []byte{1, 1, 3, 0, 6, 's', 'i', 'm', 'p', 'l', 'e', 0, 3, 'y', 'e', 's'}
     14 	for _, metadata := range [][]byte{nil, reference, voice, append(append([]byte(nil), reference...), voice...)} {
     15 		for _, channel := range []string{"tell", "Global", "Team"} {
     16 			t.Run(fmt.Sprintf("%s/metadata_%d", channel, len(metadata)), func(t *testing.T) {
     17 				world := teamChatWorld(t)
     18 				if channel == "Team" {
     19 					formTestTeam(t, world, 1, 2)
     20 				}
     21 				alice := loginTeamChat(t, world, 1)
     22 				bob := loginTeamChat(t, world, 2)
     23 				if channel == "Team" {
     24 					expectTeamChatGroup(t, alice, 60, 1)
     25 					expectTeamChatGroup(t, bob, 60, 1)
     26 				}
     27 				kind := uint16(65)
     28 				prefix := []byte{0x87, 0, 0, 0, 1}
     29 				blob := metadata
     30 				if channel == "tell" {
     31 					kind = 30
     32 					prefix = []byte{0, 0, 0, 2}
     33 					blob = append([]byte{0}, metadata...)
     34 				} else if channel == "Team" {
     35 					prefix[0] = 2
     36 				}
     37 				body := appendChatString(prefix, "hello")
     38 				body = append(body, byte(len(blob)>>8), byte(len(blob)))
     39 				body = append(body, blob...)
     40 
     41 				if _, err := alice.Write(chatPacket(kind, body)); err != nil {
     42 					t.Fatal(err)
     43 				}
     44 				nameKind, nameBody, nameErr := readChatPacket(bob)
     45 				actualKind, actual, err := readChatPacket(bob)
     46 
     47 				if nameErr != nil || nameKind != 20 || !bytes.Equal(nameBody[:4], []byte{0, 0, 0, 1}) {
     48 					t.Fatalf("sender identity: %d %x %v", nameKind, nameBody, nameErr)
     49 				}
     50 				expected := []byte{0, 0, 0, 1}
     51 				if kind == 65 {
     52 					expected = append(append([]byte(nil), prefix...), expected...)
     53 				}
     54 				expected = append(expected, 0, 5, 'h', 'e', 'l', 'l', 'o', byte(len(blob)>>8), byte(len(blob)))
     55 				expected = append(expected, blob...)
     56 				if err != nil || actualKind != kind || !bytes.Equal(actual, expected) {
     57 					t.Fatalf("metadata relay: %d %x expected %x error %v", actualKind, actual, expected, err)
     58 				}
     59 			})
     60 		}
     61 	}
     62 }
     63 
     64 func TestChatRejectsMalformedMetadata(t *testing.T) {
     65 	reference := []byte{0, 0, 0, 195, 80, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 'x', 0, 0}
     66 	voice := []byte{1, 1, 3, 0, 6, 's', 'i', 'm', 'p', 'l', 'e', 0, 3, 'y', 'e', 's'}
     67 	cases := [][]byte{
     68 		{2, 1}, {255}, {0}, {1},
     69 		append(append([]byte(nil), reference...), reference...),
     70 		append(append([]byte(nil), voice...), voice...),
     71 		append(append([]byte(nil), voice...), reference...),
     72 		append(append([]byte(nil), reference...), 2, 1),
     73 		append(append([]byte(nil), reference...), 255),
     74 		{0, 0, 0, 195, 80, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 0, 0},
     75 	}
     76 	for length := 1; length < len(reference); length++ {
     77 		cases = append(cases, reference[:length])
     78 	}
     79 	for length := 1; length < len(voice); length++ {
     80 		cases = append(cases, voice[:length])
     81 	}
     82 	for index, metadata := range cases {
     83 		for _, kind := range []uint16{30, 65} {
     84 			t.Run(fmt.Sprintf("kind_%d/case_%d", kind, index), func(t *testing.T) {
     85 				world := chatTestServer(t)
     86 				peer, done := startChatTest(t, world, 0)
     87 				if _, err := peer.Write(chatTestAuth(1)); err != nil {
     88 					t.Fatal(err)
     89 				}
     90 				for range 3 {
     91 					if _, _, err := readChatPacket(peer); err != nil {
     92 						t.Fatal(err)
     93 					}
     94 				}
     95 				prefix := []byte{0x87, 0, 0, 0, 1}
     96 				blob := metadata
     97 				if kind == 30 {
     98 					prefix = []byte{0, 0, 0, 2}
     99 					blob = append([]byte{0}, metadata...)
    100 				}
    101 				body := appendChatString(prefix, "hello")
    102 				body = append(body, byte(len(blob)>>8), byte(len(blob)))
    103 				body = append(body, blob...)
    104 
    105 				if _, err := peer.Write(chatPacket(kind, body)); err != nil {
    106 					t.Fatal(err)
    107 				}
    108 				_, _, err := readChatPacket(peer)
    109 
    110 				if err != io.EOF {
    111 					t.Fatalf("malformed metadata accepted: %v", err)
    112 				}
    113 				if ended := <-done; ended == nil {
    114 					t.Fatal("malformed metadata had no disconnect reason")
    115 				}
    116 				history, historyErr := world.chatHistory(1)
    117 				if historyErr != nil || len(history) != 0 {
    118 					t.Fatalf("rejected metadata entered history: %+v %v", history, historyErr)
    119 				}
    120 			})
    121 		}
    122 	}
    123 }
    124 
    125 func TestChatDeliversVoiceWithoutText(t *testing.T) {
    126 	for _, channel := range []string{"tell", "Global", "Team", "vicinity"} {
    127 		t.Run(channel, func(t *testing.T) {
    128 			world := teamChatWorld(t)
    129 			if channel == "Team" {
    130 				formTestTeam(t, world, 1, 2)
    131 			}
    132 			alice := loginTeamChat(t, world, 1)
    133 			bob := loginTeamChat(t, world, 2)
    134 			if channel == "Team" {
    135 				expectTeamChatGroup(t, alice, 60, 1)
    136 				expectTeamChatGroup(t, bob, 60, 1)
    137 			}
    138 			voice := []byte{1, 1, 3, 0, 6, 's', 'i', 'm', 'p', 'l', 'e', 0, 3, 'y', 'e', 's'}
    139 			kind := uint16(65)
    140 			prefix := []byte{0x87, 0, 0, 0, 1}
    141 			blob := voice
    142 			if channel == "tell" || channel == "vicinity" {
    143 				kind = 30
    144 				prefix = []byte{0, 0, 0, 2}
    145 				blob = append([]byte{0}, voice...)
    146 			} else if channel == "Team" {
    147 				prefix[0] = 2
    148 			}
    149 			body := appendChatString(prefix, "")
    150 			body = appendChatString(body, string(blob))
    151 
    152 			if channel == "vicinity" {
    153 				world.sendVicinity(world.clients[1], 0, "", voice)
    154 				kind = 34
    155 			} else if _, err := alice.Write(chatPacket(kind, body)); err != nil {
    156 				t.Fatal(err)
    157 			}
    158 			nameKind, _, nameErr := readChatPacket(bob)
    159 			actualKind, actual, err := readChatPacket(bob)
    160 
    161 			if nameErr != nil || nameKind != 20 {
    162 				t.Fatalf("sender name: %d %v", nameKind, nameErr)
    163 			}
    164 			expected := []byte{0, 0, 0, 1, 0, 0, 0, 17, 0}
    165 			if kind == 65 {
    166 				expected = append(append([]byte(nil), prefix...), 0, 0, 0, 1, 0, 0, 0, 16)
    167 			}
    168 			expected = append(expected, voice...)
    169 			if err != nil || actualKind != kind || !bytes.Equal(actual, expected) {
    170 				t.Fatalf("voice without text: %d %x expected %x error %v", actualKind, actual, expected, err)
    171 			}
    172 		})
    173 	}
    174 }
    175 
    176 func TestChatIgnoresEmptyTextWithoutMetadata(t *testing.T) {
    177 	world := chatTestServer(t)
    178 	alice := loginChatTest(t, world, 1)
    179 	bob := loginChatTest(t, world, 2)
    180 	tell := appendChatString([]byte{0, 0, 0, 2}, "")
    181 	tell = appendChatString(tell, "\x00")
    182 	group := appendChatString([]byte{0x87, 0, 0, 0, 1}, "")
    183 	group = appendChatString(group, "")
    184 
    185 	world.sendVicinity(world.clients[1], 0, "", nil)
    186 	if _, err := alice.Write(chatPacket(30, tell)); err != nil {
    187 		t.Fatal(err)
    188 	}
    189 	if _, err := alice.Write(chatPacket(65, group)); err != nil {
    190 		t.Fatal(err)
    191 	}
    192 
    193 	expectChatPing(t, alice)
    194 	expectChatPing(t, bob)
    195 	history, err := world.chatHistory(1)
    196 	if err != nil || len(history) != 0 {
    197 		t.Fatalf("empty messages entered history: %+v %v", history, err)
    198 	}
    199 }
    200 
    201 func TestChatRejectsUnsupportedTellModes(t *testing.T) {
    202 	for _, blob := range [][]byte{nil, {1}, {2}} {
    203 		t.Run(fmt.Sprintf("blob_%x", blob), func(t *testing.T) {
    204 			world := chatTestServer(t)
    205 			peer, done := startChatTest(t, world, 0)
    206 			if _, err := peer.Write(chatTestAuth(1)); err != nil {
    207 				t.Fatal(err)
    208 			}
    209 			for range 3 {
    210 				if _, _, err := readChatPacket(peer); err != nil {
    211 					t.Fatal(err)
    212 				}
    213 			}
    214 			body := appendChatString([]byte{0, 0, 0, 2}, "hello")
    215 			body = appendChatString(body, string(blob))
    216 
    217 			if _, err := peer.Write(chatPacket(30, body)); err != nil {
    218 				t.Fatal(err)
    219 			}
    220 			_, _, err := readChatPacket(peer)
    221 
    222 			if err != io.EOF {
    223 				t.Fatalf("unsupported tell mode was relayed: %v", err)
    224 			}
    225 			if ended := <-done; ended == nil {
    226 				t.Fatal("missing rejection reason")
    227 			}
    228 		})
    229 	}
    230 }
    231 
    232 func TestChatGroupMetadataFitsDeliveryFrame(t *testing.T) {
    233 	for _, size := range []int{65500, 65501} {
    234 		t.Run(fmt.Sprint(size), func(t *testing.T) {
    235 			world := chatTestServer(t)
    236 			alice, done := startChatTest(t, world, 0)
    237 			if _, err := alice.Write(chatTestAuth(1)); err != nil {
    238 				t.Fatal(err)
    239 			}
    240 			for range 3 {
    241 				if _, _, err := readChatPacket(alice); err != nil {
    242 					t.Fatal(err)
    243 				}
    244 			}
    245 			bob := loginChatTest(t, world, 2)
    246 			metadata := []byte{0, 0, 0, 195, 80, 0, 0, 0, 2, 0, 0, 0, 0}
    247 			metadata = appendChatString(metadata, strings.Repeat("x", size))
    248 			metadata = appendChatString(metadata, "")
    249 			body := appendChatString([]byte{0x87, 0, 0, 0, 1}, "hello")
    250 			body = appendChatString(body, string(metadata))
    251 
    252 			if _, err := alice.Write(chatPacket(65, body)); err != nil {
    253 				t.Fatal(err)
    254 			}
    255 
    256 			if size == 65501 {
    257 				if _, _, err := readChatPacket(alice); err != io.EOF {
    258 					t.Fatalf("oversized group accepted: %v", err)
    259 				}
    260 				if ended := <-done; ended == nil {
    261 					t.Fatal("missing rejection reason")
    262 				}
    263 				expectChatPing(t, bob)
    264 				history, err := world.chatHistory(1)
    265 				if err != nil || len(history) != 0 {
    266 					t.Fatalf("rejected group entered history: %+v %v", history, err)
    267 				}
    268 				return
    269 			}
    270 			if kind, _, err := readChatPacket(bob); err != nil || kind != 20 {
    271 				t.Fatalf("name packet: %d %v", kind, err)
    272 			}
    273 			kind, actual, err := readChatPacket(bob)
    274 			if err != nil || kind != 65 || len(actual) != 65535 || !bytes.Equal(actual[18:], metadata) {
    275 				t.Fatalf("maximum group frame: %d length %d error %v", kind, len(actual), err)
    276 			}
    277 		})
    278 	}
    279 }
    280 
    281 func TestChatVicinityRejectsReplacedSession(t *testing.T) {
    282 	world := chatTestServer(t)
    283 	bob := loginChatTest(t, world, 2)
    284 	previous := &worldClient{id: 1, ready: true}
    285 	metadata := []byte{0, 0, 0, 195, 80, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 4, 'i', 'n', 'f', 'o'}
    286 
    287 	world.sendVicinity(previous, 0, "stale reference", metadata)
    288 
    289 	expectChatPing(t, bob)
    290 	history, err := world.chatHistory(1)
    291 	if err != nil || len(history) != 0 {
    292 		t.Fatalf("stale session entered history: %+v %v", history, err)
    293 	}
    294 }