Motr  M0
serialize.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2012-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
23 #ifndef __KERNEL__
24 #include <limits.h> /* CHAR_MAX */
25 #else
26 #include <linux/kernel.h> /* INT_MIN */
27 #endif
28 
29 #include "lib/types.h" /* UINT32_MAX */
30 
31 #include "lib/misc.h" /* M0_SET0 */
32 #include "ut/ut.h" /* M0_UT_ASSERT */
33 #include "lib/vec.h" /* m0_bufvec */
34 
35 #include "net/test/serialize.h"
36 
37 enum {
39 };
40 
41 /* define limits for kernel mode */
42 /* max: should I add lib/limits.h? */
43 #ifdef __KERNEL__
44 #define CHAR_MIN (-(CHAR_MAX) - 1)
45 #define CHAR_MAX 127
46 #define UCHAR_MAX (~0)
47 #ifndef SHRT_MIN
48 #define SHRT_MIN SHORT_MIN
49 #endif
50 #ifndef SHRT_MAX
51 #define SHRT_MAX SHORT_MAX
52 #endif
53 #ifndef USHRT_MAX
54 #define USHRT_MAX USHORT_MAX
55 #endif
56 #endif
57 
58 struct simple_struct {
59  char ss_c;
60  unsigned char ss_uc;
61  short ss_s;
62  unsigned short ss_us;
63  int ss_i;
64  unsigned int ss_ui;
65  long ss_l;
66  unsigned long ss_ul;
67  long long ss_ll;
68  unsigned long long ss_ull;
69  int8_t ss_i8;
70  uint8_t ss_u8;
71  int16_t ss_i16;
72  uint16_t ss_u16;
73  int32_t ss_i32;
74  uint32_t ss_u32;
75  int64_t ss_i64;
76  uint64_t ss_u64;
77 };
78 
79 /* simple_struct_descr */
81  FIELD_DESCR(struct simple_struct, ss_c),
82  FIELD_DESCR(struct simple_struct, ss_uc),
83  FIELD_DESCR(struct simple_struct, ss_s),
84  FIELD_DESCR(struct simple_struct, ss_us),
85  FIELD_DESCR(struct simple_struct, ss_i),
86  FIELD_DESCR(struct simple_struct, ss_ui),
87  FIELD_DESCR(struct simple_struct, ss_l),
88  FIELD_DESCR(struct simple_struct, ss_ul),
89  FIELD_DESCR(struct simple_struct, ss_ll),
90  FIELD_DESCR(struct simple_struct, ss_ull),
91  FIELD_DESCR(struct simple_struct, ss_i8),
92  FIELD_DESCR(struct simple_struct, ss_u8),
93  FIELD_DESCR(struct simple_struct, ss_i16),
94  FIELD_DESCR(struct simple_struct, ss_u16),
95  FIELD_DESCR(struct simple_struct, ss_i32),
96  FIELD_DESCR(struct simple_struct, ss_u32),
97  FIELD_DESCR(struct simple_struct, ss_i64),
98  FIELD_DESCR(struct simple_struct, ss_u64),
99 };
100 
102  struct simple_struct *ss,
103  struct m0_bufvec *bv,
104  m0_bcount_t bv_offset)
105 {
107  bv, bv_offset);
108 }
109 
110 static void simple_struct_test(char c,
111  unsigned char uc,
112  short s,
113  unsigned short us,
114  int i,
115  unsigned int ui,
116  long l,
117  unsigned long ul,
118  long long ll,
119  unsigned long long ull,
120  int8_t i8,
121  uint8_t u8,
122  int16_t i16,
123  uint16_t u16,
124  int32_t i32,
125  uint32_t u32,
126  int64_t i64,
127  uint64_t u64)
128 {
129  m0_bcount_t ss_serialized_len;
130  m0_bcount_t rc_bcount;
131  char buf[SERIALIZE_BUF_LEN];
132  void *addr = buf;
134  struct m0_bufvec bv = M0_BUFVEC_INIT_BUF(&addr, &len);
135  struct simple_struct ss = {
136  .ss_c = c,
137  .ss_uc = uc,
138  .ss_s = s,
139  .ss_us = us,
140  .ss_i = i,
141  .ss_ui = ui,
142  .ss_l = l,
143  .ss_ul = ul,
144  .ss_ll = ll,
145  .ss_ull = ull,
146  .ss_i8 = i8,
147  .ss_u8 = u8,
148  .ss_i16 = i16,
149  .ss_u16 = u16,
150  .ss_i32 = i32,
151  .ss_u32 = u32,
152  .ss_i64 = i64,
153  .ss_u64 = u64,
154  };
155 
156  /* length of structure test */
157  ss_serialized_len = simple_struct_serialize(M0_NET_TEST_SERIALIZE, &ss,
158  NULL, 0);
159  M0_UT_ASSERT(ss_serialized_len > 0);
160 
161  /* simple encode-decode test */
162  M0_SET_ARR0(buf);
163  rc_bcount = simple_struct_serialize(M0_NET_TEST_SERIALIZE, &ss, &bv, 0);
164  M0_UT_ASSERT(rc_bcount == ss_serialized_len);
165 
166  M0_SET0(&ss);
167 
169  &bv, 0);
170  M0_UT_ASSERT(rc_bcount == ss_serialized_len);
171  M0_UT_ASSERT(ss.ss_c == c);
172  M0_UT_ASSERT(ss.ss_uc == uc);
173  M0_UT_ASSERT(ss.ss_s == s);
174  M0_UT_ASSERT(ss.ss_us == us);
175  M0_UT_ASSERT(ss.ss_i == i);
176  M0_UT_ASSERT(ss.ss_ui == ui);
177  M0_UT_ASSERT(ss.ss_l == l);
178  M0_UT_ASSERT(ss.ss_ul == ul);
179  M0_UT_ASSERT(ss.ss_ll == ll);
180  M0_UT_ASSERT(ss.ss_ull == ull);
181  M0_UT_ASSERT(ss.ss_i8 == i8);
182  M0_UT_ASSERT(ss.ss_u8 == u8);
183  M0_UT_ASSERT(ss.ss_i16 == i16);
184  M0_UT_ASSERT(ss.ss_u16 == u16);
185  M0_UT_ASSERT(ss.ss_i32 == i32);
186  M0_UT_ASSERT(ss.ss_u32 == u32);
187  M0_UT_ASSERT(ss.ss_i64 == i64);
188  M0_UT_ASSERT(ss.ss_u64 == u64);
189 
190  /* failure test */
191  len = 64; /* this will change the length of the first vector of 'bv' */
192  rc_bcount = simple_struct_serialize(M0_NET_TEST_SERIALIZE, &ss, &bv, 0);
193  M0_UT_ASSERT(rc_bcount == 0);
194  M0_UT_ASSERT(bv.ov_vec.v_count == &len);
195 }
196 
198 {
199  /* zero values test */
200  simple_struct_test(0, 0, 0, 0, 0, 0, 0, 0,
201  0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
202  /* one values test */
203  simple_struct_test(1, 1, 1, 1, 1, 1, 1, 1,
204  1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
205  /* -1 and 1 values test */
206  simple_struct_test(-1, 1, -1, 1, -1, 1, -1, 1,
207  -1, 1, -1, 1, -1, 1, -1, 1, -1, 1);
208  /* 42 value test */
209  simple_struct_test(42, 42, 42, 42, 42, 42, 42, 42,
210  42, 42, 42, 42, 42, 42, 42, 42, 42, 42);
211  /* min values test */
212  simple_struct_test(CHAR_MIN, 0, SHRT_MIN, 0,
213  INT_MIN, 0, LONG_MIN, 0,
214  LLONG_MIN, 0, INT8_MIN, 0,
215  INT16_MIN, 0, INT32_MIN, 0, INT64_MIN, 0);
216  /* max values test */
217  simple_struct_test(CHAR_MAX, UCHAR_MAX, SHRT_MAX, USHRT_MAX,
218  INT_MAX, UINT_MAX, LONG_MAX, ULONG_MAX,
219  LLONG_MAX, ULLONG_MAX, INT8_MAX, UINT8_MAX,
222 }
223 
224 /*
225  * Local variables:
226  * c-indentation-style: "K&R"
227  * c-basic-offset: 8
228  * tab-width: 8
229  * fill-column: 79
230  * scroll-step: 1
231  * End:
232  */
long long ss_ll
Definition: serialize.c:67
#define M0_BUFVEC_INIT_BUF(addr_ptr, count_ptr)
Definition: vec.h:165
#define NULL
Definition: misc.h:38
m0_bcount_t m0_net_test_serialize(enum m0_net_test_serialize_op op, void *obj, const struct m0_net_test_descr descr[], size_t descr_nr, struct m0_bufvec *bv, m0_bcount_t bv_offset)
Definition: serialize.c:176
static void simple_struct_test(char c, unsigned char uc, short s, unsigned short us, int i, unsigned int ui, long l, unsigned long ul, long long ll, unsigned long long ull, int8_t i8, uint8_t u8, int16_t i16, uint16_t u16, int32_t i32, uint32_t u32, int64_t i64, uint64_t u64)
Definition: serialize.c:110
#define INT32_MIN
Definition: types.h:42
struct m0_vec ov_vec
Definition: vec.h:147
#define INT64_MAX
Definition: types.h:46
unsigned long ss_ul
Definition: serialize.c:66
uint64_t m0_bcount_t
Definition: types.h:77
static int void * buf
Definition: dir.c:1019
#define M0_SET0(obj)
Definition: misc.h:64
#define UINT32_MAX
Definition: types.h:41
Definition: sock.c:887
unsigned short ss_us
Definition: serialize.c:62
op
Definition: libdemo.c:64
int i
Definition: dir.c:1033
#define M0_SET_ARR0(arr)
Definition: misc.h:72
#define UINT8_MAX
Definition: types.h:35
TYPE_DESCR(simple_struct)
#define INT8_MIN
Definition: types.h:36
#define UINT16_MAX
Definition: types.h:38
static struct m0_addb2_callback c
Definition: consumer.c:41
unsigned long long ss_ull
Definition: serialize.c:68
#define FIELD_DESCR(type, field)
Definition: serialize.h:63
#define INT8_MAX
Definition: types.h:37
Definition: xcode.h:73
#define UINT64_MAX
Definition: types.h:44
m0_bcount_t * v_count
Definition: vec.h:53
void m0_net_test_serialize_ut(void)
Definition: serialize.c:197
static struct m0_clink l[NR]
Definition: chan.c:37
short ss_s
Definition: serialize.c:61
int32_t ss_i32
Definition: serialize.c:73
unsigned int ss_ui
Definition: serialize.c:64
m0_net_test_serialize_op
Definition: serialize.h:42
#define INT64_MIN
Definition: types.h:45
unsigned char ss_uc
Definition: serialize.c:60
int64_t ss_i64
Definition: serialize.c:75
#define INT16_MIN
Definition: types.h:39
uint32_t ss_u32
Definition: serialize.c:74
#define INT16_MAX
Definition: types.h:40
static struct m0_addb2_source * s
Definition: consumer.c:39
uint16_t ss_u16
Definition: serialize.c:72
int8_t ss_i8
Definition: serialize.c:69
#define M0_UT_ASSERT(a)
Definition: ut.h:46
#define USE_TYPE_DESCR(type_name)
Definition: serialize.h:57
Definition: vec.h:145
#define INT32_MAX
Definition: types.h:43
uint64_t ss_u64
Definition: serialize.c:76
int16_t ss_i16
Definition: serialize.c:71
uint8_t ss_u8
Definition: serialize.c:70
static m0_bcount_t simple_struct_serialize(enum m0_net_test_serialize_op op, struct simple_struct *ss, struct m0_bufvec *bv, m0_bcount_t bv_offset)
Definition: serialize.c:101