Source code of Windows XP (NT5)
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.

542 lines
19 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996.
  5. //
  6. // File: ttrkcom.cxx
  7. //
  8. // Contents: testing IPersistStreamInit interface
  9. //
  10. // Classes:
  11. //
  12. // Functions:
  13. //
  14. //
  15. //
  16. // History: 21-Apl-97 weiruc Created.
  17. //
  18. // Notes: Use IStream on global memory instead of a file. Not sure if
  19. // it's worth the trouble to test with disk file.
  20. //
  21. //
  22. //--------------------------------------------------------------------------
  23. #include <pch.cxx>
  24. #pragma hdrstop
  25. #define TRKDATA_ALLOCATE
  26. #include <trkwks.hxx>
  27. #include <cfiletim.hxx>
  28. #include <trkcom.h>
  29. #include <trkcom.hxx>
  30. #include <ocidl.h>
  31. DWORD g_Debug = TRKDBG_ERROR;
  32. void ExtractPersistentState(CTrackFile* pTrackFile, LinkTrackPersistentState* target)
  33. {
  34. memcpy(target, &(pTrackFile->_PersistentState), sizeof(pTrackFile->_PersistentState));
  35. }
  36. BOOL CmpPersistentState(CTrackFile* pTrackFile, LinkTrackPersistentState* target)
  37. {
  38. if(memcmp(&(pTrackFile->_PersistentState), target, sizeof(pTrackFile->_PersistentState)) != 0)
  39. {
  40. return FALSE;
  41. }
  42. else
  43. {
  44. return TRUE;
  45. }
  46. }
  47. void FakeCreateFromPath(CTrackFile* pTrackFile)
  48. {
  49. char* pbScanner;
  50. // I can't get time() function to work. xxx
  51. // srand((unsigned)time(NULL));
  52. pTrackFile->_fLoaded = FALSE;
  53. pTrackFile->InitNew();
  54. pbScanner = (char*)&pTrackFile->_PersistentState.droidCurrent;
  55. TrkAssert(FIELD_OFFSET(LinkTrackPersistentState, droidCurrent) == sizeof(DWORD)+sizeof(CLSID));
  56. for(; pbScanner < (char*)&pTrackFile->_PersistentState + sizeof(pTrackFile->_PersistentState); pbScanner++)
  57. {
  58. *pbScanner = (char)rand();
  59. }
  60. pTrackFile->_fDirty = TRUE;
  61. }
  62. EXTERN_C int __cdecl _tmain(int argc, TCHAR **argv)
  63. {
  64. HRESULT hr;
  65. ITrackFile* pTrackFile1 = NULL;
  66. ITrackFile* pTrackFile2 = NULL;
  67. IPersistStreamInit* pPersistStreamInit = NULL;
  68. IPersistMemory* pPersistMemory = NULL;
  69. IStream* pStream = NULL;
  70. HGLOBAL hmemStream = NULL, hmemMemory = NULL;
  71. CLSID clsid;
  72. ULARGE_INTEGER cbSize_PersistStream;
  73. ULONG cbSize_PersistMemory;
  74. LinkTrackPersistentState target;
  75. HANDLE hfileTest = INVALID_HANDLE_VALUE;
  76. LPVOID pszErrorMsg;
  77. BOOL fInitNew = TRUE, // flags of whether
  78. fIsDirty = TRUE, // tests succeeded or
  79. fSaveStream = TRUE, // failed
  80. fLoadStream = TRUE,
  81. fSaveMemory = TRUE,
  82. fLoadMemory = TRUE;
  83. ULARGE_INTEGER ulSize1; //
  84. ULONG ulSize2; // filled by GetSizeMax
  85. ULONG cbWritten;
  86. LARGE_INTEGER zeroOffset; // to reset IStream seek pointer
  87. DWORD cbPath;
  88. OLECHAR oszPath[ MAX_PATH + 1 ];
  89. BYTE rgb[256];
  90. __try
  91. {
  92. TrkDebugCreate( TRK_DBG_FLAGS_WRITE_TO_DBG | TRK_DBG_FLAGS_WRITE_TO_STDOUT, "TTRKCOM" );
  93. CoInitialize( NULL );
  94. hmemStream = GlobalAlloc(GMEM_FIXED, sizeof(target));
  95. if(NULL == hmemStream)
  96. {
  97. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  98. NULL,
  99. GetLastError(),
  100. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  101. (LPTSTR) &pszErrorMsg,
  102. 0,
  103. NULL);
  104. _tprintf(TEXT("Fatal error: %s\n"), pszErrorMsg);
  105. LocalFree(pszErrorMsg);
  106. goto ExitWithError;
  107. }
  108. hmemMemory = GlobalAlloc(GMEM_FIXED, sizeof(target));
  109. if(NULL == hmemMemory)
  110. {
  111. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  112. NULL,
  113. GetLastError(),
  114. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  115. (LPTSTR) &pszErrorMsg,
  116. 0,
  117. NULL);
  118. _tprintf(TEXT("Fatal error: %s\n"), pszErrorMsg);
  119. LocalFree(pszErrorMsg);
  120. goto ExitWithError;
  121. }
  122. if(argc > 1)
  123. {
  124. _tprintf(TEXT("Usage: %s"), argv[0]);
  125. goto ExitWithError;
  126. }
  127. _tprintf(TEXT("********** Testing IPersistStreamInit and IPersistMemory **********\n"));
  128. // Create a temporary file for testing.
  129. hfileTest = CreateFile(TEXT("c:\\_testfile_"),
  130. GENERIC_WRITE, 0, NULL, CREATE_NEW,
  131. FILE_ATTRIBUTE_TEMPORARY,
  132. NULL);
  133. if(hfileTest == INVALID_HANDLE_VALUE)
  134. {
  135. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  136. NULL,
  137. GetLastError(),
  138. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  139. (LPTSTR) &pszErrorMsg,
  140. 0,
  141. NULL);
  142. _tprintf(TEXT("Fatal error: %s\n"), pszErrorMsg);
  143. LocalFree(pszErrorMsg);
  144. goto ExitWithError;
  145. }
  146. if(CloseHandle(hfileTest) == 0)
  147. {
  148. _tprintf(TEXT("Fatal error: Can't close test file \"c:\\_testfile_\"\n"));
  149. goto ExitWithError;
  150. }
  151. hfileTest = INVALID_HANDLE_VALUE;
  152. zeroOffset.QuadPart = 0;
  153. hr = CoCreateInstance(CLSID_TrackFile, NULL, CLSCTX_ALL, IID_ITrackFile, (void**)&pTrackFile1);
  154. if(!SUCCEEDED(hr))
  155. {
  156. _tprintf(TEXT("Couldn't get an ITrackFile (%08x)\n"), hr);
  157. goto ExitWithError;
  158. }
  159. hr = pTrackFile1->QueryInterface(IID_IPersistStreamInit, (void**)&pPersistStreamInit);
  160. if(!SUCCEEDED(hr))
  161. {
  162. _tprintf(TEXT("Couldn't QI ITrackFile for IPersistStreamInit. hr = %08x"), hr);
  163. }
  164. hr = pTrackFile1->QueryInterface(IID_IPersistMemory, (void**)&pPersistMemory);
  165. if(!SUCCEEDED(hr))
  166. {
  167. _tprintf(TEXT("Couldn't QI ITrackFile for IPersistMemory. hr = %08x"), hr);
  168. }
  169. hr = pPersistStreamInit->InitNew(); // Should be able to call InitNew
  170. if(!SUCCEEDED(hr)) // on a newly created object
  171. {
  172. _tprintf(TEXT("IPersistStreamInit/IPersistMemory::InitNew FAILED. hr = %08x\n"), hr);
  173. fInitNew = FALSE;
  174. }
  175. // --------------------
  176. // Parameter Validation
  177. // --------------------
  178. hr = pTrackFile1->QueryInterface( IID_IPersistStreamInit, NULL );
  179. if( E_INVALIDARG != hr )
  180. _tprintf(TEXT("ITrackFile::QueryInterface(...,NULL) FAILED, hr = %08x\n"), hr );
  181. hr = pTrackFile1->CreateFromPath( NULL );
  182. if( E_INVALIDARG != hr )
  183. _tprintf(TEXT("ITrackFile::CreateFromPath(NULL) FAILED, hr = %08x\n"), hr );
  184. hr = pTrackFile1->QueryInterface( IID_IPersistStreamInit, NULL );
  185. if( E_INVALIDARG != hr )
  186. _tprintf(TEXT("ITrackFile::QueryInterface(...,NULL) FAILED, hr = %08x\n"), hr );
  187. hr = pTrackFile1->Resolve( NULL, oszPath, 1 );
  188. if( E_INVALIDARG != hr )
  189. _tprintf(TEXT("ITrackFile::Resolve(NULL,...) FAILED, hr = %08x\n"), hr );
  190. hr = pTrackFile1->Resolve( &cbPath, NULL, 1 );
  191. if( E_INVALIDARG != hr )
  192. _tprintf(TEXT("ITrackFile::Resolve(...,NULL,...) FAILED, hr = %08x\n"), hr );
  193. hr = pPersistMemory->GetClassID( NULL );
  194. if( E_POINTER != hr )
  195. _tprintf(TEXT("IPersistMemory::GetClassID(NULL) FAILED, hr = %08x\n"), hr );
  196. hr = pPersistMemory->Load( NULL, 1);
  197. if( E_POINTER != hr )
  198. _tprintf(TEXT("IPersistMemory::Load(NULL,...) FAILED, hr = %08x\n"), hr );
  199. hr = pPersistMemory->Save( NULL, TRUE, 1);
  200. if( E_POINTER != hr )
  201. _tprintf(TEXT("IPersistMemory::Save(NULL,...) FAILED, hr = %08x\n"), hr );
  202. hr = pPersistMemory->Save( &rgb, TRUE, 1);
  203. if( E_INVALIDARG != hr )
  204. _tprintf(TEXT("IPersistMemory::Save(...,1) FAILED, hr = %08x\n"), hr );
  205. hr = pPersistMemory->GetSizeMax( NULL );
  206. if( E_POINTER != hr )
  207. _tprintf(TEXT("IPersistMemory::GetSizeMax(NULL) FAILED, hr = %08x\n"), hr );
  208. hr = pPersistStreamInit->GetSizeMax( NULL );
  209. if( E_POINTER != hr )
  210. _tprintf(TEXT("IPersistStreamInit::GetSizeMax(NULL) FAILED, hr = %08x\n"), hr );
  211. hr = pPersistStreamInit->Load( NULL );
  212. if( E_POINTER != hr )
  213. _tprintf(TEXT("IPersistStreamInit::Load(NULL) FAILED, hr = %08x\n"), hr );
  214. hr = pPersistStreamInit->Save( NULL, TRUE );
  215. if( E_POINTER != hr )
  216. _tprintf(TEXT("IPersistStreamInit::Save(NULL,...) FAILED, hr = %08x\n"), hr );
  217. if(fInitNew == TRUE)
  218. {
  219. hr = pPersistMemory->InitNew(); // InitNew can only be called once
  220. if(SUCCEEDED(hr))
  221. {
  222. _tprintf(TEXT("IPersistStreamInit/IPersistMemory::InitNew FAILED. hr = %08x\n"), hr);
  223. fInitNew = FALSE;
  224. }
  225. else if(E_UNEXPECTED != hr)
  226. {
  227. _tprintf(TEXT("IPersistStreamInit/IPersistMemory::InitNew FAILED. hr = %08x\n"), hr);
  228. fInitNew = FALSE;
  229. }
  230. }
  231. hr = pPersistStreamInit->IsDirty(); // IsDirty should return FALSE
  232. if(S_FALSE != hr)
  233. {
  234. _tprintf(TEXT("IPersistStreamInit/IPersistMemory::IsDirty FAILED. hr = %08x\n"), hr);
  235. fIsDirty = FALSE;
  236. }
  237. pPersistStreamInit->GetSizeMax(&ulSize1); // Test GetSizeMax
  238. pPersistMemory->GetSizeMax(&ulSize2);
  239. if(ulSize1.QuadPart != sizeof(target))
  240. {
  241. _tprintf(TEXT("IPersistStreamInit::GetSizeMax FAILED\n"));
  242. }
  243. else
  244. {
  245. _tprintf(TEXT("IPersistStreamInit::GetSizeMax PASSED\n"));
  246. }
  247. if(ulSize2 != sizeof(target))
  248. {
  249. _tprintf(TEXT("IPersistMemory::GetSizeMax FAILED\n"));
  250. }
  251. else
  252. {
  253. _tprintf(TEXT("IPersistMemory::GetSizeMax PASSED\n"));
  254. }
  255. pPersistStreamInit->GetClassID(&clsid); // Test GetClassID
  256. if(IID_ITrackFile != clsid)
  257. {
  258. _tprintf(TEXT("IPersistStreamInit::GetClassID FAILED\n"));
  259. }
  260. else
  261. {
  262. _tprintf(TEXT("IPersistStreamInit::GetClassID PASSED\n"));
  263. }
  264. pPersistMemory->GetClassID(&clsid);
  265. if(IID_ITrackFile != clsid)
  266. {
  267. _tprintf(TEXT("IPersistMemory::GetClassID FAILED\n"));
  268. }
  269. else
  270. {
  271. _tprintf(TEXT("IPersistMemory::GetClassID PASSED\n"));
  272. }
  273. // This call breaks on my test machine. Since I don't really need it to test my
  274. // program, I'm going to fake the result from this call.
  275. // hr = pTrackFile1->CreateFromPath(TEXT("c:\\_testfile_"));
  276. // if(!SUCCEEDED(hr))
  277. // {
  278. // _tprintf(TEXT("Fatal error: Couldn't call CTrackFile::CreateFromPath(\"c:\\_testfile_\") (%08x)\n"), hr);
  279. // goto ExitWithError;
  280. // }
  281. FakeCreateFromPath((CTrackFile*)pTrackFile1);
  282. if(fIsDirty == TRUE)
  283. {
  284. hr = pPersistStreamInit->IsDirty(); // After CreateFromPath IsDirty
  285. if(S_OK != hr) // should return dirty.
  286. {
  287. _tprintf(TEXT("IPersistStreamInit::IsDirty FAILED. hr = %08x\n"), hr);
  288. _tprintf(TEXT("IPersistMemory::IsDirty FAILED. hr = %08x\n"), hr );
  289. fIsDirty = FALSE;
  290. }
  291. }
  292. if(fInitNew == TRUE)
  293. {
  294. hr = pPersistStreamInit->InitNew(); // Shouldn't be able to call InitNew
  295. if(S_OK == hr || E_UNEXPECTED != hr) // after TrackFile is initialized.
  296. {
  297. _tprintf(TEXT("IPersistStreamInit/IPersistMemory::InitNew FAILED. hr = %08x\n"), hr);
  298. }
  299. else
  300. {
  301. _tprintf(TEXT("IPersistStreamInit/IPersistMemory::InitNew PASSED\n"));
  302. }
  303. }
  304. else
  305. {
  306. _tprintf(TEXT("IPersistStreamInit/IPersistMemory::InitNew FAILED. hr = %08x\n"), hr);
  307. }
  308. hr = CreateStreamOnHGlobal(hmemStream, FALSE, &pStream); // pPersistStreamInit::Save test
  309. if( !SUCCEEDED(hr) )
  310. {
  311. _tprintf(TEXT("Fatal error: Couldn't get an IStream (%08x)\n"), hr);
  312. goto ExitWithError;
  313. }
  314. hr = pPersistStreamInit->Save(pStream, FALSE); // fClearDirty is set to FALSE
  315. if(!SUCCEEDED(hr))
  316. {
  317. _tprintf(TEXT("IPersistStreamInit::Save FAILED (%08x)\n"), hr);
  318. fSaveStream = FALSE;
  319. }
  320. else
  321. {
  322. if(fIsDirty == TRUE)
  323. {
  324. hr = pPersistStreamInit->IsDirty(); // After Save, IsDirty should still return
  325. if(S_OK != hr) // dirty when fClearDirty is set to be FALSE.
  326. {
  327. _tprintf(TEXT("Either IPersistStreamInit/IPersistMemory::IsDirty FAILED. hr = %08x\n"), hr);
  328. _tprintf(TEXT("or IPersistStreamInit::Save with fClearDirty = FALSE FAILED\n"));
  329. fIsDirty = FALSE;
  330. }
  331. }
  332. }
  333. pStream->Seek(zeroOffset, STREAM_SEEK_SET, NULL);
  334. hr = pPersistStreamInit->Save(pStream, TRUE); // set fClearDirty to be TRUE
  335. if(!SUCCEEDED(hr))
  336. {
  337. _tprintf(TEXT("IPersistStreamInit::Save FAILED (%08x)\n"), hr);
  338. fSaveStream = FALSE;
  339. }
  340. else
  341. {
  342. if(fIsDirty == TRUE)
  343. {
  344. hr = pPersistStreamInit->IsDirty(); // After Save, IsDirty should return
  345. if(S_FALSE != hr) // clean when fClearDirty is TRUE.
  346. {
  347. _tprintf(TEXT("Either IPersistStreamInit/IPersistMemory::IsDirty FAILED. hr = %08x\n"), hr);
  348. _tprintf(TEXT("or IPersistStreamInit::Save with fClearDirty = TRUE FAILED\n"));
  349. fIsDirty = FALSE;
  350. }
  351. }
  352. }
  353. if(fIsDirty == TRUE)
  354. {
  355. _tprintf(TEXT("IPersistStreamInit/IPersistMemory::IsDirty PASSED\n"));
  356. }
  357. hr = pPersistMemory->Save(hmemMemory, FALSE, GlobalSize(hmemMemory)); // test IPersistMemory::Save
  358. if(!SUCCEEDED(hr)) // fClearDirty is set to be FALSE
  359. {
  360. _tprintf(TEXT("IPersistMemory::Save FAILED (%08x)\n"), hr);
  361. fSaveMemory = FALSE;
  362. }
  363. hr = pPersistMemory->Save(hmemMemory, TRUE, GlobalSize(hmemMemory)); // fClearDirty = TRUE
  364. if(!SUCCEEDED(hr))
  365. {
  366. _tprintf(TEXT("IPersistMemory::Save FAILED (%08x)\n"), hr);
  367. fSaveMemory = FALSE;
  368. }
  369. if(TRUE == fSaveStream && TRUE == fSaveMemory) // Compare the memory content after
  370. { // Save operation
  371. ExtractPersistentState((CTrackFile*)pTrackFile1, &target);
  372. if(memcmp(hmemStream, &target, sizeof(target)) == 0)
  373. {
  374. _tprintf(TEXT("IPersistStreamInit::Save PASSED (if no error message before about fClearDirty)\n"));
  375. }
  376. else
  377. {
  378. _tprintf(TEXT("IPersistStreamInit::Save FAILED\n"));
  379. }
  380. if(memcmp(hmemMemory, &target, sizeof(target)) == 0)
  381. {
  382. _tprintf(TEXT("IPersistMemory::Save PASSED (if no error message before about fClearDirty)\n"));
  383. }
  384. else
  385. {
  386. _tprintf(TEXT("IPersistMemory::Save FAILED\n"));
  387. }
  388. }
  389. RELEASE_INTERFACE(pTrackFile1);
  390. RELEASE_INTERFACE(pPersistMemory);
  391. RELEASE_INTERFACE(pPersistStreamInit);
  392. RELEASE_INTERFACE(pStream);
  393. // Test Load functions
  394. hr = CoCreateInstance(IID_ITrackFile, NULL, CLSCTX_ALL, IID_ITrackFile, (void**)&pTrackFile1);
  395. if(!SUCCEEDED(hr))
  396. {
  397. _tprintf(TEXT("Couldn't get an ITrackFile (%08x)\n"), hr);
  398. goto ExitWithError;
  399. }
  400. hr = pTrackFile1->QueryInterface(IID_IPersistStreamInit, (void**)&pPersistStreamInit);
  401. if(!SUCCEEDED(hr))
  402. {
  403. _tprintf(TEXT("Couldn't QI ITrackFile for IPersistStreamInit"));
  404. goto ExitWithError;
  405. }
  406. hr = CoCreateInstance(IID_ITrackFile, NULL, CLSCTX_ALL, IID_ITrackFile, (void**)&pTrackFile2);
  407. if(!SUCCEEDED(hr))
  408. {
  409. _tprintf(TEXT("Couldn't get an ITrackFile (%08x)\n"), hr);
  410. goto ExitWithError;
  411. }
  412. hr = pTrackFile2->QueryInterface(IID_IPersistMemory, (void**)&pPersistMemory);
  413. if(!SUCCEEDED(hr))
  414. {
  415. _tprintf(TEXT("Couldn't QI ITrackFile for IPersistMemory"));
  416. goto ExitWithError;
  417. }
  418. memcpy(hmemMemory, &target, sizeof(target)); // Set up sources to load from
  419. CreateStreamOnHGlobal(hmemStream, TRUE, &pStream);
  420. hr = pStream->Write((byte*)&target, sizeof(target), &cbWritten);
  421. if(!SUCCEEDED(hr) || sizeof(target) != cbWritten)
  422. {
  423. _tprintf(TEXT("Fatal error: Can't create stream object\n"));
  424. goto ExitWithError;
  425. }
  426. pStream->Seek(zeroOffset, STREAM_SEEK_SET, NULL);
  427. hr = pPersistStreamInit->Load(pStream); // Test IPersistStreamInit::Load
  428. if(!SUCCEEDED(hr))
  429. {
  430. _tprintf(TEXT("IPersistStreamInit::Load FAILED. hr = %08x\n"), hr);
  431. }
  432. else if(CmpPersistentState((CTrackFile*)pTrackFile1, (LinkTrackPersistentState*)hmemStream) != TRUE)
  433. {
  434. _tprintf(TEXT("IPersistStreamInit::Load FAILED"));
  435. }
  436. else
  437. {
  438. _tprintf(TEXT("IPersistStreamInit::Load PASSED\n"));
  439. }
  440. hr = pPersistMemory->Load(hmemMemory, GlobalSize(hmemMemory)); // Test IPersistMemory::Load
  441. if(!SUCCEEDED(hr))
  442. {
  443. _tprintf(TEXT("IPersistMemory::Load FAILED. hr = %08x\n"), hr);
  444. }
  445. else if(CmpPersistentState((CTrackFile*)pTrackFile2, (LinkTrackPersistentState*)hmemMemory) != TRUE)
  446. {
  447. _tprintf(TEXT("IPersistMemory::Load FAILED\n"));
  448. }
  449. else
  450. {
  451. _tprintf(TEXT("IPersistMemory::Load PASSED\n"));
  452. }
  453. }
  454. __except(BreakOnDebuggableException())
  455. {
  456. _tprintf(TEXT("Exception happened. Exception code = %08x\n"), GetExceptionCode());
  457. }
  458. ExitWithError:
  459. RELEASE_INTERFACE(pTrackFile1);
  460. RELEASE_INTERFACE(pTrackFile2);
  461. RELEASE_INTERFACE(pPersistStreamInit);
  462. RELEASE_INTERFACE(pPersistMemory);
  463. RELEASE_INTERFACE(pStream);
  464. GlobalFree(hmemStream);
  465. GlobalFree(hmemMemory);
  466. if(DeleteFile(TEXT("c:\\_testfile_")) == 0)
  467. {
  468. _tprintf(TEXT("WARNING: Couldn't delete test file \"c:\\_testfile_\""));
  469. }
  470. return(0);
  471. }