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.

449 lines
9.2 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. wstinv.c
  5. Abstract:
  6. This module tests invalid parameters to NetUse APIs.
  7. Author:
  8. Rita Wong (ritaw) 12-Mar-1991
  9. Revision History:
  10. --*/
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #include <nt.h>
  15. #include <ntrtl.h>
  16. #include <nturtl.h>
  17. #include <winerror.h>
  18. #include <windef.h> // Win32 type definitions
  19. #include <winbase.h> // Win32 base API prototypes
  20. #include <lm.h> // LAN Man definitions
  21. #include <netdebug.h> // NetpDbgDisplayWksta()
  22. #include <tstring.h>
  23. VOID
  24. WsTestUse(
  25. VOID
  26. );
  27. VOID
  28. TestUseAdd(
  29. IN LPBYTE Buffer,
  30. IN DWORD ExpectedStatus
  31. );
  32. VOID
  33. TestUseDel(
  34. IN LPTSTR UseName,
  35. IN DWORD ForceLevel,
  36. IN DWORD ExpectedStatus
  37. );
  38. VOID
  39. TestUseEnum(
  40. DWORD PreferedMaximumLength,
  41. IN OUT LPDWORD ResumeHandle OPTIONAL
  42. );
  43. VOID
  44. TestUseGetInfo(
  45. LPTSTR UseName,
  46. DWORD ExpectedStatus
  47. );
  48. VOID
  49. PrintUseInfo(
  50. PUSE_INFO_1 UseInfo
  51. );
  52. VOID __cdecl
  53. main(
  54. int argc,
  55. char *argv[]
  56. )
  57. {
  58. WsTestUse();
  59. }
  60. VOID
  61. WsTestUse(
  62. VOID
  63. )
  64. {
  65. CHAR Buffer[1024];
  66. PUSE_INFO_2 UseInfo = (PUSE_INFO_2) Buffer;
  67. DWORD ResumeHandle = 0;
  68. DWORD i;
  69. //
  70. // Initialize string pointers. Local device points to the bottom
  71. // of Info 2 structure; Shared resource points to the middle of
  72. // buffer (away from everything so there's no chance of overwriting
  73. // or being overwritten.
  74. //
  75. UseInfo->ui2_local = (LPTSTR) ((DWORD) UseInfo + sizeof(USE_INFO_2));
  76. UseInfo->ui2_remote = (LPTSTR) &Buffer[601];
  77. UseInfo->ui2_password = NULL;
  78. UseInfo->ui2_username = NULL;
  79. UseInfo->ui2_domainname = NULL;
  80. UseInfo->ui2_asg_type = USE_DISKDEV;
  81. //
  82. // Add \\ritaw2\public
  83. //
  84. UseInfo->ui2_local = NULL;
  85. STRCPY(UseInfo->ui2_remote, L"\\\\ritaw2\\public");
  86. TestUseAdd(Buffer, NERR_Success);
  87. UseInfo->ui2_local = (LPTSTR) ((DWORD) UseInfo + sizeof(USE_INFO_2));
  88. //
  89. // Add &: \\ritaw2\public
  90. //
  91. STRCPY(UseInfo->ui2_local, L"&:");
  92. TestUseAdd(Buffer, ERROR_INVALID_PARAMETER);
  93. //
  94. // Add 5: \\ritaw2\public
  95. //
  96. STRCPY(UseInfo->ui2_local, L"5:");
  97. TestUseAdd(Buffer, ERROR_INVALID_PARAMETER);
  98. //
  99. // Add x: \\ritaw2\public\tmp
  100. //
  101. STRCPY(UseInfo->ui2_local, L"x:");
  102. STRCPY(UseInfo->ui2_remote, L"\\\\ritaw2\\public\\tmp");
  103. TestUseAdd(Buffer, ERROR_INVALID_PARAMETER);
  104. //
  105. // Add x: \\\
  106. //
  107. STRCPY(UseInfo->ui2_local, L"x:");
  108. STRCPY(UseInfo->ui2_remote, L"\\\\\\");
  109. TestUseAdd(Buffer, ERROR_INVALID_PARAMETER);
  110. //
  111. // Add *: \\ritaw2\testdir
  112. //
  113. STRCPY(UseInfo->ui2_local, L"*:");
  114. STRCPY(UseInfo->ui2_remote, L"\\\\ritaw2\\testdir");
  115. TestUseAdd(Buffer, ERROR_INVALID_PARAMETER);
  116. //
  117. // Get info
  118. //
  119. TestUseGetInfo(L"$:", NERR_UseNotFound);
  120. TestUseGetInfo(L"", NERR_UseNotFound);
  121. TestUseGetInfo(NULL, ERROR_INVALID_ADDRESS);
  122. TestUseGetInfo(L"\\\\ritaw2\\public\\tmp", NERR_UseNotFound);
  123. TestUseGetInfo(L"\\\\\\", NERR_UseNotFound);
  124. //
  125. // Delete %: USE_NOFORCE.
  126. //
  127. TestUseDel(
  128. L"%:",
  129. USE_NOFORCE,
  130. NERR_UseNotFound
  131. );
  132. //
  133. // Delete \\ritaw2\public with invalid force level.
  134. //
  135. TestUseDel(
  136. L"\\\\ritaw2\\public",
  137. 999,
  138. ERROR_INVALID_PARAMETER
  139. );
  140. //
  141. // Delete \\ritaw2\public USE_FORCE.
  142. //
  143. TestUseDel(
  144. L"\\\\ritaw2\\public",
  145. USE_FORCE,
  146. NERR_Success
  147. );
  148. //
  149. // Add prn: \\sparkle\laserjet
  150. //
  151. UseInfo->ui2_asg_type = USE_SPOOLDEV;
  152. STRCPY(UseInfo->ui2_local, L"prn");
  153. STRCPY(UseInfo->ui2_remote, L"\\\\sparkle\\laserjet");
  154. TestUseAdd(Buffer, NERR_Success);
  155. //
  156. // Add aux: \\sparkle\laserjet
  157. //
  158. UseInfo->ui2_asg_type = USE_CHARDEV;
  159. STRCPY(UseInfo->ui2_local, L"aux");
  160. TestUseAdd(Buffer, ERROR_BAD_DEV_TYPE);
  161. //
  162. // Add lpt1: \\ritaw2\laser, should get ERROR_ALREADY_ASSIGNED because prn:
  163. // is converted to lpt1.
  164. //
  165. UseInfo->ui2_asg_type = USE_SPOOLDEV;
  166. STRCPY(UseInfo->ui2_local, L"lpt1:");
  167. STRCPY(UseInfo->ui2_remote, L"\\\\ritaw2\\printer");
  168. TestUseAdd(Buffer, ERROR_ALREADY_ASSIGNED);
  169. //
  170. // Delete LPT1 USE_LOTS_OF_FORCE, should succeed
  171. //
  172. TestUseDel(
  173. L"prn:",
  174. USE_LOTS_OF_FORCE,
  175. NERR_Success
  176. );
  177. //
  178. // Bad device type
  179. //
  180. STRCPY(UseInfo->ui2_local, L"");
  181. STRCPY(UseInfo->ui2_remote, L"\\\\ritaw2\\public");
  182. UseInfo->ui2_asg_type = 12345678;
  183. TestUseAdd(Buffer, NERR_BadAsgType);
  184. }
  185. VOID
  186. TestUseAdd(
  187. IN LPBYTE Buffer,
  188. IN DWORD ExpectedStatus
  189. )
  190. {
  191. NET_API_STATUS status;
  192. DWORD ErrorParameter;
  193. status = NetUseAdd(
  194. NULL,
  195. 2,
  196. Buffer,
  197. &ErrorParameter
  198. );
  199. printf("NetUseAdd %-5ws %-25ws ", ((PUSE_INFO_2) Buffer)->ui2_local,
  200. ((PUSE_INFO_2) Buffer)->ui2_remote);
  201. if (status != ExpectedStatus) {
  202. printf("FAILED: Got %lu, expected %lu\n", status, ExpectedStatus);
  203. }
  204. else {
  205. printf("OK: Got expected status %lu\n", status);
  206. }
  207. if (status == ERROR_INVALID_PARAMETER) {
  208. printf("NetUseAdd parameter %lu is cause of ERROR_INVALID_PARAMETER\n",
  209. ErrorParameter);
  210. }
  211. }
  212. VOID
  213. TestUseDel(
  214. IN LPTSTR UseName,
  215. IN DWORD ForceLevel,
  216. IN DWORD ExpectedStatus
  217. )
  218. {
  219. NET_API_STATUS status;
  220. DWORD Level;
  221. PWCHAR Force[4] = {
  222. L"NOFORCE",
  223. L"FORCE",
  224. L"LOTS_OF_FORCE",
  225. L"INVALID FORCE"
  226. };
  227. if (ForceLevel > 2) {
  228. Level = 3;
  229. }
  230. else {
  231. Level = ForceLevel;
  232. }
  233. printf("NetUseDel %-17ws %-13ws ", UseName, Force[Level]);
  234. status = NetUseDel(
  235. NULL,
  236. UseName,
  237. ForceLevel
  238. );
  239. if (status != ExpectedStatus) {
  240. printf("FAILED: Got %lu, expected %lu\n", status, ExpectedStatus);
  241. }
  242. else {
  243. printf("OK: Got expected status %lu\n", status);
  244. }
  245. }
  246. VOID
  247. TestUseGetInfo(
  248. LPTSTR UseName,
  249. DWORD ExpectedStatus
  250. )
  251. {
  252. NET_API_STATUS status;
  253. PUSE_INFO_1 UseInfo;
  254. printf("NetUseGetInfo %-27ws ", UseName);
  255. status = NetUseGetInfo(
  256. NULL,
  257. UseName,
  258. 2,
  259. (LPBYTE *) &UseInfo
  260. );
  261. if (status != ExpectedStatus) {
  262. printf("FAILED: Got %lu, expected %lu\n", status, ExpectedStatus);
  263. }
  264. else {
  265. printf("OK: Got expected status %lu\n", status);
  266. }
  267. if (status == NERR_Success) {
  268. PrintUseInfo(UseInfo);
  269. NetApiBufferFree(UseInfo);
  270. }
  271. }
  272. VOID
  273. TestUseEnum(
  274. IN DWORD PreferedMaximumLength,
  275. IN OUT LPDWORD ResumeHandle OPTIONAL
  276. )
  277. {
  278. DWORD i;
  279. NET_API_STATUS status;
  280. DWORD EntriesRead,
  281. TotalEntries;
  282. PUSE_INFO_1 UseInfo, saveptr;
  283. if (ARGUMENT_PRESENT(ResumeHandle)) {
  284. printf("\nInput ResumeHandle=x%08lx\n", *ResumeHandle);
  285. }
  286. status = NetUseEnum(
  287. NULL,
  288. 1,
  289. (LPBYTE *) &UseInfo,
  290. PreferedMaximumLength,
  291. &EntriesRead,
  292. &TotalEntries,
  293. ResumeHandle
  294. );
  295. saveptr = UseInfo;
  296. if (status != NERR_Success && status != ERROR_MORE_DATA) {
  297. printf("NetUseEnum FAILED %lu\n", status);
  298. }
  299. else {
  300. printf("Return code from NetUseEnum %lu\n", status);
  301. printf("EntriesRead=%lu, TotalEntries=%lu\n",
  302. EntriesRead, TotalEntries);
  303. if (ARGUMENT_PRESENT(ResumeHandle)) {
  304. printf("Output ResumeHandle=x%08lx\n", *ResumeHandle);
  305. }
  306. for (i = 0; i < EntriesRead; i++, UseInfo++) {
  307. PrintUseInfo(UseInfo);
  308. }
  309. //
  310. // Free buffer allocated for us.
  311. //
  312. NetApiBufferFree(saveptr);
  313. }
  314. }
  315. VOID
  316. PrintUseInfo(
  317. PUSE_INFO_1 UseInfo
  318. )
  319. {
  320. switch(UseInfo->ui1_status) {
  321. case USE_OK:
  322. printf("OK ");
  323. break;
  324. case USE_PAUSED:
  325. printf("Paused ");
  326. break;
  327. case USE_SESSLOST:
  328. printf("Disconnected ");
  329. break;
  330. case USE_NETERR:
  331. printf("Net error ");
  332. break;
  333. case USE_CONN:
  334. printf("Connecting ");
  335. break;
  336. case USE_RECONN:
  337. printf("Reconnecting ");
  338. break;
  339. default:
  340. printf("Unknown ");
  341. }
  342. printf(" %-7ws %-25ws", UseInfo->ui1_local,
  343. UseInfo->ui1_remote);
  344. printf("usecount=%lu, refcount=%lu\n",
  345. UseInfo->ui1_usecount, UseInfo->ui1_refcount);
  346. }