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.

262 lines
5.8 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1993.
  5. //
  6. // File: rotut.cxx
  7. //
  8. // Contents: Unit Test for ROT
  9. //
  10. // Classes: MISSING
  11. //
  12. // Functions: MISSING
  13. //
  14. // History: 16-Oct-93 Ricksa Created
  15. // 31-Dec-93 ErikGav Chicago port
  16. //
  17. //--------------------------------------------------------------------------
  18. #include <windows.h>
  19. #include <ole2.h>
  20. #include <olebind.hxx>
  21. #include <stdio.h>
  22. class CRotTestObject : public IUnknown
  23. {
  24. public:
  25. CRotTestObject(WCHAR *pwszID);
  26. // IUnknown Interface
  27. STDMETHOD(QueryInterface)(REFIID riid, void **ppv);
  28. STDMETHOD_(ULONG, AddRef)(void);
  29. STDMETHOD_(ULONG, Release)(void);
  30. private:
  31. WCHAR _awcID[256];
  32. ULONG _cRefs;
  33. };
  34. CRotTestObject::CRotTestObject(WCHAR *pwszID) : _cRefs(1)
  35. {
  36. wcscpy(_awcID, pwszID);
  37. }
  38. STDMETHODIMP CRotTestObject::QueryInterface(REFIID riid, void **ppv)
  39. {
  40. if (memcmp((void *) &riid, (void *) &IID_IUnknown, sizeof(GUID)) == 0)
  41. {
  42. _cRefs++;
  43. *ppv = (IUnknown *) this;
  44. return S_OK;
  45. }
  46. *ppv = NULL;
  47. return E_NOINTERFACE;
  48. }
  49. STDMETHODIMP_(ULONG) CRotTestObject::AddRef(void)
  50. {
  51. _cRefs++;
  52. return (ULONG) _awcID;
  53. }
  54. STDMETHODIMP_(ULONG) CRotTestObject::Release(void)
  55. {
  56. ULONG cRefs = --_cRefs;
  57. if (cRefs == 0)
  58. {
  59. delete this;
  60. }
  61. return cRefs;
  62. }
  63. //
  64. // Test Running Object Table
  65. //
  66. // BUGBUG: Need to test enumerator
  67. BOOL TestROT(REFCLSID clsid)
  68. {
  69. XUnknown punk2;
  70. XMoniker pmk;
  71. XMoniker pmk2;
  72. XRunningObjectTable prot;
  73. XEnumMoniker penummk;
  74. HRESULT hr = GetRunningObjectTable(0, &prot);
  75. TEST_FAILED_HR(FAILED(hr), "GetRunningObjectTable failed!")
  76. // Make sure that we can do something on the pointer that
  77. // we got back.
  78. prot->AddRef();
  79. prot->Release();
  80. // Create an IUnknown pointer for the class.
  81. IUnknown *punk = new CRotTestObject(L"First Test Object");
  82. hr = CreateItemMoniker(L"\\", wszPid, &pmk2);
  83. TEST_FAILED_HR(FAILED(hr), "CreateItemMoniker for \\Bob failed")
  84. // Do a get object to make sure that this is not in the ROT already
  85. hr = prot->GetObject(pmk2, &punk2);
  86. TEST_FAILED_HR(SUCCEEDED(hr), "GetObject on nonexistent succeeded")
  87. // Cookie for deregistering object
  88. DWORD dwRegister;
  89. hr = prot->Register(0, punk, pmk2, &dwRegister);
  90. TEST_FAILED_HR(FAILED(hr), "Register in ROT for \\PID failed")
  91. hr = prot->IsRunning(pmk2);
  92. TEST_FAILED_HR((hr != S_OK),
  93. "Unexpected return from IsRunning")
  94. // Test Get Object
  95. hr = prot->GetObject(pmk2, &punk2);
  96. TEST_FAILED_HR((hr != S_OK), "Unexpected from GetObject")
  97. // Confirm object identity
  98. WCHAR *pwszID = (WCHAR *) punk2->AddRef();
  99. TEST_FAILED_HR((wcscmp(pwszID, L"First Test Object") != 0),
  100. "GetObject ID is invalid");
  101. // Make sure pointer == original pointer
  102. TEST_FAILED((punk2 != punk), "GetObject Pointers are not equal!")
  103. // Clean up punk2 -- two releases because +1 on return and +1 on
  104. // addref to get id string
  105. punk2->Release();
  106. punk2.Set(NULL);
  107. // Test set the time
  108. FILETIME filetime;
  109. memset(&filetime, 'A', sizeof(filetime));
  110. hr = prot->NoteChangeTime(dwRegister, &filetime);
  111. TEST_FAILED_HR((hr != S_OK), "NoteChangeTime Failed")
  112. // Test get the time
  113. FILETIME filetime2;
  114. hr = prot->GetTimeOfLastChange(pmk2, &filetime2);
  115. TEST_FAILED_HR((hr != S_OK), "NoteChangeTime Failed")
  116. TEST_FAILED((memcmp(&filetime, &filetime2, sizeof(filetime)) != 0),
  117. "GetTimeOfLastChange != NoteChangeTime value")
  118. // Enumerate all the running monikers
  119. hr = prot->EnumRunning(&penummk);
  120. TEST_FAILED_HR(FAILED(hr), "EnumRunning Failed")
  121. // Cycle through running object table
  122. BOOL fFound = FALSE;
  123. int cIdx = 0;
  124. int cOurMoniker;
  125. while (SUCCEEDED(hr = penummk->Next(1, &pmk, NULL))
  126. && (hr != S_FALSE))
  127. {
  128. if (pmk2->IsEqual(pmk) == S_OK)
  129. {
  130. fFound = TRUE;
  131. cOurMoniker = cIdx;
  132. }
  133. pmk.Set(NULL);
  134. cIdx++;
  135. }
  136. TEST_FAILED_HR(FAILED(hr), "ROT Moniker Enumeration ended in failure")
  137. TEST_FAILED((!fFound), "Did not find our moniker in the table");
  138. // Reset the pointer
  139. hr = penummk->Reset();
  140. TEST_FAILED_HR(FAILED(hr), "ROT IEnumMoniker::Reset Failed");
  141. // Skip to our moniker
  142. hr = penummk->Skip(cOurMoniker);
  143. TEST_FAILED_HR(FAILED(hr), "ROT IEnumMoniker::Skip Failed");
  144. // Read it from the enumerator
  145. hr = penummk->Next(1, &pmk, NULL);
  146. TEST_FAILED_HR(FAILED(hr), "ROT IEnumMoniker::Next Failed");
  147. TEST_FAILED((pmk2->IsEqual(pmk) != S_OK),
  148. "ROT IEnumMoniker::Next after skip monikers !=");
  149. pmk.Set(NULL);
  150. // Clean up enumerator
  151. penummk.Set(NULL);
  152. // Test duplicate registration
  153. DWORD dwRegister2;
  154. hr = prot->Register(0, punk, pmk2, &dwRegister2);
  155. TEST_FAILED_HR((hr != MK_S_MONIKERALREADYREGISTERED),
  156. "2nd Register in ROT for \\PID failed")
  157. // Revoke non-existent object
  158. DWORD dwDummy = (DWORD) &dwRegister2;
  159. hr = prot->Revoke(dwDummy);
  160. TEST_FAILED_HR((hr != E_INVALIDARG), "Revoke for bad item wrong result")
  161. // Revoke the object
  162. hr = prot->Revoke(dwRegister);
  163. TEST_FAILED_HR(FAILED(hr), "Revoke of first reg in ROT failed")
  164. // Revoke duplicate registration
  165. hr = prot->Revoke(dwRegister2);
  166. TEST_FAILED_HR(FAILED(hr), "2nd Revoke in ROT failed")
  167. // Make sure it is no longer running
  168. hr = prot->IsRunning(pmk2);
  169. TEST_FAILED_HR((hr != S_FALSE),
  170. "Revoked ROT entry unexpected error")
  171. // If we get to here the test passed
  172. return FALSE;
  173. }