3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest/*
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest * The contents of this file are subject to the terms of the Common Development and
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest * Distribution License (the License). You may not use this file except in compliance with the
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest * License.
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest *
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest * specific language governing permission and limitations under the License.
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest *
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest * When distributing Covered Software, include this CDDL Header Notice in each file and include
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest * Header, with the fields enclosed by brackets [] replaced by your own identifying
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest * information: "Portions copyright [year] [name of copyright owner]".
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest *
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest * Copyright 2013 ForgeRock Inc.
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest */
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrestpackage com.sun.identity.shared.whitelist;
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrestimport org.fest.assertions.ComparisonFailureFactory;
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrestimport org.testng.annotations.BeforeMethod;
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrestimport org.testng.annotations.Test;
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrestimport static org.fest.assertions.Assertions.assertThat;
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest/**
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest * Unit test for URLResourceName.
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest *
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest * @author andrew.forrest@forgerock.com
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest */
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrestpublic class URLResourceNameTest {
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest private URLResourceName resourceName;
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest @BeforeMethod
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest public void setUp() {
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest resourceName = new URLResourceName();
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest }
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest /**
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest * Tests that the normalisation process adheres to the expected rules.
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest */
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest @Test
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest public void verifyPortHandling() {
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest // Previous ports are maintained.
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest assertNormalisation("http://www.example.com:80/hello", "http://www.example.com:80/hello");
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest assertNormalisation("http://www.example.com:12345/hello", "http://www.example.com:12345/hello");
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest // Verify default ports are added where appropriate.
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest assertNormalisation("http://www.example.com/hello", "http://www.example.com:80/hello");
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest assertNormalisation("https://www.example.com/hello", "https://www.example.com:443/hello");
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest assertNormalisation("protocol://www.example.com/hello", "protocol://www.example.com/hello");
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest // With wildcards.
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest assertNormalisation("http://www.example.com:80/hello*", "http://www.example.com:80/hello*");
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest assertNormalisation("http://www.example*:80/hello", "http://www.example*:80/hello");
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest assertNormalisation("http*://www.example.com:80/hello", "http*://www.example.com:80/hello");
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest // Special cases: Due to the wildcard positions it is not possible to determine the port.
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest assertNormalisation("http*://www.example.com/hello", "http*://www.example.com:*/hello");
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest assertNormalisation("https*://www.example.com/hello", "https*://www.example.com:*/hello");
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest assertNormalisation("protocol*://www.example.com/hello", "protocol*://www.example.com/hello");
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest assertNormalisation("http://www.example.com:*/hello", "http://www.example.com:*/hello");
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest }
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest /**
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest * Convenient assertion method for verifying normalisation of resources.
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest *
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest * @param untreatedResource
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest * The resource before it's been normalised.
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest * @param treatedResource
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest * The expected resource after it's been normalised.
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest */
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest private void assertNormalisation(String untreatedResource, String treatedResource) {
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest try {
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest assertThat(resourceName.canonicalize(untreatedResource)).isEqualTo(treatedResource);
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest } catch (Exception e) {
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest String message = "Normalisation failed: " + e.getMessage();
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest throw ComparisonFailureFactory.comparisonFailure(message, treatedResource, "");
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest }
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest }
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest
3e9a58c7ab182327c2fdd169ed1d70dcba846fe4Andrew Forrest}