summaryrefslogtreecommitdiff
path: root/test/java
diff options
context:
space:
mode:
Diffstat (limited to 'test/java')
-rw-r--r--test/java/net/yacy/http/AdminSecurityTest.java37
-rw-r--r--test/java/net/yacy/http/InetPathAccessHandlerTest.java12
2 files changed, 49 insertions, 0 deletions
diff --git a/test/java/net/yacy/http/AdminSecurityTest.java b/test/java/net/yacy/http/AdminSecurityTest.java
index 4fd8abb58..196eae3ea 100644
--- a/test/java/net/yacy/http/AdminSecurityTest.java
+++ b/test/java/net/yacy/http/AdminSecurityTest.java
@@ -106,4 +106,41 @@ public class AdminSecurityTest {
// a request from localhost referred by a remote page is not a localhost access
Assert.assertFalse(AdminSecurity.isLocalhostAccess("127.0.0.1", "example.org"));
}
+
+ /** Test the complete request-level policy used by the container adapter. */
+ @Test
+ public void testAdminAccessPolicy() {
+ final String user = "admin";
+ final String hash = AdminSecurity.calcHash(user + ":secret");
+ final AdminAccessPolicy localAllowed = new AdminAccessPolicy(
+ false, false, true, true, user, hash);
+
+ Assert.assertEquals(AdminAccessPolicy.Decision.PUBLIC,
+ localAllowed.decide("/index.html", "192.0.2.1", null, null));
+ Assert.assertEquals(AdminAccessPolicy.Decision.ADMIN_REQUIRED,
+ localAllowed.decide("/Settings_p.html", "192.0.2.1", null, null));
+ Assert.assertEquals(AdminAccessPolicy.Decision.LOCAL_BYPASS,
+ localAllowed.decide("/Settings_p.html", "127.0.0.1", null, null));
+ Assert.assertEquals(AdminAccessPolicy.Decision.ADMIN_REQUIRED,
+ localAllowed.decide("/Settings_p.html", "127.0.0.1", "https://example.org/", null));
+
+ final AdminAccessPolicy loginRequired = new AdminAccessPolicy(
+ false, false, true, false, user, hash);
+ Assert.assertEquals(AdminAccessPolicy.Decision.ADMIN_REQUIRED,
+ loginRequired.decide("/Settings_p.html", "127.0.0.1", null, null));
+ final String lazyAuth = "Basic " + Base64Order.standardCoder.encodeString(user + ":" + hash);
+ Assert.assertEquals(AdminAccessPolicy.Decision.LOCAL_BYPASS,
+ loginRequired.decide("/Settings_p.html", "127.0.0.1", null, lazyAuth));
+ }
+
+ /** The credential context is request-bound and fails closed after cleanup. */
+ @Test
+ public void testAdminAuthenticationContext() {
+ AdminAuthenticationContext.clear();
+ Assert.assertFalse(AdminAuthenticationContext.isLocalhostRequest());
+ AdminAuthenticationContext.setSocketPeerIp("127.0.0.1");
+ Assert.assertTrue(AdminAuthenticationContext.isLocalhostRequest());
+ AdminAuthenticationContext.clear();
+ Assert.assertFalse(AdminAuthenticationContext.isLocalhostRequest());
+ }
}
diff --git a/test/java/net/yacy/http/InetPathAccessHandlerTest.java b/test/java/net/yacy/http/InetPathAccessHandlerTest.java
index 5dfdf8a4f..641ad8bf1 100644
--- a/test/java/net/yacy/http/InetPathAccessHandlerTest.java
+++ b/test/java/net/yacy/http/InetPathAccessHandlerTest.java
@@ -32,6 +32,18 @@ import org.junit.Test;
*/
public class InetPathAccessHandlerTest {
+ @Test
+ public void testPortableRuleParsing() {
+ final InetPathAccessRule addressOnly = InetPathAccessRule.parse("192.168.1.0/24");
+ Assert.assertEquals("192.168.1.0/24", addressOnly.addressPattern());
+ Assert.assertEquals("/*", addressOnly.pathPattern());
+
+ final InetPathAccessRule addressAndPath = InetPathAccessRule.parse("127.0.0.1|/api/*");
+ Assert.assertEquals("127.0.0.1", addressAndPath.addressPattern());
+ Assert.assertEquals("/api/*", addressAndPath.pathPattern());
+ Assert.assertEquals("127.0.0.1|/api/*", addressAndPath.asJettyPattern());
+ }
+
/**
* Check the handler allow the given ip/path pairs.
*