client library: Use local exception handling; correct API
[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     if (success)
31         g_print ("Got id: %s %s\n", nai, password);
32
33     return GINT_TO_POINTER (success);
34 }
35
36
37 void test_connect ()
38 {
39     MoonshotError *error = NULL;
40     gboolean       success;
41
42     success = GPOINTER_TO_INT (test_func (&error));
43
44     if (success)
45         return;
46
47     g_print ("FAIL: %s\n", error->message);
48     g_assert_not_reached ();
49 }
50
51 void test_multithread ()
52 {
53     const int N = 100;
54
55     GThread       *thread[N];
56     MoonshotError *error[N];
57     gboolean       success[N];
58
59     GError *g_error = NULL;
60     int i;
61
62     for (i=0; i<N; i++) {
63         error[i] = NULL;
64         thread[i] = g_thread_create (test_func,
65                                      &error[i],
66                                      TRUE,
67                                      &g_error);
68         g_assert_no_error (g_error);
69     }
70
71     for (i=0; i<N; i++)
72         success[i] = GPOINTER_TO_INT (g_thread_join (thread[i]));
73
74     for (i=0; i<N; i++) {
75         if (! success[i]) {
76             g_print ("FAIL[%i]: %s\n", i, error[i]->message);
77             g_assert_not_reached ();
78         }
79     }
80 }
81
82 /* More stuff to test:
83  *   - server not available (dbus fail)
84  *   - no identities available (moonshot fail)
85  *   - valgrind
86  *   - mt
87  */
88
89 int main (int argc, char *argv[])
90 {
91     g_type_init ();
92     g_test_init (&argc, &argv, NULL);
93
94     g_test_add_func ("/basic/connect", test_connect);
95     //g_test_add_func ("/basic/multithread", test_multithread);
96
97     g_test_run ();
98 }