HIPC  0.5
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
test_msgd.c
Go to the documentation of this file.
1 #include <assert.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 
5 #include <hipc/msgd.h>
6 
7 static void test_hipcMsgdHdrPtr(void)
8 {
9  hipc_msgd md;
10 
11  assert(md.header == hipcMsgdHdrPtr(&md));
12 
13  return;
14 }
15 
17 {
18  hipc_msgd md;
19  unsigned char body[64];
20 
21  md.header[HIPC_MSGHDR_BYTE_BDYSZ] = sizeof(body);
22  md.body_p = body;
23 
25  assert(sizeof(body) == hipcMsgdBdySize(&md));
26  assert(body == hipcMsgdBdyPtr(&md));
27 
29  md.body_p = NULL;
30  assert(0 == hipcMsgdBdySize(&md));
31  assert(NULL == hipcMsgdBdyPtr(&md));
32 
33  return;
34 }
35 
36 static void test_hipcMsgdType(void)
37 {
38  hipc_msgd md;
39 
41  assert(11 == hipcMsgdType(&md));
42 
43  return;
44 }
45 
46 static void test_hipcMsgdStruno(void)
47 {
48  hipc_msgd md;
49 
51  assert(127 == hipcMsgdStruno(&md));
52 
53  return;
54 }
55 
56 static void test_hipcMsgdOffset(void)
57 {
58  hipc_msgd md;
59 
61  assert(7 == hipcMsgdOffset(&md));
62 
63  return;
64 }
65 
66 static void test_hipcMsgdRngsize(void)
67 {
68  hipc_msgd md;
69 
71  assert(5 == hipcMsgdRngsize(&md));
72 
73  return;
74 }
75 
76 struct local_mock {
77  long m1;
78  int m2;
79 };
80 
81 static void test_hipcMsgdSetBdyForPut(void)
82 {
83  hipc_msgd md;
84  struct local_mock lm;
85 
87 
89  assert(HIPC_SUCCESS == hipcMsgdSetBdyForPut(&md, &lm, sizeof(lm)));
90  assert(&lm == md.body_p);
91 
93  assert(HIPC_SUCCESS == hipcMsgdSetBdyForPut(&md, &lm, sizeof(lm)));
94  assert((unsigned char *) (&lm) + 1 == md.body_p);
95 
96  md.header[HIPC_MSGHDR_BYTE_OFFSET] = offsetof(struct local_mock, m2);
97  assert(HIPC_SUCCESS == hipcMsgdSetBdyForPut(&md, &lm, sizeof(lm)));
98  assert(&(lm.m2) == md.body_p);
99 
100 
101  md.header[HIPC_MSGHDR_BYTE_BDYSZ] = sizeof(lm);
103  assert(HIPC_SUCCESS == hipcMsgdSetBdyForPut(&md, &lm, sizeof(lm)));
104  assert(&lm == md.body_p);
105 
106  md.header[HIPC_MSGHDR_BYTE_BDYSZ] = sizeof(lm);
108  assert(HIPC_ERR_S_INV_RW_RANGE ==
109  hipcMsgdSetBdyForPut(&md, &lm, sizeof(lm)));
110 
111  md.header[HIPC_MSGHDR_BYTE_BDYSZ] = 255;
113  assert(HIPC_SUCCESS == hipcMsgdSetBdyForPut(&md, &lm, 510));
114 
115  md.header[HIPC_MSGHDR_BYTE_BDYSZ] = 255;
117  assert(HIPC_ERR_S_INV_RW_RANGE == hipcMsgdSetBdyForPut(&md, &lm, 509));
118 
119  return;
120 }
121 
122 static void test_hipcGenmdSuccessToGet(void)
123 {
124  struct local_mock lm;
125  hipc_msgd mdS;
126 
127  {
128  hipc_msgd mdR = { {HIPC_MESSAGE_TYPE_GET, 0, 0, 0}, NULL };
129  assert(HIPC_SUCCESS ==
130  hipcGenmdSuccessToGet(&mdS, &mdR, &lm, sizeof(lm)));
131  assert(HIPC_MESSAGE_TYPE_SUCCESS ==
132  mdS.header[HIPC_MSGHDR_BYTE_TYPE]);
133  assert(0 == mdS.header[HIPC_MSGHDR_BYTE_STRUNO]);
134  assert(0 == mdS.header[HIPC_MSGHDR_BYTE_OFFSET]);
135  assert(0 == mdS.header[HIPC_MSGHDR_BYTE_BDYSZ]);
136  assert(&lm == mdS.body_p);
137  }
138  {
139  hipc_msgd mdR = { {HIPC_MESSAGE_TYPE_GET, 1, 2, 3}, NULL };
140  assert(HIPC_SUCCESS ==
141  hipcGenmdSuccessToGet(&mdS, &mdR, &lm, sizeof(lm)));
142  assert(HIPC_MESSAGE_TYPE_SUCCESS ==
143  mdS.header[HIPC_MSGHDR_BYTE_TYPE]);
144  assert(1 == mdS.header[HIPC_MSGHDR_BYTE_STRUNO]);
145  assert(2 == mdS.header[HIPC_MSGHDR_BYTE_OFFSET]);
146  assert(3 == mdS.header[HIPC_MSGHDR_BYTE_BDYSZ]);
147  assert((unsigned char *) (&lm) + 2 == mdS.body_p);
148  }
149  {
151  5,
152  offsetof(struct local_mock, m2),
153  sizeof(lm.m2)}, NULL };
154  assert(HIPC_SUCCESS ==
155  hipcGenmdSuccessToGet(&mdS, &mdR, &lm, sizeof(lm)));
156  assert(HIPC_MESSAGE_TYPE_SUCCESS ==
157  mdS.header[HIPC_MSGHDR_BYTE_TYPE]);
158  assert(5 == mdS.header[HIPC_MSGHDR_BYTE_STRUNO]);
159  assert(offsetof(struct local_mock, m2) ==
160  mdS.header[HIPC_MSGHDR_BYTE_OFFSET]);
161  assert(sizeof(lm.m2) == mdS.header[HIPC_MSGHDR_BYTE_BDYSZ]);
162  assert(&(lm.m2) == mdS.body_p);
163  }
164  {
165  hipc_msgd mdR = { {HIPC_MESSAGE_TYPE_GET, 0, 0, sizeof(lm)}, NULL };
166  assert(HIPC_SUCCESS ==
167  hipcGenmdSuccessToGet(&mdS, &mdR, &lm, sizeof(lm)));
168  assert(HIPC_MESSAGE_TYPE_SUCCESS ==
169  mdS.header[HIPC_MSGHDR_BYTE_TYPE]);
170  assert(0 == mdS.header[HIPC_MSGHDR_BYTE_STRUNO]);
171  assert(0 == mdS.header[HIPC_MSGHDR_BYTE_OFFSET]);
172  assert(sizeof(lm) == mdS.header[HIPC_MSGHDR_BYTE_BDYSZ]);
173  assert(&lm == mdS.body_p);
174  }
175  {
176  hipc_msgd mdR = { {HIPC_MESSAGE_TYPE_GET, 0, 1, sizeof(lm)}, NULL };
177  assert(HIPC_ERR_S_INV_RW_RANGE ==
178  hipcGenmdSuccessToGet(&mdS, &mdR, &lm, sizeof(lm)));
179  }
180  {
181  hipc_msgd mdR = { {HIPC_MESSAGE_TYPE_GET, 0, 255, 255}, NULL };
182  assert(HIPC_SUCCESS ==
183  hipcGenmdSuccessToGet(&mdS, &mdR, &lm, 510));
184  }
185  {
186  hipc_msgd mdR = { {HIPC_MESSAGE_TYPE_GET, 0, 255, 255}, NULL };
187  assert(HIPC_ERR_S_INV_RW_RANGE ==
188  hipcGenmdSuccessToGet(&mdS, &mdR, &lm, 509));
189  }
190 
191  return;
192 }
193 
194 static void test_hipcGenmdUnstru(void)
195 {
196  hipc_msgd md;
197  char str[256];
198  char abc[] = "abc";
199 
204  assert(0 == md.header[HIPC_MSGHDR_BYTE_BDYSZ]);
205  assert(NULL == md.body_p);
206 
211  assert(255 == md.header[HIPC_MSGHDR_BYTE_BDYSZ]);
212  assert(str == md.body_p);
213 
218  assert(4 == md.header[HIPC_MSGHDR_BYTE_BDYSZ]);
219  assert(abc == md.body_p);
220 
221  hipcGenmdSuccess(&md);
225  assert(0 == md.header[HIPC_MSGHDR_BYTE_BDYSZ]);
226  assert(NULL == md.body_p);
227 
228  return;
229 }
230 
231 int main(int argc, char *argv[])
232 {
233  if (2 != argc) {
234  fprintf(stderr, "Usage: %s test_id\n", argv[0]);
235  exit(EXIT_FAILURE);
236  }
237 
238  switch (argv[1][0]) {
239  case 'a':
241  break;
242  case 'b':
244  break;
245  case 'c':
247  break;
248  case 'd':
250  break;
251  case 'e':
253  break;
254  case 'f':
256  break;
257  case 'g':
259  break;
260  case 'h':
262  break;
263  case 'i':
265  break;
266  default:
267  fprintf(stderr, "Invalid test_id: %s \n", argv[1]);
268  exit(EXIT_FAILURE);
269  }
270 
271  return 0;
272 }
unsigned char header[HIPC_MSGHDR_SIZE]
Definition: msgd.h:9
static void test_hipcMsgdType(void)
Definition: test_msgd.c:36
static void test_hipcMsgdStruno(void)
Definition: test_msgd.c:46
int main(int argc, char *argv[])
Definition: test_msgd.c:231
enum HIPC_errno hipcGenmdSuccessToGet(hipc_msgd *const mds_p, hipc_msgd const *const mdr_p, void *const stru_p, const size_t stru_size)
Initializes mds_p as a response to mdr_p whose type must be GET.
Definition: msgd.c:102
static void test_hipcGenmdUnstru(void)
Definition: test_msgd.c:194
static void test_hipcMsgdBdySize_hipcMsgdBdyPtr(void)
Definition: test_msgd.c:16
enum HIPC_errno hipcGenmdUnstru(hipc_msgd *const md_p, const unsigned char type, const enum HIPC_errno hipc_errno, char *const errdtlstr, size_t errdtlstr_size)
Initializes a HIPC message data as a unstructured type message.
Definition: msgd.c:68
long m1
Definition: test_msgd.c:77
static void test_hipcMsgdHdrPtr(void)
Definition: test_msgd.c:7
unsigned char hipcMsgdStruno(hipc_msgd const *const md_p)
Returns structure number of a HIPC message data.
Definition: msgd.c:31
enum HIPC_errno hipcMsgdSetBdyForPut(hipc_msgd *const mdr_p, void *const stru_p, const size_t stru_size)
Sets a body to a HIPC message data that contains a header of a PUT message.
Definition: msgd.c:140
static void test_hipcMsgdSetBdyForPut(void)
Definition: test_msgd.c:81
static void test_hipcGenmdSuccessToGet(void)
Definition: test_msgd.c:122
unsigned char hipcMsgdBdySize(hipc_msgd const *const md_p)
Returns body size of a HIPC message data.
Definition: msgd.c:9
#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
static void test_hipcMsgdOffset(void)
Definition: test_msgd.c:56
unsigned char hipcMsgdOffset(hipc_msgd const *const md_p)
Returns offset of a HIPC message data.
Definition: msgd.c:42
void * body_p
Definition: msgd.h:10
#define hipcMsgdBdyPtr(md_p)
Returns a pointer to a body of a HIPC message data.
Definition: msgd.h:52
unsigned char hipcMsgdType(hipc_msgd const *const md_p)
Returns type of a HIPC message data.
Definition: msgd.c:20
unsigned char hipcMsgdRngsize(hipc_msgd const *const md_p)
Returns range size of a HIPC message data.
Definition: msgd.c:53
#define hipcMsgdHdrPtr(md_p)
Returns a pointer to a header of a HIPC message data.
Definition: msgd.h:44
#define HIPC_UNSTRU
Definition: hipc.h:44
static void test_hipcMsgdRngsize(void)
Definition: test_msgd.c:66