Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: deaff22dc1139478f79129d74b95cb0e187471d2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
//
//  ========================================================================
//  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.util.resource;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.junit.Assume.*;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URI;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.FileSystemException;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.jetty.toolchain.test.FS;
import org.eclipse.jetty.toolchain.test.IO;
import org.eclipse.jetty.toolchain.test.OS;
import org.eclipse.jetty.toolchain.test.TestingDir;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.CollectionAssert;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;

public abstract class AbstractFSResourceTest
{
    @Rule
    public TestingDir testdir = new TestingDir();

    public abstract Resource newResource(URI uri) throws IOException;

    public abstract Resource newResource(File file) throws IOException;

    private URI createEmptyFile(String name) throws IOException
    {
        File file = testdir.getFile(name);
        file.createNewFile();
        return file.toURI();
    }

    @Test(expected = IllegalArgumentException.class)
    public void testNonAbsoluteURI() throws Exception
    {
        newResource(new URI("path/to/resource"));
    }

    @Test(expected = IllegalArgumentException.class)
    public void testNotFileURI() throws Exception
    {
        newResource(new URI("http://www.eclipse.org/jetty/"));
    }

    @Test(expected = IllegalArgumentException.class)
    public void testBogusFilename() throws Exception
    {
        if (OS.IS_UNIX)
        {
            // A windows path is invalid under unix
            newResource(new URI("file://Z:/:"));
        }
        else if (OS.IS_WINDOWS)
        {
            // "CON" is a reserved name under windows
            newResource(new URI("file://CON"));
        }
        else
        {
            assumeFalse("Unknown OS type",false);
        }   
    }

    @Test
    public void testIsContainedIn() throws Exception
    {
        createEmptyFile("foo");

        try (Resource base = newResource(testdir.getDir()))
        {
            Resource res = base.addPath("foo");
            assertThat("is contained in",res.isContainedIn(base),is(false));
        }
    }

    @Test
    public void testAddPath() throws Exception
    {
        File dir = testdir.getDir();
        File subdir = new File(dir,"sub");
        FS.ensureDirExists(subdir);

        try (Resource base = newResource(testdir.getDir()))
        {
            Resource sub = base.addPath("sub");
            assertThat("sub/.isDirectory",sub.isDirectory(),is(true));
            
            Resource tmp = sub.addPath("/tmp");
            assertThat("No root",tmp.exists(),is(false));
        }
    }
    
    @Test
    public void testIsDirectory() throws Exception
    {
        File dir = testdir.getDir();
        createEmptyFile("foo");

        File subdir = new File(dir,"sub");
        FS.ensureDirExists(subdir);

        try (Resource base = newResource(testdir.getDir()))
        {
            Resource res = base.addPath("foo");
            assertThat("foo.isDirectory",res.isDirectory(),is(false));

            Resource sub = base.addPath("sub");
            assertThat("sub/.isDirectory",sub.isDirectory(),is(true));
        }
    }

    @Test
    public void testLastModified() throws Exception
    {
        File file = testdir.getFile("foo");
        file.createNewFile();

        long expected = file.lastModified();

        try (Resource base = newResource(testdir.getDir()))
        {
            Resource res = base.addPath("foo");
            assertThat("foo.lastModified",res.lastModified(),is(expected));
        }
    }

    @Test
    public void testLastModified_NotExists() throws Exception
    {
        try (Resource base = newResource(testdir.getDir()))
        {
            Resource res = base.addPath("foo");
            assertThat("foo.lastModified",res.lastModified(),is(0L));
        }
    }

    @Test
    public void testLength() throws Exception
    {
        File file = testdir.getFile("foo");
        file.createNewFile();

        try (StringReader reader = new StringReader("foo"); FileWriter writer = new FileWriter(file))
        {
            IO.copy(reader,writer);
        }

        long expected = file.length();

        try (Resource base = newResource(testdir.getDir()))
        {
            Resource res = base.addPath("foo");
            assertThat("foo.length",res.length(),is(expected));
        }
    }

    @Test
    public void testLength_NotExists() throws Exception
    {
        try (Resource base = newResource(testdir.getDir()))
        {
            Resource res = base.addPath("foo");
            assertThat("foo.length",res.length(),is(0L));
        }
    }

    @Test
    public void testDelete() throws Exception
    {
        File file = testdir.getFile("foo");
        file.createNewFile();

        try (Resource base = newResource(testdir.getDir()))
        {
            // Is it there?
            Resource res = base.addPath("foo");
            assertThat("foo.exists",res.exists(),is(true));
            // delete it
            assertThat("foo.delete",res.delete(),is(true));
            // is it there?
            assertThat("foo.exists",res.exists(),is(false));
        }
    }

    @Test
    public void testDelete_NotExists() throws Exception
    {
        try (Resource base = newResource(testdir.getDir()))
        {
            // Is it there?
            Resource res = base.addPath("foo");
            assertThat("foo.exists",res.exists(),is(false));
            // delete it
            assertThat("foo.delete",res.delete(),is(false));
            // is it there?
            assertThat("foo.exists",res.exists(),is(false));
        }
    }

    @Test
    public void testName() throws Exception
    {
        String expected = testdir.getDir().getAbsolutePath();

        try (Resource base = newResource(testdir.getDir()))
        {
            assertThat("base.name",base.getName(),is(expected));
        }
    }

    @Test
    public void testInputStream() throws Exception
    {
        File file = testdir.getFile("foo");
        file.createNewFile();

        String content = "Foo is here";

        try (StringReader reader = new StringReader(content); FileWriter writer = new FileWriter(file))
        {
            IO.copy(reader,writer);
        }

        try (Resource base = newResource(testdir.getDir()))
        {
            Resource foo = base.addPath("foo");
            try (InputStream stream = foo.getInputStream(); InputStreamReader reader = new InputStreamReader(stream); StringWriter writer = new StringWriter())
            {
                IO.copy(reader,writer);
                assertThat("Stream",writer.toString(),is(content));
            }
        }
    }

    @Test
    public void testReadableByteChannel() throws Exception
    {
        File file = testdir.getFile("foo");
        file.createNewFile();

        String content = "Foo is here";

        try (StringReader reader = new StringReader(content); FileWriter writer = new FileWriter(file))
        {
            IO.copy(reader,writer);
        }

        try (Resource base = newResource(testdir.getDir()))
        {
            Resource foo = base.addPath("foo");
            try (ReadableByteChannel channel = foo.getReadableByteChannel())
            {
                ByteBuffer buf = ByteBuffer.allocate(256);
                channel.read(buf);
                buf.flip();
                String actual = BufferUtil.toUTF8String(buf);
                assertThat("ReadableByteChannel content",actual,is(content));
            }
        }
    }
    
    @Test
    public void testGetURI() throws Exception
    {
        File file = testdir.getFile("foo");
        file.createNewFile();

        URI expected = file.toURI();

        try (Resource base = newResource(testdir.getDir()))
        {
            Resource foo = base.addPath("foo");
            assertThat("getURI",foo.getURI(),is(expected));
        }
    }

    @Test
    public void testGetURL() throws Exception
    {
        File file = testdir.getFile("foo");
        file.createNewFile();

        URL expected = file.toURI().toURL();

        try (Resource base = newResource(testdir.getDir()))
        {
            Resource foo = base.addPath("foo");
            assertThat("getURL",foo.getURL(),is(expected));
        }
    }
    
    @Test
    public void testList() throws Exception
    {
        File dir = testdir.getDir();
        FS.touch(new File(dir, "foo"));
        FS.touch(new File(dir, "bar"));
        FS.ensureDirExists(new File(dir, "tick"));
        FS.ensureDirExists(new File(dir, "tock"));
        
        List<String> expected = new ArrayList<>();
        expected.add("foo");
        expected.add("bar");
        expected.add("tick/");
        expected.add("tock/");

        try (Resource base = newResource(testdir.getDir()))
        {
            String list[] = base.list();
            List<String> actual = Arrays.asList(list);
            
            CollectionAssert.assertContainsUnordered("Resource Directory Listing",
                    expected,actual);
        }
    }
    
    @Test
    public void testSymlink() throws Exception
    {
        File dir = testdir.getDir();
        
        Path foo = new File(dir, "foo").toPath();
        Path bar = new File(dir, "bar").toPath();
        
        try
        {
            Files.createFile(foo);
            Files.createSymbolicLink(bar,foo);
        }
        catch (UnsupportedOperationException | FileSystemException e)
        {
            // if unable to create symlink, no point testing the rest
            // this is the path that Microsoft Windows takes.
            assumeNoException(e);
        }
        
        try (Resource base = newResource(testdir.getDir()))
        {
            Resource resFoo = base.addPath("foo");
            Resource resBar = base.addPath("bar");
            
            // Access to the same resource, but via a symlink means that they are not equivalent
            assertThat("foo.equals(bar)", resFoo.equals(resBar), is(false));
            
            assertThat("foo.alias", resFoo.getAlias(), nullValue());
            assertThat("bar.alias", resBar.getAlias(), is(foo.toUri()));
        }
    }
    
    @Test
    public void testSemicolon() throws Exception
    {
        File dir = testdir.getDir();
        
        try
        {
            // attempt to create file
            Path foo = new File(dir, "foo;").toPath();
            Files.createFile(foo);
        }
        catch (Exception e)
        {
            // if unable to create file, no point testing the rest
            // this is the path that Microsoft Windows takes.
            assumeNoException(e);
        }

        try (Resource base = newResource(testdir.getDir()))
        {
            Resource res = base.addPath("foo;");
            assertThat("Alias: " + res,res.getAlias(),nullValue());
        }
    }

    @Test
    public void testExist_Normal() throws Exception
    {
        createEmptyFile("a.jsp");

        URI ref = testdir.getDir().toURI().resolve("a.jsp");
        try (Resource fileres = newResource(ref))
        {
            assertThat("Resource: " + fileres,fileres.exists(),is(true));
        }
    }

    @Test
    public void testSingleQuoteInFileName() throws Exception
    {
        createEmptyFile("foo's.txt");
        createEmptyFile("f o's.txt");

        URI refQuoted = testdir.getDir().toURI().resolve("foo's.txt");

        try (Resource fileres = newResource(refQuoted))
        {
            assertThat("Exists: " + refQuoted,fileres.exists(),is(true));
            assertThat("Alias: " + refQuoted,fileres.getAlias(),nullValue());
        }

        URI refEncoded = testdir.getDir().toURI().resolve("foo%27s.txt");

        try (Resource fileres = newResource(refEncoded))
        {
            assertThat("Exists: " + refEncoded,fileres.exists(),is(true));
            assertThat("Alias: " + refEncoded,fileres.getAlias(),nullValue());
        }

        URI refQuoteSpace = testdir.getDir().toURI().resolve("f%20o's.txt");

        try (Resource fileres = newResource(refQuoteSpace))
        {
            assertThat("Exists: " + refQuoteSpace,fileres.exists(),is(true));
            assertThat("Alias: " + refQuoteSpace,fileres.getAlias(),nullValue());
        }

        URI refEncodedSpace = testdir.getDir().toURI().resolve("f%20o%27s.txt");

        try (Resource fileres = newResource(refEncodedSpace))
        {
            assertThat("Exists: " + refEncodedSpace,fileres.exists(),is(true));
            assertThat("Alias: " + refEncodedSpace,fileres.getAlias(),nullValue());
        }

        URI refA = testdir.getDir().toURI().resolve("foo's.txt");
        URI refB = testdir.getDir().toURI().resolve("foo%27s.txt");

        StringBuilder msg = new StringBuilder();
        msg.append("URI[a].equals(URI[b])").append(System.lineSeparator());
        msg.append("URI[a] = ").append(refA).append(System.lineSeparator());
        msg.append("URI[b] = ").append(refB);

        // show that simple URI.equals() doesn't work
        assertThat(msg.toString(),refA.equals(refB),is(false));

        // now show that Resource.equals() does work
        try (Resource a = newResource(refA); Resource b = newResource(refB);)
        {
            assertThat("A.equals(B)",a.equals(b),is(true));
        }
    }

    @Test
    public void testExist_BadNull() throws Exception
    {
        createEmptyFile("a.jsp");

        try
        {
            // request with null at end
            URI ref = testdir.getDir().toURI().resolve("a.jsp%00");
            assertThat("Null URI",ref,notNullValue());

            newResource(ref);
            fail("Should have thrown " + InvalidPathException.class);
        }
        catch (InvalidPathException e)
        {
            // Expected path
        }
    }

    @Test
    public void testExist_BadNullX() throws Exception
    {
        createEmptyFile("a.jsp");

        try
        {
            // request with null and x at end
            URI ref = testdir.getDir().toURI().resolve("a.jsp%00x");
            assertThat("NullX URI",ref,notNullValue());

            newResource(ref);
            fail("Should have thrown " + InvalidPathException.class);
        }
        catch (InvalidPathException e)
        {
            // Expected path
        }
    }
    
    @Test
    public void testUtf8Dir() throws Exception
    {
        File dir=new File(testdir.getDir(),"bãm");
        dir.mkdir();
        File file = new File(dir,"file.txt");
        file.createNewFile();
        
        Resource base = newResource(dir);
        Assert.assertNull(base.getAlias());
        
        Resource r = base.addPath("file.txt");
        Assert.assertNull(r.getAlias());
        
    }
}

Back to the top