get.pme.sh/pnats@v0.0.0-20240304004023-26bb5a137ed0/test/proto_test.go (about) 1 // Copyright 2012-2020 The NATS Authors 2 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // you may not use this file except in compliance with the License. 4 // You may obtain a copy of the License at 5 // 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package test 15 16 import ( 17 "encoding/json" 18 "testing" 19 "time" 20 21 "get.pme.sh/pnats/server" 22 ) 23 24 const PROTO_TEST_PORT = 9922 25 26 func runProtoServer() *server.Server { 27 opts := DefaultTestOptions 28 opts.Port = PROTO_TEST_PORT 29 opts.MaxControlLine = 256 30 opts.NoSystemAccount = true 31 return RunServer(&opts) 32 } 33 34 func TestProtoBasics(t *testing.T) { 35 s := runProtoServer() 36 defer s.Shutdown() 37 38 c := createClientConn(t, "127.0.0.1", PROTO_TEST_PORT) 39 defer c.Close() 40 41 send, expect := setupConn(t, c) 42 expectMsgs := expectMsgsCommand(t, expect) 43 44 // Ping 45 send("PING\r\n") 46 expect(pongRe) 47 48 // Single Msg 49 send("SUB foo 1\r\nPUB foo 5\r\nhello\r\n") 50 matches := expectMsgs(1) 51 checkMsg(t, matches[0], "foo", "1", "", "5", "hello") 52 53 // 2 Messages 54 send("SUB * 2\r\nPUB foo 2\r\nok\r\n") 55 matches = expectMsgs(2) 56 // Could arrive in any order 57 checkMsg(t, matches[0], "foo", "", "", "2", "ok") 58 checkMsg(t, matches[1], "foo", "", "", "2", "ok") 59 } 60 61 func TestProtoErr(t *testing.T) { 62 s := runProtoServer() 63 defer s.Shutdown() 64 65 c := createClientConn(t, "127.0.0.1", PROTO_TEST_PORT) 66 defer c.Close() 67 68 send, expect := setupConn(t, c) 69 70 // Make sure we get an error on bad proto 71 send("ZZZ") 72 expect(errRe) 73 } 74 75 func TestUnsubMax(t *testing.T) { 76 s := runProtoServer() 77 defer s.Shutdown() 78 79 c := createClientConn(t, "127.0.0.1", PROTO_TEST_PORT) 80 defer c.Close() 81 82 send, expect := setupConn(t, c) 83 expectMsgs := expectMsgsCommand(t, expect) 84 85 send("SUB foo 22\r\n") 86 send("UNSUB 22 2\r\n") 87 for i := 0; i < 100; i++ { 88 send("PUB foo 2\r\nok\r\n") 89 } 90 91 time.Sleep(50 * time.Millisecond) 92 93 matches := expectMsgs(2) 94 checkMsg(t, matches[0], "foo", "22", "", "2", "ok") 95 checkMsg(t, matches[1], "foo", "22", "", "2", "ok") 96 } 97 98 func TestQueueSub(t *testing.T) { 99 s := runProtoServer() 100 defer s.Shutdown() 101 102 c := createClientConn(t, "127.0.0.1", PROTO_TEST_PORT) 103 defer c.Close() 104 105 send, expect := setupConn(t, c) 106 expectMsgs := expectMsgsCommand(t, expect) 107 108 sent := 100 109 send("SUB foo qgroup1 22\r\n") 110 send("SUB foo qgroup1 32\r\n") 111 for i := 0; i < sent; i++ { 112 send("PUB foo 2\r\nok\r\n") 113 } 114 // Wait for responses 115 time.Sleep(250 * time.Millisecond) 116 117 matches := expectMsgs(sent) 118 sids := make(map[string]int) 119 for _, m := range matches { 120 sids[string(m[sidIndex])]++ 121 } 122 if len(sids) != 2 { 123 t.Fatalf("Expected only 2 sids, got %d\n", len(sids)) 124 } 125 for k, c := range sids { 126 if c < 35 { 127 t.Fatalf("Expected ~50 (+-15) msgs for sid:'%s', got %d\n", k, c) 128 } 129 } 130 } 131 132 func TestMultipleQueueSub(t *testing.T) { 133 s := runProtoServer() 134 defer s.Shutdown() 135 136 c := createClientConn(t, "127.0.0.1", PROTO_TEST_PORT) 137 defer c.Close() 138 139 send, expect := setupConn(t, c) 140 expectMsgs := expectMsgsCommand(t, expect) 141 142 sent := 100 143 send("SUB foo g1 1\r\n") 144 send("SUB foo g1 2\r\n") 145 send("SUB foo g2 3\r\n") 146 send("SUB foo g2 4\r\n") 147 148 for i := 0; i < sent; i++ { 149 send("PUB foo 2\r\nok\r\n") 150 } 151 // Wait for responses 152 time.Sleep(250 * time.Millisecond) 153 154 matches := expectMsgs(sent * 2) 155 sids := make(map[string]int) 156 for _, m := range matches { 157 sids[string(m[sidIndex])]++ 158 } 159 if len(sids) != 4 { 160 t.Fatalf("Expected 4 sids, got %d\n", len(sids)) 161 } 162 for k, c := range sids { 163 if c < 35 { 164 t.Fatalf("Expected ~50 (+-15) msgs for '%s', got %d\n", k, c) 165 } 166 } 167 } 168 169 func TestPubToArgState(t *testing.T) { 170 s := runProtoServer() 171 defer s.Shutdown() 172 173 c := createClientConn(t, "127.0.0.1", PROTO_TEST_PORT) 174 defer c.Close() 175 176 send, expect := setupConn(t, c) 177 178 send("PUBS foo 2\r\nok\r\n") 179 expect(errRe) 180 } 181 182 func TestSubToArgState(t *testing.T) { 183 s := runProtoServer() 184 defer s.Shutdown() 185 186 c := createClientConn(t, "127.0.0.1", PROTO_TEST_PORT) 187 defer c.Close() 188 189 send, expect := setupConn(t, c) 190 191 send("SUBZZZ foo 1\r\n") 192 expect(errRe) 193 } 194 195 // Issue #63 196 func TestProtoCrash(t *testing.T) { 197 s := runProtoServer() 198 defer s.Shutdown() 199 200 c := createClientConn(t, "127.0.0.1", PROTO_TEST_PORT) 201 defer c.Close() 202 203 send, expect := sendCommand(t, c), expectCommand(t, c) 204 205 checkInfoMsg(t, c) 206 207 send("CONNECT {\"verbose\":true,\"tls_required\":false,\"user\":\"test\",\"pedantic\":true,\"pass\":\"password\"}") 208 209 time.Sleep(100 * time.Millisecond) 210 211 send("\r\n") 212 expect(okRe) 213 } 214 215 // Issue #136 216 func TestDuplicateProtoSub(t *testing.T) { 217 s := runProtoServer() 218 defer s.Shutdown() 219 220 c := createClientConn(t, "127.0.0.1", PROTO_TEST_PORT) 221 defer c.Close() 222 223 send, expect := setupConn(t, c) 224 225 send("PING\r\n") 226 expect(pongRe) 227 228 send("SUB foo 1\r\n") 229 230 send("SUB foo 1\r\n") 231 232 ns := 0 233 234 for i := 0; i < 5; i++ { 235 ns = int(s.NumSubscriptions()) 236 if ns == 0 { 237 time.Sleep(50 * time.Millisecond) 238 } else { 239 break 240 } 241 } 242 243 if ns != 1 { 244 t.Fatalf("Expected 1 subscription, got %d\n", ns) 245 } 246 } 247 248 func TestIncompletePubArg(t *testing.T) { 249 s := runProtoServer() 250 defer s.Shutdown() 251 252 c := createClientConn(t, "127.0.0.1", PROTO_TEST_PORT) 253 defer c.Close() 254 send, expect := setupConn(t, c) 255 256 size := 10000 257 goodBuf := "" 258 for i := 0; i < size; i++ { 259 goodBuf += "A" 260 } 261 goodBuf += "\r\n" 262 263 badSize := 3371 264 badBuf := "" 265 for i := 0; i < badSize; i++ { 266 badBuf += "B" 267 } 268 // Message is corrupted and since we are still reading from client, 269 // next PUB accidentally becomes part of the payload of the 270 // incomplete message thus breaking the protocol. 271 badBuf2 := "" 272 for i := 0; i < size; i++ { 273 badBuf2 += "C" 274 } 275 badBuf2 += "\r\n" 276 277 pub := "PUB example 10000\r\n" 278 send(pub + goodBuf + pub + goodBuf + pub + badBuf + pub + badBuf2) 279 expect(errRe) 280 } 281 282 func TestControlLineMaximums(t *testing.T) { 283 s := runProtoServer() 284 defer s.Shutdown() 285 286 c := createClientConn(t, "127.0.0.1", PROTO_TEST_PORT) 287 defer c.Close() 288 289 send, expect := setupConn(t, c) 290 291 pubTooLong := "PUB foo " 292 for i := 0; i < 32; i++ { 293 pubTooLong += "2222222222" 294 } 295 send(pubTooLong) 296 expect(errRe) 297 } 298 299 func TestServerInfoWithClientAdvertise(t *testing.T) { 300 opts := DefaultTestOptions 301 opts.Port = PROTO_TEST_PORT 302 opts.ClientAdvertise = "me:1" 303 s := RunServer(&opts) 304 defer s.Shutdown() 305 306 c := createClientConn(t, opts.Host, PROTO_TEST_PORT) 307 defer c.Close() 308 309 buf := expectResult(t, c, infoRe) 310 js := infoRe.FindAllSubmatch(buf, 1)[0][1] 311 var sinfo server.Info 312 err := json.Unmarshal(js, &sinfo) 313 if err != nil { 314 t.Fatalf("Could not unmarshal INFO json: %v\n", err) 315 } 316 if sinfo.Host != "me" || sinfo.Port != 1 { 317 t.Fatalf("Expected INFO Host:Port to be me:1, got %s:%d", sinfo.Host, sinfo.Port) 318 } 319 }