1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.web

File TempResourceActionTest.java

 

Code metrics

0
53
12
1
212
133
12
0.23
4.42
12
1

Classes

Class Line # Actions
TempResourceActionTest 56 53 0% 12 8
0.876923187.7%
 

Contributing tests

This file is covered by 8 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.jupiter.api.BeforeEach;
29    import org.junit.jupiter.api.Test;
30    import org.xwiki.tika.internal.TikaUtils;
31   
32    import com.xpn.xwiki.XWikiContext;
33    import com.xpn.xwiki.XWikiException;
34    import com.xpn.xwiki.internal.file.TemporaryFile;
35    import com.xpn.xwiki.test.MockitoOldcore;
36    import com.xpn.xwiki.test.junit5.mockito.InjectMockitoOldcore;
37    import com.xpn.xwiki.test.junit5.mockito.OldcoreTest;
38    import com.xpn.xwiki.web.includeservletasstring.BufferOutputStream;
39   
40    import static org.junit.jupiter.api.Assertions.assertArrayEquals;
41    import static org.junit.jupiter.api.Assertions.assertFalse;
42    import static org.junit.jupiter.api.Assertions.assertNotNull;
43    import static org.junit.jupiter.api.Assertions.assertNull;
44    import static org.junit.jupiter.api.Assertions.assertThrows;
45    import static org.mockito.Mockito.mock;
46    import static org.mockito.Mockito.never;
47    import static org.mockito.Mockito.verify;
48    import static org.mockito.Mockito.when;
49   
50    /**
51    * Unit tests for {@link TempResourceAction}.
52    *
53    * @version $Id: 362c9bc11a1541a8fe7e259c3feafd61ec743c0a $
54    */
55    @OldcoreTest
 
56    public class TempResourceActionTest
57    {
58    @InjectMockitoOldcore
59    private MockitoOldcore oldcore;
60   
61    /**
62    * The action being tested.
63    */
64    private TempResourceAction action;
65   
 
66  8 toggle @BeforeEach
67    public void beforeEach() throws Exception
68    {
69  8 this.oldcore.registerMockEnvironment();
70  8 this.action = new TempResourceAction();
71    }
72   
73    /**
74    * Creates an empty file at the specified path.
75    *
76    * @param path the file path
77    * @throws IOException if creating the empty file fails
78    */
 
79  6 toggle private void createEmptyFile(String path) throws IOException
80    {
81  6 File emptyFile = new File(this.oldcore.getTemporaryDirectory(), path);
82  6 emptyFile.getParentFile().mkdirs();
83  6 emptyFile.createNewFile();
84  6 emptyFile.deleteOnExit();
85    }
86   
87    /**
88    * Creates a file at the specified path, with the specified content.
89    *
90    * @param path the file path
91    * @throws IOException if creating the empty file fails
92    */
 
93  1 toggle private void createFile(String path, String content) throws IOException
94    {
95  1 File file = new File(this.oldcore.getTemporaryDirectory(), path);
96  1 file.getParentFile().mkdirs();
97  1 file.deleteOnExit();
98  1 FileUtils.write(file, content);
99    }
100   
101    /**
102    * {@link TempResourceAction#getTemporaryFile(String, XWikiContext)} should return {@code null} if the given URI
103    * doesn't match the known pattern.
104    */
 
105  1 toggle @Test
106    public void testGetTemporaryFileForBadURI() throws Exception
107    {
108  1 createEmptyFile("temp/secret.txt");
109  1 assertNull(action.getTemporaryFile("/xwiki/bin/temp/secret.txt", oldcore.getXWikiContext()));
110    }
111   
112    /**
113    * {@link TempResourceAction#getTemporaryFile(String, XWikiContext)} should prevent access to files outside the
114    * temporary directory by ignoring relative URIs (i.e. which use ".." to move to the parent folder).
115    */
 
116  1 toggle @Test
117    public void testGetTemporaryFileForRelativeURI() throws Exception
118    {
119  1 createEmptyFile("temp/secret.txt");
120  1 assertNull(action.getTemporaryFile("/xwiki/bin/temp/../../module/secret.txt", oldcore.getXWikiContext()));
121    }
122   
123    /**
124    * Tests {@link TempResourceAction#getTemporaryFile(String, XWikiContext)} when the file is missing.
125    */
 
126  1 toggle @Test
127    public void testGetTemporaryFileMissing() throws Exception
128    {
129  1 assertFalse(new File(this.oldcore.getTemporaryDirectory(), "temp/module/xwiki/Space/Page/file.txt").exists());
130  1 assertNull(action.getTemporaryFile("/xwiki/bin/temp/Space/Page/module/file.txt", oldcore.getXWikiContext()));
131    }
132   
133    /**
134    * Tests {@link TempResourceAction#getTemporaryFile(String, XWikiContext)} when the file is present.
135    */
 
136  1 toggle @Test
137    public void testGetTemporaryFile() throws Exception
138    {
139  1 oldcore.getXWikiContext().setWikiId("wiki");
140  1 createEmptyFile("temp/module/wiki/Space/Page/file.txt");
141  1 assertNotNull(action.getTemporaryFile("/xwiki/bin/temp/Space/Page/module/file.txt", oldcore.getXWikiContext()));
142    }
143   
144    /**
145    * Tests {@link TempResourceAction#getTemporaryFile(String, XWikiContext)} when the URL is over encoded.
146    */
 
147  1 toggle @Test
148    public void testGetTemporaryFileForOverEncodedURL() throws Exception
149    {
150  1 createEmptyFile("temp/officeviewer/xwiki/Sp*ace/Pa-ge/presentation.odp/presentation-slide0.jpg");
151  1 assertNotNull(action.getTemporaryFile(
152    "/xwiki/bin/temp/Sp%2Aace/Pa%2Dge/officeviewer/presentation.odp/presentation-slide0.jpg",
153    oldcore.getXWikiContext()));
154    }
155   
156    /**
157    * Tests {@link TempResourceAction#getTemporaryFile(String, XWikiContext)} when the URL is partially decoded. This
158    * can happen for instance when XWiki is behind Apache's {@code mode_proxy} with {@code nocanon} option disabled.
159    */
 
160  1 toggle @Test
161    public void testGetTemporaryFileForPartiallyDecodedURL() throws Exception
162    {
163  1 createEmptyFile("temp/officeviewer/xwiki/Space/Page/"
164    + "attach%3Axwiki%3ASpace.Page%40pres%2Fentation.odp/13/presentation-slide0.jpg");
165  1 assertNotNull(action.getTemporaryFile("/xwiki/bin/temp/Space/Page/officeviewer/"
166    + "attach:xwiki:Space.Page@pres%2Fentation.odp/13/presentation-slide0.jpg", oldcore.getXWikiContext()));
167    }
168   
 
169  1 toggle @Test
170    public void renderNormalBehavior() throws Exception
171    {
172  1 XWikiRequest request = mock(XWikiRequest.class);
173  1 XWikiResponse response = mock(XWikiResponse.class);
174  1 BufferOutputStream out = new BufferOutputStream();
175  1 oldcore.getXWikiContext().setRequest(request);
176  1 oldcore.getXWikiContext().setResponse(response);
177  1 when(request.getRequestURI()).thenReturn("/xwiki/bin/temp/Space/Page/module/file.txt");
178  1 when(response.getOutputStream()).thenReturn(out);
179  1 oldcore.getXWikiContext().setWikiId("wiki");
180  1 createFile("temp/module/wiki/Space/Page/file.txt", "Hello World!");
181  1 action.render(oldcore.getXWikiContext());
182  1 assertArrayEquals("Hello World!".getBytes(), out.getContentsAsByteArray());
183  1 verify(response, never()).addHeader("Content-disposition", "attachment; filename*=utf-8''file.txt");
184    }
185   
 
186  1 toggle @Test
187    public void renderWithForceDownload() throws Exception
188    {
189  1 XWikiRequest request = mock(XWikiRequest.class);
190  1 XWikiResponse response = mock(XWikiResponse.class);
191  1 oldcore.getXWikiContext().setRequest(request);
192  1 oldcore.getXWikiContext().setResponse(response);
193  1 when(request.getRequestURI()).thenReturn("/xwiki/bin/temp/Space/Page/module/file.txt");
194  1 when(request.getParameter("force-download")).thenReturn("1");
195  1 when(response.getOutputStream()).thenReturn(mock(ServletOutputStream.class));
196  1 oldcore.getXWikiContext().setWikiId("wiki");
197  1 createEmptyFile("temp/module/wiki/Space/Page/file.txt");
198  1 action.render(oldcore.getXWikiContext());
199  1 verify(response).addHeader("Content-disposition", "attachment; filename*=utf-8''file.txt");
200    }
201   
 
202  0 toggle public void renderWithInvalidPathThrowsException() throws XWikiException
203    {
204  0 XWikiRequest request = mock(XWikiRequest.class);
205  0 XWikiResponse response = mock(XWikiResponse.class);
206  0 oldcore.getXWikiContext().setRequest(request);
207  0 oldcore.getXWikiContext().setResponse(response);
208  0 when(request.getRequestURI()).thenReturn("/xwiki/bin/temp/Space/Page/module/nosuchfile.txt");
209  0 oldcore.getXWikiContext().setWikiId("wiki");
210  0 assertThrows(XWikiException.class, () -> action.render(oldcore.getXWikiContext()));
211    }
212    }