79b413f8bb420026f8baebf42c1aafe9f9b18c73
[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         fd = open( "utesting.rt", O_WRONLY | O_CREAT, 0600 );
49         if( fd < 0 ) {
50                 fprintf( stderr, "<BUGGERED> unable to open file for testing route table gen\n" );
51                 return;
52         }
53
54         rt_stuff =
55                 "newrt|end\n"                                                           // end of table check before start of table found
56                 "# comment to drive full comment test\n"
57                 "\n"                                                                            // handle blank lines
58                 "   \n"                                                                         // handle blank lines
59             "mse|4|10|localhost:4561\n"                                 // entry before start message
60             "rte|4|localhost:4561\n"                                    // entry before start message
61                 "newrt|start\n"                                                         // false start to drive detection
62                 "xxx|badentry to drive default case"
63                 "newrt|start\n"
64             "rte|0|localhost:4560,localhost:4562\n"                                     // these are legitimate entries for our testing
65             "rte|1|localhost:4562;localhost:4561,localhost:4569\n"
66             "rte|2|localhost:4562| 10\n"                                                                // new subid at end
67             "mse|4|10|localhost:4561\n"                                                                 // new msg/subid specifier rec
68             "mse|4|localhost:4561\n"                                                                    // new mse entry with less than needed fields
69                 "   rte|   5   |localhost:4563    #garbage comment\n"           // tests white space cleanup
70             "rte|6|localhost:4562\n"
71                 "newrt|end\n";
72
73         setenv( "RMR_SEED_RT", "utesting.rt", 1 );
74         write( fd, rt_stuff, strlen( rt_stuff ) );                              // write in the whole table
75
76         rt_stuff =                                                                                              // add an meid map which will fail
77                 "meid_map | start\n"
78                 "mme_ar | e2t-1 | one two three four\r"                         // also test bloody apple way with \r
79                 "mme_del | one two\n"
80                 "mme_del \n"                                                                            // short entries drive various checks for coverage
81                 "mme_ar \n"                                                                                     
82                 "mme_ar | e2t-0 \n"                                                                     
83                 "meid_map | end | 5\n";                                                         // this will fail as the short recs don't "count"
84         write( fd, rt_stuff, strlen( rt_stuff ) );
85
86         rt_stuff =
87                 "updatert|start\n"                                                                      // this is an update to the table
88             "mse|4|99|fooapp:9999,barapp:9999;logger:9999\n"    // update just one entry
89                 "updatert|end | 3\n";                                                           // bad count; this update should be rejected
90         write( fd, rt_stuff, strlen( rt_stuff ) );
91
92
93         rt_stuff =
94                 "updatert|start\n"                                                                      // this is an update to the table
95             "mse|4|10|fooapp:4561,barapp:4561;logger:9999\n"    // update just one entry
96                 "mse | 99 | -1 | %meid\n"                                                       // type 99 will route based on meid and not mtype
97                 "del|2|-1\n"                                                                            // delete an entry; not there so no action
98                 "del|2|10\n"                                                                            // delete an entry
99                 "updatert|end | 4\n";                                                           // end table; updates have a count as last field
100         write( fd, rt_stuff, strlen( rt_stuff ) );
101
102         rt_stuff =                                                                                              // this leaves an meid map in place too
103                 "meid_map | start\n"
104                 "mme_ar | localhost:4567 | meid1 meid2 meid3 meid4\n"
105                 "mme_ar | localhost:4067 | meid11 meid12\n"
106                 "meid_map | end | 2\n";
107         write( fd, rt_stuff, strlen( rt_stuff ) );
108
109         rt_stuff =                                                                                                      // verify that we can del entries in the current table
110                 "meid_map | start\n"
111                 "mme_del | meid11 meid12 meid13\n"              // includes a non-existant meid
112                 "meid_map | end | 1\n";
113         write( fd, rt_stuff, strlen( rt_stuff ) );
114         
115         close( fd );
116         read_static_rt( ctx, 1 );                                                               // force in verbose mode to see stats on tty if failure
117         unlink( "utesting.rt" );
118 }
119
120
121
122 /*
123         Generate a custom route table file using the buffer passed in.
124         Then force it to be read into the current route table. The file
125         is unlinked when finished.
126 */
127 static void gen_custom_rt( uta_ctx_t* ctx, char* buf ) {
128         int             fd;
129
130         fd = open( "Xutesting.rt", O_WRONLY | O_CREAT, 0600 );
131         if( fd < 0 ) {
132                 fprintf( stderr, "<BUGGERED> unable to open file for testing route table gen\n" );
133                 return;
134         }
135         setenv( "RMR_SEED_RT", "Xutesting.rt", 1 );
136
137         fprintf( stderr, "<INFO> creating custom rt from buffer (%d bytes)\n", (int) strlen (buf ) );
138         if( write( fd, buf, strlen( buf ) ) != strlen( buf ) ) {
139                 fprintf( stderr, "<BUGGERED> write failed: %s\n", strerror( errno ) );
140         }
141
142         close( fd );
143         read_static_rt( ctx, 1 );                                                               // force in verbose mode to see stats on tty if failure
144         unlink( "utesting.rt" );
145 }
146
147
148 #endif