LCOV - code coverage report
Current view: top level - tests - test_as2js_rc.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 468 590 79.3 %
Date: 2014-11-22 Functions: 16 19 84.2 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* test_as2js_rc.cpp -- written by Alexis WILKE for Made to Order Software Corp. (c) 2005-2014 */
       2             : 
       3             : /*
       4             : 
       5             : Copyright (c) 2005-2014 Made to Order Software Corp.
       6             : 
       7             : http://snapwebsites.org/project/as2js
       8             : 
       9             : Permission is hereby granted, free of charge, to any
      10             : person obtaining a copy of this software and
      11             : associated documentation files (the "Software"), to
      12             : deal in the Software without restriction, including
      13             : without limitation the rights to use, copy, modify,
      14             : merge, publish, distribute, sublicense, and/or sell
      15             : copies of the Software, and to permit persons to whom
      16             : the Software is furnished to do so, subject to the
      17             : following conditions:
      18             : 
      19             : The above copyright notice and this permission notice
      20             : shall be included in all copies or substantial
      21             : portions of the Software.
      22             : 
      23             : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
      24             : ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
      25             : LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
      26             : FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
      27             : EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
      28             : LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
      29             : WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
      30             : ARISING FROM, OUT OF OR IN CONNECTION WITH THE
      31             : SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
      32             : SOFTWARE.
      33             : 
      34             : */
      35             : 
      36             : #include    "test_as2js_rc.h"
      37             : #include    "test_as2js_main.h"
      38             : 
      39             : #include    "rc.h"
      40             : #include    "as2js/exceptions.h"
      41             : #include    "as2js/message.h"
      42             : 
      43             : #include    <controlled_vars/controlled_vars_limited_auto_enum_init.h>
      44             : 
      45             : #include    <unistd.h>
      46             : #include    <sys/stat.h>
      47             : 
      48             : #include    <cstring>
      49             : #include    <algorithm>
      50             : #include    <iomanip>
      51             : 
      52             : #include    <cppunit/config/SourcePrefix.h>
      53           4 : CPPUNIT_TEST_SUITE_REGISTRATION( As2JsRCUnitTests );
      54             : 
      55             : namespace
      56             : {
      57             : 
      58             : 
      59             : class test_callback : public as2js::MessageCallback
      60             : {
      61             : public:
      62           5 :     test_callback()
      63           5 :     {
      64           5 :         as2js::Message::set_message_callback(this);
      65           5 :         g_warning_count = as2js::Message::warning_count();
      66           5 :         g_error_count = as2js::Message::error_count();
      67           5 :     }
      68             : 
      69           5 :     ~test_callback()
      70          10 :     {
      71             :         // make sure the pointer gets reset!
      72           5 :         as2js::Message::set_message_callback(nullptr);
      73           5 :     }
      74             : 
      75             :     // implementation of the output
      76          10 :     virtual void output(as2js::message_level_t message_level, as2js::err_code_t error_code, as2js::Position const& pos, std::string const& message)
      77             :     {
      78          10 :         if(f_expected.empty())
      79             :         {
      80           0 :             std::cerr << "\nfilename = " << pos.get_filename() << "\n";
      81           0 :             std::cerr << "msg = " << message << "\n";
      82           0 :             std::cerr << "page = " << pos.get_page() << "\n";
      83           0 :             std::cerr << "line = " << pos.get_line() << "\n";
      84           0 :             std::cerr << "error_code = " << static_cast<int>(error_code) << "\n";
      85             :         }
      86             : 
      87          10 :         CPPUNIT_ASSERT(!f_expected.empty());
      88             : 
      89             : //std::cerr << "filename = " << pos.get_filename() << " / " << f_expected[0].f_pos.get_filename() << "\n";
      90             : //std::cerr << "msg = " << message << " / " << f_expected[0].f_message << "\n";
      91             : //std::cerr << "page = " << pos.get_page() << " / " << f_expected[0].f_pos.get_page() << "\n";
      92             : //std::cerr << "line = " << pos.get_line() << " / " << f_expected[0].f_pos.get_line() << "\n";
      93             : //std::cerr << "error_code = " << static_cast<int>(error_code) << " / " << static_cast<int>(f_expected[0].f_error_code) << "\n";
      94             : 
      95          10 :         CPPUNIT_ASSERT(f_expected[0].f_call);
      96          10 :         CPPUNIT_ASSERT(message_level == f_expected[0].f_message_level);
      97          10 :         CPPUNIT_ASSERT(error_code == f_expected[0].f_error_code);
      98          10 :         CPPUNIT_ASSERT(pos.get_filename() == f_expected[0].f_pos.get_filename());
      99          10 :         CPPUNIT_ASSERT(pos.get_function() == f_expected[0].f_pos.get_function());
     100          10 :         CPPUNIT_ASSERT(pos.get_page() == f_expected[0].f_pos.get_page());
     101          10 :         CPPUNIT_ASSERT(pos.get_page_line() == f_expected[0].f_pos.get_page_line());
     102          10 :         CPPUNIT_ASSERT(pos.get_paragraph() == f_expected[0].f_pos.get_paragraph());
     103          10 :         CPPUNIT_ASSERT(pos.get_line() == f_expected[0].f_pos.get_line());
     104          10 :         CPPUNIT_ASSERT(message == f_expected[0].f_message);
     105             : 
     106          10 :         if(message_level == as2js::message_level_t::MESSAGE_LEVEL_WARNING)
     107             :         {
     108           0 :             ++g_warning_count;
     109           0 :             CPPUNIT_ASSERT(g_warning_count == as2js::Message::warning_count());
     110             :         }
     111             : 
     112          10 :         if(message_level == as2js::message_level_t::MESSAGE_LEVEL_FATAL
     113           0 :         || message_level == as2js::message_level_t::MESSAGE_LEVEL_ERROR)
     114             :         {
     115          10 :             ++g_error_count;
     116             : //std::cerr << "error: " << g_error_count << " / " << as2js::Message::error_count() << "\n";
     117          10 :             CPPUNIT_ASSERT(g_error_count == as2js::Message::error_count());
     118             :         }
     119             : 
     120          10 :         f_expected.erase(f_expected.begin());
     121          10 :     }
     122             : 
     123          10 :     void got_called()
     124             :     {
     125          10 :         if(!f_expected.empty())
     126             :         {
     127           0 :             std::cerr << "\n*** STILL EXPECTED: ***\n";
     128           0 :             std::cerr << "filename = " << f_expected[0].f_pos.get_filename() << "\n";
     129           0 :             std::cerr << "msg = " << f_expected[0].f_message << "\n";
     130           0 :             std::cerr << "page = " << f_expected[0].f_pos.get_page() << "\n";
     131           0 :             std::cerr << "error_code = " << static_cast<int>(f_expected[0].f_error_code) << "\n";
     132             :         }
     133          10 :         CPPUNIT_ASSERT(f_expected.empty());
     134          10 :     }
     135             : 
     136          40 :     struct expected_t
     137             :     {
     138             :         controlled_vars::tlbool_t   f_call;
     139             :         as2js::message_level_t      f_message_level;
     140             :         as2js::err_code_t           f_error_code;
     141             :         as2js::Position             f_pos;
     142             :         std::string                 f_message; // UTF-8 string
     143             :     };
     144             : 
     145             :     std::vector<expected_t>     f_expected;
     146             : 
     147             :     static controlled_vars::zint32_t   g_warning_count;
     148             :     static controlled_vars::zint32_t   g_error_count;
     149             : };
     150             : 
     151           4 : controlled_vars::zint32_t   test_callback::g_warning_count;
     152           4 : controlled_vars::zint32_t   test_callback::g_error_count;
     153             : 
     154           4 : controlled_vars::zint32_t   g_empty_home_too_late;
     155             : 
     156             : }
     157             : // no name namespace
     158             : 
     159             : 
     160             : 
     161             : 
     162           7 : void As2JsRCUnitTests::setUp()
     163             : {
     164             :     // verify that this user does not have existing rc files because
     165             :     // that can interfer with the tests! (and we do not want to delete
     166             :     // those under his/her feet)
     167             : 
     168             :     // AS2JS_RC variable
     169           7 :     CPPUNIT_ASSERT(getenv("AS2JS_RC") == nullptr);
     170             : 
     171             :     // local file
     172             :     struct stat st;
     173           7 :     CPPUNIT_ASSERT(stat("as2js/as2js.rc", &st) == -1);
     174             : 
     175             :     // user defined .config file
     176           7 :     as2js::String home;
     177           7 :     home.from_utf8(getenv("HOME"));
     178          14 :     as2js::String config(home);
     179           7 :     config += "/.config/as2js/as2js.rc";
     180          14 :     std::string cfg(config.to_utf8());
     181           7 :     CPPUNIT_ASSERT(stat(cfg.c_str(), &st) == -1);
     182             : 
     183             :     // system defined configuration file
     184          14 :     CPPUNIT_ASSERT(stat("/etc/as2js/as2js.rc", &st) == -1);
     185           7 : }
     186             : 
     187             : 
     188           1 : void As2JsRCUnitTests::test_basics()
     189             : {
     190             :     // this test is not going to work if the get_home() function was
     191             :     // already called with an empty HOME variable...
     192           1 :     if(g_empty_home_too_late == 2)
     193             :     {
     194           0 :         std::cout << " --- test_empty_home() not run, the other rc unit tests are not compatible with this test --- ";
     195           1 :         return;
     196             :     }
     197             : 
     198           1 :     g_empty_home_too_late = 1;
     199             : 
     200             :     {
     201             :         // test the get_home()
     202           1 :         as2js::String home;
     203           1 :         home.from_utf8(getenv("HOME"));
     204           2 :         as2js::String rc_home(as2js::rc_t::get_home());
     205           1 :         CPPUNIT_ASSERT(rc_home == home);
     206             : 
     207             :         // verify that changing the variable after the first change returns
     208             :         // the first value...
     209           1 :         char *new_home(reinterpret_cast<char *>(malloc(256UL)));
     210           1 :         CPPUNIT_ASSERT(new_home != nullptr);
     211           1 :         strcpy(new_home, "HOME=/got/changed/now");
     212           1 :         putenv(new_home);
     213           1 :         rc_home = as2js::rc_t::get_home();
     214           1 :         CPPUNIT_ASSERT(rc_home == home);
     215             : 
     216             :         // just in case, restore the variable
     217           1 :         char *restore_home(reinterpret_cast<char *>(malloc(home.to_utf8().length() + 10)));
     218           1 :         CPPUNIT_ASSERT(restore_home != nullptr);
     219           1 :         strcpy(restore_home, ("HOME=" + home.to_utf8()).c_str());
     220           2 :         putenv(restore_home);
     221             :     }
     222             : 
     223             :     {
     224           1 :         as2js::rc_t rc;
     225           1 :         CPPUNIT_ASSERT(rc.get_scripts() == "as2js/scripts");
     226           1 :         CPPUNIT_ASSERT(rc.get_db() == "/tmp/as2js_packages.db");
     227           1 :         CPPUNIT_ASSERT(rc.get_temporary_variable_name() == "@temp");
     228             :     }
     229             : 
     230             :     {
     231           1 :         as2js::rc_t rc;
     232             : 
     233           2 :         test_callback tc;
     234             : 
     235           2 :         test_callback::expected_t expected1;
     236           1 :         expected1.f_message_level = as2js::message_level_t::MESSAGE_LEVEL_FATAL;
     237           1 :         expected1.f_error_code = as2js::err_code_t::AS_ERR_INSTALLATION;
     238           1 :         expected1.f_pos.set_filename("unknown-file");
     239           1 :         expected1.f_pos.set_function("unknown-func");
     240           1 :         expected1.f_message = "cannot find the as2js.rc file; the system default is usually put in /etc/as2js/as2js.rc";
     241           1 :         tc.f_expected.push_back(expected1);
     242             : 
     243           1 :         CPPUNIT_ASSERT_THROW(rc.init_rc(false), as2js::exception_exit);
     244           1 :         tc.got_called();
     245             : 
     246           1 :         rc.init_rc(true);
     247             : 
     248           1 :         CPPUNIT_ASSERT(rc.get_scripts() == "as2js/scripts");
     249           2 :         CPPUNIT_ASSERT(rc.get_db() == "/tmp/as2js_packages.db");
     250             :     }
     251             : }
     252             : 
     253             : 
     254           1 : void As2JsRCUnitTests::test_load_from_var()
     255             : {
     256             :     // this test is not going to work if the get_home() function was
     257             :     // already called with an empty HOME variable...
     258           1 :     if(g_empty_home_too_late == 2)
     259             :     {
     260           0 :         std::cout << " --- test_empty_home() not run, the other rc unit tests are not compatible with this test --- ";
     261           1 :         return;
     262             :     }
     263             : 
     264           1 :     g_empty_home_too_late = 1;
     265             : 
     266             :     // just in case it failed before...
     267           1 :     unlink("as2js.rc");
     268             : 
     269             :     {
     270             :         char rc_env[256UL];
     271           1 :         strcpy(rc_env, "AS2JS_RC=.");
     272           1 :         putenv(rc_env);
     273             : 
     274           1 :         test_callback tc;
     275             : 
     276           2 :         test_callback::expected_t expected1;
     277           1 :         expected1.f_message_level = as2js::message_level_t::MESSAGE_LEVEL_FATAL;
     278           1 :         expected1.f_error_code = as2js::err_code_t::AS_ERR_INSTALLATION;
     279           1 :         expected1.f_pos.set_filename("unknown-file");
     280           1 :         expected1.f_pos.set_function("unknown-func");
     281           1 :         expected1.f_message = "cannot find the as2js.rc file; the system default is usually put in /etc/as2js/as2js.rc";
     282           1 :         tc.f_expected.push_back(expected1);
     283             : 
     284           2 :         as2js::rc_t rc;
     285           1 :         CPPUNIT_ASSERT_THROW(rc.init_rc(false), as2js::exception_exit);
     286           1 :         tc.got_called();
     287             : 
     288             :         {
     289             :             // create an .rc file
     290             :             {
     291           1 :                 std::ofstream rc_file;
     292           1 :                 rc_file.open("as2js.rc");
     293           1 :                 CPPUNIT_ASSERT(rc_file.is_open());
     294           1 :                 rc_file << "// rc file\n"
     295           1 :                         << "{\n"
     296           1 :                         << "  'scripts': 'the/script',\n"
     297           1 :                         << "  'db': 'that/db',\n"
     298           1 :                         << "  'temporary_variable_name': '@temp$'\n"
     299           1 :                         << "}\n";
     300             :             }
     301             : 
     302           1 :             rc.init_rc(true);
     303           1 :             unlink("as2js.rc");
     304             : 
     305           1 :             CPPUNIT_ASSERT(rc.get_scripts() == "the/script");
     306           1 :             CPPUNIT_ASSERT(rc.get_db() == "that/db");
     307           1 :             CPPUNIT_ASSERT(rc.get_temporary_variable_name() == "@temp$");
     308             :         }
     309             : 
     310             :         {
     311             :             // create an .rc file, without scripts
     312             :             {
     313           1 :                 std::ofstream rc_file;
     314           1 :                 rc_file.open("as2js.rc");
     315           1 :                 CPPUNIT_ASSERT(rc_file.is_open());
     316           1 :                 rc_file << "// rc file\n"
     317           1 :                         << "{\n"
     318           1 :                         << "  'db': 'that/db'\n"
     319           1 :                         << "}\n";
     320             :             }
     321             : 
     322           1 :             rc.init_rc(true);
     323           1 :             unlink("as2js.rc");
     324             : 
     325           1 :             CPPUNIT_ASSERT(rc.get_scripts() == "as2js/scripts");
     326           1 :             CPPUNIT_ASSERT(rc.get_db() == "that/db");
     327           1 :             CPPUNIT_ASSERT(rc.get_temporary_variable_name() == "@temp");
     328             :         }
     329             : 
     330             :         {
     331             :             // create an .rc file, without scripts
     332             :             {
     333           1 :                 std::ofstream rc_file;
     334           1 :                 rc_file.open("as2js.rc");
     335           1 :                 CPPUNIT_ASSERT(rc_file.is_open());
     336           1 :                 rc_file << "// rc file\n"
     337           1 :                         << "{\n"
     338           1 :                         << "  'scripts': 'the/script'\n"
     339           1 :                         << "}\n";
     340             :             }
     341             : 
     342           1 :             rc.init_rc(true);
     343           1 :             unlink("as2js.rc");
     344             : 
     345           1 :             CPPUNIT_ASSERT(rc.get_scripts() == "the/script");
     346           1 :             CPPUNIT_ASSERT(rc.get_db() == "/tmp/as2js_packages.db");
     347           1 :             CPPUNIT_ASSERT(rc.get_temporary_variable_name() == "@temp");
     348             :         }
     349             : 
     350             :         {
     351             :             // create an .rc file, with just the temporary variable name
     352             :             {
     353           1 :                 std::ofstream rc_file;
     354           1 :                 rc_file.open("as2js.rc");
     355           1 :                 CPPUNIT_ASSERT(rc_file.is_open());
     356           1 :                 rc_file << "// rc file\n"
     357           1 :                         << "{\n"
     358           1 :                         << "  \"temporary_variable_name\": \"what about validity of the value? -- we on purpose use @ because it is not valid in identifiers\"\n"
     359           1 :                         << "}\n";
     360             :             }
     361             : 
     362           1 :             rc.init_rc(true);
     363           1 :             unlink("as2js.rc");
     364             : 
     365           1 :             CPPUNIT_ASSERT(rc.get_scripts() == "as2js/scripts");
     366           1 :             CPPUNIT_ASSERT(rc.get_db() == "/tmp/as2js_packages.db");
     367           1 :             CPPUNIT_ASSERT(rc.get_temporary_variable_name() == "what about validity of the value? -- we on purpose use @ because it is not valid in identifiers");
     368             :         }
     369             : 
     370             :         {
     371             :             // create an .rc file, without scripts
     372             :             {
     373           1 :                 std::ofstream rc_file;
     374           1 :                 rc_file.open("as2js.rc");
     375           1 :                 CPPUNIT_ASSERT(rc_file.is_open());
     376           1 :                 rc_file << "// rc file\n"
     377           1 :                         << "{\n"
     378           1 :                         << "  'scripts': 123\n"
     379           1 :                         << "}\n";
     380             :             }
     381             : 
     382           1 :             test_callback::expected_t expected2;
     383           1 :             expected2.f_message_level = as2js::message_level_t::MESSAGE_LEVEL_FATAL;
     384           1 :             expected2.f_error_code = as2js::err_code_t::AS_ERR_UNEXPECTED_RC;
     385           1 :             expected2.f_pos.set_filename("./as2js.rc");
     386           1 :             expected2.f_pos.set_function("unknown-func");
     387           1 :             expected2.f_pos.new_line();
     388           1 :             expected2.f_pos.new_line();
     389           1 :             expected2.f_message = "A resource file is expected to be an object of string elements.";
     390           1 :             tc.f_expected.push_back(expected2);
     391             : 
     392           1 :             CPPUNIT_ASSERT_THROW(rc.init_rc(true), as2js::exception_exit);
     393           1 :             tc.got_called();
     394           1 :             unlink("as2js.rc");
     395             : 
     396           1 :             CPPUNIT_ASSERT(rc.get_scripts() == "as2js/scripts");
     397           1 :             CPPUNIT_ASSERT(rc.get_db() == "/tmp/as2js_packages.db");
     398             :         }
     399             : 
     400             :         {
     401             :             // create a null .rc file
     402             :             {
     403           1 :                 std::ofstream rc_file;
     404           1 :                 rc_file.open("as2js.rc");
     405           1 :                 CPPUNIT_ASSERT(rc_file.is_open());
     406           1 :                 rc_file << "// rc file\n"
     407           1 :                         << "null\n";
     408             :             }
     409             : 
     410           1 :             rc.init_rc(false);
     411           1 :             unlink("as2js.rc");
     412             : 
     413           1 :             CPPUNIT_ASSERT(rc.get_scripts() == "as2js/scripts");
     414           1 :             CPPUNIT_ASSERT(rc.get_db() == "/tmp/as2js_packages.db");
     415             :         }
     416             : 
     417             :         {
     418             :             // create an .rc file, without an object nor null
     419             :             {
     420           1 :                 std::ofstream rc_file;
     421           1 :                 rc_file.open("as2js.rc");
     422           1 :                 CPPUNIT_ASSERT(rc_file.is_open());
     423           1 :                 rc_file << "// rc file\n"
     424           1 :                         << "['scripts', 123]\n";
     425             :             }
     426             : 
     427           1 :             test_callback::expected_t expected2;
     428           1 :             expected2.f_message_level = as2js::message_level_t::MESSAGE_LEVEL_FATAL;
     429           1 :             expected2.f_error_code = as2js::err_code_t::AS_ERR_UNEXPECTED_RC;
     430           1 :             expected2.f_pos.set_filename("./as2js.rc");
     431           1 :             expected2.f_pos.set_function("unknown-func");
     432           1 :             expected2.f_pos.new_line();
     433           1 :             expected2.f_message = "A resource file (.rc) must be defined as a JSON object, or set to 'null'.";
     434           1 :             tc.f_expected.push_back(expected2);
     435             : 
     436           1 :             CPPUNIT_ASSERT_THROW(rc.init_rc(true), as2js::exception_exit);
     437           1 :             tc.got_called();
     438           1 :             unlink("as2js.rc");
     439             : 
     440           1 :             CPPUNIT_ASSERT(rc.get_scripts() == "as2js/scripts");
     441           1 :             CPPUNIT_ASSERT(rc.get_db() == "/tmp/as2js_packages.db");
     442             :         }
     443             : 
     444             :         // test some other directory too
     445           1 :         strcpy(rc_env, "AS2JS_RC=/tmp");
     446           1 :         putenv(rc_env);
     447             : 
     448             :         {
     449             :             // create an .rc file
     450             :             {
     451           1 :                 std::ofstream rc_file;
     452           1 :                 rc_file.open("/tmp/as2js.rc");
     453           1 :                 CPPUNIT_ASSERT(rc_file.is_open());
     454           1 :                 rc_file << "// rc file\n"
     455           1 :                         << "{\n"
     456           1 :                         << "  'scripts': 'the/script',\n"
     457           1 :                         << "  'db': 'that/db'\n"
     458           1 :                         << "}\n";
     459             :             }
     460             : 
     461           1 :             rc.init_rc(true);
     462           1 :             unlink("/tmp/as2js.rc");
     463             : 
     464           1 :             CPPUNIT_ASSERT(rc.get_scripts() == "the/script");
     465           1 :             CPPUNIT_ASSERT(rc.get_db() == "that/db");
     466             :         }
     467             : 
     468             :         // make sure to delete that before exiting
     469           1 :         strcpy(rc_env, "AS2JS_RC");
     470           2 :         putenv(rc_env);
     471             :     }
     472             : }
     473             : 
     474             : 
     475           1 : void As2JsRCUnitTests::test_load_from_local()
     476             : {
     477             :     // this test is not going to work if the get_home() function was
     478             :     // already called with an empty HOME variable...
     479           1 :     if(g_empty_home_too_late == 2)
     480             :     {
     481           0 :         std::cout << " --- test_empty_home() not run, the other rc unit tests are not compatible with this test --- ";
     482           1 :         return;
     483             :     }
     484             : 
     485           1 :     g_empty_home_too_late = 1;
     486             : 
     487             :     // just in case it failed before...
     488           1 :     unlink("as2js/as2js.rc");
     489             : 
     490           1 :     mkdir("as2js", 0700);
     491             : 
     492             :     {
     493           1 :         test_callback tc;
     494             : 
     495           2 :         test_callback::expected_t expected1;
     496           1 :         expected1.f_message_level = as2js::message_level_t::MESSAGE_LEVEL_FATAL;
     497           1 :         expected1.f_error_code = as2js::err_code_t::AS_ERR_INSTALLATION;
     498           1 :         expected1.f_pos.set_filename("unknown-file");
     499           1 :         expected1.f_pos.set_function("unknown-func");
     500           1 :         expected1.f_message = "cannot find the as2js.rc file; the system default is usually put in /etc/as2js/as2js.rc";
     501           1 :         tc.f_expected.push_back(expected1);
     502             : 
     503           2 :         as2js::rc_t rc;
     504           1 :         CPPUNIT_ASSERT_THROW(rc.init_rc(false), as2js::exception_exit);
     505           1 :         tc.got_called();
     506             : 
     507             :         {
     508             :             // create an .rc file
     509             :             {
     510           1 :                 std::ofstream rc_file;
     511           1 :                 rc_file.open("as2js/as2js.rc");
     512           1 :                 CPPUNIT_ASSERT(rc_file.is_open());
     513           1 :                 rc_file << "// rc file\n"
     514           1 :                         << "{\n"
     515           1 :                         << "  'scripts': 'the/script',\n"
     516           1 :                         << "  'db': 'that/db'\n"
     517           1 :                         << "}\n";
     518             :             }
     519             : 
     520           1 :             rc.init_rc(true);
     521           1 :             unlink("as2js/as2js.rc");
     522             : 
     523           1 :             CPPUNIT_ASSERT(rc.get_scripts() == "the/script");
     524           1 :             CPPUNIT_ASSERT(rc.get_db() == "that/db");
     525             :         }
     526             : 
     527             :         {
     528             :             // create an .rc file, without scripts
     529             :             {
     530           1 :                 std::ofstream rc_file;
     531           1 :                 rc_file.open("as2js/as2js.rc");
     532           1 :                 CPPUNIT_ASSERT(rc_file.is_open());
     533           1 :                 rc_file << "// rc file\n"
     534           1 :                         << "{\n"
     535           1 :                         << "  'db': 'that/db'\n"
     536           1 :                         << "}\n";
     537             :             }
     538             : 
     539           1 :             rc.init_rc(true);
     540           1 :             unlink("as2js/as2js.rc");
     541             : 
     542           1 :             CPPUNIT_ASSERT(rc.get_scripts() == "as2js/scripts");
     543           1 :             CPPUNIT_ASSERT(rc.get_db() == "that/db");
     544             :         }
     545             : 
     546             :         {
     547             :             // create an .rc file, without scripts
     548             :             {
     549           1 :                 std::ofstream rc_file;
     550           1 :                 rc_file.open("as2js/as2js.rc");
     551           1 :                 CPPUNIT_ASSERT(rc_file.is_open());
     552           1 :                 rc_file << "// rc file\n"
     553           1 :                         << "{\n"
     554           1 :                         << "  'scripts': 'the/script'\n"
     555           1 :                         << "}\n";
     556             :             }
     557             : 
     558           1 :             rc.init_rc(true);
     559           1 :             unlink("as2js/as2js.rc");
     560             : 
     561           1 :             CPPUNIT_ASSERT(rc.get_scripts() == "the/script");
     562           1 :             CPPUNIT_ASSERT(rc.get_db() == "/tmp/as2js_packages.db");
     563             :         }
     564             : 
     565             :         {
     566             :             // create an .rc file, without scripts
     567             :             {
     568           1 :                 std::ofstream rc_file;
     569           1 :                 rc_file.open("as2js/as2js.rc");
     570           1 :                 CPPUNIT_ASSERT(rc_file.is_open());
     571           1 :                 rc_file << "// rc file\n"
     572           1 :                         << "{\n"
     573           1 :                         << "  'scripts': 123\n"
     574           1 :                         << "}\n";
     575             :             }
     576             : 
     577           1 :             test_callback::expected_t expected2;
     578           1 :             expected2.f_message_level = as2js::message_level_t::MESSAGE_LEVEL_FATAL;
     579           1 :             expected2.f_error_code = as2js::err_code_t::AS_ERR_UNEXPECTED_RC;
     580           1 :             expected2.f_pos.set_filename("as2js/as2js.rc");
     581           1 :             expected2.f_pos.set_function("unknown-func");
     582           1 :             expected2.f_pos.new_line();
     583           1 :             expected2.f_pos.new_line();
     584           1 :             expected2.f_message = "A resource file is expected to be an object of string elements.";
     585           1 :             tc.f_expected.push_back(expected2);
     586             : 
     587           1 :             CPPUNIT_ASSERT_THROW(rc.init_rc(true), as2js::exception_exit);
     588           1 :             tc.got_called();
     589           1 :             unlink("as2js/as2js.rc");
     590             : 
     591           1 :             CPPUNIT_ASSERT(rc.get_scripts() == "as2js/scripts");
     592           1 :             CPPUNIT_ASSERT(rc.get_db() == "/tmp/as2js_packages.db");
     593             :         }
     594             : 
     595             :         {
     596             :             // create a null .rc file
     597             :             {
     598           1 :                 std::ofstream rc_file;
     599           1 :                 rc_file.open("as2js/as2js.rc");
     600           1 :                 CPPUNIT_ASSERT(rc_file.is_open());
     601           1 :                 rc_file << "// rc file\n"
     602           1 :                         << "null\n";
     603             :             }
     604             : 
     605           1 :             rc.init_rc(false);
     606           1 :             unlink("as2js/as2js.rc");
     607             : 
     608           1 :             CPPUNIT_ASSERT(rc.get_scripts() == "as2js/scripts");
     609           1 :             CPPUNIT_ASSERT(rc.get_db() == "/tmp/as2js_packages.db");
     610             :         }
     611             : 
     612             :         {
     613             :             // create an .rc file, without an object nor null
     614             :             {
     615           1 :                 std::ofstream rc_file;
     616           1 :                 rc_file.open("as2js/as2js.rc");
     617           1 :                 CPPUNIT_ASSERT(rc_file.is_open());
     618           1 :                 rc_file << "// rc file\n"
     619           1 :                         << "['scripts', 123]\n";
     620             :             }
     621             : 
     622           1 :             test_callback::expected_t expected2;
     623           1 :             expected2.f_message_level = as2js::message_level_t::MESSAGE_LEVEL_FATAL;
     624           1 :             expected2.f_error_code = as2js::err_code_t::AS_ERR_UNEXPECTED_RC;
     625           1 :             expected2.f_pos.set_filename("as2js/as2js.rc");
     626           1 :             expected2.f_pos.set_function("unknown-func");
     627           1 :             expected2.f_pos.new_line();
     628           1 :             expected2.f_message = "A resource file (.rc) must be defined as a JSON object, or set to 'null'.";
     629           1 :             tc.f_expected.push_back(expected2);
     630             : 
     631           1 :             CPPUNIT_ASSERT_THROW(rc.init_rc(true), as2js::exception_exit);
     632           1 :             tc.got_called();
     633           1 :             unlink("as2js/as2js.rc");
     634             : 
     635           1 :             CPPUNIT_ASSERT(rc.get_scripts() == "as2js/scripts");
     636           1 :             CPPUNIT_ASSERT(rc.get_db() == "/tmp/as2js_packages.db");
     637           1 :         }
     638             :     }
     639             : 
     640             :     // delete our temporary .rc file (should already have been deleted)
     641           1 :     unlink("as2js/as2js.rc");
     642             : 
     643             :     // if possible get rid of the directory (don't check for errors)
     644           1 :     rmdir("as2js");
     645             : }
     646             : 
     647             : 
     648           1 : void As2JsRCUnitTests::test_load_from_user_config()
     649             : {
     650             :     // this test is not going to work if the get_home() function was
     651             :     // already called with an empty HOME variable...
     652           1 :     if(g_empty_home_too_late == 2)
     653             :     {
     654           0 :         std::cout << " --- test_empty_home() not run, the other rc unit tests are not compatible with this test --- ";
     655           1 :         return;
     656             :     }
     657             : 
     658           1 :     g_empty_home_too_late = 1;
     659             : 
     660           1 :     as2js::String home;
     661           1 :     home.from_utf8(getenv("HOME"));
     662             : 
     663             :     // create the folders and make sure we clean up any existing .rc file
     664             :     // (although it was checked in the setUp() function and thus we should
     665             :     // not reach here if the .rc already existed!)
     666           2 :     as2js::String config(home);
     667           1 :     config += "/.config";
     668           1 :     mkdir(config.to_utf8().c_str(), 0700); // usually this is 0755, but for security we cannot take that risk...
     669           2 :     as2js::String as2js_conf(config);
     670           1 :     as2js_conf += "/as2js";
     671           1 :     mkdir(as2js_conf.to_utf8().c_str(), 0700);
     672           2 :     as2js::String as2js_rc(as2js_conf);
     673           1 :     as2js_rc += "/as2js.rc";
     674           1 :     unlink(as2js_rc.to_utf8().c_str()); // delete that, just in case (the setup verifies that it does not exist)
     675             : 
     676             :     {
     677           1 :         test_callback tc;
     678             : 
     679           2 :         test_callback::expected_t expected1;
     680           1 :         expected1.f_message_level = as2js::message_level_t::MESSAGE_LEVEL_FATAL;
     681           1 :         expected1.f_error_code = as2js::err_code_t::AS_ERR_INSTALLATION;
     682           1 :         expected1.f_pos.set_filename("unknown-file");
     683           1 :         expected1.f_pos.set_function("unknown-func");
     684           1 :         expected1.f_message = "cannot find the as2js.rc file; the system default is usually put in /etc/as2js/as2js.rc";
     685           1 :         tc.f_expected.push_back(expected1);
     686             : 
     687           2 :         as2js::rc_t rc;
     688           1 :         CPPUNIT_ASSERT_THROW(rc.init_rc(false), as2js::exception_exit);
     689           1 :         tc.got_called();
     690             : 
     691             :         {
     692             :             // create an .rc file
     693             :             {
     694           1 :                 std::ofstream rc_file;
     695           1 :                 rc_file.open(as2js_rc.to_utf8().c_str());
     696           1 :                 CPPUNIT_ASSERT(rc_file.is_open());
     697           1 :                 rc_file << "// rc file\n"
     698           1 :                         << "{\n"
     699           1 :                         << "  'scripts': 'the/script',\n"
     700           1 :                         << "  'db': 'that/db'\n"
     701           1 :                         << "}\n";
     702             :             }
     703             : 
     704           1 :             rc.init_rc(true);
     705           1 :             unlink(as2js_rc.to_utf8().c_str());
     706             : 
     707           1 :             CPPUNIT_ASSERT(rc.get_scripts() == "the/script");
     708           1 :             CPPUNIT_ASSERT(rc.get_db() == "that/db");
     709             :         }
     710             : 
     711             :         {
     712             :             // create an .rc file, without scripts
     713             :             {
     714           1 :                 std::ofstream rc_file;
     715           1 :                 rc_file.open(as2js_rc.to_utf8().c_str());
     716           1 :                 CPPUNIT_ASSERT(rc_file.is_open());
     717           1 :                 rc_file << "// rc file\n"
     718           1 :                         << "{\n"
     719           1 :                         << "  'db': 'that/db'\n"
     720           1 :                         << "}\n";
     721             :             }
     722             : 
     723           1 :             rc.init_rc(true);
     724           1 :             unlink(as2js_rc.to_utf8().c_str());
     725             : 
     726           1 :             CPPUNIT_ASSERT(rc.get_scripts() == "as2js/scripts");
     727           1 :             CPPUNIT_ASSERT(rc.get_db() == "that/db");
     728             :         }
     729             : 
     730             :         {
     731             :             // create an .rc file, without scripts
     732             :             {
     733           1 :                 std::ofstream rc_file;
     734           1 :                 rc_file.open(as2js_rc.to_utf8().c_str());
     735           1 :                 CPPUNIT_ASSERT(rc_file.is_open());
     736           1 :                 rc_file << "// rc file\n"
     737           1 :                         << "{\n"
     738           1 :                         << "  'scripts': 'the/script'\n"
     739           1 :                         << "}\n";
     740             :             }
     741             : 
     742           1 :             rc.init_rc(true);
     743           1 :             unlink(as2js_rc.to_utf8().c_str());
     744             : 
     745           1 :             CPPUNIT_ASSERT(rc.get_scripts() == "the/script");
     746           1 :             CPPUNIT_ASSERT(rc.get_db() == "/tmp/as2js_packages.db");
     747             :         }
     748             : 
     749             :         {
     750             :             // create an .rc file, without scripts
     751             :             {
     752           1 :                 std::ofstream rc_file;
     753           1 :                 rc_file.open(as2js_rc.to_utf8().c_str());
     754           1 :                 CPPUNIT_ASSERT(rc_file.is_open());
     755           1 :                 rc_file << "// rc file\n"
     756           1 :                         << "{\n"
     757           1 :                         << "  'scripts': 123\n"
     758           1 :                         << "}\n";
     759             :             }
     760             : 
     761           1 :             test_callback::expected_t expected2;
     762           1 :             expected2.f_message_level = as2js::message_level_t::MESSAGE_LEVEL_FATAL;
     763           1 :             expected2.f_error_code = as2js::err_code_t::AS_ERR_UNEXPECTED_RC;
     764           1 :             expected2.f_pos.set_filename(as2js_rc.to_utf8().c_str());
     765           1 :             expected2.f_pos.set_function("unknown-func");
     766           1 :             expected2.f_pos.new_line();
     767           1 :             expected2.f_pos.new_line();
     768           1 :             expected2.f_message = "A resource file is expected to be an object of string elements.";
     769           1 :             tc.f_expected.push_back(expected2);
     770             : 
     771           1 :             CPPUNIT_ASSERT_THROW(rc.init_rc(true), as2js::exception_exit);
     772           1 :             tc.got_called();
     773           1 :             unlink(as2js_rc.to_utf8().c_str());
     774             : 
     775           1 :             CPPUNIT_ASSERT(rc.get_scripts() == "as2js/scripts");
     776           1 :             CPPUNIT_ASSERT(rc.get_db() == "/tmp/as2js_packages.db");
     777             :         }
     778             : 
     779             :         {
     780             :             // create a null .rc file
     781             :             {
     782           1 :                 std::ofstream rc_file;
     783           1 :                 rc_file.open(as2js_rc.to_utf8().c_str());
     784           1 :                 CPPUNIT_ASSERT(rc_file.is_open());
     785           1 :                 rc_file << "// rc file\n"
     786           1 :                         << "null\n";
     787             :             }
     788             : 
     789           1 :             rc.init_rc(false);
     790           1 :             unlink(as2js_rc.to_utf8().c_str());
     791             : 
     792           1 :             CPPUNIT_ASSERT(rc.get_scripts() == "as2js/scripts");
     793           1 :             CPPUNIT_ASSERT(rc.get_db() == "/tmp/as2js_packages.db");
     794             :         }
     795             : 
     796             :         {
     797             :             // create an .rc file, without an object nor null
     798             :             {
     799           1 :                 std::ofstream rc_file;
     800           1 :                 rc_file.open(as2js_rc.to_utf8().c_str());
     801           1 :                 CPPUNIT_ASSERT(rc_file.is_open());
     802           1 :                 rc_file << "// rc file\n"
     803           1 :                         << "['scripts', 123]\n";
     804             :             }
     805             : 
     806           1 :             test_callback::expected_t expected2;
     807           1 :             expected2.f_message_level = as2js::message_level_t::MESSAGE_LEVEL_FATAL;
     808           1 :             expected2.f_error_code = as2js::err_code_t::AS_ERR_UNEXPECTED_RC;
     809           1 :             expected2.f_pos.set_filename(as2js_rc.to_utf8().c_str());
     810           1 :             expected2.f_pos.set_function("unknown-func");
     811           1 :             expected2.f_pos.new_line();
     812           1 :             expected2.f_message = "A resource file (.rc) must be defined as a JSON object, or set to 'null'.";
     813           1 :             tc.f_expected.push_back(expected2);
     814             : 
     815           1 :             CPPUNIT_ASSERT_THROW(rc.init_rc(true), as2js::exception_exit);
     816           1 :             tc.got_called();
     817           1 :             unlink(as2js_rc.to_utf8().c_str());
     818             : 
     819           1 :             CPPUNIT_ASSERT(rc.get_scripts() == "as2js/scripts");
     820           1 :             CPPUNIT_ASSERT(rc.get_db() == "/tmp/as2js_packages.db");
     821           1 :         }
     822             :     }
     823             : 
     824             :     // delete our temporary .rc file (should already have been deleted)
     825           1 :     unlink(as2js_rc.to_utf8().c_str());
     826             : 
     827             :     // if possible get rid of the directories (don't check for errors)
     828           1 :     rmdir(as2js_conf.to_utf8().c_str());
     829           2 :     rmdir(config.to_utf8().c_str());
     830             : }
     831             : 
     832             : 
     833             : //
     834             : // WARNING: this test requires root permissions, it can generally be
     835             : //          ignored though because it uses the same process as the
     836             : //          user local file in "as2js/as2js.rc"; it is here for
     837             : //          completeness in case you absolutely want to prove that
     838             : //          works as expected
     839             : //
     840           1 : void As2JsRCUnitTests::test_load_from_system_config()
     841             : {
     842           1 :     if(getuid() != 0)
     843             :     {
     844           1 :         std::cout << " --- test_load_from_system_config() requires root access to modify the /etc/as2js directory --- ";
     845           2 :         return;
     846             :     }
     847             : 
     848             :     // this test is not going to work if the get_home() function was
     849             :     // already called with an empty HOME variable...
     850           0 :     if(g_empty_home_too_late == 2)
     851             :     {
     852           0 :         std::cout << " --- test_empty_home() not run, the other rc unit tests are not compatible with this test --- ";
     853           0 :         return;
     854             :     }
     855             : 
     856           0 :     g_empty_home_too_late = 1;
     857             : 
     858             :     // create the folders and make sure we clean up any existing .rc file
     859             :     // (although it was checked in the setUp() function and thus we should
     860             :     // not reach here if the .rc already existed!)
     861           0 :     as2js::String as2js_conf("/etc/as2js");
     862           0 :     mkdir(as2js_conf.to_utf8().c_str(), 0700); // usually this is 0755, but for security we cannot take that risk...
     863           0 :     as2js::String as2js_rc(as2js_conf);
     864           0 :     as2js_rc += "/as2js.rc";
     865           0 :     unlink(as2js_rc.to_utf8().c_str()); // delete that, just in case (the setup verifies that it does not exist)
     866             : 
     867             :     {
     868           0 :         test_callback tc;
     869             : 
     870           0 :         test_callback::expected_t expected1;
     871           0 :         expected1.f_message_level = as2js::message_level_t::MESSAGE_LEVEL_FATAL;
     872           0 :         expected1.f_error_code = as2js::err_code_t::AS_ERR_INSTALLATION;
     873           0 :         expected1.f_pos.set_filename("unknown-file");
     874           0 :         expected1.f_pos.set_function("unknown-func");
     875           0 :         expected1.f_message = "cannot find the as2js.rc file; the system default is usually put in /etc/as2js/as2js.rc";
     876           0 :         tc.f_expected.push_back(expected1);
     877             : 
     878           0 :         as2js::rc_t rc;
     879           0 :         CPPUNIT_ASSERT_THROW(rc.init_rc(false), as2js::exception_exit);
     880           0 :         tc.got_called();
     881             : 
     882             :         {
     883             :             // create an .rc file
     884             :             {
     885           0 :                 std::ofstream rc_file;
     886           0 :                 rc_file.open(as2js_rc.to_utf8().c_str());
     887           0 :                 CPPUNIT_ASSERT(rc_file.is_open());
     888           0 :                 rc_file << "// rc file\n"
     889           0 :                         << "{\n"
     890           0 :                         << "  'scripts': 'the/script',\n"
     891           0 :                         << "  'db': 'that/db'\n"
     892           0 :                         << "}\n";
     893             :             }
     894             : 
     895           0 :             rc.init_rc(true);
     896           0 :             unlink(as2js_rc.to_utf8().c_str());
     897             : 
     898           0 :             CPPUNIT_ASSERT(rc.get_scripts() == "the/script");
     899           0 :             CPPUNIT_ASSERT(rc.get_db() == "that/db");
     900             :         }
     901             : 
     902             :         {
     903             :             // create an .rc file, without scripts
     904             :             {
     905           0 :                 std::ofstream rc_file;
     906           0 :                 rc_file.open(as2js_rc.to_utf8().c_str());
     907           0 :                 CPPUNIT_ASSERT(rc_file.is_open());
     908           0 :                 rc_file << "// rc file\n"
     909           0 :                         << "{\n"
     910           0 :                         << "  'db': 'that/db'\n"
     911           0 :                         << "}\n";
     912             :             }
     913             : 
     914           0 :             rc.init_rc(true);
     915           0 :             unlink(as2js_rc.to_utf8().c_str());
     916             : 
     917           0 :             CPPUNIT_ASSERT(rc.get_scripts() == "as2js/scripts");
     918           0 :             CPPUNIT_ASSERT(rc.get_db() == "that/db");
     919             :         }
     920             : 
     921             :         {
     922             :             // create an .rc file, without scripts
     923             :             {
     924           0 :                 std::ofstream rc_file;
     925           0 :                 rc_file.open(as2js_rc.to_utf8().c_str());
     926           0 :                 CPPUNIT_ASSERT(rc_file.is_open());
     927           0 :                 rc_file << "// rc file\n"
     928           0 :                         << "{\n"
     929           0 :                         << "  'scripts': 'the/script'\n"
     930           0 :                         << "}\n";
     931             :             }
     932             : 
     933           0 :             rc.init_rc(true);
     934           0 :             unlink(as2js_rc.to_utf8().c_str());
     935             : 
     936           0 :             CPPUNIT_ASSERT(rc.get_scripts() == "the/script");
     937           0 :             CPPUNIT_ASSERT(rc.get_db() == "/tmp/as2js_packages.db");
     938             :         }
     939             : 
     940             :         {
     941             :             // create an .rc file, without scripts
     942             :             {
     943           0 :                 std::ofstream rc_file;
     944           0 :                 rc_file.open(as2js_rc.to_utf8().c_str());
     945           0 :                 CPPUNIT_ASSERT(rc_file.is_open());
     946           0 :                 rc_file << "// rc file\n"
     947           0 :                         << "{\n"
     948           0 :                         << "  'scripts': 123\n"
     949           0 :                         << "}\n";
     950             :             }
     951             : 
     952           0 :             test_callback::expected_t expected2;
     953           0 :             expected2.f_message_level = as2js::message_level_t::MESSAGE_LEVEL_FATAL;
     954           0 :             expected2.f_error_code = as2js::err_code_t::AS_ERR_UNEXPECTED_RC;
     955           0 :             expected2.f_pos.set_filename(as2js_rc.to_utf8().c_str());
     956           0 :             expected2.f_pos.set_function("unknown-func");
     957           0 :             expected2.f_pos.new_line();
     958           0 :             expected2.f_pos.new_line();
     959           0 :             expected2.f_message = "A resource file is expected to be an object of string elements.";
     960           0 :             tc.f_expected.push_back(expected2);
     961             : 
     962           0 :             CPPUNIT_ASSERT_THROW(rc.init_rc(true), as2js::exception_exit);
     963           0 :             tc.got_called();
     964           0 :             unlink(as2js_rc.to_utf8().c_str());
     965             : 
     966           0 :             CPPUNIT_ASSERT(rc.get_scripts() == "as2js/scripts");
     967           0 :             CPPUNIT_ASSERT(rc.get_db() == "/tmp/as2js_packages.db");
     968             :         }
     969             : 
     970             :         {
     971             :             // create a null .rc file
     972             :             {
     973           0 :                 std::ofstream rc_file;
     974           0 :                 rc_file.open(as2js_rc.to_utf8().c_str());
     975           0 :                 CPPUNIT_ASSERT(rc_file.is_open());
     976           0 :                 rc_file << "// rc file\n"
     977           0 :                         << "null\n";
     978             :             }
     979             : 
     980           0 :             rc.init_rc(false);
     981           0 :             unlink(as2js_rc.to_utf8().c_str());
     982             : 
     983           0 :             CPPUNIT_ASSERT(rc.get_scripts() == "as2js/scripts");
     984           0 :             CPPUNIT_ASSERT(rc.get_db() == "/tmp/as2js_packages.db");
     985             :         }
     986             : 
     987             :         {
     988             :             // create an .rc file, without an object nor null
     989             :             {
     990           0 :                 std::ofstream rc_file;
     991           0 :                 rc_file.open(as2js_rc.to_utf8().c_str());
     992           0 :                 CPPUNIT_ASSERT(rc_file.is_open());
     993           0 :                 rc_file << "// rc file\n"
     994           0 :                         << "['scripts', 123]\n";
     995             :             }
     996             : 
     997           0 :             test_callback::expected_t expected2;
     998           0 :             expected2.f_message_level = as2js::message_level_t::MESSAGE_LEVEL_FATAL;
     999           0 :             expected2.f_error_code = as2js::err_code_t::AS_ERR_UNEXPECTED_RC;
    1000           0 :             expected2.f_pos.set_filename(as2js_rc.to_utf8().c_str());
    1001           0 :             expected2.f_pos.set_function("unknown-func");
    1002           0 :             expected2.f_pos.new_line();
    1003           0 :             expected2.f_message = "A resource file (.rc) must be defined as a JSON object, or set to 'null'.";
    1004           0 :             tc.f_expected.push_back(expected2);
    1005             : 
    1006           0 :             CPPUNIT_ASSERT_THROW(rc.init_rc(true), as2js::exception_exit);
    1007           0 :             tc.got_called();
    1008           0 :             unlink(as2js_rc.to_utf8().c_str());
    1009             : 
    1010           0 :             CPPUNIT_ASSERT(rc.get_scripts() == "as2js/scripts");
    1011           0 :             CPPUNIT_ASSERT(rc.get_db() == "/tmp/as2js_packages.db");
    1012           0 :         }
    1013             :     }
    1014             : 
    1015             :     // delete our temporary .rc file (should already have been deleted)
    1016           0 :     unlink(as2js_rc.to_utf8().c_str());
    1017             : 
    1018             :     // if possible get rid of the directories (don't check for errors)
    1019           0 :     rmdir(as2js_conf.to_utf8().c_str());
    1020             : }
    1021             : 
    1022             : 
    1023           2 : void As2JsRCUnitTests::test_empty_home()
    1024             : {
    1025             :     // this test is not going to work if the get_home() function was
    1026             :     // already called...
    1027           2 :     if(g_empty_home_too_late == 1)
    1028             :     {
    1029           1 :         std::cout << " --- test_empty_home() not run, the other rc unit tests are not compatible with this test --- ";
    1030           3 :         return;
    1031             :     }
    1032             : 
    1033           1 :     g_empty_home_too_late = 2;
    1034             : 
    1035             :     // create an .rc file in the user's config directory
    1036           1 :     as2js::String home;
    1037           1 :     home.from_utf8(getenv("HOME"));
    1038             : 
    1039           2 :     as2js::String config(home);
    1040           1 :     config += "/.config";
    1041           1 :     mkdir(config.to_utf8().c_str(), 0700);
    1042             : 
    1043           2 :     as2js::String rc_path(config);
    1044           1 :     rc_path += "/as2js";
    1045           1 :     mkdir(rc_path.to_utf8().c_str(), 0700);
    1046             : 
    1047           2 :     as2js::String rc_filename(rc_path);
    1048           1 :     rc_filename += "/as2js.rc";
    1049             : 
    1050           2 :     std::ofstream rc_file;
    1051           1 :     rc_file.open(rc_filename.to_utf8().c_str());
    1052           1 :     CPPUNIT_ASSERT(rc_file.is_open());
    1053           1 :     rc_file << "// rc file\n"
    1054           1 :             << "{\n"
    1055           1 :             << "  'scripts': 'cannot read this one',\n"
    1056           1 :             << "  'db': 'because it is not accessible'\n"
    1057           1 :             << "}\n";
    1058             : 
    1059             :     // remove the variable from the environment
    1060             :     char buf[256];
    1061           1 :     strcpy(buf, "HOME");
    1062           1 :     putenv(buf);
    1063             : 
    1064             :     {
    1065           1 :         test_callback tc;
    1066             : 
    1067             :         // although we have an rc file under ~/.config/as2js/as2js.rc the
    1068             :         // rc class cannot find it because the $HOME variable was just deleted
    1069           2 :         as2js::rc_t rc;
    1070           1 :         rc.init_rc(true);
    1071             : 
    1072           1 :         CPPUNIT_ASSERT(rc.get_scripts() == "as2js/scripts");
    1073           2 :         CPPUNIT_ASSERT(rc.get_db() == "/tmp/as2js_packages.db");
    1074             :     }
    1075             : 
    1076           1 :     unlink(rc_filename.to_utf8().c_str());
    1077           2 :     rmdir(rc_path.to_utf8().c_str());
    1078          12 : }
    1079             : 
    1080             : 
    1081             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.10