]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
[Bug 3692] /dev/gpsN requirement prevents KPPS
authorJuergen Perlinger <perlinger@ntp.org>
Fri, 16 Oct 2020 06:11:40 +0000 (08:11 +0200)
committerJuergen Perlinger <perlinger@ntp.org>
Fri, 16 Oct 2020 06:11:40 +0000 (08:11 +0200)
 - first unit tests (not applicable for windows so far)

bk: 5f89399cY4sCcsxC7_xsgC4ZYarAMQ

tests/libntp/Makefile.am
tests/libntp/realpath.c [new file with mode: 0644]
tests/libntp/run-realpath.c [new file with mode: 0644]

index 67b34e7c0fc70c9af2ed4203b40313e1b58a2938..740fbd4852ddc469e404e1eea299c69f5ac7e049 100644 (file)
@@ -37,6 +37,7 @@ check_PROGRAMS =              \
        test-numtohost          \
        test-octtoint           \
        test-prettydate         \
+       test-realpath           \
        test-recvbuff           \
        test-refidsmear         \
        test-refnumtoa          \
@@ -102,6 +103,7 @@ BUILT_SOURCES +=                    \
        $(srcdir)/run-numtohost.c       \
        $(srcdir)/run-octtoint.c        \
        $(srcdir)/run-prettydate.c      \
+       $(srcdir)/run-realpath.c        \
        $(srcdir)/run-recvbuff.c        \
        $(srcdir)/run-refidsmear.c      \
        $(srcdir)/run-refnumtoa.c       \
@@ -382,6 +384,16 @@ $(srcdir)/run-prettydate.c: $(srcdir)/prettydate.c $(std_unity_list)
 
 ###
 
+test_realpath_SOURCES =                \
+       realpath.c              \
+       run-realpath.c          \
+       $(NULL)
+
+$(srcdir)/run-realpath.c: $(srcdir)/realpath.c $(std_unity_list)
+       $(run_unity) $< $@
+
+###
+
 test_recvbuff_SOURCES =                \
        recvbuff.c              \
        run-recvbuff.c          \
diff --git a/tests/libntp/realpath.c b/tests/libntp/realpath.c
new file mode 100644 (file)
index 0000000..996837c
--- /dev/null
@@ -0,0 +1,85 @@
+#include "config.h"
+
+#include "ntp_stdlib.h"
+#include "unity.h"
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <dirent.h>
+#include <stdarg.h>
+
+static char * resolved;
+
+void setUp(void);
+void setUp(void) {
+       resolved = NULL;
+}
+
+void tearDown(void);
+void tearDown(void) {
+       free(resolved);
+       resolved = NULL;
+}
+
+static int/*BOOL*/ isValidAbsPath(const char * path)
+{
+       int retv = FALSE;
+       /* this needs some elaboration: */
+       if (path && path[0] == '/') {
+               struct stat sb;
+               if (0 == lstat(path, &sb)) {
+                       retv = (sb.st_mode & S_IFMT) != S_IFLNK;
+               }
+       }
+       return retv;
+}
+
+static const char * errMsg(const char *fmt, ...)
+{
+       static char buf[256];
+       va_list va;
+       va_start(va, fmt);
+       vsnprintf(buf, sizeof(buf), fmt, va);
+       va_end(va);
+       return buf;
+}
+
+void test_CurrentWorkingDir(void);
+void test_CurrentWorkingDir(void) {
+#   ifdef SYS_WINNT
+       TEST_IGNORE_MESSAGE("not applicable to windows so far");
+#   else
+       resolved = ntp_realpath(".");
+       TEST_ASSERT_NOT_NULL_MESSAGE(resolved, "failed to resolve '.'");
+       TEST_ASSERT_TRUE_MESSAGE(isValidAbsPath(resolved), "'.' not resolved to absolute path");
+#   endif
+}
+
+void test_DevLinks(void);
+void test_DevLinks(void) {
+#   ifdef SYS_WINNT
+       TEST_IGNORE_MESSAGE("not applicable to windows so far");
+#   else
+       char            nam[80];
+       char            abs[80];
+       struct dirent * ent;
+       DIR           * dfs = opendir("/dev");
+
+       TEST_ASSERT_NOT_NULL_MESSAGE(dfs, "failed to open '/dev' !?!");
+       while (NULL != (ent = readdir(dfs))) {
+               snprintf(nam, sizeof(nam), "/dev/%s", ent->d_name);
+               resolved = ntp_realpath(nam);
+               TEST_ASSERT_NOT_NULL_MESSAGE(resolved, errMsg("could not resolve '%s'", nam));
+               strlcpy(abs, resolved, sizeof(abs));
+               free(resolved);
+               resolved = NULL;
+               /* test/development code:
+               if (strcmp(nam, abs))
+                       printf(" '%s' --> '%s'\n", nam, abs);
+               */
+               TEST_ASSERT_TRUE_MESSAGE(isValidAbsPath(abs), errMsg("could not validate '%s'", abs));
+       }
+       closedir(dfs);
+#   endif
+}
diff --git a/tests/libntp/run-realpath.c b/tests/libntp/run-realpath.c
new file mode 100644 (file)
index 0000000..ebabdab
--- /dev/null
@@ -0,0 +1,70 @@
+/* AUTOGENERATED FILE. DO NOT EDIT. */
+
+//=======Test Runner Used To Run Each Test Below=====
+#define RUN_TEST(TestFunc, TestLineNum) \
+{ \
+  Unity.CurrentTestName = #TestFunc; \
+  Unity.CurrentTestLineNumber = TestLineNum; \
+  Unity.NumberOfTests++; \
+  if (TEST_PROTECT()) \
+  { \
+      setUp(); \
+      TestFunc(); \
+  } \
+  if (TEST_PROTECT() && !TEST_IS_IGNORED) \
+  { \
+    tearDown(); \
+  } \
+  UnityConcludeTest(); \
+}
+
+//=======Automagically Detected Files To Include=====
+#include "unity.h"
+#include <setjmp.h>
+#include <stdio.h>
+#include "config.h"
+#include "ntp_stdlib.h"
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <dirent.h>
+#include <stdarg.h>
+
+//=======External Functions This Runner Calls=====
+extern void setUp(void);
+extern void tearDown(void);
+extern void test_CurrentWorkingDir(void);
+extern void test_DevLinks(void);
+
+
+//=======Suite Setup=====
+static void suite_setup(void)
+{
+extern int change_iobufs(int);
+extern int change_logfile(const char*, int);
+change_iobufs(1);
+change_logfile("stderr", 0);
+}
+
+//=======Test Reset Option=====
+void resetTest(void);
+void resetTest(void)
+{
+  tearDown();
+  setUp();
+}
+
+char const *progname;
+
+
+//=======MAIN=====
+int main(int argc, char *argv[])
+{
+  progname = argv[0];
+  suite_setup();
+  UnityBegin("realpath.c");
+  RUN_TEST(test_CurrentWorkingDir, 48);
+  RUN_TEST(test_DevLinks, 59);
+
+  return (UnityEnd());
+}