1 /*
2  * PROJECT:         ReactOS api tests
3  * LICENSE:         GPLv2+ - See COPYING in the top level directory
4  * PURPOSE:         Test for InternetOpen
5  * PROGRAMMER:      Thomas Faber <thomas.faber@reactos.org>
6  */
7 
8 #include <apitest.h>
9 
10 #define WIN32_NO_STATUS
11 #define _INC_WINDOWS
12 #define COM_NO_WINDOWS_H
13 #include <windef.h>
14 #include <winsock2.h>
15 #include <wininet.h>
16 
17 struct hostent *(WINAPI *pgethostbyname)(const char *);
18 int (WINAPI *pWSACancelBlockingCall)(void);
19 int (WINAPI *pWSAGetLastError)(void);
20 
21 HINTERNET (WINAPI *pInternetOpen)(LPCTSTR, DWORD, LPCTSTR, LPCTSTR, DWORD);
22 BOOL (WINAPI *pInternetCloseHandle)(HINTERNET);
23 
24 static
25 PVOID
GetProc(PCSTR FunctionName)26 GetProc(
27     PCSTR FunctionName)
28 {
29     HMODULE ModuleHandle;
30 
31     ModuleHandle = GetModuleHandleW(L"ws2_32");
32     if (!ModuleHandle)
33         return NULL;
34     return GetProcAddress(ModuleHandle, FunctionName);
35 }
36 
37 #define PROC(name) (p##name = GetProc(#name))
38 
39 static
40 BOOLEAN
IsWinsockLoaded(VOID)41 IsWinsockLoaded(VOID)
42 {
43     return GetModuleHandleW(L"ws2_32") != NULL;
44 }
45 
46 static
47 BOOLEAN
IsWinsockInitialized(VOID)48 IsWinsockInitialized(VOID)
49 {
50     struct hostent *Hostent;
51 
52     if (!PROC(gethostbyname) || !PROC(WSAGetLastError))
53         return FALSE;
54 
55     Hostent = pgethostbyname("localhost");
56     if (!Hostent)
57         ok_dec(pWSAGetLastError(), WSANOTINITIALISED);
58     return Hostent != NULL;
59 }
60 
61 static
62 BOOLEAN
AreLegacyFunctionsSupported(VOID)63 AreLegacyFunctionsSupported(VOID)
64 {
65     int Error;
66 
67     if (!PROC(WSACancelBlockingCall) || !PROC(WSAGetLastError))
68         return FALSE;
69 
70     Error = pWSACancelBlockingCall();
71     ok(Error == SOCKET_ERROR, "Error = %d\n", Error);
72     ok(pWSAGetLastError() == WSAEOPNOTSUPP ||
73        pWSAGetLastError() == WSAEINVAL, "WSAGetLastError = %d\n", pWSAGetLastError());
74 
75     return pWSAGetLastError() != WSAEOPNOTSUPP;
76 }
77 
START_TEST(InternetOpen)78 START_TEST(InternetOpen)
79 {
80     HMODULE ModuleHandle;
81     HINTERNET InternetHandle;
82     BOOL Success;
83 
84     ok(!IsWinsockLoaded(), "Winsock loaded on startup\n");
85     ok(!IsWinsockInitialized(), "Winsock initialized on startup\n");
86 
87     ModuleHandle = GetModuleHandleW(L"wininet");
88     ok_ptr(ModuleHandle, NULL);
89     ModuleHandle = LoadLibraryW(L"wininet");
90     ok(ModuleHandle != NULL, "LoadLibrary failed, error %lu\n", GetLastError());
91 
92     pInternetOpen = (PVOID)GetProcAddress(ModuleHandle, "InternetOpenW");
93     pInternetCloseHandle = (PVOID)GetProcAddress(ModuleHandle, "InternetCloseHandle");
94 
95     ok(!IsWinsockLoaded(), "Winsock loaded after wininet load\n");
96     ok(!IsWinsockInitialized(), "Winsock initialized after wininet load\n");
97 
98     InternetHandle = pInternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
99     ok(InternetHandle != NULL, "InternetHandle = NULL\n");
100 
101     if (InternetHandle != NULL)
102     {
103         ok(IsWinsockLoaded(), "Winsock not loaded after InternetOpen\n");
104         ok(IsWinsockInitialized(), "Winsock not initialized after InternetOpen\n");
105         ok(!AreLegacyFunctionsSupported(), "Winsock initialized with version 1\n");
106         Success = pInternetCloseHandle(InternetHandle);
107         ok(Success, "InternetCloseHandle failed, error %lu\n", GetLastError());
108     }
109 
110     ok(IsWinsockLoaded(), "Winsock unloaded after handle close\n");
111     ok(IsWinsockInitialized(), "Winsock uninitialized after handle close\n");
112 
113     FreeLibrary(ModuleHandle);
114 
115     ok(IsWinsockLoaded(), "Winsock unloaded after wininet unload\n");
116     trace("Winsock %sinitialized after wininet unload (should be uninitialized in 2003, still initialized in 7)\n",
117           IsWinsockInitialized() ? "" : "un");
118 }
119