Sockaddr_test.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "memory/Allocator.h"
  16. #include "util/platform/Sockaddr.h"
  17. #include "util/Assert.h"
  18. #include "util/CString.h"
  19. static void expectFailure(char* address)
  20. {
  21. struct Sockaddr_storage ss;
  22. Assert_true(Sockaddr_parse(address, &ss));
  23. }
  24. static void expectConvert(char* address, char* expectedOutput)
  25. {
  26. struct Sockaddr_storage ss;
  27. Assert_true(!Sockaddr_parse(address, &ss));
  28. struct Allocator* alloc = Allocator_new(20000);
  29. char* outAddr = Sockaddr_print(&ss.addr, alloc);
  30. Assert_true(outAddr);
  31. Assert_true(CString_strlen(outAddr) == CString_strlen(expectedOutput));
  32. Assert_true(!CString_strcmp(outAddr, expectedOutput));
  33. Allocator_free(alloc);
  34. }
  35. static void expectSuccess(char* address)
  36. {
  37. expectConvert(address, address);
  38. }
  39. static void parse()
  40. {
  41. struct Sockaddr_storage test;
  42. Assert_true(Sockaddr_asNative(&test.addr) == ((uint8_t*)&test) + Sockaddr_OVERHEAD);
  43. expectSuccess("0.0.0.0");
  44. expectSuccess("111.111.111.111");
  45. expectSuccess("111.111.111.111:12345");
  46. expectFailure("111.111.111.111:99999");
  47. expectFailure("[111.111.111.111]");
  48. expectFailure("[fc00::");
  49. expectConvert("[fc00::]", "fc00::");
  50. expectSuccess("fc00::");
  51. expectSuccess("::");
  52. expectSuccess("1::1");
  53. expectSuccess("1::1");
  54. expectSuccess("[1::1]:12345");
  55. expectFailure("[1::1]:99999");
  56. expectSuccess("[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:12345");
  57. expectFailure("[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:12345");
  58. expectFailure("[:]:12345");
  59. expectFailure("[0]:12345");
  60. expectFailure("0");
  61. expectFailure("1.0.0.");
  62. }
  63. int main()
  64. {
  65. parse();
  66. return 0;
  67. }