/src/test/java/org/owasp/esapi/reference/AccessControllerTest.java

http://owasp-esapi-java.googlecode.com/ · Java · 358 lines · 237 code · 42 blank · 79 comment · 6 complexity · f04850455c39119d5d42980f6f941cb2 MD5 · raw file

  1. /**
  2. * OWASP Enterprise Security API (ESAPI)
  3. *
  4. * This file is part of the Open Web Application Security Project (OWASP)
  5. * Enterprise Security API (ESAPI) project. For details, please see
  6. * <a href="http://www.owasp.org/index.php/ESAPI">http://www.owasp.org/index.php/ESAPI</a>.
  7. *
  8. * Copyright (c) 2007 - The OWASP Foundation
  9. *
  10. * The ESAPI is published by OWASP under the BSD license. You should read and accept the
  11. * LICENSE before you use, modify, and/or redistribute this software.
  12. *
  13. * @author Jeff Williams <a href="http://www.aspectsecurity.com">Aspect Security</a>
  14. * @created 2007
  15. */
  16. package org.owasp.esapi.reference;
  17. import junit.framework.Test;
  18. import junit.framework.TestCase;
  19. import junit.framework.TestSuite;
  20. import org.owasp.esapi.ESAPI;
  21. import org.owasp.esapi.AccessController;
  22. import org.owasp.esapi.Authenticator;
  23. import org.owasp.esapi.User;
  24. import org.owasp.esapi.errors.AccessControlException;
  25. /**
  26. * The Class AccessControllerTest.
  27. *
  28. * @author Jeff Williams (jeff.williams@aspectsecurity.com)
  29. */
  30. public class AccessControllerTest extends TestCase {
  31. /**
  32. * Instantiates a new access controller test.
  33. *
  34. * @param testName
  35. * the test name
  36. * @throws Exception
  37. */
  38. public AccessControllerTest(String testName) throws Exception {
  39. super(testName);
  40. Authenticator authenticator = ESAPI.authenticator();
  41. String password = authenticator.generateStrongPassword();
  42. // create a user with the "user" role for this test
  43. User alice = authenticator.getUser("testuser1");
  44. if ( alice == null ) {
  45. alice = authenticator.createUser( "testuser1", password, password);
  46. }
  47. alice.addRole("user");
  48. // create a user with the "admin" role for this test
  49. User bob = authenticator.getUser("testuser2");
  50. if ( bob == null ) {
  51. bob = authenticator.createUser( "testuser2", password, password);
  52. }
  53. bob.addRole("admin");
  54. // create a user with the "user" and "admin" roles for this test
  55. User mitch = authenticator.getUser("testuser3");
  56. if ( mitch == null ) {
  57. mitch = authenticator.createUser( "testuser3", password, password);
  58. }
  59. mitch.addRole("admin");
  60. mitch.addRole("user");
  61. }
  62. /**
  63. * {@inheritDoc}
  64. *
  65. * @throws Exception
  66. */
  67. protected void setUp() throws Exception {
  68. }
  69. /**
  70. * {@inheritDoc}
  71. *
  72. * @throws Exception
  73. */
  74. protected void tearDown() throws Exception {
  75. // none
  76. }
  77. /**
  78. * Suite.
  79. *
  80. * @return the test
  81. */
  82. public static Test suite() {
  83. TestSuite suite = new TestSuite(AccessControllerTest.class);
  84. return suite;
  85. }
  86. /**
  87. *
  88. */
  89. public void testMatchRule() {
  90. ESAPI.authenticator().setCurrentUser(null);
  91. assertFalse(ESAPI.accessController().isAuthorizedForURL("/nobody"));
  92. }
  93. /**
  94. * Test of isAuthorizedForURL method, of class
  95. * org.owasp.esapi.AccessController.
  96. *
  97. * @throws Exception
  98. */
  99. public void testIsAuthorizedForURL() throws Exception {
  100. System.out.println("isAuthorizedForURL");
  101. AccessController instance = ESAPI.accessController();
  102. Authenticator auth = ESAPI.authenticator();
  103. auth.setCurrentUser( auth.getUser("testuser1") );
  104. assertFalse(instance.isAuthorizedForURL("/nobody"));
  105. assertFalse(instance.isAuthorizedForURL("/test/admin"));
  106. assertTrue(instance.isAuthorizedForURL("/test/user"));
  107. assertTrue(instance.isAuthorizedForURL("/test/all"));
  108. assertFalse(instance.isAuthorizedForURL("/test/none"));
  109. assertTrue(instance.isAuthorizedForURL("/test/none/test.gif"));
  110. assertFalse(instance.isAuthorizedForURL("/test/none/test.exe"));
  111. assertTrue(instance.isAuthorizedForURL("/test/none/test.png"));
  112. assertFalse(instance.isAuthorizedForURL("/test/moderator"));
  113. assertTrue(instance.isAuthorizedForURL("/test/profile"));
  114. assertFalse(instance.isAuthorizedForURL("/upload"));
  115. auth.setCurrentUser( auth.getUser("testuser2") );
  116. assertFalse(instance.isAuthorizedForURL("/nobody"));
  117. assertTrue(instance.isAuthorizedForURL("/test/admin"));
  118. assertFalse(instance.isAuthorizedForURL("/test/user"));
  119. assertTrue(instance.isAuthorizedForURL("/test/all"));
  120. assertFalse(instance.isAuthorizedForURL("/test/none"));
  121. assertTrue(instance.isAuthorizedForURL("/test/none/test.png"));
  122. assertFalse(instance.isAuthorizedForURL("/test/moderator"));
  123. assertTrue(instance.isAuthorizedForURL("/test/profile"));
  124. assertFalse(instance.isAuthorizedForURL("/upload"));
  125. auth.setCurrentUser( auth.getUser("testuser3") );
  126. assertFalse(instance.isAuthorizedForURL("/nobody"));
  127. assertTrue(instance.isAuthorizedForURL("/test/admin"));
  128. assertTrue(instance.isAuthorizedForURL("/test/user"));
  129. assertTrue(instance.isAuthorizedForURL("/test/all"));
  130. assertFalse(instance.isAuthorizedForURL("/test/none"));
  131. assertTrue(instance.isAuthorizedForURL("/test/none/test.png"));
  132. assertFalse(instance.isAuthorizedForURL("/test/moderator"));
  133. assertTrue(instance.isAuthorizedForURL("/test/profile"));
  134. assertFalse(instance.isAuthorizedForURL("/upload"));
  135. try {
  136. instance.assertAuthorizedForURL("/test/admin");
  137. instance.assertAuthorizedForURL( "/nobody" );
  138. fail();
  139. } catch ( AccessControlException e ) {
  140. // expected
  141. }
  142. }
  143. /**
  144. * Test of isAuthorizedForFunction method, of class
  145. * org.owasp.esapi.AccessController.
  146. */
  147. public void testIsAuthorizedForFunction() {
  148. System.out.println("isAuthorizedForFunction");
  149. AccessController instance = ESAPI.accessController();
  150. Authenticator auth = ESAPI.authenticator();
  151. auth.setCurrentUser( auth.getUser("testuser1") );
  152. assertTrue(instance.isAuthorizedForFunction("/FunctionA"));
  153. assertFalse(instance.isAuthorizedForFunction("/FunctionAdeny"));
  154. assertFalse(instance.isAuthorizedForFunction("/FunctionB"));
  155. assertFalse(instance.isAuthorizedForFunction("/FunctionBdeny"));
  156. assertTrue(instance.isAuthorizedForFunction("/FunctionC"));
  157. assertFalse(instance.isAuthorizedForFunction("/FunctionCdeny"));
  158. auth.setCurrentUser( auth.getUser("testuser2") );
  159. assertFalse(instance.isAuthorizedForFunction("/FunctionA"));
  160. assertFalse(instance.isAuthorizedForFunction("/FunctionAdeny"));
  161. assertTrue(instance.isAuthorizedForFunction("/FunctionB"));
  162. assertFalse(instance.isAuthorizedForFunction("/FunctionBdeny"));
  163. assertTrue(instance.isAuthorizedForFunction("/FunctionD"));
  164. assertFalse(instance.isAuthorizedForFunction("/FunctionDdeny"));
  165. auth.setCurrentUser( auth.getUser("testuser3") );
  166. assertTrue(instance.isAuthorizedForFunction("/FunctionA"));
  167. assertFalse(instance.isAuthorizedForFunction("/FunctionAdeny"));
  168. assertTrue(instance.isAuthorizedForFunction("/FunctionB"));
  169. assertFalse(instance.isAuthorizedForFunction("/FunctionBdeny"));
  170. assertTrue(instance.isAuthorizedForFunction("/FunctionC"));
  171. assertFalse(instance.isAuthorizedForFunction("/FunctionCdeny"));
  172. try {
  173. instance.assertAuthorizedForFunction("/FunctionA");
  174. instance.assertAuthorizedForFunction( "/FunctionDdeny" );
  175. fail();
  176. } catch ( AccessControlException e ) {
  177. // expected
  178. }
  179. }
  180. /**
  181. * Test of isAuthorizedForData method, of class
  182. * org.owasp.esapi.AccessController.
  183. */
  184. public void testIsAuthorizedForData() {
  185. System.out.println("isAuthorizedForData");
  186. AccessController instance = ESAPI.accessController();
  187. Authenticator auth = ESAPI.authenticator();
  188. Class adminR = null;
  189. Class adminRW = null;
  190. Class userW = null;
  191. Class userRW = null;
  192. Class anyR = null;
  193. Class userAdminR = null;
  194. Class userAdminRW = null;
  195. Class undefined = null;
  196. try{
  197. adminR = Class.forName("java.util.ArrayList");
  198. adminRW = Class.forName("java.lang.Math");
  199. userW = Class.forName("java.util.Date");
  200. userRW = Class.forName("java.lang.String");
  201. anyR = Class.forName("java.io.BufferedReader");
  202. userAdminR = Class.forName("java.util.Random");
  203. userAdminRW = Class.forName("java.awt.event.MouseWheelEvent");
  204. undefined = Class.forName("java.io.FileWriter");
  205. }catch(ClassNotFoundException cnf){
  206. System.out.println("CLASS NOT FOUND.");
  207. cnf.printStackTrace();
  208. }
  209. //test User
  210. auth.setCurrentUser( auth.getUser("testuser1") );
  211. assertTrue(instance.isAuthorizedForData("read", userRW));
  212. assertFalse(instance.isAuthorizedForData("read", undefined));
  213. assertFalse(instance.isAuthorizedForData("write", undefined));
  214. assertFalse(instance.isAuthorizedForData("read", userW));
  215. assertFalse(instance.isAuthorizedForData("read", adminRW));
  216. assertTrue(instance.isAuthorizedForData("write", userRW));
  217. assertTrue(instance.isAuthorizedForData("write", userW));
  218. assertFalse(instance.isAuthorizedForData("write", anyR));
  219. assertTrue(instance.isAuthorizedForData("read", anyR));
  220. assertTrue(instance.isAuthorizedForData("read", userAdminR));
  221. assertTrue(instance.isAuthorizedForData("write", userAdminRW));
  222. //test Admin
  223. auth.setCurrentUser( auth.getUser("testuser2") );
  224. assertTrue(instance.isAuthorizedForData("read", adminRW));
  225. assertFalse(instance.isAuthorizedForData("read", undefined));
  226. assertFalse(instance.isAuthorizedForData("write", undefined));
  227. assertFalse(instance.isAuthorizedForData("read", userRW));
  228. assertTrue(instance.isAuthorizedForData("write", adminRW));
  229. assertFalse(instance.isAuthorizedForData("write", anyR));
  230. assertTrue(instance.isAuthorizedForData("read", anyR));
  231. assertTrue(instance.isAuthorizedForData("read", userAdminR));
  232. assertTrue(instance.isAuthorizedForData("write", userAdminRW));
  233. //test User/Admin
  234. auth.setCurrentUser( auth.getUser("testuser3") );
  235. assertTrue(instance.isAuthorizedForData("read", userRW));
  236. assertFalse(instance.isAuthorizedForData("read", undefined));
  237. assertFalse(instance.isAuthorizedForData("write", undefined));
  238. assertFalse(instance.isAuthorizedForData("read", userW));
  239. assertTrue(instance.isAuthorizedForData("read", adminR));
  240. assertTrue(instance.isAuthorizedForData("write", userRW));
  241. assertTrue(instance.isAuthorizedForData("write", userW));
  242. assertFalse(instance.isAuthorizedForData("write", anyR));
  243. assertTrue(instance.isAuthorizedForData("read", anyR));
  244. assertTrue(instance.isAuthorizedForData("read", userAdminR));
  245. assertTrue(instance.isAuthorizedForData("write", userAdminRW));
  246. try {
  247. instance.assertAuthorizedForData("read", userRW);
  248. instance.assertAuthorizedForData( "write", adminR );
  249. fail();
  250. } catch ( AccessControlException e ) {
  251. // expected
  252. }
  253. }
  254. /**
  255. * Test of isAuthorizedForFile method, of class
  256. * org.owasp.esapi.AccessController.
  257. */
  258. public void testIsAuthorizedForFile() {
  259. System.out.println("isAuthorizedForFile");
  260. AccessController instance = ESAPI.accessController();
  261. Authenticator auth = ESAPI.authenticator();
  262. auth.setCurrentUser( auth.getUser("testuser1") );
  263. assertTrue(instance.isAuthorizedForFile("/Dir/File1"));
  264. assertFalse(instance.isAuthorizedForFile("/Dir/File2"));
  265. assertTrue(instance.isAuthorizedForFile("/Dir/File3"));
  266. assertFalse(instance.isAuthorizedForFile("/Dir/ridiculous"));
  267. auth.setCurrentUser( auth.getUser("testuser2") );
  268. assertFalse(instance.isAuthorizedForFile("/Dir/File1"));
  269. assertTrue(instance.isAuthorizedForFile("/Dir/File2"));
  270. assertTrue(instance.isAuthorizedForFile("/Dir/File4"));
  271. assertFalse(instance.isAuthorizedForFile("/Dir/ridiculous"));
  272. auth.setCurrentUser( auth.getUser("testuser3") );
  273. assertTrue(instance.isAuthorizedForFile("/Dir/File1"));
  274. assertTrue(instance.isAuthorizedForFile("/Dir/File2"));
  275. assertFalse(instance.isAuthorizedForFile("/Dir/File5"));
  276. assertFalse(instance.isAuthorizedForFile("/Dir/ridiculous"));
  277. try {
  278. instance.assertAuthorizedForFile("/Dir/File1");
  279. instance.assertAuthorizedForFile( "/Dir/File6" );
  280. fail();
  281. } catch ( AccessControlException e ) {
  282. // expected
  283. }
  284. }
  285. /**
  286. * Test of isAuthorizedForService method, of class
  287. * org.owasp.esapi.AccessController.
  288. */
  289. public void testIsAuthorizedForService() {
  290. System.out.println("isAuthorizedForService");
  291. AccessController instance = ESAPI.accessController();
  292. Authenticator auth = ESAPI.authenticator();
  293. auth.setCurrentUser( auth.getUser("testuser1") );
  294. assertTrue(instance.isAuthorizedForService("/services/ServiceA"));
  295. assertFalse(instance.isAuthorizedForService("/services/ServiceB"));
  296. assertTrue(instance.isAuthorizedForService("/services/ServiceC"));
  297. assertFalse(instance.isAuthorizedForService("/test/ridiculous"));
  298. auth.setCurrentUser( auth.getUser("testuser2") );
  299. assertFalse(instance.isAuthorizedForService("/services/ServiceA"));
  300. assertTrue(instance.isAuthorizedForService("/services/ServiceB"));
  301. assertFalse(instance.isAuthorizedForService("/services/ServiceF"));
  302. assertFalse(instance.isAuthorizedForService("/test/ridiculous"));
  303. auth.setCurrentUser( auth.getUser("testuser3") );
  304. assertTrue(instance.isAuthorizedForService("/services/ServiceA"));
  305. assertTrue(instance.isAuthorizedForService("/services/ServiceB"));
  306. assertFalse(instance.isAuthorizedForService("/services/ServiceE"));
  307. assertFalse(instance.isAuthorizedForService("/test/ridiculous"));
  308. try {
  309. instance.assertAuthorizedForService("/services/ServiceD");
  310. instance.assertAuthorizedForService( "/test/ridiculous" );
  311. fail();
  312. } catch ( AccessControlException e ) {
  313. // expected
  314. }
  315. }
  316. }