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.

1018 lines
31 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1996.
  5. //
  6. // File: reftest.cxx
  7. //
  8. // Contents: Reference tests
  9. //
  10. // Classes:
  11. //
  12. // Functions:
  13. //
  14. //----------------------------------------------------------------------------
  15. #ifdef _MSC_VER
  16. #define INITGUID
  17. #endif
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include "../../h/storage.h"
  22. // enable debugging features of memory allocation
  23. #include "../../h/dbg.hxx"
  24. #ifdef _MSC_VER
  25. #undef INITGUID
  26. #endif
  27. #include "refilb.hxx"
  28. #ifndef _WIN32
  29. #include <unistd.h>
  30. #else
  31. #include <io.h>
  32. #endif
  33. #define STGP(x) STGM_SHARE_EXCLUSIVE | x
  34. #define STMP(x) STGM_SHARE_EXCLUSIVE | x
  35. #define ROOTP(x) STGP(x)
  36. #define EXIT_BADSC 1
  37. int g_fTestInterop = 0;
  38. #define olHChk(e) \
  39. if FAILED(sc = e) \
  40. goto EH_Err
  41. #define olChk(e) olHChk(e)
  42. #include <assert.h>
  43. #define olAssert assert
  44. #ifdef NDEBUG
  45. #define verify(exp) 1
  46. #else
  47. #define verify(exp) assert(exp)
  48. #endif
  49. #define ULIGetLow(ui) (ui.LowPart)
  50. //
  51. // some global variables used by all the tests
  52. //
  53. OLECHAR ocsDRT [ sizeof("drt.dfl")+1 ];
  54. OLECHAR ocsChild [ sizeof("Child") +1 ];
  55. OLECHAR ocsChild1[ sizeof("Child1") +1 ];
  56. OLECHAR ocsChild2[ sizeof("Child2") +1 ];
  57. OLECHAR ocsStream[ sizeof("Stream") +1 ];
  58. OLECHAR ocsRenamedStream[ sizeof("RenamedStream") +1 ];
  59. OLECHAR ocsRenamedChild[ sizeof("RenamedChild") +1 ];
  60. void error(int code, char *fmt, ...)
  61. {
  62. va_list args;
  63. args = va_start(args, fmt);
  64. printf("** Fatal error **: ");
  65. vprintf(fmt, args);
  66. va_end(args);
  67. exit(code);
  68. }
  69. BOOL IsEqualTime(FILETIME ttTime, FILETIME ttCheck)
  70. {
  71. return ttTime.dwLowDateTime == ttCheck.dwLowDateTime &&
  72. ttTime.dwHighDateTime == ttCheck.dwHighDateTime;
  73. }
  74. SCODE t_create(BOOL fTestStorage, BOOL fLarge)
  75. {
  76. IStorage *pstgRoot, *pstgChild, *pstgChild2;
  77. IStream *pstm;
  78. SCODE sc;
  79. ILockBytes *pilb=NULL;
  80. if (!fTestStorage)
  81. {
  82. printf("Testing Create ILB\n");
  83. pilb = new CFileILB(ocsDRT, (DWORD)NULL);
  84. if (pilb == NULL)
  85. error(EXIT_BADSC, "Unable to allocate an ILockBytes\n");
  86. // create a storage on the ILockBytes
  87. olHChk( StgCreateDocfileOnILockBytes( pilb,
  88. STGM_READWRITE |
  89. STGM_CREATE | STGM_SHARE_EXCLUSIVE,
  90. 0, &pstgRoot));
  91. }
  92. else if (fLarge)
  93. {
  94. STGOPTIONS stgoptions;
  95. stgoptions.usVersion = STGOPTIONS_VERSION;
  96. stgoptions.reserved = 0;
  97. stgoptions.ulSectorSize = 4096;
  98. stgoptions.pwcsTemplateFile = NULL;
  99. printf("Testing Create large storage\n");
  100. olHChk(StgCreateStorageEx ( ocsDRT,
  101. STGM_READWRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE,
  102. STGFMT_DOCFILE,
  103. 0, &stgoptions,
  104. 0, IID_IStorage, (void **) &pstgRoot));
  105. }
  106. else
  107. {
  108. printf("Testing Create storage\n");
  109. olHChk(StgCreateDocfile(ocsDRT,
  110. STGM_READWRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE,
  111. 0, &pstgRoot));
  112. }
  113. olHChk(pstgRoot->CreateStorage(ocsChild, STGP(STGM_READWRITE), 0, 0,
  114. &pstgChild));
  115. olHChk(pstgChild->CreateStorage(ocsChild2, STGP(STGM_READWRITE), 0, 0,
  116. &pstgChild2));
  117. olHChk(pstgChild2->CreateStream(ocsStream, STMP(STGM_READWRITE), 0, 0,
  118. &pstm));
  119. pstm->Release();
  120. olHChk(pstgChild2->Commit(0));
  121. pstgChild2->Release();
  122. olHChk(pstgChild->Commit(0));
  123. pstgChild->Release();
  124. pstgRoot->Release();
  125. if (pilb) pilb->Release();
  126. EH_Err:
  127. return sc;
  128. }
  129. SCODE t_open(BOOL fTestStorage, BOOL fLarge)
  130. {
  131. SCODE sc;
  132. IStorage *pstgRoot, *pstgChild, *pstgChild2;
  133. IStream *pstm;
  134. ILockBytes *pilb=NULL;
  135. if (!fTestStorage)
  136. {
  137. printf("Testing Open ILB\n");
  138. pilb = new CFileILB(ocsDRT, (DWORD)NULL);
  139. if (pilb == NULL)
  140. error(EXIT_BADSC, "Unable to allocate an ILockBytes\n");
  141. // create a storage on the ILockBytes
  142. olHChk(StgCreateDocfileOnILockBytes(pilb,
  143. STGM_READWRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE,
  144. 0, &pstgRoot));
  145. }
  146. else if (fLarge)
  147. {
  148. STGOPTIONS stgoptions;
  149. stgoptions.usVersion = STGOPTIONS_VERSION;
  150. stgoptions.reserved = 0;
  151. stgoptions.ulSectorSize = 4096;
  152. stgoptions.pwcsTemplateFile = NULL;
  153. printf("Testing Open large storage\n");
  154. olHChk(StgCreateStorageEx ( ocsDRT,
  155. STGM_READWRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE,
  156. STGFMT_DOCFILE,
  157. 0, &stgoptions,
  158. 0, IID_IStorage, (void **) &pstgRoot));
  159. }
  160. else
  161. {
  162. // create a storage
  163. printf("Testing Open storage\n");
  164. olHChk(StgCreateDocfile(ocsDRT,
  165. STGM_READWRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE,
  166. 0, &pstgRoot));
  167. }
  168. olHChk(pstgRoot->CreateStorage(ocsChild, STGP(STGM_READWRITE), 0, 0,
  169. &pstgChild));
  170. olHChk(pstgChild->CreateStorage(ocsChild2, STGP(STGM_READWRITE), 0, 0,
  171. &pstgChild2));
  172. olHChk(pstgChild2->CreateStream(ocsStream, STMP(STGM_READWRITE), 0, 0,
  173. &pstm));
  174. pstm->Release();
  175. pstgChild2->Release();
  176. pstgChild->Release();
  177. olHChk(pstgRoot->Commit(0));
  178. pstgRoot->Release();
  179. if (!fTestStorage)
  180. {
  181. olHChk(StgOpenStorageOnILockBytes(pilb, NULL,
  182. ROOTP(STGM_READWRITE), NULL, 0, &pstgRoot));
  183. }
  184. else
  185. {
  186. olHChk(StgOpenStorage(ocsDRT,
  187. NULL, ROOTP(STGM_READWRITE), NULL, 0,
  188. &pstgRoot));
  189. }
  190. olHChk(pstgRoot->OpenStorage(
  191. ocsChild,
  192. NULL,
  193. STGP(STGM_READWRITE),
  194. NULL,
  195. 0,
  196. &pstgChild));
  197. olHChk(pstgChild->OpenStorage(
  198. ocsChild2,
  199. NULL,
  200. STGP(STGM_READWRITE),
  201. NULL,
  202. 0,
  203. &pstgChild2));
  204. olHChk(pstgChild2->OpenStream(
  205. ocsStream,
  206. NULL,
  207. STMP(STGM_READWRITE),
  208. 0,
  209. &pstm));
  210. pstm->Release();
  211. pstgChild2->Release();
  212. pstgChild->Release();
  213. pstgRoot->Release();
  214. if (pilb) pilb->Release();
  215. EH_Err:
  216. return sc;
  217. } // t_open
  218. SCODE t_addref(BOOL fTestStorage, BOOL fLarge)
  219. {
  220. SCODE sc;
  221. IStorage *pstg;
  222. IStream *pstm;
  223. ULONG ul;
  224. ILockBytes *pilb=NULL;
  225. if (!fTestStorage)
  226. {
  227. printf("Testing Addref ILB\n");
  228. pilb = new CFileILB(ocsDRT, (DWORD)NULL);
  229. if (pilb == NULL)
  230. error(EXIT_BADSC, "Unable to allocate an ILockBytes\n");
  231. // create a storage on the ILockBytes
  232. olHChk(StgCreateDocfileOnILockBytes(pilb,
  233. STGM_READWRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE,
  234. 0, &pstg));
  235. }
  236. else if (fLarge)
  237. {
  238. STGOPTIONS stgoptions;
  239. stgoptions.usVersion = STGOPTIONS_VERSION;
  240. stgoptions.reserved = 0;
  241. stgoptions.ulSectorSize = 4096;
  242. stgoptions.pwcsTemplateFile = NULL;
  243. printf("Testing AddRef large storage\n");
  244. olHChk(StgCreateStorageEx ( ocsDRT,
  245. STGM_READWRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE,
  246. STGFMT_DOCFILE,
  247. 0, &stgoptions,
  248. 0, IID_IStorage, (void **) &pstg));
  249. }
  250. else
  251. {
  252. printf("Testing Addref Storage\n");
  253. olHChk(StgCreateDocfile(ocsDRT,
  254. STGM_READWRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE,
  255. 0, &pstg));
  256. }
  257. olHChk(pstg->CreateStream( ocsStream, STMP(STGM_READWRITE),
  258. 0, 0, &pstm));
  259. if ((ul = pstm->AddRef()) != 2)
  260. error(EXIT_BADSC, "Wrong reference count - %lu\n", ul);
  261. if ((ul = pstm->Release()) != 1)
  262. error(EXIT_BADSC, "Wrong reference count - %lu\n", ul);
  263. pstm->Release();
  264. if ((ul = pstg->AddRef()) != 2)
  265. error(EXIT_BADSC, "Wrong reference count - %lu\n", ul);
  266. if ((ul = pstg->Release()) != 1)
  267. error(EXIT_BADSC, "Wrong reference count - %lu\n", ul);
  268. pstg->Release();
  269. if (pilb) pilb->Release();
  270. EH_Err:
  271. return sc;
  272. } // t_addref
  273. SCODE t_dmodify(BOOL fTestStorage, BOOL fLarge)
  274. {
  275. SCODE sc;
  276. IStorage *pstgRoot, *pstgChild, *pstgChild2;
  277. IStream *pstm;
  278. ILockBytes *pilb=NULL;
  279. DECLARE_OLESTR(ocs88, "88");
  280. DECLARE_OLESTR(ocs84, "84");
  281. DECLARE_OLESTR(ocs92, "92");
  282. DECLARE_OLESTR(ocs64, "64");
  283. DECLARE_OLESTR(ocs32, "32");
  284. DECLARE_OLESTR(ocs96, "96");
  285. DECLARE_OLESTR(ocs80, "80");
  286. if (!fTestStorage)
  287. {
  288. printf("Testing Modify ILB\n");
  289. pilb = new CFileILB(ocsDRT, (DWORD)NULL);
  290. if (pilb == NULL)
  291. error(EXIT_BADSC, "Unable to allocate an ILockBytes\n");
  292. // create a storage on the ILockBytes
  293. olHChk(StgCreateDocfileOnILockBytes(pilb,
  294. STGM_READWRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE,
  295. 0, &pstgRoot));
  296. }
  297. else if (fLarge)
  298. {
  299. STGOPTIONS stgoptions;
  300. stgoptions.usVersion = STGOPTIONS_VERSION;
  301. stgoptions.reserved = 0;
  302. stgoptions.ulSectorSize = 4096;
  303. stgoptions.pwcsTemplateFile = NULL;
  304. printf("Testing Modify large storage\n");
  305. olHChk(StgCreateStorageEx ( ocsDRT,
  306. STGM_READWRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE,
  307. STGFMT_DOCFILE,
  308. 0, &stgoptions,
  309. 0, IID_IStorage, (void **) &pstgRoot));
  310. }
  311. else {
  312. printf("Testing Modify Storage\n");
  313. olHChk(StgCreateDocfile(ocsDRT,
  314. STGM_READWRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE,
  315. 0, &pstgRoot));
  316. }
  317. olHChk(pstgRoot->CreateStorage(ocsChild, STGP(STGM_READWRITE), 0,
  318. 0, &pstgChild));
  319. olHChk(pstgChild->CreateStorage(ocsChild2, STGP(STGM_READWRITE), 0,
  320. 0, &pstgChild2));
  321. olHChk(pstgChild2->CreateStream(
  322. ocsStream, STMP(STGM_READWRITE), 0, 0, &pstm));
  323. pstm->Release();
  324. // Test renaming a closed stream
  325. olHChk(pstgChild2->RenameElement(ocsStream, ocsRenamedStream));
  326. // Test destroying a stream
  327. olHChk(pstgChild2->DestroyElement(ocsRenamedStream));
  328. // Test renaming an open stream
  329. olHChk(pstgChild2->CreateStream(
  330. ocsStream,
  331. STMP(STGM_READWRITE),
  332. 0,
  333. 0,
  334. &pstm));
  335. olHChk(pstgChild2->RenameElement(ocsStream, ocsRenamedStream));
  336. olHChk(pstgChild2->DestroyElement(ocsRenamedStream));
  337. pstm->Release();
  338. pstgChild2->Release();
  339. // Test renaming a storage
  340. olHChk(pstgChild->RenameElement(ocsChild2, ocsRenamedChild));
  341. olHChk(pstgChild->CreateStream(
  342. ocsStream,
  343. STMP(STGM_READWRITE),
  344. 0,
  345. 0,
  346. &pstm));
  347. pstm->Release();
  348. olHChk(pstgChild->DestroyElement(ocsStream));
  349. // Test SetElementTimes
  350. FILETIME tm;
  351. STATSTG stat;
  352. tm.dwLowDateTime = 0x12345678;
  353. tm.dwHighDateTime = 0x9abcdef0;
  354. // Set when element not open
  355. olHChk(pstgChild->SetElementTimes(ocsRenamedChild, &tm, NULL, NULL));
  356. olHChk(pstgChild->SetElementTimes(ocsRenamedChild, NULL, &tm, NULL));
  357. olHChk(pstgChild->SetElementTimes(ocsRenamedChild, NULL, NULL, &tm));
  358. olHChk(pstgChild->OpenStorage(
  359. ocsRenamedChild,
  360. NULL,
  361. STMP(STGM_READWRITE),
  362. NULL,
  363. 0,
  364. &pstgChild2));
  365. olHChk(pstgChild2->Stat(&stat, STATFLAG_NONAME));
  366. if (!IsEqualTime(stat.ctime, tm) ||
  367. !IsEqualTime(stat.mtime, tm))
  368. error(EXIT_BADSC, "Times don't match those set by SetElementTimes\n");
  369. // Test SetClass and SetStateBits
  370. olHChk(pstgChild2->SetClass(IID_IStorage));
  371. olHChk(pstgChild2->SetStateBits(0xff00ff00, 0xffffffff));
  372. olHChk(pstgChild2->SetStateBits(0x00880088, 0xeeeeeeee));
  373. olHChk(pstgChild2->Stat(&stat, STATFLAG_NONAME));
  374. if (!IsEqualCLSID(stat.clsid, IID_IStorage))
  375. error(EXIT_BADSC, "Class ID set improperly\n");
  376. if (stat.grfStateBits != 0x11881188)
  377. error(EXIT_BADSC, "State bits set improperly: has %lX vs. %lX\n",
  378. stat.grfStateBits, 0x11881188);
  379. pstgChild2->Release();
  380. pstgChild->Release();
  381. olHChk(pstgRoot->Revert());
  382. olHChk(pstgRoot->Commit(0));
  383. olHChk(pstgRoot->DestroyElement(ocsChild));
  384. olHChk(pstgRoot->CreateStream(
  385. ocsStream,
  386. STMP(STGM_READWRITE),
  387. 0,
  388. 0,
  389. &pstm));
  390. ULARGE_INTEGER ulSize;
  391. ULISet32(ulSize, 65536);
  392. olHChk(pstm->SetSize(ulSize));
  393. pstm->Release();
  394. olHChk(pstgRoot->DestroyElement(ocsStream));
  395. olHChk(pstgRoot->CreateStream(
  396. ocsStream,
  397. STMP(STGM_READWRITE),
  398. 0,
  399. 0,
  400. &pstm));
  401. olHChk(pstm->SetSize(ulSize));
  402. pstm->Release();
  403. pstgRoot->Release();
  404. if (pilb) pilb->Release();
  405. if (!fTestStorage)
  406. {
  407. pilb = new CFileILB((TCHAR*)NULL, (DWORD)NULL);
  408. if (pilb == NULL)
  409. error(EXIT_BADSC, "Unable to allocate an ILockBytes\n");
  410. // create a storage on the ILockBytes
  411. olHChk(StgCreateDocfileOnILockBytes(pilb,
  412. STGM_READWRITE |
  413. STGM_CREATE |
  414. STGM_SHARE_EXCLUSIVE,
  415. 0, &pstgRoot));
  416. }
  417. else
  418. {
  419. olHChk(StgCreateDocfile(ocsDRT,
  420. STGM_READWRITE |
  421. STGM_CREATE |
  422. STGM_SHARE_EXCLUSIVE,
  423. 0, &pstgRoot));
  424. }
  425. // removal cases
  426. // 1) no right child
  427. olHChk(pstgRoot->CreateStorage(ocs64, STGP(STGM_READWRITE), 0, 0,
  428. &pstgChild));
  429. pstgChild->Release();
  430. olHChk(pstgRoot->CreateStorage(ocs32, STGP(STGM_READWRITE), 0, 0,
  431. &pstgChild));
  432. pstgChild->Release();
  433. olHChk(pstgRoot->DestroyElement(ocs64));
  434. // 2) right child has no left child
  435. olHChk(pstgRoot->CreateStorage(ocs64, STGP(STGM_READWRITE), 0, 0,
  436. &pstgChild));
  437. pstgChild->Release();
  438. olHChk(pstgRoot->DestroyElement(ocs32));
  439. // 3) right child has left child
  440. olHChk(pstgRoot->CreateStorage(ocs96, STGP(STGM_READWRITE), 0, 0,
  441. &pstgChild));
  442. pstgChild->Release();
  443. olHChk(pstgRoot->CreateStorage(ocs80, STGP(STGM_READWRITE), 0, 0,
  444. &pstgChild));
  445. pstgChild->Release();
  446. olHChk(pstgRoot->DestroyElement(ocs64));
  447. // 4) right child's left child has children
  448. olHChk(pstgRoot->CreateStorage(ocs88, STGP(STGM_READWRITE), 0, 0,
  449. &pstgChild));
  450. pstgChild->Release();
  451. olHChk(pstgRoot->CreateStorage(ocs84, STGP(STGM_READWRITE), 0, 0,
  452. &pstgChild));
  453. pstgChild->Release();
  454. olHChk(pstgRoot->CreateStorage(ocs92, STGP(STGM_READWRITE), 0, 0,
  455. &pstgChild));
  456. pstgChild->Release();
  457. olHChk(pstgRoot->DestroyElement(ocs80));
  458. pstgRoot->Release();
  459. if (pilb) pilb->Release();
  460. EH_Err:
  461. return sc;
  462. }
  463. SCODE t_stat(BOOL fTestStorage, BOOL fLarge)
  464. {
  465. SCODE sc;
  466. IStorage *pstgRoot, *pstgChild;
  467. IStream *pstm;
  468. STATSTG stat;
  469. ILockBytes *pilb=NULL;
  470. if (!fTestStorage)
  471. {
  472. printf("Testing Stat ILB\n");
  473. pilb = new CFileILB(ocsDRT, (DWORD)NULL);
  474. if (pilb == NULL)
  475. error(EXIT_BADSC, "Unable to allocate an ILockBytes\n");
  476. // create a storage on the ILockBytes
  477. olHChk(StgCreateDocfileOnILockBytes(pilb,
  478. STGM_READWRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE,
  479. 0, &pstgRoot));
  480. }
  481. else if (fLarge)
  482. {
  483. STGOPTIONS stgoptions;
  484. stgoptions.usVersion = STGOPTIONS_VERSION;
  485. stgoptions.reserved = 0;
  486. stgoptions.ulSectorSize = 4096;
  487. stgoptions.pwcsTemplateFile = NULL;
  488. printf("Testing Stat large storage\n");
  489. olHChk(StgCreateStorageEx ( ocsDRT,
  490. STGM_READWRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE,
  491. STGFMT_DOCFILE,
  492. 0, &stgoptions,
  493. 0, IID_IStorage, (void **) &pstgRoot));
  494. }
  495. else
  496. {
  497. printf("Testing Stat Storage\n");
  498. // create a storage
  499. olHChk(StgCreateDocfile(ocsDRT,
  500. STGM_READWRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE,
  501. 0, &pstgRoot));
  502. }
  503. olHChk(pstgRoot->CreateStorage(ocsChild, STGP(STGM_READWRITE), 0, 0,
  504. &pstgChild));
  505. olHChk(pstgChild->CreateStream(
  506. ocsStream,
  507. STMP(STGM_READWRITE),
  508. 0,
  509. 0,
  510. &pstm));
  511. olHChk(pstm->Stat(&stat, 0));
  512. delete [] stat.pwcsName;
  513. olHChk(pstm->Stat(&stat, STATFLAG_NONAME));
  514. pstm->Release();
  515. olHChk(pstgChild->Stat(&stat, 0));
  516. delete [] stat.pwcsName;
  517. olHChk(pstgChild->Stat(&stat, STATFLAG_NONAME));
  518. pstgChild->Release();
  519. olHChk(pstgRoot->Stat(&stat, 0));
  520. delete[] stat.pwcsName;
  521. olHChk(pstgRoot->Stat(&stat, STATFLAG_NONAME));
  522. pstgRoot->Release();
  523. if (pilb) pilb->Release();
  524. EH_Err:
  525. return sc;
  526. }
  527. static char NUMBERS[] = "12345678901234567890123456789012345678901234567890";
  528. SCODE t_stream(BOOL fTestStorage, BOOL fCreate, BOOL fLarge)
  529. {
  530. SCODE sc;
  531. IStorage *pstg=NULL, *pstg1=NULL, *pstg2=NULL, *pstg3=NULL;
  532. IStream *pstm=NULL, *pstmC=NULL, *pstm1=NULL, *pstm2=NULL;
  533. char buf[sizeof(NUMBERS)*2];
  534. ULONG cb;
  535. ULARGE_INTEGER ulPos, ulSize;
  536. LARGE_INTEGER lPos;
  537. ILockBytes *pilb=NULL;
  538. int i=0;
  539. DECLARE_OLESTR(ocsStorage1, "Storage1");
  540. DECLARE_OLESTR(ocsStorage1Stream1, "Storage1Stream1");
  541. DECLARE_OLESTR(ocsStorage2, "Storage2");
  542. DECLARE_OLESTR(ocsStorage2Storage1, "Storage2Storage1");
  543. DECLARE_OLESTR(ocsStorage3Stream1, "Storage3Stream1");
  544. if (fCreate)
  545. {
  546. if (!fTestStorage)
  547. {
  548. printf("Testing streams for ILB\n");
  549. pilb = new CFileILB(ocsDRT, (DWORD)NULL);
  550. if (pilb == NULL)
  551. error(EXIT_BADSC, "Unable to allocate an ILockBytes\n");
  552. // create a storage on the ILockBytes
  553. olHChk(StgCreateDocfileOnILockBytes(pilb,
  554. STGM_READWRITE |
  555. STGM_CREATE |
  556. STGM_SHARE_EXCLUSIVE,
  557. 0, &pstg));
  558. }
  559. else if (fLarge)
  560. {
  561. STGOPTIONS stgoptions;
  562. stgoptions.usVersion = STGOPTIONS_VERSION;
  563. stgoptions.reserved = 0;
  564. stgoptions.ulSectorSize = 4096;
  565. stgoptions.pwcsTemplateFile = NULL;
  566. printf("Testing streams for large storage\n");
  567. olHChk(StgCreateStorageEx ( ocsDRT,
  568. STGM_READWRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE,
  569. STGFMT_DOCFILE,
  570. 0, &stgoptions,
  571. 0, IID_IStorage, (void **) &pstg));
  572. }
  573. else
  574. {
  575. printf("Testing streams for Storage\n");
  576. // create a storage on the ILockBytes
  577. olHChk(StgCreateDocfile(ocsDRT,
  578. STGM_READWRITE | STGM_CREATE
  579. | STGM_SHARE_EXCLUSIVE,
  580. 0, &pstg));
  581. }
  582. olHChk(pstg->CreateStream( ocsStream,
  583. STMP(STGM_READWRITE), 0, 0, &pstm));
  584. olHChk(pstg->CreateStorage( ocsStorage1,
  585. STMP(STGM_READWRITE), 0, 0, &pstg1));
  586. olHChk(pstg1->CreateStream( ocsStorage1Stream1,
  587. STMP(STGM_READWRITE), 0, 0, &pstm1));
  588. olHChk(pstg->CreateStorage( ocsStorage2,
  589. STMP(STGM_READWRITE), 0, 0, &pstg2));
  590. olHChk(pstg2->CreateStorage( ocsStorage2Storage1,
  591. STMP(STGM_READWRITE), 0, 0, &pstg3));
  592. olHChk(pstg3->CreateStream( ocsStorage3Stream1,
  593. STMP(STGM_READWRITE), 0, 0, &pstm2));
  594. for (i=0; i<20; i++)
  595. olHChk(pstm->Write(NUMBERS, sizeof(NUMBERS), &cb));
  596. for (i=0; i<20; i++)
  597. olHChk(pstm2->Write(NUMBERS, sizeof(NUMBERS), &cb));
  598. olHChk(pstm->Commit(0));
  599. unsigned long ul;
  600. if ((ul = pstm->Release())!=0)
  601. error(EXIT_BADSC, "Wrong reference count - %lu\n", ul);
  602. if ((ul = pstm1->Release())!=0)
  603. error(EXIT_BADSC, "Wrong reference count - %lu\n", ul);
  604. if ((ul = pstm2->Release())!=0)
  605. error(EXIT_BADSC, "Wrong reference count - %lu\n", ul);
  606. if ((ul = pstg->Release())!=0)
  607. error(EXIT_BADSC, "Wrong reference count - %lu\n", ul);
  608. if ((ul = pstg1->Release())!=0)
  609. error(EXIT_BADSC, "Wrong reference count - %lu\n", ul);
  610. if ((ul = pstg2->Release())!=0)
  611. error(EXIT_BADSC, "Wrong reference count - %lu\n", ul);
  612. if ((ul = pstg3->Release())!=0)
  613. error(EXIT_BADSC, "Wrong reference count - %lu\n", ul);
  614. if (pilb)
  615. verify (0 == pilb->Release());
  616. return sc;
  617. }
  618. if (!fTestStorage)
  619. {
  620. pilb = new CFileILB(ocsDRT, (DWORD)NULL);
  621. if (pilb == NULL)
  622. error(EXIT_BADSC, "Unable to allocate an ILockBytes\n");
  623. // create a storage on the ILockBytes
  624. olHChk(StgOpenStorageOnILockBytes(pilb, NULL,
  625. ROOTP(STGM_READWRITE), NULL, 0,
  626. &pstg));
  627. }
  628. else
  629. {
  630. olHChk(StgOpenStorage(ocsDRT,
  631. NULL, ROOTP(STGM_READWRITE), NULL, 0,
  632. &pstg));
  633. }
  634. olHChk(pstg->OpenStream( ocsStream, NULL,
  635. STMP(STGM_READWRITE), 0, &pstm));
  636. olHChk(pstg->OpenStorage( ocsStorage1, NULL,
  637. STMP(STGM_READWRITE), 0, 0, &pstg1));
  638. olHChk(pstg1->OpenStream( ocsStorage1Stream1, NULL,
  639. STMP(STGM_READWRITE), 0, &pstm1));
  640. olHChk(pstg->OpenStorage( ocsStorage2, NULL,
  641. STMP(STGM_READWRITE), 0, 0, &pstg2));
  642. olHChk(pstg2->OpenStorage( ocsStorage2Storage1, 0,
  643. STMP(STGM_READWRITE), 0, 0, &pstg3));
  644. olHChk(pstg3->OpenStream( ocsStorage3Stream1, 0,
  645. STMP(STGM_READWRITE), 0, &pstm2));
  646. lPos.QuadPart = 0;
  647. olHChk(pstm->Seek(lPos, STREAM_SEEK_SET, &ulPos));
  648. if (ulPos.QuadPart != 0)
  649. error(EXIT_BADSC, "Incorrect seek, ptr is %lu\n", ulPos.QuadPart);
  650. for (i=0; i<20; i++)
  651. {
  652. olHChk(pstm->Read(buf, sizeof(NUMBERS), &cb));
  653. if (strcmp(buf, NUMBERS))
  654. error(EXIT_BADSC, "Incorrect stream contents\n");
  655. }
  656. ulPos.QuadPart = 0;
  657. olHChk(pstm2->Seek(lPos, STREAM_SEEK_SET, &ulPos));
  658. if (ulPos.QuadPart != 0)
  659. error(EXIT_BADSC, "Incorrect seek, ptr is %lu\n", ulPos.QuadPart);
  660. for (i=0; i<20; i++)
  661. {
  662. olHChk(pstm2->Read(buf, sizeof(NUMBERS), &cb));
  663. if (strcmp(buf, NUMBERS))
  664. error(EXIT_BADSC, "Incorrect stream contents\n");
  665. }
  666. if (!g_fTestInterop)
  667. { // some tests that changes the contents
  668. ULISet32(ulSize, sizeof(NUMBERS)/2);
  669. olHChk(pstm->SetSize(ulSize));
  670. olHChk(pstm->Seek(lPos, STREAM_SEEK_SET, NULL));
  671. olHChk(pstm->Read(buf, sizeof(NUMBERS), &cb));
  672. if (cb != sizeof(NUMBERS)/2)
  673. error(EXIT_BADSC, "SetSize failed to size stream properly\n");
  674. if (memcmp(buf, NUMBERS, sizeof(NUMBERS)/2))
  675. error(EXIT_BADSC, "SetSize corrupted contents\n");
  676. olHChk(pstm->Clone(&pstmC));
  677. olHChk(pstm->Seek(lPos, STREAM_SEEK_SET, NULL));
  678. olHChk(pstm->CopyTo(pstmC, ulSize, NULL, NULL));
  679. olHChk(pstm->Seek(lPos, STREAM_SEEK_SET, NULL));
  680. ULISet32(ulSize, sizeof(NUMBERS)&~1);
  681. olHChk(pstm->CopyTo(pstmC, ulSize, NULL, NULL));
  682. olHChk(pstm->Seek(lPos, STREAM_SEEK_SET, NULL));
  683. olHChk(pstm->Read(buf, (sizeof(NUMBERS)&~1)*2, &cb));
  684. if (memcmp(buf, NUMBERS, sizeof(NUMBERS)/2) ||
  685. memcmp(buf+sizeof(NUMBERS)/2, NUMBERS, sizeof(NUMBERS)/2) ||
  686. memcmp(buf+(sizeof(NUMBERS)&~1), NUMBERS, sizeof(NUMBERS)/2) ||
  687. memcmp(buf+3*(sizeof(NUMBERS)/2), NUMBERS, sizeof(NUMBERS)/2))
  688. error(EXIT_BADSC, "Stream contents incorrect\n");
  689. verify( 0 == pstmC->Release());
  690. }
  691. EH_Err:
  692. if (pstm) verify( 0 == pstm->Release() );
  693. if (pstm1) verify( 0 == pstm1->Release() );
  694. if (pstm2) verify( 0 == pstm2->Release() );
  695. if (pstg) verify( 0 == pstg->Release() );
  696. if (pstg1) verify( 0 == pstg1->Release() );
  697. if (pstg2) verify( 0 == pstg2->Release() );
  698. if (pstg3) verify( 0 == pstg3->Release() );
  699. if (pilb) verify( 0 == pilb->Release() );
  700. return sc;
  701. }
  702. SCODE t_stgmisc(BOOL fLarge)
  703. {
  704. SCODE sc;
  705. IStorage *pstg;
  706. FILE *f;
  707. _unlink("drt.dfl");
  708. // create zero byte file
  709. f= fopen("drt.dfl", "w+b");
  710. fclose(f);
  711. olAssert(StgIsStorageFile(ocsDRT)==S_FALSE);
  712. _unlink("drt.dfl");
  713. if (fLarge)
  714. {
  715. STGOPTIONS stgoptions;
  716. stgoptions.usVersion = STGOPTIONS_VERSION;
  717. stgoptions.reserved = 0;
  718. stgoptions.ulSectorSize = 4096;
  719. stgoptions.pwcsTemplateFile = NULL;
  720. olHChk(StgCreateStorageEx ( ocsDRT,
  721. STGM_READWRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE,
  722. STGFMT_DOCFILE,
  723. 0, &stgoptions,
  724. 0, IID_IStorage, (void **) &pstg));
  725. }
  726. else
  727. olHChk(StgCreateDocfile(ocsDRT,
  728. STGM_READWRITE|STGM_CREATE|STGM_SHARE_EXCLUSIVE,
  729. 0,
  730. &pstg));
  731. olAssert(StgIsStorageFile(ocsDRT)==S_OK);
  732. pstg->Release();
  733. EH_Err:
  734. return sc;
  735. }
  736. SCODE t_large ()
  737. {
  738. IStorage *pstg = NULL;
  739. IStream *pstm = NULL;
  740. SCODE sc = S_OK;
  741. STGOPTIONS stgoptions;
  742. stgoptions.usVersion = STGOPTIONS_VERSION;
  743. stgoptions.reserved = 0;
  744. stgoptions.ulSectorSize = 4096;
  745. stgoptions.pwcsTemplateFile = NULL;
  746. olChk(StgCreateStorageEx ( _T("large.dfl"),
  747. STGM_READWRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE,
  748. STGFMT_DOCFILE,
  749. 0, &stgoptions,
  750. 0, IID_IStorage, (void **) &pstg));
  751. olChk(pstg->CreateStream( _T("large"),
  752. STGM_READWRITE|STGM_SHARE_EXCLUSIVE, 0, 0, &pstm));
  753. ULARGE_INTEGER ulSize;
  754. ulSize.QuadPart = MAX_ULONG + (ULONGLONG) sizeof(NUMBERS);
  755. olChk(pstm->SetSize(ulSize));
  756. for (ULONG i=0; i < MAX_ULONG/sizeof(NUMBERS) + 1; i++)
  757. {
  758. ULONG cb;
  759. olChk(pstm->Write(NUMBERS, sizeof(NUMBERS), &cb));
  760. }
  761. olChk(pstg->Commit(STGC_DEFAULT));
  762. EH_Err:
  763. if (pstm != NULL)
  764. pstm->Release();
  765. if (pstg != NULL)
  766. pstg->Release();
  767. return sc;
  768. }
  769. void terminate(void)
  770. {
  771. printf("Terminate() called!\n");
  772. exit(-1);
  773. }
  774. int main(int argc, char** argv)
  775. {
  776. SCODE sc;
  777. int fCreate=0;
  778. // change the following line to whatever number to detect mem leaks
  779. //_CrtSetBreakAlloc(146);
  780. // initialize the strings
  781. INIT_OLESTR(ocsDRT, "drt.dfl");
  782. INIT_OLESTR(ocsChild, "Child");
  783. INIT_OLESTR(ocsChild1, "Child1");
  784. INIT_OLESTR(ocsChild2, "Child2");
  785. INIT_OLESTR(ocsStream, "Stream");
  786. INIT_OLESTR(ocsRenamedStream, "RenamedStream");
  787. INIT_OLESTR(ocsRenamedChild, "RenamedChild");
  788. printf("Reference storage tests:\n");
  789. printf("Optional features:\n");
  790. printf("Use '%s c' to create a test file\n", argv[0]);
  791. printf("Use '%s r' to verify read of the test file\n", argv[0]);
  792. printf("-----\n");
  793. if (argc==2) {
  794. printf(" * Interops testing --- ");
  795. if (*(argv[1])=='c') {
  796. printf("Create\n");
  797. fCreate=1;
  798. }
  799. else if (*(argv[1])=='r')
  800. printf("Read\n");
  801. else {
  802. printf("Wrong args: usage\nreftest [c|r]\nc - Create\nr - read\n");
  803. return 0;
  804. }
  805. g_fTestInterop = 1;
  806. }
  807. if (g_fTestInterop)
  808. {
  809. olChk(t_stream(FALSE, fCreate, FALSE));
  810. printf("\nTests passed successfully.\n");
  811. exit(0);
  812. }
  813. printf("\nTesting ILockBytes\n\n");
  814. olChk(t_create(FALSE, FALSE));
  815. olChk(t_open(FALSE, FALSE));
  816. olChk(t_addref(FALSE, FALSE));
  817. olChk(t_stream(FALSE, TRUE, FALSE));
  818. olChk(t_stat(FALSE, FALSE));
  819. olChk(t_dmodify(FALSE, FALSE));
  820. printf("\nTesting Storage\n\n");
  821. olChk(t_create(TRUE, FALSE));
  822. olChk(t_open(TRUE, FALSE));
  823. olChk(t_addref(TRUE, FALSE));
  824. olChk(t_dmodify(TRUE, FALSE));
  825. olChk(t_stream(TRUE, TRUE, FALSE));
  826. olChk(t_stat(TRUE, FALSE));
  827. olChk(t_stgmisc(FALSE));
  828. printf("\nTesting large Storage\n\n");
  829. olChk(t_create(TRUE, TRUE));
  830. olChk(t_open(TRUE, TRUE));
  831. olChk(t_addref(TRUE, TRUE));
  832. olChk(t_dmodify(TRUE, TRUE));
  833. olChk(t_stream(TRUE, TRUE, TRUE));
  834. olChk(t_stat(TRUE, TRUE));
  835. olChk(t_stgmisc(TRUE));
  836. #ifdef TEST_LARGE
  837. olChk(t_large());
  838. #endif
  839. printf("\nTests passed successfully.\n");
  840. return 0;
  841. EH_Err:
  842. printf("Tests failed with error %lX\n",sc);
  843. return EXIT_BADSC;
  844. }
  845. #ifdef _MSC_VER
  846. // some of these functions are a nuisance
  847. #pragma warning (disable:4127) // conditional expression is constant
  848. #pragma warning (disable:4514) // unreferenced inline function
  849. #endif