LCOV - code coverage report
Current view: top level - testutils - portconfig.h (source / functions) Hit Total Coverage
Test: report.info Lines: 38 38 100.0 %
Date: 2012-05-15 Functions: 8 8 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 92 335 27.5 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (C) 2011  Internet Systems Consortium, Inc. ("ISC")
       2                 :            : //
       3                 :            : // Permission to use, copy, modify, and/or distribute this software for any
       4                 :            : // purpose with or without fee is hereby granted, provided that the above
       5                 :            : // copyright notice and this permission notice appear in all copies.
       6                 :            : //
       7                 :            : // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
       8                 :            : // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
       9                 :            : // AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
      10                 :            : // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
      11                 :            : // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
      12                 :            : // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
      13                 :            : // PERFORMANCE OF THIS SOFTWARE.
      14                 :            : 
      15                 :            : #ifndef __ISC_TESTUTILS_PORTCONFIG_H
      16                 :            : #define __ISC_TESTUTILS_PORTCONFIG_H
      17                 :            : 
      18                 :            : #include <gtest/gtest.h>
      19                 :            : #include <cc/data.h>
      20                 :            : #include <server_common/portconfig.h>
      21                 :            : 
      22                 :            : namespace isc {
      23                 :            : namespace testutils {
      24                 :            : /**
      25                 :            :  * \brief Bits of tests for server port configuration.
      26                 :            :  *
      27                 :            :  * These are bits of tests that can be reused by server classes to check if
      28                 :            :  * configuration of the listening addresses work.
      29                 :            :  *
      30                 :            :  * You can put any of these functions into a TEST_F test and pass the server
      31                 :            :  * to it.
      32                 :            :  *
      33                 :            :  * \todo There's quite a lot of common code in the basic server handling.
      34                 :            :  *     We should refactor it, so both Resolver server and Auth server have
      35                 :            :  *     a common base class. When this is done, the common parts would be put
      36                 :            :  *     there and the tests would be at the base class, not here.
      37                 :            :  */
      38                 :            : namespace portconfig {
      39                 :            : 
      40                 :            : /**
      41                 :            :  * \brief Check setting of the listening addresses directly (as a list) works.
      42                 :            :  *
      43                 :            :  * \param server The server to test against.
      44                 :            :  */
      45                 :            : template<class Server>
      46                 :            : void
      47                 :          2 : listenAddresses(Server& server) {
      48                 :            :     using namespace isc::server_common::portconfig;
      49                 :            :     // In this test we assume the address list is originally empty.
      50 [ +  + ][ #  # ]:          2 :     EXPECT_TRUE(server.getListenAddresses().empty());
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
          [ - ][ # ][ # ]
          [ # ][ # ][ # ]
      51                 :            : 
      52                 :            :     // Try putting there some addresses
      53                 :          2 :     AddressList addresses;
      54 [ +  - ][ +  - ]:          6 :     addresses.push_back(AddressPair("127.0.0.1", 53210));
      55 [ +  - ][ +  - ]:          6 :     addresses.push_back(AddressPair("::1", 53210));
      56         [ +  - ]:          2 :     server.setListenAddresses(addresses);
      57 [ +  - ][ +  + ]:          4 :     EXPECT_EQ(2, server.getListenAddresses().size());
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
            [ +  - ][ - ]
          [ # ][ # ][ # ]
               [ # ][ + ]
      58 [ +  - ][ +  + ]:          4 :     EXPECT_EQ("::1", server.getListenAddresses()[1].first);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
            [ +  - ][ - ]
          [ # ][ # ][ # ]
               [ # ][ + ]
      59                 :            : 
      60                 :            :     // Is it independent from what we do with the vector later?
      61                 :            :     addresses.clear();
      62 [ +  - ][ +  + ]:          4 :     EXPECT_EQ(2, server.getListenAddresses().size());
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
            [ +  - ][ - ]
          [ # ][ # ][ # ]
               [ # ][ + ]
      63                 :            : 
      64                 :            :     // If we set to an empty list next, the server configuration should
      65                 :            :     // become empty, too.
      66         [ +  - ]:          2 :     server.setListenAddresses(addresses);
      67 [ +  - ][ +  + ]:          2 :     EXPECT_TRUE(server.getListenAddresses().empty());
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ +  - ]
          [ - ][ # ][ # ]
          [ # ][ # ][ # ]
                    [ + ]
      68                 :          2 : }
      69                 :            : 
      70                 :            : /**
      71                 :            :  * \brief Check setting of the addresses by config value.
      72                 :            :  *
      73                 :            :  * This passes an listen_on element to the server's updateConfig function.
      74                 :            :  * It tries little bit of switching around. It tries both setting a presumably
      75                 :            :  * valid addresses and then setting something that cant be bound, rolling back
      76                 :            :  * back to original.
      77                 :            :  *
      78                 :            :  * \param server The server object to test against.
      79                 :            :  */
      80                 :            : template<class Server>
      81                 :            : void
      82                 :          2 : listenAddressConfig(Server& server) {
      83                 :            :     using namespace isc::data;
      84                 :            :     // Try putting there some address
      85                 :            :     ElementPtr config(Element::fromJSON("{"
      86                 :            :                                         "\"listen_on\": ["
      87                 :            :                                         "   {"
      88                 :            :                                         "       \"address\": \"127.0.0.1\","
      89                 :            :                                         "       \"port\": 53210"
      90                 :            :                                         "   }"
      91                 :            :                                         "]"
      92         [ +  - ]:          2 :                                         "}"));
      93         [ +  - ]:          2 :     ConstElementPtr result(server.updateConfig(config));
      94 [ +  - ][ +  - ]:          6 :     EXPECT_EQ(result->toWire(), isc::config::createAnswer()->toWire());
         [ +  - ][ -  + ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ +  - ]
      95 [ +  - ][ +  + ]:          4 :     ASSERT_EQ(1, server.getListenAddresses().size());
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ +  - ][ #  # ]
          [ - ][ # ][ # ]
          [ # ][ # ][ + ]
                    [ # ]
      96 [ +  - ][ +  + ]:          4 :     EXPECT_EQ("127.0.0.1", server.getListenAddresses()[0].first);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
            [ +  - ][ - ]
          [ # ][ # ][ # ]
               [ # ][ + ]
      97 [ +  - ][ +  + ]:          4 :     EXPECT_EQ(53210, server.getListenAddresses()[0].second);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
            [ +  - ][ - ]
          [ # ][ # ][ # ]
               [ # ][ + ]
      98                 :            : 
      99                 :            :     // This address is rejected by the test socket requestor
     100 [ +  - ][ +  - ]:          4 :     config = Element::fromJSON("{"
     101                 :            :                                "\"listen_on\": ["
     102                 :            :                                "   {"
     103                 :            :                                "       \"address\": \"192.0.2.2\","
     104                 :            :                                "       \"port\": 53210"
     105                 :            :                                "   }"
     106                 :            :                                "]"
     107                 :            :                                "}");
     108 [ +  - ][ +  - ]:          2 :     result = server.updateConfig(config);
     109 [ +  - ][ +  - ]:          6 :     EXPECT_FALSE(result->equals(*isc::config::createAnswer()));
         [ -  + ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ +  - ]
     110 [ +  - ][ +  + ]:          4 :     ASSERT_EQ(1, server.getListenAddresses().size());
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ +  - ][ #  # ]
          [ - ][ # ][ # ]
          [ # ][ # ][ + ]
                    [ # ]
     111 [ +  - ][ +  + ]:          4 :     EXPECT_EQ("127.0.0.1", server.getListenAddresses()[0].first);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
            [ +  - ][ - ]
          [ # ][ # ][ # ]
               [ # ][ + ]
     112 [ +  - ][ +  + ]:          4 :     EXPECT_EQ(53210, server.getListenAddresses()[0].second);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
            [ +  - ][ - ]
          [ # ][ # ][ # ]
               [ # ][ + ]
     113                 :            : 
     114                 :            : }
     115                 :            : 
     116                 :            : /**
     117                 :            :  * \brief Check that given config is rejected.
     118                 :            :  *
     119                 :            :  * Try if given config is considered invalid by the server and is rejected.
     120                 :            :  * The value is converted from JSON to the data elements and passed to server's
     121                 :            :  * updateConfig method. It should not crash, but return a negative answer.
     122                 :            :  *
     123                 :            :  * It is used internally by invalidListenAddressConfig, but you can use it
     124                 :            :  * to test any other invalid configs.
     125                 :            :  *
     126                 :            :  * \todo It might be better to put it to some other namespace, as this is more
     127                 :            :  *     generic. But right now it is used only here, so until something else
     128                 :            :  *     needs it, it might as well stay here.
     129                 :            :  * \param server The server to test against.
     130                 :            :  * \param JSON Config to use.
     131                 :            :  * \param name It is used in the output if the test fails.
     132                 :            :  */
     133                 :            : template<class Server>
     134                 :            : void
     135                 :         25 : configRejected(Server& server, const std::string& JSON,
     136                 :            :                const std::string& name)
     137                 :            : {
     138         [ +  - ]:         75 :     SCOPED_TRACE(name);
     139                 :            : 
     140                 :            :     using namespace isc::data;
     141         [ +  - ]:         25 :     ElementPtr config(Element::fromJSON(JSON));
     142 [ +  - ][ +  - ]:         75 :     EXPECT_FALSE(server.updateConfig(config)->
         [ +  - ][ -  + ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ +  - ]
     143                 :            :                  equals(*isc::config::createAnswer())) <<
     144                 :            :         "Accepted invalid config " << JSON;
     145                 :         25 : }
     146                 :            : 
     147                 :            : /**
     148                 :            :  * \brief Check some invalid address configs.
     149                 :            :  *
     150                 :            :  * It tries a series of invalid listen_on configs against the server and checks
     151                 :            :  * it is rejected.
     152                 :            :  * \param server The server to check against.
     153                 :            :  */
     154                 :            : template<class Server>
     155                 :            : void
     156                 :          2 : invalidListenAddressConfig(Server& server) {
     157 [ +  - ][ +  - ]:          2 :     configRejected(server, "{"
     158                 :            :                    "\"listen_on\": \"error\""
     159                 :            :                    "}", "Wrong element type");
     160 [ +  - ][ +  - ]:          2 :     configRejected(server, "{"
     161                 :            :                    "\"listen_on\": [{}]"
     162                 :            :                    "}", "Empty address element");
     163 [ +  - ][ +  - ]:          2 :     configRejected(server, "{"
     164                 :            :                    "\"listen_on\": [{"
     165                 :            :                    "   \"port\": 1.5,"
     166                 :            :                    "   \"address\": \"192.0.2.1\""
     167                 :            :                    "}]}", "Float port");
     168 [ +  - ][ +  - ]:          2 :     configRejected(server, "{"
     169                 :            :                    "\"listen_on\": [{"
     170                 :            :                    "   \"port\": -5,"
     171                 :            :                    "   \"address\": \"192.0.2.1\""
     172                 :            :                    "}]}", "Negative port");
     173 [ +  - ][ +  - ]:          2 :     configRejected(server, "{"
     174                 :            :                    "\"listen_on\": [{"
     175                 :            :                    "   \"port\": 1000000,"
     176                 :            :                    "   \"address\": \"192.0.2.1\""
     177                 :            :                    "}]}", "Huge port");
     178 [ +  - ][ +  - ]:          2 :     configRejected(server, "{"
     179                 :            :                    "\"listen_on\": [{"
     180                 :            :                    "   \"port\": 53,"
     181                 :            :                    "   \"address\": \"bad_address\""
     182                 :            :                    "}]}", "Bad address");
     183                 :          2 : }
     184                 :            : 
     185                 :            : }
     186                 :            : }
     187                 :            : }
     188                 :            : 
     189                 :            : #endif  // __ISC_TESTUTILS_PORTCONFIG_H

Generated by: LCOV version 1.9