TC: Add missing test cases of Libc misc
authorLokesh B V <lokesh.bv@partner.samsung.com>
Fri, 18 Aug 2017 09:21:42 +0000 (14:51 +0530)
committerLokesh B V <lokesh.bv@partner.samsung.com>
Thu, 14 Sep 2017 13:33:23 +0000 (19:03 +0530)
Adds tc for lib_dumpbuffer, sendfile and match api's.

NOTE: As lib_dumpbuffer return void, the success or failure result should be
checked manually by looking into tc output.
It has 2 cases,
1. dumps "53414d53554e472d54697a656e5254 SAMSUNG-TizenRT".
2. dumps "0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a ..............." (i.e, 256 "0a").

Signed-off-by: Lokesh B V <lokesh.bv@partner.samsung.com>
apps/examples/testcase/le_tc/filesystem/fs_main.c
apps/examples/testcase/le_tc/kernel/tc_libc_misc.c

index f23cc57..383bf50 100644 (file)
 #  define STDERR_FILENO 2
 #endif
 
+#define INV_FD -3
+
 /****************************************************************************
  * Global Variables
  ****************************************************************************/
@@ -1012,6 +1014,7 @@ static void fs_vfs_sendfile_tc(void)
        struct stat st;
        int fd1, fd2, ret;
        off_t size;
+       off_t offset;
 
        snprintf(dest_file, sizeof(dest_file), "%s_dest", src_file);
 
@@ -1022,17 +1025,69 @@ static void fs_vfs_sendfile_tc(void)
        TC_ASSERT_EQ_CLEANUP("stat", ret, OK, close(fd1));
 
        size = st.st_size;
+       /* case-1: offset = 0 */
        fd2 = open(dest_file, O_WRONLY | O_CREAT);
        TC_ASSERT_GEQ_CLEANUP("open", fd2, 0, close(fd1));
 
        ret = sendfile(fd2, fd1, 0, size);
-       close(fd1);
        close(fd2);
-       TC_ASSERT_EQ("sendfile", ret, size);
+       TC_ASSERT_EQ_CLEANUP("sendfile", ret, size, close(fd1));
 
        ret = stat(dest_file, &st);
+       TC_ASSERT_EQ_CLEANUP("stat", ret, OK, close(fd1));
+       TC_ASSERT_EQ_CLEANUP("stat", st.st_size, size, close(fd1));
+
+       /* case-2: offset = 1 */
+       fd2 = open(dest_file, O_WRONLY | O_CREAT);
+       TC_ASSERT_GEQ_CLEANUP("open", fd2, 0, close(fd1));
+
+       offset = 1;
+       ret = sendfile(fd2, fd1, &offset, size - 1);
+       close(fd2);
+       TC_ASSERT_EQ_CLEANUP("sendfile", ret, size - 1, close(fd1));
+
+       ret = stat(dest_file, &st);
+       TC_ASSERT_EQ_CLEANUP("stat", ret, OK, close(fd1));
+       TC_ASSERT_EQ_CLEANUP("stat", st.st_size, size - 1, close(fd1));
+
+       /* case-3: offset = 1, invalid input fd, returns ERROR */
+       fd2 = open(dest_file, O_WRONLY | O_CREAT);
+       TC_ASSERT_GEQ_CLEANUP("open", fd2, 0, close(fd1));
+
+       offset = 1;
+       ret = sendfile(fd2, INV_FD, &offset, size - 1);
+       close(fd2);
+       TC_ASSERT_EQ_CLEANUP("sendfile", ret, ERROR, close(fd1));
+
+       /* case-4: invalid input fd, returns ERROR */
+       fd2 = open(dest_file, O_WRONLY | O_CREAT);
+       TC_ASSERT_GEQ_CLEANUP("open", fd2, 0, close(fd1));
+
+       ret = sendfile(fd2, INV_FD, NULL, size);
+       close(fd2);
+       TC_ASSERT_EQ_CLEANUP("sendfile", ret, ERROR, close(fd1));
+
+       /* case-5: offset = 0, invalid output fd, returns ERROR */
+       offset = 0;
+       ret = sendfile(INV_FD, fd1, &offset, size);
+       TC_ASSERT_EQ_CLEANUP("sendfile", ret, ERROR, close(fd1));
+
+       /* case-6: current offset of input file is EOF, returns ERROR */
+       fd2 = open(dest_file, O_WRONLY | O_CREAT);
+       TC_ASSERT_GEQ_CLEANUP("open", fd2, 0, close(fd1));
+
+       ret = lseek(fd1, 0, SEEK_END);
+       TC_ASSERT_EQ_CLEANUP("lseek", ret, size,  close(fd1);close(fd2));
+
+       ret = sendfile(fd2, fd1, NULL, size);
+       close(fd2);
+       TC_ASSERT_EQ_CLEANUP("sendfile", ret, 0, close(fd1));
+
+       ret = stat(dest_file, &st);
+       close(fd1);
        TC_ASSERT_EQ("stat", ret, OK);
-       TC_ASSERT_EQ("stat", size, st.st_size);
+       TC_ASSERT_EQ("stat", st.st_size, 0);
+
        TC_SUCCESS_RESULT();
 }
 
index ecaf80d..cc9531a 100644 (file)
@@ -26,6 +26,7 @@
 #include <tinyara/config.h>
 #include <stdio.h>
 #include <string.h>
+#include <tinyara/regex.h>
 #include <unistd.h>
 #include <errno.h>
 #include <libgen.h>
@@ -34,6 +35,7 @@
 #include <crc32.h>
 #include "tc_internal.h"
 
+#define BUFF_SIZE 256
 #define USEC_100 100
 #define VAL_50 50
 #define VAL_71 71
@@ -347,6 +349,73 @@ static void tc_libc_misc_vdbg(void)
        TC_SUCCESS_RESULT();
 }
 
+/**
+ * @fn                  :tc_libc_misc_lib_dumpbuffer
+ * @brief               :Do a pretty buffer dump
+ * @scenario            :Do a pretty buffer dump
+ * @API's covered       :lib_dumpbuffer
+ * @Preconditions       :None
+ * @Postconditions      :None
+ * @Return              :void
+ */
+static void tc_libc_misc_lib_dumpbuffer(void)
+{
+       const char *msg = "tc_libc_misc_lib_dumpbuffer";
+       unsigned char buffer[] = { 'S', 'A', 'M', 'S', 'U', 'N', 'G', '-', 'T', 'i', 'z', 'e', 'n', 'R', 'T' };
+       unsigned char *buf = NULL;
+       int idx;
+
+       /**
+        * As lib_dumpbuffer returns void, there in no way to check the success or failure case
+        * we need to manually check the dump output
+        * Case-1: output: 53414d53554e472d54697a656e5254 SAMSUNG-TizenRT
+        * Case-2: output: 0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a ...............       (i.e, 256 "0a")
+        */
+
+       /* Case-1 */
+       lib_dumpbuffer(msg, buffer, sizeof(buffer));
+
+       /* Case-2 */
+       buf = (unsigned char *)malloc(BUFF_SIZE);
+       TC_ASSERT_NEQ("malloc", buf, NULL);
+
+       for (idx = 0; idx < BUFF_SIZE; idx++) {
+               buf[idx] = 0xA;
+       }
+       lib_dumpbuffer(msg, buf, BUFF_SIZE);
+
+       free(buf);
+       buf = NULL;
+
+       TC_SUCCESS_RESULT();
+}
+
+/**
+ * @fn                  :tc_libc_misc_match
+ * @brief               :Simple shell-style filename pattern matcher
+ * @scenario            :Returns 1 (match) or 0 (no-match)
+ * @API's covered       :match
+ * @Preconditions       :None
+ * @Postconditions      :None
+ * @Return              :void
+ */
+static void tc_libc_misc_match(void)
+{
+       const char *pattern = "?|";
+       const char *string = "S";
+       int ret;
+
+       ret = match(pattern, string);
+       TC_ASSERT_EQ("match", ret, 1);
+
+       pattern = "somepattern";
+       string = "nomatch";
+       ret = match(pattern, string);
+       TC_ASSERT_EQ("match", ret, 0);
+
+       TC_SUCCESS_RESULT();
+}
+
 /****************************************************************************
  * Name: libc_misc
  ****************************************************************************/
@@ -362,6 +431,7 @@ int libc_misc_main(void)
 #ifdef CONFIG_DEBUG
 #ifdef CONFIG_DEBUG_ERROR
        tc_libc_misc_dbg();
+       tc_libc_misc_lib_dumpbuffer();
 #ifdef CONFIG_ARCH_LOWPUTC
        tc_libc_misc_lldbg();
 #endif
@@ -374,6 +444,7 @@ int libc_misc_main(void)
 #endif
 #endif
 #endif /* CONFIG_DEBUG */
+       tc_libc_misc_match();
 
        return 0;
 }