0d1e12709e28b4a3ef17b5a13e761461c9d578b5
[moonshot-ui.git] / tests / basic.c
1 #include <glib.h>
2
3 #include "libmoonshot.h"
4
5 /* FIXME: Using XDG_HOME_DIR and a test runner, we could give
6  * moonshot-ui a set of test identities and assert that they
7  * are returned correctly
8  */
9
10 gpointer test_func (gpointer data)
11 {
12     MoonshotError **error = data;
13     gboolean        success;
14
15     char *nai,
16          *password,
17          *server_certificate_hash,
18          *ca_certificate,
19          *subject_name_constraint,
20          *subject_alt_name_constraint;
21
22     success = moonshot_get_default_identity (&nai,
23                                              &password,
24                                              &server_certificate_hash,
25                                              &ca_certificate,
26                                              &subject_name_constraint,
27                                              &subject_alt_name_constraint,
28                                              error);
29
30     g_print ("Got id: %s %s\n", nai, password);
31
32     return GINT_TO_POINTER (success);
33 }
34
35
36 void test_connect ()
37 {
38     MoonshotError *error = NULL;
39     gboolean       success;
40
41     success = GPOINTER_TO_INT (test_func (&error));
42
43     if (success)
44         return;
45
46     g_print ("FAIL: %s\n", error->message);
47     g_assert_not_reached ();
48 }
49
50 void test_multithread ()
51 {
52     const int N = 100;
53
54     GThread       *thread[N];
55     MoonshotError *error[N];
56     gboolean       success[N];
57
58     GError *g_error = NULL;
59     int i;
60
61     for (i=0; i<N; i++) {
62         error[i] = NULL;
63         thread[i] = g_thread_create (test_func,
64                                      &error[i],
65                                      TRUE,
66                                      &g_error);
67         g_assert_no_error (g_error);
68     }
69
70     for (i=0; i<N; i++)
71         success[i] = GPOINTER_TO_INT (g_thread_join (thread[i]));
72
73     for (i=0; i<N; i++) {
74         if (! success[i]) {
75             g_print ("FAIL[%i]: %s\n", i, error[i]->message);
76             g_assert_not_reached ();
77         }
78     }
79 }
80
81 /* More stuff to test:
82  *   - server not available (dbus fail)
83  *   - no identities available (moonshot fail)
84  *   - valgrind
85  *   - mt
86  */
87
88 int main (int argc, char *argv[])
89 {
90     g_type_init ();
91     g_test_init (&argc, &argv, NULL);
92
93     g_test_add_func ("/basic/connect", test_connect);
94     g_test_add_func ("/basic/multithread", test_multithread);
95
96     g_test_run ();
97 }