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.

418 lines
8.7 KiB

  1. //////////////////////////////////////////////////////////////////////
  2. // File: AuthSecureProxyStress.cpp
  3. //
  4. // Copyright (c) 2001 Microsoft Corporation. All Rights Reserved.
  5. //
  6. // Purpose:
  7. // This file contains your implementation of the stress test function
  8. // WinHttp_StressTest() that is called in stressMain.cpp.
  9. //
  10. // Steps:
  11. // - Set your test case name in g_szStressTestName.
  12. // - Add your test code to WinHttp_StressTest().
  13. //
  14. // History:
  15. // 04/02/01 adamb Created
  16. //
  17. //////////////////////////////////////////////////////////////////////
  18. //////////////////////////////////////////////////////////////////////
  19. // Includes
  20. //////////////////////////////////////////////////////////////////////
  21. #include "stressMain.h"
  22. //////////////////////////////////////////////////////////////////////
  23. // Globals and constants
  24. //////////////////////////////////////////////////////////////////////
  25. // ************************************
  26. // ************************************
  27. // ** Fill in your test case name below
  28. // **
  29. LPSTR g_szStressTestName = "AuthSecureProxy Stressor";
  30. //yup, they're all global
  31. BOOL bPost = FALSE;
  32. CHAR HttpType[10] = "HTTP";
  33. BOOL bViaProxy = FALSE;
  34. CHAR CredType[5] = "SC";
  35. BOOL RunStress(BOOL bPost,CHAR HttpType[],BOOL bViaProxy,CHAR CredType[],int Scheme);
  36. ////////////////////////////////////////////////////////////
  37. // Function: WinHttp_StressTest()
  38. //
  39. // Purpose:
  40. // goes through all the ways of sending a request and
  41. // picks one of those, then uses it in a request
  42. //
  43. // yes, it does check for the signal before starting
  44. // runstress (one test per call).
  45. ////////////////////////////////////////////////////////////
  46. BOOL
  47. WinHttp_StressTest()
  48. {
  49. for(int i=0; i<2; i++)
  50. {
  51. if(i==0)
  52. bPost = FALSE;
  53. else
  54. bPost = TRUE;
  55. for(int j=0; j<2; j++)
  56. {
  57. if(j==0)
  58. strcpy(HttpType, "HTTPS");
  59. else
  60. strcpy(HttpType, "HTTP");
  61. for(int k=0; k<2; k++)
  62. {
  63. if(k==0)
  64. bViaProxy = FALSE;
  65. else
  66. bViaProxy = TRUE;
  67. for(int l=0; l<2; l++)
  68. {
  69. if(l==0)
  70. strcpy(CredType, "SC");
  71. else
  72. strcpy(CredType, "SO");
  73. for(int m=0; m<4; m++)
  74. {
  75. if(!IsTimeToExitStress())
  76. RunStress(bPost,HttpType,bViaProxy,CredType,m);
  77. else
  78. return FALSE;
  79. }
  80. }
  81. }
  82. }
  83. }
  84. return TRUE;
  85. }
  86. ////////////////////////////////////////////////////////////
  87. // Function: RunStress()
  88. //
  89. // Purpose:
  90. // this actually runs the tests given certain inputs.
  91. // this is called from WinHttp_StressTest.
  92. //
  93. ////////////////////////////////////////////////////////////
  94. BOOL RunStress(BOOL bPost,CHAR HttpType[],BOOL bViaProxy,CHAR CredType[],int Scheme)
  95. {
  96. BOOL bContinueStress = TRUE;
  97. HINTERNET hOpen = NULL;
  98. HINTERNET hConnect = NULL;
  99. HINTERNET hRequest = NULL;
  100. DWORD Count = 0, dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY,
  101. dwAuthScheme=0,dwAuthTargets=0,dwOtherScheme=0,dwOpenRequestFlags=0,
  102. dwStatus=0, cbStatus=0;
  103. LPWSTR wszHost=NULL, wszUri=NULL, wszUserName=NULL, wszPassword=NULL,
  104. wszProxy = NULL, wszProxyUserName = NULL, wszProxyPassword = NULL,
  105. wszVerb=L"GET";
  106. INTERNET_PORT nPort = INTERNET_DEFAULT_HTTP_PORT;
  107. LPSTR pPostData = NULL;
  108. DWORD dwPostDataLength = 0;
  109. if(bPost)
  110. {
  111. wszVerb=L"POST";
  112. pPostData = "If you smelllllllll what THE ROCK is cooking??? <people's eyebrow>";
  113. dwPostDataLength = strlen(pPostData);
  114. }
  115. if(strcmp(HttpType, "HTTPS"))
  116. {
  117. nPort = INTERNET_DEFAULT_HTTPS_PORT;
  118. dwOpenRequestFlags = WINHTTP_FLAG_SECURE;
  119. }
  120. //if going via proxy, then ntlm/nego aren't valid, unless going over https
  121. if(bViaProxy && ((Scheme == 0 || Scheme == 1) || strcmp(HttpType, "HTTPS")) )
  122. {
  123. wszProxy = L"xfluke";
  124. wszProxyUserName = L"xfluke\\proxyuser";
  125. wszProxyPassword = L"password";
  126. dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
  127. }
  128. switch(Scheme)
  129. {
  130. case 0: //basic
  131. wszHost = L"wiredbvt";
  132. wszUri = L"/api/Auth/Basic/echo-post-data.asp";
  133. wszUserName = L"ApiAuth";
  134. wszPassword = L"test1234!";
  135. break;
  136. case 1: //digest
  137. wszHost = L"kerby2";
  138. wszUri = L"/digest/echo-post-data.asp";
  139. wszUserName = L"authdigest";
  140. wszPassword = L"digest";
  141. break;
  142. case 2: //negotiate
  143. wszHost = L"kerby2";
  144. wszUri = L"/ie/negotiate/echo-post-data.asp";
  145. wszUserName = L"kerby2\\authnego";
  146. wszPassword = L"nego";
  147. break;
  148. case 3: //ntlm
  149. wszHost = L"clapton";
  150. wszUri = L"/test/ntlm/echo-post-data.asp";
  151. wszUserName = L"clapton\\ntlmtest";
  152. wszPassword = L"ntlm";
  153. break;
  154. }
  155. LogText("Post: %u, Proxy: %u, %s, %s, Scheme: %u", bPost, bViaProxy, HttpType, CredType, Scheme);
  156. // ***********************************
  157. // ** WinHttpOpen
  158. // **
  159. hOpen = WinHttpOpen
  160. (
  161. L"Stress Test",
  162. dwAccessType,
  163. wszProxy,
  164. NULL,
  165. 0
  166. );
  167. if(hOpen == NULL)
  168. {
  169. LogText("WinHttpOpen failed with error %u.", GetLastError());
  170. goto Exit;
  171. }
  172. // ***********************************
  173. // ** WinHttpConnect
  174. // **
  175. hConnect = WinHttpConnect
  176. (
  177. hOpen,
  178. wszHost,
  179. nPort,
  180. 0
  181. );
  182. if(hConnect==NULL)
  183. {
  184. LogText("WinHttpConnect failed with error %u.", GetLastError());
  185. goto Exit;
  186. }
  187. hRequest = WinHttpOpenRequest
  188. (
  189. hConnect,
  190. wszVerb,
  191. wszUri,
  192. NULL,
  193. NULL,
  194. NULL,
  195. dwOpenRequestFlags
  196. );
  197. if(hRequest==NULL)
  198. {
  199. LogText("WinHttpOpenRequest failed with error %u.", GetLastError());
  200. goto Exit;
  201. }
  202. Resend:
  203. if( Count++>3) // making sure that we don't have infinite looping
  204. {
  205. bContinueStress=FALSE;
  206. goto Exit;
  207. }
  208. // Send request.
  209. if(!WinHttpSendRequest
  210. (
  211. hRequest, // request handle
  212. NULL, // header string
  213. 0, // header length
  214. (PVOID) pPostData, // post data
  215. dwPostDataLength, // post data length
  216. dwPostDataLength, // total post length
  217. 0 // flags
  218. ))
  219. {
  220. LogText("WinHttpSendRequest failed with error %u.", GetLastError());
  221. goto Exit;
  222. }
  223. if (!WinHttpReceiveResponse(hRequest, NULL))
  224. {
  225. LogText("WinHttpReceiveResponse failed with error %u.", GetLastError());
  226. goto Exit;
  227. }
  228. cbStatus = sizeof(dwStatus);
  229. WinHttpQueryHeaders
  230. (
  231. hRequest,
  232. WINHTTP_QUERY_FLAG_NUMBER | WINHTTP_QUERY_STATUS_CODE,
  233. NULL,
  234. &dwStatus,
  235. &cbStatus,
  236. NULL
  237. );
  238. switch( dwStatus )
  239. {
  240. case 200:
  241. break;
  242. case 401:
  243. if(strcmp(CredType, "SC"))
  244. {
  245. if(!WinHttpQueryAuthSchemes
  246. (
  247. hRequest,
  248. &dwOtherScheme,
  249. &dwAuthScheme,
  250. &dwAuthTargets
  251. ))
  252. {
  253. LogText("WinHttpQueryAuthSchemes failed with error %u.", GetLastError());
  254. goto Exit;
  255. }
  256. if(!WinHttpSetCredentials
  257. (
  258. hRequest,
  259. dwAuthTargets,
  260. dwAuthScheme,
  261. wszUserName,
  262. wszPassword,
  263. (PVOID) NULL
  264. ))
  265. {
  266. LogText("WinHttpSetCredentials failed with error %u.", GetLastError());
  267. goto Exit;
  268. }
  269. }
  270. else
  271. {
  272. if(!WinHttpSetOption
  273. (
  274. hRequest,
  275. WINHTTP_OPTION_USERNAME,
  276. (PVOID) wszUserName,
  277. wcslen(wszUserName)
  278. ))
  279. {
  280. LogText("WinHttpSetOption failed with error %u.", GetLastError());
  281. goto Exit;
  282. }
  283. if(!WinHttpSetOption
  284. (
  285. hRequest,
  286. WINHTTP_OPTION_PASSWORD,
  287. (PVOID) wszPassword,
  288. wcslen(wszPassword)
  289. ))
  290. {
  291. LogText("WinHttpSetOption failed with error %u.", GetLastError());
  292. goto Exit;
  293. }
  294. }
  295. goto Resend;
  296. break;
  297. case 407:
  298. if(strcmp(CredType, "SC"))
  299. {
  300. if(!WinHttpQueryAuthSchemes
  301. (
  302. hRequest,
  303. &dwOtherScheme,
  304. &dwAuthScheme,
  305. &dwAuthTargets
  306. ))
  307. {
  308. LogText("WinHttpQueryAuthSchemes failed with error %u.", GetLastError());
  309. goto Exit;
  310. }
  311. if(!WinHttpSetCredentials
  312. (
  313. hRequest,
  314. dwAuthTargets,
  315. dwAuthScheme,
  316. wszProxyUserName,
  317. wszProxyPassword,
  318. (PVOID) NULL
  319. ))
  320. {
  321. LogText("WinHttpSetCredentials failed with error %u.", GetLastError());
  322. goto Exit;
  323. }
  324. }
  325. else
  326. {
  327. if(!WinHttpSetOption
  328. (
  329. hRequest,
  330. WINHTTP_OPTION_PROXY_USERNAME,
  331. (PVOID) wszProxyUserName,
  332. wcslen(wszProxyUserName)
  333. ))
  334. {
  335. LogText("WinHttpSetOption failed with error %u.", GetLastError());
  336. goto Exit;
  337. }
  338. if(!WinHttpSetOption
  339. (
  340. hRequest,
  341. WINHTTP_OPTION_PROXY_PASSWORD,
  342. (PVOID) wszProxyPassword,
  343. wcslen(wszProxyPassword)
  344. ))
  345. {
  346. LogText("WinHttpSetOption failed with error %u.", GetLastError());
  347. goto Exit;
  348. }
  349. }
  350. goto Resend;
  351. break;
  352. } //end of switch (status code)
  353. Exit:
  354. if( hRequest != NULL )
  355. {
  356. WinHttpCloseHandle(hRequest);
  357. hRequest = NULL;
  358. }
  359. if( hConnect != NULL )
  360. {
  361. WinHttpCloseHandle(hConnect);
  362. hConnect = NULL;
  363. }
  364. if( hOpen != NULL )
  365. {
  366. WinHttpCloseHandle(hOpen);
  367. hOpen = NULL;
  368. }
  369. return bContinueStress;
  370. }