nserver

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | LICENSE

commit 1895277797fab61819ba4c666d259278f1f89c85
parent dd0d4c75b2a78f2445eaf1f13bbaa644e578026a
Author: rsiddharth <s@ricketyspace.net>
Date:   Mon, 13 Apr 2020 19:52:19 -0400

nserver: ncmd.h: Add NS_STORE.

* nserver/src/ncmd.c (find_function): Add handling to find NS_STORE.
(call_function): Add handling for NS_STORE.
* nserver/src/ncmd.h (FUNCTIONS): Add NS_STORE
* nserver/tests/ncmd_tests.c (test_find_function)
(test_call_function, test_process): Update tests to check the
correctness of NS_STORE.

Diffstat:
nserver/src/ncmd.c | 16++++++++++++++++
nserver/src/ncmd.h | 1+
nserver/tests/ncmd_tests.c | 44++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 61 insertions(+), 0 deletions(-)

diff --git a/nserver/src/ncmd.c b/nserver/src/ncmd.c @@ -108,6 +108,7 @@ int find_function(struct bstrList *cmd_parts) struct tagbstring fdump = bsStatic("/dump"); struct tagbstring fdelete = bsStatic("/delete"); struct tagbstring flist = bsStatic("/list"); + struct tagbstring fstore = bsStatic("/store"); check(cmd_parts != NULL, "cmd_parts is NULL"); check(cmd_parts->qty > 0, "qty check failed"); @@ -132,6 +133,8 @@ int find_function(struct bstrList *cmd_parts) return NS_DELETE; } else if (bstricmp(cmd_name, &flist) == 0) { return NS_LIST; + } else if (bstricmp(cmd_name, &fstore) == 0) { + return NS_STORE; } else { return NS_NOP; } @@ -275,6 +278,19 @@ int call_function(int func, struct bstrList *cmd_parts, char *out) free(list); break; + case NS_STORE: + if(check_args(cmd_parts, 2) != 0) { + strncpy(out, "error: command invalid\n", RSP_SIZE); + + return -1; + } + if (ssstore(bdata(cmd_parts->entry[1])) < 0) { + strncpy(out, "error: store failed\n", RSP_SIZE); + + return -1; + } + strncpy(out, "OK\n", RSP_SIZE); + break; default: strncpy(out, "error: function not found\n", RSP_SIZE); diff --git a/nserver/src/ncmd.h b/nserver/src/ncmd.h @@ -18,6 +18,7 @@ enum FUNCTIONS { NS_DUMP, NS_DELETE, NS_LIST, + NS_STORE, NS_NOP = -1 }; diff --git a/nserver/tests/ncmd_tests.c b/nserver/tests/ncmd_tests.c @@ -202,6 +202,18 @@ char *test_find_function() funk = find_function(parts); mu_assert(funk == NS_LIST, "find function list failed"); + char *store = "/store jowl"; + parts = cmd_parts(store); + mu_assert(parts != NULL, "cmp_parts failed"); + funk = find_function(parts); + mu_assert(funk == NS_STORE, "find function list failed"); + + store = "/STore jowl"; + parts = cmd_parts(store); + mu_assert(parts != NULL, "cmp_parts failed"); + funk = find_function(parts); + mu_assert(funk == NS_STORE, "find function list failed"); + char *nop = "/meant bacon"; parts = cmd_parts(nop); mu_assert(parts != NULL, "cmd_parts failed"); @@ -283,6 +295,22 @@ char *test_call_function() || strcmp(msg, "beef\nham\n") == 0, "call function failed"); + + char *ham_store = "/store ham"; + parts = cmd_parts(ham_store); + mu_assert(parts != NULL, "cmd_parts failed"); + rc = call_function(NS_STORE, parts, msg); + mu_assert(rc == 0, "call function failed"); + mu_assert(strcmp(msg, "OK\n") == 0, "call function failed"); + + + char *beef_store = "/store beef"; + parts = cmd_parts(beef_store); + mu_assert(parts != NULL, "cmd_parts failed"); + rc = call_function(NS_STORE, parts, msg); + mu_assert(rc == 0, "call function failed"); + mu_assert(strcmp(msg, "OK\n") == 0, "call function failed"); + // delete ham and beef. char *ham_delete = "/delete ham"; parts = cmd_parts(ham_delete); @@ -353,6 +381,22 @@ char *test_process() || strcmp(out, "beef\nham\n") == 0, "process failed"); + char *ham_store = "/store ham"; + rc = process(ham_store, out); + mu_assert(rc == 0, "process failed"); + mu_assert(strcmp(out, "OK\n") == 0, "process failed"); + + + char *beef_store = "/store beef"; + rc = process(beef_store, out); + mu_assert(rc == 0, "process failed"); + mu_assert(strcmp(out, "OK\n") == 0, "process failed"); + + char *bacon_store = "/store bacon"; + rc = process(bacon_store, out); + mu_assert(rc == -1, "process failed"); + mu_assert(strcmp(out, "error: store failed\n") == 0, "process failed"); + return NULL; }