HIPC  0.5
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
test_usocutils.c
Go to the documentation of this file.
1 #include <assert.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 
5 #include <mock.h>
6 
7 #include <hipc/usoc.h>
8 #include <hipc/usocutils.h>
9 
10 #define S HIPC_SUCCESS
11 #define R 0
12 #define W 1
13 
14 struct HIPC_usoc {
15  struct MockIO mio;
16 };
17 
18 enum HIPC_errno
19 hipcUsocRead(struct HIPC_usoc *so_p, void *buf, const size_t size)
20 {
21  return mockIORead(&so_p->mio, buf, size);
22 }
23 
24 enum HIPC_errno
25 hipcUsocWrite(struct HIPC_usoc *so_p, const void *buf, const size_t size)
26 {
27  return mockIOWrite(&so_p->mio, buf, size);
28 }
29 
30 static void test_hipcUsocWriteMsgd(void)
31 {
32  struct HIPC_usoc usoc;
33  hipc_msgd md;
34 
35  {
36  struct MockIOOp ops[] = {
37  {W, S, 4, {HIPC_MESSAGE_TYPE_QUIT,HIPC_UNSTRU,11,3}},
38  {W, S, 3, {0x68,0x69,0x00}},
39  };
40  mockIOInit(&usoc.mio, ops, sizeof(ops) / sizeof(ops[0]));
41  hipcGenmdQuit(&md, 11, "hi");
42  assert(HIPC_SUCCESS == hipcUsocWriteMsgd(&usoc, &md));
43  assert(1 == mockIOCheckCompletion(&usoc.mio));
44  }
45  {
46  struct MockIOOp ops[] = {
48  {W, S, 0},
49  };
50  mockIOInit(&usoc.mio, ops, sizeof(ops) / sizeof(ops[0]));
51  hipcGenmdSuccess(&md);
52  assert(HIPC_SUCCESS == hipcUsocWriteMsgd(&usoc, &md));
53  assert(1 == mockIOCheckCompletion(&usoc.mio));
54  }
55  {
56  struct MockIOOp ops[] = {
57  {W, S, 4, {HIPC_MESSAGE_TYPE_QUIT,HIPC_UNSTRU,11,3}},
58  {W, HIPC_ERR_IO_WRITE, 3, {0x68,0x69,0x00}},
59  };
60  mockIOInit(&usoc.mio, ops, sizeof(ops) / sizeof(ops[0]));
61  hipcGenmdQuit(&md, 11, "hi");
62  assert(HIPC_ERR_IO_WRITE == hipcUsocWriteMsgd(&usoc, &md));
63  assert(1 == mockIOCheckCompletion(&usoc.mio));
64  }
65  {
66  struct MockIOOp ops[] = {
68  };
69  mockIOInit(&usoc.mio, ops, sizeof(ops) / sizeof(ops[0]));
70  hipcGenmdQuit(&md, 11, "hi");
71  assert(HIPC_ERR_IO_WRITE == hipcUsocWriteMsgd(&usoc, &md));
72  assert(1 == mockIOCheckCompletion(&usoc.mio));
73  }
74 
75  return;
76 }
77 
78 static void test_hipcUsocAccept(void)
79 {
80  struct HIPC_usoc usoc;
81  struct MOCK1S1 mstru;
82  {
83  static unsigned char buf[HIPC_MSGHDR_SIZE +
85  const struct HIPC_scfg *cfg_accepted;
86  const struct HIPC_scfg *cfg_ary[] = { &HIPCUD_cfgmock1_SCFG };
87  struct MockIOOp ops[] = {
89  {R, S, 9, {0x43,0x49,0x44,0x5f,0x4d,0x4f,0x43,0x4b,0x31}},
90  {W, S, 5, {HIPC_MESSAGE_TYPE_SYS,108,0,1,
91  sizeof(mstru)}},
92  {W, S, 6, {HIPC_MESSAGE_TYPE_SYS,0,1,2,
93  offsetof(struct MOCK1S1,m1),offsetof(struct MOCK1S1,m2)}},
94  {W, S, 6, {HIPC_MESSAGE_TYPE_SYS,0,2,2,
95  sizeof(mstru.m1),sizeof(mstru.m2)}},
96  };
97  mockIOInit(&usoc.mio, ops, sizeof(ops) / sizeof(ops[0]));
98  assert(HIPC_SUCCESS==hipcUsocAccept(&usoc, &cfg_accepted, cfg_ary, 1, buf, sizeof(buf)));
99  assert(&HIPCUD_cfgmock1_SCFG == cfg_accepted);
100  assert(1 == mockIOCheckCompletion(&usoc.mio));
101  }
102  {
103  static unsigned char buf[16];
104  const struct HIPC_scfg *cfg_accepted;
105  const struct HIPC_scfg *cfg_ary[] =
107  struct MockIOOp ops[] = {
108  {R, S, 4, {HIPC_MESSAGE_TYPE_HELLO,HIPC_UNSTRU,0,9}},
109  {R, S, 9, {0x43,0x49,0x44,0x5f,0x4d,0x4f,0x43,0x4b,0x31}},
110  {W, S, 5, {HIPC_MESSAGE_TYPE_SYS,108,0,1,
111  sizeof(mstru)}},
112  {W, S, 6, {HIPC_MESSAGE_TYPE_SYS,0,1,2,
113  offsetof(struct MOCK1S1,m1),offsetof(struct MOCK1S1,m2)}},
114  {W, S, 6, {HIPC_MESSAGE_TYPE_SYS,0,2,2,
115  sizeof(mstru.m1),sizeof(mstru.m2)}},
116  };
117  mockIOInit(&usoc.mio, ops, sizeof(ops) / sizeof(ops[0]));
118  assert(HIPC_SUCCESS==hipcUsocAccept(&usoc, &cfg_accepted, cfg_ary, 2, buf, sizeof(buf)));
119  assert(&HIPCUD_cfgmock1_SCFG == cfg_accepted);
120  assert(1 == mockIOCheckCompletion(&usoc.mio));
121  }
122  {
123  static unsigned char buf[16];
124  const struct HIPC_scfg *cfg_accepted;
125  assert(HIPC_ERR_S_INV_NULL==hipcUsocAccept(&usoc, &cfg_accepted, NULL, 0, buf, sizeof(buf)));
126  assert(NULL == cfg_accepted);
127  }
128  {
129  static unsigned char buf[1];
130  const struct HIPC_scfg *cfg_accepted;
131  const struct HIPC_scfg *cfg_ary[] = { &HIPCUD_cfgmock1_SCFG };
132  assert(HIPC_ERR_S_BUF_SHTG==hipcUsocAccept(&usoc, &cfg_accepted, cfg_ary, 1, buf, sizeof(buf)));
133  assert(NULL == cfg_accepted);
134  }
135  {
136  static unsigned char buf[16];
137  const struct HIPC_scfg *cfg_accepted;
138  const struct HIPC_scfg *cfg_ary[] = { &HIPCUD_cfgmock1_SCFG };
139  struct MockIOOp ops[] = {
141  };
142  mockIOInit(&usoc.mio, ops, sizeof(ops) / sizeof(ops[0]));
143  assert(HIPC_ERR_IO_READ==hipcUsocAccept(&usoc, &cfg_accepted, cfg_ary, 1, buf, sizeof(buf)));
144  assert(NULL == cfg_accepted);
145  assert(1 == mockIOCheckCompletion(&usoc.mio));
146  }
147  {
148  static unsigned char buf[4];
149  const struct HIPC_scfg *cfg_accepted;
150  const struct HIPC_scfg *cfg_ary[] = { &HIPCUD_cfgmock1_SCFG };
151  struct MockIOOp ops[] = {
152  {R, S, 4, {HIPC_MESSAGE_TYPE_HELLO, HIPC_UNSTRU, 0, 9}},
153  };
154  mockIOInit(&usoc.mio, ops, sizeof(ops) / sizeof(ops[0]));
155  assert(HIPC_ERR_S_BUF_SHTG==hipcUsocAccept(&usoc, &cfg_accepted, cfg_ary, 1, buf, sizeof(buf)));
156  assert(NULL == cfg_accepted);
157  assert(1 == mockIOCheckCompletion(&usoc.mio));
158  }
159  {
160  static unsigned char buf[255];
161  const struct HIPC_scfg *cfg_accepted;
162  const struct HIPC_scfg *cfg_ary[] = { &HIPCUD_cfgmock1_SCFG };
163  struct MockIOOp ops[] = {
164  {R, S, 4, {HIPC_MESSAGE_TYPE_HELLO, HIPC_UNSTRU, 0, 254}},
165  };
166  mockIOInit(&usoc.mio, ops, sizeof(ops) / sizeof(ops[0]));
167  assert(HIPC_ERR_S_BUF_SHTG==hipcUsocAccept(&usoc, &cfg_accepted, cfg_ary, 1, buf, sizeof(buf)));
168  assert(NULL == cfg_accepted);
169  assert(1 == mockIOCheckCompletion(&usoc.mio));
170  }
171  {
172  static unsigned char buf[16];
173  const struct HIPC_scfg *cfg_accepted;
174  const struct HIPC_scfg *cfg_ary[] = { &HIPCUD_cfgmock1_SCFG };
175  struct MockIOOp ops[] = {
176  {R, S, 4, {HIPC_MESSAGE_TYPE_HELLO, HIPC_UNSTRU, 0, 9}},
177  {R, HIPC_ERR_IO_READ, 9, {0x43,0x49,0x44,0x5f,0x4d,0x4f,0x43,0x4b,0x31}},
178  };
179  mockIOInit(&usoc.mio, ops, sizeof(ops) / sizeof(ops[0]));
180  assert(HIPC_ERR_IO_READ==hipcUsocAccept(&usoc, &cfg_accepted, cfg_ary, 1, buf, sizeof(buf)));
181  assert(NULL == cfg_accepted);
182  assert(1 == mockIOCheckCompletion(&usoc.mio));
183  }
184  {
185  static unsigned char buf[16];
186  const struct HIPC_scfg *cfg_accepted;
187  const struct HIPC_scfg *cfg_ary[] = { &HIPCUD_cfgmock1_SCFG };
188  struct MockIOOp ops[] = {
189  {R, S, 4, {HIPC_MESSAGE_TYPE_HELLO, HIPC_UNSTRU, 0, 1}},
190  {R, S, 1, {0x43}},
191  };
192  mockIOInit(&usoc.mio, ops, sizeof(ops) / sizeof(ops[0]));
193  assert( HIPC_ERR_S_INV_CFG==hipcUsocAccept(&usoc, &cfg_accepted, cfg_ary, 1, buf, sizeof(buf)));
194  assert(NULL == cfg_accepted);
195  assert(1 == mockIOCheckCompletion(&usoc.mio));
196  }
197  {
198  static unsigned char buf[16];
199  const struct HIPC_scfg *cfg_accepted;
200  const struct HIPC_scfg *cfg_ary[] =
201  { NULL, &HIPCUD_cfgmock1_SCFG };
202  struct MockIOOp ops[] = {
203  {R, S, 4, {HIPC_MESSAGE_TYPE_HELLO,HIPC_UNSTRU,0,9}},
204  {R, S, 9, {0x43,0x49,0x44,0x5f,0x4d,0x4f,0x43,0x4b,0x31}},
205  };
206  mockIOInit(&usoc.mio, ops, sizeof(ops) / sizeof(ops[0]));
207  assert(HIPC_ERR_S_INV_NULL==hipcUsocAccept(&usoc, &cfg_accepted, cfg_ary, 2, buf, sizeof(buf)));
208  assert(NULL == cfg_accepted);
209  assert(1 == mockIOCheckCompletion(&usoc.mio));
210  }
211  {
212  static unsigned char buf[16];
213  const struct HIPC_scfg *cfg_accepted;
214  const struct HIPC_scfg *cfg_ary[] = { &HIPCUD_cfgmock1_SCFG };
215  struct MockIOOp ops[] = {
216  {R, S, 4, {HIPC_MESSAGE_TYPE_HELLO,HIPC_UNSTRU,0,9}},
217  {R, S, 9, {0x43,0x49,0x44,0x5f,0x4d,0x4f,0x43,0x4b,0x31}},
219  sizeof(mstru)}},
220  };
221  mockIOInit(&usoc.mio, ops, sizeof(ops)/sizeof(ops[0]));
222  assert(HIPC_ERR_IO_WRITE==hipcUsocAccept(&usoc, &cfg_accepted, cfg_ary, 1, buf, sizeof(buf)));
223  assert(&HIPCUD_cfgmock1_SCFG == cfg_accepted);
224  assert(1 == mockIOCheckCompletion(&usoc.mio));
225  }
226 
227  return;
228 }
229 
230 static void test_hipcUsocAcceptSimple(void)
231 {
232  struct HIPC_usoc usoc;
233  struct MOCK1S1 mstru;
234  {
235  struct MockIOOp ops[] = {
236  {R, S, 1, {HIPC_MESSAGE_TYPE_HELLO}},
237  {R, S, 1, {HIPC_UNSTRU}},
238  {R, S, 1, {0}},
239  {R, S, 1, {9}},
240  {R, S, 1, {0x43}},
241  {R, S, 1, {0x49}},
242  {R, S, 1, {0x44}},
243  {R, S, 1, {0x5f}},
244  {R, S, 1, {0x4d}},
245  {R, S, 1, {0x4f}},
246  {R, S, 1, {0x43}},
247  {R, S, 1, {0x4b}},
248  {R, S, 1, {0x31}},
249  {W, S, 5, {HIPC_MESSAGE_TYPE_SYS,108,0,1,
250  sizeof(mstru)}},
251  {W, S, 6, {HIPC_MESSAGE_TYPE_SYS,0,1,2,
252  offsetof(struct MOCK1S1,m1),offsetof(struct MOCK1S1,m2)}},
253  {W, S, 6, {HIPC_MESSAGE_TYPE_SYS,0,2,2,
254  sizeof(mstru.m1),sizeof(mstru.m2)}},
255  };
256  mockIOInit(&usoc.mio, ops, sizeof(ops) / sizeof(ops[0]));
257  assert(HIPC_SUCCESS ==
259  assert(1 == mockIOCheckCompletion(&usoc.mio));
260  }
261  {
262  mockIOInit(&usoc.mio, NULL, 0);
263  assert(HIPC_ERR_S_INV_NULL == hipcUsocAcceptSimple(&usoc, NULL));
264  assert(1 == mockIOCheckCompletion(&usoc.mio));
265  }
266  {
267  struct MockIOOp ops[] = {
269  };
270  mockIOInit(&usoc.mio, ops, sizeof(ops) / sizeof(ops[0]));
271  assert(HIPC_ERR_IO_READ ==
273  assert(1 == mockIOCheckCompletion(&usoc.mio));
274  }
275  {
276  struct MockIOOp ops[] = {
277  {R, S, 1, {HIPC_MESSAGE_TYPE_HELLO}},
278  {R, S, 1, {HIPC_UNSTRU}},
279  {R, S, 1, {0}},
280  {R, S, 1, {8}},
281  };
282  mockIOInit(&usoc.mio, ops, sizeof(ops) / sizeof(ops[0]));
283  assert(HIPC_ERR_S_INV_CFG ==
285  assert(1 == mockIOCheckCompletion(&usoc.mio));
286  }
287  {
288  struct MockIOOp ops[] = {
289  {R, S, 1, {HIPC_MESSAGE_TYPE_HELLO}},
290  {R, S, 1, {HIPC_UNSTRU}},
291  {R, S, 1, {0}},
292  {R, S, 1, {9}},
293  {R, HIPC_ERR_IO_READ, 1, {0x43}},
294  };
295  mockIOInit(&usoc.mio, ops, sizeof(ops) / sizeof(ops[0]));
296  assert(HIPC_ERR_IO_READ ==
298  assert(1 == mockIOCheckCompletion(&usoc.mio));
299  }
300  {
301  struct MockIOOp ops[] = {
302  {R, S, 1, {HIPC_MESSAGE_TYPE_HELLO}},
303  {R, S, 1, {HIPC_UNSTRU}},
304  {R, S, 1, {0}},
305  {R, S, 1, {9}},
306  {R, S, 1, {0x43}},
307  {R, S, 1, {0x00}},
308  };
309  mockIOInit(&usoc.mio, ops, sizeof(ops) / sizeof(ops[0]));
310  assert(HIPC_ERR_S_INV_CFG ==
312  assert(1 == mockIOCheckCompletion(&usoc.mio));
313  }
314  {
315  struct MockIOOp ops[] = {
316  {R, S, 1, {HIPC_MESSAGE_TYPE_HELLO}},
317  {R, S, 1, {HIPC_UNSTRU}},
318  {R, S, 1, {0}},
319  {R, S, 1, {9}},
320  {R, S, 1, {0x43}},
321  {R, S, 1, {0x49}},
322  {R, S, 1, {0x44}},
323  {R, S, 1, {0x5f}},
324  {R, S, 1, {0x4d}},
325  {R, S, 1, {0x4f}},
326  {R, S, 1, {0x43}},
327  {R, S, 1, {0x4b}},
328  {R, S, 1, {0x31}},
330  sizeof(mstru)}},
331  };
332  mockIOInit(&usoc.mio, ops, sizeof(ops) / sizeof(ops[0]));
333  assert(HIPC_ERR_IO_WRITE ==
335  assert(1 == mockIOCheckCompletion(&usoc.mio));
336  }
337 
338  return;
339 }
340 
341 int main(int argc, char *argv[])
342 {
343  if (2 != argc) {
344  fprintf(stderr, "Usage: %s test_id\n", argv[0]);
345  exit(EXIT_FAILURE);
346  }
347 
348  switch (argv[1][0]) {
349  case 'a':
351  break;
352  case 'b':
354  break;
355  case 'c':
357  break;
358  default:
359  fprintf(stderr, "Invalid test_id: %s \n", argv[1]);
360  exit(EXIT_FAILURE);
361  }
362 
363  return 0;
364 }
enum HIPC_errno hipcUsocAcceptSimple(struct HIPC_usoc *const so_p, const struct HIPC_scfg *const cfg_p)
Accepts a HIPC session of cfg_p.
Definition: usocutils.c:141
#define S
struct MockIO mio
const struct HIPC_scfg HIPCUD_cfgmock1_SCFG
Definition: cfgmock1_scfg.c:41
char m1
Definition: cfgmock1.h:2
#define R
int m2
Definition: cfgmock1.h:3
#define HIPCUD_cfgmock1_CFGID_SIZE
Definition: cfgmock1_scfg.h:1
enum HIPC_errno hipcUsocWriteMsgd(struct HIPC_usoc *const so_p, hipc_msgd const *const md_p)
Writes md_p to so_p.
Definition: usocutils.c:14
HIPC_errno
Definition: hipc.h:4
int main(int argc, char *argv[])
int mockIOCheckCompletion(struct MockIO *mio_p)
Definition: mockio.c:19
#define hipcGenmdQuit(md_p, hipc_errno, errdtlstr)
Initializes a HIPC message data as a QUIT message.
Definition: msgd.h:26
#define hipcGenmdSuccess(md_p)
Initializes a HIPC message data as a SUCCESS message.
Definition: msgd.h:35
const struct HIPC_scfg HIPCUD_cfgmock2_SCFG
Definition: cfgmock2_scfg.c:55
enum HIPC_errno hipcUsocRead(struct HIPC_usoc *so_p, void *buf, const size_t size)
Reads exactly size bytes from so_p.
enum HIPC_errno hipcUsocWrite(struct HIPC_usoc *so_p, const void *buf, const size_t size)
Writes exactly size bytes to so_p.
void mockIOInit(struct MockIO *mio_p, const struct MockIOOp *ops, const unsigned int nops)
Definition: mockio.c:8
Definition: usoc.h:4
enum HIPC_errno mockIOWrite(struct MockIO *mio_p, const void *buf, const size_t size)
Definition: mockio.c:63
enum HIPC_errno hipcUsocAccept(struct HIPC_usoc *const so_p, const struct HIPC_scfg **const cfg_pp, const struct HIPC_scfg *const *const cfg_ary, const unsigned int n_cfg, unsigned char *const buf, size_t buf_size)
Accepts a HIPC session if cfg_ary includes a configuration of coming HELLO message.
Definition: usocutils.c:71
Definition: mockio.h:19
Definition: scfg.h:9
enum HIPC_errno mockIORead(struct MockIO *mio_p, void *buf, const size_t size)
Definition: mockio.c:30
static void test_hipcUsocWriteMsgd(void)
static void test_hipcUsocAccept(void)
#define W
#define HIPC_UNSTRU
Definition: hipc.h:44
static void test_hipcUsocAcceptSimple(void)