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.

317 lines
8.1 KiB

  1. /******************************************************************************
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. main.cpp
  5. Abstract:
  6. This file contains the unit test for the Security objects.
  7. Revision History:
  8. Davide Massarenti (Dmassare) 03/22/2000
  9. created
  10. ******************************************************************************/
  11. #include "StdAfx.h"
  12. #include <initguid.h>
  13. #include "HelpServiceTypeLib.h"
  14. #include "HelpServiceTypeLib_i.c"
  15. ////////////////////////////////////////////////////////////////////////////////
  16. static HRESULT OpenStreamForRead( /*[in]*/ LPCWSTR szFile ,
  17. /*[out]*/ IStream* *pVal )
  18. {
  19. __HCP_FUNC_ENTRY( "OpenStreamForRead" );
  20. HRESULT hr;
  21. CComPtr<MPC::FileStream> stream;
  22. MPC::wstring szFileFull;
  23. __MPC_PARAMCHECK_BEGIN(hr)
  24. __MPC_PARAMCHECK_STRING_NOT_EMPTY(szFile);
  25. __MPC_PARAMCHECK_POINTER_AND_SET(pVal,NULL);
  26. __MPC_PARAMCHECK_END();
  27. MPC::SubstituteEnvVariables( szFileFull = szFile );
  28. //
  29. // Create a stream for a file.
  30. //
  31. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::CreateInstance( &stream ));
  32. __MPC_EXIT_IF_METHOD_FAILS(hr, stream->InitForRead( szFileFull.c_str() ));
  33. //
  34. // Return the stream to the caller.
  35. //
  36. *pVal = stream.Detach();
  37. hr = S_OK;
  38. __HCP_FUNC_CLEANUP;
  39. __HCP_FUNC_EXIT(hr);
  40. }
  41. static HRESULT OpenStreamForWrite( /*[in]*/ LPCWSTR szFile ,
  42. /*[out]*/ IStream* *pVal )
  43. {
  44. __HCP_FUNC_ENTRY( "OpenStreamForWrite" );
  45. HRESULT hr;
  46. CComPtr<MPC::FileStream> stream;
  47. MPC::wstring szFileFull;
  48. __MPC_PARAMCHECK_BEGIN(hr)
  49. __MPC_PARAMCHECK_STRING_NOT_EMPTY(szFile);
  50. __MPC_PARAMCHECK_POINTER_AND_SET(pVal,NULL);
  51. __MPC_PARAMCHECK_END();
  52. MPC::SubstituteEnvVariables( szFileFull = szFile );
  53. //
  54. // Create a stream for a file.
  55. //
  56. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::CreateInstance( &stream ));
  57. __MPC_EXIT_IF_METHOD_FAILS(hr, stream->InitForWrite( szFileFull.c_str() ));
  58. //
  59. // Return the stream to the caller.
  60. //
  61. *pVal = stream.Detach();
  62. hr = S_OK;
  63. __HCP_FUNC_CLEANUP;
  64. __HCP_FUNC_EXIT(hr);
  65. }
  66. ////////////////////////////////////////////////////////////////////////////////
  67. static HRESULT Creation1()
  68. {
  69. __HCP_FUNC_ENTRY( "Creation1" );
  70. HRESULT hr;
  71. CComPtr<CPCHSecurityDescriptor> pNew;
  72. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::CreateInstance( &pNew ));
  73. hr = S_OK;
  74. __HCP_FUNC_CLEANUP;
  75. __HCP_FUNC_EXIT(hr)
  76. }
  77. static HRESULT Creation2()
  78. {
  79. __HCP_FUNC_ENTRY( "Creation2" );
  80. HRESULT hr;
  81. CComPtr<CPCHAccessControlList> pNew;
  82. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::CreateInstance( &pNew ));
  83. hr = S_OK;
  84. __HCP_FUNC_CLEANUP;
  85. __HCP_FUNC_EXIT(hr)
  86. }
  87. static HRESULT Creation3()
  88. {
  89. __HCP_FUNC_ENTRY( "Creation3" );
  90. HRESULT hr;
  91. CComPtr<CPCHAccessControlEntry> pNew;
  92. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::CreateInstance( &pNew ));
  93. hr = S_OK;
  94. __HCP_FUNC_CLEANUP;
  95. __HCP_FUNC_EXIT(hr)
  96. }
  97. ////////////////////////////////////////////////////////////////////////////////
  98. static HRESULT LoadAndSave()
  99. {
  100. __HCP_FUNC_ENTRY( "LoadAndSave" );
  101. HRESULT hr;
  102. CComPtr<CPCHSecurityDescriptor> pNew;
  103. CComPtr<IStream> pStream;
  104. CComBSTR bstrVal;
  105. __MPC_EXIT_IF_METHOD_FAILS(hr, OpenStreamForRead( L"%TEMP%\\test1.xml", &pStream ));
  106. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::CreateInstance( &pNew ));
  107. __MPC_EXIT_IF_METHOD_FAILS(hr, pNew->LoadXMLAsStream( pStream ));
  108. __MPC_EXIT_IF_METHOD_FAILS(hr, pNew->SaveXMLAsString( &bstrVal ));
  109. hr = S_OK;
  110. __HCP_FUNC_CLEANUP;
  111. __HCP_FUNC_EXIT(hr)
  112. }
  113. ////////////////////////////////////////////////////////////////////////////////
  114. static HRESULT FileToCOM()
  115. {
  116. __HCP_FUNC_ENTRY( "FileToCOM" );
  117. HRESULT hr;
  118. CPCHSecurityDescriptorDirect sd;
  119. CComPtr<CPCHSecurityDescriptor> pNew;
  120. MPC::wstring szFileFull( L"%TEMP%\\sdtest.xml" ); MPC::SubstituteEnvVariables( szFileFull );
  121. HANDLE hFile = INVALID_HANDLE_VALUE;
  122. CComPtr<IStream> pStreamIn;
  123. CComPtr<IStream> pStreamOut;
  124. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::CreateInstance( &pNew ));
  125. __MPC_EXIT_IF_INVALID_HANDLE(hr, hFile, ::CreateFileW( szFileFull.c_str(), GENERIC_ALL, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ));
  126. __MPC_EXIT_IF_METHOD_FAILS(hr, sd.AttachObject( hFile ));
  127. __MPC_EXIT_IF_METHOD_FAILS(hr, sd.ConvertSDToCOM( pNew ));
  128. __MPC_EXIT_IF_METHOD_FAILS(hr, pNew->SaveXMLAsStream( (IUnknown**)&pStreamIn ));
  129. __MPC_EXIT_IF_METHOD_FAILS(hr, OpenStreamForWrite ( L"%TEMP%\\sddump.xml", &pStreamOut ));
  130. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::BaseStream::TransferData( pStreamIn, pStreamOut ));
  131. hr = S_OK;
  132. __HCP_FUNC_CLEANUP;
  133. if(hFile != INVALID_HANDLE_VALUE) ::CloseHandle( hFile );
  134. __HCP_FUNC_EXIT(hr)
  135. }
  136. ////////////////////////////////////////////////////////////////////////////////
  137. static HRESULT COMToFile()
  138. {
  139. __HCP_FUNC_ENTRY( "COMToFile" );
  140. HRESULT hr;
  141. CPCHSecurityDescriptorDirect sd;
  142. MPC::wstring szFileFull( L"%TEMP%\\sdtest2.xml" ); MPC::SubstituteEnvVariables( szFileFull );
  143. HANDLE hFile = INVALID_HANDLE_VALUE;
  144. {
  145. CComPtr<CPCHSecurityDescriptor> pNew;
  146. CComPtr<IStream> pStream;
  147. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::CreateInstance( &pNew ));
  148. __MPC_EXIT_IF_METHOD_FAILS(hr, OpenStreamForRead ( L"%TEMP%\\sddump_pre.xml", &pStream ));
  149. __MPC_EXIT_IF_METHOD_FAILS(hr, pNew->LoadXMLAsStream( (IUnknown*) pStream ));
  150. __MPC_EXIT_IF_METHOD_FAILS(hr, sd.ConvertSDFromCOM( pNew ));
  151. __MPC_EXIT_IF_INVALID_HANDLE(hr, hFile, ::CreateFileW( szFileFull.c_str(), GENERIC_ALL, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ));
  152. }
  153. {
  154. CComPtr<CPCHSecurityDescriptor> pNew;
  155. CComPtr<IStream> pStreamIn;
  156. CComPtr<IStream> pStreamOut;
  157. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::CreateInstance( &pNew ));
  158. __MPC_EXIT_IF_METHOD_FAILS(hr, sd.ConvertSDToCOM( pNew ));
  159. __MPC_EXIT_IF_METHOD_FAILS(hr, pNew->SaveXMLAsStream( (IUnknown**)&pStreamIn ));
  160. __MPC_EXIT_IF_METHOD_FAILS(hr, OpenStreamForWrite ( L"%TEMP%\\sddump_post.xml", &pStreamOut ));
  161. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::BaseStream::TransferData( pStreamIn, pStreamOut ));
  162. }
  163. __MPC_EXIT_IF_CALL_RETURNS_FALSE(hr, ::SetKernelObjectSecurity( hFile, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION, sd.GetSD() ));
  164. hr = S_OK;
  165. __HCP_FUNC_CLEANUP;
  166. if(hFile != INVALID_HANDLE_VALUE) ::CloseHandle( hFile );
  167. __HCP_FUNC_EXIT(hr)
  168. }
  169. ////////////////////////////////////////////////////////////////////////////////
  170. static HRESULT RunTests( int argc, WCHAR **argv )
  171. {
  172. __HCP_FUNC_ENTRY( "RunTests" );
  173. HRESULT hr;
  174. __MPC_EXIT_IF_METHOD_FAILS(hr, Creation1());
  175. __MPC_EXIT_IF_METHOD_FAILS(hr, Creation2());
  176. __MPC_EXIT_IF_METHOD_FAILS(hr, Creation3());
  177. __MPC_EXIT_IF_METHOD_FAILS(hr, LoadAndSave());
  178. __MPC_EXIT_IF_METHOD_FAILS(hr, FileToCOM());
  179. __MPC_EXIT_IF_METHOD_FAILS(hr, COMToFile());
  180. hr = S_OK;
  181. __HCP_FUNC_CLEANUP;
  182. __HCP_FUNC_EXIT(hr);
  183. }
  184. int __cdecl wmain( int argc, WCHAR **argv, WCHAR **envp)
  185. {
  186. HRESULT hr;
  187. if(SUCCEEDED(hr = ::CoInitializeEx( NULL, COINIT_MULTITHREADED )))
  188. {
  189. hr = RunTests( argc, argv );
  190. ::CoUninitialize();
  191. }
  192. return FAILED(hr) ? 10 : 0;
  193. }