1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.web

File TempResourceActionTest.java

 

Code metrics

0
56
12
1
218
129
12
0.21
4.67
12
1

Classes

Class Line # Actions
TempResourceActionTest 49 56 0% 12 0
1.0100%
 

Contributing tests

This file is covered by 9 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package com.xpn.xwiki.web;
21   
22    import java.io.File;
23    import java.io.IOException;
24   
25    import javax.servlet.ServletOutputStream;
26   
27    import org.apache.commons.io.FileUtils;
28    import org.junit.Assert;
29    import org.junit.Before;
30    import org.junit.Rule;
31    import org.junit.Test;
32    import org.mockito.Mockito;
33   
34    import org.xwiki.environment.Environment;
35   
36    import com.xpn.xwiki.XWikiContext;
37    import com.xpn.xwiki.XWikiException;
38    import com.xpn.xwiki.test.MockitoOldcoreRule;
39    import com.xpn.xwiki.web.includeservletasstring.BufferOutputStream;
40   
41    import static org.mockito.Mockito.mock;
42    import static org.mockito.Mockito.when;
43   
44    /**
45    * Unit tests for {@link TempResourceAction}.
46    *
47    * @version $Id: 31e0b72cce06eacb2c499dd991bb3585e4c83b27 $
48    */
 
49    public class TempResourceActionTest
50    {
51    @Rule
52    public MockitoOldcoreRule oldcore = new MockitoOldcoreRule();
53   
54    /**
55    * The action being tested.
56    */
57    private TempResourceAction action;
58   
59    /**
60    * The base directory.
61    */
62    private File base;
63   
 
64  9 toggle @Before
65    public void setUp() throws Exception
66    {
67  9 base = new File(getClass().getResource("/").toURI());
68   
69    // Configure Servlet Environment defined in AbstractBridgedComponentTestCase so that it returns a good
70    // temporary directory
71  9 Environment environment = oldcore.getMocker().registerMockComponent(Environment.class);
72  9 when(environment.getTemporaryDirectory()).thenReturn(base);
73   
74  9 action = new TempResourceAction();
75    }
76   
77    /**
78    * Creates an empty file at the specified path.
79    *
80    * @param path the file path
81    * @throws IOException if creating the empty file fails
82    */
 
83  6 toggle private void createEmptyFile(String path) throws IOException
84    {
85  6 File emptyFile = new File(base, path);
86  6 emptyFile.getParentFile().mkdirs();
87  6 emptyFile.createNewFile();
88  6 emptyFile.deleteOnExit();
89    }
90   
91    /**
92    * Creates a file at the specified path, with the specified content.
93    *
94    * @param path the file path
95    * @throws IOException if creating the empty file fails
96    */
 
97  1 toggle private void createFile(String path, String content) throws IOException
98    {
99  1 File file = new File(base, path);
100  1 file.getParentFile().mkdirs();
101  1 file.createNewFile();
102  1 file.deleteOnExit();
103  1 FileUtils.write(file, content);
104    }
105   
106    /**
107    * {@link TempResourceAction#getTemporaryFile(String, XWikiContext)} should return {@code null} if the given URI
108    * doesn't match the known pattern.
109    */
 
110  1 toggle @Test
111    public void testGetTemporaryFileForBadURI() throws Exception
112    {
113  1 createEmptyFile("temp/secret.txt");
114  1 Assert.assertNull(action.getTemporaryFile("/xwiki/bin/temp/secret.txt", oldcore.getXWikiContext()));
115    }
116   
117    /**
118    * {@link TempResourceAction#getTemporaryFile(String, XWikiContext)} should prevent access to files outside the
119    * temporary directory by ignoring relative URIs (i.e. which use ".." to move to the parent folder).
120    */
 
121  1 toggle @Test
122    public void testGetTemporaryFileForRelativeURI() throws Exception
123    {
124  1 createEmptyFile("temp/secret.txt");
125  1 Assert.assertNull(action.getTemporaryFile("/xwiki/bin/temp/../../module/secret.txt", oldcore.getXWikiContext()));
126    }
127   
128    /**
129    * Tests {@link TempResourceAction#getTemporaryFile(String, XWikiContext)} when the file is missing.
130    */
 
131  1 toggle @Test
132    public void testGetTemporaryFileMissing() throws Exception
133    {
134  1 Assert.assertFalse(new File(base, "temp/module/xwiki/Space/Page/file.txt").exists());
135  1 Assert.assertNull(action.getTemporaryFile("/xwiki/bin/temp/Space/Page/module/file.txt", oldcore.getXWikiContext()));
136    }
137   
138    /**
139    * Tests {@link TempResourceAction#getTemporaryFile(String, XWikiContext)} when the file is present.
140    */
 
141  1 toggle @Test
142    public void testGetTemporaryFile() throws Exception
143    {
144  1 oldcore.getXWikiContext().setWikiId("wiki");
145  1 createEmptyFile("temp/module/wiki/Space/Page/file.txt");
146  1 Assert.assertNotNull(action.getTemporaryFile("/xwiki/bin/temp/Space/Page/module/file.txt", oldcore.getXWikiContext()));
147    }
148   
149    /**
150    * Tests {@link TempResourceAction#getTemporaryFile(String, XWikiContext)} when the URL is over encoded.
151    */
 
152  1 toggle @Test
153    public void testGetTemporaryFileForOverEncodedURL() throws Exception
154    {
155  1 createEmptyFile("temp/officeviewer/xwiki/Sp*ace/Pa-ge/presentation.odp/presentation-slide0.jpg");
156  1 Assert.assertNotNull(action.getTemporaryFile(
157    "/xwiki/bin/temp/Sp%2Aace/Pa%2Dge/officeviewer/presentation.odp/presentation-slide0.jpg", oldcore.getXWikiContext()));
158    }
159   
160    /**
161    * Tests {@link TempResourceAction#getTemporaryFile(String, XWikiContext)} when the URL is partially decoded. This
162    * can happen for instance when XWiki is behind Apache's {@code mode_proxy} with {@code nocanon} option disabled.
163    */
 
164  1 toggle @Test
165    public void testGetTemporaryFileForPartiallyDecodedURL() throws Exception
166    {
167  1 createEmptyFile("temp/officeviewer/xwiki/Space/Page/"
168    + "attach%3Axwiki%3ASpace.Page%40pres%2Fentation.odp/13/presentation-slide0.jpg");
169  1 Assert.assertNotNull(action.getTemporaryFile("/xwiki/bin/temp/Space/Page/officeviewer/"
170    + "attach:xwiki:Space.Page@pres%2Fentation.odp/13/presentation-slide0.jpg", oldcore.getXWikiContext()));
171    }
172   
 
173  1 toggle @Test
174    public void renderNormalBehavior() throws Exception
175    {
176  1 XWikiRequest request = mock(XWikiRequest.class);
177  1 XWikiResponse response = mock(XWikiResponse.class);
178  1 BufferOutputStream out = new BufferOutputStream();
179   
180  1 oldcore.getXWikiContext().setRequest(request);
181  1 oldcore.getXWikiContext().setResponse(response);
182  1 when(request.getRequestURI()).thenReturn("/xwiki/bin/temp/Space/Page/module/file.txt");
183  1 when(response.getOutputStream()).thenReturn(out);
184  1 oldcore.getXWikiContext().setWikiId("wiki");
185  1 createFile("temp/module/wiki/Space/Page/file.txt", "Hello World!");
186  1 action.render(oldcore.getXWikiContext());
187  1 Assert.assertArrayEquals("Hello World!".getBytes(), out.getContentsAsByteArray());
188  1 Mockito.verify(response, Mockito.never()).addHeader("Content-disposition", "attachment; filename*=utf-8''file.txt");
189    }
190   
 
191  1 toggle @Test
192    public void renderWithForceDownload() throws Exception
193    {
194  1 XWikiRequest request = mock(XWikiRequest.class);
195  1 XWikiResponse response = mock(XWikiResponse.class);
196  1 oldcore.getXWikiContext().setRequest(request);
197  1 oldcore.getXWikiContext().setResponse(response);
198  1 when(request.getRequestURI()).thenReturn("/xwiki/bin/temp/Space/Page/module/file.txt");
199  1 when(request.getParameter("force-download")).thenReturn("1");
200  1 when(response.getOutputStream()).thenReturn(mock(ServletOutputStream.class));
201  1 oldcore.getXWikiContext().setWikiId("wiki");
202  1 createEmptyFile("temp/module/wiki/Space/Page/file.txt");
203  1 action.render(oldcore.getXWikiContext());
204  1 Mockito.verify(response).addHeader("Content-disposition", "attachment; filename*=utf-8''file.txt");
205    }
206   
 
207  1 toggle @Test(expected=XWikiException.class)
208    public void renderWithInvalidPathThrowsException() throws XWikiException
209    {
210  1 XWikiRequest request = mock(XWikiRequest.class);
211  1 XWikiResponse response = mock(XWikiResponse.class);
212  1 oldcore.getXWikiContext().setRequest(request);
213  1 oldcore.getXWikiContext().setResponse(response);
214  1 when(request.getRequestURI()).thenReturn("/xwiki/bin/temp/Space/Page/module/nosuchfile.txt");
215  1 oldcore.getXWikiContext().setWikiId("wiki");
216  1 action.render(oldcore.getXWikiContext());
217    }
218    }