|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.entitlement.qa.test; |
| 11 | + |
| 12 | +import org.elasticsearch.core.CheckedRunnable; |
| 13 | +import org.elasticsearch.core.SuppressForbidden; |
| 14 | + |
| 15 | +import java.io.FileNotFoundException; |
| 16 | +import java.io.IOException; |
| 17 | +import java.nio.charset.StandardCharsets; |
| 18 | +import java.security.GeneralSecurityException; |
| 19 | +import java.security.KeyStore; |
| 20 | +import java.util.Scanner; |
| 21 | +import java.util.jar.JarFile; |
| 22 | +import java.util.zip.ZipException; |
| 23 | +import java.util.zip.ZipFile; |
| 24 | + |
| 25 | +import static java.nio.charset.Charset.defaultCharset; |
| 26 | +import static java.util.zip.ZipFile.OPEN_DELETE; |
| 27 | +import static java.util.zip.ZipFile.OPEN_READ; |
| 28 | +import static org.elasticsearch.entitlement.qa.entitled.EntitledActions.createTempFileForWrite; |
| 29 | +import static org.elasticsearch.entitlement.qa.test.EntitlementTest.ExpectedAccess.PLUGINS; |
| 30 | +import static org.elasticsearch.entitlement.qa.test.FileCheckActions.readFile; |
| 31 | + |
| 32 | +@SuppressForbidden(reason = "Explicitly checking APIs that are forbidden") |
| 33 | +@SuppressWarnings("unused") // Called via reflection |
| 34 | +public class JavaBaseFileActions { |
| 35 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 36 | + static void keystore_getInstance_1() throws IOException { |
| 37 | + try { |
| 38 | + KeyStore.getInstance(readFile().toFile(), new char[0]); |
| 39 | + } catch (GeneralSecurityException expected) { |
| 40 | + return; |
| 41 | + } |
| 42 | + throw new AssertionError("Expected an exception"); |
| 43 | + } |
| 44 | + |
| 45 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 46 | + static void keystore_getInstance_2() throws IOException { |
| 47 | + try { |
| 48 | + KeyStore.LoadStoreParameter loadStoreParameter = () -> null; |
| 49 | + KeyStore.getInstance(readFile().toFile(), loadStoreParameter); |
| 50 | + } catch (GeneralSecurityException expected) { |
| 51 | + return; |
| 52 | + } |
| 53 | + throw new AssertionError("Expected an exception"); |
| 54 | + } |
| 55 | + |
| 56 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 57 | + static void keystoreBuilder_newInstance() { |
| 58 | + try { |
| 59 | + KeyStore.Builder.newInstance("", null, readFile().toFile(), null); |
| 60 | + } catch (NullPointerException expected) { |
| 61 | + return; |
| 62 | + } |
| 63 | + throw new AssertionError("Expected an exception"); |
| 64 | + } |
| 65 | + |
| 66 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 67 | + static void zipFile_1() throws IOException { |
| 68 | + expectZipException(() -> new ZipFile(readFile().toString()).close()); |
| 69 | + } |
| 70 | + |
| 71 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 72 | + static void zipFile_2() throws IOException { |
| 73 | + expectZipException(() -> new ZipFile(readFile().toString(), defaultCharset()).close()); |
| 74 | + } |
| 75 | + |
| 76 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 77 | + static void zipFile_3() throws IOException { |
| 78 | + expectZipException(() -> new ZipFile(readFile().toFile()).close()); |
| 79 | + } |
| 80 | + |
| 81 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 82 | + static void zipFile_4() throws IOException { |
| 83 | + expectZipException(() -> new ZipFile(readFile().toFile(), defaultCharset()).close()); |
| 84 | + } |
| 85 | + |
| 86 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 87 | + static void zipFile_5_readOnly() throws IOException { |
| 88 | + expectZipException(() -> new ZipFile(readFile().toFile(), OPEN_READ).close()); |
| 89 | + } |
| 90 | + |
| 91 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 92 | + static void zipFile_5_readAndDelete() throws IOException { |
| 93 | + expectZipException(() -> new ZipFile(createTempFileForWrite().toFile(), OPEN_READ | OPEN_DELETE).close()); |
| 94 | + } |
| 95 | + |
| 96 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 97 | + static void zipFile_6_readOnly() throws IOException { |
| 98 | + expectZipException(() -> new ZipFile(readFile().toFile(), OPEN_READ, defaultCharset()).close()); |
| 99 | + } |
| 100 | + |
| 101 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 102 | + static void jarFile_1() throws IOException { |
| 103 | + expectZipException(() -> new JarFile(readFile().toString()).close()); |
| 104 | + } |
| 105 | + |
| 106 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 107 | + static void jarFile_2() throws IOException { |
| 108 | + expectZipException(() -> new JarFile(readFile().toString(), false).close()); |
| 109 | + } |
| 110 | + |
| 111 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 112 | + static void jarFile_3_readOnly() throws IOException { |
| 113 | + expectZipException(() -> new JarFile(readFile().toFile(), false, OPEN_READ).close()); |
| 114 | + } |
| 115 | + |
| 116 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 117 | + static void jarFile_3_readAndDelete() throws IOException { |
| 118 | + expectZipException(() -> new JarFile(createTempFileForWrite().toFile(), false, OPEN_READ | OPEN_DELETE).close()); |
| 119 | + } |
| 120 | + |
| 121 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 122 | + static void jarFile_4_readOnly() throws IOException { |
| 123 | + expectZipException(() -> new JarFile(readFile().toFile(), false, OPEN_READ, Runtime.version()).close()); |
| 124 | + } |
| 125 | + |
| 126 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 127 | + static void jarFile_4_readAndDelete() throws IOException { |
| 128 | + expectZipException(() -> new JarFile(createTempFileForWrite().toFile(), false, OPEN_READ | OPEN_DELETE, Runtime.version()).close()); |
| 129 | + } |
| 130 | + |
| 131 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 132 | + static void jarFile_5() throws IOException { |
| 133 | + expectZipException(() -> new JarFile(readFile().toFile()).close()); |
| 134 | + } |
| 135 | + |
| 136 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 137 | + static void jarFile_6() throws IOException { |
| 138 | + expectZipException(() -> new JarFile(readFile().toFile(), false).close()); |
| 139 | + } |
| 140 | + |
| 141 | + private static void expectZipException(CheckedRunnable<IOException> action) throws IOException { |
| 142 | + try { |
| 143 | + action.run(); |
| 144 | + } catch (ZipException expected) { |
| 145 | + return; |
| 146 | + } |
| 147 | + throw new AssertionError("Expected an exception"); |
| 148 | + } |
| 149 | + |
| 150 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 151 | + static void createScannerFile() throws FileNotFoundException { |
| 152 | + new Scanner(readFile().toFile()); |
| 153 | + } |
| 154 | + |
| 155 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 156 | + static void createScannerFileWithCharset() throws IOException { |
| 157 | + new Scanner(readFile().toFile(), StandardCharsets.UTF_8); |
| 158 | + } |
| 159 | + |
| 160 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 161 | + static void createScannerFileWithCharsetName() throws FileNotFoundException { |
| 162 | + new Scanner(readFile().toFile(), "UTF-8"); |
| 163 | + } |
| 164 | + |
| 165 | +} |
0 commit comments