/Source/AIGA/src/Alchemi.Tester/Manager/GManagerTester.cs

https://github.com/abhishek-kumar/AIGA · C# · 395 lines · 222 code · 96 blank · 77 comment · 0 complexity · 08a30519af47618e442b74671a80c6d3 MD5 · raw file

  1. #region Alchemi copyright and license notice
  2. /*
  3. * Alchemi [.NET Grid Computing Framework]
  4. * http://www.alchemi.net
  5. * Title : GManagerTester.cs
  6. * Project : Alchemi.Tester.Manager
  7. * Created on : 22 October 2005
  8. * Copyright : Copyright © 2006 The University of Melbourne
  9. * This technology has been developed with the support of
  10. * the Australian Research Council and the University of Melbourne
  11. * research grants as part of the Gridbus Project
  12. * within GRIDS Laboratory at the University of Melbourne, Australia.
  13. * Author : Tibor Biro (tb@tbiro.com)
  14. * License : GPL
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public
  17. * License as published by the Free Software Foundation;
  18. * See the GNU General Public License
  19. * (http://www.gnu.org/copyleft/gpl.html) for more
  20. details.
  21. *
  22. */
  23. #endregion
  24. using System;
  25. using Alchemi.Core.Utility;
  26. using NUnit.Framework;
  27. using Alchemi.Core;
  28. using Alchemi.Core.Manager;
  29. using Alchemi.Core.Manager.Storage;
  30. using Alchemi.Manager;
  31. using Alchemi.Manager.Storage;
  32. namespace Alchemi.Tester.Manager
  33. {
  34. /// <summary>
  35. /// Testing GManager functionality
  36. /// These tests use the InMemoryManagerStorage storage, all other storages should
  37. /// perform identically as enforced by the storage level tests.
  38. /// </summary>
  39. [TestFixture]
  40. public class GManagerTester : GManager
  41. {
  42. private InMemoryManagerStorage m_managerStorage;
  43. private void SetupApplicationsGroupsAndUsers(Permission permission)
  44. {
  45. // add permissions
  46. Int32 groupId = 12;
  47. GroupStorageView[] groups = new GroupStorageView[1];
  48. groups[0] = new GroupStorageView(groupId, "test1");
  49. UserStorageView[] users = new UserStorageView[1];
  50. users[0] = new UserStorageView("username1", "password1", groupId);
  51. m_managerStorage.AddGroups(groups);
  52. m_managerStorage.AddUsers(users);
  53. m_managerStorage.AddGroupPermission(groupId, permission);
  54. SecurityCredentials sc = new SecurityCredentials("username1", HashUtil.GetHash("password1", HashUtil.HashType.MD5));
  55. // add applications, only one assigned to this user
  56. m_managerStorage.AddApplication(new ApplicationStorageView("username1"));
  57. m_managerStorage.AddApplication(new ApplicationStorageView("username2"));
  58. m_managerStorage.AddApplication(new ApplicationStorageView("username3"));
  59. }
  60. [SetUp]
  61. public void SetUp()
  62. {
  63. m_managerStorage = new InMemoryManagerStorage();
  64. ManagerStorageFactory.SetManagerStorage(m_managerStorage);
  65. }
  66. [TearDown]
  67. public void TearDown()
  68. {
  69. m_managerStorage = null;
  70. ManagerStorageFactory.SetManagerStorage(null);
  71. }
  72. public GManagerTester()
  73. {
  74. //
  75. // TODO: Add constructor logic here
  76. //
  77. }
  78. /// <summary>
  79. /// Add an application
  80. /// test for real creator.
  81. /// Should return true.
  82. /// </summary>
  83. [Test]
  84. public void IsApplicationCreatorTestRealCreator()
  85. {
  86. ApplicationStorageView application = new ApplicationStorageView("username1");
  87. String applicationId = m_managerStorage.AddApplication(application);
  88. SecurityCredentials sc = new SecurityCredentials("username1", HashUtil.GetHash("password1", HashUtil.HashType.MD5));
  89. bool result = IsApplicationCreator(sc, applicationId);
  90. Assert.IsTrue(result);
  91. }
  92. /// <summary>
  93. /// Add an application
  94. /// test for false creator.
  95. /// Should return false.
  96. /// </summary>
  97. [Test]
  98. public void IsApplicationCreatorTestFalseCreator()
  99. {
  100. ApplicationStorageView application = new ApplicationStorageView("username1");
  101. String applicationId = m_managerStorage.AddApplication(application);
  102. SecurityCredentials sc = new SecurityCredentials("username2", HashUtil.GetHash("password1", HashUtil.HashType.MD5));
  103. bool result = IsApplicationCreator(sc, applicationId);
  104. Assert.IsFalse(result);
  105. }
  106. /// <summary>
  107. /// Add an application
  108. /// test for creator for invalid application.
  109. /// Should return true.
  110. /// </summary>
  111. [Test]
  112. public void IsApplicationCreatorTestInvalidApplication()
  113. {
  114. ApplicationStorageView application = new ApplicationStorageView("username1");
  115. String applicationId = m_managerStorage.AddApplication(application);
  116. String invalidApplicationId = Guid.NewGuid().ToString();
  117. SecurityCredentials sc = new SecurityCredentials("username1", HashUtil.GetHash("password1", HashUtil.HashType.MD5));
  118. bool result = IsApplicationCreator(sc, invalidApplicationId);
  119. Assert.IsFalse(result);
  120. }
  121. /// <summary>
  122. /// Add a group, a user and a permissions, make sure the permission check passes.
  123. /// </summary>
  124. [Test]
  125. public void EnsurePermissionTestSimpleScenario()
  126. {
  127. Int32 groupId = 12;
  128. GroupStorageView[] groups = new GroupStorageView[1];
  129. groups[0] = new GroupStorageView(groupId, "test1");
  130. UserStorageView[] users = new UserStorageView[1];
  131. users[0] = new UserStorageView("username1", "password1", groupId);
  132. m_managerStorage.AddGroups(groups);
  133. m_managerStorage.AddUsers(users);
  134. m_managerStorage.AddGroupPermission(groupId, Permission.ExecuteThread);
  135. SecurityCredentials sc = new SecurityCredentials("username1", HashUtil.GetHash("password1", HashUtil.HashType.MD5));
  136. EnsurePermission(sc, Permission.ExecuteThread);
  137. // the above throws an exception if something is wrong so we are doing OK if we get this far
  138. Assert.IsTrue(true);
  139. }
  140. /// <summary>
  141. /// Add no group or permissions
  142. /// Check for a permission
  143. /// It should throw an AuthorizationException
  144. /// </summary>
  145. [Test]
  146. public void EnsurePermissionTestNoPermission()
  147. {
  148. SecurityCredentials sc = new SecurityCredentials("username1", HashUtil.GetHash("password1", HashUtil.HashType.MD5));
  149. try
  150. {
  151. EnsurePermission(sc, Permission.ExecuteThread);
  152. }
  153. catch(AuthorizationException)
  154. {
  155. Assert.IsTrue(true);
  156. return;
  157. }
  158. Assert.IsFalse(true, "The authorization should fail");
  159. }
  160. /// <summary>
  161. /// Add a user
  162. /// Check if the user is authenticated
  163. /// </summary>
  164. [Test]
  165. public void AuthenticateUserTestSimpleScenario()
  166. {
  167. Int32 groupId = 12;
  168. UserStorageView[] users = new UserStorageView[1];
  169. users[0] = new UserStorageView("username1", "password1", groupId);
  170. m_managerStorage.AddUsers(users);
  171. SecurityCredentials sc = new SecurityCredentials("username1", HashUtil.GetHash("password1", HashUtil.HashType.MD5));
  172. AuthenticateUser(sc);
  173. // the above throws an exception if something is wrong so we are doing OK if we get this far
  174. Assert.IsTrue(true);
  175. }
  176. /// <summary>
  177. /// Add no user
  178. /// Check if the user is authenticated
  179. /// An AuthenticationException is expected
  180. /// </summary>
  181. [Test]
  182. public void AuthenticateUserTestNoUsers()
  183. {
  184. SecurityCredentials sc = new SecurityCredentials("username1", HashUtil.GetHash("password1", HashUtil.HashType.MD5));
  185. try
  186. {
  187. AuthenticateUser(sc);
  188. }
  189. catch(AuthenticationException)
  190. {
  191. Assert.IsTrue(true);
  192. return;
  193. }
  194. Assert.IsFalse(true, "The authorization should fail");
  195. }
  196. /// <summary>
  197. /// Add permissions and group membership so that EnsurePermission(sc,Permission.ManageOwnApp) does not throw an exception
  198. /// </summary>
  199. [Test]
  200. public void Admon_GetLiveApplicationListTestSimpleScenario()
  201. {
  202. SetupApplicationsGroupsAndUsers(Permission.ManageOwnApp);
  203. SecurityCredentials sc = new SecurityCredentials("username1", HashUtil.GetHash("password1", HashUtil.HashType.MD5));
  204. ApplicationStorageView[] result = Admon_GetUserApplicationList(sc);
  205. Assert.AreEqual(1, result.Length);
  206. }
  207. [Test]
  208. public void Admon_GetUserApplicationListTestSimpleScenario()
  209. {
  210. SetupApplicationsGroupsAndUsers(Permission.ManageAllApps);
  211. SecurityCredentials sc = new SecurityCredentials("username1", HashUtil.GetHash("password1", HashUtil.HashType.MD5));
  212. ApplicationStorageView[] result = Admon_GetLiveApplicationList(sc);
  213. Assert.AreEqual(3, result.Length);
  214. }
  215. /// <summary>
  216. /// Setup applications but user has only rights to see own apps
  217. /// Should return oly one application
  218. /// </summary>
  219. [Test]
  220. public void Admon_GetUserApplicationListTestNotEnoughPermissions()
  221. {
  222. SetupApplicationsGroupsAndUsers(Permission.ManageOwnApp);
  223. SecurityCredentials sc = new SecurityCredentials("username1", HashUtil.GetHash("password1", HashUtil.HashType.MD5));
  224. ApplicationStorageView[] result = Admon_GetLiveApplicationList(sc);
  225. Assert.AreEqual(1, result.Length);
  226. }
  227. [Test]
  228. public void Admon_PerformStorageMaintenanceTestNoAuthentication()
  229. {
  230. SecurityCredentials sc = new SecurityCredentials("username1", HashUtil.GetHash("password1", HashUtil.HashType.MD5));
  231. StorageMaintenanceParameters maintenanceParameters = new StorageMaintenanceParameters();
  232. try
  233. {
  234. Admon_PerformStorageMaintenance(sc, maintenanceParameters);
  235. }
  236. catch (AuthenticationException)
  237. {
  238. Assert.IsTrue(true);
  239. return;
  240. }
  241. Assert.IsFalse(true, "The authentication should fail");
  242. }
  243. [Test]
  244. public void Admon_PerformStorageMaintenanceTestNotEnoughPermissions()
  245. {
  246. SetupApplicationsGroupsAndUsers(Permission.ManageOwnApp);
  247. SecurityCredentials sc = new SecurityCredentials("username1", HashUtil.GetHash("password1", HashUtil.HashType.MD5));
  248. StorageMaintenanceParameters maintenanceParameters = new StorageMaintenanceParameters();
  249. try
  250. {
  251. Admon_PerformStorageMaintenance(sc, maintenanceParameters);
  252. }
  253. catch (AuthorizationException)
  254. {
  255. Assert.IsTrue(true);
  256. return;
  257. }
  258. Assert.IsFalse(true, "The authorization should fail");
  259. }
  260. [Test]
  261. public void Admon_PerformStorageMaintenanceTestNullMaintenanceParameters()
  262. {
  263. SetupApplicationsGroupsAndUsers(Permission.ManageAllApps);
  264. SecurityCredentials sc = new SecurityCredentials("username1", HashUtil.GetHash("password1", HashUtil.HashType.MD5));
  265. StorageMaintenanceParameters maintenanceParameters = null;
  266. try
  267. {
  268. Admon_PerformStorageMaintenance(sc, maintenanceParameters);
  269. }
  270. catch (NullReferenceException)
  271. {
  272. Assert.IsTrue(true);
  273. return;
  274. }
  275. Assert.IsFalse(true, "The null parameters should throw an error.");
  276. }
  277. /// <summary>
  278. /// This just tests calling into the Maintenance.PerformMaintenance.
  279. /// Other tests in the MaintenanceTester test the actual parameter combinations.
  280. /// </summary>
  281. [Test]
  282. public void Admon_PerformStorageMaintenanceTestWithParameters()
  283. {
  284. SetupApplicationsGroupsAndUsers(Permission.ManageAllApps);
  285. SecurityCredentials sc = new SecurityCredentials("username1", HashUtil.GetHash("password1", HashUtil.HashType.MD5));
  286. m_managerStorage.AddApplication(new ApplicationStorageView("username1"));
  287. m_managerStorage.AddExecutor(new ExecutorStorageView(true, true, DateTime.Now, "test", 1, "test", 1, 1, 1, 1));
  288. StorageMaintenanceParameters maintenanceParameters = new StorageMaintenanceParameters();
  289. maintenanceParameters.RemoveAllExecutors = true;
  290. maintenanceParameters.RemoveAllApplications = true;
  291. // just to keep things honest make sure there is something there
  292. Assert.AreNotEqual(0, m_managerStorage.GetExecutors().Length);
  293. Assert.AreNotEqual(0, m_managerStorage.GetApplications().Length);
  294. Admon_PerformStorageMaintenance(sc, maintenanceParameters);
  295. Assert.AreEqual(0, m_managerStorage.GetExecutors().Length);
  296. Assert.AreEqual(0, m_managerStorage.GetApplications().Length);
  297. }
  298. }
  299. }