[WININET_APITEST] Add Download testcase (#1832)
[reactos.git] / modules / rostests / apitests / wininet / Download.c
1 /*
2 * PROJECT: ReactOS API Tests
3 * LICENSE: LGPL-2.1+ (https://spdx.org/licenses/LGPL-2.1+)
4 * PURPOSE: wininet Download testcase
5 * COPYRIGHT: Copyright 2019 Katayama Hirofumi MZ (katayama.hirofumi.mz@gmail.com)
6 */
7 #include <apitest.h>
8 #include <stdio.h>
9
10 #define WIN32_NO_STATUS
11 #define _INC_WINDOWS
12 #define COM_NO_WINDOWS_H
13 #include <windef.h>
14 #include <wininet.h>
15
16 #define FILENAME "download-testdata.txt"
17 #define TESTDATA "This is a test data.\r\n"
18
19 static void DoDownload1(const char *url, const char *filename)
20 {
21 HANDLE hFile;
22 HINTERNET hInternet, hConnect;
23 static const char s_header[] = "Accept: */" "*\r\n\r\n";
24 BYTE buffer[256];
25 DWORD cbRead;
26 BOOL ret;
27
28 hFile = CreateFileA(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
29 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
30 ok(hFile != INVALID_HANDLE_VALUE, "hFile was INVALID_HANDLE_VALUE.\n");
31
32 hInternet = InternetOpenA(NULL, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
33 ok(hInternet != NULL, "hInternet was NULL.\n");
34
35 hConnect = InternetOpenUrlA(hInternet, url, s_header, lstrlenA(s_header),
36 INTERNET_FLAG_DONT_CACHE, 0);
37 ok(hConnect != NULL, "hConnect was NULL.\n");
38
39 for (;;)
40 {
41 Sleep(100);
42
43 ret = InternetReadFile(hConnect, buffer, ARRAYSIZE(buffer), &cbRead);
44 if (!ret || !cbRead)
45 break;
46
47 if (!WriteFile(hFile, buffer, cbRead, &cbRead, NULL))
48 {
49 ok(0, "WriteFile returns FALSE.\n");
50 break;
51 }
52 }
53
54 ok_int(InternetCloseHandle(hConnect), TRUE);
55 ok_int(InternetCloseHandle(hInternet), TRUE);
56 CloseHandle(hFile);
57 }
58
59 static void DoDownload2(const char *url, const char *filename)
60 {
61 FILE *fp;
62 char buf[256];
63 DoDownload1(url, filename);
64 ok_int(GetFileAttributesA(FILENAME) != INVALID_FILE_ATTRIBUTES, TRUE);
65 fp = fopen(FILENAME, "rb");
66 ok(fp != NULL, "fp was NULL.\n");
67 ok(fgets(buf, ARRAYSIZE(buf), fp) != NULL, "fgets failed.\n");
68 ok_str(buf, TESTDATA);
69 fclose(fp);
70 DeleteFileA(FILENAME);
71 }
72
73 START_TEST(Download)
74 {
75 // https://tinyurl.com/y4cpy2fu
76 // -->
77 // https://raw.githubusercontent.com/katahiromz/downloads/master/download-testdata.txt
78 DoDownload2("https://tinyurl.com/y4cpy2fu", FILENAME);
79
80 DoDownload2(
81 "https://raw.githubusercontent.com/katahiromz/downloads/master/download-testdata.txt",
82 FILENAME);
83 }