get.pme.sh/pnats@v0.0.0-20240304004023-26bb5a137ed0/test/pedantic_test.go (about)

     1  // Copyright 2012-2019 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  	"testing"
    18  
    19  	"get.pme.sh/pnats/server"
    20  )
    21  
    22  func runPedanticServer() *server.Server {
    23  	opts := DefaultTestOptions
    24  
    25  	opts.NoLog = false
    26  	opts.Trace = true
    27  
    28  	opts.Port = PROTO_TEST_PORT
    29  	return RunServer(&opts)
    30  }
    31  
    32  func TestPedanticSub(t *testing.T) {
    33  	s := runPedanticServer()
    34  	defer s.Shutdown()
    35  
    36  	c := createClientConn(t, "127.0.0.1", PROTO_TEST_PORT)
    37  	defer c.Close()
    38  
    39  	send := sendCommand(t, c)
    40  	expect := expectCommand(t, c)
    41  	doConnect(t, c, false, true, false)
    42  
    43  	// Ping should still be same
    44  	send("PING\r\n")
    45  	expect(pongRe)
    46  
    47  	// Test malformed subjects for SUB
    48  	// Sub can contain wildcards, but
    49  	// subject must still be legit.
    50  
    51  	// Empty terminal token
    52  	send("SUB foo. 1\r\n")
    53  	expect(errRe)
    54  
    55  	// Empty beginning token
    56  	send("SUB .foo. 1\r\n")
    57  	expect(errRe)
    58  
    59  	// Empty middle token
    60  	send("SUB foo..bar 1\r\n")
    61  	expect(errRe)
    62  
    63  	// Bad non-terminal FWC
    64  	send("SUB foo.>.bar 1\r\n")
    65  	buf := expect(errRe)
    66  
    67  	// Check that itr is 'Invalid Subject'
    68  	matches := errRe.FindAllSubmatch(buf, -1)
    69  	if len(matches) != 1 {
    70  		t.Fatal("Wanted one overall match")
    71  	}
    72  	if string(matches[0][1]) != "'Invalid Subject'" {
    73  		t.Fatalf("Expected 'Invalid Subject', got %s", string(matches[0][1]))
    74  	}
    75  }
    76  
    77  func TestPedanticPub(t *testing.T) {
    78  	s := runPedanticServer()
    79  	defer s.Shutdown()
    80  
    81  	c := createClientConn(t, "127.0.0.1", PROTO_TEST_PORT)
    82  	defer c.Close()
    83  
    84  	send := sendCommand(t, c)
    85  	expect := expectCommand(t, c)
    86  	doConnect(t, c, false, true, false)
    87  
    88  	// Ping should still be same
    89  	send("PING\r\n")
    90  	expect(pongRe)
    91  
    92  	// Test malformed subjects for PUB
    93  	// PUB subjects can not have wildcards
    94  	// This will error in pedantic mode
    95  	send("PUB foo.* 2\r\nok\r\n")
    96  	expect(errRe)
    97  
    98  	send("PUB foo.> 2\r\nok\r\n")
    99  	expect(errRe)
   100  
   101  	send("PUB foo. 2\r\nok\r\n")
   102  	expect(errRe)
   103  
   104  	send("PUB .foo 2\r\nok\r\n")
   105  	expect(errRe)
   106  
   107  	send("PUB foo..* 2\r\nok\r\n")
   108  	expect(errRe)
   109  }