1 /*
2    srvid tests
3 
4    Copyright (C) Amitay Isaacs  2015
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "replace.h"
21 
22 #include <assert.h>
23 
24 #include "common/db_hash.c"
25 #include "common/srvid.c"
26 
27 #define TEST_SRVID	0xBE11223344556677
28 
test_handler(uint64_t srvid,TDB_DATA data,void * private_data)29 static void test_handler(uint64_t srvid, TDB_DATA data, void *private_data)
30 {
31 	int *count = (int *)private_data;
32 	(*count)++;
33 }
34 
main(void)35 int main(void)
36 {
37 	struct srvid_context *srv = NULL;
38 	TALLOC_CTX *mem_ctx = talloc_new(NULL);
39 	TALLOC_CTX *tmp_ctx = talloc_new(NULL);
40 	int ret;
41 	int count = 0;
42 
43 	ret = srvid_register(srv, tmp_ctx, TEST_SRVID, test_handler, &count);
44 	assert(ret == EINVAL);
45 
46 	ret = srvid_init(mem_ctx, &srv);
47 	assert(ret == 0);
48 
49 	ret = srvid_deregister(srv, TEST_SRVID, &count);
50 	assert(ret == ENOENT);
51 
52 	ret = srvid_register(srv, tmp_ctx, TEST_SRVID, test_handler, &count);
53 	assert(ret == 0);
54 
55 	ret = srvid_exists(srv, TEST_SRVID, NULL);
56 	assert(ret == 0);
57 
58 	ret = srvid_exists(srv, TEST_SRVID, &count);
59 	assert(ret == 0);
60 
61 	ret = srvid_dispatch(srv, TEST_SRVID, 0, tdb_null);
62 	assert(ret == 0);
63 	assert(count == 1);
64 
65 	ret = srvid_dispatch(srv, 0, TEST_SRVID, tdb_null);
66 	assert(ret == 0);
67 	assert(count == 2);
68 
69 	ret = srvid_deregister(srv, TEST_SRVID, NULL);
70 	assert(ret == ENOENT);
71 
72 	ret = srvid_deregister(srv, TEST_SRVID, &count);
73 	assert(ret == 0);
74 
75 	ret = srvid_register(srv, tmp_ctx, TEST_SRVID, test_handler, &count);
76 	assert(ret == 0);
77 
78 	talloc_free(tmp_ctx);
79 	ret = srvid_exists(srv, TEST_SRVID, NULL);
80 	assert(ret == ENOENT);
81 
82 	ret = srvid_dispatch(srv, TEST_SRVID, 0, tdb_null);
83 	assert(ret == ENOENT);
84 
85 	tmp_ctx = talloc_new(NULL);
86 	assert(tmp_ctx != NULL);
87 
88 	ret = srvid_register(srv, tmp_ctx, TEST_SRVID, test_handler, NULL);
89 	assert(ret == 0);
90 	ret = srvid_exists(srv, TEST_SRVID, &count);
91 	assert(ret == ENOENT);
92 
93 	ret = srvid_register(srv, tmp_ctx, TEST_SRVID, test_handler, &count);
94 	assert(ret == 0);
95 	ret = srvid_exists(srv, TEST_SRVID, &count);
96 	assert(ret == 0);
97 
98 	talloc_free(srv);
99 	assert(talloc_get_size(mem_ctx) == 0);
100 	assert(talloc_get_size(tmp_ctx) == 0);
101 
102 	talloc_free(mem_ctx);
103 
104 	return 0;
105 }
106