LCOV - code coverage report
Current view: top level - tests - test-unixctl.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 53 77 68.8 %
Date: 2016-09-14 01:02:56 Functions: 7 11 63.6 %
Branches: 12 30 40.0 %

           Branch data     Line data    Source code
       1                 :            : /* Copyright (c) 2015 Nicira, Inc.
       2                 :            :  *
       3                 :            :  * Licensed under the Apache License, Version 2.0 (the "License");
       4                 :            :  * you may not use this file except in compliance with the License.
       5                 :            :  * You may obtain a copy of the License at:
       6                 :            :  *
       7                 :            :  *     http://www.apache.org/licenses/LICENSE-2.0
       8                 :            :  *
       9                 :            :  * Unless required by applicable law or agreed to in writing, software
      10                 :            :  * distributed under the License is distributed on an "AS IS" BASIS,
      11                 :            :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12                 :            :  * See the License for the specific language governing permissions and
      13                 :            :  * limitations under the License.
      14                 :            :  */
      15                 :            : 
      16                 :            : #include <config.h>
      17                 :            : 
      18                 :            : #include <getopt.h>
      19                 :            : 
      20                 :            : #include "command-line.h"
      21                 :            : #include "daemon.h"
      22                 :            : #include "fatal-signal.h"
      23                 :            : #include "openvswitch/vlog.h"
      24                 :            : #include "ovstest.h"
      25                 :            : #include "poll-loop.h"
      26                 :            : #include "unixctl.h"
      27                 :            : #include "util.h"
      28                 :            : 
      29                 :       1174 : VLOG_DEFINE_THIS_MODULE(test_unixctl);
      30                 :            : 
      31                 :            : static void parse_options(int *argc, char **argvp[], char **unixctl_pathp);
      32                 :            : OVS_NO_RETURN static void usage(void);
      33                 :            : 
      34                 :            : static void
      35                 :          5 : test_unixctl_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
      36                 :            :                   const char *argv[] OVS_UNUSED, void *exiting_)
      37                 :            : {
      38                 :          5 :     bool *exiting = exiting_;
      39                 :          5 :     *exiting = true;
      40                 :          5 :     unixctl_command_reply(conn, NULL);
      41                 :          5 : }
      42                 :            : 
      43                 :            : static void
      44                 :          0 : test_unixctl_echo(struct unixctl_conn *conn, int argc OVS_UNUSED,
      45                 :            :                   const char *argv[], void *aux OVS_UNUSED)
      46                 :            : {
      47                 :          0 :     unixctl_command_reply(conn, argv[1]);
      48                 :          0 : }
      49                 :            : 
      50                 :            : static void
      51                 :          0 : test_unixctl_echo_error(struct unixctl_conn *conn, int argc OVS_UNUSED,
      52                 :            :                         const char *argv[], void *aux OVS_UNUSED)
      53                 :            : {
      54                 :          0 :     unixctl_command_reply_error(conn, argv[1]);
      55                 :          0 : }
      56                 :            : 
      57                 :            : static void
      58                 :         12 : test_unixctl_log(struct unixctl_conn *conn, int argc OVS_UNUSED,
      59                 :            :                   const char *argv[], void *aux OVS_UNUSED)
      60                 :            : {
      61         [ +  - ]:         12 :     VLOG_INFO("%s", argv[1]);
      62                 :         12 :     unixctl_command_reply(conn, NULL);
      63                 :         12 : }
      64                 :            : 
      65                 :            : static void
      66                 :          0 : test_unixctl_block(struct unixctl_conn *conn OVS_UNUSED, int argc OVS_UNUSED,
      67                 :            :                    const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
      68                 :            : {
      69         [ #  # ]:          0 :     VLOG_INFO("%s", argv[1]);
      70                 :          0 :     unixctl_command_reply(conn, NULL);
      71                 :          0 : }
      72                 :            : 
      73                 :            : static int
      74                 :          5 : test_unixctl_main(int argc, char *argv[])
      75                 :            : {
      76                 :          5 :     char *unixctl_path = NULL;
      77                 :            :     struct unixctl_server *unixctl;
      78                 :          5 :     bool exiting = false;
      79                 :            : 
      80                 :          5 :     ovs_cmdl_proctitle_init(argc, argv);
      81                 :          5 :     set_program_name(argv[0]);
      82                 :          5 :     service_start(&argc, &argv);
      83                 :          5 :     fatal_ignore_sigpipe();
      84                 :          5 :     parse_options(&argc, &argv, &unixctl_path);
      85                 :            : 
      86                 :          5 :     daemonize_start(false);
      87                 :          5 :     int retval = unixctl_server_create(unixctl_path, &unixctl);
      88         [ -  + ]:          5 :     if (retval) {
      89                 :          0 :         exit(EXIT_FAILURE);
      90                 :            :     }
      91                 :          5 :     unixctl_command_register("exit", "", 0, 0, test_unixctl_exit, &exiting);
      92                 :          5 :     unixctl_command_register("echo", "ARG", 1, 1, test_unixctl_echo, NULL);
      93                 :          5 :     unixctl_command_register("echo_error", "ARG", 1, 1,
      94                 :            :                              test_unixctl_echo_error, NULL);
      95                 :          5 :     unixctl_command_register("log", "ARG", 1, 1, test_unixctl_log, NULL);
      96                 :          5 :     unixctl_command_register("block", "", 0, 0, test_unixctl_block, NULL);
      97                 :          5 :     daemonize_complete();
      98                 :            : 
      99         [ +  - ]:          5 :     VLOG_INFO("Entering run loop.");
     100         [ +  + ]:         89 :     while (!exiting) {
     101                 :         84 :         unixctl_server_run(unixctl);
     102                 :         84 :         unixctl_server_wait(unixctl);
     103         [ +  + ]:         84 :         if (exiting) {
     104                 :          5 :             poll_immediate_wake();
     105                 :            :         }
     106                 :         84 :         poll_block();
     107                 :            :     }
     108                 :          5 :     unixctl_server_destroy(unixctl);
     109                 :            : 
     110                 :          5 :     service_stop();
     111                 :          5 :     return 0;
     112                 :            : }
     113                 :            : 
     114                 :            : static void
     115                 :          5 : parse_options(int *argcp, char **argvp[], char **unixctl_pathp)
     116                 :            : {
     117                 :            :     enum {
     118                 :            :         OPT_REMOTE = UCHAR_MAX + 1,
     119                 :            :         OPT_UNIXCTL,
     120                 :            :         VLOG_OPTION_ENUMS,
     121                 :            :         DAEMON_OPTION_ENUMS
     122                 :            :     };
     123                 :            :     static const struct option long_options[] = {
     124                 :            :         {"unixctl",     required_argument, NULL, OPT_UNIXCTL},
     125                 :            :         {"help",        no_argument, NULL, 'h'},
     126                 :            :         {"version",     no_argument, NULL, 'V'},
     127                 :            :         DAEMON_LONG_OPTIONS,
     128                 :            :         VLOG_LONG_OPTIONS,
     129                 :            :         {NULL, 0, NULL, 0},
     130                 :            :     };
     131                 :          5 :     char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
     132                 :          5 :     int argc = *argcp;
     133                 :          5 :     char **argv = *argvp;
     134                 :            : 
     135                 :            :     for (;;) {
     136                 :            :         int c;
     137                 :            : 
     138                 :         19 :         c = getopt_long(argc, argv, short_options, long_options, NULL);
     139         [ +  + ]:         19 :         if (c == -1) {
     140                 :          5 :             break;
     141                 :            :         }
     142                 :            : 
     143   [ -  -  -  -  :         14 :         switch (c) {
          +  -  -  +  -  
          -  +  -  -  -  
                   -  - ]
     144                 :            :         case OPT_UNIXCTL:
     145                 :          0 :             *unixctl_pathp = optarg;
     146                 :          0 :             break;
     147                 :            : 
     148                 :            :         case 'h':
     149                 :          0 :             usage();
     150                 :            : 
     151                 :            :         case 'V':
     152                 :          0 :             ovs_print_version(0, 0);
     153                 :          0 :             exit(EXIT_SUCCESS);
     154                 :            : 
     155                 :          4 :         VLOG_OPTION_HANDLERS
     156                 :         10 :         DAEMON_OPTION_HANDLERS
     157                 :            : 
     158                 :            :         case '?':
     159                 :          0 :             exit(EXIT_FAILURE);
     160                 :            : 
     161                 :            :         default:
     162                 :          0 :             abort();
     163                 :            :         }
     164                 :         14 :     }
     165                 :          5 :     free(short_options);
     166                 :            : 
     167                 :          5 :     *argcp -= optind;
     168                 :          5 :     *argvp += optind;
     169                 :          5 : }
     170                 :            : 
     171                 :            : static void
     172                 :          0 : usage(void)
     173                 :            : {
     174                 :          0 :     printf("%s: Open vSwitch unixctl test program\n"
     175                 :            :            "usage: %s [OPTIONS]\n",
     176                 :            :            program_name, program_name);
     177                 :          0 :     daemon_usage();
     178                 :          0 :     vlog_usage();
     179                 :          0 :     printf("\nOther options:\n"
     180                 :            :            "  --unixctl=SOCKET        override default control socket name\n"
     181                 :            :            "  -h, --help              display this help message\n"
     182                 :            :            "  -V, --version           display version information\n");
     183                 :          0 :     exit(EXIT_SUCCESS);
     184                 :            : }
     185                 :            : 
     186                 :       1184 : OVSTEST_REGISTER("test-unixctl", test_unixctl_main);

Generated by: LCOV version 1.12