Leaked source code of windows server 2003
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.

235 lines
6.4 KiB

  1. /*++
  2. Copyright (c) 1989-2001 Microsoft Corporation
  3. Module Name:
  4. ip6test.c
  5. Abstract:
  6. test driver
  7. Author:
  8. Jiandong Ruan
  9. Revision History:
  10. --*/
  11. #include <nt.h>
  12. #include <ntrtl.h>
  13. #include <nturtl.h>
  14. #include <windows.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include "ip6util.h"
  18. BOOL
  19. SmbLookupHost(
  20. WCHAR *host,
  21. PSMB_IP_ADDRESS ipaddr
  22. );
  23. struct {
  24. WCHAR *src;
  25. WCHAR *dest;
  26. } test_cases[] = {
  27. { L"::1", L"::1" },
  28. { L"::", L"::" },
  29. { L"fe80::2b0:d0ff:fe1d:e082", L"fe80::2b0:d0ff:fe1d:e082" },
  30. { L"::0123", L"::123" },
  31. { L"0::0123", L"::123" },
  32. { L"::04567:0123", L"::4567:123" },
  33. { L"0::4567:0123", L"::4567:123" },
  34. { L"::089Ab:4567:0123", L"::89ab:4567:123" },
  35. { L"0::89Ab:4567:0123", L"::89ab:4567:123" },
  36. { L"::cDeF:089Ab:4567:0123", L"::cDeF:89ab:4567:123" },
  37. { L"0::0cDeF:089Ab:4567:0123", L"::cDeF:89ab:4567:123" },
  38. { L"0123::", L"123::" },
  39. { L"0123::0", L"123::" },
  40. { L"0123:4567::", L"123:4567::" },
  41. { L"0123:4567::0", L"123:4567::" },
  42. { L"0123:4567:89aB::", L"123:4567:89ab::" },
  43. { L"0123:4567:89aB::0", L"123:4567:89ab::" },
  44. { L"0123:4567:89aB:cDeF::", L"123:4567:89ab:cdef::" },
  45. { L"0123:4567:89aB:cDeF::0", L"123:4567:89ab:cdef::" },
  46. { L"0ea8::001", L"ea8::1" },
  47. { L"2ea8::001", L"2ea8::1" },
  48. { L"0:2ea8::f01", L"0:2ea8::f01" },
  49. { L"0:2ea8::0:f01", L"0:2ea8::f01" },
  50. { L"0:2ea8:0::f01", L"0:2ea8::f01" },
  51. { L"0:2ea8::f01:00", L"0:2ea8::f01:0" },
  52. { L"fedc::f01", L"fedc::f01" },
  53. { L"0fedc::f01", L"fedc::f01" },
  54. { L"0fedc::ba98:f01", L"fedc::ba98:f01" },
  55. { L"0FEDC::BA98:fffe:f01", L"fedc::ba98:fffe:f01" },
  56. { L"0fedc::0fffc:fffd:0fffe:f01", L"fedc::fffc:fffd:fffe:f01" },
  57. { L"0fedc:0fffc:fffd:0fffe::f01", L"fedc:fffc:fffd:fffe::f01" },
  58. { L"0fedc:0fffc:fffd::f01", L"fedc:fffc:fffd::f01" },
  59. { L"0fedc:0fffc::f01", L"fedc:fffc::f01" },
  60. { L"0ea8::0::001", NULL },
  61. { L"001fedc:001fffc::f01", NULL },
  62. { L"001fedc::001fffc::f01", NULL },
  63. { L"0fedc::0001ba98:fffd:0fffe:f01", NULL },
  64. { L"0fedc::ba98:001fffd:0fffe:f01", NULL },
  65. { L"0fedc::00ba98:fffd:0010000:f01", NULL }
  66. };
  67. #define CASE_NUMBER (sizeof(test_cases)/sizeof(test_cases[0]))
  68. #define WSTR_LOOPBACK L"::1"
  69. void test_host_lookup(void);
  70. void test_inet_functions(void);
  71. void _cdecl main(void)
  72. {
  73. test_inet_functions();
  74. test_host_lookup();
  75. }
  76. void test_inet_functions(void)
  77. {
  78. WCHAR Buffer[40];
  79. WCHAR Buffer2[40];
  80. SMB_IP6_ADDRESS loopback, addr;
  81. int i, j;
  82. UNICODE_STRING uc1, uc2;
  83. ip6addr_getloopback(&loopback);
  84. hton_ip6addr(&loopback);
  85. //
  86. // Test inet_ntoa6W and inet_addr6W
  87. //
  88. if (inet_ntoa6W(Buffer, 40, &loopback) && wcscmp(Buffer, WSTR_LOOPBACK) != 0) {
  89. printf ("inet_ntoa6W on loopback: [failed]\n");
  90. }
  91. for (i = 0; i < CASE_NUMBER; i++) {
  92. if (inet_addr6W(test_cases[i].src, &addr)) {
  93. if (test_cases[i].dest == NULL) {
  94. printf ("inet_addr6W on %ws: [failed]\n", test_cases[i].src);
  95. printf ("\tExpected behavior: inet_addr6W should have returned FALSE\n");
  96. continue;
  97. }
  98. RtlInitUnicodeString(&uc2, test_cases[i].dest);
  99. uc1.Buffer = Buffer2;
  100. uc1.MaximumLength = sizeof(Buffer2);
  101. RtlUpcaseUnicodeString(&uc1, &uc2, FALSE);
  102. Buffer2[uc1.Length/sizeof(WCHAR)] = 0;
  103. if (!inet_ntoa6W(Buffer, 40, &addr)) {
  104. printf ("inet_ntoa6W on %-40ws: [failed]\n", test_cases[i].src);
  105. printf ("\tExpected behavior: inet_ntoa6W should have returned TRUE, %ws\n",
  106. Buffer2);
  107. continue;
  108. }
  109. if (wcscmp(Buffer, Buffer2) != 0) {
  110. printf ("inet_addr6W and inet_ntoa6W on %-40ws [failed]\n", test_cases[i].src);
  111. printf ("\tinet_addr6W returns:\n");
  112. putchar('\t');
  113. putchar('\t');
  114. for (j = 0; j < 8; j++) {
  115. if (j != 0) {
  116. putchar(':');
  117. }
  118. printf ("%04x", htons(addr.sin6_addr[j]));
  119. }
  120. putchar('\n');
  121. printf ("\tinet_ntoa returns: %ws (Expected %ws)\n", Buffer, Buffer2);
  122. }
  123. } else {
  124. if (test_cases[i].dest != NULL) {
  125. printf ("inet_addr6W on %ws: [failed]\n", test_cases[i].src);
  126. printf ("\tExpected behavior: inet_addr6W should have returned TRUE\n");
  127. continue;
  128. }
  129. }
  130. }
  131. }
  132. void test_host_lookup(void)
  133. {
  134. SMB_IP_ADDRESS addr;
  135. FILE *fp;
  136. int i;
  137. WCHAR Buffer[40], Buffer2[40];
  138. if (SmbLookupHost(L"localhost", &addr)) {
  139. if (!inet_ntoa6W(Buffer, 40, &addr.ip6)) {
  140. printf ("etc/hosts [failed]\n");
  141. }
  142. printf ("%ws localhost\n", Buffer);
  143. }
  144. wcscpy(Buffer2, L"::1");
  145. fp = fopen("hosts", "w+");
  146. if (fp == NULL) {
  147. printf ("failed to create file\n");
  148. return;
  149. }
  150. fprintf(fp, " # some comments \n");
  151. fprintf(fp, "# some comments \n");
  152. fprintf(fp, "# \t\tsome comments\n");
  153. fprintf(fp, "%ws Jruan-Dev # some comments", Buffer2);
  154. fclose(fp);
  155. if (SmbLookupHost(L"jruan-dev", &addr)) {
  156. if (!inet_ntoa6W(Buffer, 40, &addr.ip6)) {
  157. printf ("etc/hosts on %-40ws: [failed]\n", Buffer2);
  158. printf ("\tExpected behavior: inet_ntoa6W should have returned TRUE, %ws\n", Buffer2);
  159. } else {
  160. if (wcscmp(Buffer, Buffer2) != 0) {
  161. printf ("etc/hosts on [failed]\n");
  162. printf ("\tExpected: \"%ws\" Get \"%ws\"\n", Buffer2, Buffer);
  163. }
  164. }
  165. } else {
  166. printf ("etc/hosts [failed]\n");
  167. }
  168. }
  169. FILE*
  170. Smb_fopen(
  171. PWCHAR path,
  172. PWCHAR mode
  173. )
  174. {
  175. FILE *fp;
  176. UNREFERENCED_PARAMETER(path);
  177. UNREFERENCED_PARAMETER(mode);
  178. fp = fopen ("hosts", "r");
  179. return fp;
  180. }
  181. void
  182. Smb_fclose(
  183. FILE* fp
  184. )
  185. {
  186. if (fp) {
  187. fclose(fp);
  188. }
  189. }
  190. int
  191. Smb_fgetc(
  192. FILE* fp
  193. )
  194. {
  195. return fgetc(fp);
  196. }