rdb.tcl 3.6 KB

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