tools/nolibc: fix return type of getpagesize()
[linux-block.git] / tools / testing / selftests / nolibc / nolibc-test.c
index 7cd1aa800b8a06668c665a5ec950642df1332f1f..31f2bd789e2dde01e1e43ce21d8eafdc414babc9 100644 (file)
@@ -15,6 +15,7 @@
 #include <string.h>
 #ifndef _NOLIBC_STDIO_H
 /* standard libcs need more includes */
+#include <sys/auxv.h>
 #include <sys/io.h>
 #include <sys/ioctl.h>
 #include <sys/mman.h>
 /* will be used to test initialization of environ */
 static char **test_envp;
 
+/* will be used to test initialization of argv */
+static char **test_argv;
+
+/* will be used to test initialization of argc */
+static int test_argc;
+
 /* will be used by some test cases as readable file, please don't write it */
 static const char *argv0;
 
@@ -144,7 +151,8 @@ static void result(int llen, enum RESULT r)
        else
                msg = "[FAIL]";
 
-       putcharn(' ', 64 - llen);
+       if (llen < 64)
+               putcharn(' ', 64 - llen);
        puts(msg);
 }
 
@@ -410,6 +418,56 @@ static int expect_ptrne(const void *expr, int llen, const void *cmp)
        return ret;
 }
 
+#define EXPECT_PTRGE(cond, expr, cmp)                          \
+       do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptrge(expr, llen, cmp); } while (0)
+
+static int expect_ptrge(const void *expr, int llen, const void *cmp)
+{
+       int ret = !(expr >= cmp);
+
+       llen += printf(" = <%p> ", expr);
+       result(llen, ret ? FAIL : OK);
+       return ret;
+}
+
+#define EXPECT_PTRGT(cond, expr, cmp)                          \
+       do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptrgt(expr, llen, cmp); } while (0)
+
+static int expect_ptrgt(const void *expr, int llen, const void *cmp)
+{
+       int ret = !(expr > cmp);
+
+       llen += printf(" = <%p> ", expr);
+       result(llen, ret ? FAIL : OK);
+       return ret;
+}
+
+
+#define EXPECT_PTRLE(cond, expr, cmp)                          \
+       do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptrle(expr, llen, cmp); } while (0)
+
+static int expect_ptrle(const void *expr, int llen, const void *cmp)
+{
+       int ret = !(expr <= cmp);
+
+       llen += printf(" = <%p> ", expr);
+       result(llen, ret ? FAIL : OK);
+       return ret;
+}
+
+
+#define EXPECT_PTRLT(cond, expr, cmp)                          \
+       do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptrlt(expr, llen, cmp); } while (0)
+
+static int expect_ptrlt(const void *expr, int llen, const void *cmp)
+{
+       int ret = !(expr < cmp);
+
+       llen += printf(" = <%p> ", expr);
+       result(llen, ret ? FAIL : OK);
+       return ret;
+}
+
 #define EXPECT_PTRER2(cond, expr, expret, experr1, experr2)            \
        do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptrerr2(expr, expret, experr1, experr2, llen); } while (0)
 
@@ -511,6 +569,51 @@ static int expect_strne(const char *expr, int llen, const char *cmp)
 #define CASE_TEST(name) \
        case __LINE__: llen += printf("%d %s", test, #name);
 
+int run_startup(int min, int max)
+{
+       int test;
+       int ret = 0;
+       /* kernel at least passes HOME and TERM, shell passes more */
+       int env_total = 2;
+       /* checking NULL for argv/argv0, environ and _auxv is not enough, let's compare with sbrk(0) or &end */
+       extern char end;
+       char *brk = sbrk(0) != (void *)-1 ? sbrk(0) : &end;
+       /* differ from nolibc, both glibc and musl have no global _auxv */
+       const unsigned long *test_auxv = (void *)-1;
+#ifdef NOLIBC
+       test_auxv = _auxv;
+#endif
+
+       for (test = min; test >= 0 && test <= max; test++) {
+               int llen = 0; /* line length */
+
+               /* avoid leaving empty lines below, this will insert holes into
+                * test numbers.
+                */
+               switch (test + __LINE__ + 1) {
+               CASE_TEST(argc);             EXPECT_GE(1, test_argc, 1); break;
+               CASE_TEST(argv_addr);        EXPECT_PTRGT(1, test_argv, brk); break;
+               CASE_TEST(argv_environ);     EXPECT_PTRLT(1, test_argv, environ); break;
+               CASE_TEST(argv_total);       EXPECT_EQ(1, environ - test_argv - 1, test_argc ?: 1); break;
+               CASE_TEST(argv0_addr);       EXPECT_PTRGT(1, argv0, brk); break;
+               CASE_TEST(argv0_str);        EXPECT_STRNZ(1, argv0 > brk ? argv0 : NULL); break;
+               CASE_TEST(argv0_len);        EXPECT_GE(1,  argv0 > brk ? strlen(argv0) : 0, 1); break;
+               CASE_TEST(environ_addr);     EXPECT_PTRGT(1, environ, brk); break;
+               CASE_TEST(environ_envp);     EXPECT_PTREQ(1, environ, test_envp); break;
+               CASE_TEST(environ_auxv);     EXPECT_PTRLT(test_auxv != (void *)-1, environ, test_auxv); break;
+               CASE_TEST(environ_total);    EXPECT_GE(test_auxv != (void *)-1, (void *)test_auxv - (void *)environ - 1, env_total); break;
+               CASE_TEST(environ_HOME);     EXPECT_PTRNZ(1, getenv("HOME")); break;
+               CASE_TEST(auxv_addr);        EXPECT_PTRGT(test_auxv != (void *)-1, test_auxv, brk); break;
+               CASE_TEST(auxv_AT_UID);      EXPECT_EQ(1, getauxval(AT_UID), getuid()); break;
+               CASE_TEST(auxv_AT_PAGESZ);   EXPECT_GE(1, getauxval(AT_PAGESZ), 4096); break;
+               case __LINE__:
+                       return ret; /* must be last */
+               /* note: do not set any defaults so as to permit holes above */
+               }
+       }
+       return ret;
+}
+
 
 /* used by some syscall tests below */
 int test_getdents64(const char *dir)
@@ -533,7 +636,7 @@ int test_getdents64(const char *dir)
 
 static int test_getpagesize(void)
 {
-       long x = getpagesize();
+       int x = getpagesize();
        int c;
 
        if (x < 0)
@@ -623,7 +726,7 @@ int test_mmap_munmap(void)
 
        page_size = getpagesize();
        if (page_size < 0)
-               return -1;
+               return 1;
 
        /* find a right file to mmap, existed and accessible */
        for (i = 0; files[i] != NULL; i++) {
@@ -634,7 +737,7 @@ int test_mmap_munmap(void)
                        break;
        }
        if (ret == -1)
-               return ret;
+               return 1;
 
        ret = stat(files[i], &stat_buf);
        if (ret == -1)
@@ -654,7 +757,7 @@ int test_mmap_munmap(void)
 
        mem = mmap(NULL, length + offset - pa_offset, PROT_READ, MAP_SHARED, fd, pa_offset);
        if (mem == MAP_FAILED) {
-               ret = -1;
+               ret = 1;
                goto end;
        }
 
@@ -662,7 +765,28 @@ int test_mmap_munmap(void)
 
 end:
        close(fd);
-       return ret;
+       return !!ret;
+}
+
+static int test_pipe(void)
+{
+       const char *const msg = "hello, nolibc";
+       int pipefd[2];
+       char buf[32];
+       size_t len;
+
+       if (pipe(pipefd) == -1)
+               return 1;
+
+       write(pipefd[1], msg, strlen(msg));
+       close(pipefd[1]);
+       len = read(pipefd[0], buf, sizeof(buf));
+       close(pipefd[0]);
+
+       if (len != strlen(msg))
+               return 1;
+
+       return !!memcmp(buf, msg, len);
 }
 
 
@@ -750,6 +874,7 @@ int run_syscall(int min, int max)
                CASE_TEST(mmap_munmap_good);  EXPECT_SYSZR(1, test_mmap_munmap()); break;
                CASE_TEST(open_tty);          EXPECT_SYSNE(1, tmp = open("/dev/null", 0), -1); if (tmp != -1) close(tmp); break;
                CASE_TEST(open_blah);         EXPECT_SYSER(1, tmp = open("/proc/self/blah", 0), -1, ENOENT); if (tmp != -1) close(tmp); break;
+               CASE_TEST(pipe);              EXPECT_SYSZR(1, test_pipe()); break;
                CASE_TEST(poll_null);         EXPECT_SYSZR(1, poll(NULL, 0, 0)); break;
                CASE_TEST(poll_stdout);       EXPECT_SYSNE(1, ({ struct pollfd fds = { 1, POLLOUT, 0}; poll(&fds, 1, 0); }), -1); break;
                CASE_TEST(poll_fault);        EXPECT_SYSER(1, poll((void *)1, 1, 0), -1, EFAULT); break;
@@ -795,7 +920,6 @@ int run_stdlib(int min, int max)
                 * test numbers.
                 */
                switch (test + __LINE__ + 1) {
-               CASE_TEST(environ);            EXPECT_PTREQ(1, environ, test_envp); break;
                CASE_TEST(getenv_TERM);        EXPECT_STRNZ(1, getenv("TERM")); break;
                CASE_TEST(getenv_blah);        EXPECT_STRZR(1, getenv("blah")); break;
                CASE_TEST(setcmp_blah_blah);   EXPECT_EQ(1, strcmp("blah", "blah"), 0); break;
@@ -1079,6 +1203,7 @@ int prepare(void)
 /* This is the definition of known test names, with their functions */
 static const struct test test_names[] = {
        /* add new tests here */
+       { .name = "startup",    .func = run_startup    },
        { .name = "syscall",    .func = run_syscall    },
        { .name = "stdlib",     .func = run_stdlib     },
        { .name = "vfprintf",   .func = run_vfprintf   },
@@ -1125,6 +1250,8 @@ int main(int argc, char **argv, char **envp)
        char *test;
 
        argv0 = argv[0];
+       test_argc = argc;
+       test_argv = argv;
        test_envp = envp;
 
        /* when called as init, it's possible that no console was opened, for