xref: /minix3/minix/tests/ds/dstest.c (revision 433d6423c39e34ec4b79c950597bb2d236f886be)
1*433d6423SLionel Sambuc #include "inc.h"
2*433d6423SLionel Sambuc 
3*433d6423SLionel Sambuc char *key_u32 = "test_u32";
4*433d6423SLionel Sambuc char *key_str = "test_str";
5*433d6423SLionel Sambuc char *key_mem = "test_mem";
6*433d6423SLionel Sambuc char *key_label = "test_label";
7*433d6423SLionel Sambuc 
8*433d6423SLionel Sambuc /*===========================================================================*
9*433d6423SLionel Sambuc  *				test_u32				     *
10*433d6423SLionel Sambuc  *===========================================================================*/
test_u32(void)11*433d6423SLionel Sambuc void test_u32(void)
12*433d6423SLionel Sambuc {
13*433d6423SLionel Sambuc 	int r;
14*433d6423SLionel Sambuc 	u32_t value;
15*433d6423SLionel Sambuc 
16*433d6423SLionel Sambuc 	/* Publish and retrieve. */
17*433d6423SLionel Sambuc 	r = ds_publish_u32(key_u32, 1234, 0);
18*433d6423SLionel Sambuc 	assert(r == OK);
19*433d6423SLionel Sambuc 	r = ds_retrieve_u32(key_u32, &value);
20*433d6423SLionel Sambuc 	assert(r == OK && value == 1234);
21*433d6423SLionel Sambuc 
22*433d6423SLionel Sambuc 	/* If dstest deletes 'test_u32' immediately after publishing it,
23*433d6423SLionel Sambuc 	 * subs will catch the event, but it can't check it immediately.
24*433d6423SLionel Sambuc 	 * So dstest will sleep 2 seconds to wait for subs to complete. */
25*433d6423SLionel Sambuc 	sleep(2);
26*433d6423SLionel Sambuc 
27*433d6423SLionel Sambuc 	/* Publish again without DSF_OVERWRITE. */
28*433d6423SLionel Sambuc 	r = ds_publish_u32(key_u32, 4321, 0);
29*433d6423SLionel Sambuc 	assert(r == EEXIST);
30*433d6423SLionel Sambuc 
31*433d6423SLionel Sambuc 	/* Publish again with DSF_OVERWRITE to overwrite it. */
32*433d6423SLionel Sambuc 	r = ds_publish_u32(key_u32, 4321, DSF_OVERWRITE);
33*433d6423SLionel Sambuc 	assert(r == OK);
34*433d6423SLionel Sambuc 	r = ds_retrieve_u32(key_u32, &value);
35*433d6423SLionel Sambuc 	assert(r == OK && value == 4321);
36*433d6423SLionel Sambuc 
37*433d6423SLionel Sambuc 	/* Delete. */
38*433d6423SLionel Sambuc 	r = ds_delete_u32(key_u32);
39*433d6423SLionel Sambuc 	assert(r == OK);
40*433d6423SLionel Sambuc 	r = ds_retrieve_u32(key_u32, &value);
41*433d6423SLionel Sambuc 	assert(r == ESRCH);
42*433d6423SLionel Sambuc 
43*433d6423SLionel Sambuc 	printf("DSTEST: U32 test successful!\n");
44*433d6423SLionel Sambuc }
45*433d6423SLionel Sambuc 
46*433d6423SLionel Sambuc /*===========================================================================*
47*433d6423SLionel Sambuc  *				test_str				     *
48*433d6423SLionel Sambuc  *===========================================================================*/
test_str(void)49*433d6423SLionel Sambuc void test_str(void)
50*433d6423SLionel Sambuc {
51*433d6423SLionel Sambuc 	int r;
52*433d6423SLionel Sambuc 	char *string = "little";
53*433d6423SLionel Sambuc 	char *longstring = "verylooooooongstring";
54*433d6423SLionel Sambuc 	char buf[17];
55*433d6423SLionel Sambuc 
56*433d6423SLionel Sambuc 	r = ds_publish_str(key_str, string, 0);
57*433d6423SLionel Sambuc 	assert(r == OK);
58*433d6423SLionel Sambuc 
59*433d6423SLionel Sambuc 	r = ds_retrieve_str(key_str, buf, sizeof(buf)-1);
60*433d6423SLionel Sambuc 	assert(r == OK && strcmp(string, buf) == 0);
61*433d6423SLionel Sambuc 
62*433d6423SLionel Sambuc 	r = ds_delete_str(key_str);
63*433d6423SLionel Sambuc 	assert(r == OK);
64*433d6423SLionel Sambuc 
65*433d6423SLionel Sambuc 	/* Publish a long string. */
66*433d6423SLionel Sambuc 	r = ds_publish_str(key_str, longstring, 0);
67*433d6423SLionel Sambuc 	assert(r == OK);
68*433d6423SLionel Sambuc 
69*433d6423SLionel Sambuc 	r = ds_retrieve_str(key_str, buf, sizeof(buf)-1);
70*433d6423SLionel Sambuc 	assert(r == OK && strcmp(string, buf) != 0
71*433d6423SLionel Sambuc 		&& strncmp(longstring, buf, sizeof(buf)-1) == 0);
72*433d6423SLionel Sambuc 
73*433d6423SLionel Sambuc 	r = ds_delete_str(key_str);
74*433d6423SLionel Sambuc 	assert(r == OK);
75*433d6423SLionel Sambuc 
76*433d6423SLionel Sambuc 	printf("DSTEST: STRING test successful!\n");
77*433d6423SLionel Sambuc }
78*433d6423SLionel Sambuc 
79*433d6423SLionel Sambuc /*===========================================================================*
80*433d6423SLionel Sambuc  *				test_mem				     *
81*433d6423SLionel Sambuc  *===========================================================================*/
test_mem(void)82*433d6423SLionel Sambuc void test_mem(void)
83*433d6423SLionel Sambuc {
84*433d6423SLionel Sambuc 	char *string1 = "ok, this is a string";
85*433d6423SLionel Sambuc 	char *string2 = "ok, this is a very looooong string";
86*433d6423SLionel Sambuc 	size_t len1 = strlen(string1) + 1;
87*433d6423SLionel Sambuc 	size_t len2 = strlen(string2) + 1;
88*433d6423SLionel Sambuc 	char buf[100];
89*433d6423SLionel Sambuc 	size_t get_len;
90*433d6423SLionel Sambuc 	int r;
91*433d6423SLionel Sambuc 
92*433d6423SLionel Sambuc 	/* Publish and retrieve. */
93*433d6423SLionel Sambuc 	r = ds_publish_mem(key_mem, string1, len1, 0);
94*433d6423SLionel Sambuc 	assert(r == OK);
95*433d6423SLionel Sambuc 	get_len = 100;
96*433d6423SLionel Sambuc 	r = ds_retrieve_mem(key_mem, buf, &get_len);
97*433d6423SLionel Sambuc 	assert(r == OK && strcmp(string1, buf) == 0);
98*433d6423SLionel Sambuc 	assert(get_len == len1);
99*433d6423SLionel Sambuc 
100*433d6423SLionel Sambuc 	/* Let get_len=8, which is less than strlen(string1). */
101*433d6423SLionel Sambuc 	get_len = 8;
102*433d6423SLionel Sambuc 	r = ds_retrieve_mem(key_mem, buf, &get_len);
103*433d6423SLionel Sambuc 	assert(r == OK && get_len == 8);
104*433d6423SLionel Sambuc 
105*433d6423SLionel Sambuc 	/* Publish again to overwrite with a bigger memory range. */
106*433d6423SLionel Sambuc 	r = ds_publish_mem(key_mem, string2, len2, DSF_OVERWRITE);
107*433d6423SLionel Sambuc 	assert(r == OK);
108*433d6423SLionel Sambuc 
109*433d6423SLionel Sambuc 	get_len = 100;
110*433d6423SLionel Sambuc 	r = ds_retrieve_mem(key_mem, buf, &get_len);
111*433d6423SLionel Sambuc 	assert(r == OK && strcmp(string2, buf) == 0);
112*433d6423SLionel Sambuc 	assert(get_len == len2);
113*433d6423SLionel Sambuc 
114*433d6423SLionel Sambuc 	r = ds_delete_mem(key_mem);
115*433d6423SLionel Sambuc 	assert(r == OK);
116*433d6423SLionel Sambuc 
117*433d6423SLionel Sambuc 	printf("DSTEST: MEM test successful!\n");
118*433d6423SLionel Sambuc }
119*433d6423SLionel Sambuc 
120*433d6423SLionel Sambuc /*===========================================================================*
121*433d6423SLionel Sambuc  *				test_label				     *
122*433d6423SLionel Sambuc  *===========================================================================*/
test_label(void)123*433d6423SLionel Sambuc void test_label(void)
124*433d6423SLionel Sambuc {
125*433d6423SLionel Sambuc 	int r;
126*433d6423SLionel Sambuc 	char label[DS_MAX_KEYLEN];
127*433d6423SLionel Sambuc 	endpoint_t endpoint;
128*433d6423SLionel Sambuc 
129*433d6423SLionel Sambuc 	/* Retrieve own label and endpoint. */
130*433d6423SLionel Sambuc 	r = ds_retrieve_label_name(label, sef_self());
131*433d6423SLionel Sambuc 	assert(r == OK);
132*433d6423SLionel Sambuc 	r = ds_retrieve_label_endpt(label, &endpoint);
133*433d6423SLionel Sambuc 	assert(r == OK && endpoint == sef_self());
134*433d6423SLionel Sambuc 
135*433d6423SLionel Sambuc 	/* Publish and delete. */
136*433d6423SLionel Sambuc 	r = ds_publish_label(label, endpoint, 0);
137*433d6423SLionel Sambuc 	assert(r == EPERM);
138*433d6423SLionel Sambuc 	r = ds_delete_label(label);
139*433d6423SLionel Sambuc 	assert(r == EPERM);
140*433d6423SLionel Sambuc 
141*433d6423SLionel Sambuc 	printf("DSTEST: LABEL test successful!\n");
142*433d6423SLionel Sambuc }
143*433d6423SLionel Sambuc 
144*433d6423SLionel Sambuc /*===========================================================================*
145*433d6423SLionel Sambuc  *			       sef_cb_init_fresh			     *
146*433d6423SLionel Sambuc  *===========================================================================*/
sef_cb_init_fresh(int UNUSED (type),sef_init_info_t * UNUSED (info))147*433d6423SLionel Sambuc static int sef_cb_init_fresh(int UNUSED(type), sef_init_info_t *UNUSED(info))
148*433d6423SLionel Sambuc {
149*433d6423SLionel Sambuc 	/* Run all the tests. */
150*433d6423SLionel Sambuc 	test_u32();
151*433d6423SLionel Sambuc 	test_str();
152*433d6423SLionel Sambuc 	test_mem();
153*433d6423SLionel Sambuc 	test_label();
154*433d6423SLionel Sambuc 
155*433d6423SLionel Sambuc 	return OK;
156*433d6423SLionel Sambuc }
157*433d6423SLionel Sambuc 
158*433d6423SLionel Sambuc /*===========================================================================*
159*433d6423SLionel Sambuc  *			       sef_local_startup			     *
160*433d6423SLionel Sambuc  *===========================================================================*/
sef_local_startup(void)161*433d6423SLionel Sambuc static void sef_local_startup(void)
162*433d6423SLionel Sambuc {
163*433d6423SLionel Sambuc 	/* Let SEF perform startup. */
164*433d6423SLionel Sambuc 	sef_setcb_init_fresh(sef_cb_init_fresh);
165*433d6423SLionel Sambuc 
166*433d6423SLionel Sambuc 	sef_startup();
167*433d6423SLionel Sambuc }
168*433d6423SLionel Sambuc 
169*433d6423SLionel Sambuc /*===========================================================================*
170*433d6423SLionel Sambuc  *				main					     *
171*433d6423SLionel Sambuc  *===========================================================================*/
main(void)172*433d6423SLionel Sambuc int main(void)
173*433d6423SLionel Sambuc {
174*433d6423SLionel Sambuc 	/* SEF local startup. */
175*433d6423SLionel Sambuc 	sef_local_startup();
176*433d6423SLionel Sambuc 
177*433d6423SLionel Sambuc 	return 0;
178*433d6423SLionel Sambuc }
179