]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
add test/test-elf
authorGustavo Sverzut Barbieri <barbieri@profusion.mobi>
Mon, 19 Dec 2011 23:23:45 +0000 (21:23 -0200)
committerLucas De Marchi <lucas.demarchi@profusion.mobi>
Sat, 24 Dec 2011 03:44:31 +0000 (01:44 -0200)
will be focused on testing ELF operations and takes a filename to load
instead of looking for it in the system.

Makefile.am
test/.gitignore
test/test-elf.c [new file with mode: 0644]

index 9523d71b619b6cf0071ebafc1b81729e6844f4f6..56d2f29da04b3fb8f5f8c88b50b0e5d0bf4b0e76 100644 (file)
@@ -106,7 +106,7 @@ test_test_loaded_LDADD = libkmod/libkmod.la
 noinst_PROGRAMS = test/test-insmod test/test-rmmod test/test-rmmod2 \
                  test/test-lookup test/test-path-from-name \
                  test/test-get-dependencies test/test-mod-double-ref \
-                 test/test-blacklist \
+                 test/test-blacklist test/test-elf \
                  $(check_PROGRAMS)
 
 test_test_rmmod_LDADD = libkmod/libkmod.la
@@ -117,3 +117,6 @@ test_test_path_from_name_LDADD = libkmod/libkmod.la
 test_test_get_dependencies_LDADD = libkmod/libkmod.la
 test_test_mod_double_ref_LDADD = libkmod/libkmod.la
 test_test_blacklist_LDADD = libkmod/libkmod.la
+
+test_test_elf_SOURCES = test/test-elf.c
+test_test_elf_LDADD = libkmod/libkmod.la
index 0722c91ee155e1b61ed64567a69a7e3c52d0e526..3118b91862709bf6a3b9dfefbecaf2a2ba22bcbd 100644 (file)
@@ -9,3 +9,4 @@ test-path-from-name
 test-get-dependencies
 test-mod-double-ref
 test-blacklist
+test-elf
diff --git a/test/test-elf.c b/test/test-elf.c
new file mode 100644 (file)
index 0000000..773ca6b
--- /dev/null
@@ -0,0 +1,88 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stddef.h>
+#include <errno.h>
+#include <unistd.h>
+#include <inttypes.h>
+#include <string.h>
+#include <libkmod.h>
+#include <getopt.h>
+
+int main(int argc, char *argv[])
+{
+       struct kmod_ctx *ctx;
+       struct kmod_module *mod;
+       struct kmod_list *list, *l;
+       int err;
+
+       printf("libkmod version %s\n", VERSION);
+
+       if (argc != 2) {
+               fprintf(stderr, "Usage:\n\t%s <module-path.ko>\n", argv[0]);
+               return EXIT_FAILURE;
+       }
+
+       ctx = kmod_new(NULL, NULL);
+       if (ctx == NULL)
+               return EXIT_FAILURE;
+
+       err = kmod_module_new_from_path(ctx, argv[1], &mod);
+       if (err < 0) {
+               fprintf(stderr, "ERROR: could not load %s: %s\n",
+                       argv[1], strerror(-errno));
+               goto module_error;
+       }
+
+       list = NULL;
+       err = kmod_module_get_info(mod, &list);
+       if (err <= 0)
+               printf("no information! (%s)\n", strerror(-err));
+       else {
+               puts("info:");
+               kmod_list_foreach(l, list) {
+                       const char *key, *val;
+                       key = kmod_module_info_get_key(l);
+                       val = kmod_module_info_get_value(l);
+                       printf("\t%s: %s\n", key, val ? val : "");
+               }
+               kmod_module_info_free_list(list);
+       }
+
+       list = NULL;
+       err = kmod_module_get_versions(mod, &list);
+       if (err <= 0)
+               printf("no modversions! (%s)\n", strerror(-err));
+       else {
+               puts("modversions:");
+               kmod_list_foreach(l, list) {
+                       const char *symbol;
+                       uint64_t crc;
+                       symbol = kmod_module_version_get_symbol(l);
+                       crc = kmod_module_version_get_crc(l);
+                       printf("\t%s: %#"PRIx64"\n", symbol, crc);
+               }
+               kmod_module_versions_free_list(list);
+       }
+
+       list = NULL;
+       err = kmod_module_get_symbols(mod, &list);
+       if (err <= 0)
+               printf("no symbols! (%s)\n", strerror(-err));
+       else {
+               puts("symbols:");
+               kmod_list_foreach(l, list) {
+                       const char *symbol;
+                       uint64_t crc;
+                       symbol = kmod_module_symbol_get_symbol(l);
+                       crc = kmod_module_symbol_get_crc(l);
+                       printf("\t%s: %#"PRIx64"\n", symbol, crc);
+               }
+               kmod_module_symbols_free_list(list);
+       }
+
+       kmod_module_unref(mod);
+module_error:
+       kmod_unref(ctx);
+
+       return (err < 0) ? EXIT_FAILURE : EXIT_SUCCESS;
+}