]> git.meshlink.io Git - meshlink/blobdiff - test/encrypted.c
Refactor the non-blackbox test suite.
[meshlink] / test / encrypted.c
index e51debd65e210d6b70b4165d5a0dd1e374736812..17b2fd80a7fc81f1a11ffdd30672758f97b2929a 100644 (file)
@@ -3,58 +3,31 @@
 #include <unistd.h>
 #include <errno.h>
 #include <sys/time.h>
+#include <assert.h>
 
 #include "meshlink.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);
-}
+#include "utils.h"
 
 int main() {
        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 +35,6 @@ int main() {
 
        // Destroy the mesh.
 
-       if(!meshlink_destroy("encrypted_conf")) {
-               fprintf(stderr, "Could not destroy configuration\n");
-               return 1;
-       }
-
-       if(!access("encrypted_conf", F_OK) || errno != ENOENT) {
-               fprintf(stderr, "Configuration not fully destroyed\n");
-               return 1;
-       }
-
-       return 0;
+       assert(meshlink_destroy("encrypted_conf"));
+       assert(access("encrypted_conf", F_OK) == -1 && errno == ENOENT);
 }