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.

535 lines
17 KiB

  1. //-------------------------------------------------------------------------
  2. //
  3. // Microsoft OLE
  4. // Copyright (C) Microsoft Corporation, 1994 - 1995.
  5. //
  6. // File: init.cxx
  7. //
  8. // Contents: OLE storage base tests
  9. //
  10. // Functions: main
  11. //
  12. // History: 26-Feb-1997 SCousens Created.
  13. //
  14. //--------------------------------------------------------------------------
  15. #include <dfheader.hxx>
  16. #pragma hdrstop
  17. #include <init.hxx>
  18. HRESULT MakeVirtualDF (UINT uOp, ChanceDF *pTestChanceDF, VirtualCtrNode **ppvcnRoot, VirtualDF **ppTestVirtualDF);
  19. // Global:
  20. // For stress debug purposes. So we can get to the seed from
  21. // within the debugger
  22. // do not use gulSeed for anything else!
  23. ULONG gulSeed=0;
  24. //----------------------------------------------------------------------------
  25. // FUNCTION: CreateTestDocfile [multiple]
  26. //
  27. // PARAMS: argc - # params on commandline
  28. // argv - array of pointers to commandline
  29. // ppVirtualDFRoot - bucket for RootCtrNode
  30. // ppTestVirtualDF - bucket for pVirtualDF
  31. // ppTestChanceDF - bucket for pChanceDF
  32. //
  33. // SYNOPSIS: Determine whether we are creating a DF, or
  34. // opening and existing one.
  35. // Creating
  36. // Create a ChanceDF of random nodes,
  37. // Create the VirtualDF from this (GenerateVirtualDF)
  38. // Commit and close the resulting docfile
  39. // Opening
  40. // Figure the name, open it (GenerateVirtualDFFromDiskDF)
  41. // Also need to set _pgdu, _pgdi
  42. //
  43. // RETURN: hr. S_OK or whatever failure was encountered.
  44. //
  45. // NOTES: All stms and Stgs of the docfile will have been commited
  46. // and and saved, but all will be open before returning
  47. // The name of the file will be the first string generated
  48. // after creating the DataGen for strings. Its the way it
  49. // works, we can use this knowledge to our advantage (until
  50. // someone changes that behaviour and breaks us)
  51. //
  52. // HISTORY: 28-Feb-1997 SCousens Created.
  53. //
  54. //----------------------------------------------------------------------------
  55. HRESULT CreateTestDocfile (
  56. int argc,
  57. char **argv,
  58. VirtualCtrNode **ppvcnRoot,
  59. VirtualDF **ppTestVirtualDF,
  60. ChanceDF **ppTestChanceDF)
  61. {
  62. HRESULT hr = S_OK;
  63. ChanceDF *pChanceDF = NULL;
  64. DH_FUNCENTRY(&hr, DH_LVL_DFLIB, _TEXT("CreateTestDocfile"));
  65. DH_VDATEPTROUT (ppvcnRoot, VirtualCtrNode *);
  66. DH_VDATEPTROUT (ppTestVirtualDF, VirtualDF *);
  67. *ppvcnRoot = NULL;
  68. *ppTestVirtualDF = NULL;
  69. if (NULL != ppTestChanceDF)
  70. {
  71. DH_VDATEPTROUT (ppTestChanceDF, ChanceDF *);
  72. *ppTestChanceDF = NULL;
  73. }
  74. // Always create this. There is lots of juicy info that
  75. // we will need either way.
  76. // Create the new ChanceDocFile tree that would consist of chance nodes.
  77. if (S_OK == hr)
  78. {
  79. pChanceDF = new ChanceDF();
  80. if(NULL == pChanceDF)
  81. {
  82. hr = E_OUTOFMEMORY;
  83. }
  84. }
  85. if (S_OK == hr)
  86. {
  87. hr = pChanceDF->CreateFromParams (argc, argv);
  88. DH_HRCHECK(hr, TEXT("pChanceDF->CreateFromParams")) ;
  89. }
  90. // Make VirtualDF
  91. if (S_OK == hr)
  92. {
  93. hr = MakeVirtualDF (pChanceDF->GetOpenCreateDF (),
  94. pChanceDF,
  95. ppvcnRoot,
  96. ppTestVirtualDF);
  97. }
  98. // If the caller wants ChanceDF give it to them, otherwise delete it
  99. if (NULL == ppTestChanceDF)
  100. {
  101. delete pChanceDF;
  102. }
  103. else
  104. {
  105. *ppTestChanceDF = pChanceDF;
  106. }
  107. return hr;
  108. }
  109. //----------------------------------------------------------------------------
  110. //
  111. // FUNCTION: CreateTestDocfile [multiple]
  112. //
  113. // PARAMS: pcdfd - CDFD for chancedf
  114. // pFileName - name of docfile
  115. // ulSeed - seed (to get name)
  116. // ppVirtualDFRoot - bucket for RootCtrNode
  117. // ppTestVirtualDF - bucket for pVirtualDF
  118. // ppTestChanceDF - bucket for pChanceDF
  119. //
  120. // SYNOPSIS: See above
  121. //
  122. // RETURN: hr. S_OK or whatever failure was encountered.
  123. //
  124. // NOTES: See description above
  125. //
  126. // HISTORY: 19-Mar-1997 SCousens Created.
  127. //
  128. //----------------------------------------------------------------------------
  129. HRESULT CreateTestDocfile (
  130. CDFD *pcdfd,
  131. LPTSTR pFileName,
  132. VirtualCtrNode **ppvcnRoot,
  133. VirtualDF **ppTestVirtualDF,
  134. ChanceDF **ppTestChanceDF)
  135. {
  136. HRESULT hr = S_OK;
  137. ChanceDF *pChanceDF = NULL;
  138. LPTSTR pDocFileName = NULL;
  139. DH_FUNCENTRY(&hr, DH_LVL_DFLIB, _TEXT("CreateTestDocfile"));
  140. DH_VDATEPTRIN (pcdfd, CDFD);
  141. DH_VDATEPTROUT (ppvcnRoot, VirtualCtrNode *);
  142. DH_VDATEPTROUT (ppTestVirtualDF, VirtualDF *);
  143. *ppvcnRoot = NULL;
  144. *ppTestVirtualDF = NULL;
  145. if (NULL != ppTestChanceDF)
  146. {
  147. DH_VDATEPTROUT (ppTestChanceDF, ChanceDF *);
  148. *ppTestChanceDF = NULL;
  149. }
  150. // If dont have a filename, make one
  151. if (NULL == pFileName)
  152. {
  153. DG_STRING *pdgu = NULL;
  154. if (S_OK == hr)
  155. {
  156. // Create a new DataGen object to create random UNICODE strings.
  157. pdgu = new DG_STRING (pcdfd->ulSeed);
  158. if (NULL == pdgu)
  159. {
  160. hr = E_OUTOFMEMORY;
  161. }
  162. }
  163. if (S_OK == hr)
  164. {
  165. // Generate random name for root
  166. hr = GenerateRandomName(
  167. pdgu,
  168. MINLENGTH,
  169. MAXLENGTH,
  170. &pDocFileName);
  171. DH_HRCHECK(hr, TEXT("GenerateRandomName")) ;
  172. }
  173. delete pdgu;
  174. }
  175. // Always create this.
  176. // Create the new ChanceDocFile tree that would consist of chance nodes.
  177. if (S_OK == hr)
  178. {
  179. pChanceDF = new ChanceDF();
  180. if(NULL == pChanceDF)
  181. {
  182. hr = E_OUTOFMEMORY;
  183. }
  184. }
  185. if (S_OK == hr)
  186. {
  187. hr = pChanceDF->Create (
  188. pcdfd,
  189. NULL == pFileName ? pFileName : pDocFileName);
  190. DH_HRCHECK(hr, TEXT("pChanceDF->Create")) ;
  191. }
  192. // Make VirtualDF
  193. if (S_OK == hr)
  194. {
  195. hr = MakeVirtualDF (g_uOpenCreateDF, //Global set in ProcessCmdLine()
  196. pChanceDF,
  197. ppvcnRoot,
  198. ppTestVirtualDF);
  199. }
  200. // cleanup
  201. if (NULL != pDocFileName)
  202. {
  203. delete []pDocFileName;
  204. }
  205. // If the caller wants ChanceDF give it to them, otherwise delete it
  206. if (NULL == ppTestChanceDF)
  207. {
  208. delete pChanceDF;
  209. }
  210. else
  211. {
  212. *ppTestChanceDF = pChanceDF;
  213. }
  214. return hr;
  215. }
  216. //----------------------------------------------------------------------------
  217. // FUNCTION: MakeVirtualDF
  218. //
  219. // PARAMS: uOp - Create|Open flag (FL_DISTRIB_xxx)
  220. // pTestChanceDF - ptr to pChanceDF
  221. // ppVirtualDFRoot - bucket for RootCtrNode
  222. // ppTestVirtualDF - bucket for pVirtualDF
  223. //
  224. // SYNOPSIS: This function should be called by
  225. // CreateTestDocfile
  226. // If FL_OPEN is set, and cannot open docfile,
  227. // return ERROR.
  228. // Otherwise we need to create a storage file
  229. // as we are either as 1st phase, or single phase.
  230. //
  231. // RETURN: hr. S_OK or whatever failure was encountered.
  232. //
  233. // HISTORY: 28-Feb-1997 SCousens Created.
  234. // BUGBUG: GenerateVirtualDFFromDiskDF does not fill in the CRC for vsn.
  235. // we need to write a rtn that will do this for enumtest 100.
  236. //----------------------------------------------------------------------------
  237. HRESULT MakeVirtualDF (
  238. UINT uOp,
  239. ChanceDF *pTestChanceDF,
  240. VirtualCtrNode **ppvcnRoot,
  241. VirtualDF **ppTestVirtualDF)
  242. {
  243. HRESULT hr = S_OK;
  244. // This is internal func. Shouldnt have to do this.
  245. DH_ASSERT (NULL != pTestChanceDF);
  246. DH_ASSERT (NULL != ppvcnRoot);
  247. DH_ASSERT (NULL != ppTestVirtualDF);
  248. DH_FUNCENTRY(&hr, DH_LVL_DFLIB, _TEXT("MakeVirtualDF"));
  249. //for stress debug purposes. So we can get to the seed.
  250. //do not use gulSeed for anything!
  251. gulSeed = pTestChanceDF->GetSeed ();
  252. // If its OPEN, try to open existing.
  253. if (FL_DISTRIB_OPEN == uOp)
  254. {
  255. ULONG ulSeed = 0;
  256. DG_STRING *pgdu = NULL;
  257. LPTSTR tszDocfileName = NULL;
  258. // Get the seed. We need this to generate the filename.
  259. ulSeed = pTestChanceDF->GetSeed();
  260. // Create a new DataGen object to create the filename
  261. pgdu = new DG_STRING(ulSeed);
  262. if (NULL == pgdu)
  263. {
  264. hr = E_OUTOFMEMORY;
  265. }
  266. // if filename was specified, get it from chanceDF
  267. if (hr == S_OK)
  268. {
  269. tszDocfileName = pTestChanceDF->GetDocFileName();
  270. if (NULL != tszDocfileName)
  271. {
  272. LPTSTR pTmpName = tszDocfileName;
  273. tszDocfileName = new TCHAR[_tcslen (pTmpName)+1];
  274. if (NULL == tszDocfileName)
  275. {
  276. hr = E_OUTOFMEMORY;
  277. }
  278. else
  279. {
  280. _tcscpy(tszDocfileName, pTmpName);
  281. }
  282. }
  283. else
  284. {
  285. // this will generate the name of the file that was created
  286. hr = GenerateRandomName (pgdu, MINLENGTH, MAXLENGTH, &tszDocfileName);
  287. DH_HRCHECK (hr, TEXT("GenerateRandomName"));
  288. }
  289. }
  290. if (S_OK == hr)
  291. {
  292. *ppTestVirtualDF = new VirtualDF();
  293. if (NULL == *ppTestVirtualDF)
  294. {
  295. hr = E_OUTOFMEMORY;
  296. }
  297. }
  298. if (hr == S_OK)
  299. {
  300. // Remove create bit - open does not like it.
  301. hr = GenerateVirtualDFFromDiskDF(
  302. *ppTestVirtualDF,
  303. tszDocfileName,
  304. pTestChanceDF->GetRootMode() & ~STGM_CREATE,
  305. ppvcnRoot,
  306. ulSeed);
  307. DH_HRCHECK (hr, TEXT("GenerateVirtualDFFromDiskDF"));
  308. }
  309. // make sure ALL sub stg/stm are open and have valid _pstg
  310. if (hr == S_OK)
  311. {
  312. hr = ParseVirtualDFAndOpenAllSubStgsStms (
  313. *ppvcnRoot,
  314. pTestChanceDF->GetStgMode (),
  315. pTestChanceDF->GetStmMode ());
  316. DH_HRCHECK (hr, TEXT("ParseVirtualDFAndOpenAllSubStgsStms"));
  317. }
  318. if (NULL != tszDocfileName)
  319. {
  320. delete tszDocfileName;
  321. }
  322. if (NULL != pgdu)
  323. {
  324. delete pgdu;
  325. }
  326. }
  327. // else !open, so create one.
  328. else
  329. {
  330. // Create the VirtualDocFile tree from the ChanceDocFile tree created in
  331. // the previous step. The VirtualDocFile tree consists of VirtualCtrNodes
  332. // and VirtualStmNodes.
  333. if (S_OK == hr)
  334. {
  335. *ppTestVirtualDF = new VirtualDF();
  336. if (NULL == *ppTestVirtualDF)
  337. {
  338. hr = E_OUTOFMEMORY;
  339. }
  340. }
  341. if (S_OK == hr)
  342. {
  343. hr = (*ppTestVirtualDF)->GenerateVirtualDF (pTestChanceDF, ppvcnRoot);
  344. DH_HRCHECK(hr, TEXT("pTestVirtualDF->GenerateVirtualDF")) ;
  345. }
  346. // Commit all stms and stgs in newly created storage file
  347. if (S_OK == hr)
  348. {
  349. hr = ParseVirtualDFAndCommitAllOpenStgs (*ppvcnRoot, STGC_DEFAULT, NODE_INC_TOPSTG);
  350. DH_HRCHECK(hr, TEXT("ParseVirtualDFAndCommitAllOpenStgs")) ;
  351. }
  352. // If we are testing conversion, close and reopen the file.
  353. // BUGBUG: If we are distributed and happen to have the
  354. // cmdline for conversion, you will do an extra open and
  355. // close. DIF files may get an unacceptible time penalty.
  356. if (DoingConversion())
  357. {
  358. // close the file
  359. if (S_OK == hr)
  360. {
  361. hr = ParseVirtualDFAndCloseOpenStgsStms (*ppvcnRoot, NODE_INC_TOPSTG);
  362. DH_HRCHECK(hr, TEXT("ParseVirtualDFAndCloseOpenStgsStms")) ;
  363. }
  364. // just for fun, verify that we indeed have an nssfile
  365. // Assume there is a file name, else we failed somewhere
  366. // creating it and hr should != S_OK
  367. if (S_OK == hr)
  368. {
  369. hr = VerifyNssfile ((*ppTestVirtualDF)->GetDocFileName());
  370. DH_HRCHECK(hr, TEXT("VerifyNssfile")) ;
  371. }
  372. // open root storage
  373. if (S_OK == hr)
  374. {
  375. // if in SIMPLEMODE, get rid of Create.
  376. ULONG mode = pTestChanceDF->GetRootMode ();
  377. if (mode & STGM_SIMPLE)
  378. {
  379. mode &= ~STGM_CREATE;
  380. }
  381. hr = (*ppvcnRoot)->OpenRoot (NULL,
  382. mode,
  383. NULL,
  384. 0);
  385. DH_HRCHECK(hr, TEXT("StgOpenStorage"));
  386. }
  387. // open up the rest of storages and streams
  388. if (S_OK == hr)
  389. {
  390. hr = ParseVirtualDFAndOpenAllSubStgsStms (*ppvcnRoot,
  391. pTestChanceDF->GetStgMode (),
  392. pTestChanceDF->GetStmMode ());
  393. DH_HRCHECK(hr, TEXT("ParseVirtualDFAndOpenAllSubStgsStms")) ;
  394. }
  395. DH_TRACE ((DH_LVL_TRACE2, TEXT("Docfile closed and reopened for conversion")));
  396. }
  397. }
  398. return hr;
  399. }
  400. //----------------------------------------------------------------------------
  401. //
  402. // FUNCTION: CleanupTestDocfile
  403. //
  404. // PARAMS: ppVirtualDFRoot - bucket for RootCtrNode
  405. // ppTestVirtualDF - bucket for pVirtualDF
  406. // ppTestChanceDF - bucket for pChanceDF
  407. // fDeleteFile - If was an error, dont delete docfile
  408. //
  409. // SYNOPSIS: Cleanup all items that were setup in CreateTestDocfile
  410. // - chancedf
  411. // - virtualdf
  412. // - delete docfile on disk (if there were no errors)
  413. //
  414. // RETURN: hr. S_OK or whatever failure was encountered.
  415. //
  416. // HISTORY: 28-Feb-1997 SCousens Created.
  417. //
  418. //----------------------------------------------------------------------------
  419. HRESULT CleanupTestDocfile (
  420. VirtualCtrNode **ppVirtualDFRoot,
  421. VirtualDF **ppTestVirtualDF,
  422. ChanceDF **ppTestChanceDF,
  423. BOOL fDeleteFile)
  424. {
  425. LPTSTR pFileName = NULL;
  426. HRESULT hr = S_OK;
  427. VirtualCtrNode *pvcnRootNode= *ppVirtualDFRoot;
  428. VirtualDF *pVirtualDF = *ppTestVirtualDF;
  429. ChanceDF *pChanceDF = *ppTestChanceDF;
  430. DH_FUNCENTRY (NULL, DH_LVL_DFLIB, TEXT("CleanupTestDocfile"));
  431. // Make sure everything in the docfile is closed
  432. if (NULL != pvcnRootNode)
  433. {
  434. hr = ParseVirtualDFAndCloseOpenStgsStms (pvcnRootNode, NODE_INC_TOPSTG);
  435. DH_HRCHECK (hr, TEXT("ParseVirtualDFAndCloseOpenStgsStms")) ;
  436. }
  437. // Get file name
  438. if (TRUE == fDeleteFile && DeleteTestDF ())
  439. {
  440. if (NULL != pvcnRootNode)
  441. {
  442. pFileName= new TCHAR[_tcslen (pVirtualDF->GetDocFileName ())+1];
  443. if (pFileName != NULL)
  444. {
  445. _tcscpy (pFileName, pVirtualDF->GetDocFileName ());
  446. }
  447. }
  448. }
  449. // Delete Virtual docfile tree
  450. if (NULL != pVirtualDF)
  451. {
  452. hr = pVirtualDF->DeleteVirtualDocFileTree (pvcnRootNode);
  453. DH_HRCHECK (hr, TEXT("pVirtualDF->DeleteVirtualFileDocTree")) ;
  454. delete pVirtualDF;
  455. pVirtualDF = NULL;
  456. }
  457. // Delete Chance docfile tree
  458. if (NULL != pChanceDF)
  459. {
  460. hr = pChanceDF->DeleteChanceDocFileTree (pChanceDF->GetChanceDFRoot());
  461. DH_HRCHECK (hr, TEXT("pChanceDF->DeleteChanceFileDocTree")) ;
  462. delete pChanceDF;
  463. pChanceDF = NULL;
  464. }
  465. // Delete the docfile on disk
  466. if ((S_OK == hr) && (NULL != pFileName))
  467. {
  468. if (FALSE == DeleteFile(pFileName))
  469. {
  470. hr = HRESULT_FROM_WIN32 (GetLastError()) ;
  471. DH_HRCHECK (hr, TEXT("DeleteFile")) ;
  472. }
  473. }
  474. // Delete the docfile name
  475. if (NULL != pFileName)
  476. {
  477. delete pFileName;
  478. pFileName = NULL;
  479. }
  480. return hr;
  481. }