Source code of Windows XP (NT5)
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.

556 lines
9.6 KiB

  1. /*++
  2. Copyright (c) 1993 Microsoft Corporation
  3. Module Name:
  4. tconnect.c
  5. Abstract:
  6. Test for workstation connection APIs.
  7. Author:
  8. Rita Wong (ritaw) 17-Feb-1993
  9. Environment:
  10. User Mode - Win32
  11. Revision History:
  12. --*/
  13. #ifndef UNICODE
  14. #define UNICODE
  15. #endif
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <nt.h>
  19. #include <ntrtl.h>
  20. #include <nturtl.h>
  21. #include <windef.h>
  22. #include <winbase.h>
  23. #include <winnetwk.h>
  24. #include <npapi.h>
  25. #define REDIR_NOT_READY
  26. DWORD
  27. TestCreateConnection(
  28. IN LPWSTR LocalName OPTIONAL,
  29. IN LPWSTR RemoteName,
  30. IN LPWSTR Password OPTIONAL,
  31. IN LPWSTR UserName OPTIONAL,
  32. IN DWORD ExpectedError
  33. );
  34. DWORD
  35. TestDeleteConnection(
  36. IN LPWSTR ConnectionName,
  37. IN BOOL ForceFlag,
  38. IN DWORD ExpectedError
  39. );
  40. DWORD
  41. TestOpenEnum(
  42. IN DWORD Scope,
  43. IN LPNETRESOURCEW NetR OPTIONAL,
  44. OUT LPHANDLE EnumHandle,
  45. IN DWORD ExpectedError
  46. );
  47. DWORD
  48. TestEnum(
  49. IN HANDLE EnumHandle,
  50. IN DWORD EntriesRequested,
  51. IN LPVOID Buffer,
  52. IN DWORD BufferSize,
  53. OUT LPDWORD BytesNeeded,
  54. IN DWORD ExpectedError
  55. );
  56. VOID
  57. PrintNetResource(
  58. LPNETRESOURCE NetR
  59. );
  60. BYTE WorkBuffer[1024];
  61. BYTE WorkBuffer2[1024];
  62. void __cdecl
  63. main(
  64. int argc,
  65. char *argv[]
  66. )
  67. {
  68. DWORD status;
  69. LPWSTR LocalName;
  70. LPWSTR RemoteName;
  71. LPWSTR Password;
  72. LPWSTR UserName;
  73. NETRESOURCEW NetR;
  74. HANDLE EnumHandle;
  75. DWORD BytesNeeded;
  76. LocalName = L"E:";
  77. RemoteName = L"\\\\MyServerName\\A_Volume\\A_Directory";
  78. Password = L"MyPassword";
  79. UserName = L"MyUserName";
  80. TestCreateConnection(
  81. LocalName,
  82. RemoteName,
  83. Password,
  84. UserName,
  85. WN_SUCCESS
  86. );
  87. TestDeleteConnection(
  88. LocalName,
  89. TRUE,
  90. WN_SUCCESS
  91. );
  92. //-------------------------//
  93. TestCreateConnection(
  94. NULL,
  95. RemoteName,
  96. NULL,
  97. NULL,
  98. WN_SUCCESS
  99. );
  100. TestDeleteConnection(
  101. RemoteName,
  102. TRUE,
  103. WN_SUCCESS
  104. );
  105. //-------------------------//
  106. TestCreateConnection(
  107. L"LPT1",
  108. RemoteName,
  109. NULL,
  110. NULL,
  111. ERROR_INVALID_PARAMETER
  112. );
  113. //-------------------------//
  114. TestCreateConnection(
  115. LocalName,
  116. L"\\\\Server",
  117. NULL,
  118. NULL,
  119. ERROR_INVALID_NAME
  120. );
  121. //-------------------------//
  122. printf("\n");
  123. //-------------------------//
  124. #ifdef REDIR_NOT_READY
  125. if (argc == 2) {
  126. ANSI_STRING AStr;
  127. UNICODE_STRING UStr;
  128. RtlZeroMemory(WorkBuffer2, sizeof(WorkBuffer2));
  129. UStr.Buffer = WorkBuffer2;
  130. UStr.MaximumLength = sizeof(WorkBuffer2);
  131. RtlInitString(&AStr, argv[1]);
  132. RtlAnsiStringToUnicodeString(
  133. &UStr,
  134. &AStr,
  135. FALSE
  136. );
  137. NetR.lpRemoteName = UStr.Buffer;
  138. }
  139. else {
  140. NetR.lpRemoteName = L"lanman";
  141. }
  142. //-------------------------//
  143. TestOpenEnum(
  144. RESOURCE_GLOBALNET,
  145. &NetR,
  146. &EnumHandle,
  147. WN_SUCCESS
  148. );
  149. TestEnum(
  150. EnumHandle,
  151. 0xFFFFFFFF,
  152. WorkBuffer,
  153. sizeof(WorkBuffer),
  154. &BytesNeeded,
  155. WN_SUCCESS
  156. );
  157. TestEnum(
  158. EnumHandle,
  159. 0xFFFFFFFF,
  160. WorkBuffer,
  161. sizeof(WorkBuffer),
  162. &BytesNeeded,
  163. WN_NO_MORE_ENTRIES
  164. );
  165. (void) NPCloseEnum(EnumHandle);
  166. //-------------------------//
  167. printf("\n");
  168. //-------------------------//
  169. TestOpenEnum(
  170. RESOURCE_GLOBALNET,
  171. &NetR,
  172. &EnumHandle,
  173. WN_SUCCESS
  174. );
  175. TestEnum(
  176. EnumHandle,
  177. 0xFFFFFFFF,
  178. WorkBuffer,
  179. 200,
  180. &BytesNeeded,
  181. WN_SUCCESS
  182. );
  183. TestEnum(
  184. EnumHandle,
  185. 0xFFFFFFFF,
  186. WorkBuffer,
  187. sizeof(NETRESOURCEW) + 5,
  188. &BytesNeeded,
  189. WN_MORE_DATA
  190. );
  191. TestEnum(
  192. EnumHandle,
  193. 0xFFFFFFFF,
  194. WorkBuffer,
  195. BytesNeeded,
  196. &BytesNeeded,
  197. WN_SUCCESS
  198. );
  199. TestEnum(
  200. EnumHandle,
  201. 0xFFFFFFFF,
  202. WorkBuffer,
  203. sizeof(WorkBuffer),
  204. &BytesNeeded,
  205. WN_SUCCESS
  206. );
  207. TestEnum(
  208. EnumHandle,
  209. 0xFFFFFFFF,
  210. WorkBuffer,
  211. 0,
  212. &BytesNeeded,
  213. WN_NO_MORE_ENTRIES
  214. );
  215. (void) NPCloseEnum(EnumHandle);
  216. #else
  217. NetR.lpRemoteName = L"\\\\S";
  218. TestOpenEnum(
  219. RESOURCE_GLOBALNET,
  220. &NetR,
  221. &EnumHandle,
  222. WN_SUCCESS
  223. );
  224. //-------------------------//
  225. NetR.lpRemoteName = L"\\\\A Long Server Name";
  226. TestOpenEnum(
  227. RESOURCE_GLOBALNET,
  228. &NetR,
  229. &EnumHandle,
  230. WN_SUCCESS
  231. );
  232. //-------------------------//
  233. NetR.lpRemoteName = L"\\\\S\\";
  234. TestOpenEnum(
  235. RESOURCE_GLOBALNET,
  236. &NetR,
  237. &EnumHandle,
  238. ERROR_INVALID_NAME
  239. );
  240. //-------------------------//
  241. NetR.lpRemoteName = L"lanman";
  242. TestOpenEnum(
  243. RESOURCE_GLOBALNET,
  244. &NetR,
  245. &EnumHandle,
  246. ERROR_INVALID_NAME
  247. );
  248. //-------------------------//
  249. NetR.lpRemoteName = L"\\\\S\\Y";
  250. TestOpenEnum(
  251. RESOURCE_GLOBALNET,
  252. &NetR,
  253. &EnumHandle,
  254. WN_SUCCESS
  255. );
  256. //-------------------------//
  257. NetR.lpRemoteName = L"\\\\Server\\Volume\\Dir";
  258. TestOpenEnum(
  259. RESOURCE_GLOBALNET,
  260. &NetR,
  261. &EnumHandle,
  262. WN_SUCCESS
  263. );
  264. #endif
  265. }
  266. DWORD
  267. TestCreateConnection(
  268. IN LPWSTR LocalName OPTIONAL,
  269. IN LPWSTR RemoteName,
  270. IN LPWSTR Password OPTIONAL,
  271. IN LPWSTR UserName OPTIONAL,
  272. IN DWORD ExpectedError
  273. )
  274. {
  275. DWORD status;
  276. NETRESOURCEW NetR;
  277. printf("\nTestCreateConnection: Local %ws, Remote %ws", LocalName, RemoteName);
  278. if (ARGUMENT_PRESENT(UserName)) {
  279. printf(" UserName %ws", UserName);
  280. }
  281. if (ARGUMENT_PRESENT(Password)) {
  282. printf(" Password %ws", Password);
  283. }
  284. printf("\n");
  285. NetR.lpLocalName = LocalName;
  286. NetR.lpRemoteName = RemoteName;
  287. NetR.dwType = RESOURCETYPE_ANY;
  288. status = NPAddConnection(
  289. &NetR,
  290. Password,
  291. UserName
  292. );
  293. if (status != WN_SUCCESS) {
  294. status = GetLastError();
  295. }
  296. if (status != ExpectedError) {
  297. printf(" FAILED: expected %lu got %lu\n", ExpectedError, status);
  298. }
  299. else {
  300. printf(" SUCCESS: got %lu as expected\n", status);
  301. }
  302. return status;
  303. }
  304. DWORD
  305. TestDeleteConnection(
  306. IN LPWSTR ConnectionName,
  307. IN BOOL ForceFlag,
  308. IN DWORD ExpectedError
  309. )
  310. {
  311. DWORD status;
  312. printf("\nTestDeleteConnection: Connection %ws, ForceFlag %u\n",
  313. ConnectionName, ForceFlag);
  314. status = NPCancelConnection(
  315. ConnectionName,
  316. ForceFlag
  317. );
  318. if (status != WN_SUCCESS) {
  319. status = GetLastError();
  320. }
  321. if (status != ExpectedError) {
  322. printf(" FAILED: expected %lu got %lu\n", ExpectedError, status);
  323. }
  324. else {
  325. printf(" SUCCESS: got %lu as expected\n", status);
  326. }
  327. return status;
  328. }
  329. DWORD
  330. TestOpenEnum(
  331. IN DWORD Scope,
  332. IN LPNETRESOURCEW NetR OPTIONAL,
  333. OUT LPHANDLE EnumHandle,
  334. IN DWORD ExpectedError
  335. )
  336. {
  337. DWORD status;
  338. if (NetR != NULL) {
  339. printf("\nTestOpenEnum: Remote %ws\n", NetR->lpRemoteName);
  340. }
  341. status = NPOpenEnum(
  342. Scope,
  343. 0,
  344. 0,
  345. NetR,
  346. EnumHandle
  347. );
  348. if (status != WN_SUCCESS) {
  349. status = GetLastError();
  350. }
  351. if (status != ExpectedError) {
  352. printf(" FAILED: expected %lu got %lu\n", ExpectedError, status);
  353. }
  354. else {
  355. printf(" SUCCESS: got %lu as expected\n", status);
  356. }
  357. return status;
  358. }
  359. DWORD
  360. TestEnum(
  361. IN HANDLE EnumHandle,
  362. IN DWORD EntriesRequested,
  363. IN LPVOID Buffer,
  364. IN DWORD BufferSize,
  365. OUT LPDWORD BytesNeeded,
  366. IN DWORD ExpectedError
  367. )
  368. {
  369. DWORD status;
  370. DWORD EntriesRead = EntriesRequested;
  371. DWORD i;
  372. LPNETRESOURCE NetR = Buffer;
  373. *BytesNeeded = BufferSize;
  374. printf("\nTestEnum: EntriesRequested x%08lx, BufferSize %lu\n",
  375. EntriesRead, *BytesNeeded);
  376. status = NPEnumResource(
  377. EnumHandle,
  378. &EntriesRead,
  379. Buffer,
  380. BytesNeeded
  381. );
  382. if (status == WN_SUCCESS) {
  383. printf(" EntriesRead is %lu\n", EntriesRead);
  384. for (i = 0; i < EntriesRead; i++, NetR++) {
  385. PrintNetResource(NetR);
  386. }
  387. }
  388. else if (status != WN_NO_MORE_ENTRIES) {
  389. status = GetLastError();
  390. if (status == WN_MORE_DATA) {
  391. printf(" BytesNeeded is %lu\n", *BytesNeeded);
  392. }
  393. }
  394. if (status != ExpectedError) {
  395. printf(" FAILED: expected %lu got %lu\n", ExpectedError, status);
  396. }
  397. else {
  398. printf(" SUCCESS: got %lu as expected\n", status);
  399. }
  400. return status;
  401. }
  402. VOID
  403. PrintNetResource(
  404. LPNETRESOURCE NetR
  405. )
  406. {
  407. if (NetR->lpLocalName != NULL) {
  408. printf("%-7ws", NetR->lpLocalName);
  409. }
  410. printf(" %-ws\n", NetR->lpRemoteName);
  411. if (NetR->lpComment != NULL) {
  412. printf(" %-ws\n", NetR->lpComment);
  413. }
  414. if (NetR->lpProvider != NULL) {
  415. printf(" %-ws\n", NetR->lpProvider);
  416. }
  417. printf("Scope: x%lx", NetR->dwScope);
  418. printf(" Type: x%lx", NetR->dwType);
  419. printf(" DisplayType: x%lx", NetR->dwDisplayType);
  420. printf(" Usage: x%lx\n", NetR->dwUsage);
  421. }