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.

314 lines
8.2 KiB

  1. /*++
  2. Copyright Microsoft Corporation. All rights reserved.
  3. Module Name:
  4. test_par.cpp
  5. Abstract:
  6. Test program for CObjectPathParser objects
  7. History:
  8. --*/
  9. #include "precomp.h"
  10. #include "genlex.h"
  11. #include "objpath.h"
  12. BOOL bVerbose = FALSE;
  13. void fatal(int n)
  14. {
  15. printf("\n*** Test failed on source line %d ***\n", n);
  16. exit(1);
  17. }
  18. void DisplayVariant(VARIANT * pvar)
  19. {
  20. SCODE sc;
  21. VARTYPE vSave;
  22. VARTYPE vtSimple = pvar->vt & ~VT_ARRAY & ~VT_BYREF;
  23. VARIANT vTemp;
  24. if(pvar->vt == VT_NULL)
  25. {
  26. printf(" data is NULL");
  27. return;
  28. }
  29. // keep in mind that our bstr are acutally WCHAR * in this context.
  30. if(vtSimple == VT_BSTR)
  31. {
  32. printf(" Type is 0x%x, value is %S", pvar->vt, pvar->bstrVal);
  33. return;
  34. }
  35. VariantInit(&vTemp);
  36. vSave = pvar->vt;
  37. pvar->vt = vtSimple;
  38. sc = VariantChangeTypeEx(&vTemp, pvar,0,0, VT_BSTR);
  39. pvar->vt = vSave;
  40. if(sc == S_OK)
  41. {
  42. printf(" Type is 0x%x, value is %S", pvar->vt, vTemp.bstrVal);
  43. }
  44. else
  45. printf(" Couldnt convert type 0x%x, error code 0x%x", pvar->vt, sc);
  46. VariantClear(&vTemp);
  47. }
  48. void DumpIt(WCHAR * pTest, ParsedObjectPath * pOutput)
  49. {
  50. DWORD dwCnt;
  51. if(!bVerbose)
  52. return;
  53. printf("\n\nTesting -%S-", pTest);
  54. if(pOutput == NULL)
  55. return;
  56. printf("\nClass is, %S, Singleton is %d", pOutput->m_pClass, pOutput->m_bSingletonObj);
  57. printf("\nNumber of keys is %d", pOutput->m_dwNumKeys);
  58. for(dwCnt = 0; dwCnt < pOutput->m_dwNumKeys; dwCnt++)
  59. {
  60. printf(" -%S-", pOutput->m_paKeys[dwCnt]->m_pName);
  61. DisplayVariant((&pOutput->m_paKeys[dwCnt]->m_vValue));
  62. }
  63. printf("\nNumber of namespaces is %d", pOutput->m_dwNumNamespaces);
  64. for(dwCnt = 0; dwCnt < pOutput->m_dwNumNamespaces; dwCnt++)
  65. printf(" -%S-", pOutput->m_paNamespaces[dwCnt]);
  66. }
  67. // this tests a normal single key path
  68. void test1()
  69. {
  70. int iRet;
  71. ParsedObjectPath * pOutput;
  72. WCHAR * pTest = L"\\\\.\\root\\default:MyClass=\"a\"";
  73. WCHAR * pRet = NULL;
  74. CObjectPathParser p;
  75. iRet = p.Parse(pTest, &pOutput);
  76. if(iRet != CObjectPathParser::NoError)
  77. fatal(__LINE__);
  78. DumpIt(pTest, pOutput);
  79. if(_wcsicmp(pOutput->m_pClass,L"MyClass"))
  80. fatal(__LINE__);
  81. if(pOutput->m_dwNumKeys != 1)
  82. fatal(__LINE__);
  83. if(pOutput->m_bSingletonObj)
  84. fatal(__LINE__);
  85. p.Unparse(pOutput, &pRet);
  86. printf("\nUnparse -%S-", pRet);
  87. // if(_wcsicmp(pTest, pRet))
  88. // fatal(__LINE__);
  89. delete pRet;
  90. delete pOutput;
  91. }
  92. // this tests a singleton
  93. void test2()
  94. {
  95. int iRet;
  96. ParsedObjectPath * pOutput;
  97. CObjectPathParser p;
  98. WCHAR * pTest = L"\\\\.\\root\\default:MyClass=@";
  99. WCHAR * pRet = NULL;
  100. iRet = p.Parse(pTest, &pOutput);
  101. if(iRet != CObjectPathParser::NoError)
  102. fatal(__LINE__);
  103. DumpIt(pTest, pOutput);
  104. if(_wcsicmp(pOutput->m_pClass,L"MyClass"))
  105. fatal(__LINE__);
  106. if(pOutput->m_dwNumKeys != 0)
  107. fatal(__LINE__);
  108. if(!pOutput->m_bSingletonObj)
  109. fatal(__LINE__);
  110. p.Unparse(pOutput, &pRet);
  111. printf("\nUnparse -%S-", pRet);
  112. // if(_wcsicmp(pTest, pRet))
  113. // fatal(__LINE__);
  114. delete pRet;
  115. delete pOutput;
  116. }
  117. // this tests a multiple key path
  118. void test3()
  119. {
  120. int iRet;
  121. ParsedObjectPath * pOutput;
  122. CObjectPathParser p;
  123. WCHAR * pTest = L"\\\\.\\root\\default:MyClass.key=23,key2=\"xx\"";
  124. WCHAR * pRet = NULL;
  125. iRet = p.Parse(pTest, &pOutput);
  126. if(iRet != CObjectPathParser::NoError)
  127. fatal(__LINE__);
  128. DumpIt(pTest, pOutput);
  129. if(_wcsicmp(pOutput->m_pClass,L"MyClass"))
  130. fatal(__LINE__);
  131. if(pOutput->m_dwNumKeys != 2)
  132. fatal(__LINE__);
  133. if(pOutput->m_bSingletonObj)
  134. fatal(__LINE__);
  135. p.Unparse(pOutput, &pRet);
  136. printf("\nUnparse -%S-", pRet);
  137. // if(_wcsicmp(pTest, pRet))
  138. // fatal(__LINE__);
  139. delete pRet;
  140. delete pOutput;
  141. }
  142. // this tests an error in a single key path - missing closing quote
  143. void test4()
  144. {
  145. int iRet;
  146. ParsedObjectPath * pOutput;
  147. CObjectPathParser p;
  148. WCHAR * pTest = L"\\\\.\\root\\default:MyClass=\"hello";
  149. WCHAR * pRet = NULL;
  150. iRet = p.Parse(pTest, &pOutput);
  151. if(iRet == CObjectPathParser::NoError)
  152. fatal(__LINE__);
  153. }
  154. // this tests forward path slashes and a mix of slashes in the key
  155. void test5()
  156. {
  157. int iRet;
  158. ParsedObjectPath * pOutput;
  159. WCHAR * pTest = L"//./root/default:MyClass.key=\"ab/c\\\\def\""; // it takes four '\'s within a quoted string to yield a single '\'
  160. WCHAR * pRet = NULL;
  161. CObjectPathParser p;
  162. iRet = p.Parse(pTest, &pOutput);
  163. if(iRet != CObjectPathParser::NoError)
  164. fatal(__LINE__);
  165. DumpIt(pTest, pOutput);
  166. if(_wcsicmp(pOutput->m_pClass,L"MyClass"))
  167. fatal(__LINE__);
  168. if(pOutput->m_dwNumKeys != 1)
  169. fatal(__LINE__);
  170. if(pOutput->m_bSingletonObj)
  171. fatal(__LINE__);
  172. p.Unparse(pOutput, &pRet);
  173. printf("\nUnparse -%S-", pRet);
  174. // if(_wcsicmp(pTest, pRet))
  175. // fatal(__LINE__);
  176. delete pRet;
  177. delete pOutput;
  178. }
  179. // This tests unicode
  180. void test6()
  181. {
  182. int iRet;
  183. ParsedObjectPath * pOutput;
  184. WCHAR * pTest = L"//./root/\x0100xde\231faul\xffef:MyClass.\x0100\231\xffef=\"\x0100\xffef\"";
  185. WCHAR * pRet = NULL;
  186. CObjectPathParser p;
  187. iRet = p.Parse(pTest, &pOutput);
  188. if(iRet != CObjectPathParser::NoError)
  189. fatal(__LINE__);
  190. // note that the dump will not output much information since printf doesnt like unicode
  191. DumpIt(pTest, pOutput);
  192. if(_wcsicmp(pOutput->m_pClass,L"MyClass"))
  193. fatal(__LINE__);
  194. if(_wcsicmp(pOutput->m_paNamespaces[0],L"root"))
  195. fatal(__LINE__);
  196. if(_wcsicmp(pOutput->m_paNamespaces[1],L"\x0100xde\231faul\xffef"))
  197. fatal(__LINE__);
  198. if(_wcsicmp(pOutput->m_paKeys[0]->m_pName,L"\x0100\231\xffef"))
  199. fatal(__LINE__);
  200. if(_wcsicmp(pOutput->m_paKeys[0]->m_vValue.bstrVal,L"\x0100\xffef"))
  201. fatal(__LINE__);
  202. if(pOutput->m_dwNumKeys != 1)
  203. fatal(__LINE__);
  204. if(pOutput->m_bSingletonObj)
  205. fatal(__LINE__);
  206. p.Unparse(pOutput, &pRet);
  207. printf("\nUnparse -%S-", pRet);
  208. // if(_wcsicmp(pTest, pRet))
  209. // fatal(__LINE__);
  210. delete pRet;
  211. delete pOutput;
  212. }
  213. // This tests association type paths
  214. void test7()
  215. {
  216. int iRet;
  217. ParsedObjectPath * pOutput;
  218. WCHAR * pTest = L"\\\\.\\root\\default:Win32Users.Ant=\"\\\\\\\\WKSTA\\\\root\\\\default:System.Name=\\\"WKSTA\\\"\",Dep=\"Win32User.Name=\\\".Default\\\"\"";
  219. WCHAR * pRet = NULL;
  220. CObjectPathParser p;
  221. iRet = p.Parse(pTest, &pOutput);
  222. if(iRet != CObjectPathParser::NoError)
  223. fatal(__LINE__);
  224. // note that the dump will not output much information since printf doesnt like unicode
  225. DumpIt(pTest, pOutput);
  226. if(_wcsicmp(pOutput->m_pClass,L"Win32Users"))
  227. fatal(__LINE__);
  228. if(_wcsicmp(pOutput->m_paNamespaces[0],L"root"))
  229. fatal(__LINE__);
  230. if(_wcsicmp(pOutput->m_paNamespaces[1],L"default"))
  231. fatal(__LINE__);
  232. if(_wcsicmp(pOutput->m_paKeys[0]->m_pName,L"Ant"))
  233. fatal(__LINE__);
  234. if(_wcsicmp(pOutput->m_paKeys[0]->m_vValue.bstrVal,
  235. L"\\\\WKSTA\\root\\default:System.Name=\"WKSTA\""))
  236. fatal(__LINE__);
  237. if(_wcsicmp(pOutput->m_paKeys[1]->m_pName,L"Dep"))
  238. fatal(__LINE__);
  239. if(_wcsicmp(pOutput->m_paKeys[1]->m_vValue.bstrVal,L"Win32User.Name=\".Default\""))
  240. fatal(__LINE__);
  241. if(pOutput->m_dwNumKeys != 2)
  242. fatal(__LINE__);
  243. if(pOutput->m_bSingletonObj)
  244. fatal(__LINE__);
  245. p.Unparse(pOutput, &pRet);
  246. printf("\nUnparse -%S-", pRet);
  247. // if(_wcsicmp(pTest, pRet))
  248. // fatal(__LINE__);
  249. delete pRet;
  250. delete pOutput;
  251. }
  252. int main(int argc, char **argv)
  253. {
  254. int i;
  255. bVerbose = TRUE;
  256. for(i = 0; i< 1; i++)
  257. {
  258. printf("\ndoing test %d",i);
  259. test1();
  260. test2();
  261. test3();
  262. test4();
  263. test5();
  264. test6();
  265. test7();
  266. }
  267. return 0;
  268. }