nserver

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

commit ca7b0734119dd26faae442fdf02db172c5cabdac
parent 66377d4496381cf5e0f942023414f08b70019ecd
Author: rsiddharth <s@ricketyspace.net>
Date:   Thu, 28 Nov 2019 02:43:48 -0500

nserver: Add ncmd_tests.c

* nserver/tests/ncmd_tests.c (test_sanitize): Test for sanitize.
(all_tests): New function.

Diffstat:
nserver/tests/ncmd_tests.c | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 61 insertions(+), 0 deletions(-)

diff --git a/nserver/tests/ncmd_tests.c b/nserver/tests/ncmd_tests.c @@ -0,0 +1,61 @@ +#include "minunit.h" +#include <ncmd.h> + +char *test_sanitize() +{ + char *cmd = calloc(128, sizeof(char)); + mu_assert(cmd != NULL, "calloc failed"); + + char *c_cmd = "/create api/beef\n"; + size_t c_cmd_sz = strlen(c_cmd); + + strncpy(cmd, c_cmd, c_cmd_sz + 1); + mu_assert(strlen(cmd) == c_cmd_sz, "strncpy failed"); + + sanitize(cmd); + mu_assert(strlen(cmd) == c_cmd_sz - 1, "sanitize failed"); + mu_assert(strcmp(cmd, "/create api/beef") == 0, "sanitize failed"); + + + // Clear cmd. + memset(cmd, '\0', 128); + + + c_cmd = "/create api/ham\n\n\n"; + c_cmd_sz = strlen(c_cmd); + + strncpy(cmd, c_cmd, c_cmd_sz + 1); + mu_assert(strlen(cmd) == c_cmd_sz, "strncpy failed"); + + sanitize(cmd); + mu_assert(strlen(cmd) == c_cmd_sz - 3, "sanitize failed"); + mu_assert(strcmp(cmd, "/create api/ham") == 0, "sanitize failed"); + + + // Clear cmd. + memset(cmd, '\0', 128); + + + c_cmd = "/create api/bacon\n/create api/turkey\n"; + c_cmd_sz = strlen(c_cmd); + + strncpy(cmd, c_cmd, c_cmd_sz + 1); + mu_assert(strlen(cmd) == c_cmd_sz, "strncpy failed"); + + sanitize(cmd); + mu_assert(strlen(cmd) == c_cmd_sz - 20, "sanitize failed"); + mu_assert(strcmp(cmd, "/create api/bacon") == 0, "sanitize failed"); + + return NULL; +} + +char *all_tests() +{ + mu_suite_start(); + + mu_run_test(test_sanitize); + + return NULL; +} + +RUN_TESTS(all_tests);