5748ca136f22dc600348254574c9a8af78c6c063
[ric-plt/lib/rmr.git] / test / test_gen_rt.c
1 // : vi ts=4 sw=4 noet :
2 /*
3 ==================================================================================
4             Copyright (c) 2019 Nokia
5             Copyright (c) 2018-2019 AT&T Intellectual Property.
6
7    Licensed under the Apache License, Version 2.0 (the "License");
8    you may not use this file except in compliance with the License.
9    You may obtain a copy of the License at
10
11            http://www.apache.org/licenses/LICENSE-2.0
12
13    Unless required by applicable law or agreed to in writing, software
14    distributed under the License is distributed on an "AS IS" BASIS,
15    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16    See the License for the specific language governing permissions and
17    limitations under the License.
18 ==================================================================================
19 */
20
21 /*
22         Mnemonic:       test_gen_rt.c
23         Abstract:       This provides the means to generate a route table to disk.
24         Author:         E. Scott Daniels
25         Date:           6 January 2019
26 */
27
28 #ifndef _test_gen_rt_c
29 #define _test_gen_rt_c
30
31
32 /*
33         Generate a simple route table (for all but direct route table testing).
34         This gets tricky inasmuch as we generate two in one; first a whole table 
35         and then two update tables. The first is a table with a bad counter in the
36         last record to test that we don't load that table and error. The second
37         is a good update. The same applies to the meid map; first has a bad counter
38         and some bad records to drive coverage testing. The end should leave a good
39         meid map in the table.
40
41         Once the file is generated, it is forced in via static read and the file
42         is unlinked.
43 */
44 static void gen_rt( uta_ctx_t* ctx ) {
45         int             fd;
46         char*   rt_stuff;               // strings for the route table
47
48 ctx->flags |= 0x08;
49         fd = open( "utesting.rt", O_WRONLY | O_CREAT, 0600 );
50         if( fd < 0 ) {
51                 fprintf( stderr, "<BUGGERED> unable to open file for testing route table gen\n" );
52                 return;
53         }
54
55         rt_stuff =
56                 "newrt|end\n"                                                           // end of table check before start of table found
57                 "# comment to drive full comment test\n"
58                 "\n"                                                                            // handle blank lines
59                 "   \n"                                                                         // handle blank lines
60             "mse|4|10|localhost:4561\n"                                 // entry before start message
61             "rte|4|localhost:4561\n"                                    // entry before start message
62                 "newrt|start\n"                                                         // false start to drive detection
63                 "xxx|badentry to drive default case"
64                 "newrt|start\n"
65             "rte|0|localhost:4560,localhost:4562\n"                                     // these are legitimate entries for our testing
66             "rte|1|localhost:4562;localhost:4561,localhost:4569\n"
67             "rte|2|localhost:4562| 10\n"                                                                // new subid at end
68             "mse|4|10|localhost:4561\n"                                                                 // new msg/subid specifier rec
69             "mse|4|localhost:4561\n"                                                                    // new mse entry with less than needed fields
70                 "   rte|   5   |localhost:4563    #garbage comment\n"           // tests white space cleanup
71             "rte|6|localhost:4562\n"
72                 "newrt|end\n";
73
74         setenv( "RMR_SEED_RT", "utesting.rt", 1 );
75         write( fd, rt_stuff, strlen( rt_stuff ) );                              // write in the whole table
76
77         rt_stuff =                                                                                              // add an meid map which will fail
78                 "meid_map | start\n"
79                 "mme_ar | e2t-1 | one two three four\r"                         // also test bloody apple way with \r
80                 "mme_del | one two\n"
81                 "mme_del \n"                                                                            // short entries drive various checks for coverage
82                 "mme_ar \n"                                                                                     
83                 "mme_ar | e2t-0 \n"                                                                     
84                 "meid_map | end | 5\n";                                                         // this will fail as the short recs don't "count"
85         write( fd, rt_stuff, strlen( rt_stuff ) );
86
87         rt_stuff =
88                 "updatert|start\n"                                                                      // this is an update to the table
89             "mse|4|99|fooapp:9999,barapp:9999;logger:9999\n"    // update just one entry
90                 "updatert|end | 3\n";                                                           // bad count; this update should be rejected
91         write( fd, rt_stuff, strlen( rt_stuff ) );
92
93
94         rt_stuff =
95                 "updatert|start\n"                                                                      // this is an update to the table
96             "mse|4|10|fooapp:4561,barapp:4561;logger:9999\n"    // update just one entry
97                 "mse | 99 | -1 | %meid\n"                                                       // type 99 will route based on meid and not mtype
98                 "del|2|-1\n"                                                                            // delete an entry; not there so no action
99                 "del|2|10\n"                                                                            // delete an entry
100                 "updatert|end | 4\n";                                                           // end table; updates have a count as last field
101         write( fd, rt_stuff, strlen( rt_stuff ) );
102
103         rt_stuff =                                                                                              // this leaves an meid map in place too
104                 "meid_map | start\n"
105                 "mme_ar | localhost:4567 | meid1 meid2 meid3 meid4\n"
106                 "mme_ar | localhost:4067 | meid11 meid12\n"
107                 "meid_map | end | 2\n";
108         write( fd, rt_stuff, strlen( rt_stuff ) );
109
110         rt_stuff =                                                                                                      // verify that we can del entries in the current table
111                 "meid_map | start\n"
112                 "mme_del | meid11 meid12 meid13\n"              // includes a non-existant meid
113                 "meid_map | end | 1\n";
114         write( fd, rt_stuff, strlen( rt_stuff ) );
115         
116         close( fd );
117         read_static_rt( ctx, 1 );                                                               // force in verbose mode to see stats on tty if failure
118         unlink( "utesting.rt" );
119 }
120
121
122
123 /*
124         Generate a custom route table file using the buffer passed in.
125         Then force it to be read into the current route table. The file
126         is unlinked when finished.
127 */
128 static void gen_custom_rt( uta_ctx_t* ctx, char* buf ) {
129         int             fd;
130
131         fd = open( "Xutesting.rt", O_WRONLY | O_CREAT, 0600 );
132         if( fd < 0 ) {
133                 fprintf( stderr, "<BUGGERED> unable to open file for testing route table gen\n" );
134                 return;
135         }
136         setenv( "RMR_SEED_RT", "Xutesting.rt", 1 );
137
138         fprintf( stderr, "<INFO> creating custom rt from buffer (%d bytes)\n", (int) strlen (buf ) );
139         if( write( fd, buf, strlen( buf ) ) != strlen( buf ) ) {
140                 fprintf( stderr, "<BUGGERED> write failed: %s\n", strerror( errno ) );
141         }
142
143         close( fd );
144         read_static_rt( ctx, 1 );                                                               // force in verbose mode to see stats on tty if failure
145         unlink( "utesting.rt" );
146 }
147
148
149 #endif