"Fossies" - the Fresh Open Source Software Archive

Member "redis-6.2.5/tests/unit/protocol.tcl" (21 Jul 2021, 5632 Bytes) of package /linux/misc/redis-6.2.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Tcl/Tk source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "protocol.tcl": 6.2.4_vs_6.2.5.

    1 start_server {tags {"protocol network"}} {
    2     test "Handle an empty query" {
    3         reconnect
    4         r write "\r\n"
    5         r flush
    6         assert_equal "PONG" [r ping]
    7     }
    8 
    9     test "Negative multibulk length" {
   10         reconnect
   11         r write "*-10\r\n"
   12         r flush
   13         assert_equal PONG [r ping]
   14     }
   15 
   16     test "Out of range multibulk length" {
   17         reconnect
   18         r write "*20000000\r\n"
   19         r flush
   20         assert_error "*invalid multibulk length*" {r read}
   21     }
   22 
   23     test "Wrong multibulk payload header" {
   24         reconnect
   25         r write "*3\r\n\$3\r\nSET\r\n\$1\r\nx\r\nfooz\r\n"
   26         r flush
   27         assert_error "*expected '$', got 'f'*" {r read}
   28     }
   29 
   30     test "Negative multibulk payload length" {
   31         reconnect
   32         r write "*3\r\n\$3\r\nSET\r\n\$1\r\nx\r\n\$-10\r\n"
   33         r flush
   34         assert_error "*invalid bulk length*" {r read}
   35     }
   36 
   37     test "Out of range multibulk payload length" {
   38         reconnect
   39         r write "*3\r\n\$3\r\nSET\r\n\$1\r\nx\r\n\$2000000000\r\n"
   40         r flush
   41         assert_error "*invalid bulk length*" {r read}
   42     }
   43 
   44     test "Non-number multibulk payload length" {
   45         reconnect
   46         r write "*3\r\n\$3\r\nSET\r\n\$1\r\nx\r\n\$blabla\r\n"
   47         r flush
   48         assert_error "*invalid bulk length*" {r read}
   49     }
   50 
   51     test "Multi bulk request not followed by bulk arguments" {
   52         reconnect
   53         r write "*1\r\nfoo\r\n"
   54         r flush
   55         assert_error "*expected '$', got 'f'*" {r read}
   56     }
   57 
   58     test "Generic wrong number of args" {
   59         reconnect
   60         assert_error "*wrong*arguments*ping*" {r ping x y z}
   61     }
   62 
   63     test "Unbalanced number of quotes" {
   64         reconnect
   65         r write "set \"\"\"test-key\"\"\" test-value\r\n"
   66         r write "ping\r\n"
   67         r flush
   68         assert_error "*unbalanced*" {r read}
   69     }
   70 
   71     set c 0
   72     foreach seq [list "\x00" "*\x00" "$\x00"] {
   73         incr c
   74         test "Protocol desync regression test #$c" {
   75             if {$::tls} {
   76                 set s [::tls::socket [srv 0 host] [srv 0 port]]
   77             } else {
   78                 set s [socket [srv 0 host] [srv 0 port]]
   79             }
   80             puts -nonewline $s $seq
   81             set payload [string repeat A 1024]"\n"
   82             set test_start [clock seconds]
   83             set test_time_limit 30
   84             while 1 {
   85                 if {[catch {
   86                     puts -nonewline $s payload
   87                     flush $s
   88                     incr payload_size [string length $payload]
   89                 }]} {
   90                     set retval [gets $s]
   91                     close $s
   92                     break
   93                 } else {
   94                     set elapsed [expr {[clock seconds]-$test_start}]
   95                     if {$elapsed > $test_time_limit} {
   96                         close $s
   97                         error "assertion:Redis did not closed connection after protocol desync"
   98                     }
   99                 }
  100             }
  101             set retval
  102         } {*Protocol error*}
  103     }
  104     unset c
  105 
  106     # recover the broken connection
  107     reconnect
  108     r ping
  109 
  110     # raw RESP response tests
  111     r readraw 1
  112 
  113     test "raw protocol response" {
  114         r srandmember nonexisting_key
  115     } {*-1}
  116 
  117     r deferred 1
  118 
  119     test "raw protocol response - deferred" {
  120         r srandmember nonexisting_key
  121         r read
  122     } {*-1}
  123 
  124     test "raw protocol response - multiline" {
  125         r sadd ss a
  126         assert_equal [r read] {:1}
  127         r srandmember ss 100
  128         assert_equal [r read] {*1}
  129         assert_equal [r read] {$1}
  130         assert_equal [r read] {a}
  131     }
  132 
  133     # restore connection settings
  134     r readraw 0
  135     r deferred 0
  136 
  137     # check the connection still works
  138     assert_equal [r ping] {PONG}
  139 
  140     test {RESP3 attributes} {
  141         r hello 3
  142         set res [r debug protocol attrib]
  143         # currently the parser in redis.tcl ignores the attributes
  144 
  145         # restore state
  146         r hello 2
  147         set _ $res
  148     } {Some real reply following the attribute}
  149 
  150     test {RESP3 attributes readraw} {
  151         r hello 3
  152         r readraw 1
  153         r deferred 1
  154 
  155         r debug protocol attrib
  156         assert_equal [r read] {|1}
  157         assert_equal [r read] {$14}
  158         assert_equal [r read] {key-popularity}
  159         assert_equal [r read] {*2}
  160         assert_equal [r read] {$7}
  161         assert_equal [r read] {key:123}
  162         assert_equal [r read] {:90}
  163         assert_equal [r read] {$39}
  164         assert_equal [r read] {Some real reply following the attribute}
  165 
  166         # restore state
  167         r readraw 0
  168         r deferred 0
  169         r hello 2
  170         set _ {}
  171     } {}
  172 
  173     test {RESP3 attributes on RESP2} {
  174         r hello 2
  175         set res [r debug protocol attrib]
  176         set _ $res
  177     } {Some real reply following the attribute}
  178 
  179     test "test big number parsing" {
  180         r hello 3
  181         r debug protocol bignum
  182     } {1234567999999999999999999999999999999}
  183 
  184     test "test bool parsing" {
  185         r hello 3
  186         assert_equal [r debug protocol true] 1
  187         assert_equal [r debug protocol false] 0
  188         r hello 2
  189         assert_equal [r debug protocol true] 1
  190         assert_equal [r debug protocol false] 0
  191         set _ {}
  192     } {}
  193 }
  194 
  195 start_server {tags {"regression"}} {
  196     test "Regression for a crash with blocking ops and pipelining" {
  197         set rd [redis_deferring_client]
  198         set fd [r channel]
  199         set proto "*3\r\n\$5\r\nBLPOP\r\n\$6\r\nnolist\r\n\$1\r\n0\r\n"
  200         puts -nonewline $fd $proto$proto
  201         flush $fd
  202         set res {}
  203 
  204         $rd rpush nolist a
  205         $rd read
  206         $rd rpush nolist a
  207         $rd read
  208     }
  209 }