You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

144 lines
3.9 KiB

  1. #include "srunner.h"
  2. #include "netcfg.h"
  3. START_TEST(test_parse_standalone_v4_address)
  4. {
  5. struct netcfg_interface interface;
  6. netcfg_interface_init(&interface);
  7. int rv;
  8. interface.masklen = 7;
  9. rv = netcfg_parse_cidr_address("192.0.2.12", &interface);
  10. fail_unless (rv,
  11. "parsing failed, rv = %i", rv);
  12. fail_unless (interface.masklen == 0,
  13. "masklen was %i, should have been 0",
  14. interface.masklen);
  15. fail_unless (strcmp("192.0.2.12", interface.ipaddress) == 0,
  16. "IP address was %s, should have been 192.10.2.12",
  17. interface.ipaddress);
  18. fail_unless (interface.address_family == AF_INET,
  19. "Address family should have been AF_INET");
  20. }
  21. END_TEST
  22. START_TEST(test_parse_cidr_v4_address)
  23. {
  24. struct netcfg_interface interface;
  25. netcfg_interface_init(&interface);
  26. int rv;
  27. interface.masklen = 7;
  28. rv = netcfg_parse_cidr_address("192.0.2.12/24", &interface);
  29. fail_unless (rv,
  30. "parsing failed, rv = %i", rv);
  31. fail_unless (interface.masklen == 24,
  32. "masklen was %i, should have been 24",
  33. interface.masklen);
  34. fail_unless (strcmp("192.0.2.12", interface.ipaddress) == 0,
  35. "IP address was %s, should have been 192.10.2.12",
  36. interface.ipaddress);
  37. fail_unless (interface.address_family == AF_INET,
  38. "Address family should have been AF_INET");
  39. }
  40. END_TEST
  41. START_TEST(test_parse_standalone_v6_address)
  42. {
  43. struct netcfg_interface interface;
  44. netcfg_interface_init(&interface);
  45. int rv;
  46. interface.masklen = 7;
  47. rv = netcfg_parse_cidr_address("fd80:0:0::2", &interface);
  48. fail_unless (rv,
  49. "parsing failed, rv = %i", rv);
  50. fail_unless (interface.masklen == 0,
  51. "masklen was %i, should have been 0",
  52. interface.masklen);
  53. fail_unless (strcmp("fd80::2", interface.ipaddress) == 0,
  54. "IP address was %s, should have been fd80::2",
  55. interface.ipaddress);
  56. fail_unless (interface.address_family == AF_INET6,
  57. "Address family should have been AF_INET6");
  58. }
  59. END_TEST
  60. START_TEST(test_parse_cidr_v6_address)
  61. {
  62. struct netcfg_interface interface;
  63. netcfg_interface_init(&interface);
  64. int rv;
  65. interface.masklen = 7;
  66. rv = netcfg_parse_cidr_address("fd80:0::4/64", &interface);
  67. fail_unless (rv,
  68. "parsing failed, rv = %i", rv);
  69. fail_unless (interface.masklen == 64,
  70. "masklen was %i, should have been 26",
  71. interface.masklen);
  72. fail_unless (strcmp("fd80::4", interface.ipaddress) == 0,
  73. "IP address was %s, should have been fd80::4",
  74. interface.ipaddress);
  75. fail_unless (interface.address_family == AF_INET6,
  76. "Address family should have been AF_INET6");
  77. }
  78. END_TEST
  79. START_TEST(test_parse_cidr_ignore_leading_trailing_spaces)
  80. {
  81. struct netcfg_interface interface;
  82. netcfg_interface_init(&interface);
  83. int rv;
  84. interface.masklen = 7;
  85. rv = netcfg_parse_cidr_address(" 192.0.2.12 ", &interface);
  86. fail_unless (rv,
  87. "parsing failed, rv = %i", rv);
  88. fail_unless (interface.masklen == 0,
  89. "masklen was %i, should have been 0",
  90. interface.masklen);
  91. fail_unless (strcmp("192.0.2.12", interface.ipaddress) == 0,
  92. "IP address was %s, should have been 192.10.2.12",
  93. interface.ipaddress);
  94. fail_unless (interface.address_family == AF_INET,
  95. "Address family should have been AF_INET");
  96. }
  97. END_TEST
  98. Suite *test_netcfg_parse_cidr_address_suite (void)
  99. {
  100. Suite *s = suite_create ("netcfg_parse_cidr_address");
  101. TCase *tc = tcase_create ("netcfg_parse_cidr_address");
  102. tcase_add_test (tc, test_parse_standalone_v4_address);
  103. tcase_add_test (tc, test_parse_cidr_v4_address);
  104. tcase_add_test (tc, test_parse_standalone_v6_address);
  105. tcase_add_test (tc, test_parse_cidr_v6_address);
  106. tcase_add_test (tc, test_parse_cidr_ignore_leading_trailing_spaces);
  107. suite_add_tcase (s, tc);
  108. return s;
  109. }