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.

1829 lines
68 KiB

  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Security.Principal;
  4. using Microsoft.Interop.Security.AzRoles;
  5. using System.Xml;
  6. using System.Threading;
  7. namespace AzCSharpTest
  8. {
  9. /// <summary>
  10. /// Summary description for Class1.
  11. /// </summary>
  12. class AzTestEntry
  13. {
  14. /// <summary>
  15. /// The main entry point for the application.
  16. /// </summary>
  17. [MTAThread]
  18. static void Main(string[] args)
  19. {
  20. // if asked for or wrong syntax, do the usage
  21. if (args.Length < 2 || args.Length > 3 || args[0] == "?" || args[0] == "/?")
  22. {
  23. DoUsage();
  24. return;
  25. }
  26. Console.WriteLine("This C# program tests AzRoles managed wrapper");
  27. try
  28. {
  29. // we will read the XML data file for the test
  30. XmlDocument xmlTestDataDoc = new XmlDocument();
  31. XmlDocument xmlBaseStoreDoc = new XmlDocument();
  32. if (xmlTestDataDoc == null || xmlBaseStoreDoc == null)
  33. {
  34. Exception exp = new Exception("Can't create XML documents. Testing aborted.");
  35. throw exp;
  36. }
  37. //
  38. // test data file is the first argument, and the base store
  39. // XML document is the second parameter
  40. //
  41. xmlTestDataDoc.Load(args[0]);
  42. xmlBaseStoreDoc.Load(args[1]);
  43. AzTestMode tm = AzTestMode.PrintAll;
  44. if (args.Length == 3)
  45. {
  46. //
  47. // this may not be a valid string for numbers we can accept
  48. // will throw in that case
  49. tm = (AzTestMode)(Convert.ToInt32(args[2]));
  50. }
  51. Console.WriteLine("");
  52. Console.WriteLine("Test data and base XML AzRole Store are loaded successfully.");
  53. CAzRolesTest azTest = new CAzRolesTest(xmlTestDataDoc, xmlBaseStoreDoc);
  54. azTest.TestAzRoles(tm);
  55. }
  56. catch (Exception e)
  57. {
  58. Console.WriteLine("Exception occurs " + e.Message + "\r\n" + e.StackTrace);
  59. }
  60. finally
  61. {
  62. GC.Collect();
  63. }
  64. }
  65. static void DoUsage()
  66. {
  67. Console.WriteLine("To use AzCSharpTest, use the following command:");
  68. Console.WriteLine("");
  69. Console.WriteLine("AzCSharpTest.exe TestInstructionFile BaseXMLStoreURL [EchoMode]");
  70. Console.WriteLine("");
  71. Console.WriteLine(" Where");
  72. Console.WriteLine(" TestInstructionFile is the XML file describing how to test.");
  73. Console.WriteLine(" BaseXMLStoreURL is the XML on which this test will be based on.");
  74. Console.WriteLine(" Optional EchoMode is default to 5, and can be one of the following values:");
  75. Console.WriteLine(" 1 for echoing error messages only,");
  76. Console.WriteLine(" 2 for echoing warning messages only,");
  77. Console.WriteLine(" 3 for echoing informational messages only,");
  78. Console.WriteLine(" 4 for echoing success messages only,");
  79. Console.WriteLine(" 5 for echoing all messages.");
  80. }
  81. }
  82. enum AzTestMode
  83. {
  84. PrintFailure = 1,
  85. PrintWarning = 2,
  86. PrintMessage = 3,
  87. PrintSuccess = 4,
  88. PrintAll = 5
  89. }
  90. enum AzTestMethod
  91. {
  92. PutProp = 1,
  93. GetProp = 2,
  94. ExeMethod = 3
  95. }
  96. enum AzTestResult
  97. {
  98. Success = 1,
  99. Failure = 2,
  100. Warning = 3,
  101. Message = 4
  102. }
  103. enum AzTestCase
  104. {
  105. Create = 1,
  106. Open = 2,
  107. AccessCheck = 3
  108. }
  109. //
  110. // this is the object that our main routine creates to start the test.
  111. // Call TestAzRoles to do the start testing.
  112. // This class also has a call back for test results (TestResult)
  113. //
  114. class CAzRolesTest
  115. {
  116. private int m_iFailureCount;
  117. private int m_iSuccessCount;
  118. private int m_iWarningCount;
  119. private AzTestMode m_TestMode = AzTestMode.PrintFailure;
  120. private XmlDocument m_docTestData;
  121. private XmlDocument m_docBaseStoreDoc;
  122. //
  123. // Our object needs to know two XML document, the first one
  124. // being the testing instruction XML, and the second one being the
  125. // data store (which is exactly an AzRoles XML store). Our test
  126. // usually consists of three steps:
  127. // (1) create a copy of this XML store (second parameter);
  128. // (2) open the newly created store (by our test) and compare the results
  129. // (3) Do access check test.
  130. // The whole test is not hard coded, instead, it is driven by the
  131. // testing instruction XML (first parameter). See samples for
  132. // the schema of this xml file.
  133. //
  134. public CAzRolesTest(XmlDocument xmlTestData, XmlDocument xmlStore)
  135. {
  136. m_docTestData = xmlTestData;
  137. m_docBaseStoreDoc = xmlStore;
  138. m_iSuccessCount = 0;
  139. m_iFailureCount = 0;
  140. m_iWarningCount = 0;
  141. }
  142. //
  143. // determines if the given result needs to be echoed to the console
  144. //
  145. private bool NeedEcho(AzTestResult atr)
  146. {
  147. if (m_TestMode == AzTestMode.PrintAll)
  148. return true;
  149. if (atr == AzTestResult.Success)
  150. return (m_TestMode == AzTestMode.PrintSuccess);
  151. else if (atr == AzTestResult.Failure)
  152. return (m_TestMode == AzTestMode.PrintFailure);
  153. else if (atr == AzTestResult.Warning)
  154. return (m_TestMode == AzTestMode.PrintWarning);
  155. else if (atr == AzTestResult.Message)
  156. return (m_TestMode == AzTestMode.PrintMessage);
  157. else
  158. return true;
  159. }
  160. //
  161. // callback for test result
  162. //
  163. public void TestResult(AzTestResult atr, string strMsg)
  164. {
  165. bool bNeedEcho = NeedEcho(atr);
  166. string strFinalMsg = "";
  167. if (atr == AzTestResult.Success)
  168. {
  169. if (bNeedEcho)
  170. strFinalMsg = "---Success--- " + strMsg;
  171. m_iSuccessCount++;
  172. }
  173. else if (atr == AzTestResult.Warning)
  174. {
  175. if (bNeedEcho)
  176. strFinalMsg = "---Warning--- " + strMsg;
  177. m_iWarningCount++;
  178. }
  179. else if (atr == AzTestResult.Message)
  180. {
  181. if (bNeedEcho)
  182. strFinalMsg = "-----FYI----- " + strMsg;
  183. }
  184. else
  185. {
  186. if (bNeedEcho)
  187. strFinalMsg = "***Failure*** " + strMsg;
  188. m_iFailureCount++;
  189. }
  190. if (bNeedEcho)
  191. {
  192. Console.WriteLine(strFinalMsg);
  193. }
  194. }
  195. //
  196. // retrive the given named attribute as a integer
  197. //
  198. private int GetIntAttribute(XmlNode xNode, string strAttrName, int defIfMissing)
  199. {
  200. XmlNode xAttriNode = xNode.Attributes.GetNamedItem(strAttrName);
  201. if (xAttriNode != null)
  202. return Convert.ToInt32(xAttriNode.Value);
  203. else
  204. return defIfMissing;
  205. }
  206. //
  207. // this function kicks out the testing
  208. //
  209. public void TestAzRoles(AzTestMode tm)
  210. {
  211. m_TestMode = tm;
  212. try
  213. {
  214. // we know that we only have one AzAuthorizationStore node on an AzRoles XML store.
  215. XmlNode xStoreAdmin = m_docBaseStoreDoc.SelectSingleNode("AzAdminManager");
  216. int iCreate = Convert.ToInt32(tagAZ_PROP_CONSTANTS.AZ_AZSTORE_FLAG_CREATE);
  217. int iManage = Convert.ToInt32(tagAZ_PROP_CONSTANTS.AZ_AZSTORE_FLAG_MANAGE_STORE_ONLY);
  218. // The root of our test instruction XML file is CSharpTestData
  219. XmlNode xTestRoot = m_docTestData.SelectSingleNode("CSharpTestData");
  220. // we might be creating a new store and use it for get property test or
  221. // access check test
  222. AzAuthorizationStoreClass azStoreNew = null;
  223. // We must do a create first. There is a single (so far) element called
  224. // AzAdminCreate, which decides how to create this target new store.
  225. // The most important attribute of this node is Url, which is the target
  226. // AzRoels store's url. If this URL points to an AD store, this test
  227. // will create an AD store.
  228. XmlNode xAdminNode = xTestRoot.SelectSingleNode("AzAdminCreate");
  229. CAzTestAzStore.OnTestResult onResult = new CAzTest.OnTestResult(this.TestResult);
  230. string strUrl = null;
  231. if (xAdminNode != null)
  232. {
  233. // The flag used to initialize the new store we need to create
  234. int flagAzCreate = GetIntAttribute(xAdminNode, "CreateFlag", 0);
  235. // Whether a new store will be created or not. We must eventually support
  236. // exporting data only, not a brand new store. Currently, we only tested
  237. // creating a new store.
  238. bool bCreateStore = Convert.ToBoolean(CAzTest.GetAttribute(xAdminNode, "CreateStore", true));
  239. // Get the new store's URL and create a brand new one
  240. azStoreNew = new AzAuthorizationStoreClass();
  241. strUrl = CAzTest.GetAttribute(xAdminNode, "Url", true);
  242. CAzTestAzStore.CreateTargetStore(strUrl, bCreateStore, flagAzCreate, ref azStoreNew);
  243. // Hook up the callback
  244. CAzTestAzStore TestAzStore = new CAzTestAzStore(azStoreNew, onResult, AzTestCase.Create);
  245. // Test the creation
  246. TestAzStore.StartTest(xStoreAdmin, xStoreAdmin);
  247. // resolve the link properties
  248. TestAzStore.UpdateLinkProperties(xStoreAdmin, xStoreAdmin);
  249. }
  250. // now do the open test. Right now, we don't use any attribute. The mere existence
  251. // of this element signals that we want to conduct a store wise verification of the
  252. // newly put data in the new store
  253. XmlNodeList xList = m_docTestData.GetElementsByTagName("AzAdminOpen");
  254. if (xList != null)
  255. {
  256. foreach (XmlNode n in xList)
  257. {
  258. DoOpenACTest(azStoreNew, onResult, AzTestCase.Open, xStoreAdmin, n);
  259. }
  260. }
  261. // Do access check test.
  262. xList = m_docTestData.GetElementsByTagName("AzAdminTest");
  263. if (xList != null)
  264. {
  265. foreach (XmlNode n in xList)
  266. {
  267. DoOpenACTest(azStoreNew, onResult, AzTestCase.AccessCheck, xStoreAdmin, n);
  268. }
  269. }
  270. }
  271. catch (Exception e)
  272. {
  273. TestResult(AzTestResult.Failure, e.Message + e.StackTrace);
  274. }
  275. finally
  276. {
  277. Console.WriteLine("");
  278. Console.WriteLine("Test completed with {0} successes.", m_iSuccessCount);
  279. Console.WriteLine("Test completed with {0} warnings.", m_iWarningCount);
  280. Console.WriteLine("Test completed with {0} errors.", m_iFailureCount);
  281. }
  282. }
  283. private void DoOpenACTest(AzAuthorizationStoreClass azStore, CAzTestAzStore.OnTestResult onResult,
  284. AzTestCase atc, XmlNode xStoreAdmin, XmlNode xTestNode)
  285. {
  286. object obReserved = null;
  287. // if this node doesn't have a Url attribute, then it means that
  288. // we are going to use the newly created store.
  289. // Ultimately, we want to support the Open phase to optionally use it's own URL.
  290. // But the problem that XML eats insignificant white characters make our
  291. // straightforward string comparision fail in those cases where insignificant
  292. // white characters are eaten by XML.
  293. string strUrl = null;
  294. //
  295. // We don't support the open testing to support its own URL at this time
  296. //
  297. if (atc == AzTestCase.AccessCheck)
  298. {
  299. strUrl = CAzTest.GetAttribute(xTestNode, "Url", false);
  300. }
  301. AzAuthorizationStoreClass azStoreOpen = null;
  302. if (strUrl != null)
  303. {
  304. azStoreOpen = new AzAuthorizationStoreClass();
  305. azStoreOpen.Initialize(0, strUrl, obReserved);
  306. }
  307. else
  308. {
  309. azStoreOpen = azStore;// if no Url attribute, then we must use the given one
  310. }
  311. if (azStoreOpen == null)
  312. {
  313. Exception eNoAdmin = new Exception("No AzAuthorizationStore to do open test");
  314. throw eNoAdmin;
  315. }
  316. CAzTestAzStore TestAzStore = new CAzTestAzStore(azStoreOpen, onResult, atc);
  317. if (atc == AzTestCase.Open)
  318. TestAzStore.StartTest(null, xStoreAdmin);
  319. else if (atc == AzTestCase.AccessCheck)
  320. TestAzStore.StartTest(null, xTestNode);
  321. else
  322. {
  323. Exception e = new Exception("DoOpenACTest called with invalid AzTestCase parameter");
  324. throw e;
  325. }
  326. }
  327. }
  328. struct PropIDAndName
  329. {
  330. public PropIDAndName(tagAZ_PROP_CONSTANTS id, string name)
  331. {
  332. ID = id;
  333. strName = name;
  334. }
  335. public tagAZ_PROP_CONSTANTS ID;
  336. public string strName;
  337. }
  338. // This is the base interface of our test objects. Since our COM objects
  339. // do not have a common interface base, we have to invent a new interface
  340. // which all our test objects (one for each of our objects) implements
  341. // For example, even the simple GetProperty/SetProperty must be done this
  342. // way because our IAzXXX do not share a common base interface unless we
  343. // will use Invoke to call a method, which is way too complicated.
  344. interface IAzTest
  345. {
  346. // this is to kick out the creation (put) and open (get) tests
  347. void StartTest(XmlNode xNodeAdmin, XmlNode xNode);
  348. // this is to kick out the access check test
  349. void TestAccessCheck(XmlNode xNode);
  350. // Link properties must be done after the creation is complete
  351. void UpdateLinkProperties(XmlNode xAdminNode, XmlNode xNode);
  352. // for echo purposes
  353. string GetObjectTypeName();
  354. string GetObjectName();
  355. void SendWarning(string msg);
  356. void SendMessage(string msg, bool bCritical);
  357. void SendTestResult(AzTestMethod atm, bool bSuccess, string strObjType, string strObjName, string strAttr, string strValue, string msg);
  358. // Some properties of our objects are saved as attributes of the node.
  359. // But others are saved as sub-elements. This creates a problem for us
  360. // to do a good OO design. We ask each object to return its own IDs and
  361. // Names of such properties
  362. PropIDAndName[] GetPropSubnodesIDAndNames();
  363. PropIDAndName[] GetPropAttributeIDAndNames();
  364. // Different IAzXXX objects have different sets of objects they can contain
  365. // This returns all objects names of such children objects.
  366. string[] GetChildObjectNames();
  367. // Set/Get Property must go through each individual object instead of using
  368. // polymorphism.
  369. void SetProperty(tagAZ_PROP_CONSTANTS PropID, string strValue);
  370. object GetProperty(tagAZ_PROP_CONSTANTS PropID);
  371. bool FindProperty(tagAZ_PROP_CONSTANTS propID, string strValue);
  372. // Although each or our IAzXXX object has a Submit method, they are not polymorphic
  373. // I certainly wish C# has templates as C++ does.
  374. void Submit(int i);
  375. // Again, in order for us to do most repeatitive work in the base, each test
  376. // object must tell us what children test object it can create/open.
  377. IAzTest GetTestObjectByName(string strEleName, string strObjName, bool bCreateNew);
  378. }
  379. // Most of the functionality of our testing objects are implemented by this base object
  380. // For each IAzXXX, we have a CAzTestXXX class that does the testing for that IAzXXX object.
  381. // All these CAzTestXXX are derived from CAzTest base class.
  382. abstract class CAzTest : IAzTest
  383. {
  384. // for call back
  385. protected OnTestResult m_resultCallback;
  386. public delegate void OnTestResult(AzTestResult atr, string strMsg);
  387. // this is our test case (create, open, or access check)
  388. protected AzTestCase m_atcCase;
  389. // constructor.
  390. public CAzTest(OnTestResult resultCallback, AzTestCase testCase)
  391. {
  392. m_resultCallback = resultCallback;
  393. m_atcCase = testCase;
  394. }
  395. // some XML helpers
  396. public static string GetSubElementAttribute(XmlNode nAC,
  397. string strSubNodeName,
  398. string strAttribName,
  399. bool bThrowIfMissing)
  400. {
  401. try
  402. {
  403. XmlNode appNode = nAC.SelectSingleNode(strSubNodeName);
  404. return appNode.Attributes[strAttribName].Value;
  405. }
  406. catch (Exception e)
  407. {
  408. if (bThrowIfMissing)
  409. {
  410. Exception Exp = new Exception("Missing " + strSubNodeName + " node or " +
  411. strAttribName + " attribute for access check.\r\n" + e.StackTrace);
  412. throw Exp;
  413. }
  414. else
  415. {
  416. return null;
  417. }
  418. }
  419. }
  420. public static string GetAttribute(XmlNode nAC, string strAttribName, bool bThrowIfMissing)
  421. {
  422. try
  423. {
  424. return nAC.Attributes[strAttribName].Value;
  425. }
  426. catch (Exception e)
  427. {
  428. if (bThrowIfMissing)
  429. {
  430. Exception eExp = new Exception("Node " + nAC.Name + " is missing attribute " + strAttribName +
  431. "\r\n" + e.StackTrace);
  432. throw eExp;
  433. }
  434. else
  435. {
  436. return null;
  437. }
  438. }
  439. }
  440. //
  441. // This is a helper to walk through the XML dom tree to find a
  442. // particular node with the given guid attribute.
  443. //
  444. // *******Warning*******
  445. // The algorithm is pretty lazy and slow. When we evolve this
  446. // program into a released tool, we need to pay attention
  447. // to this kind of slow algorithm and use something faster
  448. // *******Warning*******
  449. //
  450. protected string FindObjectNameByGuid(XmlNode xFindRootNode, string strGuid)
  451. {
  452. string strName = null;
  453. //
  454. // see if any immediate child has a matching guid.
  455. //
  456. foreach (XmlNode n in xFindRootNode.ChildNodes)
  457. {
  458. if (n.Attributes == null)
  459. continue;
  460. XmlNode attrNode = n.Attributes.GetNamedItem("Guid");
  461. if (attrNode != null && attrNode.Value.ToUpper() == strGuid.ToUpper())
  462. {
  463. //
  464. // we found it, but don't assume that the name is there
  465. //
  466. if (n.Attributes["Name"] != null)
  467. strName = n.Attributes["Name"].Value;
  468. if (strName == null)
  469. {
  470. //
  471. // this is a serious error
  472. //
  473. Exception e = new Exception("CAzTest.FindNameByBuid no name for " + strGuid);
  474. throw e;
  475. }
  476. return strName;
  477. }
  478. else
  479. {
  480. //
  481. // no match, then do the recursive search
  482. //
  483. strName = FindObjectNameByGuid(n, strGuid);
  484. if (strName != null)
  485. {
  486. break;
  487. }
  488. }
  489. }
  490. return strName;
  491. }
  492. //
  493. // all of our GetProperty functions will return object. But we only
  494. // has three different possible values: string, int, and boolean.
  495. // This function tests if the object is equal to the string representation
  496. // of the value.
  497. //
  498. protected bool ObjectEqualToStringValue(object ob, string strVal)
  499. {
  500. System.Type t = ob.GetType();
  501. if (t == System.Type.GetType("System.String"))
  502. {
  503. string strUpperVal = strVal.ToUpper();
  504. string strUpperGet = ob.ToString().ToUpper();
  505. return ((strUpperVal.Length == 0 && strUpperGet.Length == 0) || (strUpperVal == strUpperGet));
  506. }
  507. else if (t == System.Type.GetType("System.Int32"))
  508. {
  509. int iVal = Convert.ToInt32(strVal);
  510. return (iVal == (int)ob);
  511. }
  512. else if (t == System.Type.GetType("System.Boolean"))
  513. {
  514. bool b = Convert.ToBoolean(strVal);
  515. return (b == (bool)ob);
  516. }
  517. return false;
  518. }
  519. // For echoing purposes. Sub classes should not implement them.
  520. public void SendWarning(string msg)
  521. {
  522. m_resultCallback(AzTestResult.Warning, msg);
  523. }
  524. public void SendMessage(string msg, bool bCritical)
  525. {
  526. AzTestResult atr = AzTestResult.Message;
  527. if (bCritical)
  528. atr = AzTestResult.Failure;
  529. m_resultCallback(atr, msg);
  530. }
  531. public void SendTestResult(AzTestMethod atm,
  532. bool bSuccess,
  533. string strObjType,
  534. string strObjName,
  535. string strAttr,
  536. string strValue,
  537. string strReason)
  538. {
  539. string message;
  540. string strMethod;
  541. if (atm == AzTestMethod.PutProp)
  542. {
  543. strMethod = " Setting (" + strAttr + ") to (" + strValue + ") on " + strObjName + "\r\n";
  544. }
  545. else if (atm == AzTestMethod.GetProp)
  546. {
  547. strMethod = " Getting (" + strAttr + ") as (" + strValue + ") on " + strObjName + "\r\n";
  548. }
  549. else
  550. {
  551. strMethod = " Executing " + strAttr + " on " + strObjName + "\r\n";
  552. }
  553. if (bSuccess)
  554. {
  555. message = "The following operation on " + strObjType + " has succeeded:\r\n" + strMethod;
  556. }
  557. else
  558. {
  559. message = "The following operation on " + strObjType + " has failed:\r\n" + strMethod;
  560. message += " Reason: " + strReason;
  561. }
  562. m_resultCallback((bSuccess ? AzTestResult.Success : AzTestResult.Failure), message);
  563. }
  564. // We expect some sub classes to override these implementations.
  565. // But other leaf objects don't need to, and we provide convenient
  566. // implementation for them. Note, we will return null and the caller
  567. // should expect that.
  568. virtual public string[] GetChildObjectNames()
  569. {
  570. return null;
  571. }
  572. virtual public IAzTest GetTestObjectByName(string strEleName, string strObjName, bool bCreateNew)
  573. {
  574. return null;
  575. }
  576. // sub class must implement these functions. We don't know hot to implement them.
  577. public abstract string GetObjectTypeName();
  578. public abstract string GetObjectName();
  579. public abstract void Submit(int i);
  580. public abstract void SetProperty(tagAZ_PROP_CONSTANTS PropID, string strValue);
  581. public abstract object GetProperty(tagAZ_PROP_CONSTANTS PropID);
  582. //
  583. // Some of our properties returned from GetProperty will be arrays. While
  584. // the test can only give one string value a time, we need to walk through
  585. // the entire array to seek a match.
  586. // Aslo, GroupType attribute of a app group object records string value "LdapQuery"
  587. // but we will get 1 for "LdapQuery" group type when calling GetProperty
  588. //
  589. public virtual bool FindProperty(tagAZ_PROP_CONSTANTS propID, string strValue)
  590. {
  591. object obValue = GetProperty(propID);
  592. if (obValue != null)
  593. {
  594. System.Type t = obValue.GetType();
  595. // if not an array, then do straightforward comparison
  596. if (!t.IsArray)
  597. {
  598. if (propID == tagAZ_PROP_CONSTANTS.AZ_PROP_GROUP_TYPE)
  599. {
  600. strValue = (strValue == "LdapQuery") ? "1" : "0";
  601. }
  602. return ObjectEqualToStringValue(obValue, strValue);
  603. }
  604. else
  605. {
  606. // compare with each element until we found a match
  607. Array ar = obValue as Array;
  608. foreach (object ob in ar)
  609. {
  610. if (ObjectEqualToStringValue(ob, strValue))
  611. return true;
  612. }
  613. return false;
  614. }
  615. }
  616. return false;
  617. }
  618. public virtual PropIDAndName[] GetLinkIDAndNames()
  619. {
  620. return null;
  621. }
  622. public virtual PropIDAndName[] GetPropSubnodesIDAndNames()
  623. {
  624. return null;
  625. }
  626. public virtual PropIDAndName[] GetPropAttributeIDAndNames()
  627. {
  628. return null;
  629. }
  630. virtual public void StartTest(XmlNode xNodeAdmin, XmlNode xNode)
  631. {
  632. if (m_atcCase == AzTestCase.Create)
  633. {
  634. TestCreateOpen(xNodeAdmin, xNode, true);
  635. }
  636. else if (m_atcCase == AzTestCase.Open)
  637. {
  638. TestCreateOpen(xNodeAdmin, xNode, false);
  639. }
  640. else // access check
  641. {
  642. TestAccessCheck(xNode);
  643. }
  644. }
  645. virtual public void TestAccessCheck(XmlNode xNode)
  646. {
  647. ; // only admin manager knows how to conduct an access check
  648. }
  649. protected void TestCreateOpen(XmlNode xNodeAdmin, XmlNode xNode, bool bCreate)
  650. {
  651. // create all properties based on the data file's attributes
  652. AzTestMethod atmPutGet = bCreate ? AzTestMethod.PutProp : AzTestMethod.GetProp;
  653. bool bDoneSomething = false;
  654. PropIDAndName[] IdsAndNames = GetPropAttributeIDAndNames();
  655. if (IdsAndNames != null)
  656. {
  657. foreach (PropIDAndName IdName in IdsAndNames)
  658. {
  659. string strValue = "";
  660. try
  661. {
  662. // we allow the attribute to be missing. So, don't throw if can't find it
  663. strValue = GetAttribute(xNode, IdName.strName, false);
  664. if (strValue != null)
  665. {
  666. if (bCreate)
  667. {
  668. SetProperty(IdName.ID, strValue);
  669. bDoneSomething = true;
  670. }
  671. else
  672. {
  673. if (!FindProperty(IdName.ID, strValue))
  674. {
  675. string strMsg = "Property " + IdName.strName + " with value " + strValue + " is missing";
  676. Exception mismatchExp = new Exception(strMsg);
  677. throw mismatchExp;
  678. }
  679. }
  680. SendTestResult(atmPutGet, true, GetObjectTypeName(), GetObjectName(), IdName.strName, strValue, "");
  681. }
  682. else
  683. {
  684. SendMessage("The following attribute " + IdName.strName + " is missing on " + GetObjectName(), false);
  685. }
  686. }
  687. catch (Exception eSet)
  688. {
  689. SendTestResult(atmPutGet, false, GetObjectTypeName(), GetObjectName(), IdName.strName, strValue, eSet.Message + "\r\n" + eSet.StackTrace);
  690. }
  691. }
  692. }
  693. if (bCreate)
  694. {
  695. if (bDoneSomething)
  696. Submit(0);
  697. bDoneSomething = false;
  698. }
  699. // Get information regarding subnodes that are implemented as subnodes
  700. IdsAndNames = GetPropSubnodesIDAndNames();
  701. if (IdsAndNames != null)
  702. {
  703. foreach (PropIDAndName IdName in IdsAndNames)
  704. {
  705. // since now we know the subnodes' name, we can select all
  706. // and do the creation
  707. XmlNodeList xList = xNode.SelectNodes(IdName.strName);
  708. foreach (XmlNode n in xList)
  709. {
  710. try
  711. {
  712. if (bCreate)
  713. {
  714. SetProperty(IdName.ID, n.InnerText);
  715. bDoneSomething = true;
  716. }
  717. else
  718. {
  719. if (!FindProperty(IdName.ID, n.InnerText))
  720. {
  721. string strMsg = "Property " + IdName.strName + " with value " + n.InnerText + " is missing.";
  722. Exception mismatchExp = new Exception(strMsg);
  723. throw mismatchExp;
  724. }
  725. }
  726. SendTestResult(atmPutGet, true, GetObjectTypeName(), GetObjectName(), IdName.strName, n.InnerText, "");
  727. }
  728. catch (Exception eSet)
  729. {
  730. SendTestResult(atmPutGet, false, GetObjectTypeName(), GetObjectName(), IdName.strName, n.InnerText, eSet.Message + "\r\n" + eSet.StackTrace);
  731. }
  732. }
  733. }
  734. }
  735. if (bCreate && bDoneSomething)
  736. Submit(0);
  737. // now do the recursive
  738. string[] strSubObjects = GetChildObjectNames();
  739. if (strSubObjects != null)
  740. {
  741. foreach (string subElementName in strSubObjects)
  742. {
  743. XmlNodeList xList = xNode.SelectNodes(subElementName);
  744. foreach (XmlNode n in xList)
  745. {
  746. // true -> we will throw if Name attribute is missing because
  747. // we can't create sub object without names
  748. string NewObjName = GetAttribute(n, "Name", true);
  749. // true -> we will create a new object
  750. IAzTest azSubObj = null;
  751. try
  752. {
  753. azSubObj = GetTestObjectByName(subElementName, NewObjName, bCreate);
  754. }
  755. catch (Exception e)
  756. {
  757. // this shouldn't happen
  758. SendMessage("Failed while calling GetTestObjectByName with type =<" +
  759. subElementName + "> and name=<" + NewObjName + ">\r\n" + e.Message, true);
  760. }
  761. if (azSubObj != null)
  762. {
  763. azSubObj.StartTest(xNodeAdmin, n);
  764. bDoneSomething = true;
  765. }
  766. }
  767. }
  768. }
  769. if (bCreate)
  770. Submit(0);
  771. }
  772. virtual public void UpdateLinkProperties(XmlNode xAdminNode, XmlNode xNode)
  773. {
  774. bool bDoneSomething = false;
  775. PropIDAndName[] IdsAndNames = GetLinkIDAndNames();
  776. if (IdsAndNames != null)
  777. {
  778. foreach (PropIDAndName IdName in IdsAndNames)
  779. {
  780. // To resolve linking properties, we need to use
  781. // the guid to lookup the name and set the attribute by name
  782. XmlNodeList xList = xNode.SelectNodes(IdName.strName);
  783. foreach (XmlNode n in xList)
  784. {
  785. string strLinkName = FindObjectNameByGuid(xAdminNode, n.InnerText);
  786. try
  787. {
  788. SetProperty(IdName.ID, strLinkName);
  789. bDoneSomething = true;
  790. SendTestResult(AzTestMethod.PutProp, true, GetObjectTypeName(), GetObjectName(), IdName.strName, strLinkName, "");
  791. }
  792. catch (Exception eSet)
  793. {
  794. SendTestResult(AzTestMethod.PutProp, false, GetObjectTypeName(), GetObjectName(), IdName.strName, strLinkName, eSet.Message + "\r\n" + eSet.StackTrace);
  795. }
  796. }
  797. }
  798. }
  799. // if we have updated some links, then commit them
  800. if (bDoneSomething)
  801. Submit(0);
  802. // for each children, ask them to do a link update
  803. string[] strSubObjects = GetChildObjectNames();
  804. if (strSubObjects != null)
  805. {
  806. string strSubEleName;
  807. foreach (string strSubEleTagName in strSubObjects)
  808. {
  809. XmlNodeList xList = xNode.SelectNodes(strSubEleTagName);
  810. foreach (XmlNode n in xList)
  811. {
  812. // if must have a name for us to continue
  813. strSubEleName = GetAttribute(n, "Name", true);
  814. try
  815. {
  816. IAzTest subObj = GetTestObjectByName(strSubEleTagName, strSubEleName, false);
  817. subObj.UpdateLinkProperties(xAdminNode, n);
  818. }
  819. catch (Exception e)
  820. {
  821. SendMessage("Failed while calling GetTestObjectByName with type =<" +
  822. strSubEleTagName + "> and name=<" + strSubEleName + ">\r\n" + e.Message, true);
  823. }
  824. }
  825. }
  826. }
  827. }
  828. }
  829. class CAzTestAzStore : CAzTest
  830. {
  831. IAzAuthorizationStore m_store;
  832. static PropIDAndName[] AttrIDsNames = {new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_APPLICATION_DATA, "ApplicationData"),
  833. new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_APPLY_STORE_SACL, "ApplyStoreSacl"),
  834. new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_DESCRIPTION, "Description"),
  835. new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_AZSTORE_DOMAIN_TIMEOUT, "DomainTimeout"),
  836. new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_GENERATE_AUDITS, "GenerateAudits"),
  837. new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_AZSTORE_MAX_SCRIPT_ENGINES, "MaxScriptEngines"),
  838. new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_AZSTORE_SCRIPT_ENGINE_TIMEOUT, "ScriptEngineTimeout")};
  839. static string[] m_strChildSubNodes = {"AzApplication", "AzApplicationGroup"};
  840. public CAzTestAzStore(IAzAuthorizationStore store, OnTestResult cb, AzTestCase atc)
  841. : base(cb, atc)
  842. {
  843. m_store = store;
  844. }
  845. public override string GetObjectTypeName()
  846. {
  847. return "AzAuthorizationStore";
  848. }
  849. public override string GetObjectName()
  850. {
  851. return "AzStore";
  852. }
  853. public override PropIDAndName[] GetPropAttributeIDAndNames()
  854. {
  855. return AttrIDsNames;
  856. }
  857. public override string[] GetChildObjectNames()
  858. {
  859. return m_strChildSubNodes;
  860. }
  861. public override void Submit(int i)
  862. {
  863. object ob = null;
  864. m_store.Submit(i, ob);
  865. }
  866. public override void SetProperty(tagAZ_PROP_CONSTANTS PropID, string strVal)
  867. {
  868. object obReserved = null;
  869. m_store.SetProperty(Convert.ToInt32(PropID), strVal, obReserved);
  870. }
  871. public override object GetProperty(tagAZ_PROP_CONSTANTS PropID)
  872. {
  873. object obReserved = null;
  874. return m_store.GetProperty(Convert.ToInt32(PropID), obReserved);
  875. }
  876. public override IAzTest GetTestObjectByName(string strEleName, string strObjName, bool bCreate)
  877. {
  878. object obReserved = null;
  879. IAzTest testObj = null;
  880. if (strEleName == "AzApplication")
  881. {
  882. // create a task object
  883. IAzApplication app = null;
  884. if (bCreate)
  885. app = m_store.CreateApplication(strObjName, obReserved);
  886. else
  887. app = m_store.OpenApplication(strObjName, obReserved);
  888. testObj = new CAzTestApplication(app, m_resultCallback, m_atcCase);
  889. }
  890. else if (strEleName == "AzApplicationGroup")
  891. {
  892. IAzApplicationGroup AzAppGroup = null;
  893. if (bCreate)
  894. AzAppGroup = m_store.CreateApplicationGroup(strObjName, obReserved);
  895. else
  896. AzAppGroup = m_store.OpenApplicationGroup(strObjName, obReserved);
  897. testObj = new CAzTestAppGroup(AzAppGroup, m_resultCallback, m_atcCase);
  898. }
  899. return testObj;
  900. }
  901. public static void CreateTargetStore(string strUrl, bool bCreateNewStore, int iCreateFlag, ref AzAuthorizationStoreClass azStoreOut)
  902. {
  903. object obReserved = null;
  904. AzAuthorizationStoreClass azStore = new AzAuthorizationStoreClass();
  905. //
  906. // 1 means create store. So, by adding 1, we mean to create the store.
  907. // This may fail, and in that case we will just
  908. // delete it. We will create it and not just manage only. So, we will hard code the
  909. // flag as 1 for otherwise, AzRoles won't allow us to do access check
  910. //
  911. int iCreate = Convert.ToInt32(tagAZ_PROP_CONSTANTS.AZ_AZSTORE_FLAG_CREATE);
  912. int iManage = Convert.ToInt32(tagAZ_PROP_CONSTANTS.AZ_AZSTORE_FLAG_MANAGE_STORE_ONLY);
  913. int iNewStoreCreateFlag = bCreateNewStore ? iCreate + iCreateFlag : iCreateFlag;
  914. try
  915. {
  916. azStore.Initialize(iNewStoreCreateFlag, strUrl, obReserved);
  917. Console.WriteLine("");
  918. Console.WriteLine("AzAuthorizationStore created at " + strUrl);
  919. }
  920. catch (COMException)
  921. {
  922. try
  923. {
  924. //
  925. // we will assume that there is already this store and thus
  926. // we will delete it. AZ_ADMIN_FLAG_MANAGE_STORE_ONLY doesn't need auditing priv
  927. //
  928. azStore = new AzAuthorizationStoreClass();
  929. azStore.Initialize(iManage, strUrl, obReserved);
  930. azStore.Delete(obReserved);
  931. azStore = new AzAuthorizationStoreClass();
  932. azStore.Initialize(iNewStoreCreateFlag, strUrl, obReserved);
  933. Console.WriteLine("");
  934. Console.WriteLine("AzAuthorizationStore created " + strUrl);
  935. }
  936. catch (Exception doubleExp)
  937. {
  938. //
  939. // we don't know what is really going on, so re-throw
  940. //
  941. throw doubleExp;
  942. }
  943. }
  944. azStoreOut = azStore;
  945. }
  946. public override void TestAccessCheck(XmlNode xNode)
  947. {
  948. //
  949. // We should support the testing of those prop/methods that XML store don't support
  950. //
  951. /*
  952. XmlNodeList acList = xNode.SelectNodes("TestMethod");
  953. foreach (XmlNode n in acList)
  954. {
  955. CAzTestAccessCheck ac = new CAzTestAccessCheck(m_resultCallback);
  956. ac.DoAccessCheck(m_store, n);
  957. }
  958. */
  959. XmlNodeList acList = xNode.SelectNodes("AccessCheck");
  960. foreach (XmlNode n in acList)
  961. {
  962. CAzTestAccessCheck ac = new CAzTestAccessCheck(m_resultCallback);
  963. ac.DoAccessCheck(m_store, n);
  964. }
  965. }
  966. }
  967. class CAzTestAppGroup : CAzTest
  968. {
  969. private IAzApplicationGroup m_appGroup;
  970. static PropIDAndName[] AttrIDsNames = {new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_APPLICATION_DATA, "ApplicationData"),
  971. new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_DESCRIPTION, "Description"),
  972. new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_GROUP_TYPE, "GroupType")};
  973. static PropIDAndName[] SubnodesProp = {new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_GROUP_LDAP_QUERY, "LdapQuery"),
  974. new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_GROUP_MEMBERS, "Member"),
  975. new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_GROUP_NON_MEMBERS, "NonMember")};
  976. static PropIDAndName[] LinkIDsNames = {new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_GROUP_APP_MEMBERS, "AppMemberLink"),
  977. new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_GROUP_APP_NON_MEMBERS, "AppNonMemberLink")};
  978. public CAzTestAppGroup(IAzApplicationGroup appGp, OnTestResult cb, AzTestCase atc)
  979. : base(cb, atc)
  980. {
  981. m_appGroup = appGp;
  982. }
  983. public override string GetObjectTypeName()
  984. {
  985. return "AzApplicationGroup";
  986. }
  987. public override string GetObjectName()
  988. {
  989. return m_appGroup.Name;
  990. }
  991. public override PropIDAndName[] GetPropAttributeIDAndNames()
  992. {
  993. return AttrIDsNames;
  994. }
  995. public override PropIDAndName[] GetPropSubnodesIDAndNames()
  996. {
  997. return SubnodesProp;
  998. }
  999. public override PropIDAndName[] GetLinkIDAndNames()
  1000. {
  1001. return LinkIDsNames;
  1002. }
  1003. public override void Submit(int i)
  1004. {
  1005. object ob = null;
  1006. m_appGroup.Submit(i, ob);
  1007. }
  1008. public override void SetProperty(tagAZ_PROP_CONSTANTS PropID, string strVal)
  1009. {
  1010. object obReserved = null;
  1011. if (PropID == tagAZ_PROP_CONSTANTS.AZ_PROP_GROUP_APP_MEMBERS)
  1012. {
  1013. m_appGroup.AddAppMember(strVal, obReserved);
  1014. }
  1015. else if (PropID == tagAZ_PROP_CONSTANTS.AZ_PROP_GROUP_APP_NON_MEMBERS)
  1016. {
  1017. m_appGroup.AddAppNonMember(strVal, obReserved);
  1018. }
  1019. else if (PropID == tagAZ_PROP_CONSTANTS.AZ_PROP_GROUP_MEMBERS)
  1020. {
  1021. m_appGroup.AddMember(strVal, obReserved);
  1022. }
  1023. else if (PropID == tagAZ_PROP_CONSTANTS.AZ_PROP_GROUP_NON_MEMBERS)
  1024. {
  1025. m_appGroup.AddNonMember(strVal, obReserved);
  1026. }
  1027. else if (PropID == tagAZ_PROP_CONSTANTS.AZ_PROP_GROUP_LDAP_QUERY)
  1028. {
  1029. m_appGroup.LdapQuery = strVal;
  1030. }
  1031. else
  1032. {
  1033. // group type property is an odd baby. While we see in the store
  1034. // a string "LdapQuery", we translate it in cache 1 and we have
  1035. // to use the numeric value to set.
  1036. if (PropID == tagAZ_PROP_CONSTANTS.AZ_PROP_GROUP_TYPE)
  1037. {
  1038. if (strVal == "LdapQuery")
  1039. {
  1040. strVal = "1";
  1041. }
  1042. else
  1043. {
  1044. strVal = "0";
  1045. }
  1046. }
  1047. m_appGroup.SetProperty(Convert.ToInt32(PropID), strVal, obReserved);
  1048. }
  1049. }
  1050. public override object GetProperty(tagAZ_PROP_CONSTANTS PropID)
  1051. {
  1052. object obReserved = null;
  1053. return m_appGroup.GetProperty(Convert.ToInt32(PropID), obReserved);
  1054. }
  1055. }
  1056. class CAzTestApplication : CAzTest
  1057. {
  1058. private IAzApplication m_app;
  1059. static PropIDAndName[] AttrIDsNames = {new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_APPLICATION_DATA, "ApplicationData"),
  1060. new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_DESCRIPTION, "Description"),
  1061. new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_APPLY_STORE_SACL, "ApplyStoreSacl"),
  1062. new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_APPLICATION_AUTHZ_INTERFACE_CLSID, "AuthzInterfaceClsid"),
  1063. new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_APPLICATION_VERSION, "Version")};
  1064. string[] ChildSubNodes = {"AzTask", "AzOperation", "AzScope", "AzRole", "AzApplicationGroup"};
  1065. public CAzTestApplication(IAzApplication app, CAzTestAzStore.OnTestResult cb, AzTestCase atc)
  1066. : base(cb, atc)
  1067. {
  1068. m_app = app;
  1069. }
  1070. public override string GetObjectTypeName()
  1071. {
  1072. return "AzApplication";
  1073. }
  1074. public override string GetObjectName()
  1075. {
  1076. return m_app.Name;
  1077. }
  1078. public override PropIDAndName[] GetPropAttributeIDAndNames()
  1079. {
  1080. return AttrIDsNames;
  1081. }
  1082. public override string[] GetChildObjectNames()
  1083. {
  1084. return ChildSubNodes;
  1085. }
  1086. public override void Submit(int i)
  1087. {
  1088. object ob = null;
  1089. m_app.Submit(i, ob);
  1090. }
  1091. public override void SetProperty(tagAZ_PROP_CONSTANTS PropID, string strVal)
  1092. {
  1093. object obReserved = null;
  1094. m_app.SetProperty(Convert.ToInt32(PropID), strVal, obReserved);
  1095. }
  1096. public override object GetProperty(tagAZ_PROP_CONSTANTS PropID)
  1097. {
  1098. object obReserved = null;
  1099. return m_app.GetProperty(Convert.ToInt32(PropID), obReserved);
  1100. }
  1101. public override IAzTest GetTestObjectByName(string strEleName, string strObjName, bool bCreate)
  1102. {
  1103. object obReserved = null;
  1104. IAzTest testObj = null;
  1105. if (strEleName == "AzTask")
  1106. {
  1107. // create a task object
  1108. IAzTask task = null;
  1109. if (bCreate)
  1110. task = m_app.CreateTask(strObjName, obReserved);
  1111. else
  1112. task = m_app.OpenTask(strObjName, obReserved);
  1113. testObj = new CAzTestTask(task, m_resultCallback, m_atcCase);
  1114. }
  1115. else if (strEleName == "AzOperation")
  1116. {
  1117. IAzOperation op = null;
  1118. if (bCreate)
  1119. op = m_app.CreateOperation(strObjName, obReserved);
  1120. else
  1121. op = m_app.OpenOperation(strObjName, obReserved);
  1122. testObj = new CAzTestOperation(op, m_resultCallback, m_atcCase);
  1123. }
  1124. else if (strEleName == "AzScope")
  1125. {
  1126. IAzScope AzScope = null;
  1127. if (bCreate)
  1128. AzScope = m_app.CreateScope(strObjName, obReserved);
  1129. else
  1130. AzScope = m_app.OpenScope(strObjName, obReserved);
  1131. testObj = new CAzTestScope(AzScope, m_resultCallback, m_atcCase);
  1132. }
  1133. else if (strEleName == "AzRole")
  1134. {
  1135. IAzRole AzRole = null;
  1136. if (bCreate)
  1137. AzRole = m_app.CreateRole(strObjName, obReserved);
  1138. else
  1139. AzRole = m_app.OpenRole(strObjName, obReserved);
  1140. testObj = new CAzTestRole(AzRole, m_resultCallback, m_atcCase);
  1141. }
  1142. else if (strEleName == "AzApplicationGroup")
  1143. {
  1144. IAzApplicationGroup AzAppGroup = null;
  1145. if (bCreate)
  1146. AzAppGroup = m_app.CreateApplicationGroup(strObjName, obReserved);
  1147. else
  1148. AzAppGroup = m_app.OpenApplicationGroup(strObjName, obReserved);
  1149. testObj = new CAzTestAppGroup(AzAppGroup, m_resultCallback, m_atcCase);
  1150. }
  1151. return testObj;
  1152. }
  1153. }
  1154. class CAzTestTask : CAzTest
  1155. {
  1156. private IAzTask m_task;
  1157. static PropIDAndName[] AttrSubNodes = {new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_APPLICATION_DATA, "ApplicationData"),
  1158. new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_DESCRIPTION, "Description"),
  1159. new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_TASK_IS_ROLE_DEFINITION, "RoleDefinition"),
  1160. new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_TASK_BIZRULE_IMPORTED_PATH, "BizRuleImportedPath")};
  1161. static PropIDAndName[] PropSubNodes = {new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_TASK_BIZRULE_LANGUAGE, "BizRuleLanguage"),
  1162. new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_TASK_BIZRULE, "BizRule")};
  1163. static PropIDAndName[] LinkIDsNames = {new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_TASK_TASKS, "TaskLink"),
  1164. new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_TASK_OPERATIONS, "OperationLink")};
  1165. public CAzTestTask(IAzTask task, OnTestResult cb, AzTestCase atc)
  1166. : base(cb, atc)
  1167. {
  1168. m_task = task;
  1169. }
  1170. public override string GetObjectTypeName()
  1171. {
  1172. return "AzTask";
  1173. }
  1174. public override string GetObjectName()
  1175. {
  1176. return m_task.Name;
  1177. }
  1178. public override PropIDAndName[] GetPropAttributeIDAndNames()
  1179. {
  1180. return AttrSubNodes;
  1181. }
  1182. public override PropIDAndName[] GetPropSubnodesIDAndNames()
  1183. {
  1184. return PropSubNodes;
  1185. }
  1186. public override PropIDAndName[] GetLinkIDAndNames()
  1187. {
  1188. return LinkIDsNames;
  1189. }
  1190. public override void Submit(int i)
  1191. {
  1192. object ob = null;
  1193. m_task.Submit(i, ob);
  1194. }
  1195. public override void SetProperty(tagAZ_PROP_CONSTANTS PropID, string strVal)
  1196. {
  1197. object obReserved = null;
  1198. if (PropID == tagAZ_PROP_CONSTANTS.AZ_PROP_TASK_OPERATIONS)
  1199. {
  1200. m_task.AddOperation(strVal, obReserved);
  1201. }
  1202. else if (PropID == tagAZ_PROP_CONSTANTS.AZ_PROP_TASK_TASKS)
  1203. {
  1204. m_task.AddTask(strVal, obReserved);
  1205. }
  1206. else
  1207. {
  1208. m_task.SetProperty(Convert.ToInt32(PropID), strVal, obReserved);
  1209. }
  1210. }
  1211. public override object GetProperty(tagAZ_PROP_CONSTANTS PropID)
  1212. {
  1213. object obReserved = null;
  1214. return m_task.GetProperty(Convert.ToInt32(PropID), obReserved);
  1215. }
  1216. }
  1217. class CAzTestOperation : CAzTest
  1218. {
  1219. private IAzOperation m_op;
  1220. static PropIDAndName[] AttrSubNodes = {new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_APPLICATION_DATA, "ApplicationData"),
  1221. new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_DESCRIPTION, "Description")};
  1222. static PropIDAndName[] PropSubNodes = {new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_OPERATION_ID, "OperationID")};
  1223. public CAzTestOperation(IAzOperation op, OnTestResult cb, AzTestCase atc)
  1224. : base(cb, atc)
  1225. {
  1226. m_op = op;
  1227. }
  1228. public override string GetObjectTypeName()
  1229. {
  1230. return "AzOpearation";
  1231. }
  1232. public override string GetObjectName()
  1233. {
  1234. return m_op.Name;
  1235. }
  1236. public override PropIDAndName[] GetPropSubnodesIDAndNames()
  1237. {
  1238. return PropSubNodes;
  1239. }
  1240. public override PropIDAndName[] GetPropAttributeIDAndNames()
  1241. {
  1242. return AttrSubNodes;
  1243. }
  1244. public override void Submit(int i)
  1245. {
  1246. object ob = null;
  1247. m_op.Submit(i, ob);
  1248. }
  1249. public override void SetProperty(tagAZ_PROP_CONSTANTS PropID, string strVal)
  1250. {
  1251. object obReserved = null;
  1252. m_op.SetProperty(Convert.ToInt32(PropID), strVal, obReserved);
  1253. }
  1254. public override object GetProperty(tagAZ_PROP_CONSTANTS PropID)
  1255. {
  1256. object obReserved = null;
  1257. return m_op.GetProperty(Convert.ToInt32(PropID), obReserved);
  1258. }
  1259. }
  1260. class CAzTestScope : CAzTest
  1261. {
  1262. private IAzScope m_scope;
  1263. static PropIDAndName[] AttrSubNodes = {new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_APPLICATION_DATA, "ApplicationData"),
  1264. new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_DESCRIPTION, "Description")};
  1265. string[] ChildSubNodes = {"AzTask", "AzApplicationGroup", "AzRole"};
  1266. public CAzTestScope(IAzScope scope, CAzTestAzStore.OnTestResult cb, AzTestCase atc)
  1267. : base(cb, atc)
  1268. {
  1269. m_scope = scope;
  1270. }
  1271. public override string GetObjectName()
  1272. {
  1273. return m_scope.Name;
  1274. }
  1275. public override string GetObjectTypeName()
  1276. {
  1277. return "AzScope";
  1278. }
  1279. public override PropIDAndName[] GetPropAttributeIDAndNames()
  1280. {
  1281. return AttrSubNodes;
  1282. }
  1283. public override string[] GetChildObjectNames()
  1284. {
  1285. return ChildSubNodes;
  1286. }
  1287. public override void SetProperty(tagAZ_PROP_CONSTANTS PropID, string strVal)
  1288. {
  1289. object obReserved = null;
  1290. m_scope.SetProperty(Convert.ToInt32(PropID), strVal, obReserved);
  1291. }
  1292. public override object GetProperty(tagAZ_PROP_CONSTANTS PropID)
  1293. {
  1294. object obReserved = null;
  1295. return m_scope.GetProperty(Convert.ToInt32(PropID), obReserved);
  1296. }
  1297. public override void Submit(int i)
  1298. {
  1299. object ob = null;
  1300. m_scope.Submit(i, ob);
  1301. }
  1302. public override IAzTest GetTestObjectByName(string strEleName, string strObjName, bool bCreate)
  1303. {
  1304. object obReserved = null;
  1305. IAzTest testObj = null;
  1306. if (strEleName == "AzTask")
  1307. {
  1308. // create a task object
  1309. IAzTask task = null;
  1310. if (bCreate)
  1311. task = m_scope.CreateTask(strObjName, obReserved);
  1312. else
  1313. task = m_scope.OpenTask(strObjName, obReserved);
  1314. testObj = new CAzTestTask(task, m_resultCallback, m_atcCase);
  1315. }
  1316. else if (strEleName == "AzRole")
  1317. {
  1318. IAzRole AzRole = null;
  1319. if (bCreate)
  1320. AzRole = m_scope.CreateRole(strObjName, obReserved);
  1321. else
  1322. AzRole = m_scope.OpenRole(strObjName, obReserved);
  1323. testObj = new CAzTestRole(AzRole, m_resultCallback, m_atcCase);
  1324. }
  1325. else if (strEleName == "AzApplicationGroup")
  1326. {
  1327. IAzApplicationGroup AzAppGroup = null;
  1328. if (bCreate)
  1329. AzAppGroup = m_scope.CreateApplicationGroup(strObjName, obReserved);
  1330. else
  1331. AzAppGroup = m_scope.OpenApplicationGroup(strObjName, obReserved);
  1332. testObj = new CAzTestAppGroup(AzAppGroup, m_resultCallback, m_atcCase);
  1333. }
  1334. return testObj;
  1335. }
  1336. }
  1337. class CAzTestRole : CAzTest
  1338. {
  1339. private IAzRole m_role;
  1340. static PropIDAndName[] AttrSubNodes = {new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_APPLICATION_DATA, "ApplicationData"),
  1341. new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_DESCRIPTION, "Description")};
  1342. static PropIDAndName[] PropSubNodes = {new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_ROLE_MEMBERS, "Member")};
  1343. static PropIDAndName[] LinkIDsNames = {new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_ROLE_APP_MEMBERS, "AppMemberLink"),
  1344. new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_ROLE_TASKS, "TaskLink"),
  1345. new PropIDAndName(tagAZ_PROP_CONSTANTS.AZ_PROP_ROLE_OPERATIONS, "OperationLink")};
  1346. public CAzTestRole(IAzRole role, CAzTestAzStore.OnTestResult cb, AzTestCase atc)
  1347. : base(cb, atc)
  1348. {
  1349. m_role = role;
  1350. }
  1351. public override string GetObjectName()
  1352. {
  1353. return m_role.Name;
  1354. }
  1355. public override string GetObjectTypeName()
  1356. {
  1357. return "AzRole";
  1358. }
  1359. public override PropIDAndName[] GetPropAttributeIDAndNames()
  1360. {
  1361. return AttrSubNodes;
  1362. }
  1363. public override PropIDAndName[] GetPropSubnodesIDAndNames()
  1364. {
  1365. return PropSubNodes;
  1366. }
  1367. public override PropIDAndName[] GetLinkIDAndNames()
  1368. {
  1369. return LinkIDsNames;
  1370. }
  1371. public override void Submit(int i)
  1372. {
  1373. object ob = null;
  1374. m_role.Submit(i, ob);
  1375. }
  1376. public override void SetProperty(tagAZ_PROP_CONSTANTS PropID, string strVal)
  1377. {
  1378. object obReserved = null;
  1379. if (PropID == tagAZ_PROP_CONSTANTS.AZ_PROP_ROLE_APP_MEMBERS)
  1380. {
  1381. m_role.AddAppMember(strVal, obReserved);
  1382. }
  1383. else if (PropID == tagAZ_PROP_CONSTANTS.AZ_PROP_ROLE_TASKS)
  1384. {
  1385. m_role.AddTask(strVal, obReserved);
  1386. }
  1387. else if (PropID == tagAZ_PROP_CONSTANTS.AZ_PROP_ROLE_OPERATIONS)
  1388. {
  1389. m_role.AddOperation(strVal, obReserved);
  1390. }
  1391. else if (PropID == tagAZ_PROP_CONSTANTS.AZ_PROP_ROLE_MEMBERS)
  1392. {
  1393. m_role.AddMember(strVal, obReserved);
  1394. }
  1395. else
  1396. {
  1397. m_role.SetProperty(Convert.ToInt32(PropID), strVal, obReserved);
  1398. }
  1399. }
  1400. public override object GetProperty(tagAZ_PROP_CONSTANTS PropID)
  1401. {
  1402. object obReserved = null;
  1403. return m_role.GetProperty(Convert.ToInt32(PropID), obReserved);
  1404. }
  1405. }
  1406. class CAzTestAccessCheck : CAzTest
  1407. {
  1408. public CAzTestAccessCheck(OnTestResult cb) : base(cb, AzTestCase.AccessCheck)
  1409. {
  1410. }
  1411. public override string GetObjectName()
  1412. {
  1413. return null;
  1414. }
  1415. public override string GetObjectTypeName()
  1416. {
  1417. return null;
  1418. }
  1419. public override void SetProperty(tagAZ_PROP_CONSTANTS id, string strval)
  1420. {
  1421. ;// do nothing
  1422. }
  1423. public override object GetProperty(tagAZ_PROP_CONSTANTS PropID)
  1424. {
  1425. return null;
  1426. }
  1427. public override void Submit(int i)
  1428. {
  1429. ;// do nothing
  1430. }
  1431. private object[] GatherAttributes(XmlNode xNode, string strSubEleName, string strAttriName, bool bIsValue)
  1432. {
  1433. int iCount = 0;
  1434. XmlNodeList nList = null;
  1435. if (xNode != null)
  1436. {
  1437. nList = xNode.SelectNodes(strSubEleName);
  1438. if (nList != null)
  1439. {
  1440. iCount = nList.Count;
  1441. }
  1442. }
  1443. object[] oAllAttributes = new object[iCount];
  1444. for (int i = 0; i < iCount; i++)
  1445. {
  1446. bool bIsInt = false;
  1447. if (bIsValue)
  1448. {
  1449. // if this attribute is a value (instead of name), then we need
  1450. // to look for the IsInteger attribute and see what the value is
  1451. // Also, don't throw if this attribute is missing - default to string value
  1452. string strIsInt = GetAttribute(nList[i], "IsInteger", false);
  1453. if (strIsInt != null)
  1454. {
  1455. bIsInt = Convert.ToBoolean(strIsInt);
  1456. }
  1457. }
  1458. if (bIsInt)
  1459. {
  1460. oAllAttributes[i] = Convert.ToInt32(GetAttribute(nList[i], strAttriName, true));
  1461. }
  1462. else
  1463. {
  1464. oAllAttributes[i] = GetAttribute(nList[i], strAttriName, true);
  1465. }
  1466. }
  1467. return oAllAttributes;
  1468. }
  1469. public void DoAccessCheck(IAzAuthorizationStore azStore, XmlNode nAC)
  1470. {
  1471. string strApp = "";
  1472. string strUser = "";
  1473. try
  1474. {
  1475. // Each access check must be done against one single application. We must have
  1476. // this element, otherwise, we throw.
  1477. strApp = GetSubElementAttribute(nAC, "App", "Name", true);
  1478. // this attribute tells us if the expected result of the access check is
  1479. // deny or grant access
  1480. bool bExpectSuccess = Convert.ToBoolean(GetAttribute(nAC, "ExpectedResult", true));
  1481. // gather all scope names from the <Scopes> element.
  1482. XmlNode nTemp = nAC.SelectSingleNode("Scopes");
  1483. object[] oScopes = GatherAttributes(nTemp, "Scope", "Name", false);
  1484. // give some meaningful feedback
  1485. SendMessage("Conducting access check for " + strApp, false);
  1486. // we will use this thread's context to check. We can design the test instruction
  1487. // to determine if we should use name to initialize the client context. For now,
  1488. // we simply use the name as feedback information
  1489. object obReserved = null;
  1490. IAzApplication app = azStore.OpenApplication(strApp, obReserved);
  1491. // see if we have Account attribute. If yes, we will use name to create
  1492. // the client context. Otherwise, we will use the thread token
  1493. IAzClientContext clientContext = null;
  1494. strUser = GetAttribute(nAC, "Account", false);
  1495. string strDomain = GetAttribute(nAC, "Domain", false);
  1496. if (strUser == null)
  1497. {
  1498. WindowsIdentity identity = WindowsIdentity.GetCurrent();
  1499. strUser = identity.Name;
  1500. SendMessage("Current user is: " + strUser, false);
  1501. clientContext = app.InitializeClientContextFromToken(0, obReserved);
  1502. }
  1503. else
  1504. {
  1505. clientContext = app.InitializeClientContextFromName(strUser, strDomain, obReserved);
  1506. }
  1507. // fill in the access check parameter
  1508. nTemp = nAC.SelectSingleNode("Parameters");
  1509. object[] oNames = GatherAttributes(nTemp, "Parameter", "Name", false);
  1510. // true -> this attribute is a value attribute, so we need to check
  1511. // the type of the value.
  1512. object[] oValues = GatherAttributes(nTemp, "Parameter", "Value", true);
  1513. // populate the Op IDs
  1514. nTemp = nAC.SelectSingleNode("Operations");
  1515. object[] oOperations = GatherAttributes(nTemp, "OpID", "ID", true);
  1516. object[] oOther = new object[1];
  1517. oOther[0] = obReserved;
  1518. object[] results = (object[])clientContext.AccessCheck( strApp,
  1519. oScopes,
  1520. oOperations,
  1521. oNames,
  1522. oValues,
  1523. null,
  1524. null,
  1525. null);
  1526. bool bAuthorized = true;
  1527. foreach(int i in results)
  1528. {
  1529. if ( i != 0 )
  1530. {
  1531. bAuthorized = false;
  1532. break;
  1533. }
  1534. }
  1535. // if the access check resutl matches our expectation, we consider it a success
  1536. bool bSucceed = (bAuthorized && bExpectSuccess || !bAuthorized && !bExpectSuccess);
  1537. string strExpected = bSucceed ? "AS EXPECTED." : "NOT AS EXPECTED.";
  1538. Console.WriteLine("");
  1539. if (bAuthorized)
  1540. {
  1541. SendMessage("Client " + strUser + " is authorized - " + strExpected, false);
  1542. }
  1543. else
  1544. {
  1545. SendMessage("Client " + strUser + " is not authorized - " + strExpected, false);
  1546. }
  1547. SendTestResult(AzTestMethod.ExeMethod, bSucceed, "AccessCheck", strUser, strApp, "", "");
  1548. }
  1549. catch (Exception e)
  1550. {
  1551. SendTestResult(AzTestMethod.ExeMethod,
  1552. false,
  1553. "AccessCheck",
  1554. strUser,
  1555. strApp,
  1556. "",
  1557. e.Message + "\r\n" + e.StackTrace);
  1558. }
  1559. }
  1560. }
  1561. }