diff test/dynload_plain/dynload_plain.c @ 242:85b61e8facfe

dynload: - added new function dlGetLibraryPath to get path of already loaded lib - covered new function in dynload_plain test - cleanups/cosmetics for consistency
author Tassilo Philipp
date Thu, 04 May 2017 13:42:17 +0200
parents 808045066f12
children 0ba6189a51dd
line wrap: on
line diff
--- a/test/dynload_plain/dynload_plain.c	Tue May 02 03:49:56 2017 +0200
+++ b/test/dynload_plain/dynload_plain.c	Thu May 04 13:42:17 2017 +0200
@@ -27,6 +27,7 @@
 #include "../common/platformInit.h"
 
 #include <string.h>
+#include <sys/stat.h>
 #if defined(DC_WINDOWS)
 #  include <io.h>
 #  define F_OK 0
@@ -42,17 +43,21 @@
   DLLib* pLib;
   DLSyms* pSyms;
   const char* path = NULL;
-  const char* clibs[] = { // hacky/lazy list of some clib paths per platform
+  const char* clibs[] = { /* hacky/lazy list of some clib paths per platform */
     "/lib/libc.so",
     "/lib/libc.so.6",
     "/lib/libc.so.7",
+    "/lib/libc.so.39.3", /* hack: for OpenBSD used in dyncall test env */
     "/lib64/libc.so",
     "/lib64/libc.so.6",
     "/lib64/libc.so.7",
     "/lib32/libc.so",
     "/lib32/libc.so.6",
     "/lib32/libc.so.7",
-	"/usr/lib/system/libsystem_c.dylib",
+    "/usr/lib/libc.so",
+    "/usr/lib/libc.so.6",
+    "/usr/lib/libc.so.7",
+    "/usr/lib/system/libsystem_c.dylib",
     "/usr/lib/libc.dylib",
     "\\ReactOS\\system32\\msvcrt.dll",
     "C:\\ReactOS\\system32\\msvcrt.dll",
@@ -72,26 +77,47 @@
     printf("using clib to test at: %s\n", path);
     ++r;
 
-    // dl*Library tests
-    // --------
-    pLib = dlLoadLibrary(path); // check if we can load a lib
+    /* dl*Library tests */
+    /* ---------------- */
+    pLib = dlLoadLibrary(path); /* check if we can load a lib */
     if(pLib) {
+      char queriedPath[200]; /* enough for our test paths */
+      int bs;
+
       printf("pLib handle: %p\n", pLib);
       ++r;
 
-      p = dlFindSymbol(pLib, "printf"); // check if we can lookup a symbol
+      p = dlFindSymbol(pLib, "printf"); /* check if we can lookup a symbol */
       printf("printf at: %p\n", p);
       r += (p != NULL);
 
+      bs = dlGetLibraryPath(pLib, queriedPath, 200);
+      if(bs && bs <= 200) {
+	    struct stat st0, st1; /* to check if same file */
+        int b;
+        printf("path of lib looked up via handle: %s\n", queriedPath);
+        b = (stat(path, &st0) != -1) && (stat(queriedPath, &st1) != -1);
+        printf("lib (inode:%d) and looked up lib (inode:%d) are same: %d\n", b?st0.st_ino:-1, b?st1.st_ino:-1, b && (st0.st_ino == st1.st_ino));
+        r += b && (st0.st_ino == st1.st_ino); /* compare if same lib using inode */
+/*@@@ check of resolved path is absolute*/
+
+        /* check correct bufsize retval */
+        b = (bs == strlen(queriedPath) + 1);
+        printf("looked up path's needed buffer size (%d) computed correctly: %d\n", bs, b);
+        r += b;
+      }
+      else
+        printf("failed to query lib path using lib's handle\n");
+
       dlFreeLibrary(pLib);
     }
     else
       printf("unable to open library %s\n", path);
 
 
-    // dlSyms* tests (intentionally after freeing lib above, as they work standalone)
-    // --------
-    pSyms = dlSymsInit(path); // check if we can iterate over symbols - init
+    /* dlSyms* tests (intentionally after freeing lib above, as they work standalone) */
+    /* ------------- */
+    pSyms = dlSymsInit(path); /* check if we can iterate over symbols - init */
     if(pSyms) {
       int n;
       const char* name;
@@ -99,13 +125,13 @@
       printf("pSyms handle: %p\n", pSyms);
       ++r;
 
-      n = dlSymsCount(pSyms); // check if there are some syms to iterate over
+      n = dlSymsCount(pSyms); /* check if there are some syms to iterate over */
       printf("num of libc symbols: %d\n", n);
       r += (n > 0);
 
       for(i=0; i<n; ++i) {
         name = dlSymsName(pSyms, i);
-        if(name && strcmp(name, "printf") == 0) { // check if we find "printf" also in iterated symbols
+        if(name && strcmp(name, "printf") == 0) { /* check if we find "printf" also in iterated symbols */
           ++r;
           break;
         }
@@ -113,11 +139,11 @@
       printf("printf symbol found by iteration: %d\n", i<n);
 
       name = (i<n) ? dlSymsName(pSyms, i) : NULL;
-      r += (name && strcmp(name, "printf") == 0); // check if we can lookup "printf" by index
+      r += (name && strcmp(name, "printf") == 0); /* check if we can lookup "printf" by index */
       printf("printf symbol name by index: %s\n", name?name:"");
 
-      pLib = dlLoadLibrary(path); // check if we can resolve ptr -> name,
-      if(pLib) {                  // need to lookup by name again, first
+      pLib = dlLoadLibrary(path); /* check if we can resolve ptr -> name, */
+      if(pLib) {                  /* need to lookup by name again, first */
         p = dlFindSymbol(pLib, "printf");
         name = dlSymsNameFromValue(pSyms, p);
         printf("printf symbol name by its own address (%p): %s\n", p, name?name:"");
@@ -131,8 +157,8 @@
       printf("dlSymsInit failed\n");
   }
 
-  // All worked if we got a score of 6 right ones
-  r = (r == 8);
+  /* Check final score of right ones to see if all worked */
+  r = (r == 10);
   printf("result: dynload_plain: %d\n", r);
   return !r;
 }