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.

2505 lines
70 KiB

  1. -------------------------------------------------------------------------
  2. //
  3. // Microsoft OLE
  4. // Copyright (C) Microsoft Corporation, 1994 - 1995.
  5. //
  6. // File: roottsts.cxx
  7. //
  8. // Contents: storage base tests basically pertaining to root tests in general
  9. //
  10. // Functions:
  11. //
  12. // History: 24-June-1996 NarindK Created.
  13. // 27-Mar-97 SCousens conversionified
  14. //
  15. //--------------------------------------------------------------------------
  16. #include <dfheader.hxx>
  17. #pragma hdrstop
  18. #include <sys/stat.h>
  19. #include <share.h>
  20. #include "init.hxx"
  21. //----------------------------------------------------------------------------
  22. //
  23. // Test: ROOTTEST_100
  24. //
  25. // Synopsis: A randomly named file is created and random data is written
  26. // into it.The file is converted to a root docfile, then new IStorage
  27. // (root docfile) is committed and enumerated to ensure that only
  28. // a single IStream named "CONTENTS" (STG_CONVERTED_NAME) exists.
  29. // The CONTENTS IStream is instantiated, read, verified, and released.
  30. //
  31. // Arguments:[argc]
  32. // [argv]
  33. //
  34. // Returns: HRESULT
  35. //
  36. // History: 24-June-1996 NarindK Created.
  37. //
  38. // Notes: This test runs in direct, transacted, and transacted deny write
  39. // modes
  40. //
  41. // New Test Notes:
  42. // 1. Old File: LRCONV.CXX
  43. // 2. Old name of test : LegitRootConvert test
  44. // New Name of test : ROOTTEST_100
  45. // 3. To run the test, do the following at command prompt.
  46. // a. stgbase /seed:2 /dfdepth:0-0 /dfstg:0-0 /dfstm:0-0 /t:ROOTTEST-100
  47. // /dfRootMode:dirReadWriteShEx
  48. // b. stgbase /seed:2 /dfdepth:0-0 /dfstg:0-0 /dfstm:0-0 /t:ROOTTEST-100
  49. // /dfRootMode:xactReadWriteShEx
  50. // c. stgbase /seed:2 /dfdepth:0-0 /dfstg:0-0 /dfstm:0-0 /t:ROOTTEST-100
  51. // /dfRootMode:xactReadWriteShDenyW
  52. //
  53. // BUGNOTE: Conversion: ROOTTEST-100 NO
  54. //
  55. //-----------------------------------------------------------------------------
  56. HRESULT ROOTTEST_100(int argc, char *argv[])
  57. {
  58. HRESULT hr = S_OK;
  59. HRESULT hr2 = S_OK;
  60. ChanceDF *pTestChanceDF = NULL;
  61. LPTSTR pRootDocFileName = NULL;
  62. DWORD dwRootMode = NULL;
  63. LPSTORAGE pIStorage = NULL;
  64. LPOLESTR pOleStrTemp = NULL;
  65. ULONG ulRef = 0;
  66. FILE *hFile = NULL;
  67. DG_STRING *pdgu = NULL;
  68. DG_INTEGER *pdgi = NULL;
  69. LPTSTR ptszRandomDataBuffer = NULL;
  70. ULONG cMin = 512;
  71. ULONG cMax = 4096;
  72. ULONG cRandom = 0;
  73. ULONG culBytesWritten = 0;
  74. DWORD dwMemCRC = 0;
  75. DWORD dwActCRC = 0;
  76. LPENUMSTATSTG penumWalk = NULL;
  77. LPTSTR ptszConvName = NULL;
  78. STATSTG statStg;
  79. ULONG cItemsInConvertedDocFile= 0;
  80. LPMALLOC pMalloc = NULL;
  81. // Not for 2phase. Bail.
  82. if (DoingDistrib ())
  83. {
  84. return S_OK;
  85. }
  86. DH_FUNCENTRY(NULL, DH_LVL_DFLIB, _TEXT("ROOTTEST_100"));
  87. DH_TRACE((DH_LVL_TRACE1, TEXT("--------------------------------------------")) );
  88. DH_TRACE((DH_LVL_TRACE1, TEXT("Test variation ROOTTEST_100 started.")) );
  89. DH_TRACE((DH_LVL_TRACE1,
  90. TEXT("Attempt legitimate ops on Root conversion.")));
  91. // Create the new ChanceDocFile tree that would consist of chance nodes.
  92. if (S_OK == hr)
  93. {
  94. pTestChanceDF = new ChanceDF();
  95. if(NULL == pTestChanceDF)
  96. {
  97. hr = E_OUTOFMEMORY;
  98. }
  99. DH_HRCHECK(hr, TEXT("new ChanceDF")) ;
  100. }
  101. if (S_OK == hr)
  102. {
  103. hr = pTestChanceDF->CreateFromParams(argc, argv);
  104. DH_HRCHECK(hr, TEXT("pTestChanceDF->CreateFromParams")) ;
  105. }
  106. if (S_OK == hr)
  107. {
  108. dwRootMode = pTestChanceDF->GetRootMode();
  109. DH_TRACE((DH_LVL_TRACE1,
  110. TEXT("Run Mode for ROOTTEST_100, Access mode: %lx"),
  111. pTestChanceDF->GetRootMode()));
  112. }
  113. if(S_OK == hr)
  114. {
  115. // Create a new DataGen object to create random UNICODE strings.
  116. pdgu = new(NullOnFail) DG_STRING(pTestChanceDF->GetSeed());
  117. if (NULL == pdgu)
  118. {
  119. hr = E_OUTOFMEMORY;
  120. }
  121. DH_HRCHECK(hr, TEXT("new DG_STRING")) ;
  122. }
  123. // Generate RootDocFile name
  124. if(S_OK == hr)
  125. {
  126. hr = GenerateRandomName(pdgu, MINLENGTH,MAXLENGTH, &pRootDocFileName);
  127. DH_HRCHECK(hr, TEXT("GenerateRandomName")) ;
  128. }
  129. if(S_OK == hr)
  130. {
  131. // Convert RootDocFile name to OLECHAR
  132. hr = TStringToOleString(pRootDocFileName, &pOleStrTemp);
  133. DH_HRCHECK(hr, TEXT("TStringToOleString")) ;
  134. }
  135. // Try calling StgCreateDocFile with mode as per dwRootFlags | STGM_CONVERT
  136. // This tests the case of CONVERT specified, but no file exists to convert.
  137. // This shouldn't fail.
  138. // Just make sure no DocFile of the name exists. Delete if it does.
  139. if(NULL != pRootDocFileName)
  140. {
  141. DeleteFile(pRootDocFileName);
  142. }
  143. if (S_OK == hr)
  144. {
  145. pIStorage = NULL;
  146. HRESULT hr2 = StgCreateDocfile(
  147. pOleStrTemp,
  148. pTestChanceDF->GetRootMode() | STGM_CONVERT,
  149. 0,
  150. &pIStorage);
  151. if(S_OK == hr2 && NULL != pIStorage)
  152. {
  153. DH_TRACE((DH_LVL_TRACE1, TEXT("StgCreateDocFile passed as expected.")));
  154. }
  155. else
  156. {
  157. DH_TRACE((DH_LVL_ERROR,
  158. TEXT("StgCreateDocFile failed unexpectedly, hr = 0x%lx ."), hr));
  159. hr = (S_OK==hr2)?E_FAIL:hr2;
  160. }
  161. }
  162. // Now do a valid commit
  163. if (S_OK == hr)
  164. {
  165. hr = pIStorage->Commit(STGC_DEFAULT);
  166. if (S_OK == hr)
  167. {
  168. DH_TRACE((
  169. DH_LVL_TRACE1,
  170. TEXT("IStorage::Commit completed successfully.")));
  171. }
  172. else
  173. {
  174. DH_TRACE((DH_LVL_ERROR,
  175. TEXT("IStorage::Commit unsuccessful, hr=0x%lx."), hr));
  176. }
  177. }
  178. // Close the root docfile
  179. if (NULL != pIStorage)
  180. {
  181. ulRef = pIStorage->Release();
  182. DH_ASSERT(0 == ulRef);
  183. pIStorage = NULL;
  184. }
  185. //Now actually prepare the file and then attempt conversion.
  186. if (S_OK == hr)
  187. {
  188. hFile = _tfopen(pRootDocFileName, TEXT("w+"));
  189. if(NULL == hFile)
  190. {
  191. hr = E_OUTOFMEMORY;
  192. }
  193. DH_HRCHECK(hr, TEXT("_tfopen")) ;
  194. }
  195. if(S_OK == hr)
  196. {
  197. // Create a new DataGen object to create random INTEGER.
  198. pdgi = new(NullOnFail) DG_INTEGER(pTestChanceDF->GetSeed());
  199. if (NULL == pdgi)
  200. {
  201. hr = E_OUTOFMEMORY;
  202. }
  203. DH_HRCHECK(hr, TEXT("new DG_INTEGER")) ;
  204. }
  205. if(S_OK == hr)
  206. {
  207. hr = pdgi->Generate(&cRandom, cMin, cMax);
  208. DH_HRCHECK(hr, TEXT("Generate")) ;
  209. }
  210. if(S_OK == hr)
  211. {
  212. hr = GenerateRandomString(pdgu,cRandom, cRandom, &ptszRandomDataBuffer);
  213. DH_HRCHECK(hr, TEXT("GenerateRandomString")) ;
  214. }
  215. if(S_OK == hr)
  216. {
  217. // Calculate CRC on this buffer ptszRandomDataBuffer
  218. hr = CalculateCRCForDataBuffer(
  219. ptszRandomDataBuffer,
  220. cRandom,
  221. &dwMemCRC);
  222. DH_HRCHECK(hr, TEXT("CalculateCRCForDataBuffer")) ;
  223. }
  224. if(S_OK == hr)
  225. {
  226. // Write to file using fwrite with above data and close the file.
  227. culBytesWritten = fwrite((void *) ptszRandomDataBuffer,
  228. (size_t) 1,
  229. (size_t) cRandom,
  230. hFile);
  231. DH_ASSERT(culBytesWritten == cRandom);
  232. }
  233. if (NULL != hFile)
  234. {
  235. fclose(hFile);
  236. }
  237. // Call StgCreateDocfile with STGM_CONVERT now.
  238. if (S_OK == hr)
  239. {
  240. pIStorage = NULL;
  241. HRESULT hr2 = StgCreateDocfile(
  242. pOleStrTemp,
  243. pTestChanceDF->GetRootMode() | STGM_CONVERT,
  244. 0,
  245. &pIStorage);
  246. if(STG_S_CONVERTED == hr2 && pIStorage!=NULL)
  247. {
  248. DH_TRACE((DH_LVL_TRACE1, TEXT("StgCreateDocFile passed as expected.")));
  249. }
  250. else
  251. {
  252. DH_TRACE((
  253. DH_LVL_TRACE1,
  254. TEXT("StgCreateDocFile failed unexpectedly, hr = 0x%lx ."), hr));
  255. hr = (hr2==S_OK)?E_FAIL:hr2;
  256. }
  257. }
  258. // Commit the Root DocFile
  259. if (S_OK == hr)
  260. {
  261. hr = pIStorage->Commit(STGC_DEFAULT);
  262. if (S_OK == hr)
  263. {
  264. DH_TRACE((
  265. DH_LVL_TRACE1,
  266. TEXT("IStorage::Commit completed successfully.")));
  267. }
  268. else
  269. {
  270. DH_TRACE((
  271. DH_LVL_TRACE1,
  272. TEXT("IStorage::Commit unsuccessful, hr=0x%lx."), hr));
  273. }
  274. }
  275. // Close the root docfile
  276. if (NULL != pIStorage)
  277. {
  278. ulRef = pIStorage->Release();
  279. DH_ASSERT(0 == ulRef);
  280. pIStorage = NULL;
  281. }
  282. // Verify the CRC by opening Root DocFile, reading its CONTENTS stream,
  283. // calculating CRC on that and comparing that against the earlier CRC.
  284. if (S_OK == hr)
  285. {
  286. pIStorage = NULL;
  287. hr = StgOpenStorage(
  288. pOleStrTemp,
  289. NULL,
  290. pTestChanceDF->GetRootMode(),
  291. NULL,
  292. 0,
  293. &pIStorage);
  294. if(S_OK == hr)
  295. {
  296. DH_TRACE((DH_LVL_TRACE1, TEXT("StgOpenStorage passed as expected.")));
  297. if(NULL == pIStorage)
  298. {
  299. DH_TRACE((
  300. DH_LVL_TRACE1,
  301. TEXT("StgCreateDocfile failed to return IStorage"),
  302. hr));
  303. hr = E_FAIL;
  304. }
  305. }
  306. else
  307. {
  308. DH_TRACE((
  309. DH_LVL_TRACE1,
  310. TEXT("StgOpenStorage failed unexpectedly, hr = 0x%lx."), hr));
  311. }
  312. }
  313. if(S_OK == hr)
  314. {
  315. hr = pIStorage->EnumElements(0, NULL, 0, &penumWalk);
  316. DH_HRCHECK(hr, TEXT("IStorage::EnumElements")) ;
  317. }
  318. if ( S_OK == hr )
  319. {
  320. hr = CoGetMalloc(MEMCTX_TASK, &pMalloc);
  321. DH_HRCHECK(hr, TEXT("CoGetMalloc")) ;
  322. }
  323. // Call Next with celt equal to zero
  324. while((S_OK == hr) && (S_OK == penumWalk->Next(1, &statStg , NULL)))
  325. {
  326. cItemsInConvertedDocFile++;
  327. //Convert OLECHAR to TCHAR
  328. hr = OleStringToTString(statStg.pwcsName, &ptszConvName);
  329. DH_HRCHECK(hr, TEXT("OleStringToTString")) ;
  330. if(S_OK == hr)
  331. {
  332. if((statStg.type != STGTY_STREAM) ||
  333. (0 != _tcscmp(ptszConvName, STG_CONVERTED_NAME)))
  334. {
  335. DH_TRACE((
  336. DH_LVL_TRACE1,
  337. TEXT("Converted DocFile containes other than %s stream"),
  338. STG_CONVERTED_NAME));
  339. hr = E_FAIL;
  340. }
  341. }
  342. if(S_OK == hr)
  343. {
  344. hr = CalculateCRCForDocFileStmData(
  345. pIStorage,
  346. STG_CONVERTED_NAME,
  347. statStg.cbSize.LowPart,
  348. &dwActCRC);
  349. DH_HRCHECK(hr, TEXT("CalculateCRCForDocFileStmData")) ;
  350. }
  351. if(NULL != statStg.pwcsName)
  352. {
  353. pMalloc->Free(statStg.pwcsName);
  354. statStg.pwcsName = NULL;
  355. }
  356. }
  357. // Release LPENUMSTATSTG pointer
  358. if(NULL != penumWalk)
  359. {
  360. ulRef = penumWalk->Release();
  361. DH_ASSERT(0 == ulRef);
  362. penumWalk = NULL;
  363. }
  364. // Release pMalloc
  365. if(NULL != pMalloc)
  366. {
  367. pMalloc->Release();
  368. pMalloc = NULL;
  369. }
  370. // Close the root docfile
  371. if (NULL != pIStorage)
  372. {
  373. ulRef = pIStorage->Release();
  374. DH_ASSERT(0 == ulRef);
  375. pIStorage = NULL;
  376. }
  377. // if everything goes well, log test as passed else failed.
  378. if ((S_OK == hr) &&
  379. (1 == cItemsInConvertedDocFile) &&
  380. (dwMemCRC == dwActCRC))
  381. {
  382. DH_TRACE((DH_LVL_TRACE1,
  383. TEXT("CRC of ordinary file & converted DocFile equal as exp.")));
  384. DH_TRACE((DH_LVL_TRACE1,
  385. TEXT("Ordinary file contents converted to CONTENTS stm as exp.")));
  386. DH_LOG((LOG_PASS, TEXT("Test variation ROOTTEST_100 passed.")) );
  387. }
  388. else
  389. {
  390. DH_TRACE((DH_LVL_TRACE1,
  391. TEXT("CRC don't match or docfile not correctly cpnverted unexp")));
  392. DH_LOG((LOG_FAIL,
  393. TEXT("Test variation ROOTTEST_100 failed, hr=0x%lx."),
  394. hr) );
  395. // test failed. make it look like it failed.
  396. hr = FirstError (hr, E_FAIL);
  397. }
  398. // Cleanup
  399. // Delete Chance docfile tree
  400. if(NULL != pTestChanceDF)
  401. {
  402. hr2 = pTestChanceDF->DeleteChanceDocFileTree(
  403. pTestChanceDF->GetChanceDFRoot());
  404. DH_HRCHECK(hr2, TEXT("pTestChanceDF->DeleteChanceFileDocTree")) ;
  405. delete pTestChanceDF;
  406. pTestChanceDF = NULL;
  407. }
  408. // Delete temp string
  409. if(NULL != pOleStrTemp)
  410. {
  411. delete pOleStrTemp;
  412. pOleStrTemp = NULL;
  413. }
  414. if(NULL != ptszRandomDataBuffer)
  415. {
  416. delete ptszRandomDataBuffer;
  417. ptszRandomDataBuffer = NULL;
  418. }
  419. if(NULL != ptszConvName)
  420. {
  421. delete ptszConvName;
  422. ptszConvName = NULL;
  423. }
  424. // Delete DataGen object
  425. if(NULL != pdgu)
  426. {
  427. delete pdgu;
  428. pdgu = NULL;
  429. }
  430. if(NULL != pdgi)
  431. {
  432. delete pdgi;
  433. pdgi = NULL;
  434. }
  435. // Delete the docfile on disk
  436. if((S_OK == hr) && (NULL != pRootDocFileName))
  437. {
  438. if(FALSE == DeleteFile(pRootDocFileName))
  439. {
  440. hr2 = HRESULT_FROM_WIN32(GetLastError()) ;
  441. DH_HRCHECK(hr2, TEXT("DeleteFile")) ;
  442. }
  443. }
  444. // Delete temp string
  445. if(NULL != pRootDocFileName)
  446. {
  447. delete pRootDocFileName;
  448. pRootDocFileName = NULL;
  449. }
  450. // Stop logging the test
  451. DH_TRACE((DH_LVL_TRACE1, TEXT("Test variation ROOTTEST_100 finished")) );
  452. DH_TRACE((DH_LVL_TRACE1, TEXT("--------------------------------------------")) );
  453. return hr;
  454. }
  455. //----------------------------------------------------------------------------
  456. //
  457. // Test: ROOTTEST_101
  458. //
  459. // Synposis: From 4 to 16 temporary (NULL name) root docfiles are created,
  460. // committed, Stat'ed, determined to be a storage object, released,
  461. // and instantiated with STG_E_FAILIFTHERE to prove that the temporary
  462. // docfile remained persistent. The docfile is then deleted.
  463. //
  464. // Arguments:[argc]
  465. // [argv]
  466. //
  467. // Returns: HRESULT
  468. //
  469. // History: 24-June-1996 NarindK Created.
  470. //
  471. // Notes: This test runs in direct, transacted, and transacted deny write
  472. // modes
  473. //
  474. // New Test Notes:
  475. // 1. Old File: LRCONV.CXX
  476. // 2. Old name of test : LegitRootNull test
  477. // New Name of test : ROOTTEST_101
  478. // 3. To run the test, do the following at command prompt.
  479. // a. stgbase /seed:2 /dfdepth:0-0 /dfstg:0-0 /dfstm:0-0 /t:ROOTTEST-101
  480. // /dfRootMode:dirReadWriteShEx
  481. // b. stgbase /seed:2 /dfdepth:0-0 /dfstg:0-0 /dfstm:0-0 /t:ROOTTEST-101
  482. // /dfRootMode:xactReadWriteShEx
  483. // c. stgbase /seed:2 /dfdepth:0-0 /dfstg:0-0 /dfstm:0-0 /t:ROOTTEST-101
  484. // /dfRootMode:xactReadWriteShDenyW
  485. //
  486. // BUGNOTE: Conversion: ROOTTEST-101 NO
  487. //
  488. //-----------------------------------------------------------------------------
  489. HRESULT ROOTTEST_101(int argc, char *argv[])
  490. {
  491. HRESULT hr = S_OK;
  492. HRESULT hr2 = S_OK;
  493. ChanceDF *pTestChanceDF = NULL;
  494. DWORD dwRootMode = NULL;
  495. LPSTORAGE pIStorage = NULL;
  496. ULONG ulRef = 0;
  497. DG_INTEGER *pdgi = NULL;
  498. STATSTG statStg;
  499. LPMALLOC pMalloc = NULL;
  500. LPTSTR ptszRootTempName = NULL;
  501. LPOLESTR poszRootTempName = NULL;
  502. BOOL fPass = TRUE;
  503. ULONG cNumVars = 0;
  504. ULONG cRandomMinVar = 4;
  505. ULONG cRandomMaxVar = 16;
  506. // Not for 2phase. Bail.
  507. if (DoingDistrib ())
  508. {
  509. return S_OK;
  510. }
  511. DH_FUNCENTRY(NULL, DH_LVL_DFLIB, _TEXT("ROOTTEST_101"));
  512. DH_TRACE((DH_LVL_TRACE1, TEXT("--------------------------------------------")) );
  513. DH_TRACE((DH_LVL_TRACE1, TEXT("Test variation ROOTTEST_101 started.")) );
  514. DH_TRACE((DH_LVL_TRACE1,
  515. TEXT("Attempt legitimate ops on temporaray root docfile.")));
  516. // Create the new ChanceDocFile tree that would consist of chance nodes.
  517. if (S_OK == hr)
  518. {
  519. pTestChanceDF = new ChanceDF();
  520. if(NULL == pTestChanceDF)
  521. {
  522. hr = E_OUTOFMEMORY;
  523. }
  524. DH_HRCHECK(hr, TEXT("new ChanceDF")) ;
  525. }
  526. if (S_OK == hr)
  527. {
  528. hr = pTestChanceDF->CreateFromParams(argc, argv);
  529. DH_HRCHECK(hr, TEXT("ChanceDF::CreateFromParams")) ;
  530. }
  531. if (S_OK == hr)
  532. {
  533. dwRootMode = pTestChanceDF->GetRootMode();
  534. DH_TRACE((
  535. DH_LVL_TRACE1,
  536. TEXT("Run Mode for ROOTTEST_101, Access mode: %lx"),
  537. pTestChanceDF->GetRootMode()));
  538. }
  539. if(S_OK == hr)
  540. {
  541. // Create a new DataGen object to create random numbers.
  542. pdgi = new(NullOnFail) DG_INTEGER(pTestChanceDF->GetSeed());
  543. if (NULL == pdgi)
  544. {
  545. hr = E_OUTOFMEMORY;
  546. }
  547. DH_HRCHECK(hr, TEXT("new DG_INTEGER")) ;
  548. }
  549. // Generate RootDocFile name
  550. if(S_OK == hr)
  551. {
  552. hr = pdgi->Generate(&cNumVars, cRandomMinVar, cRandomMaxVar);
  553. DH_HRCHECK(hr, TEXT("Generate")) ;
  554. }
  555. if ( S_OK == hr )
  556. {
  557. hr = CoGetMalloc(MEMCTX_TASK, &pMalloc);
  558. DH_HRCHECK(hr, TEXT("CoGetMalloc")) ;
  559. }
  560. while((0 != cNumVars) && (S_OK == hr) && (TRUE == fPass))
  561. {
  562. cNumVars--;
  563. DH_TRACE((DH_LVL_TRACE1, TEXT("cNumVars = %ld "), cNumVars));
  564. // Try calling StgCreateDocFile with NULL name - temporary root DocFile
  565. if (S_OK == hr)
  566. {
  567. pIStorage = NULL;
  568. hr = StgCreateDocfile(
  569. NULL,
  570. pTestChanceDF->GetRootMode() | STGM_CREATE,
  571. 0,
  572. &pIStorage);
  573. DH_HRCHECK(hr, TEXT(" StgCreateDocfile")) ;
  574. }
  575. // Now do a valid commit. BUGBUG: Use random modes..
  576. if (S_OK == hr)
  577. {
  578. hr = pIStorage->Commit(STGC_DEFAULT);
  579. DH_HRCHECK(hr, TEXT("IStorage::Commit")) ;
  580. }
  581. // Do a Stat on root Docfile
  582. if (S_OK == hr)
  583. {
  584. hr = pIStorage->Stat(&statStg, STATFLAG_DEFAULT);
  585. DH_HRCHECK(hr, TEXT("IStorage::Stat")) ;
  586. }
  587. if(S_OK == hr)
  588. {
  589. //Convert OLECHAR to TCHAR
  590. hr = OleStringToTString(statStg.pwcsName, &ptszRootTempName);
  591. DH_HRCHECK(hr, TEXT("OleStringToTString")) ;
  592. }
  593. if(S_OK == hr)
  594. {
  595. // Convert TCHAR name to OLECHAR
  596. hr = TStringToOleString(ptszRootTempName, &poszRootTempName);
  597. DH_HRCHECK(hr, TEXT("TStringToOleString")) ;
  598. }
  599. // Check with StgIsStorage
  600. if(S_OK == hr)
  601. {
  602. hr = StgIsStorageFile(poszRootTempName);
  603. DH_HRCHECK(hr, TEXT("StgIsStorageFile"));
  604. }
  605. // Close the root docfile
  606. if (S_OK == hr)
  607. {
  608. ulRef = pIStorage->Release();
  609. DH_ASSERT(0 == ulRef);
  610. }
  611. // Again call StgIsStorage now
  612. if(S_OK == hr)
  613. {
  614. hr = StgIsStorageFile(poszRootTempName);
  615. DH_HRCHECK(hr, TEXT("StgIsStorageFile"));
  616. }
  617. // Call StgCreateDocFile with STGM_FAILIFTHERE flag. This should fail.
  618. if (S_OK == hr)
  619. {
  620. pIStorage = NULL;
  621. HRESULT hr2 = StgCreateDocfile(
  622. poszRootTempName,
  623. pTestChanceDF->GetRootMode() | STGM_FAILIFTHERE,
  624. 0,
  625. &pIStorage);
  626. if((S_OK != hr2) && (NULL == pIStorage))
  627. {
  628. DH_TRACE((
  629. DH_LVL_TRACE1,
  630. TEXT("StgCreateDocFile failed as expected, hr = 0x%lx."),hr2));
  631. }
  632. else
  633. {
  634. DH_TRACE((
  635. DH_LVL_ERROR,
  636. TEXT("StgCreateDocFile passed unexpectedly,hr=0x%lx "), hr2));
  637. hr = (hr2==S_OK)?E_FAIL:hr2;
  638. fPass = FALSE;
  639. }
  640. }
  641. // Try calling StgCreateDocFile with STGM_CREATE| STGM_DELETEONRELEASE
  642. if (S_OK == hr)
  643. {
  644. pIStorage = NULL;
  645. hr = StgCreateDocfile(
  646. poszRootTempName,
  647. pTestChanceDF->GetRootMode()|
  648. STGM_CREATE |
  649. STGM_DELETEONRELEASE,
  650. 0,
  651. &pIStorage);
  652. DH_HRCHECK(hr, TEXT(" StgCreateDocfile")) ;
  653. }
  654. // Now do a valid commit. BUGBUG: Use random modes..
  655. if (S_OK == hr)
  656. {
  657. hr = pIStorage->Commit(STGC_DEFAULT);
  658. DH_HRCHECK(hr, TEXT("IStorage::Commit")) ;
  659. }
  660. // Delete temp string
  661. if(NULL != ptszRootTempName)
  662. {
  663. delete ptszRootTempName;
  664. ptszRootTempName = NULL;
  665. }
  666. if(NULL != poszRootTempName)
  667. {
  668. delete poszRootTempName;
  669. poszRootTempName = NULL;
  670. }
  671. // Free the statStg.pwcsName
  672. if(NULL != statStg.pwcsName)
  673. {
  674. pMalloc->Free(statStg.pwcsName);
  675. statStg.pwcsName = NULL;
  676. }
  677. // Stat the root IStorage
  678. if (S_OK == hr)
  679. {
  680. hr = pIStorage->Stat(&statStg, STATFLAG_DEFAULT);
  681. DH_HRCHECK(hr, TEXT("IStorage::Stat")) ;
  682. }
  683. if(S_OK == hr)
  684. {
  685. //Convert OLECHAR to TCHAR
  686. hr = OleStringToTString(statStg.pwcsName, &ptszRootTempName);
  687. DH_HRCHECK(hr, TEXT("OleStringToTString")) ;
  688. }
  689. if(S_OK == hr)
  690. {
  691. // Convert TCHAR name to OLECHAR
  692. hr = TStringToOleString(ptszRootTempName, &poszRootTempName);
  693. DH_HRCHECK(hr, TEXT("TStringToOleString")) ;
  694. }
  695. // Check with StgIsStorage
  696. if(S_OK == hr)
  697. {
  698. hr = StgIsStorageFile(poszRootTempName);
  699. DH_HRCHECK(hr, TEXT("StgIsStorageFile")) ;
  700. }
  701. // Close the root docfile
  702. if (S_OK == hr)
  703. {
  704. ulRef = pIStorage->Release();
  705. DH_ASSERT(0 == ulRef);
  706. }
  707. // Again call StgIsStorage now
  708. if(S_OK == hr)
  709. {
  710. HRESULT hr2 = StgIsStorageFile(poszRootTempName);
  711. if(STG_E_FILENOTFOUND == hr2)
  712. {
  713. DH_TRACE((
  714. DH_LVL_TRACE1,
  715. TEXT("StgIsStorage returned hr = 0x%lx as expected."), hr2));
  716. }
  717. else
  718. {
  719. DH_TRACE((
  720. DH_LVL_ERROR,
  721. TEXT("StgIsStorage returned hr = 0x%lx unexpectedly."), hr2));
  722. hr = (hr2==S_OK)?E_FAIL:hr2;
  723. fPass = FALSE;
  724. }
  725. }
  726. // Delete temp string
  727. if(NULL != ptszRootTempName)
  728. {
  729. delete ptszRootTempName;
  730. ptszRootTempName = NULL;
  731. }
  732. if(NULL != poszRootTempName)
  733. {
  734. delete poszRootTempName;
  735. poszRootTempName = NULL;
  736. }
  737. // Free the statStg.pwcsName
  738. if(NULL != statStg.pwcsName)
  739. {
  740. pMalloc->Free(statStg.pwcsName);
  741. statStg.pwcsName = NULL;
  742. }
  743. }
  744. // if everything goes well, log test as passed else failed.
  745. if ((S_OK == hr) && (TRUE == fPass))
  746. {
  747. DH_LOG((LOG_PASS, TEXT("Test variation ROOTTEST_101 passed.")) );
  748. }
  749. else
  750. {
  751. DH_LOG((LOG_FAIL,
  752. TEXT("Test variation ROOTTEST_101 failed, hr=0x%lx; fPass=%d."),
  753. hr,
  754. fPass));
  755. // test failed. make it look like it failed.
  756. hr = FirstError (hr, E_FAIL);
  757. }
  758. // Cleanup
  759. // Delete Chance docfile tree
  760. if(NULL != pTestChanceDF)
  761. {
  762. hr2 = pTestChanceDF->DeleteChanceDocFileTree(
  763. pTestChanceDF->GetChanceDFRoot());
  764. DH_HRCHECK(hr2, TEXT("pTestChanceDF->DeleteChanceFileDocTree")) ;
  765. delete pTestChanceDF;
  766. pTestChanceDF = NULL;
  767. }
  768. // Release pMalloc
  769. if(NULL != pMalloc)
  770. {
  771. pMalloc->Release();
  772. pMalloc = NULL;
  773. }
  774. // Delete DataGen object
  775. if(NULL != pdgi)
  776. {
  777. delete pdgi;
  778. pdgi = NULL;
  779. }
  780. // Stop logging the test
  781. DH_TRACE((DH_LVL_TRACE1, TEXT("Test variation ROOTTEST_101 finished")) );
  782. DH_TRACE((DH_LVL_TRACE1, TEXT("--------------------------------------------")) );
  783. return hr;
  784. }
  785. //----------------------------------------------------------------------------
  786. //
  787. // Test: ROOTTEST_102
  788. //
  789. // Synposis: From 4 to 16 times, a root docfile with a random name is created,
  790. // committed, Stat'ed, tested to see if it is a storage object,
  791. // released, tested to see if we still think it's a storage object,
  792. // and finally deleted from the file system.
  793. //
  794. // Arguments:[argc]
  795. // [argv]
  796. //
  797. // Returns: HRESULT
  798. //
  799. // Notes: This test runs in direct, transacted, and transacted deny write
  800. // modes
  801. //
  802. // New Test Notes:
  803. // 1. Old File: LRNORM.CXX
  804. // 2. Old name of test : LegitRootNull test
  805. // New Name of test : ROOTTEST_102
  806. // 3. To run the test, do the following at command prompt.
  807. // a. stgbase /seed:2 /dfdepth:0-0 /dfstg:0-0 /dfstm:0-0 /t:ROOTTEST-102
  808. // /dfRootMode:dirReadWriteShEx
  809. // b. stgbase /seed:2 /dfdepth:0-0 /dfstg:0-0 /dfstm:0-0 /t:ROOTTEST-102
  810. // /dfRootMode:xactReadWriteShEx
  811. // c. stgbase /seed:2 /dfdepth:0-0 /dfstg:0-0 /dfstm:0-0 /t:ROOTTEST-102
  812. // /dfRootMode:xactReadWriteShDenyW
  813. //
  814. // BUGNOTE: Conversion: ROOTTEST-102 NO
  815. //
  816. //-----------------------------------------------------------------------------
  817. HRESULT ROOTTEST_102(int argc, char *argv[])
  818. {
  819. HRESULT hr = S_OK;
  820. HRESULT hr2 = S_OK;
  821. ChanceDF *pTestChanceDF = NULL;
  822. DWORD dwRootMode = NULL;
  823. LPSTORAGE pIStorage = NULL;
  824. ULONG ulRef = 0;
  825. DG_INTEGER *pdgi = NULL;
  826. DG_STRING *pdgu = NULL;
  827. STATSTG statStg;
  828. LPMALLOC pMalloc = NULL;
  829. LPTSTR ptszRootTempName = NULL;
  830. LPOLESTR poszRootTempName = NULL;
  831. LPTSTR ptszRootName = NULL;
  832. LPOLESTR poszRootName = NULL;
  833. BOOL fPass = TRUE;
  834. ULONG cNumVars = 0;
  835. ULONG cRandomMinVar = 4;
  836. ULONG cRandomMaxVar = 16;
  837. FILE *hFileRootNonDocFile = NULL;
  838. // Not for 2phase. Bail.
  839. if (DoingDistrib ())
  840. {
  841. return S_OK;
  842. }
  843. DH_FUNCENTRY(NULL, DH_LVL_DFLIB, _TEXT("ROOTTEST_102"));
  844. DH_TRACE((DH_LVL_TRACE1, TEXT("--------------------------------------------")) );
  845. DH_TRACE((DH_LVL_TRACE1, TEXT("Test variation ROOTTEST_102 started.")) );
  846. DH_TRACE((DH_LVL_TRACE1,
  847. TEXT("Attempt legitimate ops on random root docfile.")));
  848. // Create the new ChanceDocFile tree that would consist of chance nodes.
  849. if (S_OK == hr)
  850. {
  851. pTestChanceDF = new ChanceDF();
  852. if(NULL == pTestChanceDF)
  853. {
  854. hr = E_OUTOFMEMORY;
  855. }
  856. }
  857. if (S_OK == hr)
  858. {
  859. hr = pTestChanceDF->CreateFromParams(argc, argv);
  860. DH_HRCHECK(hr, TEXT("pTestChanceDF->CreateFromParams")) ;
  861. }
  862. if (S_OK == hr)
  863. {
  864. dwRootMode = pTestChanceDF->GetRootMode();
  865. DH_TRACE((
  866. DH_LVL_TRACE1,
  867. TEXT("Run Mode for ROOTTEST_102, Access mode: %lx"),
  868. pTestChanceDF->GetRootMode()));
  869. }
  870. if(S_OK == hr)
  871. {
  872. // Create a new DataGen object to create random numbers.
  873. pdgi = new(NullOnFail) DG_INTEGER(pTestChanceDF->GetSeed());
  874. if (NULL == pdgi)
  875. {
  876. hr = E_OUTOFMEMORY;
  877. }
  878. }
  879. // Generate RootDocFile name
  880. if(S_OK == hr)
  881. {
  882. hr = pdgi->Generate(&cNumVars, cRandomMinVar, cRandomMaxVar);
  883. DH_HRCHECK(hr, TEXT("Generate")) ;
  884. }
  885. // Create DataGen
  886. if(S_OK == hr)
  887. {
  888. // Create a new DataGen object to create random UNICODE strings.
  889. pdgu = new(NullOnFail) DG_STRING(pTestChanceDF->GetSeed());
  890. if (NULL == pdgu)
  891. {
  892. hr = E_OUTOFMEMORY;
  893. }
  894. }
  895. if ( S_OK == hr )
  896. {
  897. hr = CoGetMalloc(MEMCTX_TASK, &pMalloc);
  898. DH_HRCHECK(hr, TEXT("CoGetMalloc")) ;
  899. }
  900. while((0 != cNumVars) && (S_OK == hr) && (TRUE == fPass))
  901. {
  902. cNumVars--;
  903. DH_TRACE((DH_LVL_TRACE1, TEXT("cNumVars = %ld "), cNumVars));
  904. // Generate RootDocFile name
  905. if(S_OK == hr)
  906. {
  907. hr=GenerateRandomName(pdgu, MINLENGTH,MAXLENGTH, &ptszRootName);
  908. DH_HRCHECK(hr, TEXT("GenerateRandomName")) ;
  909. }
  910. if(S_OK == hr)
  911. {
  912. // Convert TCHAR name to OLECHAR
  913. hr = TStringToOleString(ptszRootName, &poszRootName);
  914. DH_HRCHECK(hr, TEXT("TStringToOleString")) ;
  915. }
  916. // Try calling StgCreateDocFile with above random name
  917. if (S_OK == hr)
  918. {
  919. pIStorage = NULL;
  920. hr = StgCreateDocfile(
  921. poszRootName,
  922. dwRootMode | STGM_CREATE,
  923. 0,
  924. &pIStorage);
  925. DH_HRCHECK(hr, TEXT(" StgCreateDocfile")) ;
  926. }
  927. if(S_OK == hr)
  928. {
  929. DH_TRACE((DH_LVL_TRACE1, TEXT("StgCreateDocFile passed as expected.")));
  930. }
  931. else
  932. {
  933. DH_TRACE((
  934. DH_LVL_TRACE1,
  935. TEXT("StgCreateDocFile failed unexpectedly, hr=0x%lx"), hr));
  936. }
  937. // Now do a valid commit. BUGBUG: Use random modes..
  938. if (S_OK == hr)
  939. {
  940. hr = pIStorage->Commit(STGC_DEFAULT);
  941. DH_HRCHECK(hr, TEXT("IStorage::Commit")) ;
  942. }
  943. if (S_OK == hr)
  944. {
  945. DH_TRACE((
  946. DH_LVL_TRACE1,
  947. TEXT("IStorage::Commit completed successfully.")));
  948. }
  949. else
  950. {
  951. DH_TRACE((
  952. DH_LVL_TRACE1,
  953. TEXT("IStorage::Commit unsuccessful, hr=0x%lx."),
  954. hr));
  955. }
  956. // Do a Stat on root Docfile
  957. if (S_OK == hr)
  958. {
  959. hr = pIStorage->Stat(&statStg, STATFLAG_DEFAULT);
  960. DH_HRCHECK(hr, TEXT("IStorage::Stat")) ;
  961. }
  962. if (S_OK == hr)
  963. {
  964. DH_TRACE((
  965. DH_LVL_TRACE1,
  966. TEXT("IStorage::Stat completed successfully.")));
  967. }
  968. else
  969. {
  970. DH_TRACE((
  971. DH_LVL_TRACE1,
  972. TEXT("IStorage::Stat unsuccessful, hr=0x%lx."),
  973. hr));
  974. }
  975. if(S_OK == hr)
  976. {
  977. //Convert OLECHAR to TCHAR
  978. hr = OleStringToTString(statStg.pwcsName, &ptszRootTempName);
  979. DH_HRCHECK(hr, TEXT("OleStringToTString")) ;
  980. }
  981. if(S_OK == hr)
  982. {
  983. // Convert TCHAR name to OLECHAR
  984. hr = TStringToOleString(ptszRootTempName, &poszRootTempName);
  985. DH_HRCHECK(hr, TEXT("TStringToOleString")) ;
  986. }
  987. // Check with StgIsStorage
  988. if(S_OK == hr)
  989. {
  990. hr = StgIsStorageFile(poszRootTempName);
  991. }
  992. if(S_OK == hr)
  993. {
  994. DH_TRACE((
  995. DH_LVL_TRACE1,
  996. TEXT("StgIsStorage hr = 0x%lx as exp before root release."),
  997. hr));
  998. }
  999. else
  1000. {
  1001. DH_TRACE((
  1002. DH_LVL_TRACE1,
  1003. TEXT("StgIsStorage hr = 0x%lx unexp before root release."),
  1004. hr));
  1005. }
  1006. // Close the root docfile
  1007. if (S_OK == hr)
  1008. {
  1009. ulRef = pIStorage->Release();
  1010. DH_ASSERT(0 == ulRef);
  1011. }
  1012. // Again call StgIsStorage now
  1013. if(S_OK == hr)
  1014. {
  1015. hr = StgIsStorageFile(poszRootTempName);
  1016. }
  1017. if(S_OK == hr)
  1018. {
  1019. DH_TRACE((
  1020. DH_LVL_TRACE1,
  1021. TEXT("StgIsStorage hr = 0x%lx as exp after releasing rootDF"),
  1022. hr));
  1023. }
  1024. else
  1025. {
  1026. DH_TRACE((
  1027. DH_LVL_TRACE1,
  1028. TEXT("StgIsStorage hr=0x%lx unexp after releasing root DF."),
  1029. hr));
  1030. }
  1031. // Call StgCreateDocFile with STGM_FAILIFTHERE flag. This should fail.
  1032. if (S_OK == hr)
  1033. {
  1034. pIStorage = NULL;
  1035. HRESULT hr2 = StgCreateDocfile(
  1036. poszRootTempName,
  1037. dwRootMode | STGM_FAILIFTHERE,
  1038. 0,
  1039. &pIStorage);
  1040. DH_HRCHECK(hr2, TEXT(" StgCreateDocfile")) ;
  1041. if((S_OK != hr2) && (NULL == pIStorage))
  1042. {
  1043. DH_TRACE((
  1044. DH_LVL_TRACE1,
  1045. TEXT("StgCreateDocFile failed as expected, hr = 0x%lx."),hr2));
  1046. }
  1047. else
  1048. {
  1049. DH_TRACE((
  1050. DH_LVL_TRACE1,
  1051. TEXT("StgCreateDocFile passed unexpectedly,hr=0x%lx "), hr2));
  1052. hr = (S_OK==hr2)?E_FAIL:hr2;
  1053. fPass = FALSE;
  1054. }
  1055. }
  1056. // Delete temp string
  1057. if(NULL != ptszRootTempName)
  1058. {
  1059. delete ptszRootTempName;
  1060. ptszRootTempName = NULL;
  1061. }
  1062. if(NULL != poszRootTempName)
  1063. {
  1064. delete poszRootTempName;
  1065. poszRootTempName = NULL;
  1066. }
  1067. // Change the mode to READ_ONLY and then try.
  1068. if(S_OK == hr)
  1069. {
  1070. _tchmod(ptszRootName, _S_IREAD);
  1071. }
  1072. // Now call StgOpenStorage on it. It should fail.
  1073. if(S_OK == hr)
  1074. {
  1075. pIStorage = NULL;
  1076. HRESULT hr2 = StgOpenStorage(
  1077. poszRootTempName,
  1078. NULL,
  1079. dwRootMode,
  1080. NULL,
  1081. 0,
  1082. &pIStorage);
  1083. DH_HRCHECK(hr2, TEXT("StgOpenStorage")) ;
  1084. if((S_OK != hr2) && (NULL == pIStorage))
  1085. {
  1086. DH_TRACE((
  1087. DH_LVL_TRACE1,
  1088. TEXT("StgOpenStorage fail exp with Read Only mode, hr=0x%lx"),
  1089. hr2));
  1090. }
  1091. else
  1092. {
  1093. DH_TRACE((
  1094. DH_LVL_TRACE1,
  1095. TEXT("StgOpenStorage pass unexp with readonly mode,hr=0x%lx"),
  1096. hr2));
  1097. hr = (S_OK==hr2)?E_FAIL:hr2;
  1098. fPass = FALSE;
  1099. }
  1100. }
  1101. // Change back the mode to read/write.
  1102. if(S_OK == hr)
  1103. {
  1104. _tchmod(ptszRootName, _S_IREAD | _S_IWRITE);
  1105. }
  1106. // Try calling StgCreateDocFile with STGM_CREATE| STGM_DELETEONRELEASE
  1107. if (S_OK == hr)
  1108. {
  1109. pIStorage = NULL;
  1110. hr = StgCreateDocfile(
  1111. poszRootTempName,
  1112. pTestChanceDF->GetRootMode()|
  1113. STGM_CREATE |
  1114. STGM_DELETEONRELEASE,
  1115. 0,
  1116. &pIStorage);
  1117. DH_HRCHECK(hr, TEXT(" StgCreateDocfile")) ;
  1118. }
  1119. if(S_OK == hr)
  1120. {
  1121. DH_TRACE((
  1122. DH_LVL_TRACE1,
  1123. TEXT("StgCreateDocFile pass exp with mode \
  1124. STGM_CREATE|STGM_DELETEONRELEASE.")));
  1125. }
  1126. else
  1127. {
  1128. DH_TRACE((
  1129. DH_LVL_TRACE1,
  1130. TEXT("StgCreateDocFile failed unexp with mode \
  1131. STGM_CREATE | STGM_DELETEONRELEASE, hr=0x%lx"), hr));
  1132. }
  1133. // Now do a valid commit. BUGBUG: Use random modes..
  1134. if (S_OK == hr)
  1135. {
  1136. hr = pIStorage->Commit(STGC_DEFAULT);
  1137. DH_HRCHECK(hr, TEXT("IStorage::Commit")) ;
  1138. }
  1139. if (S_OK == hr)
  1140. {
  1141. DH_TRACE((
  1142. DH_LVL_TRACE1,
  1143. TEXT("IStorage::Commit completed successfully.")));
  1144. }
  1145. else
  1146. {
  1147. DH_TRACE((
  1148. DH_LVL_TRACE1,
  1149. TEXT("IStorage::Commit unsuccessful, hr=0x%lx."),
  1150. hr));
  1151. }
  1152. // Free the statStg.pwcsName
  1153. if(NULL != statStg.pwcsName)
  1154. {
  1155. pMalloc->Free(statStg.pwcsName);
  1156. statStg.pwcsName = NULL;
  1157. }
  1158. // Stat the root IStorage
  1159. if (S_OK == hr)
  1160. {
  1161. hr = pIStorage->Stat(&statStg, STATFLAG_DEFAULT);
  1162. DH_HRCHECK(hr, TEXT("IStorage::Stat")) ;
  1163. }
  1164. if (S_OK == hr)
  1165. {
  1166. DH_TRACE((
  1167. DH_LVL_TRACE1,
  1168. TEXT("IStorage::Stat completed successfully.")));
  1169. }
  1170. else
  1171. {
  1172. DH_TRACE((
  1173. DH_LVL_TRACE1,
  1174. TEXT("IStorage::Stat unsuccessful, hr=0x%lx."),
  1175. hr));
  1176. }
  1177. if(S_OK == hr)
  1178. {
  1179. //Convert OLECHAR to TCHAR
  1180. hr = OleStringToTString(statStg.pwcsName, &ptszRootTempName);
  1181. DH_HRCHECK(hr, TEXT("OleStringToTString")) ;
  1182. }
  1183. if(S_OK == hr)
  1184. {
  1185. // Convert TCHAR name to OLECHAR
  1186. hr = TStringToOleString(ptszRootTempName, &poszRootTempName);
  1187. DH_HRCHECK(hr, TEXT("TStringToOleString")) ;
  1188. }
  1189. // Check with StgIsStorage
  1190. if(S_OK == hr)
  1191. {
  1192. hr = StgIsStorageFile(poszRootTempName);
  1193. }
  1194. if(S_OK == hr)
  1195. {
  1196. DH_TRACE((
  1197. DH_LVL_TRACE1,
  1198. TEXT("StgIsStorage hr = 0x%lx as exp before root release."),
  1199. hr));
  1200. }
  1201. else
  1202. {
  1203. DH_TRACE((
  1204. DH_LVL_TRACE1,
  1205. TEXT("StgIsStorage hr=0x%lx unexp before root release."),
  1206. hr));
  1207. }
  1208. // Close the root docfile
  1209. if (S_OK == hr)
  1210. {
  1211. ulRef = pIStorage->Release();
  1212. DH_ASSERT(0 == ulRef);
  1213. }
  1214. // Again call StgIsStorage now
  1215. if(S_OK == hr)
  1216. {
  1217. HRESULT hr2 = StgIsStorageFile(poszRootTempName);
  1218. if(STG_E_FILENOTFOUND == hr2)
  1219. {
  1220. DH_TRACE((
  1221. DH_LVL_TRACE1,
  1222. TEXT("StgIsStorage hr=0x%lx as exp after root reelase."),
  1223. hr2));
  1224. }
  1225. else
  1226. {
  1227. DH_TRACE((
  1228. DH_LVL_TRACE1,
  1229. TEXT("StgIsStorage hr = 0x%lx unexp after root release."),
  1230. hr2));
  1231. hr = (S_OK==hr2)?E_FAIL:hr2;
  1232. fPass = FALSE;
  1233. }
  1234. }
  1235. // Check if a non docfile fails StgIsStorage
  1236. hFileRootNonDocFile = _tfopen(ptszRootName, TEXT("w+"));
  1237. if(NULL == hFileRootNonDocFile)
  1238. {
  1239. DH_TRACE((
  1240. DH_LVL_ERROR,
  1241. TEXT("fopen unexpectedly failed %s "),
  1242. ptszRootName));
  1243. }
  1244. else
  1245. {
  1246. DH_TRACE((
  1247. DH_LVL_TRACE1,
  1248. TEXT("fopen passed as expected of file %s "),
  1249. ptszRootName));
  1250. fclose(hFileRootNonDocFile);
  1251. }
  1252. // Delete temp string
  1253. if(NULL != ptszRootTempName)
  1254. {
  1255. delete ptszRootTempName;
  1256. ptszRootTempName = NULL;
  1257. }
  1258. if(NULL != poszRootTempName)
  1259. {
  1260. delete poszRootTempName;
  1261. poszRootTempName = NULL;
  1262. }
  1263. if(NULL != poszRootName)
  1264. {
  1265. delete poszRootName;
  1266. poszRootName = NULL;
  1267. }
  1268. // Free the statStg.pwcsName
  1269. if(NULL != statStg.pwcsName)
  1270. {
  1271. pMalloc->Free(statStg.pwcsName);
  1272. statStg.pwcsName = NULL;
  1273. }
  1274. if((S_OK == hr) && (NULL != ptszRootName))
  1275. {
  1276. if(FALSE == DeleteFile(ptszRootName))
  1277. {
  1278. hr = HRESULT_FROM_WIN32(GetLastError()) ;
  1279. DH_HRCHECK(hr, TEXT("DeleteFile")) ;
  1280. }
  1281. delete ptszRootName;
  1282. ptszRootName = NULL;
  1283. }
  1284. }
  1285. // if everything goes well, log test as passed else failed.
  1286. if ((S_OK == hr) && (TRUE == fPass))
  1287. {
  1288. DH_LOG((LOG_PASS, TEXT("Test variation ROOTTEST_102 passed.")) );
  1289. }
  1290. else
  1291. {
  1292. DH_LOG((LOG_FAIL,
  1293. TEXT("Test variation ROOTTEST_102 failed, hr=0x%lx."),
  1294. hr) );
  1295. // test failed. make it look like it failed.
  1296. hr = FirstError (hr, E_FAIL);
  1297. }
  1298. // Cleanup
  1299. // Delete Chance docfile tree
  1300. if(NULL != pTestChanceDF)
  1301. {
  1302. hr2 = pTestChanceDF->DeleteChanceDocFileTree(
  1303. pTestChanceDF->GetChanceDFRoot());
  1304. DH_HRCHECK(hr2, TEXT("pTestChanceDF->DeleteChanceFileDocTree")) ;
  1305. delete pTestChanceDF;
  1306. pTestChanceDF = NULL;
  1307. }
  1308. // Release pMalloc
  1309. if(NULL != pMalloc)
  1310. {
  1311. pMalloc->Release();
  1312. pMalloc = NULL;
  1313. }
  1314. // Delete DataGen object
  1315. if(NULL != pdgi)
  1316. {
  1317. delete pdgi;
  1318. pdgi = NULL;
  1319. }
  1320. if(NULL != pdgu)
  1321. {
  1322. delete pdgu;
  1323. pdgu = NULL;
  1324. }
  1325. // Stop logging the test
  1326. DH_TRACE((DH_LVL_TRACE1, TEXT("Test variation ROOTTEST_102 finished")) );
  1327. DH_TRACE((DH_LVL_TRACE1, TEXT("--------------------------------------------")) );
  1328. return hr;
  1329. }
  1330. //----------------------------------------------------------------------------
  1331. //
  1332. // Test: ROOTTEST_103
  1333. //
  1334. // Synopsis:A root docfile with a random name is created, committed, and release
  1335. // For each of the access mode combinations listed in the table, the
  1336. // the root docfile is instantiated with the permissions, and then
  1337. // for each of the access mode combinations, a 2nd instantiation of
  1338. // the root docfile is attempted. After the 2nd instantiation call,
  1339. // a check is made to determine whether the instantiation should have
  1340. // worked or failed depending upon the 1st instantiation mode. The
  1341. // second instantiation is released if necessary. Once all modes
  1342. // have been attempted for the 2nd instantiation, the 1st instantiation
  1343. // is released, and the test goes on the the next access mode entry
  1344. // in the array.
  1345. //
  1346. // Arguments:[argc]
  1347. // [argv]
  1348. //
  1349. // Returns: HRESULT
  1350. //
  1351. // History: 26-June-1996 NarindK Created
  1352. // 12-Aug-1996 JiminLi Updated
  1353. //
  1354. // Notes: This test runs in direct, transacted modes
  1355. //
  1356. // New Test Notes:
  1357. // 1. Old File: LRMULTAC.CXX
  1358. // 2. Old name of test : LegitRootMultAccess test
  1359. // New Name of test : ROOTTEST_103
  1360. // 3. To run the test, do the following at command prompt.
  1361. // a. stgbase /seed:0 /dfdepth:0-0 /dfstg:0-0 /dfstm:0-0 /t:ROOTTEST-103
  1362. // /dfRootMode:dirReadWriteShEx
  1363. // b. stgbase /seed:0 /dfdepth:0-0 /dfstg:0-0 /dfstm:0-0 /t:ROOTTEST-103
  1364. // /dfRootMode:xactReadWriteShEx
  1365. //
  1366. // BUGNOTE: Conversion: ROOTTEST-103 NO
  1367. //
  1368. //-----------------------------------------------------------------------------
  1369. HRESULT ROOTTEST_103(int argc, char *argv[])
  1370. {
  1371. HRESULT hr = S_OK;
  1372. HRESULT hr2 = S_OK;
  1373. ChanceDF *pTestChanceDF = NULL;
  1374. LPTSTR pRootDocFileName = NULL;
  1375. DWORD dwRootMode = NULL;
  1376. DWORD dwOpenMode = NULL;
  1377. LPSTORAGE pIStorageFirst = NULL;
  1378. LPSTORAGE pIStorageSecond = NULL;
  1379. LPOLESTR pOleStrTemp = NULL;
  1380. ULONG ulRef = 0;
  1381. DG_STRING *pdgu = NULL;
  1382. BOOL fShouldWork = FALSE;
  1383. ULONG count = 0;
  1384. ULONG subcount = 0;
  1385. BOOL fPass = TRUE;
  1386. struct
  1387. {
  1388. DWORD usMode;
  1389. BOOL afPermsOk[30];
  1390. } aPerms[15]={ STGM_READ,
  1391. F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,
  1392. T,T,T,T,F,T,F,T,F,T,F,T,F,T,F,
  1393. STGM_WRITE,
  1394. F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,
  1395. T,T,T,T,F,F,F,T,T,F,F,T,T,F,F,
  1396. STGM_READWRITE,
  1397. F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,
  1398. T,T,T,T,F,F,F,T,F,F,F,T,F,F,F,
  1399. STGM_READ | STGM_SHARE_DENY_NONE,
  1400. F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,
  1401. T,T,T,T,F,T,F,T,F,T,F,T,F,T,F,
  1402. STGM_READ | STGM_SHARE_DENY_READ,
  1403. F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,
  1404. F,T,F,F,F,F,F,T,F,T,F,F,F,F,F,
  1405. STGM_READ | STGM_SHARE_DENY_WRITE,
  1406. F,F,F,F,F,T,F,F,F,F,F,F,F,F,F,
  1407. T,F,F,T,F,T,F,F,F,F,F,F,F,F,F,
  1408. STGM_READ | STGM_SHARE_EXCLUSIVE,
  1409. F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,
  1410. F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,
  1411. STGM_WRITE | STGM_SHARE_DENY_NONE,
  1412. F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,
  1413. T,T,T,T,F,F,F,T,T,F,F,T,T,F,F,
  1414. STGM_WRITE | STGM_SHARE_DENY_READ,
  1415. F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,
  1416. F,T,F,F,F,F,F,T,T,F,F,F,F,F,F,
  1417. STGM_WRITE | STGM_SHARE_DENY_WRITE,
  1418. F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,
  1419. T,F,F,T,F,F,F,F,F,F,F,F,F,F,F,
  1420. STGM_WRITE | STGM_SHARE_EXCLUSIVE,
  1421. F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,
  1422. F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,
  1423. STGM_READWRITE | STGM_SHARE_DENY_NONE,
  1424. F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,
  1425. T,T,T,T,F,F,F,T,F,F,F,T,F,F,F,
  1426. STGM_READWRITE | STGM_SHARE_DENY_READ,
  1427. F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,
  1428. F,T,F,F,F,F,F,T,F,F,F,F,F,F,F,
  1429. STGM_READWRITE | STGM_SHARE_DENY_WRITE,
  1430. F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,
  1431. T,F,F,T,F,F,F,F,F,F,F,F,F,F,F,
  1432. STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
  1433. F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,
  1434. F,F,F,F,F,F,F,F,F,F,F,F,F,F,F
  1435. };
  1436. // Not for 2phase. Bail.
  1437. if (DoingDistrib ())
  1438. {
  1439. return S_OK;
  1440. }
  1441. DH_FUNCENTRY(NULL, DH_LVL_DFLIB, _TEXT("ROOTTEST_103"));
  1442. DH_TRACE((DH_LVL_TRACE1, TEXT("--------------------------------------------")) );
  1443. DH_TRACE((DH_LVL_TRACE1, TEXT("Test variation ROOTTEST_103 started.")) );
  1444. DH_TRACE((DH_LVL_TRACE1,
  1445. TEXT("Attempt multiple accesses on a root docfile.")));
  1446. // Create the new ChanceDocFile tree that would consist of chance nodes.
  1447. if (S_OK == hr)
  1448. {
  1449. pTestChanceDF = new ChanceDF();
  1450. if(NULL == pTestChanceDF)
  1451. {
  1452. hr = E_OUTOFMEMORY;
  1453. }
  1454. }
  1455. if (S_OK == hr)
  1456. {
  1457. hr = pTestChanceDF->CreateFromParams(argc, argv);
  1458. DH_HRCHECK(hr, TEXT("pTestChanceDF->CreateFromParams")) ;
  1459. }
  1460. if (S_OK == hr)
  1461. {
  1462. dwRootMode = pTestChanceDF->GetRootMode();
  1463. DH_TRACE((
  1464. DH_LVL_TRACE1,
  1465. TEXT("Run Mode for ROOTTEST_103, Access mode: %lx"),
  1466. pTestChanceDF->GetRootMode()));
  1467. }
  1468. if(S_OK == hr)
  1469. {
  1470. // Create a new DataGen object to create random UNICODE strings.
  1471. pdgu = new(NullOnFail) DG_STRING(pTestChanceDF->GetSeed());
  1472. if (NULL == pdgu)
  1473. {
  1474. hr = E_OUTOFMEMORY;
  1475. }
  1476. }
  1477. // Generate RootDocFile name
  1478. if(S_OK == hr)
  1479. {
  1480. hr = GenerateRandomName(pdgu, MINLENGTH,MAXLENGTH, &pRootDocFileName);
  1481. DH_HRCHECK(hr, TEXT("GenerateRandomName")) ;
  1482. }
  1483. if(S_OK == hr)
  1484. {
  1485. // Convert RootDocFile name to OLECHAR
  1486. hr = TStringToOleString(pRootDocFileName, &pOleStrTemp);
  1487. DH_HRCHECK(hr, TEXT("TStringToOleString")) ;
  1488. }
  1489. // Try calling StgCreateDocFile with mode as per dwRootFlags
  1490. // Just make sure no DocFile of the name exists. Delete if it does.
  1491. if (S_OK == hr)
  1492. {
  1493. pIStorageFirst = NULL;
  1494. hr = StgCreateDocfile(
  1495. pOleStrTemp,
  1496. dwRootMode | STGM_CREATE,
  1497. 0,
  1498. &pIStorageFirst);
  1499. }
  1500. if(S_OK == hr)
  1501. {
  1502. DH_TRACE((DH_LVL_TRACE1, TEXT("StgCreateDocFile passed as expected.")));
  1503. }
  1504. else
  1505. {
  1506. DH_TRACE((
  1507. DH_LVL_TRACE1,
  1508. TEXT("StgCreateDocFile failed unexpectedly, hr = 0x%lx ."), hr));
  1509. }
  1510. // Now do a valid commit
  1511. if (S_OK == hr)
  1512. {
  1513. hr = pIStorageFirst->Commit(STGC_ONLYIFCURRENT);
  1514. }
  1515. if (S_OK == hr)
  1516. {
  1517. DH_TRACE((
  1518. DH_LVL_TRACE1,
  1519. TEXT("IStorage::Commit completed successfully.")));
  1520. }
  1521. else
  1522. {
  1523. DH_TRACE((
  1524. DH_LVL_TRACE1,
  1525. TEXT("IStorage::Commit couldn't complete successfully.")));
  1526. }
  1527. // Close the root docfile
  1528. if (S_OK == hr)
  1529. {
  1530. ulRef = pIStorageFirst->Release();
  1531. DH_ASSERT(0 == ulRef);
  1532. pIStorageFirst = NULL;
  1533. }
  1534. for ( count=0; count<15; count++)
  1535. {
  1536. DH_ASSERT(count < 15);
  1537. // For the first open:
  1538. // if instantiation mode contains STGM_TRANSACTED, then always
  1539. // instantiated; otherwise, if read-only(STGM_READ) is specified in the
  1540. // mode, then only instantiated when STGM_SHARE_DENY_WRITE or
  1541. // STGM_SHARE_EXCLUSIVE is also set; if STGM_WRITE is specified in the
  1542. // mode, then only instantiated when STGM_SHARE_EXCLUSIVE is also set.
  1543. if ((dwRootMode & STGM_TRANSACTED) ||
  1544. ((aPerms[count].usMode & STGM_RW) == STGM_READ &&
  1545. ((aPerms[count].usMode & STGM_SHARE) == STGM_SHARE_DENY_WRITE ||
  1546. (aPerms[count].usMode & STGM_SHARE) == STGM_SHARE_EXCLUSIVE)) ||
  1547. (((aPerms[count].usMode & STGM_RW) == STGM_WRITE ||
  1548. (aPerms[count].usMode & STGM_RW) == STGM_READWRITE) &&
  1549. (aPerms[count].usMode & STGM_SHARE) == STGM_SHARE_EXCLUSIVE))
  1550. {
  1551. pIStorageFirst = NULL;
  1552. if (dwRootMode & STGM_TRANSACTED)
  1553. {
  1554. dwOpenMode = STGM_TRANSACTED | aPerms[count].usMode;
  1555. }
  1556. else
  1557. {
  1558. dwOpenMode = STGM_DIRECT | aPerms[count].usMode;
  1559. }
  1560. hr = StgOpenStorage(
  1561. pOleStrTemp,
  1562. NULL,
  1563. dwOpenMode,
  1564. NULL,
  1565. 0,
  1566. &pIStorageFirst);
  1567. if (S_OK != hr)
  1568. {
  1569. DH_TRACE((DH_LVL_ERROR,
  1570. TEXT("Error in first open (mode=%#lx), hr=0x%lx"),
  1571. dwOpenMode,
  1572. hr));
  1573. break;
  1574. }
  1575. else
  1576. {
  1577. for (subcount=0;subcount<15;subcount++)
  1578. {
  1579. DH_ASSERT(subcount < 15);
  1580. // For the second open:
  1581. // if the mode contains STGM_TRANSACTED then always
  1582. // instantiates; otherwise only instantiates if
  1583. // STGM_SHARE_DENY_WRITE or STGM_SHARE_EXCLUSIVE
  1584. // is set for this index in the table because for
  1585. // direct mode, both STGM_READ or STGM_WRITE need
  1586. // combined with at least STGM_SHARE_DENY_WRITE.
  1587. if ((dwRootMode & STGM_TRANSACTED) ||
  1588. ((aPerms[subcount].usMode & STGM_SHARE) ==
  1589. STGM_SHARE_DENY_WRITE ||
  1590. (aPerms[subcount].usMode & STGM_SHARE) ==
  1591. STGM_SHARE_EXCLUSIVE))
  1592. {
  1593. pIStorageSecond = NULL;
  1594. if (dwRootMode & STGM_TRANSACTED)
  1595. {
  1596. dwOpenMode = STGM_TRANSACTED
  1597. | aPerms[subcount].usMode;
  1598. fShouldWork = aPerms[count].afPermsOk[subcount+15];
  1599. }
  1600. else
  1601. {
  1602. dwOpenMode = STGM_DIRECT
  1603. | aPerms[subcount].usMode;
  1604. fShouldWork = aPerms[count].afPermsOk[subcount];
  1605. }
  1606. hr = StgOpenStorage(
  1607. pOleStrTemp,
  1608. NULL,
  1609. dwOpenMode,
  1610. NULL,
  1611. 0,
  1612. &pIStorageSecond);
  1613. // determine whether or not the instantiation should
  1614. // have worked. For every 'F' in the access modes
  1615. // table, the 2nd instantion should fail.
  1616. if (((fShouldWork == FALSE) && (S_OK == hr)) ||
  1617. ((fShouldWork == TRUE) && (S_OK != hr)))
  1618. {
  1619. DH_TRACE((
  1620. DH_LVL_TRACE1,
  1621. TEXT("\t i=%u, j=%u "),
  1622. count,
  1623. subcount));
  1624. DWORD dwMode = dwRootMode & STGM_TRANSACTED ?
  1625. STGM_TRANSACTED : STGM_DIRECT;
  1626. DH_TRACE((DH_LVL_ERROR,
  1627. TEXT("Mode1:%#lx, Mode2:%lx; hr=%#lx; Expect %s"),
  1628. aPerms[count].usMode | dwMode,
  1629. aPerms[subcount].usMode | dwMode,
  1630. hr,
  1631. (LPTSTR)(FALSE == fShouldWork ?
  1632. TEXT("failure"):TEXT("success"))));
  1633. fPass = FALSE;
  1634. hr = S_OK;
  1635. }
  1636. else
  1637. {
  1638. // Success or fail as expected
  1639. hr = S_OK;
  1640. }
  1641. if(NULL != pIStorageSecond)
  1642. {
  1643. ulRef = pIStorageSecond->Release();
  1644. DH_ASSERT(0 == ulRef);
  1645. pIStorageSecond = NULL;
  1646. }
  1647. }
  1648. }
  1649. if(NULL != pIStorageFirst)
  1650. {
  1651. ulRef = pIStorageFirst->Release();
  1652. DH_ASSERT(0 == ulRef);
  1653. pIStorageFirst = NULL;
  1654. }
  1655. }
  1656. }
  1657. if (S_OK != hr)
  1658. {
  1659. break;
  1660. }
  1661. }
  1662. // if everything goes well, log test as passed else failed.
  1663. if (fPass && (S_OK == hr))
  1664. {
  1665. DH_LOG((LOG_PASS, TEXT("Test variation ROOTTEST_103 passed.")) );
  1666. }
  1667. else
  1668. {
  1669. DH_LOG((LOG_FAIL,
  1670. TEXT("Test variation ROOTTEST_103 failed, hr=0x%lx."),
  1671. hr) );
  1672. // test failed. make it look like it failed.
  1673. hr = FirstError (hr, E_FAIL);
  1674. }
  1675. // Cleanup
  1676. // Delete Chance docfile tree
  1677. if(NULL != pTestChanceDF)
  1678. {
  1679. hr2 = pTestChanceDF->DeleteChanceDocFileTree(
  1680. pTestChanceDF->GetChanceDFRoot());
  1681. DH_HRCHECK(hr2, TEXT("pTestChanceDF->DeleteChanceFileDocTree")) ;
  1682. delete pTestChanceDF;
  1683. pTestChanceDF = NULL;
  1684. }
  1685. // Delete temp string
  1686. if(NULL != pOleStrTemp)
  1687. {
  1688. delete pOleStrTemp;
  1689. pOleStrTemp = NULL;
  1690. }
  1691. // Delete DataGen object
  1692. if(NULL != pdgu)
  1693. {
  1694. delete pdgu;
  1695. pdgu = NULL;
  1696. }
  1697. // Delete the docfile on disk
  1698. if((S_OK == hr) && (NULL != pRootDocFileName))
  1699. {
  1700. if(FALSE == DeleteFile(pRootDocFileName))
  1701. {
  1702. hr2 = HRESULT_FROM_WIN32(GetLastError()) ;
  1703. DH_HRCHECK(hr2, TEXT("DeleteFile")) ;
  1704. }
  1705. }
  1706. // Delete temp string
  1707. if ((S_OK == hr) && fPass && (NULL != pRootDocFileName))
  1708. {
  1709. delete pRootDocFileName;
  1710. pRootDocFileName = NULL;
  1711. }
  1712. // Stop logging the test
  1713. DH_TRACE((DH_LVL_TRACE1, TEXT("Test variation ROOTTEST_103 finished")) );
  1714. DH_TRACE((DH_LVL_TRACE1, TEXT("--------------------------------------------")) );
  1715. return hr;
  1716. }
  1717. //----------------------------------------------------------------------------
  1718. //
  1719. // Test: ROOTTEST_104
  1720. //
  1721. // Synopsis: This test first creates a root docfile. An IStream is created
  1722. // inside the root docfile and a random number of bytes are
  1723. // written to it. The stream is released, the root docfile is
  1724. // committed, and the root docfile is released.
  1725. // The root docfile is instantiated in STGM_TRANSACTED mode and
  1726. // and then released. A count of the files in the current directory
  1727. // is then made and saved. The root docfile is then instantiated
  1728. // in STGM_TRANSACTED | STGM_SHARE_DENY_WRITE mode and another count is
  1729. // made. We then verify that only 1 scratch file was created,
  1730. // indicating that for STGM_DENY_WRITE mode, no copy is made of
  1731. // the instantiated IStorage. (Note: if in the second time, the
  1732. // file is opened in STGM_TRANSACTED mode only, not STGM_SHARE_DENY_
  1733. // WRITE mode, two scratch files would be made.)
  1734. //
  1735. // Arguments:[argc]
  1736. // [argv]
  1737. //
  1738. // Returns: HRESULT
  1739. //
  1740. // History: 26-June-1996 NarindK Created.
  1741. //
  1742. // Notes: This test runs in transacted modes
  1743. //
  1744. // New Test Notes:
  1745. // 1. Old File: LRTWWDW.CXX
  1746. // 2. Old name of test : LegitRootTwwDenyWrite test
  1747. // New Name of test : ROOTTEST_104
  1748. // 3. To run the test, do the following at command prompt.
  1749. // stgbase /t:ROOTTEST-104
  1750. //
  1751. // BUGNOTE: Conversion: ROOTTEST-104 NO
  1752. //
  1753. //-----------------------------------------------------------------------------
  1754. HRESULT ROOTTEST_104(int /* UNREF argc */, char * * /* UNREF argv*/)
  1755. {
  1756. #ifdef _MAC
  1757. DH_TRACE((DH_LVL_TRACE1, TEXT("!!!!!!!!!!!!!!ROOTTEST_104 crashes")) );
  1758. DH_TRACE((DH_LVL_TRACE1, TEXT("!!!!!!!!!!!!!!To be investigated")) );
  1759. return E_NOTIMPL;
  1760. #else
  1761. HRESULT hr = S_OK;
  1762. HRESULT hr2 = S_OK;
  1763. DWORD dwRootMode = STGM_READWRITE | STGM_TRANSACTED;
  1764. LPSTORAGE pIStorage = NULL;
  1765. ULONG ulRef = 0;
  1766. DG_STRING *pdgu = NULL;
  1767. DG_INTEGER *pdgi = NULL;
  1768. LPTSTR ptszRootName = NULL;
  1769. LPOLESTR poszRootName = NULL;
  1770. LPTSTR ptszStreamName = NULL;
  1771. LPOLESTR poszStreamName = NULL;
  1772. BOOL fPass = TRUE;
  1773. ULONG culSeed = 0;
  1774. ULONG cRandom = 0;
  1775. LPSTREAM pIStream = NULL;
  1776. LPTSTR ptcsBuffer = NULL;
  1777. LPOLESTR pocsBuffer = NULL;
  1778. ULONG culWritten = 0;
  1779. ULONG culFilesInDirectory = 0;
  1780. BOOL fNssfile = FALSE;
  1781. // Not for 2phase. Bail.
  1782. if (DoingDistrib ())
  1783. {
  1784. return S_OK;
  1785. }
  1786. DH_FUNCENTRY(NULL, DH_LVL_DFLIB, _TEXT("ROOTTEST_104"));
  1787. DH_TRACE((DH_LVL_TRACE1, TEXT("--------------------------------------------")) );
  1788. DH_TRACE((DH_LVL_TRACE1, TEXT("Test variation ROOTTEST_104 started.")) );
  1789. DH_TRACE((DH_LVL_TRACE1,
  1790. TEXT("Attempt operations on root docfile in transacted mode.")));
  1791. if(S_OK == hr)
  1792. {
  1793. // Create a new DataGen object to create random UNICODE strings.
  1794. pdgu = new(NullOnFail) DG_STRING(culSeed);
  1795. if (NULL == pdgu)
  1796. {
  1797. hr = E_OUTOFMEMORY;
  1798. }
  1799. }
  1800. // Generate RootDocFile name
  1801. if(S_OK == hr)
  1802. {
  1803. hr=GenerateRandomName(pdgu, MINLENGTH,MAXLENGTH, &ptszRootName);
  1804. DH_HRCHECK(hr, TEXT("GenerateRandomName")) ;
  1805. }
  1806. if(S_OK == hr)
  1807. {
  1808. // Convert TCHAR name to OLECHAR
  1809. hr = TStringToOleString(ptszRootName, &poszRootName);
  1810. DH_HRCHECK(hr, TEXT("TStringToOleString")) ;
  1811. }
  1812. // Try calling StgCreateDocFile with above random name
  1813. if (S_OK == hr)
  1814. {
  1815. pIStorage = NULL;
  1816. hr = StgCreateDocfile(
  1817. poszRootName,
  1818. dwRootMode | STGM_FAILIFTHERE,
  1819. 0,
  1820. &pIStorage);
  1821. DH_HRCHECK(hr, TEXT(" StgCreateDocfile")) ;
  1822. }
  1823. if(S_OK == hr)
  1824. {
  1825. DH_TRACE((DH_LVL_TRACE1, TEXT("StgCreateDocFile passed as expected.")));
  1826. }
  1827. else
  1828. {
  1829. DH_TRACE((
  1830. DH_LVL_TRACE1,
  1831. TEXT("StgCreateDocFile failed unexpectedly, hr=0x%lx"), hr));
  1832. }
  1833. // Generate random name for stream
  1834. if(S_OK == hr)
  1835. {
  1836. hr=GenerateRandomName(pdgu, MINLENGTH,MAXLENGTH, &ptszStreamName);
  1837. DH_HRCHECK(hr, TEXT("GenerateRandomName")) ;
  1838. }
  1839. if(S_OK == hr)
  1840. {
  1841. // Convert TCHAR name to OLECHAR
  1842. hr = TStringToOleString(ptszStreamName, &poszStreamName);
  1843. DH_HRCHECK(hr, TEXT("TStringToOleString")) ;
  1844. }
  1845. // Create a stream
  1846. if (S_OK == hr)
  1847. {
  1848. hr = pIStorage->CreateStream(
  1849. poszStreamName,
  1850. STGM_READWRITE | STGM_SHARE_EXCLUSIVE | STGM_FAILIFTHERE,
  1851. 0,
  1852. 0,
  1853. &pIStream);
  1854. }
  1855. if(S_OK == hr)
  1856. {
  1857. DH_TRACE((DH_LVL_TRACE1, TEXT("IStorage::CreateStream passed as expected")));
  1858. }
  1859. else
  1860. {
  1861. DH_TRACE((
  1862. DH_LVL_TRACE1,
  1863. TEXT("IStorage::CreateStream failed unexpectedly,hr=0x%lx"), hr));
  1864. }
  1865. if(S_OK == hr)
  1866. {
  1867. // Create a new DataGen object to create random integers.
  1868. pdgi = new(NullOnFail) DG_INTEGER(culSeed);
  1869. if (NULL == pdgi)
  1870. {
  1871. hr = E_OUTOFMEMORY;
  1872. }
  1873. }
  1874. // Write random size data into stream
  1875. if(S_OK == hr)
  1876. {
  1877. hr = pdgi->Generate(&cRandom, 1, USHRT_MAX);
  1878. DH_HRCHECK(hr, TEXT("Generate")) ;
  1879. }
  1880. if(S_OK == hr)
  1881. {
  1882. hr = GenerateRandomString(pdgu, cRandom, cRandom, &ptcsBuffer);
  1883. DH_HRCHECK(hr, TEXT("GenerateRandomString")) ;
  1884. }
  1885. if(S_OK == hr)
  1886. {
  1887. // Convert data to OLECHAR
  1888. hr = TStringToOleString(ptcsBuffer, &pocsBuffer);
  1889. DH_HRCHECK(hr, TEXT("TStringToOleString")) ;
  1890. }
  1891. if(S_OK == hr)
  1892. {
  1893. hr = pIStream->Write(pocsBuffer, cRandom, &culWritten);
  1894. DH_HRCHECK(hr, TEXT("IStream::Write")) ;
  1895. }
  1896. // Release stream
  1897. if (NULL != pIStream)
  1898. {
  1899. ulRef = pIStream->Release();
  1900. DH_ASSERT(0 == ulRef);
  1901. pIStream = NULL;
  1902. }
  1903. // Now do a valid commit. BUGBUG: Use random modes..
  1904. if (S_OK == hr)
  1905. {
  1906. hr = pIStorage->Commit(STGC_DEFAULT);
  1907. DH_HRCHECK(hr, TEXT("IStorage::Commit")) ;
  1908. }
  1909. if (S_OK == hr)
  1910. {
  1911. DH_TRACE((
  1912. DH_LVL_TRACE1,
  1913. TEXT("IStorage::Commit completed successfully.")));
  1914. }
  1915. else
  1916. {
  1917. DH_TRACE((
  1918. DH_LVL_TRACE1,
  1919. TEXT("IStorage::Commit unsuccessful, hr=0x%lx."),
  1920. hr));
  1921. }
  1922. // Close the root docfile
  1923. if (NULL != pIStorage)
  1924. {
  1925. ulRef = pIStorage->Release();
  1926. DH_ASSERT(0 == ulRef);
  1927. pIStorage = NULL;
  1928. }
  1929. // Open the root docfile
  1930. if (S_OK == hr)
  1931. {
  1932. pIStorage = NULL;
  1933. hr = StgOpenStorage(
  1934. poszRootName,
  1935. NULL,
  1936. dwRootMode,
  1937. NULL,
  1938. 0,
  1939. &pIStorage);
  1940. DH_HRCHECK(hr, TEXT("StgOpenStorage")) ;
  1941. }
  1942. if (S_OK == hr)
  1943. {
  1944. DH_TRACE((
  1945. DH_LVL_TRACE1,
  1946. TEXT("StgOpenStorage completed successfully.")));
  1947. }
  1948. else
  1949. {
  1950. DH_TRACE((
  1951. DH_LVL_TRACE1,
  1952. TEXT("StgOpenStorage couldn't complete successfully, hr = 0x%lx"),
  1953. hr));
  1954. }
  1955. // Close the root docfile
  1956. if (NULL != pIStorage)
  1957. {
  1958. ulRef = pIStorage->Release();
  1959. DH_ASSERT(0 == ulRef);
  1960. pIStorage = NULL;
  1961. }
  1962. // count number of files in directory
  1963. if (S_OK == hr)
  1964. {
  1965. culFilesInDirectory = CountFilesInDirectory(WILD_MASK);
  1966. //If opening as nssfile, decrement # tempfiles, (uses a stream)
  1967. if (DoingOpenNssfile ())
  1968. {
  1969. //VerifyNssfile returns S_OK if valid nssfile - else an error
  1970. if (S_OK == VerifyNssfile (ptszRootName))
  1971. {
  1972. //nssfiles use a stream in file, not temp file.
  1973. culFilesInDirectory--;
  1974. }
  1975. }
  1976. }
  1977. // Open the root docfile
  1978. if (S_OK == hr)
  1979. {
  1980. pIStorage = NULL;
  1981. hr = StgOpenStorage(
  1982. poszRootName,
  1983. NULL,
  1984. dwRootMode | STGM_SHARE_DENY_WRITE,
  1985. NULL,
  1986. 0,
  1987. &pIStorage);
  1988. DH_HRCHECK(hr, TEXT("StgOpenStorage")) ;
  1989. }
  1990. if (S_OK == hr)
  1991. {
  1992. DH_TRACE((
  1993. DH_LVL_TRACE1,
  1994. TEXT("StgOpenStorage with STGM_SHARE_DENY_WRITE passed.")));
  1995. }
  1996. else
  1997. {
  1998. DH_TRACE((
  1999. DH_LVL_TRACE1,
  2000. TEXT("StgOpenStorage with STGM_SHARE_DENY_WRITE fail, hr=0x%lx"),
  2001. hr));
  2002. }
  2003. // Check number of files
  2004. if((culFilesInDirectory + 1) != CountFilesInDirectory(WILD_MASK))
  2005. {
  2006. DH_TRACE((
  2007. DH_LVL_TRACE1,
  2008. TEXT(">1 scratch file unexp STGM_SHARE_DENY_WRITE instantiation.")));
  2009. hr = S_FALSE;
  2010. }
  2011. else
  2012. {
  2013. DH_TRACE((
  2014. DH_LVL_TRACE1,
  2015. TEXT("1 scratchfile exp with STGM_SHARE_DENY_WRITE instantiation")));
  2016. }
  2017. // Close the root docfile
  2018. if (NULL != pIStorage)
  2019. {
  2020. ulRef = pIStorage->Release();
  2021. DH_ASSERT(0 == ulRef);
  2022. pIStorage = NULL;
  2023. }
  2024. // if everything goes well, log test as passed else failed.
  2025. if (S_OK == hr)
  2026. {
  2027. DH_LOG((LOG_PASS, TEXT("Test variation ROOTTEST_104 passed.")) );
  2028. }
  2029. else
  2030. {
  2031. DH_LOG((LOG_FAIL,
  2032. TEXT("Test variation ROOTTEST_104 failed, hr=0x%lx."),
  2033. hr) );
  2034. }
  2035. // Cleanup
  2036. // Delete DataGen object
  2037. if(NULL != pdgu)
  2038. {
  2039. delete pdgu;
  2040. pdgu = NULL;
  2041. }
  2042. if(NULL != pdgi)
  2043. {
  2044. delete pdgi;
  2045. pdgi = NULL;
  2046. }
  2047. // Delete temp string
  2048. if(NULL != ptszStreamName)
  2049. {
  2050. delete ptszStreamName;
  2051. ptszStreamName = NULL;
  2052. }
  2053. if(NULL != poszStreamName)
  2054. {
  2055. delete poszStreamName;
  2056. poszStreamName = NULL;
  2057. }
  2058. if(NULL != poszRootName)
  2059. {
  2060. delete poszRootName;
  2061. poszRootName = NULL;
  2062. }
  2063. if(NULL != ptcsBuffer)
  2064. {
  2065. delete ptcsBuffer;
  2066. ptcsBuffer = NULL;
  2067. }
  2068. if(NULL != pocsBuffer)
  2069. {
  2070. delete pocsBuffer;
  2071. pocsBuffer = NULL;
  2072. }
  2073. // Delete the docfile on disk
  2074. if((S_OK == hr) && (NULL != ptszRootName))
  2075. {
  2076. if(FALSE == DeleteFile(ptszRootName))
  2077. {
  2078. hr2 = HRESULT_FROM_WIN32(GetLastError()) ;
  2079. DH_HRCHECK(hr2, TEXT("DeleteFile")) ;
  2080. }
  2081. }
  2082. // Delete temp string
  2083. if(NULL != ptszRootName)
  2084. {
  2085. delete ptszRootName;
  2086. ptszRootName = NULL;
  2087. }
  2088. // Stop logging the test
  2089. DH_TRACE((DH_LVL_TRACE1, TEXT("Test variation ROOTTEST_104 finished")) );
  2090. DH_TRACE((DH_LVL_TRACE1, TEXT("--------------------------------------------")) );
  2091. return hr;
  2092. #endif //_MAC
  2093. }