/apps/test/index.php

https://github.com/postme/PHP-NIST-RBAC-library · PHP · 338 lines · 129 code · 62 blank · 147 comment · 0 complexity · f63bc0eddfed65909679d8b60a77cead MD5 · raw file

  1. <?php
  2. /**
  3. * NIST Core RBAC
  4. * @package NIST RBAC test framework
  5. * @author M.E. Post <meintmeint.net>
  6. * @version 0.66
  7. * @copyright M.E. Post
  8. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  9. */
  10. /**
  11. * NIST RBAC PHP API Test Framework
  12. */
  13. /**
  14. * Include configuration file
  15. */
  16. include dirname(__FILE__) . '/configuration.php';
  17. /**
  18. * Include the helper functions
  19. */
  20. include dirname(__FILE__) . '/../../include/php/include.php';
  21. /**
  22. * Include the NIST Core RBAC API library
  23. */
  24. include dirname(__FILE__) . '/../../lib/rbac_api.php';
  25. /* initialize variables */
  26. $random = getRandomString(5);
  27. $result = '';
  28. /* Switch on output buffering, no output to screen but dump in string */
  29. ob_start();
  30. print '<h1>Test Framework</h1>';
  31. /**
  32. * Test 1: AddUser
  33. *
  34. * This test adds a user to the RBAC user table
  35. */
  36. $user = 'TestUser_' . $random;
  37. $password = 'test';
  38. $first_name = 'Test';
  39. $family_name = 'User';
  40. $email = 'test@user.org';
  41. $result = AddUser($user, $password, $first_name, $family_name, $email);
  42. print '<div class="marginspace"><h2>Test 1 - AddUser:</h2>';
  43. print $result ? '<p class="no-error">User <strong>' . $user . '</strong> added succesfully</p>' : '<p class="error">Failed adding user <strong>' . $user . '</strong></p></div>';
  44. print '</div>';
  45. /**
  46. * Test 2: AddObject
  47. *
  48. * This test adds an object to the RBAC object table
  49. */
  50. $object = 'TestObject_' . $random;
  51. $locked = 0;
  52. $result = AddObject($object, $locked);
  53. print '<div class="marginspace"><h2>Test 2 - AddObject:</h2>';
  54. print $result ? '<p class="no-error">Object <strong>' . $object . '</strong> added succesfully</p>' : '<p class="error">Failed adding object <strong>' . $object . '</strong></p></div>';
  55. print '</div>';
  56. /**
  57. * Test 3: AddOperation
  58. *
  59. * This test adds an operation to the RBAC operation table
  60. */
  61. $operation = 'TestOperation_' . $random;
  62. $mask = '0110';
  63. $result = AddOperation($operation, $mask, $locked);
  64. print '<div class="marginspace"><h2>Test 3 - AddOperation:</h2>';
  65. print $result ? '<p class="no-error">Operation <strong>' . $operation . '</strong> added succesfully</p>' : '<p class="error">Failed adding operation <strong>' . $operation . '</strong></p></div>';
  66. print '</div>';
  67. /**
  68. * Test 4: AddPermission
  69. *
  70. * This test adds a permission to the RBAC permission table
  71. */
  72. $permission = 'TestPermission_' . $random;
  73. $result = AddPermission($permission, $object, $operation);
  74. print '<div class="marginspace"><h2>Test 4 - AddPermission:</h2>';
  75. print $result ? '<p class="no-error">Permission <strong>' . $permission . '</strong> added succesfully</p>' : '<p class="error">Failed adding permission <strong>' . $permission . '</strong></p>';
  76. print '</div>';
  77. /**
  78. * Test 5: AddRole
  79. *
  80. * This test adds a role to the RBAC role table
  81. */
  82. $role = 'TestRole_' . $random;
  83. $result = AddRole($role);
  84. print '<div class="marginspace"><h2>Test 5 - AddRole:</h2>';
  85. print $result ? '<p class="no-error">Role <strong>' . $role . '</strong> added succesfully</p>' : '<p class="error">Failed adding role <strong>' . $role . '</strong></p>';
  86. print '</div>';
  87. /**
  88. * Test 6: GrantPermission
  89. *
  90. * This test associates a permission with a role
  91. */
  92. $permission_set = array(array($object, $operation));
  93. $result = GrantPermission($permission_set, $role);
  94. print '<div class="marginspace"><h2>Test 6 - GrantPermission:</h2>';
  95. print $result ? '<p class="no-error">Permission <strong>' . $permission . '</strong> added succesfully to Role <strong>' . $role . '</strong></p>' : '<p class="error">Failed adding permission <strong>' . $permission . '</strong> to Role <strong>' . $role . '/<strong></p>';
  96. print '</div>';
  97. /**
  98. * Test 7: AssignUser
  99. *
  100. * This test associates a user with a role
  101. */
  102. $result = AssignUser($user, array($role));
  103. print '<div class="marginspace"><h2>Test 7 - AssignUser:</h2>';
  104. print $result ? '<p class="no-error">User <strong>' . $user . '</strong> associated succesfully with Role <strong>' . $role . '</strong></p>' : '<p class="error">Failed associating user <strong>' . $user . '</strong> with Role <strong>' . $role . '</strong></p>';
  105. print '</div>';
  106. /**
  107. * Test 8: CreateSession
  108. *
  109. * This test creates a session for the user
  110. */
  111. ini_set('session.hash_function', '1');
  112. session_start();
  113. session_regenerate_id();
  114. $session = session_id();
  115. $result = CreateSession($user, $session);
  116. print '<div class="marginspace"><h2>Test 8 - CreateSession:</h2>';
  117. print $result ? '<p class="no-error">User <strong>' . $user . '</strong> associated succesfully with Session <strong>' . $session . '</strong></p>' : '<p class="error">Failed associating user <strong>' . $user . '</strong> with Session <strong>' . $session . '</strong></p>';
  118. print '</div>';
  119. /**
  120. * Test 9: AddActiveRole
  121. *
  122. * This test temporary associates a user with a role during the session
  123. */
  124. $role2 = 'TestRole2_' . $random;
  125. AddRole($role2);
  126. $result = AddActiveRole($user, $session, array($role2));
  127. print '<div class="marginspace"><h2>Test 9 - AddActiveRole:</h2>';
  128. print $result ? '<p class="no-error">User <strong>' . $user . '</strong> associated succesfully with Role <strong>' . $role2 . '</strong> for the duration of Session <strong>' . $session . '</strong></p>' : '<p class="error">Failed associating user <strong>' . $user . '</strong> with Role <strong>' . $role2 . '</strong> for the duration of Session <strong>' . $session . '</strong></p>';
  129. print '</div>';
  130. /**
  131. * Test 10: AssignedUsers
  132. *
  133. * This test shows all users that have been assigned to a role
  134. */
  135. print '<div class="marginspace"><h2>Test 10 - AssignedUsers:</h2>';
  136. print showTableTest(AssignedUsers($role));
  137. print '<br/>';
  138. print '</div>';
  139. /**
  140. * Test 11: AssignedRoles
  141. *
  142. * This test shows all roles that have been assigned to a user
  143. */
  144. print '<div class="marginspace"><h2>Test 11 - AssignedRoles:</h2>';
  145. print showTableTest(AssignedRoles($user));
  146. print '<br/>';
  147. print '</div>';
  148. /**
  149. * Test 12: RolePermissions
  150. *
  151. * This test shows all permissions associated with a specific role
  152. */
  153. print '<div class="marginspace"><h2>Test 12 - RolePermissions:</h2>';
  154. print showTableTest(RolePermissions($role));
  155. print '<br/>';
  156. print '</div>';
  157. /**
  158. * Test 13: UserPermissions
  159. *
  160. * This test shows all permissions associated with a specific user
  161. */
  162. print '<div class="marginspace"><h2>Test 13 - UserPermissions:</h2>';
  163. print showTableTest(UserPermissions($user));
  164. print '<br/>';
  165. print '</div>';
  166. /**
  167. * Test 14: SessionRoles
  168. *
  169. * This test shows all roles associated with a specific session
  170. */
  171. print '<div class="marginspace"><h2>Test 14 - SessionRoles:</h2>';
  172. print showTableTest(SessionRoles($session));
  173. print '<br/>';
  174. /**
  175. * Test 15: SessionPermissions
  176. *
  177. * This test shows all permissions associated with a specific session
  178. */
  179. print '<div class="marginspace"><h2>Test 15 - SessionPermissions:</h2>';
  180. print showTableTest(SessionPermissions($session));
  181. print '<br/>';
  182. /**
  183. * Test 16: DropActiveRole
  184. *
  185. * This test removes temporary associations between a user and role(s) during the session
  186. */
  187. $result = DropActiveRole($user, $session, array($role2));
  188. print '<div class="marginspace"><h2>Test 16 - DropActiveRole:</h2>';
  189. print $result ? '<p class="no-error">User <strong>' . $user . '</strong> with Role <strong>' . $role2 . '</strong> removed from Session <strong>' . $session . '</strong></p>' : '<p class="error">Failed removing association between user <strong>' . $user . '</strong> and Role <strong>' . $role2 . '</strong> for Session <strong>' . $session . '</strong></p>';
  190. print '</div>';
  191. /**
  192. * Test 17: SessionRoles
  193. *
  194. * This test shows all roles associated with a specific session
  195. */
  196. print '<div class="marginspace"><h2>Test 17 - SessionRoles:</h2>';
  197. print showTableTest(SessionRoles($session));
  198. print '<br/>';
  199. /**
  200. * Test 18: DeleteSession
  201. *
  202. * This test deletes a session for the user
  203. */
  204. $result = DeleteSession(array($session));
  205. print '<div class="marginspace"><h2>Test 18 - DeleteSession:</h2>';
  206. print $result ? '<p class="no-error">Session <strong>' . $session . '</strong> deleted</p>' : '<p class="error">Failed deleting Session <strong>' . $session . '</strong></p>';
  207. print '</div>';
  208. /**
  209. * Test 19: DeassignUser
  210. *
  211. * This test dissociates a user from a role
  212. */
  213. $result = DeassignUser($user, array($role));
  214. print '<div class="marginspace"><h2>Test 19 - DeassignUser:</h2>';
  215. print $result ? '<p class="no-error">User <strong>' . $user . '</strong> dissociated succesfully from Role <strong>' . $role . '</strong></p>' : '<p class="error">Failed dissociating user <strong>' . $user . '</strong> from Role <strong>' . $role . '</strong></p>';
  216. print '</div>';
  217. /**
  218. * Test 20: RevokePermission
  219. *
  220. * This test revokes a permission from a role
  221. */
  222. $permission_set = array(array($object, $operation));
  223. $result = RevokePermission($permission_set, $role);
  224. print '<div class="marginspace"><h2>Test 20 - RevokePermission:</h2>';
  225. print $result ? '<p class="no-error">Permission <strong>' . $permission . '</strong> revoked from Role <strong>' . $role . '</strong></p>' : '<p class="error">Failed revoking permission <strong>' . $permission . '</strong> from Role <strong>' . $role . '/<strong></p>';
  226. print '</div>';
  227. /**
  228. * Test 21: DeleteRole
  229. *
  230. * This test deletes a role from the RBAC role table
  231. */
  232. $result = DeleteRole(array($role, $role2));
  233. print '<div class="marginspace"><h2>Test 21 - DeleteRole:</h2>';
  234. print $result ? '<p class="no-error">Role <strong>' . $role . '</strong> deleted succesfully</p>' : '<p class="error">Failed deleting role <strong>' . $role . '</strong></p>';
  235. print '</div>';
  236. /**
  237. * Test 22: DeletePermission
  238. *
  239. * This test deletes a permission from the RBAC permission table
  240. */
  241. $result = DeletePermission(array($permission));
  242. print '<div class="marginspace"><h2>Test 22 - DeletePermission:</h2>';
  243. print $result ? '<p class="no-error">Permission <strong>' . $permission . '</strong> deleted succesfully</p>' : '<p class="error">Failed deleting permission <strong>' . $permission . '</strong></p>';
  244. print '</div>';
  245. /**
  246. * Test 23: DeleteOperation
  247. *
  248. * This test adds an operation to the RBAC operation table
  249. */
  250. $result = DeleteOperation(array($operation));
  251. print '<div class="marginspace"><h2>Test 23 - DeleteOperation:</h2>';
  252. print $result ? '<p class="no-error">Operation <strong>' . $operation . '</strong> deleted succesfully</p>' : '<p class="error">Failed deleting operation <strong>' . $operation . '</strong></p></div>';
  253. print '</div>';
  254. /**
  255. * Test 24: DeleteObject
  256. *
  257. * This test adds an object to the RBAC object table
  258. */
  259. $result = DeleteObject(array($object));
  260. print '<div class="marginspace"><h2>Test 24 - DeleteObject:</h2>';
  261. print $result ? '<p class="no-error">Object <strong>' . $object . '</strong> deleted succesfully</p>' : '<p class="error">Failed deleting object <strong>' . $object . '</strong></p></div>';
  262. print '</div>';
  263. /**
  264. * Test 25: DeleteUser
  265. *
  266. * This test adds a user to the RBAC user table
  267. */
  268. $result = DeleteUser(array($user));
  269. print '<div class="marginspace"><h2>Test 25 - DeleteUser:</h2>';
  270. print $result ? '<p class="no-error">User <strong>' . $user . '</strong> deleted succesfully</p>' : '<p class="error">Failed deleting user <strong>' . $user . '</strong></p></div>';
  271. print '</div>';
  272. print '<h1>All tests have run succesfully</h1>';
  273. $page = ob_get_contents();
  274. ob_end_clean();
  275. print mergeContentWithTemplate($page);
  276. ?>