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.

341 lines
8.2 KiB

  1. #include <windows.h>
  2. #include <wmimsg.h>
  3. #include <comutl.h>
  4. #include <rcvtest.h>
  5. #include <rcvtest_i.c>
  6. #include <stdio.h>
  7. FILE* g_pfLogFile;
  8. class CReceiveTest : public IReceiveTest, public IClassFactory
  9. {
  10. public:
  11. STDMETHOD_(ULONG,AddRef)() { return 1; }
  12. STDMETHOD_(ULONG,Release)() { return 1; }
  13. STDMETHOD(QueryInterface)( REFIID riid , void** ppv )
  14. {
  15. if ( riid == IID_IUnknown || riid == IID_IReceiveTest )
  16. {
  17. *ppv = (IReceiveTest*)this;
  18. }
  19. else if ( riid == IID_IClassFactory )
  20. {
  21. *ppv = (IClassFactory*)this;
  22. }
  23. else
  24. {
  25. return E_NOINTERFACE;
  26. }
  27. return S_OK;
  28. }
  29. STDMETHOD(CreateInstance)( IUnknown* pOuter, REFIID riid, void** ppv )
  30. {
  31. return QueryInterface( riid, ppv );
  32. }
  33. STDMETHOD(LockServer) (BOOL bLock ) { return S_OK; }
  34. STDMETHOD(RunTest)( LPCWSTR wszTarget,
  35. DWORD dwFlags,
  36. LPCWSTR wszPrincipal,
  37. ULONG cMsgs,
  38. ULONG* pultime );
  39. STDMETHOD(Kill)()
  40. {
  41. PostThreadMessage( GetCurrentThreadId(), WM_QUIT, 0, 0 );
  42. return S_OK;
  43. }
  44. };
  45. struct CTestMsgHandler : public IWmiMessageTraceSink,
  46. public IWmiMessageSendReceive
  47. {
  48. HRESULT m_hr;
  49. HANDLE m_hEvent;
  50. long m_cCurrentMsgs;
  51. long m_cExpectedMsgs;
  52. SYSTEMTIME m_Start;
  53. CTestMsgHandler( long cExpectedMsgs, HANDLE hEvent )
  54. : m_cExpectedMsgs( cExpectedMsgs ), m_cCurrentMsgs(0),
  55. m_hEvent( hEvent ), m_hr( S_OK )
  56. {
  57. }
  58. ~CTestMsgHandler()
  59. {
  60. CloseHandle( m_hEvent );
  61. }
  62. HRESULT GetResult() { return m_hr; }
  63. STDMETHOD_(ULONG,AddRef)() { return 1; }
  64. STDMETHOD_(ULONG,Release)() { return 1; }
  65. STDMETHOD(QueryInterface)( REFIID riid , void** ppv )
  66. {
  67. if ( riid == IID_IWmiMessageTraceSink ||
  68. riid == IID_IUnknown )
  69. {
  70. *ppv = (IWmiMessageTraceSink*)this;
  71. }
  72. else if ( riid == IID_IWmiMessageSendReceive )
  73. {
  74. *ppv = (IWmiMessageSendReceive*)this;
  75. }
  76. if ( *ppv == NULL )
  77. {
  78. return E_NOINTERFACE;
  79. }
  80. return S_OK;
  81. }
  82. void LogMessage( PBYTE pMsg,
  83. ULONG cMsg,
  84. PBYTE pAuxData,
  85. ULONG cAuxData,
  86. DWORD dwFlagStatus,
  87. IUnknown* pCtx )
  88. {
  89. if ( g_pfLogFile == NULL )
  90. {
  91. return;
  92. }
  93. IWmiMessageReceiverContext* pRecvCtx;
  94. pCtx->QueryInterface( IID_IWmiMessageReceiverContext,
  95. (void**)&pRecvCtx );
  96. SYSTEMTIME st;
  97. WCHAR awchTime[64];
  98. pRecvCtx->GetTimeSent( &st );
  99. swprintf( awchTime, L"%d:%d:%d:%d", st.wHour,
  100. st.wMinute,
  101. st.wSecond,
  102. st.wMilliseconds );
  103. WCHAR awchTarget[256];
  104. ULONG cTarget;
  105. pRecvCtx->GetTarget( awchTarget, 256, &cTarget );
  106. WCHAR awchSource[256];
  107. ULONG cSource;
  108. pRecvCtx->GetSendingMachine( awchSource, 256, &cSource );
  109. WCHAR awchSenderId[256];
  110. ULONG cSenderId;
  111. pRecvCtx->GetSenderId( awchSenderId, 256, &cSenderId );
  112. awchSource[cSource] = '\0';
  113. awchSenderId[cSenderId] = '\0';
  114. awchTarget[cTarget] = '\0';
  115. BOOL bAuth;
  116. bAuth = pRecvCtx->IsSenderAuthenticated() == S_OK ? TRUE : FALSE;
  117. fwprintf( g_pfLogFile, L"MSG - #%d, Len:%d, AuxLen:%d, Status:%d, "
  118. L"Time:%s, Source:%s, Target:%s, SenderId:%s, Auth:%d\n",
  119. m_cCurrentMsgs, cMsg, cAuxData, dwFlagStatus,
  120. awchTime, awchSource, awchTarget, awchSenderId, bAuth );
  121. fflush( g_pfLogFile );
  122. }
  123. STDMETHOD(Notify)( HRESULT hRes,
  124. GUID guidSource,
  125. LPCWSTR wszError,
  126. IUnknown* pCtx )
  127. {
  128. if ( g_pfLogFile != NULL )
  129. {
  130. fwprintf( g_pfLogFile, L"Notify : HR=0x%x, ErrorStr : %s\n",
  131. hRes, wszError );
  132. }
  133. if ( SUCCEEDED(m_hr) )
  134. {
  135. m_hr = hRes;
  136. SetEvent( m_hEvent );
  137. }
  138. return S_OK;
  139. }
  140. STDMETHOD(SendReceive)( PBYTE pMsg,
  141. ULONG cMsg,
  142. PBYTE pAuxData,
  143. ULONG cAuxData,
  144. DWORD dwFlagStatus,
  145. IUnknown* pCtx )
  146. {
  147. LogMessage( pMsg, cMsg, pAuxData, cAuxData, dwFlagStatus, pCtx );
  148. long cCurrentMsgs = InterlockedIncrement( &m_cCurrentMsgs );
  149. if ( cCurrentMsgs == 1 )
  150. {
  151. GetSystemTime( &m_Start );
  152. }
  153. if ( cCurrentMsgs >= m_cExpectedMsgs )
  154. {
  155. SetEvent( m_hEvent );
  156. }
  157. return S_OK;
  158. }
  159. };
  160. static HANDLE g_hShutdown;
  161. HRESULT CReceiveTest::RunTest( LPCWSTR wszTarget,
  162. DWORD dwFlags,
  163. LPCWSTR wszPrincipal,
  164. ULONG cMsgs,
  165. ULONG* pulTime )
  166. {
  167. HRESULT hr;
  168. CLSID clsidReceiver;
  169. *pulTime = 0;
  170. if ( ( dwFlags & WMIMSG_MASK_QOS) != WMIMSG_FLAG_QOS_SYNCHRONOUS )
  171. {
  172. clsidReceiver = CLSID_WmiMessageMsmqReceiver;
  173. }
  174. else
  175. {
  176. clsidReceiver = CLSID_WmiMessageRpcReceiver;
  177. }
  178. CWbemPtr<IWmiMessageReceiver> pReceiver;
  179. hr = CoCreateInstance( clsidReceiver,
  180. NULL,
  181. CLSCTX_INPROC,
  182. IID_IWmiMessageReceiver,
  183. (void**)&pReceiver );
  184. if ( FAILED(hr) )
  185. {
  186. return hr;
  187. }
  188. HANDLE hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
  189. CTestMsgHandler TestHndlr = CTestMsgHandler( cMsgs, hEvent );
  190. WMIMSG_RCVR_AUTH_INFO AuthInfo;
  191. if ( wszPrincipal != NULL )
  192. {
  193. AuthInfo.awszPrincipal = &wszPrincipal;
  194. AuthInfo.cwszPrincipal = 1;
  195. hr = pReceiver->Open( wszTarget, dwFlags, &AuthInfo, &TestHndlr );
  196. }
  197. else
  198. {
  199. hr = pReceiver->Open( wszTarget, dwFlags, NULL, &TestHndlr );
  200. }
  201. if ( FAILED(hr) )
  202. {
  203. return hr;
  204. }
  205. SYSTEMTIME Start, End;
  206. WaitForSingleObject( hEvent, INFINITE );
  207. if ( FAILED( TestHndlr.m_hr ) )
  208. {
  209. return TestHndlr.m_hr;
  210. }
  211. Start = TestHndlr.m_Start;
  212. GetSystemTime( &End );
  213. __int64 i64Start, i64End;
  214. DWORD dwElapsed;
  215. SystemTimeToFileTime( &Start, PFILETIME(&i64Start) );
  216. SystemTimeToFileTime( &End, PFILETIME(&i64End) );
  217. dwElapsed = DWORD(i64End - i64Start) / 10000;
  218. *pulTime = dwElapsed;
  219. return S_OK;
  220. };
  221. extern "C" int __cdecl main( int argc, char* argv[] )
  222. {
  223. if ( argc > 1 )
  224. {
  225. LPCSTR szLogFile = argv[1];
  226. g_pfLogFile = fopen( szLogFile, "w" );
  227. if ( g_pfLogFile == NULL )
  228. {
  229. printf( "Could not open LogFile %s", szLogFile );
  230. return 1;
  231. }
  232. }
  233. CoInitializeEx( NULL, COINIT_MULTITHREADED );
  234. CoInitializeSecurity( NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_CONNECT,
  235. RPC_C_IMP_LEVEL_IDENTIFY, NULL, EOAC_NONE, NULL );
  236. g_hShutdown = CreateEvent( NULL, TRUE, FALSE, NULL );
  237. CReceiveTest RcvTest;
  238. HRESULT hr;
  239. DWORD dwReg;
  240. hr = CoRegisterClassObject( CLSID_ReceiveTest,
  241. (IClassFactory*)&RcvTest,
  242. CLSCTX_LOCAL_SERVER,
  243. REGCLS_MULTIPLEUSE,
  244. &dwReg );
  245. if ( FAILED(hr) )
  246. {
  247. return 1;
  248. }
  249. WaitForSingleObject( g_hShutdown, INFINITE );
  250. if ( g_pfLogFile != NULL )
  251. {
  252. fclose( g_pfLogFile );
  253. }
  254. CoRevokeClassObject( dwReg );
  255. CoUninitialize();
  256. return 0;
  257. }