From 6a5bac309e868deef749c36949723b415de2496f Mon Sep 17 00:00:00 2001 From: Oran Agra Date: Wed, 14 Jul 2021 19:14:31 +0300 Subject: Test infra, handle RESP3 attributes and big-numbers and bools (#9235) - promote the code in DEBUG PROTOCOL to addReplyBigNum - DEBUG PROTOCOL ATTRIB skips the attribute when client is RESP2 - networking.c addReply for push and attributes generate assertion when called on a RESP2 client, anything else would produce a broken protocol that clients can't handle. --- tests/support/redis.tcl | 52 ++++++++++++++++++++++++++++++----------------- tests/unit/protocol.tcl | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 18 deletions(-) (limited to 'tests') diff --git a/tests/support/redis.tcl b/tests/support/redis.tcl index 285b53574..978163e98 100644 --- a/tests/support/redis.tcl +++ b/tests/support/redis.tcl @@ -247,30 +247,46 @@ proc ::redis::redis_read_null fd { return {} } +proc ::redis::redis_read_bool fd { + set v [redis_read_line $fd] + if {$v == "t"} {return 1} + if {$v == "f"} {return 0} + return -code error "Bad protocol, '$v' as bool type" +} + proc ::redis::redis_read_reply {id fd} { if {$::redis::readraw($id)} { return [redis_read_line $fd] } - set type [read $fd 1] - switch -exact -- $type { - _ {redis_read_null $fd} - : - - + {redis_read_line $fd} - , {expr {double([redis_read_line $fd])}} - - {return -code error [redis_read_line $fd]} - $ {redis_bulk_read $fd} - > - - ~ - - * {redis_multi_bulk_read $id $fd} - % {redis_read_map $id $fd} - default { - if {$type eq {}} { - catch {close $fd} - set ::redis::fd($id) {} - return -code error "I/O error reading reply" + while {1} { + set type [read $fd 1] + switch -exact -- $type { + _ {return [redis_read_null $fd]} + : - + ( - + + {return [redis_read_line $fd]} + , {return [expr {double([redis_read_line $fd])}]} + # {return [redis_read_bool $fd]} + - {return -code error [redis_read_line $fd]} + $ {return [redis_bulk_read $fd]} + > - + ~ - + * {return [redis_multi_bulk_read $id $fd]} + % {return [redis_read_map $id $fd]} + | { + # ignore attributes for now (nowhere to store them) + redis_read_map $id $fd + continue + } + default { + if {$type eq {}} { + catch {close $fd} + set ::redis::fd($id) {} + return -code error "I/O error reading reply" + } + return -code error "Bad protocol, '$type' as reply type byte" } - return -code error "Bad protocol, '$type' as reply type byte" } } } diff --git a/tests/unit/protocol.tcl b/tests/unit/protocol.tcl index ffc751f8f..ed0fe6482 100644 --- a/tests/unit/protocol.tcl +++ b/tests/unit/protocol.tcl @@ -136,6 +136,60 @@ start_server {tags {"protocol network"}} { # check the connection still works assert_equal [r ping] {PONG} + + test {RESP3 attributes} { + r hello 3 + set res [r debug protocol attrib] + # currently the parser in redis.tcl ignores the attributes + + # restore state + r hello 2 + set _ $res + } {Some real reply following the attribute} {resp3} + + test {RESP3 attributes readraw} { + r hello 3 + r readraw 1 + r deferred 1 + + r debug protocol attrib + assert_equal [r read] {|1} + assert_equal [r read] {$14} + assert_equal [r read] {key-popularity} + assert_equal [r read] {*2} + assert_equal [r read] {$7} + assert_equal [r read] {key:123} + assert_equal [r read] {:90} + assert_equal [r read] {$39} + assert_equal [r read] {Some real reply following the attribute} + + # restore state + r readraw 0 + r deferred 0 + r hello 2 + set _ {} + } {} {resp3} + + test {RESP3 attributes on RESP2} { + r hello 2 + set res [r debug protocol attrib] + set _ $res + } {Some real reply following the attribute} + + test "test big number parsing" { + r hello 3 + r debug protocol bignum + } {1234567999999999999999999999999999999} {needs:debug resp3} + + test "test bool parsing" { + r hello 3 + assert_equal [r debug protocol true] 1 + assert_equal [r debug protocol false] 0 + r hello 2 + assert_equal [r debug protocol true] 1 + assert_equal [r debug protocol false] 0 + set _ {} + } {} {needs:debug resp3} } start_server {tags {"regression"}} { -- cgit v1.2.1