1set server_path [tmpdir "server.rdb-encoding-test"]
2
3# Copy RDB with different encodings in server path
4exec cp tests/assets/encodings.rdb $server_path
5
6start_server [list overrides [list "dir" $server_path "dbfilename" "encodings.rdb"]] {
7  test "RDB encoding loading test" {
8    r select 0
9    csvdump r
10  } {"0","compressible","string","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
11"0","hash","hash","a","1","aa","10","aaa","100","b","2","bb","20","bbb","200","c","3","cc","30","ccc","300","ddd","400","eee","5000000000",
12"0","hash_zipped","hash","a","1","b","2","c","3",
13"0","list","list","1","2","3","a","b","c","100000","6000000000","1","2","3","a","b","c","100000","6000000000","1","2","3","a","b","c","100000","6000000000",
14"0","list_zipped","list","1","2","3","a","b","c","100000","6000000000",
15"0","number","string","10"
16"0","set","set","1","100000","2","3","6000000000","a","b","c",
17"0","set_zipped_1","set","1","2","3","4",
18"0","set_zipped_2","set","100000","200000","300000","400000",
19"0","set_zipped_3","set","1000000000","2000000000","3000000000","4000000000","5000000000","6000000000",
20"0","string","string","Hello World"
21"0","zset","zset","a","1","b","2","c","3","aa","10","bb","20","cc","30","aaa","100","bbb","200","ccc","300","aaaa","1000","cccc","123456789","bbbb","5000000000",
22"0","zset_zipped","zset","a","1","b","2","c","3",
23}
24}
25
26set server_path [tmpdir "server.rdb-startup-test"]
27
28start_server [list overrides [list "dir" $server_path]] {
29    test {Server started empty with non-existing RDB file} {
30        r debug digest
31    } {0000000000000000000000000000000000000000}
32    # Save an RDB file, needed for the next test.
33    r save
34}
35
36start_server [list overrides [list "dir" $server_path]] {
37    test {Server started empty with empty RDB file} {
38        r debug digest
39    } {0000000000000000000000000000000000000000}
40}
41
42start_server [list overrides [list "dir" $server_path]] {
43    test {Test RDB stream encoding} {
44        for {set j 0} {$j < 1000} {incr j} {
45            if {rand() < 0.9} {
46                r xadd stream * foo $j
47            } else {
48                r xadd stream * bar $j
49            }
50        }
51        r xgroup create stream mygroup 0
52        r xreadgroup GROUP mygroup Alice COUNT 1 STREAMS stream >
53        set digest [r debug digest]
54        r debug reload
55        set newdigest [r debug digest]
56        assert {$digest eq $newdigest}
57        r del stream
58    }
59}
60
61# Helper function to start a server and kill it, just to check the error
62# logged.
63set defaults {}
64proc start_server_and_kill_it {overrides code} {
65    upvar defaults defaults srv srv server_path server_path
66    set config [concat $defaults $overrides]
67    set srv [start_server [list overrides $config]]
68    uplevel 1 $code
69    kill_server $srv
70}
71
72# Make the RDB file unreadable
73file attributes [file join $server_path dump.rdb] -permissions 0222
74
75# Detect root account (it is able to read the file even with 002 perm)
76set isroot 0
77catch {
78    open [file join $server_path dump.rdb]
79    set isroot 1
80}
81
82# Now make sure the server aborted with an error
83if {!$isroot} {
84    start_server_and_kill_it [list "dir" $server_path] {
85        test {Server should not start if RDB file can't be open} {
86            wait_for_condition 50 100 {
87                [string match {*Fatal error loading*} \
88                    [exec tail -1 < [dict get $srv stdout]]]
89            } else {
90                fail "Server started even if RDB was unreadable!"
91            }
92        }
93    }
94}
95
96# Fix permissions of the RDB file.
97file attributes [file join $server_path dump.rdb] -permissions 0666
98
99# Corrupt its CRC64 checksum.
100set filesize [file size [file join $server_path dump.rdb]]
101set fd [open [file join $server_path dump.rdb] r+]
102fconfigure $fd -translation binary
103seek $fd -8 end
104puts -nonewline $fd "foobar00"; # Corrupt the checksum
105close $fd
106
107# Now make sure the server aborted with an error
108start_server_and_kill_it [list "dir" $server_path] {
109    test {Server should not start if RDB is corrupted} {
110        wait_for_condition 50 100 {
111            [string match {*CRC error*} \
112                [exec tail -10 < [dict get $srv stdout]]]
113        } else {
114            fail "Server started even if RDB was corrupted!"
115        }
116    }
117}
118