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.

404 lines
14 KiB

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright 1996 Microsoft Corporation. All Rights Reserved.
  7. // ===========================================================================
  8. //
  9. // Test driver for the persistant URL Cache component
  10. //
  11. // This define is a hack
  12. #define __CACHE_INCLUDE__
  13. #include <windows.h>
  14. #include <winhttp.h>
  15. #include "..\cache.hxx"
  16. #include <conio.h>
  17. #include <stdio.h>
  18. #include <crtdbg.h>
  19. #include <stdlib.h>
  20. #include <time.h>
  21. // support for Unicode
  22. #include <tchar.h>
  23. // file I/O include
  24. #include <fstream.h>
  25. #define CACHE_ENTRY_BUFFER_SIZE (1024 * 5)
  26. #define DEFAULT_BUFFER_SIZE 1024
  27. #define MAX_URL_LENGTH (1024 * 5)
  28. // URL string constants
  29. #define URL_1 "t-eddieng"
  30. #define URL_2 "http://www.microsoft.com"
  31. // global variables
  32. BYTE GlobalCacheEntryInfoBuffer[CACHE_ENTRY_BUFFER_SIZE];
  33. // General Utility Functions
  34. // =======================================================================
  35. LPTSTR
  36. ConvertGmtTimeToString(
  37. FILETIME Time,
  38. LPTSTR OutputBuffer
  39. )
  40. {
  41. SYSTEMTIME SystemTime;
  42. FILETIME LocalTime;
  43. static FILETIME ftNone = {0, 0};
  44. if (!memcmp (&Time, &ftNone, sizeof(FILETIME)))
  45. _stprintf (OutputBuffer, _T( "<none>" ));
  46. else
  47. {
  48. FileTimeToLocalFileTime( &Time , &LocalTime );
  49. FileTimeToSystemTime( &LocalTime, &SystemTime );
  50. _stprintf( OutputBuffer,
  51. _T( "%02u/%02u/%04u %02u:%02u:%02u " ),
  52. SystemTime.wMonth,
  53. SystemTime.wDay,
  54. SystemTime.wYear,
  55. SystemTime.wHour,
  56. SystemTime.wMinute,
  57. SystemTime.wSecond );
  58. }
  59. return( OutputBuffer );
  60. }
  61. DWORD TestCommitCacheEntryWithoutInet(VOID) {
  62. DWORD dwExpectedSize = 0;
  63. TCHAR lpFileExtension[] = "html";
  64. TCHAR lpszFileName[MAX_PATH];
  65. TCHAR szBuffer[] = "<HTML><TITLE>Test</TITLE><BODY>Testgadfasing</BODY></HTML>";
  66. HANDLE hWrite;
  67. DWORD dwWritten;
  68. // Prepare cache entry by calling CreateUrlCacheEntry
  69. if( !CreateUrlCacheEntryA(
  70. URL_1,
  71. dwExpectedSize,
  72. lpFileExtension,
  73. lpszFileName,
  74. 0 ) ) {
  75. printf ("CreateUrlCacheEntry failed: %x\n", GetLastError());
  76. return( GetLastError() );
  77. }
  78. _tprintf(_T( "URL = %s\n" ), lpszFileName);
  79. // Now monkey around with the local file (lpszFileName)
  80. hWrite = CreateFile(lpszFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  81. if (hWrite == INVALID_HANDLE_VALUE) {
  82. printf ("CreateFile failed: %x\n", GetLastError());
  83. return GetLastError();
  84. }
  85. WriteFile(hWrite, &szBuffer, sizeof(szBuffer), &dwWritten, NULL);
  86. if (sizeof(szBuffer) != dwWritten) {
  87. printf ("WriteFile failed: %x\n", GetLastError());
  88. return GetLastError();
  89. }
  90. CloseHandle(hWrite);
  91. FILETIME unknownTime1;
  92. unknownTime1.dwLowDateTime = 0;
  93. unknownTime1.dwHighDateTime = 0;
  94. FILETIME unknownTime2;
  95. unknownTime2.dwLowDateTime = 0;
  96. unknownTime2.dwHighDateTime = 0;
  97. // Commit cache entry by calling CommitUrlCacheEntry
  98. if ( !CommitUrlCacheEntryA(URL_1,
  99. lpszFileName,
  100. unknownTime1, /* unknown expire time */
  101. unknownTime2, /* unknown last modified time */
  102. NORMAL_CACHE_ENTRY,
  103. NULL,
  104. 0,
  105. NULL,
  106. NULL)) {
  107. printf ("CommitUrlCacheEntry failed: %x\n", GetLastError());
  108. return GetLastError();
  109. }
  110. return TRUE;
  111. }
  112. /* +++
  113. FPTestEnumerateCache Function pointers
  114. --- */
  115. // =======================================================================
  116. DWORD TestCommitCacheEntryFromInet(LPINTERNET_CACHE_ENTRY_INFO lpCacheEntryInfo) {
  117. TCHAR TimeBuffer[DEFAULT_BUFFER_SIZE];
  118. LPTSTR Tab = _T( "" );
  119. TCHAR *lpFileExtension = NULL;
  120. LPSTR lpszUrlName = lpCacheEntryInfo->lpszSourceUrlName;
  121. LPSTR lpszFileName = lpCacheEntryInfo->lpszLocalFileName;
  122. TCHAR szBuffer[] = "<HTML><TITLE>Test</TITLE><BODY>Testing</BODY></HTML>";
  123. HANDLE hWrite;
  124. DWORD dwWritten;
  125. DWORD dwExpectedSize;
  126. printf ("%s\n", lpszFileName);
  127. // Now monkey around with the local file (lpszFileName)
  128. hWrite = CreateFile(lpszFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  129. if (hWrite == INVALID_HANDLE_VALUE) {
  130. printf ("CreateFile failed: %x\n", GetLastError());
  131. return GetLastError();
  132. }
  133. WriteFile(hWrite, &szBuffer, sizeof(szBuffer), &dwWritten, NULL);
  134. if (sizeof(szBuffer) != dwWritten) {
  135. printf ("WriteFile failed: %x\n", GetLastError());
  136. return GetLastError();
  137. }
  138. dwExpectedSize = GetFileSize(hWrite, NULL);
  139. CloseHandle(hWrite);
  140. // Commit cache entry by calling CommitUrlCacheEntry
  141. if ( !CommitUrlCacheEntryA(lpszUrlName,
  142. lpszFileName,
  143. lpCacheEntryInfo->ExpireTime, /* expire time */
  144. lpCacheEntryInfo->LastModifiedTime, /* last modified time */
  145. NORMAL_CACHE_ENTRY,
  146. NULL,
  147. 0,
  148. NULL,
  149. NULL)) {
  150. printf ("CommitUrlCacheEntry failed: %x\n", GetLastError());
  151. return GetLastError();
  152. }
  153. return ERROR_SUCCESS;
  154. }
  155. // getting the content of the a cache entry into memory (i.e. don't need to write to disk)
  156. DWORD TestRetrieveCacheEntryToMemory(LPINTERNET_CACHE_ENTRY_INFO lpCacheEntryInfo) {
  157. DWORD dwLen;
  158. DWORD szFileSize = lpCacheEntryInfo->dwSizeLow;
  159. DWORD dwCurLocation = 0;
  160. TCHAR lpszUrlName[MAX_URL_LENGTH];
  161. HANDLE hCacheEntryStream;
  162. LPSTR pszBuffer;
  163. DWORD dwEntryBufferSize = CACHE_ENTRY_BUFFER_SIZE;
  164. strncpy(lpszUrlName, lpCacheEntryInfo->lpszSourceUrlName, MAX_URL_LENGTH);
  165. if ((hCacheEntryStream = RetrieveUrlCacheEntryStreamA(lpszUrlName, lpCacheEntryInfo,
  166. &dwEntryBufferSize, TRUE, 0)) == NULL)
  167. {
  168. return GetLastError();
  169. }
  170. // srand( (unsigned)time(NULL));
  171. //while (TRUE) {
  172. dwLen = szFileSize;
  173. pszBuffer = new char[dwLen+1];
  174. ZeroMemory(pszBuffer, dwLen+1);
  175. if (ReadUrlCacheEntryStream(hCacheEntryStream, dwCurLocation, (LPVOID)pszBuffer, &dwLen, 0)) {
  176. printf ("%s\n\n", pszBuffer);
  177. delete pszBuffer;
  178. }
  179. else
  180. {
  181. delete pszBuffer;
  182. dwCurLocation += dwLen;
  183. if (dwCurLocation == szFileSize)
  184. return GetLastError();
  185. _ASSERT(0);
  186. }
  187. //}
  188. // close the cache entry stream handle
  189. UnlockUrlCacheEntryStream(hCacheEntryStream, 0);
  190. return ERROR_SUCCESS;
  191. }
  192. DWORD TestGetUrlCacheEntryInfo(LPINTERNET_CACHE_ENTRY_INFO lpCacheEntryInfo) {
  193. TCHAR TimeBuffer[DEFAULT_BUFFER_SIZE];
  194. LPTSTR Tab = _T( "" );
  195. if (!(lpCacheEntryInfo->CacheEntryType & COOKIE_CACHE_ENTRY) &&
  196. !(lpCacheEntryInfo->CacheEntryType & URLHISTORY_CACHE_ENTRY)) {
  197. printf("\n\n------------------------------------------------------------------\n");
  198. _tprintf( _T( "%sUrlName = %s\n" ), Tab, lpCacheEntryInfo->lpszSourceUrlName );
  199. #if UNICODE
  200. _tprintf( _T( "%sLocalFileName = %ws\n" ), Tab, lpCacheEntryInfo->lpszLocalFileName );
  201. #else
  202. _tprintf( _T( "%sLocalFileName = %s\n" ), Tab, lpCacheEntryInfo->lpszLocalFileName );
  203. #endif
  204. _tprintf( _T( "%sdwStructSize = %lx\n" ), Tab, lpCacheEntryInfo->dwStructSize );
  205. _tprintf( _T( "%sCacheEntryType = %lx\n" ), Tab, lpCacheEntryInfo->CacheEntryType );
  206. _tprintf( _T( "%sUseCount = %ld\n" ), Tab, lpCacheEntryInfo->dwUseCount );
  207. _tprintf( _T( "%sHitRate = %ld\n" ), Tab, lpCacheEntryInfo->dwHitRate );
  208. _tprintf( _T( "%sSize = %ld:%ld\n" ), Tab, lpCacheEntryInfo->dwSizeLow, lpCacheEntryInfo->dwSizeHigh );
  209. _tprintf( _T( "%sLastModifiedTime = %s\n" ), Tab, ConvertGmtTimeToString( lpCacheEntryInfo->LastModifiedTime, TimeBuffer) );
  210. _tprintf( _T( "%sExpireTime = %s\n" ), Tab, ConvertGmtTimeToString( lpCacheEntryInfo->ExpireTime, TimeBuffer) );
  211. _tprintf( _T( "%sLastAccessTime = %s\n" ), Tab, ConvertGmtTimeToString( lpCacheEntryInfo->LastAccessTime, TimeBuffer) );
  212. _tprintf( _T( "%sLastSyncTime = %s\n" ), Tab, ConvertGmtTimeToString( lpCacheEntryInfo->LastSyncTime, TimeBuffer) );
  213. #if 1
  214. _tprintf( _T( "%sHeaderInfo = \n%s\n" ), Tab, lpCacheEntryInfo->lpHeaderInfo );
  215. #endif
  216. _tprintf( _T( "%sHeaderInfoSize = %ld\n" ), Tab, lpCacheEntryInfo->dwHeaderInfoSize );
  217. #if UNICODE
  218. _tprintf( _T( "%sFileExtension = %ws\n" ), Tab, lpCacheEntryInfo->lpszFileExtension );
  219. #else
  220. _tprintf( _T( "%sFileExtension = %s\n" ), Tab, lpCacheEntryInfo->lpszFileExtension );
  221. #endif
  222. }
  223. return ERROR_SUCCESS;
  224. }
  225. // =============================================================================
  226. typedef DWORD (*FPTestEnumerateCache)(LPINTERNET_CACHE_ENTRY_INFO);
  227. /* +++
  228. TestEnumerateCache
  229. Purpose:
  230. Enumerate the persistent URL cache entry in the system.
  231. Parameters:
  232. [in] count - number of iterations to enumerate. If set to 0 than enumerate all
  233. ptrfunc - a the function pointer to call for each cache entry. If set to NULL then
  234. no functions are called
  235. Return Value:
  236. ERROR_SUCCESS if call succeeds. Otherwise return GetLastError()
  237. --- */
  238. DWORD TestEnumerateCache(DWORD dwCount, FPTestEnumerateCache pfTestEnumerateCache) {
  239. LPINTERNET_CACHE_ENTRY_INFO lpCacheEntryInfo;
  240. DWORD CacheEntryInfoBufferSize;
  241. DWORD dwBufferSize;
  242. HANDLE EnumHandle;
  243. DWORD dwIndex = 0;
  244. TCHAR UrlName[1024];
  245. DWORD dwError = ERROR_SUCCESS;
  246. do
  247. {
  248. memset(GlobalCacheEntryInfoBuffer, 0, CACHE_ENTRY_BUFFER_SIZE);
  249. dwBufferSize = CACHE_ENTRY_BUFFER_SIZE;
  250. if( dwIndex++ == 0)
  251. {
  252. EnumHandle = FindFirstUrlCacheEntryEx (
  253. NULL, // search pattern
  254. 0, // flags
  255. 0xffffffff, // filter
  256. 0, // groupid
  257. (LPINTERNET_CACHE_ENTRY_INFO)GlobalCacheEntryInfoBuffer,
  258. &dwBufferSize,
  259. NULL,
  260. NULL,
  261. NULL
  262. );
  263. if( EnumHandle == NULL ) {
  264. return( GetLastError() );
  265. }
  266. } else {
  267. if( !FindNextUrlCacheEntryEx(
  268. EnumHandle,
  269. (LPINTERNET_CACHE_ENTRY_INFO)GlobalCacheEntryInfoBuffer,
  270. &dwBufferSize, NULL, NULL, NULL))
  271. {
  272. dwError = GetLastError();
  273. if( dwError != ERROR_NO_MORE_ITEMS )
  274. return( dwError );
  275. break;
  276. }
  277. }
  278. // now we've got the URL entry, do something about it
  279. strcpy(UrlName, ((LPINTERNET_CACHE_ENTRY_INFO)GlobalCacheEntryInfoBuffer)->lpszSourceUrlName);
  280. lpCacheEntryInfo = (LPINTERNET_CACHE_ENTRY_INFO)GlobalCacheEntryInfoBuffer;
  281. CacheEntryInfoBufferSize = CACHE_ENTRY_BUFFER_SIZE;
  282. // clean up the block of memory
  283. memset(GlobalCacheEntryInfoBuffer, 0, CACHE_ENTRY_BUFFER_SIZE);
  284. _ASSERT(UrlName);
  285. // put cache entry info into lpCacheEntryInfo
  286. if (GetUrlCacheEntryInfoA(UrlName,
  287. lpCacheEntryInfo,
  288. &CacheEntryInfoBufferSize ))
  289. {
  290. if (pfTestEnumerateCache(lpCacheEntryInfo) != ERROR_SUCCESS)
  291. printf ("Function pointer call failed\n");
  292. }
  293. }
  294. while (dwCount == 0 || (dwCount != 0 && dwIndex < dwCount));
  295. FindCloseUrlCache(EnumHandle);
  296. return TRUE;
  297. }
  298. // =============================================================================
  299. /* +++
  300. Sets of test cases to execute directly from main()
  301. ---*/
  302. void TestCase1() {
  303. (void) TestEnumerateCache(0, (FPTestEnumerateCache)TestGetUrlCacheEntryInfo);
  304. //(void) TestEnumerateCache(1, (FPTestEnumerateCache)TestCommitCacheEntryFromInet);
  305. }
  306. void TestCase2() {
  307. (void) TestCommitCacheEntryWithoutInet();
  308. }
  309. // test retrieving the cache entry to memory (i.e. don't need a file) incrementally using the Wininet API
  310. void TestCase3() {
  311. (void) TestEnumerateCache(4, (FPTestEnumerateCache)TestRetrieveCacheEntryToMemory);
  312. }
  313. void TestCase4() {
  314. (void) TestEnumerateCache(1, (FPTestEnumerateCache)TestCommitCacheEntryFromInet);
  315. }
  316. void __cdecl main() {
  317. // HACK: At the time being, DLLUrlCacheEntry has to be called explicitly during initialization and termination,
  318. // since the urlcache component is not attached to WinHttp's DLL hooks yet.
  319. // We'll remove this constraint when the cache component becomes part of WinHTTP
  320. DLLUrlCacheEntry(DLL_PROCESS_ATTACH);
  321. TestCase1();
  322. DLLUrlCacheEntry(DLL_PROCESS_DETACH);
  323. }