1 #include <check.h>
2 #include <stdint.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include "../../test.h"
6 #include "../../../src/alloc.h"
7 #include "../../../src/conf.h"
8 #include "../../../src/conffile.h"
9 #include "../../../src/fsops.h"
10 #include "../../../src/hexmap.h"
11 #include "../../../src/iobuf.h"
12 #include "../../../src/lock.h"
13 #include "../../../src/prepend.h"
14 #include "../../../src/server/protocol1/dpth.h"
15
16 static const char *basepath="utest_dpth";
17
assert_components(struct dpth * dpth,int prim,int seco,int tert)18 static void assert_components(struct dpth *dpth,
19 int prim, int seco, int tert)
20 {
21 fail_unless(dpth->comp[0]==prim);
22 fail_unless(dpth->comp[1]==seco);
23 fail_unless(dpth->comp[2]==tert);
24 }
25
setup(void)26 static struct dpth *setup(void)
27 {
28 struct dpth *dpth;
29 hexmap_init();
30 fail_unless(recursive_delete(basepath)==0);
31 fail_unless((dpth=dpth_alloc())!=NULL);
32 assert_components(dpth, 0, 0, 0);
33
34 return dpth;
35 }
36
tear_down(struct dpth ** dpth)37 static void tear_down(struct dpth **dpth)
38 {
39 dpth_free(dpth);
40 fail_unless(recursive_delete(basepath)==0);
41 alloc_check();
42 }
43
44 struct init_data
45 {
46 uint16_t prim;
47 uint16_t seco;
48 uint16_t tert;
49 uint16_t prim_expected;
50 uint16_t seco_expected;
51 uint16_t tert_expected;
52 int ret_expected;
53 };
54
55 static struct init_data in[] = {
56 { 0x0000, 0x0000, 0x0000,
57 0x0000, 0x0000, 0x0001, 0 },
58 { 0x0000, 0x0000, 0xAAAA,
59 0x0000, 0x0000, 0xAAAB, 0 },
60 { 0x0000, 0x0000, 0xFFFF,
61 0x0000, 0x0001, 0x0000, 0 },
62 { 0x0000, 0x3333, 0xFFFF,
63 0x0000, 0x3334, 0x0000, 0 },
64 { 0x0000, 0x7530, 0xFFFF,
65 0x0001, 0x0000, 0x0000, 0 },
66 { 0x3333, 0xFFFF, 0xFFFF,
67 0x3334, 0x0000, 0x0000, 0 },
68 { 0x7530, 0x7530, 0xFFFF,
69 0x0000, 0x0000, 0x0000, -1 }
70 };
71
START_TEST(test_incr)72 START_TEST(test_incr)
73 {
74 FOREACH(in)
75 {
76 struct dpth *dpth;
77 dpth=setup();
78 fail_unless(dpth_protocol1_init(dpth,
79 basepath, MAX_STORAGE_SUBDIRS)==0);
80 dpth->comp[0]=in[i].prim;
81 dpth->comp[1]=in[i].seco;
82 dpth->comp[2]=in[i].tert;
83 fail_unless(dpth_incr(dpth)==in[i].ret_expected);
84 if(!in[i].ret_expected)
85 assert_components(dpth,
86 in[i].prim_expected,
87 in[i].seco_expected,
88 in[i].tert_expected);
89 tear_down(&dpth);
90 }
91 }
92 END_TEST
93
START_TEST(test_init)94 START_TEST(test_init)
95 {
96 FOREACH(in)
97 {
98 struct fzp *fp=NULL;
99 char *path=NULL;
100 struct dpth *dpth;
101 char *savepath;
102 dpth=setup();
103 dpth->comp[0]=in[i].prim;
104 dpth->comp[1]=in[i].seco;
105 dpth->comp[2]=in[i].tert;
106 savepath=dpth_protocol1_mk(dpth, 0, CMD_ERROR);
107 path=prepend_s(basepath, savepath);
108 fail_unless(build_path_w(path)==0);
109 // Create a file.
110 fail_unless((fp=fzp_open(path, "wb"))!=NULL);
111 fzp_close(&fp);
112
113 // Now when calling dpth_init(), the components should be
114 // incremented appropriately.
115 dpth_free(&dpth);
116 fail_unless((dpth=dpth_alloc())!=NULL);
117 fail_unless(dpth_protocol1_init(dpth,
118 basepath, MAX_STORAGE_SUBDIRS)
119 ==in[i].ret_expected);
120 assert_components(dpth,
121 in[i].prim_expected,
122 in[i].seco_expected,
123 in[i].tert_expected);
124
125 free_w(&path);
126 tear_down(&dpth);
127 }
128 }
129 END_TEST
130
assert_mk(struct dpth * dpth,int compression,enum cmd cmd,const char * expected)131 static void assert_mk(struct dpth *dpth, int compression, enum cmd cmd,
132 const char *expected)
133 {
134 ck_assert_str_eq(dpth_protocol1_mk(dpth, compression, cmd), expected);
135 }
136
START_TEST(test_mk)137 START_TEST(test_mk)
138 {
139 struct dpth *dpth=setup();
140 dpth->comp[0]=9;
141 dpth->comp[1]=9;
142 dpth->comp[2]=9;
143 assert_mk(dpth, 0, CMD_FILE, "0009/0009/0009");
144 assert_mk(dpth, 5, CMD_FILE, "0009/0009/0009.gz");
145 assert_mk(dpth, 0, CMD_EFS_FILE, "0009/0009/0009");
146 assert_mk(dpth, 5, CMD_EFS_FILE, "0009/0009/0009");
147 tear_down(&dpth);
148 }
149 END_TEST
150
151 struct str_data
152 {
153 uint16_t prim;
154 uint16_t seco;
155 uint16_t tert;
156 char str[15];
157 uint16_t prim_expected;
158 uint16_t seco_expected;
159 uint16_t tert_expected;
160 int ret_expected;
161 };
162
163 static struct str_data str[] = {
164 { 0x0000,0x0000,0x0000, "t/some/path", 0x0000,0x0000,0x0000, 0 },
165 { 0x0000,0x0000,0x0000, "invalid", 0x0000,0x0000,0x0000, -1 },
166 { 0x0000,0x0000,0x0000, "0000/0G00/0000", 0x0000,0x0000,0x0000, -1 },
167 { 0x0000,0x0000,0x0000, "0000/0000/0010", 0x0000,0x0000,0x0010, 0 },
168 { 0x0000,0x0000,0x0000, "0000/0011/0010", 0x0000,0x0011,0x0010, 0 },
169 { 0x0000,0x0000,0x0000, "0012/0011/0010", 0x0012,0x0011,0x0010, 0 },
170 { 0x0000,0x0000,0xAAAA, "0000/0000/0010", 0x0000,0x0000,0xAAAA, 0 },
171 { 0x0000,0xAAAA,0x0000, "0000/0010/0000", 0x0000,0xAAAA,0x0000, 0 },
172 { 0x1111,0x0000,0x0000, "1110/1111/1111", 0x1111,0x0000,0x0000, 0 },
173 { 0x1111,0x2222,0x0000, "1110/3333/4444", 0x1111,0x2222,0x0000, 0 },
174 { 0x1111,0x2222,0x3333, "1110/3333/4444", 0x1111,0x2222,0x3333, 0 },
175 { 0x1111,0x2222,0x3333, "1112/1111/1111", 0x1112,0x1111,0x1111, 0 }
176 };
177
START_TEST(test_set_from_string)178 START_TEST(test_set_from_string)
179 {
180 FOREACH(str)
181 {
182 int ret;
183 struct dpth *dpth;
184 dpth=setup();
185 dpth->comp[0]=str[i].prim;
186 dpth->comp[1]=str[i].seco;
187 dpth->comp[2]=str[i].tert;
188 ret=dpth_protocol1_set_from_string(dpth, str[i].str);
189 fail_unless(ret==str[i].ret_expected);
190 assert_components(dpth,
191 str[i].prim_expected,
192 str[i].seco_expected,
193 str[i].tert_expected);
194 tear_down(&dpth);
195 }
196 }
197 END_TEST
198
suite_server_protocol1_dpth(void)199 Suite *suite_server_protocol1_dpth(void)
200 {
201 Suite *s;
202 TCase *tc_core;
203
204 s=suite_create("server_protocol1_dpth");
205
206 tc_core=tcase_create("Core");
207
208 tcase_add_test(tc_core, test_incr);
209 tcase_add_test(tc_core, test_init);
210 tcase_add_test(tc_core, test_mk);
211 tcase_add_test(tc_core, test_set_from_string);
212 suite_add_tcase(s, tc_core);
213
214 return s;
215 }
216