583f3b3e1261b54538bb29c866cd11dcffec50e8
[cilux] / src / drivers / ot / test / teston.c
1
2 /* -------------------------------------------------------------------------- */
3
4 #include <kernelapi.h>
5 #include <notification.h>
6
7 /* -------------------------------------------------------------------------- */
8
9 static int  handles_resource(char* name);
10 static void sync_resource(ni_resource* res);
11
12 static void start_tests(void);
13 static void running_tests(ni_event* evt);
14
15 static void test_state(n_object* o, char* os, char* uid, char* cont);
16
17 /* -------------------------------------------------------------------------- */
18
19 EXPORT int teston_module_loaded(void)
20 {
21     ni_register_driver("teston", handles_resource, sync_resource);
22
23     k_log_out("Test ON Driver initialised");
24
25     start_tests();
26
27     return 1;
28 }
29
30 EXPORT int teston_module_event(void* data)
31 {
32     ni_event* evt=data;
33
34     running_tests(evt);
35
36     ni_event_delete(evt);
37     return 1;
38 }
39
40 EXPORT int teston_module_tick(void)
41 {
42     return 1;
43 }
44
45 /* -------------------------------------------------------------------------- */
46
47 int handles_resource(char* name)
48 {
49     return 1;
50 }
51
52 void sync_resource(ni_resource* res)
53 {
54 }
55
56 /* -------------------------------------------------------------------------- */
57
58 /*
59  - object.create .update .commit .rollback (auto inc version #?)
60  - object.see - may return empty so wait for ..
61  - seestate(object) - state asked for /or/ object is subscribing
62  */
63 void start_tests(void)
64 {
65     k_log_out("Creating o11111");
66
67     char* o11111s = "UID: 11111-4141a\n"
68                     "\n"
69                     "This: is one content\n";
70
71     n_object* o11111 = n_object_new(o11111s);
72
73     test_state(o11111, o11111s, "11111-4141a", "is one content");
74
75     k_log_out("Committing o11111");
76
77     n_commit(o11111);
78
79     k_log_out("Creating o22222");
80
81     char* o22222s = "UID: 22222-ef990\n"
82                     "\n"
83                     "This: is two content\n";
84
85     n_object* o22222 = n_object_new(o22222s);
86
87     test_state(o22222, o22222s, "22222-ef990", "is two content");
88
89     n_object* o2 = n_see(o11111, "22222-ef990");
90
91     k_assert(!o2, "Object 2 has not been committed yet, but Object 1 can see it:\n%s\n", n_to_string(o2));
92
93     n_commit(o22222);
94
95     o2 = n_see(o11111, "22222-ef990");
96
97     k_assert(o2!=0, "Object 2 has been committed, but can't be seen by Object 1");
98
99     test_state(o2, o22222s, "22222-ef990", "is two content");
100
101     n_object* o3 = n_see(o22222, "33333-18bbc");
102
103     k_assert(!o3, "Object 3 has not been created yet, but Object 2 can see it:\n%s", n_to_string(o3));
104 }
105
106 void running_tests(ni_event* evt)
107 {
108     char* o33333s = 
109             "UID: 33333-18bbc\n"
110             "\n"
111             "This: is three content\n";
112
113     n_object* o33333 = n_object_new(o33333s);
114
115     n_commit(o33333);
116
117 }
118
119 /* -------------------------------------------------------------------------- */
120
121 void test_state(n_object* o, char* os, char* uid, char* cont)
122 {
123     char* c;
124
125     k_log_out("Checking %s", uid);
126
127     c=n_to_string(o);
128     k_assert(c && !strcmp(c, os), "To-string was\n%s", c? c: "null");
129
130     c=n_uid(o);
131     k_assert(c && !strcmp(c, uid), "UID was %s in n_uid",  c? c: "null");
132
133     c=n_header(o, "UID");
134     k_assert(c && !strcmp(c, uid), "UID was %s in n_header",  c? c: "null");
135
136     c=k_hashtable_get(n_headers(o), "UID");
137     k_assert(c && !strcmp(c, uid), "UID was %s in hash get",  c? c: "null");
138
139     c=k_hashtable_get(n_content(o), "This");
140     k_assert(c && !strcmp(c, cont), "Content was %s", c? c: "null");
141
142     c=n_to_string(o);
143     k_assert(c && !strcmp(c, os), "To-string was\n%s", c? c: "null");
144 }
145
146 /* -------------------------------------------------------------------------- */
147
148
149