LCOV - code coverage report
Current view: top level - test - test-ip-protocol-list.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 40 40 100.0 %
Date: 2019-08-22 15:41:25 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /* SPDX-License-Identifier: LGPL-2.1+ */
       2             : 
       3             : #include <netinet/in.h>
       4             : 
       5             : #include "macro.h"
       6             : #include "ip-protocol-list.h"
       7             : #include "stdio-util.h"
       8             : #include "string-util.h"
       9             : 
      10           2 : static void test_int(int i) {
      11             :         char str[DECIMAL_STR_MAX(int)];
      12             : 
      13           2 :         assert_se(ip_protocol_from_name(ip_protocol_to_name(i)) == i);
      14             : 
      15           2 :         xsprintf(str, "%i", i);
      16           2 :         assert_se(ip_protocol_from_name(ip_protocol_to_name(parse_ip_protocol(str))) == i);
      17           2 : }
      18             : 
      19           2 : static void test_int_fail(int i) {
      20             :         char str[DECIMAL_STR_MAX(int)];
      21             : 
      22           2 :         assert_se(!ip_protocol_to_name(i));
      23             : 
      24           2 :         xsprintf(str, "%i", i);
      25           2 :         assert_se(parse_ip_protocol(str) == -EINVAL);
      26           2 : }
      27             : 
      28           2 : static void test_str(const char *s) {
      29           2 :         assert_se(streq(ip_protocol_to_name(ip_protocol_from_name(s)), s));
      30           2 :         assert_se(streq(ip_protocol_to_name(parse_ip_protocol(s)), s));
      31           2 : }
      32             : 
      33           3 : static void test_str_fail(const char *s) {
      34           3 :         assert_se(ip_protocol_from_name(s) == -EINVAL);
      35           3 :         assert_se(parse_ip_protocol(s) == -EINVAL);
      36           3 : }
      37             : 
      38           8 : static void test_parse_ip_protocol(const char *s, int expected) {
      39           8 :         assert_se(parse_ip_protocol(s) == expected);
      40           8 : }
      41             : 
      42           1 : int main(int argc, const char *argv[]) {
      43           1 :         test_int(IPPROTO_TCP);
      44           1 :         test_int(IPPROTO_DCCP);
      45           1 :         test_int_fail(-1);
      46           1 :         test_int_fail(1024 * 1024);
      47             : 
      48           1 :         test_str("sctp");
      49           1 :         test_str("udp");
      50           1 :         test_str_fail("hoge");
      51           1 :         test_str_fail("-1");
      52           1 :         test_str_fail("1000000000");
      53             : 
      54           1 :         test_parse_ip_protocol("sctp", IPPROTO_SCTP);
      55           1 :         test_parse_ip_protocol("ScTp", IPPROTO_SCTP);
      56           1 :         test_parse_ip_protocol("ip", IPPROTO_IP);
      57           1 :         test_parse_ip_protocol("", IPPROTO_IP);
      58           1 :         test_parse_ip_protocol("1", 1);
      59           1 :         test_parse_ip_protocol("0", 0);
      60           1 :         test_parse_ip_protocol("-10", -EINVAL);
      61           1 :         test_parse_ip_protocol("100000000", -EINVAL);
      62             : 
      63           1 :         return 0;
      64             : }

Generated by: LCOV version 1.14