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.

357 lines
7.2 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1993.
  5. //
  6. // File: midl.cxx
  7. //
  8. // Contents: MIDL memory allocation tests
  9. //
  10. // Functions:
  11. //
  12. // History: 29-Sep-93 CarlH Created
  13. //
  14. //--------------------------------------------------------------------------
  15. #include "memtest.hxx"
  16. #pragma hdrstop
  17. #include <rpc.h>
  18. #include "test.h"
  19. static const char g_szMIDLClient[] = "midl";
  20. static const char g_szMIDLServer[] = "server";
  21. static const WCHAR g_wszMIDLSignal[] = L"testsignal";
  22. static const WCHAR g_wszStopSignal[] = L"stopsignal";
  23. static const DWORD g_ccallMin = 1;
  24. static const DWORD g_ccallMax = 1;
  25. static CSignal g_sigStop(g_wszStopSignal);
  26. BYTE g_ab0[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  27. BYTE g_ab1[] = {0x00, 0xFF};
  28. BYTE g_ab2[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  29. BYTE g_ab3[] = {0x69};
  30. SCountedBytes g_acbIn[] =
  31. {
  32. {(sizeof(g_ab0) / sizeof(g_ab0[0])), g_ab0},
  33. {(sizeof(g_ab1) / sizeof(g_ab1[0])), g_ab1},
  34. {(sizeof(g_ab2) / sizeof(g_ab2[0])), g_ab2},
  35. {(sizeof(g_ab3) / sizeof(g_ab3[0])), g_ab3}
  36. };
  37. SCountedCountedBytes g_ccbIn =
  38. {
  39. sizeof(g_acbIn) / sizeof(g_acbIn[0]),
  40. g_acbIn
  41. };
  42. DWORD StartServer(WCHAR *pwszServer, DWORD grfOptions);
  43. DWORD ConnectServer(void);
  44. DWORD DisconnectServer(void);
  45. BOOL CompareBytes(SCountedCountedBytes *pccbA, SCountedCountedBytes *pccbB);
  46. BOOL FreeBytes(SCountedCountedBytes *pccb);
  47. BOOL TestMIDLClient(WCHAR *pwszServer, DWORD grfOptions)
  48. {
  49. CSignal sig(g_wszMIDLSignal);
  50. SCountedCountedBytes ccbOut = {0, NULL};
  51. RPC_STATUS stat;
  52. BOOL fPassed;
  53. PrintHeader(g_szMIDLClient);
  54. PrintTrace(g_szMIDLClient, "starting server\n");
  55. stat = StartServer(pwszServer, grfOptions);
  56. if (!(fPassed = (stat == 0)))
  57. goto done;
  58. PrintTrace(g_szMIDLClient, "waiting for server\n");
  59. stat = sig.Wait(grfOptions & MIDL_DEBUG ? INFINITE : 10000);
  60. if (!(fPassed = (stat == 0)))
  61. goto done;
  62. PrintTrace(g_szMIDLClient, "connecting to server\n");
  63. stat = ConnectServer();
  64. if (!(fPassed = (stat == 0)))
  65. goto done;
  66. PrintTrace(g_szMIDLClient, "copying bytes\n");
  67. fPassed = _CopyBytes(&g_ccbIn, &ccbOut);
  68. if (!fPassed)
  69. goto done;
  70. PrintTrace(g_szMIDLClient, "stopping server\n");
  71. fPassed = _StopServer();
  72. if (!fPassed)
  73. goto done;
  74. PrintTrace(g_szMIDLClient, "comparing bytes\n");
  75. fPassed = CompareBytes(&g_ccbIn, &ccbOut);
  76. if (!fPassed)
  77. goto done;
  78. PrintTrace(g_szMIDLClient, "freeing bytes\n");
  79. fPassed = FreeBytes(&ccbOut);
  80. if (!fPassed)
  81. goto done;
  82. PrintTrace(g_szMIDLClient, "disconnecting from server\n");
  83. stat = DisconnectServer();
  84. if (!(fPassed = (stat == 0)))
  85. goto done;
  86. done:
  87. PrintResult(g_szMIDLClient, fPassed);
  88. return (fPassed);
  89. }
  90. BOOL TestMIDLServer(DWORD grfOptions)
  91. {
  92. CSignal sig(g_wszMIDLSignal);
  93. RPC_STATUS stat;
  94. BOOL fPassed;
  95. PrintHeader(g_szMIDLServer);
  96. PrintTrace(g_szMIDLServer, "registering protocol\n");
  97. stat = RpcServerUseAllProtseqsIf(g_ccallMax, Test_ServerIfHandle, NULL);
  98. if (!(fPassed = (stat == 0)))
  99. goto done;
  100. PrintTrace(g_szMIDLServer, "registering interface\n");
  101. stat = RpcServerRegisterIf(Test_ServerIfHandle, NULL, NULL);
  102. if (!(fPassed = (stat == 0)))
  103. goto done;
  104. #ifdef NO_OLE_RPC
  105. PrintTrace(g_szMIDLServer, "listening\n");
  106. stat = RpcServerListen(
  107. g_ccallMin,
  108. g_ccallMax,
  109. TRUE);
  110. if (!(fPassed = ((stat == 0) || (stat == RPC_S_ALREADY_LISTENING))))
  111. goto done;
  112. #endif // NO_OLE_RPC
  113. PrintTrace(g_szMIDLServer, "signaling client\n");
  114. stat = sig.Signal();
  115. if (!(fPassed = (stat == 0)))
  116. goto done;
  117. PrintTrace(g_szMIDLServer, "waiting...\n");
  118. #ifdef NO_OLE_RPC
  119. stat = RpcMgmtWaitServerListen();
  120. if (!(fPassed = ((stat == 0) || (stat == RPC_S_ALREADY_LISTENING))))
  121. goto done;
  122. #else
  123. g_sigStop.Wait();
  124. #endif // NO_OLE_RPC
  125. done:
  126. PrintResult(g_szMIDLServer, fPassed);
  127. return (fPassed);
  128. }
  129. boolean CopyBytes(SCountedCountedBytes *pccbIn, SCountedCountedBytes *pccbOut)
  130. {
  131. HRESULT hr;
  132. BOOL fPassed;
  133. long ccb = pccbIn->ccb;
  134. long icb;
  135. PrintTrace(g_szMIDLServer, "copying bytes\n");
  136. pccbOut->ccb = ccb;
  137. pccbOut->pcb = (SCountedBytes *)CoTaskMemAlloc(ccb * sizeof(*pccbOut->pcb));
  138. if (!(fPassed = (pccbOut != 0)))
  139. goto done;
  140. for (icb = 0; icb < ccb; icb++)
  141. {
  142. long cb = pccbIn->pcb[icb].cb;
  143. pccbOut->pcb[icb].cb = cb;
  144. pccbOut->pcb[icb].pb = (byte *)CoTaskMemAlloc(cb);
  145. if (!(fPassed = (pccbOut->pcb[icb].pb != 0)))
  146. goto done;
  147. memcpy(pccbOut->pcb[icb].pb, pccbIn->pcb[icb].pb, cb);
  148. }
  149. done:
  150. return (fPassed);
  151. }
  152. BOOL CompareBytes(SCountedCountedBytes *pccbA, SCountedCountedBytes *pccbB)
  153. {
  154. BOOL fPassed;
  155. long icb;
  156. if (!(fPassed = (pccbA->ccb == pccbB->ccb)))
  157. goto done;
  158. for (icb = 0; icb < pccbA->ccb; icb++)
  159. {
  160. if (!(fPassed = (pccbA->pcb[icb].cb == pccbB->pcb[icb].cb)))
  161. goto done;
  162. int cmp = memcmp(
  163. pccbA->pcb[icb].pb,
  164. pccbB->pcb[icb].pb,
  165. pccbB->pcb[icb].cb);
  166. if (!(fPassed = (cmp == 0)))
  167. goto done;
  168. }
  169. done:
  170. return (fPassed);
  171. }
  172. BOOL FreeBytes(SCountedCountedBytes *pccb)
  173. {
  174. BOOL fPassed = TRUE;
  175. for (long icb = 0; icb < pccb->ccb; icb++)
  176. {
  177. CoTaskMemFree(pccb->pcb[icb].pb);
  178. }
  179. CoTaskMemFree(pccb->pcb);
  180. done:
  181. return (fPassed);
  182. }
  183. boolean StopServer(void)
  184. {
  185. BOOL fPassed;
  186. PrintTrace(g_szMIDLServer, "stopping\n");
  187. #ifdef NO_OLE_RPC
  188. fPassed = (RpcMgmtStopServerListening(NULL) == 0);
  189. #else
  190. g_sigStop.Signal();
  191. fPassed = TRUE;
  192. #endif // NO_OLE_RPC
  193. return (fPassed);
  194. }
  195. DWORD StartServer(WCHAR *pwszServer, DWORD grfOptions)
  196. {
  197. WCHAR wszCommandLine[MAX_PATH + 1];
  198. ULONG cwchDebug;
  199. BOOL fOK;
  200. DWORD stat;
  201. if (grfOptions & MIDL_DEBUG)
  202. {
  203. cwchDebug = wsprintf(
  204. wszCommandLine,
  205. L"ntsd %ws %ws ",
  206. (grfOptions & MIDL_AUTOGO ? L"-g" : L""),
  207. (grfOptions & MIDL_AUTOEND ? L"-G" : L""));
  208. }
  209. else
  210. {
  211. cwchDebug = 0;
  212. }
  213. wsprintf(
  214. wszCommandLine + cwchDebug,
  215. L"%ws midlserver %cc %cs %cv",
  216. pwszServer,
  217. (grfOptions & GLOBAL_CLEANUP ? '+' : '-'),
  218. (grfOptions & GLOBAL_STATUS ? '+' : '-'),
  219. (grfOptions & GLOBAL_VERBOSE ? '+' : '-'));
  220. PrintTrace(g_szMIDLClient, "server command line: %ws\n", wszCommandLine);
  221. STARTUPINFO sui;
  222. PROCESS_INFORMATION pi;
  223. sui.cb = sizeof(sui);
  224. sui.lpReserved = 0;
  225. sui.lpDesktop = 0;
  226. sui.lpTitle = wszCommandLine;
  227. sui.dwFlags = 0;
  228. sui.cbReserved2 = 0;
  229. sui.lpReserved2 = 0;
  230. fOK = CreateProcess(
  231. NULL,
  232. wszCommandLine,
  233. NULL,
  234. NULL,
  235. FALSE,
  236. CREATE_NEW_CONSOLE,
  237. NULL,
  238. NULL,
  239. &sui,
  240. &pi);
  241. if (fOK)
  242. {
  243. CloseHandle(pi.hThread);
  244. CloseHandle(pi.hProcess);
  245. stat = 0;
  246. }
  247. else
  248. {
  249. stat = GetLastError();
  250. }
  251. return (stat);
  252. }
  253. DWORD ConnectServer(void)
  254. {
  255. RPC_STATUS stat;
  256. WCHAR *pwszBinding;
  257. stat = RpcStringBindingCompose(
  258. NULL,
  259. PWSZ_PROTOCOL,
  260. NULL,
  261. PWSZ_ENDPOINT,
  262. NULL,
  263. &pwszBinding);
  264. if (stat == 0)
  265. {
  266. stat = RpcBindingFromStringBinding(pwszBinding, &g_hbindTest);
  267. RpcStringFree(&pwszBinding);
  268. }
  269. return (stat);
  270. }
  271. DWORD DisconnectServer(void)
  272. {
  273. RPC_STATUS stat;
  274. stat = RpcBindingFree(&g_hbindTest);
  275. return (stat);
  276. }