| 
 | 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.CheckedConsumer;  | 
 | 13 | +import org.elasticsearch.entitlement.qa.entitled.EntitledActions;  | 
 | 14 | + | 
 | 15 | +import java.io.IOException;  | 
 | 16 | +import java.net.URLConnection;  | 
 | 17 | + | 
 | 18 | +import static org.elasticsearch.entitlement.qa.test.EntitlementTest.ExpectedAccess.PLUGINS;  | 
 | 19 | + | 
 | 20 | +class URLConnectionFileActions {  | 
 | 21 | + | 
 | 22 | +    private static void withJdkFileConnection(CheckedConsumer<URLConnection, Exception> connectionConsumer) throws Exception {  | 
 | 23 | +        var conn = EntitledActions.createFileURLConnection();  | 
 | 24 | +        // Be sure we got the connection implementation we want  | 
 | 25 | +        assert conn.getClass().getSimpleName().equals("FileURLConnection");  | 
 | 26 | +        try {  | 
 | 27 | +            connectionConsumer.accept(conn);  | 
 | 28 | +        } catch (IOException e) {  | 
 | 29 | +            // It's OK, it means we passed entitlement checks, and we tried to perform some operation  | 
 | 30 | +        }  | 
 | 31 | +    }  | 
 | 32 | + | 
 | 33 | +    @EntitlementTest(expectedAccess = PLUGINS)  | 
 | 34 | +    static void sunFileURLConnectionConnect() throws Exception {  | 
 | 35 | +        withJdkFileConnection(URLConnection::connect);  | 
 | 36 | +    }  | 
 | 37 | + | 
 | 38 | +    @EntitlementTest(expectedAccess = PLUGINS)  | 
 | 39 | +    static void sunFileURLConnectionGetHeaderFields() throws Exception {  | 
 | 40 | +        withJdkFileConnection(URLConnection::getHeaderFields);  | 
 | 41 | +    }  | 
 | 42 | + | 
 | 43 | +    @EntitlementTest(expectedAccess = PLUGINS)  | 
 | 44 | +    static void sunFileURLConnectionGetHeaderFieldWithName() throws Exception {  | 
 | 45 | +        withJdkFileConnection(urlConnection -> urlConnection.getHeaderField("date"));  | 
 | 46 | +    }  | 
 | 47 | + | 
 | 48 | +    @EntitlementTest(expectedAccess = PLUGINS)  | 
 | 49 | +    static void sunFileURLConnectionGetHeaderFieldWithIndex() throws Exception {  | 
 | 50 | +        withJdkFileConnection(urlConnection -> urlConnection.getHeaderField(0));  | 
 | 51 | +    }  | 
 | 52 | + | 
 | 53 | +    @EntitlementTest(expectedAccess = PLUGINS)  | 
 | 54 | +    static void sunFileURLConnectionGetContentLength() throws Exception {  | 
 | 55 | +        withJdkFileConnection(URLConnection::getContentLength);  | 
 | 56 | +    }  | 
 | 57 | + | 
 | 58 | +    @EntitlementTest(expectedAccess = PLUGINS)  | 
 | 59 | +    static void sunFileURLConnectionGetContentLengthLong() throws Exception {  | 
 | 60 | +        withJdkFileConnection(URLConnection::getContentLengthLong);  | 
 | 61 | +    }  | 
 | 62 | + | 
 | 63 | +    @EntitlementTest(expectedAccess = PLUGINS)  | 
 | 64 | +    static void sunFileURLConnectionGetHeaderFieldKey() throws Exception {  | 
 | 65 | +        withJdkFileConnection(urlConnection -> urlConnection.getHeaderFieldKey(0));  | 
 | 66 | +    }  | 
 | 67 | + | 
 | 68 | +    @EntitlementTest(expectedAccess = PLUGINS)  | 
 | 69 | +    static void sunFileURLConnectionGetLastModified() throws Exception {  | 
 | 70 | +        withJdkFileConnection(URLConnection::getLastModified);  | 
 | 71 | +    }  | 
 | 72 | + | 
 | 73 | +    @EntitlementTest(expectedAccess = PLUGINS)  | 
 | 74 | +    static void sunFileURLConnectionGetInputStream() throws Exception {  | 
 | 75 | +        withJdkFileConnection(URLConnection::getInputStream);  | 
 | 76 | +    }  | 
 | 77 | + | 
 | 78 | +    @EntitlementTest(expectedAccess = PLUGINS)  | 
 | 79 | +    static void sunFileURLConnectionGetContentType() throws Exception {  | 
 | 80 | +        withJdkFileConnection(URLConnection::getContentType);  | 
 | 81 | +    }  | 
 | 82 | + | 
 | 83 | +    @EntitlementTest(expectedAccess = PLUGINS)  | 
 | 84 | +    static void sunFileURLConnectionGetContentEncoding() throws Exception {  | 
 | 85 | +        withJdkFileConnection(URLConnection::getContentEncoding);  | 
 | 86 | +    }  | 
 | 87 | + | 
 | 88 | +    @EntitlementTest(expectedAccess = PLUGINS)  | 
 | 89 | +    static void sunFileURLConnectionGetExpiration() throws Exception {  | 
 | 90 | +        withJdkFileConnection(URLConnection::getExpiration);  | 
 | 91 | +    }  | 
 | 92 | + | 
 | 93 | +    @EntitlementTest(expectedAccess = PLUGINS)  | 
 | 94 | +    static void sunFileURLConnectionGetDate() throws Exception {  | 
 | 95 | +        withJdkFileConnection(URLConnection::getDate);  | 
 | 96 | +    }  | 
 | 97 | + | 
 | 98 | +    @EntitlementTest(expectedAccess = PLUGINS)  | 
 | 99 | +    static void sunFileURLConnectionGetHeaderFieldInt() throws Exception {  | 
 | 100 | +        withJdkFileConnection(conn -> conn.getHeaderFieldInt("field", 0));  | 
 | 101 | +    }  | 
 | 102 | + | 
 | 103 | +    @EntitlementTest(expectedAccess = PLUGINS)  | 
 | 104 | +    static void sunFileURLConnectionGetHeaderFieldLong() throws Exception {  | 
 | 105 | +        withJdkFileConnection(conn -> conn.getHeaderFieldLong("field", 0));  | 
 | 106 | +    }  | 
 | 107 | + | 
 | 108 | +    @EntitlementTest(expectedAccess = PLUGINS)  | 
 | 109 | +    static void sunFileURLConnectionGetContent() throws Exception {  | 
 | 110 | +        withJdkFileConnection(URLConnection::getContent);  | 
 | 111 | +    }  | 
 | 112 | + | 
 | 113 | +    @EntitlementTest(expectedAccess = PLUGINS)  | 
 | 114 | +    static void sunFileURLConnectionGetContentWithClasses() throws Exception {  | 
 | 115 | +        withJdkFileConnection(conn -> conn.getContent(new Class<?>[] { String.class }));  | 
 | 116 | +    }  | 
 | 117 | +}  | 
0 commit comments