KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > staticexport > TestSecure


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/staticexport/TestSecure.java,v $
3  * Date : $Date: 2005/07/06 11:40:29 $
4  * Version: $Revision: 1.9 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.staticexport;
33
34 import org.opencms.file.CmsFile;
35 import org.opencms.file.CmsObject;
36 import org.opencms.file.CmsProject;
37 import org.opencms.file.CmsRequestContext;
38 import org.opencms.file.types.CmsResourceTypeFolder;
39 import org.opencms.main.OpenCms;
40 import org.opencms.site.CmsSiteManager;
41 import org.opencms.test.OpenCmsTestCase;
42 import org.opencms.test.OpenCmsTestProperties;
43 import org.opencms.xml.page.CmsXmlPage;
44 import org.opencms.xml.page.CmsXmlPageFactory;
45
46 import java.util.Locale JavaDoc;
47
48 import junit.extensions.TestSetup;
49 import junit.framework.Test;
50 import junit.framework.TestSuite;
51
52 /**
53  * Tests for the XML page that require a running OpenCms system.<p>
54  *
55  * @author Alexander Kandzior
56  *
57  * @version $Revision: 1.9 $
58  *
59  * @since 6.0.0
60  */

61 public class TestSecure extends OpenCmsTestCase {
62
63     /** the prefix of the normal server. */
64     private static final String JavaDoc SERVER_NORMAL = "http://localhost";
65
66     /** the prefix of the secure server. */
67     private static final String JavaDoc SERVER_SECURE = "https://localhost";
68
69     /**
70      * Default JUnit constructor.<p>
71      *
72      * @param arg0 JUnit parameters
73      */

74     public TestSecure(String JavaDoc arg0) {
75
76         super(arg0);
77     }
78
79     /**
80      * Test suite for this test class.<p>
81      *
82      * @return the test suite
83      */

84     public static Test suite() {
85
86         OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH);
87
88         TestSuite suite = new TestSuite();
89         suite.setName(TestSecure.class.getName());
90
91         suite.addTest(new TestSecure("testSecureServerConfig"));
92         suite.addTest(new TestSecure("testLinkInXmlPage"));
93         suite.addTest(new TestSecure("testSecureLinkProcessing"));
94         suite.addTest(new TestSecure("testSetupSecondSite"));
95
96         TestSetup wrapper = new TestSetup(suite) {
97
98             protected void setUp() {
99
100                 setupOpenCms("simpletest", "/sites/default/");
101             }
102
103             protected void tearDown() {
104
105                 removeOpenCms();
106             }
107
108         };
109
110         return wrapper;
111     }
112
113     /**
114      * Test if links in XML-pages are converted to html-links with correct server prefixes.<p>
115      *
116      * make a link from one site to another site, and check, if the correct prefixes are set
117      * @throws Exception in case something goes wrong
118      */

119     public void testLinkInXmlPage() throws Exception JavaDoc {
120
121         CmsObject cms = getCmsObject();
122
123         echo("Testing security of links");
124         CmsProject onlineProject = cms.readProject("Online");
125         cms.getRequestContext().setCurrentProject(onlineProject);
126         cms.getRequestContext().setSiteRoot("/sites/default");
127         String JavaDoc filename = "/folder1/page1.html";
128
129         CmsFile file = cms.readFile(filename);
130         CmsXmlPage page = CmsXmlPageFactory.unmarshal(cms, file);
131
132         String JavaDoc element = "body";
133
134         String JavaDoc text = page.getStringValue(cms, element, Locale.ENGLISH);
135         assertTrue(text.indexOf(SERVER_SECURE) != -1);
136
137     }
138
139     /**
140      * Test if links to secure pages are secure.<p>
141      *
142      * Description of the issue:
143      * links from secure pages to insecure pages must have a normal server prefix.<p>
144      * links from normal pages to secure pages must have a secure server prefix.<p>
145      * links from secure pages to secure pages have no server prefix.<p>
146      *
147      * @throws Exception in case something goes wrong
148      */

149     public void testSecureLinkProcessing() throws Exception JavaDoc {
150
151         CmsObject cms = getCmsObject();
152         CmsProject onlineProject = cms.readProject("Online");
153         cms.getRequestContext().setCurrentProject(onlineProject);
154         CmsRequestContext requestContext = cms.getRequestContext();
155         CmsLinkManager linkManager = OpenCms.getLinkManager();
156
157         requestContext.setUri("/folder1/page1.html");
158
159         // link from normal to secure document need to have secure server prefix
160
assertHasSecurePrefix(linkManager.substituteLink(cms, "/folder1/page4.html"));
161
162         // image links must be without server prefix
163
assertHasNoPrefix(linkManager.substituteLink(cms, "/folder1/image1.gif"));
164         requestContext.setUri("/folder1/page4.html");
165
166         // link from secure to normal document need to have normal server prefix
167
assertHasNormalPrefix(linkManager.substituteLink(cms, "/folder1/page3.html"));
168
169         // image links need to be relative
170
assertHasNoPrefix(linkManager.substituteLink(cms, "/folder1/image1.gif"));
171
172         // in offline mode, no server prefixes must be set
173
cms.getRequestContext().setCurrentProject(cms.readProject("Offline"));
174         assertHasNoPrefix(linkManager.substituteLink(cms, "/folder1/page3.html"));
175
176     }
177
178     /**
179      * Test if the site configuration succeeded.<p>
180      *
181      * @throws Exception in case something goes wrong
182      */

183     public void testSecureServerConfig() throws Exception JavaDoc {
184
185         CmsObject cms = getCmsObject();
186         echo("Testing the configuration");
187
188         // test, if a secure server exists for the current site
189
assertTrue(CmsSiteManager.getCurrentSite(cms).hasSecureServer());
190
191         // test, if the secure server is 'https:www.mysite.com'
192
assertEquals(SERVER_SECURE, CmsSiteManager.getCurrentSite(cms).getSecureUrl());
193
194     }
195
196     /**
197      * Test if links in XML-pages are converted to html-links with correct server prefixes.<p>
198      *
199      * make a link from one site to another site, and check, if the correct prefixes are set
200      * @throws Exception in case something goes wrong
201      */

202     public void testSetupSecondSite() throws Exception JavaDoc {
203
204         CmsObject cms = getCmsObject();
205         // the second site sould be correctly initialized
206
// what has to be done is to create the site root folder of the testsite
207
cms.getRequestContext().setSiteRoot("/");
208         cms.createResource("/sites/testsite/", CmsResourceTypeFolder.getStaticTypeId());
209         cms.unlockResource("/sites/testsite/");
210
211         // copy one page to the new site
212
cms.copyResource("/sites/default/folder1/page1.html", "/sites/testsite/page1.html");
213         cms.unlockResource("/sites/testsite/page1.html");
214
215         cms.publishProject();
216
217         CmsProject onlineProject = cms.readProject("Online");
218         cms.getRequestContext().setCurrentProject(onlineProject);
219         cms.getRequestContext().setSiteRoot("/sites/testsite/");
220
221         // read the page
222
CmsFile file = cms.readFile("page1.html");
223         CmsXmlPage page = CmsXmlPageFactory.unmarshal(cms, file);
224
225         String JavaDoc element = "body";
226
227         // the converted HTML must have links with normal and secure server prefixes of the first site
228
String JavaDoc text = page.getStringValue(cms, element, Locale.ENGLISH);
229
230         System.out.println(text);
231
232         assertTrue(text.indexOf(SERVER_SECURE) != -1);
233         assertTrue(text.indexOf(SERVER_NORMAL) != -1);
234     }
235
236     /**
237      * Checks that the link begins with a slash.<p>
238      *
239      * @param link the link to test
240      */

241     private void assertHasNoPrefix(String JavaDoc link) {
242
243         assertTrue(link.startsWith("/"));
244     }
245
246     /**
247      * Checks that the link uses a non secure protocoll.<p>
248      *
249      * @param link the link to check
250      */

251     private void assertHasNormalPrefix(String JavaDoc link) {
252
253         assertTrue(link.startsWith(SERVER_NORMAL));
254     }
255
256     /**
257      * Checks that the link uses a secure protocoll.<p>
258      *
259      * @param link the link to check
260      */

261     private void assertHasSecurePrefix(String JavaDoc link) {
262
263         assertTrue(link.startsWith(SERVER_SECURE));
264     }
265
266 }
Popular Tags