E. Scott Daniels | 2301eb8 | 2020-07-15 13:46:51 -0400 | [diff] [blame^] | 1 | // vi: ts=4 sw=4 noet: |
| 2 | /* |
| 3 | ================================================================================== |
| 4 | Copyright (c) 2020 AT&T Intellectual Property. |
| 5 | Copyright (c) 2020 Nokia |
| 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: config_test.cpp |
| 23 | Abstract: Unit test for config odule |
| 24 | |
| 25 | Date: 13 July 2020 |
| 26 | Author: E. Scott Daniels |
| 27 | */ |
| 28 | |
| 29 | #include <memory> |
| 30 | |
| 31 | #include "ricxfcpp/jhash.hpp" |
| 32 | |
| 33 | #include "config.h" // things under test to build with coverage options |
| 34 | #include "config.cpp" |
| 35 | |
| 36 | #include "ut_support.cpp" |
| 37 | |
| 38 | /* |
| 39 | Drive the tests across a single config file. |
| 40 | */ |
| 41 | static int drive_config( std::string fname, bool expect_port ) { |
| 42 | int errors = 0; |
| 43 | std::shared_ptr<Jhash> jh; |
| 44 | std::string svalue; |
| 45 | std::string url_def; |
| 46 | bool bvalue; |
| 47 | |
| 48 | jh = munchkin::Conf_parse( fname ); |
| 49 | errors += fail_if( jh == NULL, "did not allocate json hash for valid file (some tests being skipped as a result)" ); |
| 50 | |
| 51 | if( jh != NULL ) { |
| 52 | |
| 53 | // --- test digging from controls section ----------------------------------------- |
| 54 | url_def = "https://localhost:29080"; |
| 55 | svalue = munchkin::Conf_get_cvstr( jh, "collector_url", url_def ); // override default expected |
| 56 | errors += fail_if( svalue.compare( "" ) == 0, "cvstr for good value returned empty string" ); |
| 57 | errors += fail_if( svalue.compare( url_def ) == 0, "cvstr for good value returned the default" ); |
| 58 | |
| 59 | bvalue = munchkin::Conf_get_cvbool( jh, "hr_logging", false ); |
| 60 | errors += fail_if( !bvalue, "hr_logging reported false" ); |
| 61 | |
| 62 | bvalue = munchkin::Conf_get_cvbool( jh, "hr_logging" ); |
| 63 | errors += fail_if( !bvalue, "hr_logging with no default reported false" ); |
| 64 | |
| 65 | bvalue = munchkin::Conf_get_cvbool( jh, "novar", false ); |
| 66 | errors += fail_if( bvalue, "no bool reported true" ); |
| 67 | |
| 68 | svalue = munchkin::Conf_get_cvstr( jh, "Xcollector_url", url_def ); // default expected |
| 69 | errors += fail_if( svalue.compare( "" ) == 0, "cvstr for missing value returned empty string" ); |
| 70 | errors += fail_if( svalue.compare( url_def ) != 0, "cvstr for missing value did not return the default" ); |
| 71 | |
| 72 | svalue = munchkin::Conf_get_cvstr( jh, "Xcollector_url" ); // coverage w/o defaut; expect "" |
| 73 | errors += fail_if( svalue.compare( "" ) != 0, "cvstr for missing value didn't return the empty string" ); |
| 74 | |
| 75 | // ---- test ability to dig port string out --------------------------------------- |
| 76 | svalue = munchkin::Conf_get_port( jh, "rmr-data" ); |
| 77 | if( expect_port ) { |
| 78 | errors += fail_if( svalue.compare( "4560" ) != 0, "didn't get port" ); |
| 79 | } else { |
| 80 | errors += fail_if( svalue.compare( "45l0" ) == 0, "got port when not expected" ); |
| 81 | } |
| 82 | |
| 83 | } |
| 84 | |
| 85 | return errors; |
| 86 | } |
| 87 | |
| 88 | int main( int argc, char** argv ) { |
| 89 | std::string svalue; |
| 90 | std::shared_ptr<Jhash> jh; |
| 91 | int errors = 0; |
| 92 | |
| 93 | set_test_name( "config_test" ); |
| 94 | |
| 95 | |
| 96 | fprintf( stderr, "<INFO> ---- warning messages expected below this and are OK\n" ); |
| 97 | |
| 98 | jh = munchkin::Conf_parse( "data/no_such_file" ); |
| 99 | errors += fail_if( jh != NULL, "allocated json hash for a bad file" ); |
| 100 | |
| 101 | errors += drive_config( "data/config.json", true ); // "normal" config |
| 102 | errors += drive_config( "data/config2.json", true ); // "additional ports and ordering to drive coverage |
| 103 | errors += drive_config( "data/config3.json", false ); |
| 104 | |
| 105 | svalue = munchkin::Conf_get_cvstr( NULL, "Xcollector_url" ); |
| 106 | errors += fail_if( svalue.compare( "" ) != 0, "given a nil pointer didn't return empty value" ); |
| 107 | |
| 108 | svalue = munchkin::Conf_get_port( NULL, "rmr-data" ); |
| 109 | errors += fail_if( svalue.compare( "" ) != 0, "get port given a nil pointer didn't return empty value" ); |
| 110 | |
| 111 | announce_results( errors ); |
| 112 | exit( !! errors ); |
| 113 | } |