c11 safe string handling support
[vpp.git] / src / vppinfra / test_maplog.c
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15
16 #include <vppinfra/maplog.h>
17
18 clib_maplog_main_t maplog_main;
19
20 typedef struct
21 {
22   u64 serial_number;
23   u64 junk[7];
24 } test_entry_t;
25
26 typedef enum
27 {
28   TEST_NORMAL,
29   TEST_CIRCULAR,
30 } test_type_t;
31
32 static void
33 process_maplog_records (clib_maplog_header_t * h,
34                         test_entry_t * e, u64 records_this_file)
35 {
36   static int print_header;
37   int i = 0;
38
39   if (print_header == 0)
40     {
41       print_header = 1;
42       fformat (stdout, "%U", format_maplog_header, h, 1 /* verbose */ );
43     }
44
45   while (records_this_file--)
46     {
47       /* Padding at the end of a damaged log? */
48       if (e->serial_number == 0ULL)
49         break;
50       fformat (stdout, "%4lld ", e->serial_number);
51       if (++i == 8)
52         {
53           fformat (stdout, "\n");
54           i = 0;
55         }
56       e++;
57     }
58   fformat (stdout, "\n--------------\n");
59 }
60
61 int
62 test_maplog_main (unformat_input_t * input)
63 {
64   clib_maplog_main_t *mm = &maplog_main;
65   clib_maplog_init_args_t _a, *a = &_a;
66   int rv;
67   int i, limit;
68   test_entry_t *t;
69   int noclose = 0;
70   test_type_t which = TEST_NORMAL;
71
72   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
73     {
74       if (unformat (input, "noclose"))
75         noclose = 1;
76       else if (unformat (input, "circular"))
77         which = TEST_CIRCULAR;
78       else
79         clib_warning ("unknown input '%U'", format_unformat_error, input);
80     }
81
82   clib_memset (a, 0, sizeof (*a));
83   a->mm = mm;
84   a->file_basename = "/tmp/maplog_test";
85   a->file_size_in_bytes = 4096;
86   a->record_size_in_bytes = sizeof (test_entry_t);
87   a->application_id = 1;
88   a->application_major_version = 1;
89   a->application_minor_version = 0;
90   a->application_patch_version = 0;
91   a->maplog_is_circular = (which == TEST_CIRCULAR) ? 1 : 0;
92
93   rv = clib_maplog_init (a);
94
95   if (rv)
96     {
97       clib_warning ("clib_maplog_init returned %d", rv);
98       exit (1);
99     }
100
101   limit = (which == TEST_CIRCULAR) ? (64 + 2) : 64 * 5;
102
103   for (i = 0; i < limit; i++)
104     {
105       t = clib_maplog_get_entry (mm);
106       t->serial_number = i + 1;
107     }
108
109   if (noclose)
110     clib_memset (mm, 0, sizeof (*mm));
111   else
112     clib_maplog_close (mm);
113
114   clib_maplog_process ("/tmp/maplog_test", process_maplog_records);
115
116   return 0;
117 }
118
119 #ifdef CLIB_UNIX
120 int
121 main (int argc, char *argv[])
122 {
123   unformat_input_t i;
124   int ret;
125
126   clib_mem_init (0, 64ULL << 20);
127
128   unformat_init_command_line (&i, argv);
129   ret = test_maplog_main (&i);
130   unformat_free (&i);
131
132   return ret;
133 }
134 #endif /* CLIB_UNIX */
135
136
137 /*
138  * fd.io coding-style-patch-verification: ON
139  *
140  * Local Variables:
141  * eval: (c-set-style "gnu")
142  * End:
143  */