1 /*
2 Copyright (C) 2013 Red Hat, Inc.
3 
4 This file is part of IcedTea.
5 
6 IcedTea is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, version 2.
9 
10 IcedTea 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 General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with IcedTea; see the file COPYING.  If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 02110-1301 USA.
19 
20 Linking this library statically or dynamically with other modules is
21 making a combined work based on this library.  Thus, the terms and
22 conditions of the GNU General Public License cover the whole
23 combination.
24 
25 As a special exception, the copyright holders of this library give you
26 permission to link this library with independent modules to produce an
27 executable, regardless of the license terms of these independent
28 modules, and to copy and distribute the resulting executable under
29 terms of your choice, provided that you also meet, for each linked
30 independent module, the terms and conditions of the license of that
31 module.  An independent module is a module which is not derived from
32 or based on this library.  If you modify this library, you may extend
33 this exception to your version of the library, but you are not
34 obligated to do so.  If you do not wish to do so, delete this
35 exception statement from your version.
36  */
37 
38 package net.sourceforge.jnlp.cache;
39 
40 import static net.sourceforge.jnlp.util.FileTestUtils.assertNoFileLeak;
41 import static org.junit.Assert.assertEquals;
42 import static org.junit.Assert.assertFalse;
43 import static org.junit.Assert.assertTrue;
44 
45 import java.io.File;
46 import java.net.URL;
47 import java.util.ArrayList;
48 import java.util.List;
49 
50 import net.sourceforge.jnlp.Version;
51 import net.sourceforge.jnlp.util.FileTestUtils;
52 
53 import org.junit.Test;
54 
55 public class NativeLibraryStorageTest {
56 
57     /**************************************************************************
58      *                          Test helpers                                    *
59      **************************************************************************/
60 
61     /* Associates an extension with whether it represents a native library */
62     static class FileExtension {
FileExtension(String extension, boolean isNative)63         public FileExtension(String extension, boolean isNative) {
64             this.extension = extension;
65             this.isNative = isNative;
66         }
67         final String extension;
68         final boolean isNative;
69     }
70 
makeExtensionsToTest()71     static private List<FileExtension> makeExtensionsToTest() {
72         List<FileExtension> exts = new ArrayList<FileExtension>();
73         exts.add(new FileExtension(".foobar", false)); /* Dummy non-native test extension */
74         for (String ext : NativeLibraryStorage.NATIVE_LIBRARY_EXTENSIONS) {
75             exts.add(new FileExtension(ext, true));
76         }
77         return exts;
78     }
79 
80     /* All the native library types we support, as well as one negative test */
81     static final List<FileExtension> extensionsToTest = makeExtensionsToTest();
82 
83     /* Creates a NativeLibraryStorage object, caching the given URLs */
nativeLibraryStorageWithCache(URL... urlsToCache)84     static NativeLibraryStorage nativeLibraryStorageWithCache(URL... urlsToCache) {
85         ResourceTracker tracker = new ResourceTracker();
86         for (URL urlToCache : urlsToCache) {
87             tracker.addResource(urlToCache, new Version("1.0"), null, UpdatePolicy.ALWAYS);
88         }
89 
90         return new NativeLibraryStorage(tracker);
91     }
92 
93     /**************************************************************************
94      *                          Test cases                                    *
95      **************************************************************************/
96 
97     /* Tests searching for native libraries in jars */
98     @Test
testJarFileSearch()99     public void testJarFileSearch() throws Exception {
100         /* Create a temporary directory to create jars in */
101         File tempDirectory = FileTestUtils.createTempDirectory();
102 
103         for (FileExtension ext : extensionsToTest) {
104             /* Create empty file to search for */
105             String testFileName = "foobar" + ext.extension;
106             File testFile = new File(tempDirectory, testFileName);
107             FileTestUtils.createFileWithContents(testFile, "");
108 
109             /* Create jar to search in */
110             File jarLocation = new File(tempDirectory, "test.jar");
111             FileTestUtils.createJarWithContents(jarLocation, testFile);
112 
113             final URL tempJarUrl = jarLocation.toURI().toURL();
114             final NativeLibraryStorage storage = nativeLibraryStorageWithCache(tempJarUrl);
115 
116             assertNoFileLeak( new Runnable () {
117                 @Override
118                 public void run() {
119                     storage.addSearchJar(tempJarUrl);
120                 }
121             });
122 
123             /* This check isn't critical, but ensures we do not accidentally add jars as search directories */
124             assertFalse(storage.getSearchDirectories().contains(tempJarUrl));
125 
126             /* If the file we added is native, it should be found
127              * Due to an implementation detail, non-native files will not be found */
128             boolean testFileWasFound = storage.findLibrary(testFileName) != null;
129             assertEquals(ext.isNative, testFileWasFound);
130         }
131     }
132 
133     /* Tests searching for native libraries in directories */
134     @Test
testDirectorySearch()135     public void testDirectorySearch() throws Exception {
136         /* Create a temporary directory to search in */
137         File tempDirectory = FileTestUtils.createTempDirectory();
138 
139         for (FileExtension ext : extensionsToTest) {
140             /* Create empty file in the directory */
141             String testFileName = "foobar" + ext.extension;
142             FileTestUtils.createFileWithContents(new File(tempDirectory, testFileName), "");
143 
144             /* Add the directory to the search list */
145             NativeLibraryStorage storage = nativeLibraryStorageWithCache(/* None needed */);
146             storage.addSearchDirectory(tempDirectory);
147 
148             /* Ensure directory is in our search list */
149             assertTrue(storage.getSearchDirectories().contains(tempDirectory));
150 
151             /* The file should be found, regardless if it was native */
152             boolean testFileWasFound = storage.findLibrary(testFileName) != null;
153             assertTrue(testFileWasFound);
154         }
155     }
156 
157     @Test
testCleanupTemporaryFolder()158     public void testCleanupTemporaryFolder() throws Exception {
159         NativeLibraryStorage storage = nativeLibraryStorageWithCache(/* None needed */);
160         storage.ensureNativeStoreDirectory();
161 
162         /* The temporary native store directory should be our only search folder */
163         assertTrue(storage.getSearchDirectories().size() == 1);
164 
165         File searchDirectory = storage.getSearchDirectories().get(0);
166         assertTrue(searchDirectory.exists());
167 
168         /* Test that it has been deleted */
169         storage.cleanupTemporaryFolder();
170         assertFalse(searchDirectory.exists());
171     }
172 }