protocol.tcl 6.5 KB

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