001 /*
002 Copyright (c) 1996-2012, Damon Hart-Davis
003 All rights reserved.
004
005 Redistribution and use in source and binary forms, with or without
006 modification, are permitted provided that the following conditions are
007 met:
008
009 * Redistributions of source code must retain the above copyright
010 notice, this list of conditions and the following disclaimer.
011
012 * Redistributions in binary form must reproduce the above copyright
013 notice, this list of conditions and the following disclaimer in the
014 documentation and/or other materials provided with the
015 distribution.
016
017 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
018 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
019 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
020 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
021 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
022 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
023 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
024 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
025 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
026 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
027 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028 */
029
030 /*
031 * Created by IntelliJ IDEA.
032 * User: Administrator
033 * Date: 28-Dec-02
034 * Time: 22:24:51
035 */
036 package org.hd.d.pg2k.test.dev;
037
038 import junit.framework.TestCase;
039
040 import org.hd.d.pg2k.svrCore.CoreConsts;
041 import org.hd.d.pg2k.svrCore.HostUtils;
042 import org.hd.d.pg2k.webSvr.virtualHosts.VirtualHosts;
043 import org.hd.d.pg2k.webSvr.virtualHosts.VirtualHosts.VirtualHost;
044
045 /**Test of various virtual host features.
046 */
047 public final class VirtualHostTest extends TestCase
048 {
049 public VirtualHostTest(final String name)
050 {
051 super(name);
052 }
053
054 /**Test canonicalisation of HTTP host names.
055 * This checks for:
056 * <ul>
057 * <li>Conversion to a single case.
058 * <li>Stripping of superfluous trailing dots.
059 * <li>Stripping of prefixes such as "www.", "master." and "mirror-*.".
060 * </ul>
061 */
062 public static void testHostnameCanonicalisation()
063 {
064 // canonical form we are aiming for.
065 final String canonical = "gallery.hd.org";
066
067 // Items that should reduce to the canonical form.
068 final String good[] =
069 {
070 canonical,
071 canonical.toUpperCase(),
072 "www." + canonical + ".",
073 "master." + canonical + ".",
074 "mirror-uk-th9." + canonical + ".",
075 "mirror-us-ga1." + canonical + ".",
076 "www." + canonical + "....",
077 canonical + "...",
078 "wWw.GaLLERy.HD.ORG."
079 };
080 for(int i = good.length; --i >= 0; )
081 {
082 assertEquals("Should have reduced to canonical form",
083 canonical,
084 HostUtils.normaliseVirtualHostName(good[i]));
085 }
086
087 // Items that should NOT reduce to the canonical form...
088 final String bad[] =
089 {
090 "", ".",
091 "random.hd.org",
092 "someother.gallery.hd.org",
093 "wwwgallery.hd.org",
094 "ibm.com"
095 };
096 for(int i = bad.length; --i >= 0; )
097 {
098 try
099 {
100 assertNotSame("Should NOT have reduced to canonical form",
101 canonical,
102 HostUtils.normaliseVirtualHostName(bad[i]));
103 }
104 catch(final IllegalArgumentException e) { } // Allowed to reject them outright...
105 }
106 }
107
108 /**Check that lookups by name and/or URI yield the correct VirtualHost details. */
109 public static void testVirtualHostLookupDetails()
110 {
111 // Ensure that the host map is not empty.
112 assertTrue("There must be at least one good host name",
113 VirtualHosts.hostDetails.size() > 0);
114
115 // Make sure that if we look up a bogus hostname
116 // we get a null.
117 assertNull("Looking up a bogus hostname should give null",
118 VirtualHosts.getVirtualHostDetails("bogus.name", null));
119
120 // Make sure that if we look up the default/main hostname
121 // we get the standard default livery.
122 assertSame("Looking up the main hostname should give standard livery details",
123 VirtualHosts.VIRTUAL_HOST_DEFAULT,
124 VirtualHosts.getVirtualHostDetails(CoreConsts.MAIN_DATA_HOST, null));
125
126 // Make sure that if we look up non-canonicalised default/main hostname
127 // we get the standard default livery.
128 assertSame("Looking up the main hostname in non-canonical form should give standard livery details",
129 VirtualHosts.VIRTUAL_HOST_DEFAULT,
130 VirtualHosts.getVirtualHostDetails(
131 "www." + CoreConsts.MAIN_DATA_HOST.toUpperCase() + ".",
132 null));
133
134 // Make sure that if we look up "localhost"
135 // we get correct non-null "localhost" VirtualHost details
136 // so that we won't get redirected and can do sensible testing!
137 assertSame("Looking up localhost should give correct non-null livery details",
138 VirtualHosts.VIRTUAL_HOST_LOCALHOST,
139 VirtualHosts.getVirtualHostDetails("localhost", null));
140
141 // Make sure that some critical aliases *can* be looked up with
142 // non-null VirtualHost results...
143 final String testNames[] =
144 {
145 // "aloha-earth.com", "alohaearth.com", "alohaearth.org",
146 "pix.org.za", "photo.org.za", "photos.org.za",
147 "londonpix.org.uk",
148 "localhost"
149 };
150 for(int i = testNames.length; --i >= 0; )
151 {
152 final String tn = testNames[i];
153 final VirtualHost virtualHostDetails = VirtualHosts.getVirtualHostDetails(tn, null);
154 assertNotNull("Must be able to look up main/alias name: " + tn, virtualHostDetails);
155 assertNotNull("String redering must be reasonable", virtualHostDetails.toString());
156 assertFalse("String redering must be reasonable", "".equals(virtualHostDetails.toString()));
157 }
158
159 // // Makes sure that all the canonical VirtualHost entries
160 // // TODO: and all their aliases
161 // // correctly map to themselves, even with de-canonicalisation.
162 // for(final Iterator it = VirtualHosts.canonicalVirtualHosts.iterator(); it.hasNext(); )
163 // {
164 // final VirtualHosts.VirtualHost vh = (VirtualHosts.VirtualHost) it.next();
165 // assertNotNull("No canonicalVirtualHosts entry may be null", vh);
166 //
167 // }
168 }
169
170 /**Test handling/parsing of (lower-cased) mirror names.
171 */
172 public static void testMirrorNameHandling()
173 {
174 // Some bogus non-mirror names/tags.
175 assertFalse(HostUtils.isMirrorName("some.random.non-mirror.name"));
176 assertFalse(HostUtils.isPrimaryMirrorName("some.random.non-mirror.name"));
177 assertFalse(HostUtils.isPrimaryMirrorTag("some"));
178 assertFalse(HostUtils.isPrimaryMirrorTag(null));
179
180 // Primary mirror name/tag (first component ending in '1').
181 assertTrue(HostUtils.isMirrorName("mirror-uk-ww1.suffix"));
182 assertTrue(HostUtils.isPrimaryMirrorName("mirror-uk-ww1.suffix"));
183 assertTrue(HostUtils.isPrimaryMirrorTag("uk-ww1"));
184
185 // Non-primary mirror name/tag (first component NOT ending in '1').
186 assertTrue(HostUtils.isMirrorName("mirror-uk-ww2.suffix"));
187 assertFalse(HostUtils.isPrimaryMirrorName("mirror-uk-ww2.suffix"));
188 assertFalse(HostUtils.isPrimaryMirrorTag("uk-ww2"));
189 }
190 }