LCOV - code coverage report
Current view: top level - network - test-routing-policy-rule.c (source / functions) Hit Total Coverage
Test: systemd_full.info Lines: 42 42 100.0 %
Date: 2019-08-23 13:36:53 Functions: 2 2 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 30 52 57.7 %

           Branch data     Line data    Source code
       1                 :            : /***
       2                 :            :   SPDX-License-Identifier: LGPL-2.1+
       3                 :            : ***/
       4                 :            : 
       5                 :            : #include "fd-util.h"
       6                 :            : #include "fileio.h"
       7                 :            : #include "log.h"
       8                 :            : #include "macro.h"
       9                 :            : #include "network-internal.h"
      10                 :            : #include "networkd-manager.h"
      11                 :            : #include "string-util.h"
      12                 :            : #include "tests.h"
      13                 :            : #include "tmpfile-util.h"
      14                 :            : 
      15                 :         28 : static void test_rule_serialization(const char *title, const char *ruleset, const char *expected) {
      16                 :         28 :         char pattern[] = "/tmp/systemd-test-routing-policy-rule.XXXXXX",
      17                 :         28 :              pattern2[] = "/tmp/systemd-test-routing-policy-rule.XXXXXX",
      18                 :         28 :              pattern3[] = "/tmp/systemd-test-routing-policy-rule.XXXXXX";
      19                 :            :         const char *cmd;
      20                 :            :         int fd, fd2, fd3;
      21                 :         28 :         _cleanup_fclose_ FILE *f = NULL, *f2 = NULL, *f3 = NULL;
      22                 :         28 :         Set *rules = NULL;
      23                 :         28 :         _cleanup_free_ char *buf = NULL;
      24                 :            :         size_t buf_size;
      25                 :            : 
      26         [ +  - ]:         28 :         log_info("========== %s ==========", title);
      27         [ +  - ]:         28 :         log_info("put:\n%s\n", ruleset);
      28                 :            : 
      29                 :         28 :         fd = mkostemp_safe(pattern);
      30         [ -  + ]:         28 :         assert_se(fd >= 0);
      31         [ -  + ]:         28 :         assert_se(f = fdopen(fd, "a+"));
      32         [ -  + ]:         28 :         assert_se(write_string_stream(f, ruleset, 0) == 0);
      33                 :            : 
      34         [ -  + ]:         28 :         assert_se(routing_policy_load_rules(pattern, &rules) == 0);
      35                 :            : 
      36                 :         28 :         fd2 = mkostemp_safe(pattern2);
      37         [ -  + ]:         28 :         assert_se(fd2 >= 0);
      38         [ -  + ]:         28 :         assert_se(f2 = fdopen(fd2, "a+"));
      39                 :            : 
      40         [ -  + ]:         28 :         assert_se(routing_policy_serialize_rules(rules, f2) == 0);
      41         [ -  + ]:         28 :         assert_se(fflush_and_check(f2) == 0);
      42                 :            : 
      43         [ -  + ]:         28 :         assert_se(read_full_file(pattern2, &buf, &buf_size) == 0);
      44                 :            : 
      45         [ +  - ]:         28 :         log_info("got:\n%s", buf);
      46                 :            : 
      47                 :         28 :         fd3 = mkostemp_safe(pattern3);
      48         [ -  + ]:         28 :         assert_se(fd3 >= 0);
      49         [ -  + ]:         28 :         assert_se(f3 = fdopen(fd3, "w"));
      50   [ +  +  -  + ]:         28 :         assert_se(write_string_stream(f3, expected ?: ruleset, 0) == 0);
      51                 :            : 
      52   [ +  +  +  -  :        252 :         cmd = strjoina("diff -u ", pattern3, " ", pattern2);
          -  +  -  +  +  
                +  +  - ]
      53         [ +  - ]:         28 :         log_info("$ %s", cmd);
      54         [ -  + ]:         28 :         assert_se(system(cmd) == 0);
      55                 :            : 
      56         [ +  + ]:         56 :         set_free_with_destructor(rules, routing_policy_rule_free);
      57                 :         28 : }
      58                 :            : 
      59                 :          4 : int main(int argc, char **argv) {
      60                 :          8 :         _cleanup_free_ char *p = NULL;
      61                 :            : 
      62                 :          4 :         test_setup_logging(LOG_DEBUG);
      63                 :            : 
      64                 :          4 :         test_rule_serialization("basic parsing",
      65                 :            :                                 "RULE=from=1.2.3.4/32 to=2.3.4.5/32 family=AF_INET tos=5 priority=0 fwmark=1/2 table=10", NULL);
      66                 :            : 
      67                 :          4 :         test_rule_serialization("ignored values",
      68                 :            :                                 "RULE=something=to=ignore from=1.2.3.4/32 from=1.2.3.4/32"
      69                 :            :                                 "   \t  to=2.3.4.5/24 to=2.3.4.5/32 tos=5 fwmark=2 fwmark=1 table=10 table=20",
      70                 :            :                                 "RULE=from=1.2.3.4/32"
      71                 :            :                                 " to=2.3.4.5/32 family=AF_INET tos=5 priority=0 fwmark=1/0 table=20");
      72                 :            : 
      73                 :          4 :         test_rule_serialization("ipv6",
      74                 :            :                                 "RULE=from=1::2/64 to=2::3/64 family=AF_INET6 priority=0 table=6", NULL);
      75                 :            : 
      76         [ -  + ]:          4 :         assert_se(asprintf(&p, "RULE=from=1::2/64 to=2::3/64 family=AF_INET6 priority=0 table=%d", RT_TABLE_MAIN) >= 0);
      77                 :          4 :         test_rule_serialization("default table",
      78                 :            :                                 "RULE=from=1::2/64 to=2::3/64", p);
      79                 :            : 
      80                 :          4 :         test_rule_serialization("incoming interface",
      81                 :            :                                 "RULE=from=1::2/64 to=2::3/64 table=1 iif=lo",
      82                 :            :                                 "RULE=from=1::2/64 to=2::3/64 family=AF_INET6 priority=0 iif=lo table=1");
      83                 :            : 
      84                 :          4 :         test_rule_serialization("outgoing interface",
      85                 :            :                                 "RULE=from=1::2/64 to=2::3/64 family=AF_INET6 priority=0 oif=eth0 table=1", NULL);
      86                 :            : 
      87                 :          4 :         test_rule_serialization("freeing interface names",
      88                 :            :                                 "RULE=from=1::2/64 to=2::3/64 family=AF_INET6 iif=e0 iif=e1 oif=e0 oif=e1 table=1",
      89                 :            :                                 "RULE=from=1::2/64 to=2::3/64 family=AF_INET6 priority=0 iif=e1 oif=e1 table=1");
      90                 :            : 
      91                 :          4 :         return 0;
      92                 :            : }

Generated by: LCOV version 1.14