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.

627 lines
16 KiB

  1. //
  2. // Client.cpp - client implementation
  3. //
  4. #include "precomp.h"
  5. #include "bidispl.h"
  6. #include "bidisplp.h"
  7. #ifndef MODULE
  8. #define MODULE "BIDITST:"
  9. #endif
  10. #ifdef DEBUG
  11. MODULE_DEBUG_INIT( DBG_ERROR | DBG_WARNING | DBG_TRACE | DBG_INFO , DBG_ERROR );
  12. #else
  13. MODULE_DEBUG_INIT( DBG_ERROR | DBG_WARNING | DBG_TRACE, DBG_ERROR );
  14. #endif
  15. VOID DbgBreakPoint (VOID)
  16. {
  17. }
  18. void DumpData (DWORD dwType, PBYTE pData, DWORD dwSize)
  19. {
  20. DBGMSG (DBG_INFO, ("dwType = %d, pData = %x, dwSize = %d\n", dwType, pData, dwSize)) ;
  21. DWORD col = 0;
  22. CHAR szBuf[512];
  23. szBuf[0] = 0;
  24. for (DWORD i = 0; i < dwSize; i++) {
  25. sprintf (szBuf + strlen (szBuf), "%02x ", pData[i]);
  26. if ((col ++) % 16 == 15) {
  27. DBGMSG (DBG_INFO, ("%s\n", szBuf));
  28. szBuf[0] = 0;
  29. }
  30. }
  31. DBGMSG (DBG_INFO, ("%s\n", szBuf));
  32. }
  33. void DumpOutputData (DWORD dwType, PBYTE pData, DWORD dwSize)
  34. {
  35. DWORD i, col = 0;
  36. CHAR szBuf[512];
  37. szBuf[0] = 0;
  38. printf ("\tdwType = %d, pData = %x, dwSize = %d\n", dwType, pData, dwSize);
  39. switch (dwType) {
  40. case BIDI_NULL:
  41. printf("\tBIDI_NULL\n");
  42. break;
  43. case BIDI_INT:
  44. printf ("\tBIDI_INT = %d\n", *(INT*)pData);
  45. break;
  46. case BIDI_FLOAT:
  47. printf ("\tBIDI_FLOAT = %f\n", *(FLOAT*)pData);
  48. break;
  49. case BIDI_BOOL:
  50. printf ("\tBIDI_BOOL = %d\n", *(BOOL*)pData);
  51. break;
  52. case BIDI_ENUM:
  53. case BIDI_TEXT:
  54. case BIDI_STRING:
  55. printf ("\tBIDI_STRING = |%ws|\n", pData);
  56. break;
  57. case BIDI_BLOB:
  58. printf ("\tBIDI_BLOB\n", pData);
  59. for (i = 0; i < dwSize; i++) {
  60. sprintf (szBuf + strlen (szBuf), "%02x ", pData[i]);
  61. if ((col ++) % 16 == 15) {
  62. printf ("%s\n", szBuf);
  63. szBuf[0] = 0;
  64. }
  65. }
  66. printf ("%s\n", szBuf);
  67. default:
  68. printf ("\tunsupported\n", pData);
  69. }
  70. }
  71. void DumpRequest (IBidiRequest * pIRequest)
  72. {
  73. DWORD dwTotoal, i;
  74. HRESULT hr, hResult;
  75. LPWSTR pszSchema = NULL;
  76. BYTE *pData;
  77. DWORD uSize;
  78. DWORD dwType;
  79. DWORD dwTotal;
  80. hr = pIRequest->GetResult (&hResult);
  81. SPLASSERT (SUCCEEDED (hr));
  82. hr = pIRequest->GetEnumCount (&dwTotal);
  83. SPLASSERT (SUCCEEDED (hr));
  84. printf("hr = %x; total = %d\n", hResult, dwTotal);
  85. for (i = 0; i < dwTotal; i++) {
  86. hr = pIRequest->GetOutputData (i, &pszSchema, &dwType, &pData, &uSize);
  87. printf("%d: %ws\n", i, (pszSchema)?pszSchema:L"");
  88. DumpOutputData (dwType, pData, uSize);
  89. }
  90. }
  91. void DumpContainer (IBidiRequestContainer * pIReqContainer)
  92. {
  93. IBidiRequest * pRequest = NULL;
  94. DWORD dwRef, i;
  95. DWORD dwTotal;
  96. HRESULT hr;
  97. hr = pIReqContainer->GetRequestCount (&dwTotal);
  98. SPLASSERT (SUCCEEDED (hr));
  99. IEnumUnknown *pEnumIunk;
  100. hr = pIReqContainer->GetEnumObject (&pEnumIunk) ;
  101. SPLASSERT (SUCCEEDED (hr));
  102. for (i = 0; i < dwTotal; i++){
  103. IUnknown *pIunk;
  104. DWORD dwFetched;
  105. hr = pEnumIunk->Next (1, &pIunk, &dwFetched);
  106. SPLASSERT (SUCCEEDED (hr));
  107. SPLASSERT (dwFetched == 1);
  108. IBidiRequest *pIRequest = NULL;
  109. hr = pIunk->QueryInterface (IID_IBidiRequest, (void **) & pIRequest);
  110. SPLASSERT (SUCCEEDED (hr));
  111. DumpRequest (pIRequest);
  112. dwRef = pIunk->Release();
  113. dwRef = pIRequest->Release();
  114. }
  115. pEnumIunk->Release ();
  116. }
  117. void TestBidiRequest ()
  118. {
  119. HRESULT hr;
  120. IBidiRequest * pIReq = NULL ;
  121. hr = ::CoCreateInstance(CLSID_BidiRequest,
  122. NULL,
  123. CLSCTX_INPROC_SERVER,
  124. IID_IBidiRequest,
  125. (void**)&pIReq) ;
  126. if (SUCCEEDED(hr))
  127. {
  128. hr = pIReq->SetSchema (L"/printer/duplex");
  129. DBGMSG (DBG_TRACE, ("SetSchema hr = %x\n", hr));
  130. DWORD dwData = 30;
  131. DumpData (1, (PBYTE) &dwData, sizeof (DWORD));
  132. hr = pIReq->SetInputData (1, (LPBYTE) &dwData, sizeof (DWORD));
  133. DBGMSG (DBG_TRACE, ("SetInputData hr = 0x%x\n", hr));
  134. IBidiRequestSpl *pISpl = NULL;
  135. hr = pIReq->QueryInterface (IID_IBidiRequestSpl, (void **) & pISpl);
  136. if (SUCCEEDED(hr))
  137. {
  138. LPWSTR pSchema;
  139. DBGMSG (DBG_TRACE, ("Calling GetSchema\n"));
  140. hr = pISpl->GetSchema ( & pSchema);
  141. DBGMSG (DBG_TRACE, ("GetSchema hr=%x, %ws\n", hr, pSchema));
  142. hr = pISpl->SetResult (0x30);
  143. DBGMSG (DBG_TRACE, ("SetResult hr=%x\n", hr, pSchema));
  144. DWORD dwTotal = 2;
  145. for (DWORD i = 0; i <dwTotal; i++ ) {
  146. WCHAR szSchema[512];
  147. DWORD dwType;
  148. BYTE pData[512];
  149. ULONG uSize;
  150. for (int j = 0; j < rand() % 512 - 1; j++) {
  151. szSchema[j] = L'0';
  152. }
  153. dwType = BIDI_BLOB;
  154. uSize = rand () % 512;
  155. for (j = 0; j < (int) uSize; j++) {
  156. pData[j] = (BYTE) (j+i) ;
  157. }
  158. hr = pISpl->AppendOutputData (szSchema, dwType, pData, uSize);
  159. DumpData (dwType, pData, uSize);
  160. DBGMSG (DBG_TRACE, ("AppendOutputData hr = 0x%x\n", hr));
  161. }
  162. }
  163. HRESULT hrResult;
  164. hr = pIReq->GetResult (&hrResult);
  165. DBGMSG (DBG_TRACE, ("GetResult hr = 0x%x, Result=0x%x\n", hr, hrResult));
  166. DWORD dwTotal;
  167. hr = pIReq->GetEnumCount (&dwTotal);
  168. DBGMSG (DBG_TRACE, ("GetEnumCount hr = 0x%x, Result=%d\n", hr, dwTotal));
  169. for (DWORD i = 0; i <dwTotal; i++ ) {
  170. LPWSTR pSchema;
  171. DWORD dwType;
  172. LPBYTE pData;
  173. ULONG uSize;
  174. hr = pIReq->GetOutputData (i, &pSchema, &dwType, &pData, &uSize);
  175. DBGMSG (DBG_TRACE, ("GetOutputData hr = 0x%x, dwType=%d, dwSize=%d\n", hr, dwType, uSize));
  176. DumpData (dwType, pData, uSize);
  177. }
  178. DBGMSG (DBG_TRACE, ("Release IBididRequestSpl interface.\n")) ;
  179. pIReq->Release() ;
  180. DBGMSG (DBG_TRACE, ("Release IBididRequest interface.\n")) ;
  181. pISpl->Release() ;
  182. }
  183. else
  184. {
  185. DBGMSG (DBG_TRACE, ("Client: \t\tCould not create component. hr = %x\n ", hr));
  186. }
  187. }
  188. IBidiRequest * GenRequest ()
  189. {
  190. HRESULT hr;
  191. IBidiRequest * pIReq = NULL ;
  192. hr = ::CoCreateInstance(CLSID_BidiRequest,
  193. NULL,
  194. CLSCTX_INPROC_SERVER,
  195. IID_IBidiRequest,
  196. (void**)&pIReq) ;
  197. if (SUCCEEDED(hr))
  198. {
  199. hr = pIReq->SetSchema (L"/printer/duplex");
  200. DBGMSG (DBG_TRACE, ("SetSchema hr = %x\n", hr));
  201. DWORD dwData = rand() % 1000;
  202. DumpData (1, (PBYTE) &dwData, sizeof (DWORD));
  203. hr = pIReq->SetInputData (BIDI_INT, (LPBYTE) &dwData, sizeof (DWORD));
  204. DBGMSG (DBG_TRACE, ("SetInputData hr = 0x%x\n", hr));
  205. DBGMSG (DBG_TRACE, ("Release IBididRequestSpl interface.\n")) ;
  206. return pIReq;
  207. }
  208. else
  209. {
  210. DBGMSG (DBG_TRACE, ("Client: \t\tCould not create component. hr = %x\n ", hr));
  211. return NULL;
  212. }
  213. }
  214. IBidiRequestContainer * GenRequestContainer (DWORD dwTotal)
  215. {
  216. HRESULT hr;
  217. ULONG dwRef;
  218. IBidiRequestContainer * pIReqContainer = NULL ;
  219. hr = ::CoCreateInstance(CLSID_BidiRequestContainer,
  220. NULL,
  221. CLSCTX_INPROC_SERVER,
  222. IID_IBidiRequestContainer,
  223. (void**)&pIReqContainer) ;
  224. SPLASSERT (SUCCEEDED (hr));
  225. IBidiRequest * pRequest = NULL;
  226. for (DWORD i = 0; i < dwTotal; i++) {
  227. pRequest = GenRequest ();
  228. SPLASSERT (pRequest != NULL);
  229. hr = pIReqContainer->AddRequest (pRequest);
  230. SPLASSERT (SUCCEEDED (hr));
  231. dwRef = pRequest->Release ();
  232. SPLASSERT (dwRef == 1);
  233. }
  234. return pIReqContainer;
  235. }
  236. void TestContainer ()
  237. {
  238. HRESULT hr;
  239. ULONG dwRef;
  240. IBidiRequestContainer * pIReqContainer = NULL ;
  241. hr = ::CoCreateInstance(CLSID_BidiRequestContainer,
  242. NULL,
  243. CLSCTX_INPROC_SERVER,
  244. IID_IBidiRequestContainer,
  245. (void**)&pIReqContainer) ;
  246. SPLASSERT (SUCCEEDED (hr));
  247. IBidiRequest * pRequest = NULL;
  248. DWORD dwTotal = 3;
  249. for (DWORD i = 0; i < dwTotal; i++) {
  250. pRequest = GenRequest ();
  251. SPLASSERT (pRequest != NULL);
  252. hr = pIReqContainer->AddRequest (pRequest);
  253. SPLASSERT (SUCCEEDED (hr));
  254. dwRef = pRequest->Release ();
  255. SPLASSERT (dwRef == 1);
  256. }
  257. DWORD dwTotal2;
  258. hr = pIReqContainer->GetRequestCount (&dwTotal2);
  259. SPLASSERT (SUCCEEDED (hr));
  260. SPLASSERT (dwTotal == dwTotal2);
  261. IEnumUnknown *pEnumIunk;
  262. hr = pIReqContainer->GetEnumObject (&pEnumIunk) ;
  263. SPLASSERT (SUCCEEDED (hr));
  264. for (i = 0; i < dwTotal2; i++){
  265. IUnknown *pIunk;
  266. DWORD dwFetched;
  267. hr = pEnumIunk->Next (1, &pIunk, &dwFetched);
  268. SPLASSERT (SUCCEEDED (hr));
  269. SPLASSERT (dwFetched == 1);
  270. IBidiRequestSpl *pISpl = NULL;
  271. hr = pIunk->QueryInterface (IID_IBidiRequestSpl, (void **) & pISpl);
  272. SPLASSERT (SUCCEEDED (hr));
  273. dwRef = pIunk->Release();
  274. dwRef = pISpl->Release();
  275. }
  276. dwRef = pEnumIunk->Release ();
  277. DBGMSG (DBG_TRACE, ("Release IBididRequestContainer interface.\n")) ;
  278. dwRef = pIReqContainer->Release() ;
  279. SPLASSERT (dwRef == 0);
  280. }
  281. void TestBidiAPI ()
  282. {
  283. HRESULT hr;
  284. ULONG dwRef;
  285. IBidiSpl * pIBidiSpl = NULL ;
  286. IBidiRequestContainer *pIReqContainer;
  287. hr = ::CoCreateInstance(CLSID_BidiSpl,
  288. NULL,
  289. CLSCTX_INPROC_SERVER,
  290. IID_IBidiSpl,
  291. (void**)&pIBidiSpl) ;
  292. SPLASSERT (SUCCEEDED (hr));
  293. DBGMSG (DBG_TRACE, ("IBidiSpl created.\n")) ;
  294. pIReqContainer = GenRequestContainer (3);
  295. // Test Open/Close
  296. hr = pIBidiSpl->MultiSendRecv (L"Get", pIReqContainer);
  297. SPLASSERT (hr == E_HANDLE);
  298. hr = pIBidiSpl->BindDevice (L"No such Printer", 0);
  299. SPLASSERT (!SUCCEEDED (hr));
  300. hr = pIBidiSpl->BindDevice (L"Test", BIDI_ACCESS_USER);
  301. SPLASSERT (SUCCEEDED (hr));
  302. hr = pIBidiSpl->UnbindDevice ();
  303. SPLASSERT (SUCCEEDED (hr));
  304. hr = pIBidiSpl->UnbindDevice ();
  305. SPLASSERT (!SUCCEEDED (hr));
  306. hr = pIBidiSpl->BindDevice (L"Test", BIDI_ACCESS_USER);
  307. SPLASSERT (SUCCEEDED (hr));
  308. pIReqContainer = GenRequestContainer (3);
  309. hr = pIBidiSpl->MultiSendRecv (L"Get", NULL);
  310. SPLASSERT (!SUCCEEDED (hr));;
  311. hr = pIBidiSpl->MultiSendRecv (L"Get", pIReqContainer);
  312. SPLASSERT (SUCCEEDED (hr));
  313. dwRef = pIReqContainer->Release ();
  314. SPLASSERT (dwRef == 0);
  315. dwRef = pIBidiSpl->Release ();
  316. SPLASSERT (dwRef == 0);
  317. DBGMSG (DBG_TRACE, ("IBidiSpl released.\n")) ;
  318. }
  319. IBidiRequest * GenRequest (LPWSTR pSchema)
  320. {
  321. HRESULT hr;
  322. IBidiRequest * pIReq = NULL ;
  323. hr = ::CoCreateInstance(CLSID_BidiRequest,
  324. NULL,
  325. CLSCTX_INPROC_SERVER,
  326. IID_IBidiRequest,
  327. (void**)&pIReq) ;
  328. SPLASSERT (SUCCEEDED(hr));
  329. hr = pIReq->SetSchema (pSchema);
  330. SPLASSERT (SUCCEEDED(hr));
  331. return pIReq;
  332. }
  333. IBidiRequestContainer * GenRequestContainer2 (DWORD dwTotal, LPWSTR pSchema)
  334. {
  335. HRESULT hr;
  336. ULONG dwRef;
  337. IBidiRequestContainer * pIReqContainer = NULL ;
  338. hr = ::CoCreateInstance(CLSID_BidiRequestContainer,
  339. NULL,
  340. CLSCTX_INPROC_SERVER,
  341. IID_IBidiRequestContainer,
  342. (void**)&pIReqContainer) ;
  343. SPLASSERT (SUCCEEDED (hr));
  344. IBidiRequest * pRequest = NULL;
  345. for (DWORD i = 0; i < dwTotal; i++) {
  346. pRequest = GenRequest (pSchema);
  347. SPLASSERT (pRequest != NULL);
  348. hr = pIReqContainer->AddRequest (pRequest);
  349. SPLASSERT (SUCCEEDED (hr));
  350. dwRef = pRequest->Release ();
  351. SPLASSERT (dwRef == 1);
  352. }
  353. return pIReqContainer;
  354. }
  355. IBidiRequestContainer *CreateContainer ()
  356. {
  357. HRESULT hr;
  358. IBidiRequestContainer * pIReqContainer = NULL ;
  359. hr = ::CoCreateInstance(CLSID_BidiRequestContainer,
  360. NULL,
  361. CLSCTX_INPROC_SERVER,
  362. IID_IBidiRequestContainer,
  363. (void**)&pIReqContainer) ;
  364. SPLASSERT (SUCCEEDED (hr));
  365. return pIReqContainer;
  366. }
  367. void AddRequest (
  368. IBidiRequestContainer * pIReqContainer,
  369. LPWSTR pSchema)
  370. {
  371. IBidiRequest * pRequest = NULL;
  372. HRESULT hr;
  373. DWORD dwRef;
  374. pRequest = GenRequest (pSchema);
  375. SPLASSERT (pRequest != NULL);
  376. hr = pIReqContainer->AddRequest (pRequest);
  377. SPLASSERT (SUCCEEDED (hr));
  378. dwRef = pRequest->Release ();
  379. SPLASSERT (dwRef == 1);
  380. }
  381. void TestBidiAPI2 ()
  382. {
  383. HRESULT hr;
  384. ULONG dwRef;
  385. IBidiSpl * pIBidiSpl = NULL ;
  386. IBidiRequestContainer *pIReqContainer;
  387. IBidiRequest * pRequest = NULL;
  388. hr = ::CoCreateInstance(CLSID_BidiSpl,
  389. NULL,
  390. CLSCTX_INPROC_SERVER,
  391. IID_IBidiSpl,
  392. (void**)&pIBidiSpl) ;
  393. SPLASSERT (SUCCEEDED (hr));
  394. DBGMSG (DBG_TRACE, ("IBidiSpl created.\n")) ;
  395. hr = pIBidiSpl->BindDevice (L"Test", BIDI_ACCESS_USER);
  396. SPLASSERT (SUCCEEDED (hr));
  397. #if 0
  398. pIReqContainer = GenRequestContainer2 (10, L"/Printer/Installableoption/DuplexUnit");
  399. hr = pIBidiSpl->MultiSendRecv (L"Get", pIReqContainer);
  400. SPLASSERT (SUCCEEDED (hr));
  401. DumpContainer (pIReqContainer);
  402. dwRef = pIReqContainer->Release ();
  403. SPLASSERT (dwRef == 0);
  404. #endif
  405. // Case 2
  406. pIReqContainer = CreateContainer ();
  407. //AddRequest (pIReqContainer, L"/Printer/Installableoption/DuplexUnit");
  408. //AddRequest (pIReqContainer, L"/Communication/Version");
  409. AddRequest (pIReqContainer, L"/Communication/BidiProtocol");
  410. AddRequest (pIReqContainer, L"/Printer/BlackInk1/Level");
  411. hr = pIBidiSpl->MultiSendRecv (L"Get", pIReqContainer);
  412. SPLASSERT (SUCCEEDED (hr));
  413. DumpContainer (pIReqContainer);
  414. dwRef = pIReqContainer->Release ();
  415. SPLASSERT (dwRef == 0);
  416. // Case 3
  417. pIReqContainer = CreateContainer ();
  418. AddRequest (pIReqContainer, L"/Printers/Alerts");
  419. hr = pIBidiSpl->MultiSendRecv (L"GetAll", pIReqContainer);
  420. SPLASSERT (SUCCEEDED (hr));
  421. DumpContainer (pIReqContainer);
  422. dwRef = pIReqContainer->Release ();
  423. SPLASSERT (dwRef == 0);
  424. dwRef = pIBidiSpl->Release ();
  425. SPLASSERT (dwRef == 0);
  426. DBGMSG (DBG_TRACE, ("IBidiSpl released.\n")) ;
  427. }
  428. void StartTest ()
  429. {
  430. //TestContainer ();
  431. //TestBidiRequest ();
  432. //TestBidiAPI ();
  433. TestBidiAPI2 ();
  434. }
  435. int __cdecl main(int argc, char **argv)
  436. {
  437. HRESULT hr;
  438. hr = CoInitializeEx (NULL, COINIT_MULTITHREADED) ;
  439. SPLASSERT (SUCCEEDED (hr))
  440. StartTest ();
  441. // Uninitialize COM Library
  442. CoUninitialize() ;
  443. #if 0
  444. hr = CoInitializeEx (NULL, COINIT_APARTMENTTHREADED ) ;
  445. SPLASSERT (SUCCEEDED (hr))
  446. StartTest ();
  447. // Uninitialize COM Library
  448. CoUninitialize() ;
  449. hr = CoInitialize (NULL);
  450. SPLASSERT (SUCCEEDED (hr))
  451. StartTest ();
  452. // Uninitialize COM Library
  453. CoUninitialize() ;
  454. #endif
  455. printf("Test successfully finished!\n");
  456. return 0 ;
  457. }