1 /*
2  * PROJECT:         ReactOS API Tests
3  * LICENSE:         LGPLv2.1+ - See COPYING.LIB in the top level directory
4  * PURPOSE:         Tests for ICMP functions
5  * PROGRAMMERS:     Tim Crawford
6  */
7 
8 #include <apitest.h>
9 #include <ws2tcpip.h>
10 #include <iphlpapi.h>
11 #include <icmpapi.h>
12 
13 static
14 void
15 test_IcmpCreateFile(void)
16 {
17     HANDLE hIcmp;
18 
19     SetLastError(0xDEADBEEF);
20     hIcmp = IcmpCreateFile();
21     ok(hIcmp != INVALID_HANDLE_VALUE, "IcmpCreateFile failed unexpectedly: %lu\n", GetLastError());
22 
23     if (hIcmp != INVALID_HANDLE_VALUE)
24         IcmpCloseHandle(hIcmp);
25 }
26 
27 static
28 void
29 test_Icmp6CreateFile(void)
30 {
31     HANDLE hIcmp;
32 
33     SetLastError(0xDEADBEEF);
34     hIcmp = Icmp6CreateFile();
35 
36     if (GetLastError() == ERROR_FILE_NOT_FOUND)
37     {
38         /* On Windows Server 2003, the IPv6 protocol must be installed. */
39         skip("IPv6 is not available.\n");
40         return;
41     }
42 
43     ok(hIcmp != INVALID_HANDLE_VALUE, "Icmp6CreateFile failed unexpectedly: %lu\n", GetLastError());
44 
45     if (hIcmp != INVALID_HANDLE_VALUE)
46         IcmpCloseHandle(hIcmp);
47 }
48 
49 static
50 void
51 test_IcmpCloseHandle(void)
52 {
53     HANDLE hIcmp;
54     BOOL bRet;
55 
56     SetLastError(0xDEADBEEF);
57     hIcmp = IcmpCreateFile();
58     if (hIcmp != INVALID_HANDLE_VALUE)
59     {
60         bRet = IcmpCloseHandle(hIcmp);
61         ok(bRet, "IcmpCloseHandle failed unexpectedly: %lu\n", GetLastError());
62     }
63 
64     SetLastError(0xDEADBEEF);
65     hIcmp = Icmp6CreateFile();
66     if (hIcmp != INVALID_HANDLE_VALUE)
67     {
68         bRet = IcmpCloseHandle(hIcmp);
69         ok(bRet, "IcmpCloseHandle failed unexpectedly: %lu\n", GetLastError());
70     }
71 
72     hIcmp = INVALID_HANDLE_VALUE;
73     SetLastError(0xDEADBEEF);
74     bRet = IcmpCloseHandle(hIcmp);
75     ok(!bRet, "IcmpCloseHandle succeeded unexpectedly\n");
76     ok_err(ERROR_INVALID_HANDLE);
77 
78     hIcmp = NULL;
79     SetLastError(0xDEADBEEF);
80     bRet = IcmpCloseHandle(hIcmp);
81     ok(!bRet, "IcmpCloseHandle succeeded unexpectedly\n");
82     ok_err(ERROR_INVALID_HANDLE);
83 }
84 
85 static
86 void
87 test_IcmpSendEcho(void)
88 {
89     HANDLE hIcmp;
90     unsigned long ipaddr = INADDR_NONE;
91     DWORD bRet = 0, error = 0;
92     char SendData[32] = "Data Buffer";
93     PVOID ReplyBuffer = NULL;
94     DWORD ReplySize = 0;
95 
96     SetLastError(0xDEADBEEF);
97     hIcmp = IcmpCreateFile();
98     if (hIcmp == INVALID_HANDLE_VALUE)
99     {
100         skip("IcmpCreateFile failed unexpectedly: %lu\n", GetLastError());
101         return;
102     }
103 
104     ipaddr = 0x08080808; // 8.8.8.8
105 
106     ReplySize = sizeof(ICMP_ECHO_REPLY);
107     ReplyBuffer = malloc(ReplySize);
108     SetLastError(0xDEADBEEF);
109     bRet = IcmpSendEcho(hIcmp, ipaddr, SendData, sizeof(SendData),
110         NULL, ReplyBuffer, ReplySize, 5000);
111 
112     ok(!bRet, "IcmpSendEcho succeeded unexpectedly\n");
113     error = GetLastError();
114     ok(error == IP_GENERAL_FAILURE, "IcmpSendEcho returned unexpected error: %lu\n", error);
115     free(ReplyBuffer);
116 
117     ReplySize = sizeof(ICMP_ECHO_REPLY) + sizeof(SendData);
118     ReplyBuffer = malloc(ReplySize);
119     SetLastError(0xDEADBEEF);
120     bRet = IcmpSendEcho(hIcmp, ipaddr, SendData, sizeof(SendData),
121         NULL, ReplyBuffer, ReplySize, 5000);
122 
123     ok(bRet, "IcmpSendEcho failed unexpectedly: %lu\n", GetLastError());
124     free(ReplyBuffer);
125 
126     IcmpCloseHandle(hIcmp);
127 }
128 
129 START_TEST(icmp)
130 {
131     test_IcmpCreateFile();
132     test_Icmp6CreateFile();
133     test_IcmpCloseHandle();
134     test_IcmpSendEcho();
135 }
136