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.

802 lines
20 KiB

  1. //-------------------------------------------------------------------------
  2. //
  3. // Microsoft OLE
  4. // Copyright (C) Microsoft Corporation, 1993 - 1996.
  5. // All rights reserved.
  6. //
  7. //--------------------------------------------------------------------------
  8. #include <dfheader.hxx>
  9. #pragma hdrstop
  10. #include <debdlg.h>
  11. // global defines
  12. #define LOG_FILE_NAME "/t:utest"
  13. // Debug Object
  14. DH_DEFINE;
  15. // Usage: If you would like to use CreateFromParams to create ChanceDocFile
  16. // tree, give arguments e.g
  17. // test /seed:2 /dfdepth:0-9 /dfstg:1-11 /dfstm:10-30 /dfstmlen:0-222
  18. // /dfRootCrMode:dirReadWriteShEx
  19. //
  20. // if want to use CreateFromSize but through CreateFromParams e.g.
  21. // test /seed:2 /dfsize:tiny
  22. //
  23. // If the seed value given is zero, the datagen objects computes the
  24. // seed value to be used based on current time and some other compu-
  25. // tations, so the resulting docfiles wouldn't be same in different
  26. // instants of time if seed is 0 even if all the other parameters are
  27. // same. With seed value other than zero, the docfiles produced at
  28. // different instances of time would be identical with other parameters // remaining same.
  29. //
  30. //
  31. //-------------------------------------------------------------------
  32. //
  33. // Function: main
  34. //
  35. // Synopsis: entry point for unit test program
  36. //
  37. // Parameters: [argc] - Argument count
  38. // [argv] - Arguments
  39. //
  40. // Returns: void
  41. //
  42. // History: 20-Apr-1996 Narindk Created
  43. //
  44. //--------------------------------------------------------------------
  45. VOID __cdecl main(int argc, char *argv[])
  46. {
  47. HRESULT hr = S_OK;
  48. ChanceDF *pTestChanceDF = NULL;
  49. VirtualDF *pTestVirtualDF = NULL;
  50. VirtualCtrNode *pVirtualDFRoot = NULL;
  51. // Initialize our log object
  52. DH_CREATELOGCMDLINE( LOG_FILE_NAME ) ;
  53. DH_SETLOGLOC(DH_LOC_LOG) ;
  54. DH_FUNCENTRY(NULL, DH_LVL_DFLIB, _TEXT("::main"));
  55. // Initialize the OLE server.
  56. hr = CoInitialize(NULL);
  57. if (S_OK != hr)
  58. {
  59. DH_LOG((
  60. LOG_FAIL,
  61. TEXT("test: CoInitialize failed, hr=0x%08x\n"), hr));
  62. return;
  63. }
  64. // I: Using CreateFromSize within program to generate DocFile
  65. // Create the new ChanceDocFile tree that would consist of chance nodes.
  66. if (S_OK == hr)
  67. {
  68. pTestChanceDF = new ChanceDF();
  69. if(NULL == pTestChanceDF)
  70. {
  71. hr = E_OUTOFMEMORY;
  72. }
  73. }
  74. if (S_OK == hr)
  75. {
  76. // To check from CreateFromSize
  77. hr = pTestChanceDF->CreateFromSize(DF_HUGE, 2);
  78. DH_HRCHECK(hr, TEXT("pTestChanceDF->CreateFromSize")) ;
  79. }
  80. // Create the VirtualDocFile tree from the ChanceDocFile tree created in
  81. // the previous step. The VirtualDocFile tree consists of VirtualCtrNodes
  82. // and VirtualStmNodes.
  83. if (S_OK == hr)
  84. {
  85. pTestVirtualDF = new VirtualDF();
  86. if(NULL == pTestVirtualDF)
  87. {
  88. hr = E_OUTOFMEMORY;
  89. }
  90. }
  91. if (S_OK == hr)
  92. {
  93. hr = pTestVirtualDF->GenerateVirtualDF(pTestChanceDF, &pVirtualDFRoot);
  94. DH_HRCHECK(hr, TEXT("pTestVirtualDF->GenerateVirtualDF")) ;
  95. }
  96. if (S_OK == hr)
  97. {
  98. DH_LOG((
  99. LOG_PASS,
  100. TEXT("DocFile - CreateFromSize - successfully created.\n")));
  101. }
  102. else
  103. {
  104. DH_LOG((
  105. LOG_FAIL,
  106. TEXT("DocFile - CreateFromSize - failed.\n")));
  107. }
  108. // Cleanup
  109. if(NULL != pTestChanceDF)
  110. {
  111. hr = pTestChanceDF->DeleteChanceDocFileTree(
  112. pTestChanceDF->GetChanceDFRoot());
  113. DH_HRCHECK(hr, TEXT("pTestChanceDF->DeleteChanceFileDocTree")) ;
  114. delete pTestChanceDF;
  115. pTestChanceDF = NULL;
  116. }
  117. if(NULL != pTestVirtualDF)
  118. {
  119. hr = pTestVirtualDF->DeleteVirtualDocFileTree(pVirtualDFRoot);
  120. DH_HRCHECK(hr, TEXT("pTestVirtualDF->DeleteVirtualFileDocTree")) ;
  121. delete pTestVirtualDF;
  122. pTestVirtualDF = NULL;
  123. }
  124. // II: Using CreateFromParams to generate DocFile
  125. // a. Using CreateFromParams to call Create. e.g.
  126. // test /seed:3 /dfdepth:0-9 /dfstg:1-11 /dfstm:10-30
  127. // /dfstmlen:0-222 /dfRootCrMode:4114 /dfStgCrMode:4114
  128. // /dfStmCrMode:4114
  129. // b. Using CreateFromParams to call CreateFromSize. e.g.
  130. // test /seed:2 /dfsize:tiny
  131. // c. Using CreateFromParams to call CreateFromFile (NYI). e.g.
  132. // test /seed:4 /dftemp:myfile
  133. //
  134. // if you do not provide the parameters, but still call the function
  135. // CreateFromParam, all default values would be used.
  136. // Create the new ChanceDocFile tree that would consist of chance nodes.
  137. if (S_OK == hr)
  138. {
  139. pTestChanceDF = new ChanceDF();
  140. if(NULL == pTestChanceDF)
  141. {
  142. hr = E_OUTOFMEMORY;
  143. }
  144. }
  145. // Can pass the arguments after simulating command line arguments in
  146. // the test itself.
  147. if (S_OK == hr)
  148. {
  149. // To create it from Params.
  150. hr = pTestChanceDF->CreateFromParams(argc, argv);
  151. DH_HRCHECK(hr, TEXT("pTestChanceDF->CreateFromParams")) ;
  152. }
  153. // Create the VirtualDocFile tree from the ChanceDocFile tree created in
  154. // the previous step. The VirtualDocFile tree consists of VirtualCtrNodes
  155. // and VirtualStmNodes.
  156. if (S_OK == hr)
  157. {
  158. pTestVirtualDF = new VirtualDF();
  159. if(NULL == pTestVirtualDF)
  160. {
  161. hr = E_OUTOFMEMORY;
  162. }
  163. }
  164. if (S_OK == hr)
  165. {
  166. hr = pTestVirtualDF->GenerateVirtualDF(pTestChanceDF, &pVirtualDFRoot);
  167. DH_HRCHECK(hr, TEXT("pTestVirtualDF->GenerateVirtualDF")) ;
  168. }
  169. if (S_OK == hr)
  170. {
  171. DH_LOG((
  172. LOG_PASS,
  173. TEXT("DocFile - CreateFromParams - successfully created.\n")));
  174. }
  175. else
  176. {
  177. DH_LOG((
  178. LOG_FAIL,
  179. TEXT("DocFile - CreateFromParams - failed.\n")));
  180. }
  181. // Cleanup
  182. if(NULL != pTestChanceDF)
  183. {
  184. hr = pTestChanceDF->DeleteChanceDocFileTree(
  185. pTestChanceDF->GetChanceDFRoot());
  186. DH_HRCHECK(hr, TEXT("pTestChanceDF->DeleteChanceFileDocTree")) ;
  187. delete pTestChanceDF;
  188. pTestChanceDF = NULL;
  189. }
  190. if(NULL != pTestVirtualDF)
  191. {
  192. hr = pTestVirtualDF->DeleteVirtualDocFileTree(pVirtualDFRoot);
  193. DH_HRCHECK(hr, TEXT("pTestVirtualDF->DeleteVirtualFileDocTree")) ;
  194. delete pTestVirtualDF;
  195. pTestVirtualDF = NULL;
  196. }
  197. // III: Using CreateFromSize within program to generate DocFile.
  198. // Do the following operations:
  199. // a. Close the root node and open root node again. Check it succeeds
  200. // Create the new ChanceDocFile tree that would consist of chance nodes.
  201. if (S_OK == hr)
  202. {
  203. pTestChanceDF = new ChanceDF();
  204. if(NULL == pTestChanceDF)
  205. {
  206. hr = E_OUTOFMEMORY;
  207. }
  208. }
  209. if (S_OK == hr)
  210. {
  211. // To check from CreateFromSize
  212. hr = pTestChanceDF->CreateFromSize(DF_MEDIUM, 3);
  213. DH_HRCHECK(hr, TEXT("pTestChanceDF->CreateFromSize")) ;
  214. }
  215. // Create the VirtualDocFile tree from the ChanceDocFile tree created in
  216. // the previous step. The VirtualDocFile tree consists of VirtualCtrNodes
  217. // and VirtualStmNodes.
  218. if (S_OK == hr)
  219. {
  220. pTestVirtualDF = new VirtualDF();
  221. if(NULL == pTestVirtualDF)
  222. {
  223. hr = E_OUTOFMEMORY;
  224. }
  225. }
  226. if (S_OK == hr)
  227. {
  228. hr = pTestVirtualDF->GenerateVirtualDF(pTestChanceDF, &pVirtualDFRoot);
  229. DH_HRCHECK(hr, TEXT("pTestVirtualDF->GenerateVirtualDF")) ;
  230. }
  231. if (S_OK == hr)
  232. {
  233. DH_LOG((
  234. LOG_PASS,
  235. TEXT("DocFile - CreateFromSize - successfully created.\n")));
  236. }
  237. else
  238. {
  239. DH_LOG((
  240. LOG_FAIL,
  241. TEXT("DocFile - CreateFromSize - failed.\n")));
  242. }
  243. // a. Close the root node and open root node again. Check it succeeds
  244. if (S_OK == hr)
  245. {
  246. hr = pVirtualDFRoot->Close();
  247. DH_HRCHECK(hr, TEXT("VirtualCtrNode::Close")) ;
  248. }
  249. if (S_OK == hr)
  250. {
  251. DH_LOG((
  252. LOG_PASS,
  253. TEXT("Root Storage closed successfully.\n")));
  254. }
  255. else
  256. {
  257. DH_LOG((
  258. LOG_FAIL,
  259. TEXT("Root Storage couldn't be closed successfully.\n")));
  260. }
  261. if (S_OK == hr)
  262. {
  263. hr = pVirtualDFRoot->OpenRoot(
  264. NULL,
  265. STGM_READWRITE |
  266. STGM_SHARE_EXCLUSIVE ,
  267. NULL,
  268. 0);
  269. DH_HRCHECK(hr, TEXT("VirtualCtrNode::OpenRoot")) ;
  270. }
  271. if (S_OK == hr)
  272. {
  273. DH_LOG((
  274. LOG_PASS,
  275. TEXT("Root Storage opened successfully.\n")));
  276. }
  277. else
  278. {
  279. DH_LOG((
  280. LOG_FAIL,
  281. TEXT("Root Storage couldn't be opened successfully.\n")));
  282. }
  283. // b. Close the root node's stream and open it again. Check it succeeds
  284. VirtualStmNode *pvsnTest = NULL;
  285. VirtualCtrNode *pvcnTest = NULL;
  286. if (S_OK == hr)
  287. {
  288. // This call will return us the root of VirtualDocFileTree becoz'
  289. // we are passing NULL for second parameter.
  290. hr = GetVirtualStgNodeForTest(pTestVirtualDF, NULL, &pvcnTest, 0);
  291. DH_HRCHECK(hr, TEXT("GetVirtualStgNodeForTest")) ;
  292. }
  293. if (S_OK == hr)
  294. {
  295. // This call will return us the first stream of pvcnTest VirtualCtrNode
  296. // as we are passing zero for fourth parameter.
  297. hr = GetVirtualStmNodeForTest(pvcnTest, &pvsnTest, 0);
  298. DH_HRCHECK(hr, TEXT("GetVirtualStgNodeForTest")) ;
  299. }
  300. if (S_OK == hr)
  301. {
  302. hr = pvsnTest->Close();
  303. DH_HRCHECK(hr, TEXT("VirtualStmNode::Close")) ;
  304. }
  305. if (S_OK == hr)
  306. {
  307. DH_LOG((
  308. LOG_PASS,
  309. TEXT("VirtualStmNode::Close completed successfully.\n")));
  310. }
  311. else
  312. {
  313. DH_LOG((
  314. LOG_FAIL,
  315. TEXT("VirtualStmNode::Close couldn't complete successfully.\n")));
  316. }
  317. if (S_OK == hr)
  318. {
  319. hr = pvsnTest->Open(
  320. NULL,
  321. STGM_READWRITE |
  322. STGM_SHARE_EXCLUSIVE ,
  323. 0);
  324. DH_HRCHECK(hr, TEXT("VirtualStmNode::Open")) ;
  325. }
  326. if (S_OK == hr)
  327. {
  328. DH_LOG((
  329. LOG_PASS,
  330. TEXT("VirtualStmNode::Open completed successfully.\n")));
  331. }
  332. else
  333. {
  334. DH_LOG((
  335. LOG_FAIL,
  336. TEXT("VirtualStmNode::Open couldn't complete successfully.\n")));
  337. }
  338. // c.1 Set the size of the stream
  339. DWORD dwStreamSize = 512;
  340. ULARGE_INTEGER uli;
  341. ULISet32(uli, dwStreamSize);
  342. if (S_OK == hr)
  343. {
  344. hr = pvsnTest->SetSize(uli);
  345. }
  346. if (S_OK == hr)
  347. {
  348. DH_LOG((
  349. LOG_PASS,
  350. TEXT("IStream::SetSize function completed successfully.\n")));
  351. }
  352. else
  353. {
  354. DH_LOG((
  355. LOG_FAIL,
  356. TEXT("IStream::SetSize function wasn't successful.\n")));
  357. }
  358. // c.2 Write into opened test stream. Check it succeeds
  359. DWORD dwSize = 0;
  360. DWORD dwBufSize = 512;
  361. DWORD dwWritten = 0;
  362. ULONG *lpBuf = NULL;
  363. const ULONG kulPattern = 0xABCDABCD; // Arbitrary fill pattern for stream
  364. lpBuf = new ULONG [dwBufSize];
  365. if (lpBuf == NULL)
  366. {
  367. hr = E_OUTOFMEMORY;
  368. }
  369. for (dwSize = 0; dwSize < dwBufSize; dwSize++)
  370. {
  371. lpBuf[dwSize] = kulPattern;
  372. }
  373. if (S_OK == hr)
  374. {
  375. hr = pvsnTest->Write(lpBuf, dwBufSize, &dwWritten);
  376. }
  377. if (S_OK == hr)
  378. {
  379. DH_LOG((
  380. LOG_PASS,
  381. TEXT("IStream::Write function completed successfully.\n")));
  382. }
  383. else
  384. {
  385. DH_LOG((
  386. LOG_FAIL,
  387. TEXT("IStream::Write function wasn't successful.\n")));
  388. }
  389. // Cleanup
  390. delete []lpBuf;
  391. // d. Seek test stream. Check it succeeds
  392. LARGE_INTEGER lint;
  393. memset(&lint, 0, sizeof(LARGE_INTEGER));
  394. // Position the stream header to the begining
  395. if (S_OK == hr)
  396. {
  397. hr = pvsnTest->Seek(lint, STREAM_SEEK_SET, NULL);
  398. }
  399. if (S_OK == hr)
  400. {
  401. DH_LOG((
  402. LOG_PASS,
  403. TEXT("IStream::Seek function completed successfully.\n")));
  404. }
  405. else
  406. {
  407. DH_LOG((
  408. LOG_FAIL,
  409. TEXT("IStream::Seek function wasn't successful.\n")));
  410. }
  411. // e. Read test stream. Check it succeeds
  412. DWORD dwRead = 0;
  413. ULONG *lpStr = NULL;
  414. lpStr = new ULONG [dwBufSize];
  415. if (lpBuf == NULL)
  416. {
  417. hr = E_OUTOFMEMORY;
  418. }
  419. if ( S_OK == hr )
  420. {
  421. // Read the stream.
  422. hr = pvsnTest->Read(lpStr, dwBufSize, &dwRead);
  423. }
  424. if (S_OK == hr)
  425. {
  426. DH_LOG((
  427. LOG_PASS,
  428. TEXT("IStream::Read function completed successfully.\n")));
  429. }
  430. else
  431. {
  432. DH_LOG((
  433. LOG_FAIL,
  434. TEXT("IStream::Read function wasn't successful.\n")));
  435. }
  436. // Cleanup
  437. delete []lpStr;
  438. // f. Close the root node's substorage and open it again. Check it
  439. // succeeds
  440. VirtualCtrNode *pvcnTest1 = NULL;
  441. ULONG cChildCnt = 0;
  442. if (S_OK == hr)
  443. {
  444. // This call will return us the first substorage root of
  445. // VirtualDocFileTree if its exists. We may use pvcnTest
  446. // which points to the root of virtual docfile tree.
  447. cChildCnt =
  448. pVirtualDFRoot->GetVirtualCtrNodeChildrenCount();
  449. /*
  450. _ftprintf(stdout,
  451. TEXT("No of substorages in RootStorage are %lu"),
  452. cChildCnt);
  453. */
  454. if(cChildCnt <= 0)
  455. {
  456. hr = S_FALSE;
  457. DH_LOG((
  458. LOG_INFO,
  459. TEXT("No substorages of this storage")));
  460. }
  461. }
  462. if (S_OK == hr)
  463. {
  464. // Get the first substorage of the root.
  465. hr = GetVirtualStgNodeForTest(
  466. pTestVirtualDF,pVirtualDFRoot,&pvcnTest1,1);
  467. DH_HRCHECK(hr, TEXT("GetVirtualStgNodeForTest")) ;
  468. }
  469. if (S_OK == hr)
  470. {
  471. hr = pvcnTest1->Close();
  472. DH_HRCHECK(hr, TEXT("VirtualCtrNode::Close")) ;
  473. }
  474. if (S_OK == hr)
  475. {
  476. DH_LOG((
  477. LOG_PASS,
  478. TEXT("VirtualCtrNode::Close completed successfully.\n")));
  479. }
  480. else
  481. {
  482. DH_LOG((
  483. LOG_FAIL,
  484. TEXT("VirtualCtrNode::Close couldn't complete successfully.\n")));
  485. }
  486. if (S_OK == hr)
  487. {
  488. hr = pvcnTest1->Open(
  489. NULL,
  490. STGM_READWRITE |
  491. STGM_SHARE_EXCLUSIVE,
  492. NULL,
  493. 0);
  494. DH_HRCHECK(hr, TEXT("VirtualCtrNode::Open")) ;
  495. }
  496. if (S_OK == hr)
  497. {
  498. DH_LOG((
  499. LOG_PASS,
  500. TEXT("VirtualCtrNode::Open completed successfully.\n")));
  501. }
  502. else
  503. {
  504. DH_LOG((
  505. LOG_FAIL,
  506. TEXT("VirtualCtrNode::Open couldn't complete successfully.\n")));
  507. }
  508. // f: Adds a new storage to an existing storage. We would add another
  509. // substorage to our root storage for the test.
  510. VirtualCtrNode *pvcnNewStorage = NULL;
  511. LPWSTR pNewStgName = L"NewTestStg";
  512. if(S_OK == hr)
  513. {
  514. hr = AddStorage(
  515. pVirtualDFRoot,
  516. &pvcnNewStorage,
  517. pNewStgName,
  518. STGM_READWRITE |
  519. STGM_SHARE_EXCLUSIVE |
  520. STGM_CREATE |
  521. STGM_DIRECT);
  522. DH_HRCHECK(hr, TEXT("AddStorage")) ;
  523. }
  524. if(S_OK == hr)
  525. {
  526. DH_LOG((
  527. LOG_PASS,
  528. TEXT("VirtualCtrNode::AddStorage completed successfully.\n")));
  529. }
  530. else
  531. {
  532. DH_LOG((
  533. LOG_FAIL,
  534. TEXT("VirtualCtrNode::AddStorage couldn't complete successfully.\n")));
  535. }
  536. // g: Deletes a storage in an existing storage. We would delete first
  537. // child substorage of our root storage for the test.
  538. if(S_OK == hr)
  539. {
  540. hr = DestroyStorage(
  541. pTestVirtualDF,
  542. pVirtualDFRoot->GetFirstChildVirtualCtrNode());
  543. DH_HRCHECK(hr, TEXT("DeleteStorage")) ;
  544. }
  545. if(S_OK == hr)
  546. {
  547. DH_LOG((
  548. LOG_PASS,
  549. TEXT("VirtualCtrNode::DestroyStorage completed successfully.\n")));
  550. }
  551. else
  552. {
  553. DH_LOG((
  554. LOG_FAIL,
  555. TEXT("VirtualCtrNode::DestroyStorage couldn't complete successfully.\n")));
  556. }
  557. // h: Adds a new stream to an existing storage. We would add a stream to
  558. // newly created substorage "NewTestStg" of our root storage for test.
  559. VirtualStmNode *pvsnNewStream = NULL;
  560. LPWSTR pNewStmName = L"NewTestStm";
  561. ULONG cbNewSize = 20;
  562. if(S_OK == hr)
  563. {
  564. hr = AddStream(
  565. pvcnNewStorage,
  566. &pvsnNewStream,
  567. pNewStmName,
  568. cbNewSize,
  569. STGM_READWRITE |
  570. STGM_SHARE_EXCLUSIVE |
  571. STGM_CREATE |
  572. STGM_DIRECT);
  573. DH_HRCHECK(hr, TEXT("AddStream")) ;
  574. }
  575. if(S_OK == hr)
  576. {
  577. DH_LOG((
  578. LOG_PASS,
  579. TEXT("VirtualStmNode::AddStream completed successfully.\n")));
  580. }
  581. else
  582. {
  583. DH_LOG((
  584. LOG_FAIL,
  585. TEXT("VirtualStmNode::AddStream couldn't complete successfully.\n")));
  586. }
  587. // i: Renames the newly created storage from NewTestStg to NewRenStg
  588. LPCWSTR pRenStgName = L"RenStg";
  589. // First close the opened storage that we want to rename
  590. if (S_OK == hr)
  591. {
  592. hr = pvcnNewStorage->Close();
  593. DH_HRCHECK(hr, TEXT("VirtualCtrNode::Close")) ;
  594. }
  595. if(S_OK == hr)
  596. {
  597. hr = pvcnNewStorage->Rename(pRenStgName);
  598. }
  599. if(S_OK == hr)
  600. {
  601. DH_LOG((
  602. LOG_PASS,
  603. TEXT("IStorage::RenameElement completed successfully.\n")));
  604. }
  605. else
  606. {
  607. DH_LOG((
  608. LOG_FAIL,
  609. TEXT("IStorage::RenameElement couldn't complete successfully.\n")));
  610. }
  611. // Final Cleanup
  612. if(NULL != pTestChanceDF)
  613. {
  614. hr = pTestChanceDF->DeleteChanceDocFileTree(
  615. pTestChanceDF->GetChanceDFRoot());
  616. DH_HRCHECK(hr, TEXT("pTestChanceDF->DeleteChanceFileDocTree")) ;
  617. delete pTestChanceDF;
  618. pTestChanceDF = NULL;
  619. }
  620. if(NULL != pTestVirtualDF)
  621. {
  622. hr = pTestVirtualDF->DeleteVirtualDocFileTree(pVirtualDFRoot);
  623. DH_HRCHECK(hr, TEXT("pTestVirtualDF->DeleteVirtualFileDocTree")) ;
  624. delete pTestVirtualDF;
  625. pTestVirtualDF = NULL;
  626. }
  627. // Uninitialize OLE
  628. CoUninitialize();
  629. // Log test results and quit
  630. if (S_OK == hr)
  631. {
  632. DH_LOG((LOG_PASS, TEXT("Test program executed successfully.\n")));
  633. exit(0);
  634. }
  635. else
  636. {
  637. DH_LOG((LOG_FAIL, TEXT("Test program execution failed.\n")));
  638. exit(1);
  639. }
  640. }