387becd8cf7043a3ae46c872b1bf940e551095ff
[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_object(char* uid);
10 static void view_event(n_object* o, n_object* v);
11 static void cast_event(n_object* o, n_object* c);
12 static void sync_object(n_object* o);
13
14 static void start_tests(void);
15 static void view_event_1(n_object* o, n_object* v);
16 static void view_event_2(n_object* o, n_object* v);
17 static void view_event_3(n_object* o, n_object* v);
18 static void cast_event_1(n_object* o, n_object* c);
19 static void cast_event_2(n_object* o, n_object* c);
20 static void cast_event_3(n_object* o, n_object* c);
21
22 static void test_state(n_object* o, char* os, char* uid, char* cont);
23
24 /* -------------------------------------------------------------------------- */
25
26 EXPORT int teston_module_loaded(void)
27 {
28     n_register_driver("teston", handles_object, view_event, cast_event, sync_object);
29
30     k_log_out("Test ON Driver initialised");
31
32     start_tests();
33
34     return 1;
35 }
36
37 EXPORT int teston_module_event(void* data)
38 {
39     n_dispatch((n_event*)data);
40     return 1;
41 }
42
43 EXPORT int teston_module_tick(void)
44 {
45     return 1;
46 }
47
48 /* -------------------------------------------------------------------------- */
49
50 int handles_object(char* uid)
51 {
52     return 1;
53 }
54
55 void sync_object(n_object* o)
56 {
57 }
58
59 /* -------------------------------------------------------------------------- */
60
61 void view_event(n_object* o, n_object* v)
62 {
63     if(n_uid_is(o, "11111-4141a")) view_event_1(o,v);
64     if(n_uid_is(o, "22222-ef990")) view_event_2(o,v);
65     if(n_uid_is(o, "33333-18bbc")) view_event_3(o,v);
66     k_assert(0, "No match to UID in view_event");
67 }
68
69 void cast_event(n_object* o, n_object* c)
70 {
71     if(n_uid_is(o, "11111-4141a")) cast_event_1(o,c);
72     if(n_uid_is(o, "22222-ef990")) cast_event_2(o,c);
73     if(n_uid_is(o, "33333-18bbc")) cast_event_3(o,c);
74     k_assert(0, "No match to UID in cast_event");
75 }
76
77 /* -------------------------------------------------------------------------- */
78
79 /*
80  - object.create .update .commit .rollback (auto inc version #?)
81  - object.view - may return empty so wait for ..
82  - viewstate(object) - state asked for /or/ object is subscribing
83  */
84 void start_tests(void)
85 {
86     k_log_out("Creating o11111 ------------------");
87     char* o11111s = "UID: 11111-4141a\n"
88                     "\n"
89                     "Cost: £111\n";
90     n_object* o11111 = n_object_new(o11111s);
91     test_state(o11111, o11111s, "11111-4141a", "£111");
92     k_log_out("Committing o11111");
93     n_commit(o11111);
94
95     k_log_out("Creating o22222 ------------------");
96     char* o22222s = "UID: 22222-ef990\n"
97                     "Permit: - UID: 11111-4141a\n"
98                     "          quiet\n"
99                     "\n"
100                     "Cost: £222\n";
101     n_object* o22222 = n_object_new(o22222s);
102     test_state(o22222, o22222s, "22222-ef990", "£222");
103
104     k_log_out("o11111 views o22222 ------------------");
105     n_object* o2 = n_view(o11111, "22222-ef990");  /* creates Cast:/shell o2 and sends View event to o2 */
106     k_assert(!o2, "Object 2 has not been committed yet, but Object 1 can view it:\n%s\n", n_to_string(o2));
107     n_commit(o11111); /* ?? commit View (i.e. header change set) */
108
109     n_commit(o22222); /* merges with shell and sends Cast event to o1 */
110
111     k_log_out("o11111 views o22222 ------------------");
112     o2 = n_view(o11111, "22222-ef990"); /* quiet, so no View event to o2, satisfiable now so no Cast event to o1 */
113     k_assert(o2!=0, "Object 2 has been committed, but can't be viewed by Object 1");
114     test_state(o2, o22222s, "22222-ef990", "£222");
115
116     k_log_out("o22222 views o33333 ------------------");
117     n_object* o3 = n_view(o22222, "33333-18bbc"); /* creats Cast:/shell o3 and sends View event to o3 */
118     k_assert(!o3, "Object 3 has not been created yet, but Object 2 can view it:\n%s", n_to_string(o3));
119     n_commit(o22222); /* ?? commit View (i.e. header change set) */
120 }
121
122 /* -------------------------------------------------------------------------- */
123
124 void view_event_1(n_object* o, n_object* v)
125 {
126 }
127
128 void cast_event_1(n_object* o, n_object* c)
129 {
130     k_log_out("o11111 is cast o22222 ------------------");
131     test_state(c, 0, "22222-ef990", "£222");
132     k_assert(!strcmp(n_header(o, "View:1:UID"),   "22222-ef990"), "o1 <-Cast- o2: o1 first View UID not o2" );
133     k_assert(!strcmp(n_header(c, "Cast:1:UID"),   "11111-4141a"), "o1 <-Cast- o2: o2 first Cast UID not o1" );
134     k_assert(!strcmp(n_header(c, "Permit:1:UID"), "11111-4141a"), "o1 <-Cast- o2: o2 first Permit UID not o1" );
135 }
136
137 /* -------------------------------------------------------------------------- */
138
139 void view_event_2(n_object* o, n_object* v)
140 {
141     k_log_out("o22222 gets view from o11111 ------------------");
142     test_state(v, 0, "11111-4141a", "£111");
143     k_assert(!strcmp(n_header(v, "View:1:UID"),   "22222-ef990"), "o1 -View-> o2: o1 first View UID not o2" );
144     k_assert(!strcmp(n_header(o, "Cast:1:UID"),   "11111-4141a"), "o1 -View-> o2: o2 first Cast UID not o1" );
145     k_assert(!strcmp(n_header(o, "Permit:1:UID"), "11111-4141a"), "o1 -View-> o2: o2 first Permit UID not o1" );
146 }
147
148 void cast_event_2(n_object* o, n_object* c)
149 {
150     k_log_out("o22222 is cast o33333 ------------------");
151     test_state(c, 0, "33333-18bbc", "£333");
152     k_assert(!strcmp(n_header(o, "View:1:UID"),   "33333-18bbc"), "o2 <-Cast- o3: o2 first View UID not o3" );
153     k_assert(!strcmp(n_header(c, "Cast:1:UID"),   "22222-ef990"), "o2 <-Cast- o3: o3 first Cast UID not o2" );
154     k_assert(!strcmp(n_header(c, "Permit:1:UID"), "22222-ef990"), "o2 <-Cast- o3: o3 first Permit UID not o2" );
155 }
156
157 /* -------------------------------------------------------------------------- */
158
159 void view_event_3(n_object* o, n_object* v)
160 {
161     k_log_out("o33333 gets view from o22222 ------------------");
162     test_state(v, 0, "22222-ef990", "£222");
163     k_assert(!strcmp(n_header(v, "View:1:UID"),   "33333-18bbc"), "o2 -View-> o3: o2 first View UID not o3" );
164     k_assert(!strcmp(n_header(o, "Cast:1:UID"),   "22222-ef990"), "o2 -View-> o3: o3 first Cast UID not o2" );
165     k_assert(       !n_header(o, "Permit:1:UID"),                 "o2 -View-> o3: o3 has a Permit" );
166
167     k_log_out("Creating o33333 ------------------");
168     char* o33333s = "UID: 33333-18bbc\n"
169                     "Permit: - UID: 22222-ef990\n"
170                     "          quiet\n"
171                     "\n"
172                     "Cost: £333\n";
173     n_object* o33333 = n_object_new(o33333s);
174     test_state(o33333, o33333s, "33333-18bbc", "£333");
175     k_assert(!strcmp(n_header(o33333, "Permit:1:UID"), "22222-ef990"), "o3 first Permit not o2" );
176     k_assert(!strcmp(n_header(o33333, "Permit:1:quiet"), ""),          "o3 first Permit not quiet" );
177
178     k_log_out("Committing o33333");
179     n_commit(o33333);
180 }
181
182 void cast_event_3(n_object* o, n_object* c)
183 {
184 }
185
186 /* -------------------------------------------------------------------------- */
187
188 void test_state(n_object* o, char* os, char* uid, char* cont)
189 {
190     char* c;
191
192     k_log_out("Checking %s", uid);
193
194     if(os){
195     c=n_to_string(o);
196     k_assert(c && !strcmp(c, os), "To-string was\n%s", c? c: "null");
197     }
198
199     c=n_uid(o);
200     k_assert(c && !strcmp(c, uid), "UID was %s in n_uid",  c? c: "null");
201
202     c=n_header(o, "UID");
203     k_assert(c && !strcmp(c, uid), "UID was %s in n_header",  c? c: "null");
204
205     c=k_hashtable_get(n_headers(o), "UID");
206     k_assert(c && !strcmp(c, uid), "UID was %s in hash get",  c? c: "null");
207
208     c=k_hashtable_get(n_content(o), "Cost");
209     k_assert(c && !strcmp(c, cont), "Content was %s", c? c: "null");
210
211     if(os){
212     c=n_to_string(o);
213     k_assert(c && !strcmp(c, os), "To-string was\n%s", c? c: "null");
214     }
215 }
216
217 /* -------------------------------------------------------------------------- */
218
219
220