From 8ab8a2cb727880375c156d09a63f05f797e127fe Mon Sep 17 00:00:00 2001 From: wassa Date: Fri, 13 Dec 2002 23:23:27 +0000 Subject: [PATCH] Implemented Regex ARP Match function and related tests. Fixed log statement in Resource Tree Match function. git-svn-id: https://subversion.switch.ch/svn/shibboleth/java-idp/trunk@373 ab3bd59b-922f-494d-bb5f-6f0a3c29deca --- .../middleware/shibboleth/aa/arp/ArpTests.java | 61 +++++++++++++++++++- .../aa/arp/provider/RegexMatchFunction.java | 20 ++++++- .../aa/arp/provider/ResourceTreeMatchFunction.java | 4 +- 3 files changed, 78 insertions(+), 7 deletions(-) diff --git a/src/edu/internet2/middleware/shibboleth/aa/arp/ArpTests.java b/src/edu/internet2/middleware/shibboleth/aa/arp/ArpTests.java index 16cc4da..fc9259a 100755 --- a/src/edu/internet2/middleware/shibboleth/aa/arp/ArpTests.java +++ b/src/edu/internet2/middleware/shibboleth/aa/arp/ArpTests.java @@ -145,9 +145,12 @@ public class ArpTests extends TestCase { assertNotNull( "ArpEngine did not properly load the Resource Tree SHAR function.", resourceTreeFunction); + MatchFunction regexFunction = + ArpEngine.lookupMatchFunction(new URI("urn:mace:shibboleth:arp:matchFunction:regexMatch")); + assertNotNull("ArpEngine did not properly load the Regex function.", regexFunction); /* - * Test the Exact SHAR function + * Test the Exact SHAR function (requester) */ assertTrue( @@ -169,7 +172,7 @@ public class ArpTests extends TestCase { } /* - * Test the Resource Tree function + * Test the Resource Tree function (resource) */ URL requestURL1 = new URL("http://www.example.edu/test/"); @@ -232,6 +235,60 @@ public class ArpTests extends TestCase { //This is supposed to fail } + /* + * Test the Regex function (requester & resource) + */ + + //Try requester regexes + assertTrue( + "Regex function: false negative", + regexFunction.match("^shar\\.example\\.edu$", "shar.example.edu")); + assertTrue( + "Regex function: false negative", + regexFunction.match("^.*\\.example\\.edu$", "shar.example.edu")); + assertTrue( + "Regex function: false negative", + regexFunction.match("^shar[1-9]?\\.example\\.edu$", "shar1.example.edu")); + assertTrue("Regex function: false negative", regexFunction.match(".*\\.edu", "shar.example.edu")); + assertTrue( + "Regex function: false positive", + !regexFunction.match("^shar[1-9]\\.example\\.edu$", "shar.example.edu")); + assertTrue( + "Regex function: false positive", + !regexFunction.match("^shar\\.example\\.edu$", "www.example.edu")); + assertTrue( + "Regex function: false positive", + !regexFunction.match("^shar\\.example\\.edu$", "www.example.com")); + + //Try resource regexes + assertTrue( + "Regex function: false negative", + regexFunction.match("^http://www\\.example\\.edu/.*$", requestURL1)); + assertTrue( + "Regex function: false negative", + regexFunction.match("^http://www\\.example\\.edu/.*$", requestURL2)); + assertTrue( + "Regex function: false negative", + regexFunction.match("^http://.*\\.example\\.edu/.*$", requestURL2)); + assertTrue( + "Regex function: false negative", + regexFunction.match("^https?://.*\\.example\\.edu/.*$", requestURL2)); + assertTrue("Regex function: false negative", regexFunction.match(".*", requestURL2)); + assertTrue( + "Regex function: false positive", + !regexFunction.match("^https?://.*\\.example\\.edu/$", requestURL2)); + assertTrue( + "Regex function: false positive", + !regexFunction.match("^https?://www\\.example\\.edu/test/$", requestURL3)); + + //Make sure we properly handle bad input + try { + regexFunction.match(null, null); + fail("Regex function seems to take improper input without throwing an exception."); + } catch (ArpException ie) { + //This is supposed to fail + } + } catch (ArpException e) { fail("Encountered a problem loading match function: " + e); } catch (URISyntaxException e) { diff --git a/src/edu/internet2/middleware/shibboleth/aa/arp/provider/RegexMatchFunction.java b/src/edu/internet2/middleware/shibboleth/aa/arp/provider/RegexMatchFunction.java index 2c1f19d..8de03d1 100755 --- a/src/edu/internet2/middleware/shibboleth/aa/arp/provider/RegexMatchFunction.java +++ b/src/edu/internet2/middleware/shibboleth/aa/arp/provider/RegexMatchFunction.java @@ -49,16 +49,22 @@ package edu.internet2.middleware.shibboleth.aa.arp.provider; +import java.net.URL; + import edu.internet2.middleware.shibboleth.aa.arp.MatchFunction; import edu.internet2.middleware.shibboleth.aa.arp.MatchingException; +import org.apache.log4j.Logger; + /** - * Match function implementaiton that does regular expression matching on both - * requesters and resources. + * Match function implementaiton that does regular expression matching on both requesters + * and resources. * * @author Walter Hoehn (wassa@columbia.edu) */ public class RegexMatchFunction implements MatchFunction { + private static Logger log = Logger.getLogger(RegexMatchFunction.class.getName()); + /** * @see edu.internet2.middleware.shibboleth.aa.arp.MatchFunction#match(Object, * Object) @@ -66,6 +72,14 @@ public class RegexMatchFunction implements MatchFunction { public boolean match(Object arpComponent, Object requestComponent) throws MatchingException { - return false; + if (!(arpComponent instanceof String) && !(requestComponent instanceof String || requestComponent instanceof URL)) { + log.error("Invalid use of ARP matching function (RegexMatchFunction)."); + throw new MatchingException( + "Invalid use of ARP matching function (RegexMatchFunction)."); + } + if (requestComponent instanceof URL) { + return ((URL) requestComponent).toString().matches((String) arpComponent); + } + return ((String) requestComponent).matches((String) arpComponent); } } diff --git a/src/edu/internet2/middleware/shibboleth/aa/arp/provider/ResourceTreeMatchFunction.java b/src/edu/internet2/middleware/shibboleth/aa/arp/provider/ResourceTreeMatchFunction.java index f3f48be..f48f4f8 100755 --- a/src/edu/internet2/middleware/shibboleth/aa/arp/provider/ResourceTreeMatchFunction.java +++ b/src/edu/internet2/middleware/shibboleth/aa/arp/provider/ResourceTreeMatchFunction.java @@ -84,9 +84,9 @@ public class ResourceTreeMatchFunction implements MatchFunction { arpURL = new URL((String) arpComponent); } catch (MalformedURLException e) { log.error( - "Invalid use of ARP matching function (ExacthSharMatchFunction): ARP Component is not a URL."); + "Invalid use of ARP matching function (ResourceTreeMatchFunction): ARP Component is not a URL."); throw new MatchingException( - "Invalid use of ARP matching function (ExacthSharMatchFunction)."); + "Invalid use of ARP matching function (ResourceTreeMatchFunction)."); } if (!matchProtocol(arpURL, (URL) requestComponent)) { -- 1.7.10.4