|
| 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; |
| 11 | + |
| 12 | +import org.elasticsearch.entitlement.bridge.Util; |
| 13 | +import org.elasticsearch.test.ESTestCase; |
| 14 | + |
| 15 | +import static org.elasticsearch.entitlement.BridgeUtilTests.MockSensitiveClass.mockSensitiveMethod; |
| 16 | + |
| 17 | +/** |
| 18 | + * Note: this is not un the bridge package because that is a uniquely bad one to use for tests. |
| 19 | + * Since: |
| 20 | + * <ol> |
| 21 | + * <li> |
| 22 | + * we much patch the bridge module into {@code java.base} for it to be reachable |
| 23 | + * from JDK methods, |
| 24 | + * </li> |
| 25 | + * <li> |
| 26 | + * the bridge module exports {@code org.elasticsearch.entitlement.bridge}, and |
| 27 | + * </li> |
| 28 | + * <li> |
| 29 | + * Java forbids split packages |
| 30 | + * </li> |
| 31 | + * </ol> |
| 32 | + * |
| 33 | + * ...therefore, we'll be unable to load any tests in the {@code org.elasticsearch.entitlement.bridge} |
| 34 | + * package from the classpath. |
| 35 | + * <p> |
| 36 | + * Hence, we put this test in another package. It's still accessible during testing, though, |
| 37 | + * because we export the bridge to `ALL-UNNAMED` anyway. |
| 38 | + */ |
| 39 | +public class BridgeUtilTests extends ESTestCase { |
| 40 | + |
| 41 | + public void testCallerClass() { |
| 42 | + assertEquals(BridgeUtilTests.class, mockSensitiveMethod()); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * A separate class so the stack walk can discern the sensitive method's own class |
| 47 | + * from that of its caller. |
| 48 | + */ |
| 49 | + static class MockSensitiveClass { |
| 50 | + public static Class<?> mockSensitiveMethod() { |
| 51 | + return Util.getCallerClass(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | +} |
0 commit comments