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.

459 lines
15 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1998.
  5. //
  6. // File: tstparam.hxx
  7. //
  8. // Synopsys: Test paramers container class definitions
  9. //
  10. // Classes: CTestParams, CParamNode
  11. //
  12. // Notes: The test parameter containers provide a layer of abstraction
  13. // above the particular ways to pass test parameters -
  14. // (command line, environment, registry...)
  15. //
  16. // History: 10-Sep-1998 georgis created
  17. //
  18. //---------------------------------------------------------------------------
  19. #ifndef __TSTPARAM_HXX__
  20. #define __TSTPARAM_HXX__
  21. // Maximum registry string value length
  22. #define MAX_REGSTRLEN _MAX_PATH
  23. //+--------------------------------------------------------------------------
  24. //
  25. // Macros: GETPARAM
  26. //
  27. // Synopsys: Get parameter from the default container
  28. //
  29. // Parameters: param: the parameter definition string
  30. // var: the variable which receives the result
  31. //
  32. // Notes: The parameter definition string must be in format "name:format"
  33. // where name is the name which identifies the parameter and
  34. // format shows how to read this parameter.
  35. // E.g. "my_int:%i"
  36. //
  37. // Formats may be of two types:
  38. // 1) Any standard sscanf formats starting with %
  39. // E.g. %i %u %d %lx %s ...
  40. //
  41. // 2) Custom formats
  42. // bool - read BOOL (*pTarget is BOOL)
  43. // cstr - read constant string (*pTarget is const char*)
  44. // astr - heap allocated ascii string (*pTarget is char*)
  45. // tstr - heap allocated TCHAR string (*pTarget is LPTSTR)
  46. // olestr - heap allocated OLESTR string (*pTarget is LPOLESTR)
  47. //
  48. // For the heap allocated formats the string obtained is writable,
  49. // and the caller is responsible for deleting it.
  50. //
  51. // History: 02-Oct-1998 georgis Created
  52. //
  53. //+--------------------------------------------------------------------------
  54. #define GETPARAM(param,var) \
  55. g_TestParams.GetParam(param,&var)
  56. //+--------------------------------------------------------------------------
  57. //
  58. // Macros: GETPARAM_ABORTONERROR
  59. //
  60. // Synopsys: Get parameter from the default container
  61. // and abort on error
  62. //
  63. // Parameters: param: the parameter definition string
  64. // var: the variable which receives the result
  65. //
  66. // History: 20-Oct-1998 georgis Created
  67. //
  68. //+--------------------------------------------------------------------------
  69. #define GETPARAM_ABORTONERROR(param,var) \
  70. hr=g_TestParams.GetParam(param,&var); \
  71. if (S_FALSE!=hr) \
  72. { \
  73. DH_HRCHECK_ABORT(hr,TEXT(param)); \
  74. } \
  75. hr=S_OK;
  76. //+--------------------------------------------------------------------------
  77. //
  78. // Macros: GETPARAM_DEFINE
  79. //
  80. // Synopsys: Expands to definition of local variable, and
  81. // initialization by the parameter value or the default
  82. //
  83. // Parameters: type: the variable type
  84. // var: the variable which receives the result
  85. // param: the parameter definition string
  86. // defaultval: the defauilt value
  87. //
  88. // History: 02-Oct-1998 georgis Created
  89. //
  90. //+--------------------------------------------------------------------------
  91. #define GETPARAM_DEFINE(type,var,param,defaultval) \
  92. type var=defaultval; \
  93. g_TestParams.GetParam(param,&var)
  94. //+--------------------------------------------------------------------------
  95. //
  96. // Macros: GETPARAM_REQUIRED
  97. //
  98. // Synopsys: As GETPARAM, but aborts the current function on failure to
  99. // obtain the parameter (see DH_*ABORT macros)
  100. //
  101. // Parameters: param: the parameter definition string
  102. // var: the variable which receives the result
  103. //
  104. // History: 02-Oct-1998 georgis Created
  105. //
  106. //+--------------------------------------------------------------------------
  107. #define GETPARAM_REQUIRED(param,var) \
  108. hr=g_TestParams.GetParam(param,&var); \
  109. DH_HRCHECK_ABORT(hr,TEXT(param));
  110. //+--------------------------------------------------------------------------
  111. //
  112. // Macros: GETPARAM_ENUM
  113. //
  114. // Synopsys: Gets the value of a parameter as enum (DWORD)
  115. //
  116. // Example: GETPARAM_ENUM(
  117. // "mode",
  118. // &dwMode,
  119. // "rw", STGM_READWRITE,
  120. // "r", STGM_READ,
  121. // "w", STGM_WRITE,
  122. // NULL);
  123. //
  124. // if e.g. there is command line switch /mode:r
  125. // the dwMode will be set to STGM_READ
  126. //
  127. // History: 02-Oct-1998 georgis Created
  128. //
  129. //+--------------------------------------------------------------------------
  130. #define GETPARAM_ENUM g_TestParams.GetEnum
  131. //+--------------------------------------------------------------------------
  132. //
  133. // Macros: GETPARAM_RANGE
  134. //
  135. // Synopsys: Gets the value of a parameter as range (min, max)
  136. //
  137. // Example: GETPARAM_RANGE("streamsize",ullMin,ullMax);
  138. //
  139. // Notes: The min and max can be ULONG or ULONGLONG
  140. //
  141. // History: 02-Oct-1998 georgis Created
  142. //
  143. //+--------------------------------------------------------------------------
  144. #define GETPARAM_RANGE(param,min,max) \
  145. g_TestParams.GetRange(param,&min,&max);
  146. //+--------------------------------------------------------------------------
  147. //
  148. // Macros: GETPARAM_ISPRESENT
  149. //
  150. // Synopsys: Returns TRUE is the parameter is present in
  151. // the default container
  152. //
  153. // Parameters: param: the parameter definition string
  154. //
  155. // History: 02-Oct-1998 georgis Created
  156. //
  157. //+--------------------------------------------------------------------------
  158. #define GETPARAM_ISPRESENT(param) \
  159. g_TestParams.IsPresent(param)
  160. //+--------------------------------------------------------------------------
  161. //
  162. // Macros: SETPARAM
  163. //
  164. // Synopsys: Set param in the global container (see SetParam method)
  165. //
  166. // History: 02-Oct-1998 georgis Created
  167. //
  168. //+--------------------------------------------------------------------------
  169. #define SETPARAM g_TestParams.SetParam
  170. // Custom parameter types:
  171. #define PARAMFMT_BOOL L"bool"
  172. #define PARAMFMT_CSTR L"cstr"
  173. #define PARAMFMT_ASTR L"astr"
  174. #define PARAMFMT_TSTR L"tstr"
  175. #define PARAMFMT_OLESTR L"olestr"
  176. // Flags: Each parameter in CTestParams have name,value and flags (DWORD)
  177. #define PARAMMASK_PRIORITY 0x000000ff // The priority of the parameter
  178. #define PARAMMASK_SOURCE 0x0000ff00 // From where this param was read
  179. #define PARAMMASK_USAGE 0x00ff0000 // Who used this parameter
  180. #define PARAMMASK_OPTIONS 0xff000000 // Misc flags
  181. #define PARAMMASK_ALL 0xffffffff
  182. // Default priorities
  183. #define PARAMPRIORITY_RUNTIME 0x00000080 // Set using SetParam
  184. #define PARAMPRIORITY_CMDLINE 0x00000040
  185. #define PARAMPRIORITY_ENVIRON 0x00000020
  186. #define PARAMPRIORITY_REGISTRY 0x00000010
  187. // Basic parameter sources
  188. #define PARAMSOURCE_RUNTIME 0x00008000
  189. #define PARAMSOURCE_CMDLINE 0x00004000
  190. #define PARAMSOURCE_ENVIRON 0x00002000
  191. #define PARAMSOURCE_REGISTRY 0x00001000
  192. #define PARAMSOURCE_ANY PARAMMASK_SOURCE
  193. // Parameter usage markers (Who used the parameter)
  194. #define PARAMUSAGE_GENERAL 0x00010000 // During initialization
  195. #define PARAMUSAGE_TESTCASE 0x00020000 // the current testcase
  196. #define PARAMUSAGE_DFLREPRO 0x000f0000 // what to include in repro by default
  197. #define PARAMUSAGE_TESTDRIVER 0x00100000 // exclude from single testcase repro
  198. #define PARAMUSAGE_SELECTOR_G 0x00200000 // reserved for global CSelector object
  199. #define PARAMUSAGE_SELECTOR_L 0x00400000 // reserved for local CSelector objects
  200. // General options
  201. #define PARAMFLAG_MUSTSET 0x01000000 // fail if can't override param
  202. #define PARAMFLAG_OVERRIDEUSED 0x02000000 // override even if used
  203. #define PARAMFLAG_REPROINFO 0x04000000 // e.g. "seed:%x"
  204. // Sources masks summary
  205. #define PARAMFLAGS_CMDLINE PARAMSOURCE_CMDLINE | PARAMPRIORITY_CMDLINE
  206. #define PARAMFLAGS_ENVIRON PARAMSOURCE_ENVIRON | PARAMPRIORITY_ENVIRON
  207. #define PARAMFLAGS_REGISTRY PARAMSOURCE_REGISTRY | PARAMPRIORITY_REGISTRY
  208. #define PARAMFLAGS_RUNTIME PARAMSOURCE_RUNTIME | PARAMPRIORITY_RUNTIME
  209. // Parameters which may be added at runtime (as seed).
  210. // They are added from the begining as "used", in the curent scope
  211. // and are deleted when the usage is cleared
  212. #define PARAMFLAGS_REPROINFO PARAMFLAG_REPROINFO|PARAMFLAG_OVERRIDEUSED| \
  213. PARAMFLAG_MUSTSET|PARAMFLAGS_RUNTIME
  214. // Forward definition of the internal class
  215. class CParamNode;
  216. //+--------------------------------------------------------------------------
  217. //
  218. // Class: CTestParams
  219. //
  220. // Synopsys: Test parameter container class.
  221. //
  222. // Purpose: To provide a level of abstraction above the specific ways
  223. // for passing the test parameters (command line, environment,
  224. // registry keys ...).
  225. //
  226. // History: 09-Sep-1998 georgis Created
  227. //
  228. //---------------------------------------------------------------------------
  229. class CTestParams
  230. {
  231. public:
  232. // Constructor and Destructor
  233. CTestParams();
  234. virtual ~CTestParams();
  235. // Info function TRUE if the container is empty
  236. inline HRESULT IsEmpty()
  237. {return NULL==m_pParamsList;};
  238. // Functions for extracting / adding parameters
  239. HRESULT GetParam(
  240. const char *pszName,
  241. void* pTarget);
  242. HRESULT SetParam(
  243. const char *pszName,
  244. void* pTarget,
  245. DWORD dwFlags=PARAMFLAGS_RUNTIME);
  246. BOOL __cdecl IsPresent(
  247. const char *pszName ,...);
  248. HRESULT DeleteParam(
  249. const char *ptszName);
  250. HRESULT __cdecl GetEnum(
  251. const char *pszParamName,
  252. DWORD *pdwValue,...);
  253. HRESULT GetRange(
  254. const char *pszParamName,
  255. ULONGLONG *pullMin,
  256. ULONGLONG *pullMax);
  257. HRESULT GetRange(
  258. const char *pszParamName,
  259. ULONG *pulMin,
  260. ULONG *pulMax);
  261. // Functions for handling flags
  262. void SetUsage(
  263. DWORD dwFlags)
  264. {m_dwUsageInfo=dwFlags;};
  265. void ClearUsage(
  266. DWORD dwFlags);
  267. HRESULT ChangeFlags(
  268. DWORD dwOldFlags,
  269. DWORD dwNewFlags);
  270. // Functions to read from some parameter sources
  271. HRESULT ReadCommandLine(
  272. LPCTSTR ptszCtommandLine=NULL,
  273. DWORD dwFlags=PARAMFLAGS_CMDLINE);
  274. HRESULT ReadEnvironment(
  275. LPCWSTR pszPrefix=NULL,
  276. DWORD dwFlags=PARAMFLAGS_ENVIRON);
  277. HRESULT ReadRegistry(
  278. HKEY hBaseKey,
  279. LPCTSTR ptszKeyName,
  280. DWORD dwFlags=PARAMFLAGS_REGISTRY);
  281. // Handle the parameter set as whole
  282. HRESULT SaveParams(
  283. char **ppcBuffer,
  284. DWORD *pdwSize,
  285. DWORD dwMask=PARAMSOURCE_ANY);
  286. HRESULT LoadParams(
  287. char *pcBuffer,
  288. DWORD dwSize,
  289. DWORD dwChangeFlags=0);
  290. HRESULT GetReproLine(LPWSTR *ppwszReproLine, DWORD dwFlags);
  291. HRESULT GetReproLine(char **ppszReproLine, DWORD dwFlags);
  292. protected:
  293. virtual HRESULT NotifyOnFirstUse(){return S_OK;}; // use when inherit
  294. private:
  295. // Custom format params
  296. HRESULT SetCustomFmtParam(
  297. LPCWSTR pszFormat,
  298. LPCWSTR pszValue,
  299. void* pTarget,
  300. DWORD dwFlags);
  301. HRESULT GetCustomFmtParam(
  302. LPCWSTR pszName,
  303. LPCWSTR pszFormat,
  304. void* pTarget);
  305. CParamNode* FindParam(
  306. LPCWSTR Name,
  307. CParamNode** ppPrev);
  308. HRESULT AddParam(
  309. LPCWSTR pwszName,
  310. LPCWSTR pwszValue,
  311. DWORD dwFlags,
  312. BOOL bWasQuoted=FALSE);
  313. CParamNode *m_pParamsList;
  314. DWORD m_dwUsageInfo;
  315. BOOL m_bUsed;
  316. CRITICAL_SECTION m_sync;
  317. };
  318. //+--------------------------------------------------------------------------
  319. //
  320. // Class: CParamNode (internaly used in CTestParams)
  321. //
  322. // Synopsys: Single linked list node for one parameter info
  323. //
  324. // History: 29-Sep-1998 georgis Created
  325. //
  326. //---------------------------------------------------------------------------
  327. class CParamNode
  328. {
  329. friend class CTestParams;
  330. DWORD m_dwHashValue;
  331. DWORD m_dwFlags; // source,priority,usage
  332. LPWSTR m_pwszName;
  333. LPWSTR m_pwszValue;
  334. CParamNode *m_pNext;
  335. CParamNode(DWORD dwFlags);
  336. ~CParamNode();
  337. HRESULT Init(
  338. LPCWSTR pwszName,
  339. LPCWSTR pwszValue);
  340. HRESULT ChangeValue(
  341. LPCWSTR pwszValue,
  342. DWORD dwFlags);
  343. inline void MarkAsUsed(DWORD dwFlags) // Mark the param as used
  344. {m_dwFlags|=PARAMMASK_USAGE & dwFlags;};
  345. inline void ClearUsage(DWORD dwFlags) // Clear given usage flags
  346. {m_dwFlags &=~(PARAMMASK_USAGE & dwFlags);};
  347. inline void ChangeFlags(DWORD dwFlags) // Change all except usage
  348. {m_dwFlags = (dwFlags & ~PARAMMASK_USAGE)|(PARAMMASK_USAGE & m_dwFlags);};
  349. };
  350. // Utilities functions for converting Unicode strings to/from escaped ascii
  351. #define ESCAPE_CHARACTER '#'
  352. #define ESCAPED_MARKER ':'
  353. #define ESCAPE_NOTNEEDED(x)\
  354. ((' '<=x)&&('~'>=x))
  355. #define ESCAPEMODE_PREFIX_ALWAYS 0
  356. #define ESCAPEMODE_PREFIX_NEVER 1
  357. #define ESCAPEMODE_PREFIX_IFCHANGED 2
  358. #define ESCAPE_MODE_DEFAULT ESCAPEMODE_PREFIX_ALWAYS
  359. HRESULT Unicode2Escaped(
  360. LPCWSTR pwszString,
  361. char ** ppszString,
  362. DWORD dwEscapeMode=ESCAPE_MODE_DEFAULT);
  363. HRESULT Unicode2Escaped(
  364. LPCWSTR pwszString,
  365. LPWSTR* ppwszString,
  366. DWORD dwEscapeMode=ESCAPE_MODE_DEFAULT);
  367. HRESULT Escaped2Unicode(
  368. const char * pszEscaped,
  369. LPWSTR *ppwszString,
  370. DWORD dwEscapeMode=ESCAPE_MODE_DEFAULT);
  371. HRESULT Escaped2Unicode(
  372. LPCWSTR pszEscaped,
  373. LPWSTR *ppwszString,
  374. DWORD dwEscapeMode=ESCAPE_MODE_DEFAULT);
  375. // CTOLESTG specific
  376. // parameters to affect the param handling itself
  377. #define PARAMDUMP "paramdump:%lx" // specify to dump on the first Get*
  378. #define STG_CMDLINEONLY "cmdonly:bool" // read only from command line
  379. // The only way to start using CTestParams in all the exisating suites
  380. // without modifications in to read parameters in the object's constructor
  381. class CStgParams : public CTestParams
  382. {
  383. protected:
  384. virtual HRESULT NotifyOnFirstUse();
  385. };
  386. extern CStgParams g_TestParams;
  387. #endif __TSTPARAM_HXX__