Sockaddr_test.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 <http://www.gnu.org/licenses/>.
  14. */
  15. #include "memory/MallocAllocator.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 = MallocAllocator_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. static void fromName()
  64. {
  65. struct Allocator* alloc = MallocAllocator_new(20000);
  66. Sockaddr_fromName("localhost", alloc);
  67. // This will fail in some cases (eg dns hijacking)
  68. //Assert_true(!Sockaddr_fromName("hasjklgyolgbvlbiogi", alloc));
  69. Allocator_free(alloc);
  70. }
  71. int main()
  72. {
  73. parse();
  74. fromName();
  75. return 0;
  76. }