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.

hostport.cxx 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* Copyright 2016 Pierre Ossman <ossman@cendio.se> for Cendio AB
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21. #include <stdio.h>
  22. #include <rfb/Hostname.h>
  23. static void doTest(const char* hostAndPort,
  24. const char* expectedHost, int expectedPort)
  25. {
  26. char* host;
  27. int port;
  28. printf("\"%s\": ", hostAndPort);
  29. rfb::getHostAndPort(hostAndPort, &host, &port);
  30. if (strcmp(host, expectedHost) != 0)
  31. printf("FAILED (\"%s\" != \"%s\")", host, expectedHost);
  32. else if (port != expectedPort)
  33. printf("FAILED (%d != %d)", port, expectedPort);
  34. else
  35. printf("OK");
  36. printf("\n");
  37. fflush(stdout);
  38. rfb::strFree(host);
  39. }
  40. int main(int argc, char** argv)
  41. {
  42. doTest(":5", "localhost", 5905);
  43. doTest("1.2.3.4", "1.2.3.4", 5900);
  44. doTest("1.2.3.4:5", "1.2.3.4", 5905);
  45. doTest("1.2.3.4:99", "1.2.3.4", 5999);
  46. doTest("1.2.3.4:100", "1.2.3.4", 100);
  47. doTest("1.2.3.4:5901", "1.2.3.4", 5901);
  48. doTest("1.2.3.4::5", "1.2.3.4", 5);
  49. doTest("1.2.3.4::99", "1.2.3.4", 99);
  50. doTest("1.2.3.4::5901", "1.2.3.4", 5901);
  51. doTest("[1.2.3.4]", "1.2.3.4", 5900);
  52. doTest("[1.2.3.4]:5", "1.2.3.4", 5905);
  53. doTest("[1.2.3.4]:100", "1.2.3.4", 100);
  54. doTest("[1.2.3.4]::5", "1.2.3.4", 5);
  55. doTest("[1.2.3.4]::100", "1.2.3.4", 100);
  56. // Ambigiuous. For now we'll keep the old behaviour...
  57. doTest("::1", "localhost", 1);
  58. doTest("2001:1234::20:1", "2001:1234::20:1", 5900);
  59. doTest("[::1]", "::1", 5900);
  60. doTest("[2001:1234::20:1]", "2001:1234::20:1", 5900);
  61. doTest("[2001:1234::20:1]:5", "2001:1234::20:1", 5905);
  62. doTest("[2001:1234::20:1]:99", "2001:1234::20:1", 5999);
  63. doTest("[2001:1234::20:1]:100", "2001:1234::20:1", 100);
  64. doTest("[2001:1234::20:1]:5901", "2001:1234::20:1", 5901);
  65. doTest(" 1.2.3.4 ", "1.2.3.4", 5900);
  66. doTest(" 1.2.3.4:5901 ", "1.2.3.4", 5901);
  67. doTest(" 1.2.3.4: 5901 ", "1.2.3.4", 5901);
  68. doTest(" 1.2.3.4 :5901 ", "1.2.3.4", 5901);
  69. doTest(" [1.2.3.4]:5902 ", "1.2.3.4", 5902);
  70. doTest(" :5903 ", "localhost", 5903);
  71. doTest(" ::4 ", "localhost", 4);
  72. doTest(" [::1] ", "::1", 5900);
  73. doTest(" 2001:1234::20:1 ", "2001:1234::20:1", 5900);
  74. doTest(" [2001:1234::20:1] ", "2001:1234::20:1", 5900);
  75. doTest(" [2001:1234::20:1]:5905 ", "2001:1234::20:1", 5905);
  76. return 0;
  77. }