1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
/* Some unit tests that don't require Valkey to be running. */
#include "fmacros.h"
#include "win32.h"
#include "cluster.h"
#include "command.h"
#include "test_utils.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Helper for the macro ASSERT_KEY below. */
void check_key(const char *key, struct cmd *command, const char *file, int line) {
if (command->result != CMD_PARSE_OK) {
fprintf(stderr, "%s:%d: Command parsing failed: %s\n", file, line,
command->errstr);
assert(0);
}
char *actual_key = command->key.start;
int actual_keylen = command->key.len;
if ((int)strlen(key) != actual_keylen ||
strncmp(key, actual_key, actual_keylen)) {
fprintf(stderr,
"%s:%d: Expected key to be \"%s\" but got \"%.*s\"\n",
file, line, key, actual_keylen, actual_key);
assert(0);
}
}
/* Checks that a command (struct cmd *) has the given key (string). */
#define ASSERT_KEY(command, key) \
do { \
check_key(key, command, __FILE__, __LINE__); \
} while (0)
void test_valkey_parse_error_nonresp(void) {
struct cmd *c = command_get();
c->cmd = strdup("+++Not RESP+++\r\n");
c->clen = strlen(c->cmd);
valkey_parse_cmd(c);
ASSERT_MSG(c->result == CMD_PARSE_ERROR, "Unexpected parse success");
ASSERT_MSG(!strcmp(c->errstr, "Command parse error"), c->errstr);
command_destroy(c);
}
/* Create a 65 bytes command string while limit is 64 bytes */
void test_valkey_parse_too_long_cmd(void) {
char str[66];
memset(str, 'A', 65);
str[65] = '\0';
struct cmd *c = command_get();
int len = valkeyFormatCommand(&c->cmd, str);
ASSERT_MSG(len >= 0, "Format command error");
c->clen = len;
valkey_parse_cmd(c);
ASSERT_MSG(c->result == CMD_PARSE_ERROR, "Unexpected parse success");
ASSERT_MSG(!strncmp(c->errstr, "Unknown command AAAA", 20), c->errstr);
command_destroy(c);
}
/* Parse a subcommand longer than the 64 char limit */
void test_valkey_parse_too_long_subcommand(void) {
char subcmd[66];
memset(subcmd, 'A', 65);
subcmd[65] = '\0';
struct cmd *c = command_get();
int len = valkeyFormatCommand(&c->cmd, "XGROUP %s", subcmd);
ASSERT_MSG(len >= 0, "Format command error");
c->clen = len;
valkey_parse_cmd(c);
ASSERT_MSG(c->result == CMD_PARSE_ERROR, "Unexpected parse success");
ASSERT_MSG(!strncmp(c->errstr, "Unknown command XGROUP AAAAAAA", 30), c->errstr);
command_destroy(c);
}
void test_valkey_parse_unknown_cmd(void) {
struct cmd *c = command_get();
int len = valkeyFormatCommand(&c->cmd, "OIOIOI");
ASSERT_MSG(len >= 0, "Format command error");
c->clen = len;
valkey_parse_cmd(c);
ASSERT_MSG(c->result == CMD_PARSE_ERROR, "Unexpected parse success");
ASSERT_MSG(!strcmp(c->errstr, "Unknown command OIOIOI"), c->errstr);
command_destroy(c);
}
void test_valkey_parse_cmd_get(void) {
struct cmd *c = command_get();
int len = valkeyFormatCommand(&c->cmd, "GET foo");
ASSERT_MSG(len >= 0, "Format command error");
c->clen = len;
valkey_parse_cmd(c);
ASSERT_MSG(c->result == CMD_PARSE_OK, "Parse not OK");
ASSERT_KEY(c, "foo");
command_destroy(c);
}
void test_valkey_parse_cmd_mset(void) {
struct cmd *c = command_get();
int len = valkeyFormatCommand(&c->cmd, "MSET foo val1 bar val2");
ASSERT_MSG(len >= 0, "Format command error");
c->clen = len;
valkey_parse_cmd(c);
ASSERT_MSG(c->result == CMD_PARSE_OK, "Parse not OK");
ASSERT_KEY(c, "foo");
command_destroy(c);
}
void test_valkey_parse_cmd_eval_1(void) {
struct cmd *c = command_get();
int len = valkeyFormatCommand(&c->cmd, "EVAL dummyscript 1 foo");
ASSERT_MSG(len >= 0, "Format command error");
c->clen = len;
valkey_parse_cmd(c);
ASSERT_MSG(c->result == CMD_PARSE_OK, "Parse not OK");
ASSERT_KEY(c, "foo");
command_destroy(c);
}
void test_valkey_parse_cmd_eval_0(void) {
struct cmd *c = command_get();
int len = valkeyFormatCommand(&c->cmd, "EVAL dummyscript 0 foo");
ASSERT_MSG(len >= 0, "Format command error");
c->clen = len;
valkey_parse_cmd(c);
ASSERT_MSG(c->result == CMD_PARSE_OK, "Parse not OK");
ASSERT_MSG(c->key.len == 0, "Unexpected key found");
command_destroy(c);
}
void test_valkey_parse_cmd_xgroup_no_subcommand(void) {
struct cmd *c = command_get();
int len = valkeyFormatCommand(&c->cmd, "XGROUP");
ASSERT_MSG(len >= 0, "Format command error");
c->clen = len;
valkey_parse_cmd(c);
ASSERT_MSG(c->result == CMD_PARSE_ERROR, "Unexpected parse success");
ASSERT_MSG(!strcmp(c->errstr, "Unknown command XGROUP"), c->errstr);
command_destroy(c);
}
void test_valkey_parse_cmd_xgroup_unknown_subcommand(void) {
struct cmd *c = command_get();
int len = valkeyFormatCommand(&c->cmd, "XGROUP OIOIOI");
ASSERT_MSG(len >= 0, "Format command error");
c->clen = len;
valkey_parse_cmd(c);
ASSERT_MSG(c->result == CMD_PARSE_ERROR, "Unexpected parse success");
ASSERT_MSG(!strcmp(c->errstr, "Unknown command XGROUP OIOIOI"), c->errstr);
command_destroy(c);
}
void test_valkey_parse_cmd_xgroup_destroy_no_key(void) {
struct cmd *c = command_get();
int len = valkeyFormatCommand(&c->cmd, "xgroup destroy");
ASSERT_MSG(len >= 0, "Format command error");
c->clen = len;
valkey_parse_cmd(c);
ASSERT_MSG(c->result == CMD_PARSE_ERROR, "Parse not OK");
const char *expected_error =
"Failed to find keys of command XGROUP DESTROY";
ASSERT_MSG(!strncmp(c->errstr, expected_error, strlen(expected_error)),
c->errstr);
command_destroy(c);
}
void test_valkey_parse_cmd_xgroup_destroy_ok(void) {
struct cmd *c = command_get();
int len = valkeyFormatCommand(&c->cmd, "xgroup destroy mystream mygroup");
ASSERT_MSG(len >= 0, "Format command error");
c->clen = len;
valkey_parse_cmd(c);
ASSERT_KEY(c, "mystream");
command_destroy(c);
}
void test_valkey_parse_cmd_xreadgroup_ok(void) {
struct cmd *c = command_get();
/* Use group name and consumer name "streams" just to try to confuse the
* parser. The parser shouldn't mistake those for the STREAMS keyword. */
int len = valkeyFormatCommand(
&c->cmd, "XREADGROUP GROUP streams streams COUNT 1 streams mystream >");
ASSERT_MSG(len >= 0, "Format command error");
c->clen = len;
valkey_parse_cmd(c);
ASSERT_KEY(c, "mystream");
command_destroy(c);
}
void test_valkey_parse_cmd_xread_ok(void) {
struct cmd *c = command_get();
int len = valkeyFormatCommand(
&c->cmd, "XREAD BLOCK 42 STREAMS mystream another $ $");
ASSERT_MSG(len >= 0, "Format command error");
c->clen = len;
valkey_parse_cmd(c);
ASSERT_KEY(c, "mystream");
command_destroy(c);
}
void test_valkey_parse_cmd_restore_ok(void) {
/* The ordering of RESTORE and RESTORE-ASKING in the lookup-table was wrong
* in a previous version, leading to the command not being found. */
struct cmd *c = command_get();
int len = valkeyFormatCommand(&c->cmd, "restore k 0 xxx");
ASSERT_MSG(len >= 0, "Format command error");
c->clen = len;
valkey_parse_cmd(c);
ASSERT_KEY(c, "k");
command_destroy(c);
}
void test_valkey_parse_cmd_restore_asking_ok(void) {
/* The ordering of RESTORE and RESTORE-ASKING in the lookup-table was wrong
* in a previous version, leading to the command not being found. */
struct cmd *c = command_get();
int len = valkeyFormatCommand(&c->cmd, "restore-asking k 0 xxx");
ASSERT_MSG(len >= 0, "Format command error");
c->clen = len;
valkey_parse_cmd(c);
ASSERT_KEY(c, "k");
command_destroy(c);
}
void test_valkey_parse_cmd_georadius_ro_ok(void) {
/* The position of GEORADIUS_RO was wrong in a previous version of the
* lookup-table, leading to the command not being found. */
struct cmd *c = command_get();
int len = valkeyFormatCommand(&c->cmd, "georadius_ro k 0 0 0 km");
ASSERT_MSG(len >= 0, "Format command error");
c->clen = len;
valkey_parse_cmd(c);
ASSERT_KEY(c, "k");
command_destroy(c);
}
void test_valkey_parse_cmd_sadd_ok(void) {
char key[201];
memset(key, 'A', 200);
key[200] = '\0';
struct cmd *c = command_get();
int len = valkeyFormatCommand(&c->cmd, "SADD %s value", key);
ASSERT_MSG(len >= 0, "Format command error");
c->clen = len;
valkey_parse_cmd(c);
ASSERT_KEY(c, key);
command_destroy(c);
}
int main(void) {
test_valkey_parse_error_nonresp();
test_valkey_parse_too_long_cmd();
test_valkey_parse_too_long_subcommand();
test_valkey_parse_unknown_cmd();
test_valkey_parse_cmd_get();
test_valkey_parse_cmd_mset();
test_valkey_parse_cmd_eval_1();
test_valkey_parse_cmd_eval_0();
test_valkey_parse_cmd_xgroup_no_subcommand();
test_valkey_parse_cmd_xgroup_unknown_subcommand();
test_valkey_parse_cmd_xgroup_destroy_no_key();
test_valkey_parse_cmd_xgroup_destroy_ok();
test_valkey_parse_cmd_xreadgroup_ok();
test_valkey_parse_cmd_xread_ok();
test_valkey_parse_cmd_restore_ok();
test_valkey_parse_cmd_restore_asking_ok();
test_valkey_parse_cmd_georadius_ro_ok();
test_valkey_parse_cmd_sadd_ok();
return 0;
}
|