| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 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 |
|
@link |
| 46 |
|
|
| 47 |
|
@version |
| 48 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (68) |
Complexity: 12 |
Complexity Density: 0.21 |
|
| 49 |
|
public class TempResourceActionTest |
| 50 |
|
{ |
| 51 |
|
@Rule |
| 52 |
|
public MockitoOldcoreRule oldcore = new MockitoOldcoreRule(); |
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
private TempResourceAction action; |
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
private File base; |
| 63 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 64 |
9 |
@Before... |
| 65 |
|
public void setUp() throws Exception |
| 66 |
|
{ |
| 67 |
9 |
base = new File(getClass().getResource("/").toURI()); |
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 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 |
|
|
| 79 |
|
|
| 80 |
|
@param |
| 81 |
|
@throws |
| 82 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 83 |
6 |
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 |
|
|
| 93 |
|
|
| 94 |
|
@param |
| 95 |
|
@throws |
| 96 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
| 97 |
1 |
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 |
| 108 |
|
|
| 109 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 110 |
1 |
@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 |
| 119 |
|
|
| 120 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 121 |
1 |
@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 |
|
@link |
| 130 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 131 |
1 |
@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 |
|
@link |
| 140 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 141 |
1 |
@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 |
|
@link |
| 151 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 152 |
1 |
@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 |
|
@link |
| 162 |
|
|
| 163 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 164 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
| 173 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
| 191 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
| 207 |
1 |
@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 |
|
} |