diff --git a/src/box/key_def.c b/src/box/key_def.c index da1c23135ffab2206dc2f812912b52e609a66faf..ca8a00dc5af757431bb011f02aa510577124acae 100644 --- a/src/box/key_def.c +++ b/src/box/key_def.c @@ -627,6 +627,23 @@ box_key_def_extract_key(box_key_def_t *key_def, box_tuple_t *tuple, return tuple_extract_key(tuple, key_def, multikey_idx, key_size_ptr); } +int +box_key_def_validate_key(const box_key_def_t *key_def, const char *key, + uint32_t *key_size_ptr) +{ + const char *pos = key; + uint32_t part_count = mp_decode_array(&pos); + if (part_count > key_def->part_count) { + diag_set(ClientError, ER_KEY_PART_COUNT, key_def->part_count, + part_count); + return -1; + } + int rc = key_validate_parts(key_def, pos, part_count, true, &pos); + if (rc == 0 && key_size_ptr != NULL) + *key_size_ptr = pos - key; + return rc; +} + /* }}} Module API functions */ int diff --git a/src/box/key_def.h b/src/box/key_def.h index 8ed294032a1564538a121d34c1e5e30640485bcb..3c004d9e46c8783dc35f9188ecf6ad28e12517ef 100644 --- a/src/box/key_def.h +++ b/src/box/key_def.h @@ -560,6 +560,32 @@ API_EXPORT char * box_key_def_extract_key(box_key_def_t *key_def, box_tuple_t *tuple, int multikey_idx, uint32_t *key_size_ptr); +/** + * Check a key against given key definition. + * + * Verifies key parts against given key_def's field types with + * respect to nullability. + * + * A partial key (with less part than defined in @a key_def) is + * verified by given key parts, the omitted tail is not verified + * anyhow. + * + * Note: nil is accepted for nullable fields, but only for them. + * + * @param key_def Key definition. + * @param key MessagePack'ed data for matching. + * @param key_size_ptr Here will be size of the validated key. + * + * @retval 0 The key is valid. + * @retval -1 The key is invalid. + * + * In case of an invalid key set a diag and return -1. + * @sa <box_error_last>(). + */ +API_EXPORT int +box_key_def_validate_key(const box_key_def_t *key_def, const char *key, + uint32_t *key_size_ptr); + /** \endcond public */ /* diff --git a/src/exports.h b/src/exports.h index 12997f8e936ac241dca4eae9e48a2198f2075249..e77a118788fca91e2490436d9e530d685b18013e 100644 --- a/src/exports.h +++ b/src/exports.h @@ -35,6 +35,7 @@ EXPORT(box_key_def_extract_key) EXPORT(box_key_def_merge) EXPORT(box_key_def_new) EXPORT(box_key_def_new_v2) +EXPORT(box_key_def_validate_key) EXPORT(box_key_def_validate_tuple) EXPORT(box_key_part_def_create) EXPORT(box_latch_delete) diff --git a/test/app-tap/module_api.c b/test/app-tap/module_api.c index a5b10a7f3e3aed9efadaadf7ea1e107a011b41c2..55f237027b16be4f500440aabad71e516be2e13b 100644 --- a/test/app-tap/module_api.c +++ b/test/app-tap/module_api.c @@ -1731,6 +1731,112 @@ test_key_def_extract_key(struct lua_State *L) return 1; } +/** + * Basic <box_key_def_validate_key>() test. + */ +static int +test_key_def_validate_key(struct lua_State *L) +{ + /* + * Create a key_def. + * + * | tuple + * | [x, x, x] + * | key_def ^ ^ + * | | | | + * | (0) <-----+---- unsigned + * | | | + * | (1) <---- unsigned (optional) + */ + box_key_part_def_t parts[2]; + box_key_part_def_create(&parts[0]); + box_key_part_def_create(&parts[1]); + parts[0].fieldno = 2; + parts[0].field_type = "unsigned"; + parts[1].fieldno = 0; + parts[1].field_type = "unsigned"; + parts[1].flags |= BOX_KEY_PART_DEF_IS_NULLABLE; + box_key_def_t *key_def = box_key_def_new_v2(parts, 2); + assert(key_def != NULL); + + /* + * Create keys to validate them against given key_def. + * + * | # | key | Is valid? | + * | - | -------------- | --------- | + * | 0 | [1, 1] | valid | + * | 1 | [1, null] | valid | + * | 2 | [1] | valid | + * | 3 | [] | valid | + * | 4 | [null] | invalid | + * | 5 | [1, 2, 3] | invalid | + * | 6 | [1, -1] | invalid | + */ + struct { + const char *data; + uint32_t size; + } keys[] = { + /* [0] = */ {"\x92\x01\x01", 3}, + /* [1] = */ {"\x92\x01\xc0", 3}, + /* [2] = */ {"\x91\x01", 2}, + /* [3] = */ {"\x90", 1}, + /* [4] = */ {"\x91\xc0", 2}, + /* [5] = */ {"\x93\x01\x02\x03", 4}, + /* [6] = */ {"\x92\x01\xff", 3}, + }; + int expected_results[] = { + /* [0] = */ 0, + /* [1] = */ 0, + /* [2] = */ 0, + /* [3] = */ 0, + /* [4] = */ -1, + /* [5] = */ -1, + /* [6] = */ -1, + }; + uint32_t expected_error_codes[] = { + /* [0] = */ box_error_code_MAX, + /* [1] = */ box_error_code_MAX, + /* [2] = */ box_error_code_MAX, + /* [3] = */ box_error_code_MAX, + /* [4] = */ ER_KEY_PART_TYPE, + /* [5] = */ ER_KEY_PART_COUNT, + /* [6] = */ ER_KEY_PART_TYPE, + }; + + for (size_t i = 0; i < lengthof(keys); ++i) { + uint32_t key_size = 0; + const char *key = keys[i].data; + int rc = box_key_def_validate_key(key_def, key, &key_size); + assert(rc == expected_results[i]); + (void)rc; + (void)expected_results; + + if (expected_error_codes[i] == box_error_code_MAX) { + /* Verify key_size. */ + assert(key_size != 0); + assert(key_size == keys[i].size); + + /* + * Verify that no NULL pointer dereference + * occurs when NULL is passed as + * key_size_ptr. + */ + box_key_def_validate_key(key_def, key, NULL); + } else { + assert(rc != 0); + box_error_t *e = box_error_last(); + (void)e; + assert(box_error_code(e) == expected_error_codes[i]); + } + } + + /* Clean up. */ + box_key_def_delete(key_def); + + lua_pushboolean(L, 1); + return 1; +} + /* }}} key_def api v2 */ static int @@ -2122,6 +2228,7 @@ luaopen_module_api(lua_State *L) {"test_key_def_validate_tuple", test_key_def_validate_tuple}, {"test_key_def_merge", test_key_def_merge}, {"test_key_def_extract_key", test_key_def_extract_key}, + {"test_key_def_validate_key", test_key_def_validate_key}, {NULL, NULL} }; luaL_register(L, "module_api", lib); diff --git a/test/app-tap/module_api.test.lua b/test/app-tap/module_api.test.lua index 4de4504623371a5ca1cd0a56f97ee804a08fcdbd..4e06cf431e6b6000b85df72640ef5ab421de9592 100755 --- a/test/app-tap/module_api.test.lua +++ b/test/app-tap/module_api.test.lua @@ -177,7 +177,7 @@ local function test_iscdata(test, module) end local test = require('tap').test("module_api", function(test) - test:plan(33) + test:plan(34) local status, module = pcall(require, 'module_api') test:is(status, true, "module") test:ok(status, "module is loaded")