Imported Upstream version 17.22.1
[platform/upstream/libzypp.git] / tests / zypp / base / Sysconfig_test.cc
1
2 #include <iostream>
3 #include <sstream>
4 #include <fstream>
5 #include <map>
6 #include <string>
7
8 #include <boost/test/unit_test.hpp>
9
10 #include "zypp/base/Logger.h"
11 #include "zypp/base/Exception.h"
12 #include "zypp/TmpPath.h"
13 #include "zypp/PathInfo.h"
14 #include "zypp/ExternalProgram.h"
15
16 #include "zypp/base/Sysconfig.h"
17
18 using boost::unit_test::test_suite;
19 using boost::unit_test::test_case;
20 using namespace boost::unit_test;
21
22 using namespace std;
23 using namespace zypp;
24
25 #define DATADIR (Pathname(TESTS_SRC_DIR) + "/zypp/base/data/Sysconfig")
26
27 BOOST_AUTO_TEST_CASE(Sysconfig)
28 {
29   Pathname file = DATADIR / "proxy";
30   map<string,string> values = zypp::base::sysconfig::read(file);
31   BOOST_CHECK_EQUAL( values.size(), 6 );
32   BOOST_CHECK_EQUAL( values["PROXY_ENABLED"], "no");
33   BOOST_CHECK_EQUAL( values["GOPHER_PROXY"], "");
34   BOOST_CHECK_EQUAL( values["NO_PROXY"], "localhost, 127.0.0.1");
35 }
36
37 BOOST_AUTO_TEST_CASE(SysconfigWrite)
38 {
39   Pathname file = DATADIR / "proxy";
40   filesystem::TmpFile tmpf( filesystem::TmpFile::makeSibling( file ) );
41   filesystem::copy( file, tmpf.path() );
42
43   BOOST_REQUIRE_THROW( zypp::base::sysconfig::writeStringVal( "/tmp/wrzlprmpf", "PROXY_ENABLED", "yes", "# fifi\n fofo\n" ),
44                        zypp::Exception );
45   BOOST_CHECK( zypp::base::sysconfig::writeStringVal( tmpf.path(), "PROXY_ENABLED", "yes", "# fifi\n fofo\n" ) );
46   BOOST_CHECK( !zypp::base::sysconfig::writeStringVal( tmpf.path(), "NEW1","12" ) );
47   BOOST_CHECK( zypp::base::sysconfig::writeStringVal( tmpf.path(), "NEW2","13", "# fifi\n# fofo" ) );
48   BOOST_CHECK( zypp::base::sysconfig::writeStringVal( tmpf.path(), "NEW3","13\"str\"", "fifi\nffofo" ) );
49
50   std::ostringstream s;
51   ExternalProgram( "diff -u " + file.asString() + " " + tmpf.path().asString() + " | tail -n +3" ) >> s;
52   BOOST_CHECK_EQUAL( s.str(),
53                      "@@ -8,7 +8,7 @@\n"
54                      " # This setting allows to turn the proxy on and off while\n"
55                      " # preserving the particular proxy setup.\n"
56                      " #\n"
57                      "-PROXY_ENABLED=\"no\"\n"
58                      "+PROXY_ENABLED=\"yes\"\n"
59                      " \n"
60                      " ## Type:\tstring\n"
61                      " ## Default:\t\"\"\n"
62                      "@@ -49,3 +49,11 @@\n"
63                      " # Example: NO_PROXY=\"www.me.de, do.main, localhost\"\n"
64                      " #\n"
65                      " NO_PROXY=\"localhost, 127.0.0.1\"\n"
66                      "+\n"
67                      "+# fifi\n"
68                      "+# fofo\n"
69                      "+NEW2=\"13\"\n"
70                      "+\n"
71                      "+# fifi\n"
72                      "+# ffofo\n"
73                      "+NEW3=\"13\\\"str\\\"\"\n"
74   );
75 }
76