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.

684 lines
16 KiB

  1. /*++
  2. 1998 Seagate Software, Inc. All rights reserved.
  3. Module Name:
  4. HsmMgdRs.cpp
  5. Abstract:
  6. This component is an object representation of the HSM managed resource. It
  7. is both a persistable and collectable.
  8. Author:
  9. Cat Brant [cbrant] 13-Jan-1997
  10. Revision History:
  11. --*/
  12. #include "stdafx.h"
  13. #include "Wsb.h"
  14. #include "HsmEng.h"
  15. #include "HsmServ.h"
  16. #include "HsmConn.h"
  17. #include "HsmMgdRs.h"
  18. #include "Fsa.h"
  19. #define WSB_TRACE_IS WSB_TRACE_BIT_HSMENG
  20. HRESULT
  21. CHsmManagedResource::FinalConstruct(
  22. void
  23. )
  24. /*++
  25. Routine Description:
  26. This method does some initialization of the object that is necessary
  27. after construction.
  28. Arguments:
  29. None.
  30. Return Value:
  31. S_OK
  32. Anything returned by CWsbObject::FinalConstruct().
  33. --*/
  34. {
  35. HRESULT hr = S_OK;
  36. try {
  37. WsbTrace(OLESTR("CHsmManagedResource::FinalConstruct: this = %p\n"),
  38. static_cast<void*>(this));
  39. WsbAffirmHr(CWsbObject::FinalConstruct());
  40. m_ResourceId = GUID_NULL;
  41. } WsbCatch(hr);
  42. return(hr);
  43. }
  44. void CHsmManagedResource::FinalRelease(
  45. )
  46. {
  47. WsbTrace(OLESTR("CHsmManagedResource::FinalRelease: this = %p\n"),
  48. static_cast<void*>(this));
  49. // Let the parent class do his thing.
  50. CWsbObject::FinalRelease();
  51. }
  52. HRESULT
  53. CHsmManagedResource::GetResourceId(
  54. OUT GUID *pResourceId
  55. )
  56. /*++
  57. Routine Description:
  58. See IHsmManagedResource::GetResourceId
  59. Arguments:
  60. See IHsmManagedResource::GetResourceId
  61. Return Value:
  62. See IHsmManagedResource::GetResourceId
  63. --*/
  64. {
  65. HRESULT hr = S_OK;
  66. WsbTraceIn(OLESTR("CHsmManagedResource::GetResourceId"),OLESTR(""));
  67. try {
  68. //Make sure we can provide data memebers
  69. WsbAssert(0 != pResourceId, E_POINTER);
  70. //Provide the data members
  71. *pResourceId = m_ResourceId;
  72. } WsbCatch(hr);
  73. WsbTraceOut(OLESTR("CHsmManagedResource::GetResourceId"),
  74. OLESTR("hr = <%ls>, ResourceId = <%ls>>"),WsbHrAsString(hr), WsbPtrToGuidAsString(pResourceId));
  75. return(hr);
  76. }
  77. HRESULT
  78. CHsmManagedResource::InitFromFsaResource(
  79. IN IUnknown *pFsaResource
  80. )
  81. /*++
  82. Routine Description:
  83. See IHsmManagedResource::InitFromFsaResource
  84. Arguments:
  85. See IHsmManagedResource::InitFromFsaResource
  86. Return Value:
  87. See IHsmManagedResource::InitFromFsaResource
  88. --*/
  89. {
  90. HRESULT hr = S_OK;
  91. WsbTraceIn(OLESTR("CHsmManagedResource::InitFromFsaResource"),OLESTR(""));
  92. try {
  93. CComPtr<IFsaResource> l_pFsaResource;
  94. //Make sure we can provide data memebers
  95. WsbAssert(pFsaResource != 0, E_POINTER);
  96. //Provide the data members
  97. WsbAffirmHr(pFsaResource->QueryInterface(IID_IFsaResource, (void**) &l_pFsaResource));
  98. WsbAffirmHr(l_pFsaResource->GetIdentifier(&m_ResourceId));
  99. m_pFsaResourceInterface = pFsaResource;
  100. } WsbCatch(hr);
  101. WsbTraceOut(OLESTR("CHsmManagedResource::InitFromFsaResource"),OLESTR("hr = <%ls>"),WsbHrAsString(hr));
  102. return(hr);
  103. }
  104. HRESULT
  105. CHsmManagedResource::GetFsaResource(
  106. OUT IUnknown **ppFsa
  107. )
  108. /*++
  109. Routine Description:
  110. See IHsmManagedResource::GetFsaResource
  111. Arguments:
  112. See IHsmManagedResource::GetFsaResource
  113. Return Value:
  114. See IHsmManagedResource::GetFsaResource
  115. --*/
  116. {
  117. HRESULT hr = S_OK;
  118. WsbTraceIn(OLESTR("CHsmManagedResource::GetFsaResource"),OLESTR(""));
  119. try {
  120. CComPtr<IUnknown> l_pFsaResource;
  121. WsbAssert( 0 != ppFsa, E_POINTER);
  122. //
  123. // Using the GUID for this managed resource, get the COM
  124. // IFsaResource interface
  125. //
  126. WsbAffirmHr(HsmConnectFromId (HSMCONN_TYPE_RESOURCE, m_ResourceId, IID_IUnknown, (void **)ppFsa) );
  127. } WsbCatch(hr);
  128. WsbTraceOut(OLESTR("CHsmManagedResource::GetFsaResource"), OLESTR("hr = <%ls>"),WsbHrAsString(hr));
  129. return(hr);
  130. }
  131. HRESULT
  132. CHsmManagedResource::SetResourceId
  133. (
  134. IN GUID ResourceId
  135. )
  136. /*++
  137. Routine Description:
  138. See IHsmManagedResource::Set().
  139. Arguments:
  140. See IHsmManagedResource::Set().
  141. Return Value:
  142. S_OK - Success.
  143. --*/
  144. {
  145. WsbTraceIn(OLESTR("CHsmManagedResource::SetResourceId"),
  146. OLESTR("ResourceId = <%ls>"),
  147. WsbGuidAsString(ResourceId));
  148. m_isDirty = TRUE;
  149. m_ResourceId = ResourceId;
  150. WsbTraceOut(OLESTR("CHsmManagedResource::SetResourceId"), OLESTR("hr = <%ls>"),
  151. WsbHrAsString(S_OK));
  152. return(S_OK);
  153. }
  154. HRESULT
  155. CHsmManagedResource::GetClassID (
  156. OUT LPCLSID pClsId
  157. )
  158. /*++
  159. Routine Description:
  160. See IPerist::GetClassID()
  161. Arguments:
  162. See IPerist::GetClassID()
  163. Return Value:
  164. See IPerist::GetClassID()
  165. --*/
  166. {
  167. HRESULT hr = S_OK;
  168. WsbTraceIn(OLESTR("CHsmManagedResource::GetClassID"), OLESTR(""));
  169. try {
  170. WsbAssert(0 != pClsId, E_POINTER);
  171. *pClsId = CLSID_CHsmManagedResource;
  172. } WsbCatch(hr);
  173. WsbTraceOut(OLESTR("CHsmManagedResource::GetClassID"), OLESTR("hr = <%ls>, CLSID = <%ls>"), WsbHrAsString(hr), WsbGuidAsString(*pClsId));
  174. return(hr);
  175. }
  176. HRESULT
  177. CHsmManagedResource::GetSizeMax (
  178. OUT ULARGE_INTEGER* pcbSize
  179. )
  180. /*++
  181. Routine Description:
  182. See IPersistStream::GetSizeMax().
  183. Arguments:
  184. See IPersistStream::GetSizeMax().
  185. Return Value:
  186. See IPersistStream::GetSizeMax().
  187. --*/
  188. {
  189. HRESULT hr = S_OK;
  190. WsbTraceIn(OLESTR("CHsmManagedResource::GetSizeMax"), OLESTR(""));
  191. try {
  192. WsbAssert(0 != pcbSize, E_POINTER);
  193. pcbSize->QuadPart = WsbPersistSizeOf(GUID);
  194. } WsbCatch(hr);
  195. WsbTraceOut(OLESTR("CHsmManagedResource::GetSizeMax"),
  196. OLESTR("hr = <%ls>, Size = <%ls>"), WsbHrAsString(hr),
  197. WsbPtrToUliAsString(pcbSize));
  198. return(hr);
  199. }
  200. HRESULT
  201. CHsmManagedResource::Load (
  202. IN IStream* pStream
  203. )
  204. /*++
  205. Routine Description:
  206. See IPersistStream::Load().
  207. Arguments:
  208. See IPersistStream::Load().
  209. Return Value:
  210. See IPersistStream::Load().
  211. --*/
  212. {
  213. HRESULT hr = S_OK;
  214. ULONG ulBytes;
  215. WsbTraceIn(OLESTR("CHsmManagedResource::Load"), OLESTR(""));
  216. try {
  217. WsbAssert(0 != pStream, E_POINTER);
  218. WsbAffirmHr(pStream->Read((void*) &m_ResourceId, sizeof(GUID), &ulBytes));
  219. WsbAffirm(ulBytes == sizeof(GUID), E_FAIL);
  220. } WsbCatch(hr);
  221. WsbTraceOut(OLESTR("CHsmManagedResource::Load"),
  222. OLESTR("hr = <%ls>, GUID = <%ls>"),
  223. WsbHrAsString(hr),
  224. WsbGuidAsString(m_ResourceId));
  225. return(hr);
  226. }
  227. HRESULT
  228. CHsmManagedResource::Save (
  229. IN IStream* pStream,
  230. IN BOOL clearDirty
  231. )
  232. /*++
  233. Routine Description:
  234. See IPersistStream::Save().
  235. Arguments:
  236. See IPersistStream::Save().
  237. Return Value:
  238. See IPersistStream::Save().
  239. --*/
  240. {
  241. HRESULT hr = S_OK;
  242. ULONG ulBytes;
  243. WsbTraceIn(OLESTR("CHsmManagedResource::Save"), OLESTR("clearDirty = <%ls>"), WsbBoolAsString(clearDirty));
  244. try {
  245. WsbAssert(0 != pStream, E_POINTER);
  246. WsbAffirmHr(pStream->Write((void*) &m_ResourceId, sizeof(GUID), &ulBytes));
  247. WsbAffirm(ulBytes == sizeof(GUID), E_FAIL);
  248. // If we got it saved and we were asked to clear the dirty bit, then
  249. // do so now.
  250. if (clearDirty) {
  251. m_isDirty = FALSE;
  252. }
  253. } WsbCatch(hr);
  254. WsbTraceOut(OLESTR("CHsmManagedResource::Save"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  255. return(hr);
  256. }
  257. HRESULT
  258. CHsmManagedResource::Test (
  259. OUT USHORT *pTestsPassed,
  260. OUT USHORT *pTestsFailed
  261. )
  262. /*++
  263. Routine Description:
  264. See IWsbTestable::Test().
  265. Arguments:
  266. See IWsbTestable::Test().
  267. Return Value:
  268. See IWsbTestable::Test().
  269. --*/
  270. {
  271. #if 0
  272. HRESULT hr = S_OK;
  273. CComPtr<IHsmManagedResource> pHsmManagedResource1;
  274. CComPtr<IHsmManagedResource> pHsmManagedResource2;
  275. SHORT result;
  276. GUID l_ResourceId;
  277. WsbTraceIn(OLESTR("CHsmManagedResource::Test"), OLESTR(""));
  278. *pTestsPassed = *pTestsFailed = 0;
  279. try {
  280. // Get the pHsmManagedResource interface.
  281. WsbAffirmHr(((IUnknown*)(IHsmManagedResource*) this)->QueryInterface(IID_IHsmManagedResource,
  282. (void**) &pHsmManagedResource1));
  283. try {
  284. // Set the HsmManagedResource to a value, and see if it is returned.
  285. WsbAffirmHr(pHsmManagedResource1->SetResourceId(CLSID_CHsmManagedResource));
  286. WsbAffirmHr(pHsmManagedResource1->GetResourceId(&l_ResourceId));
  287. WsbAffirm((l_ResourceId == CLSID_CHsmManagedResource), E_FAIL);
  288. } WsbCatch(hr);
  289. if (hr == S_OK) {
  290. (*pTestsPassed)++;
  291. } else {
  292. (*pTestsFailed)++;
  293. }
  294. hr = S_OK;
  295. try {
  296. //Create another instance and test the comparisson methods:
  297. WsbAffirmHr(CoCreateInstance(CLSID_CHsmManagedResource, NULL, CLSCTX_ALL, IID_IHsmManagedResource, (void**) &pHsmManagedResource2));
  298. // Check the default values.
  299. WsbAffirmHr(pHsmManagedResource2->GetResourceId(&l_ResourceId));
  300. WsbAffirm((l_ResourceId == GUID_NULL), E_FAIL);
  301. } WsbCatch(hr);
  302. if (hr == S_OK) {
  303. (*pTestsPassed)++;
  304. } else {
  305. (*pTestsFailed)++;
  306. }
  307. hr = S_OK;
  308. try {
  309. // IsEqual()
  310. WsbAffirmHr(pHsmManagedResource1->SetResourceId(CLSID_CWsbBool));
  311. WsbAffirmHr(pHsmManagedResource2->SetResourceId(CLSID_CWsbBool));
  312. WsbAffirmHr(pHsmManagedResource1->IsEqual(pHsmManagedResource2));
  313. } WsbCatch(hr);
  314. if (hr == S_OK) {
  315. (*pTestsPassed)++;
  316. } else {
  317. (*pTestsFailed)++;
  318. }
  319. hr = S_OK;
  320. try {
  321. WsbAffirmHr(pHsmManagedResource1->SetResourceId(CLSID_CWsbBool));
  322. WsbAffirmHr(pHsmManagedResource2->SetResourceId(CLSID_CWsbLong));
  323. WsbAffirm((pHsmManagedResource1->IsEqual(pHsmManagedResource2) == S_FALSE), E_FAIL);
  324. } WsbCatch(hr);
  325. if (hr == S_OK) {
  326. (*pTestsPassed)++;
  327. } else {
  328. (*pTestsFailed)++;
  329. }
  330. hr = S_OK;
  331. try {
  332. // CompareTo()
  333. WsbAffirmHr(pHsmManagedResource1->SetResourceId(CLSID_CWsbBool));
  334. WsbAffirmHr(pHsmManagedResource2->SetResourceId(CLSID_CWsbBool));
  335. WsbAffirm((pHsmManagedResource1->CompareTo(pHsmManagedResource2, &result) == S_OK) && (result != 0), E_FAIL);
  336. } WsbCatch(hr);
  337. if (hr == S_OK) {
  338. (*pTestsPassed)++;
  339. } else {
  340. (*pTestsFailed)++;
  341. }
  342. hr = S_OK;
  343. try {
  344. WsbAffirmHr(pHsmManagedResource1->SetResourceId(CLSID_CWsbBool));
  345. WsbAffirmHr(pHsmManagedResource2->SetResourceId(CLSID_CWsbLong));
  346. WsbAffirm(((pHsmManagedResource1->CompareTo(pHsmManagedResource2, &result) == S_FALSE) && (result > 0)), E_FAIL);
  347. } WsbCatch(hr);
  348. if (hr == S_OK) {
  349. (*pTestsPassed)++;
  350. } else {
  351. (*pTestsFailed)++;
  352. }
  353. hr = S_OK;
  354. try {
  355. WsbAffirmHr(pHsmManagedResource1->SetResourceId(CLSID_CWsbBool));
  356. WsbAffirmHr(pHsmManagedResource2->SetResourceId(CLSID_CWsbBool));
  357. WsbAffirm((pHsmManagedResource1->CompareTo(pHsmManagedResource2, &result) == S_OK), E_FAIL);
  358. } WsbCatch(hr);
  359. if (hr == S_OK) {
  360. (*pTestsPassed)++;
  361. } else {
  362. (*pTestsFailed)++;
  363. }
  364. try {
  365. // Try out the persistence stuff.
  366. CComPtr<IPersistFile> pFile1;
  367. CComPtr<IPersistFile> pFile2;
  368. WsbAffirmHr(pHsmManagedResource1->QueryInterface(IID_IPersistFile, (void**) &pFile1));
  369. WsbAffirmHr(pHsmManagedResource2->QueryInterface(IID_IPersistFile, (void**) &pFile2));
  370. LPOLESTR szTmp = NULL;
  371. // The item should be dirty.
  372. try {
  373. WsbAffirmHr(pHsmManagedResource2->SetResourceId(CLSID_CWsbLong));
  374. WsbAffirmHr(pFile2->IsDirty());
  375. } WsbCatch(hr);
  376. if (hr == S_OK) {
  377. (*pTestsPassed)++;
  378. } else {
  379. (*pTestsFailed)++;
  380. }
  381. hr = S_OK;
  382. try {
  383. // Save the item, and remember.
  384. WsbAffirmHr(pFile2->Save(OLESTR("c:\\WsbTests\\mngdRes.tst"), TRUE));
  385. } WsbCatch(hr);
  386. if (hr == S_OK) {
  387. (*pTestsPassed)++;
  388. } else {
  389. (*pTestsFailed)++;
  390. }
  391. hr = S_OK;
  392. try {
  393. // It shouldn't be dirty.
  394. WsbAffirm((pFile2->IsDirty() == S_FALSE), E_FAIL);
  395. } WsbCatch(hr);
  396. if (hr == S_OK) {
  397. (*pTestsPassed)++;
  398. } else {
  399. (*pTestsFailed)++;
  400. }
  401. hr = S_OK;
  402. try {
  403. // Try reading it in to another object.
  404. WsbAffirmHr(pHsmManagedResource1->SetResourceId(CLSID_CWsbLong));
  405. WsbAffirmHr(pFile1->Load(OLESTR("c:\\WsbTests\\mngdRes.tst"), 0));
  406. WsbAffirmHr(pHsmManagedResource1->CompareToIHsmManagedResource(pHsmManagedResource2, &result));
  407. }WsbCatch(hr);
  408. if (hr == S_OK) {
  409. (*pTestsPassed)++;
  410. } else {
  411. (*pTestsFailed)++;
  412. }
  413. } WsbCatch(hr);
  414. } WsbCatch(hr);
  415. WsbTraceOut(OLESTR("CHsmManagedResource::Test"), OLESTR("hr = <%ls>"),WsbHrAsString(hr));
  416. #else
  417. UNREFERENCED_PARAMETER(pTestsPassed);
  418. UNREFERENCED_PARAMETER(pTestsFailed);
  419. #endif
  420. return(S_OK);
  421. }
  422. HRESULT CHsmManagedResource::CompareTo (
  423. IN IUnknown* pCollectable,
  424. OUT short* pResult
  425. )
  426. /*++
  427. Routine Description:
  428. 1 : object > value
  429. 0 : object = value
  430. -1 : object < value
  431. In addition, the return code is S_OK if the object = value and
  432. S_FALSE otherwise.
  433. Arguments:
  434. Return Value:
  435. S_OK - object = value
  436. S_FALSE - object != value
  437. --*/
  438. {
  439. HRESULT hr = S_OK;
  440. CComPtr<IHsmManagedResource> pHsmManagedResource;
  441. WsbTraceIn(OLESTR("CHsmManagedResource::CompareTo"), OLESTR(""));
  442. // Did they give us a valid item to compare to?
  443. try {
  444. WsbAssert(pCollectable != NULL, E_POINTER);
  445. // We need the IWsbLong interface to get the value of the object.
  446. WsbAffirmHr(pCollectable->QueryInterface(IID_IHsmManagedResource, (void**) &pHsmManagedResource));
  447. hr = pHsmManagedResource->CompareToIHsmManagedResource(this, pResult);
  448. } WsbCatch(hr);
  449. WsbTraceOut(OLESTR("CHsmManagedResource::CompareTo"), OLESTR("hr = <%ls>, pResult = <%d>"), WsbHrAsString(hr), pResult);
  450. return(hr);
  451. }
  452. HRESULT CHsmManagedResource::CompareToIHsmManagedResource (
  453. IN IHsmManagedResource* pHsmManagedResource,
  454. OUT short* pResult
  455. )
  456. {
  457. HRESULT hr = S_OK;
  458. GUID l_ResourceId;
  459. BOOL areGuidsEqual;
  460. WsbTraceIn(OLESTR("CHsmManagedResource::CompareToIHsmManagedResource"), OLESTR(""));
  461. try {
  462. // Did they give us a valid item to compare to?
  463. WsbAssert(pHsmManagedResource != NULL, E_POINTER);
  464. WsbAffirmHr(((IHsmManagedResource *)pHsmManagedResource)->GetResourceId(&l_ResourceId));
  465. // Make sure the GUID matches. Then see if the SegStartLoc is in the range of this entry
  466. areGuidsEqual = IsEqualGUID(m_ResourceId, l_ResourceId);
  467. WsbAffirm( (areGuidsEqual == TRUE), S_FALSE);
  468. } WsbCatch(hr);
  469. WsbTraceOut(OLESTR("CHsmManagedResource::CompareToIHsmManagedResource"), OLESTR("hr = <%ls>, pResult = <%d>"), WsbHrAsString(hr), pResult);
  470. return(hr);
  471. }