]> git.meshlink.io Git - meshlink/blobdiff - test/encrypted.c
Don't use assert() to check the results of pthread_*() calls.
[meshlink] / test / encrypted.c
index e51debd65e210d6b70b4165d5a0dd1e374736812..ac0dfa92023afdb77a47a49170444b76fc3eeb69 100644 (file)
@@ -1,60 +1,38 @@
+#ifdef NDEBUG
+#undef NDEBUG
+#endif
+
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
 #include <errno.h>
 #include <sys/time.h>
+#include <assert.h>
+#include <dirent.h>
 
 #include "meshlink.h"
+#include "utils.h"
 
-void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
-       static struct timeval tv0;
-       struct timeval tv;
-
-       if(tv0.tv_sec == 0) {
-               gettimeofday(&tv0, NULL);
-       }
-
-       gettimeofday(&tv, NULL);
-       fprintf(stderr, "%u.%.03u ", (unsigned int)(tv.tv_sec - tv0.tv_sec), (unsigned int)tv.tv_usec / 1000);
-
-       if(mesh) {
-               fprintf(stderr, "(%s) ", mesh->name);
-       }
-
-       fprintf(stderr, "[%d] %s\n", level, text);
-}
-
-int main() {
+int main(void) {
        meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
 
        // Open a new meshlink instance.
 
+       assert(meshlink_destroy("encrypted_conf"));
        meshlink_handle_t *mesh = meshlink_open_encrypted("encrypted_conf", "foo", "encrypted", DEV_CLASS_BACKBONE, "right", 5);
-
-       if(!mesh) {
-               fprintf(stderr, "Could not initialize configuration for foo\n");
-               return 1;
-       }
+       assert(mesh);
 
        // Close the mesh and open it again, now with a different key.
 
        meshlink_close(mesh);
 
        mesh = meshlink_open_encrypted("encrypted_conf", "foo", "encrypted", DEV_CLASS_BACKBONE, "wrong", 5);
-
-       if(mesh) {
-               fprintf(stderr, "Could open mesh with the wrong key\n");
-               return 1;
-       }
+       assert(!mesh);
 
        // Open it again, now with the right key.
 
        mesh = meshlink_open_encrypted("encrypted_conf", "foo", "encrypted", DEV_CLASS_BACKBONE, "right", 5);
-
-       if(!mesh) {
-               fprintf(stderr, "Could not open mesh with the right key\n");
-               return 1;
-       }
+       assert(mesh);
 
        // That's it.
 
@@ -62,15 +40,16 @@ int main() {
 
        // Destroy the mesh.
 
-       if(!meshlink_destroy("encrypted_conf")) {
-               fprintf(stderr, "Could not destroy configuration\n");
-               return 1;
-       }
+       assert(meshlink_destroy("encrypted_conf"));
+
+       DIR *dir = opendir("encrypted_conf");
+       assert(dir);
+       struct dirent *ent;
 
-       if(!access("encrypted_conf", F_OK) || errno != ENOENT) {
-               fprintf(stderr, "Configuration not fully destroyed\n");
-               return 1;
+       while((ent = readdir(dir))) {
+               fprintf(stderr, "%s\n", ent->d_name);
+               assert(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."));
        }
 
-       return 0;
+       closedir(dir);
 }