Add common module unit tests 37/226437/15
authorAgnieszka Baumann <a.baumann@samsung.com>
Wed, 19 Feb 2020 11:27:47 +0000 (12:27 +0100)
committerMichal Bloch <m.bloch@partner.samsung.com>
Thu, 4 Jun 2020 12:57:23 +0000 (14:57 +0200)
Change-Id: I4f376da7f1433dd6e4357080abc7927cf7b2c8a3

tests/CMakeLists.txt
tests/test-common.c

index 3620784..8007e7d 100644 (file)
@@ -70,6 +70,7 @@ ADD_TESTS(cmocka-dbus-get-cpu-lists "${UNIT_TESTS_CFLAGS}" "-Wl,--wrap=fread_int
 ADD_TESTS(cmocka-file-helper "${UNIT_TESTS_CFLAGS}" "-Wl,--wrap=fopen,--wrap=fputs,--wrap=fscanf,--wrap=fgets,--wrap=fopen64,--wrap=vfscanf,--wrap=fscanf64,--wrap=__isoc99_fscanf,--wrap=asprintf -O0" cmocka-file-helper.c)
 ADD_TESTS(cmocka-test-child-pid "${UNIT_TESTS_CFLAGS}" "-Wl,--wrap=fread_int,--wrap=fread_uint,--wrap=fread_ulong,--wrap=g_slist_prepend,--wrap=g_slist_remove -O0" cmocka-test-child-pid.c)
 ADD_TESTS(cmocka-proc-app-list "${UNIT_TESTS_CFLAGS}" "-Wl,--wrap=fread_int,--wrap=fread_uint,--wrap=fread_ulong,--wrap=g_slist_prepend,--wrap=g_slist_remove,--wrap=g_slist_prepend,--wrap=g_slist_remove -O0" cmocka-proc-app-list.c)
+ADD_TESTS(test-common "${UNIT_TESTS_CFLAGS}" "-O0" test-common.c)
 
 function(ADD_SKIP_TEST name wraps sources)
        ADD_EXECUTABLE(${name} ${sources})
@@ -100,7 +101,6 @@ ADD_SKIP_TEST(test-freezer "-O0" test-freezer.c)
 ADD_SKIP_TEST(test-cpu "-O0" test-cpu.c)
 ADD_SKIP_TEST(test-block "-O0" test-block.c)
 ADD_SKIP_TEST(test-block-monitor "-O0" test-block-monitor.c)
-ADD_SKIP_TEST(test-common "-O0" test-common.c)
 ADD_SKIP_TEST(test-common-procfs "-O0" test-common-procfs.c)
 ADD_SKIP_TEST(test-vip-agent "-O0" test-vip-agent.c)
 
index 4c0e6fd..c659557 100644 (file)
  *
 */
 
+#include "resourced.h"
+#include "util.h"
+
+#include <assert.h>
+#include <errno.h>
+#include <stdarg.h>
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+const char *LONG = "1234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910";
+
+void test_streq_ptr_positive()
+{
+       assert(streq_ptr(NULL, NULL));
+       assert(streq_ptr("", ""));
+       assert(streq_ptr(".word", ".word"));
+       assert(streq_ptr("UpperCase", "UpperCase"));
+       assert(streq_ptr("LOWERCase", "LOWERCase"));
+       assert(streq_ptr("      tabulator", "   tabulator"));
+       assert(streq_ptr(LONG, LONG));
+       assert(streq_ptr("🥌", "🥌"));
+       assert(!(streq_ptr("word", NULL)));
+       assert(!(streq_ptr(NULL, "word")));
+       assert(!(streq_ptr("word", "")));
+       assert(!(streq_ptr("Word", "word")));
+       assert(!(streq_ptr("word", " word")));
+       assert(!streq_ptr(LONG, NULL));
+       assert(!streq_ptr(LONG, "word"));
+}
+
+void test_parse_boolean_positive()
+{
+       assert(parse_boolean("1"));
+       assert(parse_boolean(LONG));
+       assert(parse_boolean("y🥌"));
+       assert(parse_boolean("y"));
+       assert(parse_boolean("Y"));
+       assert(parse_boolean("yes"));
+       assert(parse_boolean("Yes"));
+       assert(parse_boolean("y e s"));
+       assert(parse_boolean("true"));
+       assert(parse_boolean("True"));
+       assert(parse_boolean("t"));
+       assert(parse_boolean("T"));
+       assert(parse_boolean("True"));
+       assert(parse_boolean("ON"));
+       assert(parse_boolean("on"));
+       assert(parse_boolean("enable"));
+       assert(parse_boolean("ENABLE"));
+       assert(parse_boolean("ENaBLED"));
+
+       assert(!(parse_boolean("0")));
+       assert(!(parse_boolean("N")));
+       assert(!(parse_boolean("n")));
+       assert(!(parse_boolean("n o")));
+       assert(!(parse_boolean("NO")));
+       assert(!(parse_boolean("f")));
+       assert(!(parse_boolean("F")));
+       assert(!(parse_boolean("f a l s e")));
+       assert(!(parse_boolean("fAlSe")));
+       assert(!(parse_boolean("off")));
+       assert(!(parse_boolean("diSaBle")));
+       assert(!(parse_boolean("DISABLED")));
+}
+
+void test_parse_boolean_negative()
+{
+       assert(parse_boolean("e n a b l e d") == -EINVAL);
+       assert(parse_boolean("O N") == -EINVAL);
+       assert(parse_boolean("o n") == -EINVAL);
+       assert(parse_boolean("11111") == -EINVAL);
+       assert(parse_boolean("") == -EINVAL);
+       assert(parse_boolean("0000") == -EINVAL);
+       assert(parse_boolean("DISA BLED") == -EINVAL);
+       assert(parse_boolean("DDDDDISABLED") == -EINVAL);
+       assert(parse_boolean("offoff") == -EINVAL);
+       assert(parse_boolean("2") == -EINVAL);
+       assert(parse_boolean("O FF") == -EINVAL);
+       assert(parse_boolean("") == -EINVAL);
+       assert(parse_boolean("doesntexist") == -EINVAL);
+       assert(parse_boolean("🥌") == -EINVAL);
+}
+
+void test_parse_bytes_positive()
+{
+       size_t w;
+
+       assert(parse_bytes("012345678B", &w) == 0);
+       /*result is always in bytes*/
+       assert(w == 12345678);
+
+       assert(parse_bytes("789K", &w) == 0);
+       assert(w == 789U * 1024);
+
+       assert(parse_bytes("001M", &w) == 0);
+       assert(w == 1U * 1024 * 1024);
+
+       assert(parse_bytes("0M", &w) == 0);
+       assert(w == 0U * 1024 * 1024);
+
+       assert(parse_bytes("23232323", &w) == 0);
+       assert(w = 23232323);
+
+       assert(parse_bytes("1G", &w) == 0);
+       assert(w == 1U * 1024 * 1024 * 1024);
+
+       assert(parse_bytes("2G", &w) == 0);
+       assert(w == 2U * 1024 * 1024 * 1024);
+
+       assert(parse_bytes("000000000000000000000000000000000000000000000000000000000000000000000000000000", &w) == 0);
+       assert(w == 0);
+}
+
+void test_parse_bytes_negative()
+{
+       size_t w;
+
+       int expected_err = (sizeof w == 4) ? -ERANGE : 0; /* it depends on architecture: 32-bit or 64-bit */
+
+       assert(parse_bytes("4G", &w) == expected_err);
+
+       assert(parse_bytes("777G", &w) == expected_err);
+
+       assert(parse_bytes("17179869183G", &w) == expected_err);
+
+       assert(parse_bytes("-1G", &w) == -EINVAL);
+
+       assert(parse_bytes("-21", &w) == -EINVAL);
+
+       assert(parse_bytes("-21B", &w) == -EINVAL);
 
-int main(int argc, char* argv[])
+       assert(parse_bytes("🥌", &w) == -EINVAL);
+
+       assert(parse_bytes("1KG", &w) == -EINVAL);
+
+       assert(parse_bytes("1234567891011121314151617181912021222324252627282930G", &w) == -ERANGE);
+
+       assert(parse_bytes(LONG, &w) == -ERANGE);
+
+       assert(parse_bytes("G", &w) == -EINVAL);
+
+       assert(parse_bytes("B", &w) == -EINVAL);
+
+       assert(parse_bytes("BKMG", &w) == -EINVAL);
+
+       assert(parse_bytes("K9", &w) == -EINVAL);
+
+       assert(parse_bytes("1X", &w) == -EINVAL);
+
+       assert(parse_bytes("1g", &w) == -EINVAL);
+
+       assert(parse_bytes("2m", &w) == -EINVAL);
+}
+
+void test_split_positive()
+{
+       char c[] = "::Lorem::ipsum";
+       size_t l;
+       char st[] = "Some::other:::text:";
+       char *state = &st;
+
+       assert(strcmp("Some::other:::text:", split(&c, &l, ":", &state)) == 0);
+       assert(strcmp(c, "::Lorem::ipsum") == 0);
+       assert(l == 4);
+       assert(strcmp(state, "::other:::text:") == 0);
+
+       assert(strcmp("other:::text:", split(&c, &l, ":", &state)) == 0);
+       assert(strcmp(c, "::Lorem::ipsum") == 0);
+       assert(l == 5);
+       assert(strcmp(state, ":::text:") == 0);
+
+       assert(strcmp("text:", split(&c, &l, ":", &state)) == 0);
+       assert(strcmp(c, "::Lorem::ipsum") == 0);
+       assert(l == 4);
+       assert(strcmp(state, ":") == 0);
+
+       assert(strcmp("", split(&c, &l, ":", &state)) == 0);
+       assert(strcmp(c, "::Lorem::ipsum") == 0);
+       assert(l == 0);
+       assert(strcmp(state, "") == 0);
+
+       /* the state is a null pointer */
+       state = NULL;
+
+       assert(strcmp("Lorem::ipsum", split(&c, &l, ":", &state)) == 0);
+       assert(strcmp(c, "::Lorem::ipsum") == 0);
+       assert(l == 5);
+       assert(strcmp(state, "::ipsum") == 0);
+
+       assert(strcmp("ipsum", split(&c, &l, ":", &state)) == 0);
+       assert(strcmp(c, "::Lorem::ipsum") == 0);
+       assert(l == 5);
+       assert(strcmp(state, "") == 0);
+
+       /* the separator is empty */
+       state = &st;
+
+       assert(strcmp("Some::other:::text:", split(&c, &l, "", &state)) == 0);
+       assert(strcmp(c, "::Lorem::ipsum") == 0);
+       assert(l == 19);
+       assert(strcmp(state, "") == 0);
+
+       /* the string is empty */
+       char cc[] = "";
+       char str[] = "";
+       state = &str;
+       l = 12345;
+
+       char *tmp = split(&cc, &l, ":", &state);
+       assert(tmp == NULL);
+       assert(strcmp(cc, "") == 0);
+       assert(l == 12345);
+       assert(strcmp(state, "") == 0);
+}
+
+void test_truncate_nl_positive()
 {
-       return SKIP_TEST;
+       char s[] = "some text\n";
+       assert(strcmp(truncate_nl(s), "some text") == 0);
+       assert(strcmp(s, "some text") == 0);
+
+       char r[] = "2\n\nwith digit";
+       assert(strcmp(truncate_nl(r), "2") == 0);
+       assert(strcmp(r, "2") == 0);
+
+       char t[] = "\nnew line is at the beginning of the string";
+       assert(strcmp(truncate_nl(t), "") == 0);
+       assert(strcmp(t, "") == 0);
+
+       char w[] = "\n";
+       assert(strcmp(truncate_nl(w), "") == 0);
+       assert(strcmp(w, "") == 0);
+
+       char x[] = "without end line";
+       assert(strcmp(truncate_nl(x), "without end line") == 0);
+       assert(strcmp(x, "without end line") == 0);
+
+       char z[] = "";
+       assert(strcmp(truncate_nl(z), "") == 0);
+       assert(strcmp(z, "") == 0);
+}
+
+void test_strstrip_positive()
+{
+       char s[] = "[ erase        white \nsigns    \n";
+       assert(strcmp(strstrip(s), "[ erase        white \nsigns") == 0);
+
+       char r[] = "[   other t e x t          \n";
+       assert(strcmp(strstrip(r), "[   other t e x t") == 0 );
+
+       char x[] = "    \n";
+       assert(strcmp(strstrip(x), "") == 0);
+       assert(strcmp(x, "") == 0);
+
+       char w[] = " as d asd  d    as d   abc";
+       assert(strcmp(strstrip(w), "as d asd  d    as d   abc") == 0);
+       assert(strcmp(w, " as d asd  d    as d   abc") == 0);
+
+       char y[] = " abc   \n";
+       assert(strcmp(strstrip(y), "abc") == 0);
+       assert(strcmp(y, " abc") == 0);
+
+       char t[] = "";
+       assert(strcmp(strstrip(t), "") == 0);
+
+       char z[] = "[ erase        white        \n signs";
+       assert(strcmp(strstrip(z), "[ erase        white        \n signs") == 0);
+}
+
+void test_str_to_strv_positive()
+{
+       char str[] = ":very::long:::string::::";
+       char **strv;
+       str_to_strv(&str, &strv, ":");
+
+       assert(sizeof_strv(strv) == 4);
+       assert(strcmp(strv[0], "very") == 0);
+       assert(strcmp(strv[1], "long") == 0);
+       assert(strcmp(strv[2], "string") == 0);
+       assert(strcmp(strv[3], "") == 0);
+       assert(strv[4] == NULL);
+
+       strv_free_full(&strv);
+       assert(strv == NULL);
+
+       /* two separators */
+       char **wstrv;
+       str_to_strv(&str, &wstrv, ":r");
+
+       assert(sizeof_strv(wstrv) == 6);
+       assert(strcmp(wstrv[0], "ve") == 0);
+       assert(strcmp(wstrv[1], "y") == 0);
+       assert(strcmp(wstrv[2], "long") == 0);
+       assert(strcmp(wstrv[3], "st") == 0);
+       assert(strcmp(wstrv[4], "ing") == 0);
+       assert(strcmp(wstrv[5], "") == 0);
+       assert(wstrv[6] == NULL);
+
+       strv_free_full(&wstrv);
+       assert(wstrv == NULL);
+
+       /* there is no 'p', but there is ':' */
+       char **xstrv;
+       str_to_strv(&str, &xstrv, ":p");
+
+       assert(sizeof_strv(xstrv) == 4);
+       assert(strcmp(xstrv[0], "very") == 0);
+       assert(strcmp(xstrv[1], "long") == 0);
+       assert(strcmp(xstrv[2], "string") == 0);
+       assert(strcmp(xstrv[3], "") == 0);
+       assert(xstrv[4] == NULL);
+
+       strv_free_full(&xstrv);
+       assert(xstrv == NULL);
+
+       /* with no separator at the end */
+       char st[] = ":not:so::long";
+       char **astrv;
+
+       str_to_strv(&st, &astrv, ":");
+
+       assert(sizeof_strv(astrv) == 3);
+       assert(strcmp(astrv[0], "not") == 0);
+       assert(strcmp(astrv[1], "so") == 0);
+       assert(strcmp(astrv[2], "long") == 0);
+       assert(astrv[3] == NULL);
+
+       strv_free_full(&astrv);
+       assert(astrv == NULL);
+
+       /* with empty separator */
+       char **bstrv;
+       str_to_strv(&st, &bstrv, "");
+
+       assert(sizeof_strv(bstrv) == 1);
+       assert(strcmp(bstrv[0], ":not:so::long") == 0);
+       assert(bstrv[1] == NULL);
+
+       strv_free_full(&bstrv);
+       assert(bstrv == NULL);
+
+       /* there is no 'k' in the string, so it won't be split */
+       char ptr[] = ":very::long:::string::::";
+       char **zstrv;
+
+       str_to_strv(&ptr, &zstrv, "k");
+
+       assert(sizeof_strv(zstrv) == 1);
+       assert(strcmp(zstrv[0], ":very::long:::string::::") == 0);
+       assert(zstrv[1] == NULL);
+
+       strv_free_full(&zstrv);
+       assert(zstrv == NULL);
+}
+
+void test_strv_attach_positive()
+{
+       char str[] = "c:d:";
+       char **strv;
+       char **swsk;
+       str_to_strv(&str, &strv, ":");
+
+       char **d = malloc(3 * sizeof(char *));
+       assert(d);
+       d[0] = strdup("a");
+       d[1] = strdup("b");
+       d[2] = NULL;
+       assert(d[0]);
+       assert(d[1]);
+
+       assert(sizeof_strv(strv) == 3);
+       assert(strcmp(strv[0], "c") == 0);
+       assert(strcmp(strv[1], "d") == 0);
+       assert(strcmp(strv[2], "") == 0);
+       assert(strv[3] == NULL);
+
+       strv_attach(strv, d, &swsk, true);
+
+       assert(sizeof_strv(swsk) == 5);
+       assert(strcmp(swsk[0], "c") == 0);
+       assert(strcmp(swsk[1], "d") == 0);
+       assert(strcmp(swsk[2], "") == 0);
+       assert(strcmp(swsk[3], "a") == 0);
+       assert(strcmp(swsk[4], "b") == 0);
+       assert(swsk[5] == NULL);
+
+       str_to_strv(&str, &strv, ":");
+       char *dr[3] = {"a", "b", NULL};
+
+       assert(sizeof_strv(strv) == 3);
+       assert(strcmp(strv[0], "c") == 0);
+       assert(strcmp(strv[1], "d") == 0);
+       assert(strcmp(strv[2], "") == 0);
+       assert(strv[3] == NULL);
+
+       strv_attach(strv, &dr, &swsk, false);
+
+       assert(sizeof_strv(swsk) == 5);
+       assert(strcmp(swsk[0], "c") == 0);
+       assert(strcmp(swsk[1], "d") == 0);
+       assert(strcmp(swsk[2], "") == 0);
+       assert(strcmp(swsk[3], "a") == 0);
+       assert(strcmp(swsk[4], "b") == 0);
+       assert(swsk[5] == NULL);
+
+       /* with no separator at the end */
+       char str2[] = "e:f";
+       char **rtrv;
+
+       str_to_strv(&str, &strv, ":");
+       str_to_strv(&str2, &rtrv, ":");
+
+       assert(sizeof_strv(strv) == 3);
+       assert(strcmp(strv[0], "c") == 0);
+       assert(strcmp(strv[1], "d") == 0);
+       assert(strcmp(strv[2], "") == 0);
+       assert(strv[3] == NULL);
+
+       assert(sizeof_strv(rtrv) == 2);
+       assert(strcmp(rtrv[0], "e") == 0);
+       assert(strcmp(rtrv[1], "f") == 0);
+       assert(rtrv[2] == NULL);
+
+       strv_attach(strv, rtrv, &swsk, false);
+
+       assert(sizeof_strv(swsk) == 5);
+       assert(strcmp(swsk[0], "c") == 0);
+       assert(strcmp(swsk[1], "d") == 0);
+       assert(strcmp(swsk[2], "") == 0);
+       assert(strcmp(swsk[3], "e") == 0);
+       assert(strcmp(swsk[4], "f") == 0);
+       assert(swsk[5] == NULL);
+
+       strv_free_full(&swsk);
+       assert(swsk == NULL);
+
+       /* with empty strings and separators*/
+       char str3[] = "";
+       char str4[] = "";
+
+       char **ftrv;
+       char **ptrv;
+       char **fwsk;
+
+       str_to_strv(&str3, &ftrv, "");
+       str_to_strv(&str4, &ptrv, "");
+       assert(ftrv == NULL);
+       assert(ptrv == NULL);
+
+       strv_attach(ftrv, ptrv, &fwsk, false);
+       assert(fwsk == NULL);
+
+       strv_free_full(&swsk);
+       assert(fwsk == NULL);
+}
+
+void positive_tests()
+{
+       test_streq_ptr_positive();
+       test_parse_boolean_positive();
+       test_parse_bytes_positive();
+       test_split_positive();
+       test_truncate_nl_positive();
+       test_strstrip_positive();
+       test_str_to_strv_positive();
+       test_strv_attach_positive();
+}
+
+void negative_tests()
+{
+       test_parse_boolean_negative();
+       test_parse_bytes_negative();
+}
+
+int main()
+{
+       positive_tests();
+       negative_tests();
+
+       return 0;
 }