|  | 
|  | 1 | +/* | 
|  | 2 | + * Copyright OpenSearch Contributors | 
|  | 3 | + * SPDX-License-Identifier: Apache-2.0 | 
|  | 4 | + * | 
|  | 5 | + * The OpenSearch Contributors require contributions made to | 
|  | 6 | + * this file be licensed under the Apache-2.0 license or a | 
|  | 7 | + * compatible open source license. | 
|  | 8 | + * | 
|  | 9 | + */ | 
|  | 10 | + | 
|  | 11 | +package org.opensearch.commons.authuser.util; | 
|  | 12 | + | 
|  | 13 | +import java.io.IOException; | 
|  | 14 | +import java.net.InetAddress; | 
|  | 15 | +import java.net.InetSocketAddress; | 
|  | 16 | +import java.util.ArrayList; | 
|  | 17 | +import java.util.HashMap; | 
|  | 18 | +import java.util.regex.Pattern; | 
|  | 19 | + | 
|  | 20 | +import org.junit.After; | 
|  | 21 | +import org.junit.Test; | 
|  | 22 | + | 
|  | 23 | +import static org.junit.Assert.assertEquals; | 
|  | 24 | +import static org.junit.Assert.assertFalse; | 
|  | 25 | +import static org.junit.Assert.assertTrue; | 
|  | 26 | +import static org.junit.Assert.fail; | 
|  | 27 | + | 
|  | 28 | +public class SafeSerializationUtilsTest { | 
|  | 29 | + | 
|  | 30 | +    @After | 
|  | 31 | +    public void clearCache() { | 
|  | 32 | +        SafeSerializationUtils.safeClassCache.clear(); | 
|  | 33 | +    } | 
|  | 34 | + | 
|  | 35 | +    @Test | 
|  | 36 | +    public void testSafeClasses() { | 
|  | 37 | +        assertTrue(SafeSerializationUtils.isSafeClass(String.class)); | 
|  | 38 | +        assertTrue(SafeSerializationUtils.isSafeClass(InetSocketAddress.class)); | 
|  | 39 | +        assertTrue(SafeSerializationUtils.isSafeClass(Pattern.class)); | 
|  | 40 | +    } | 
|  | 41 | + | 
|  | 42 | +    @Test | 
|  | 43 | +    public void testSafeAssignableClasses() { | 
|  | 44 | +        assertTrue(SafeSerializationUtils.isSafeClass(InetAddress.class)); | 
|  | 45 | +        assertTrue(SafeSerializationUtils.isSafeClass(Integer.class)); | 
|  | 46 | +        assertTrue(SafeSerializationUtils.isSafeClass(ArrayList.class)); | 
|  | 47 | +        assertTrue(SafeSerializationUtils.isSafeClass(HashMap.class)); | 
|  | 48 | +        assertTrue(SafeSerializationUtils.isSafeClass(Enum.class)); | 
|  | 49 | +    } | 
|  | 50 | + | 
|  | 51 | +    @Test | 
|  | 52 | +    public void testArraysAreSafe() { | 
|  | 53 | +        assertTrue(SafeSerializationUtils.isSafeClass(String[].class)); | 
|  | 54 | +        assertTrue(SafeSerializationUtils.isSafeClass(int[].class)); | 
|  | 55 | +        assertTrue(SafeSerializationUtils.isSafeClass(Object[].class)); | 
|  | 56 | +    } | 
|  | 57 | + | 
|  | 58 | +    @Test | 
|  | 59 | +    public void testUnsafeClasses() { | 
|  | 60 | +        assertFalse(SafeSerializationUtils.isSafeClass(SafeSerializationUtilsTest.class)); | 
|  | 61 | +        assertFalse(SafeSerializationUtils.isSafeClass(Runtime.class)); | 
|  | 62 | +    } | 
|  | 63 | + | 
|  | 64 | +    @Test | 
|  | 65 | +    public void testProhibitUnsafeClasses() { | 
|  | 66 | +        try { | 
|  | 67 | +            SafeSerializationUtils.prohibitUnsafeClasses(String.class); | 
|  | 68 | +        } catch (IOException e) { | 
|  | 69 | +            fail("Should not throw exception for safe class"); | 
|  | 70 | +        } | 
|  | 71 | + | 
|  | 72 | +        try { | 
|  | 73 | +            SafeSerializationUtils.prohibitUnsafeClasses(SafeSerializationUtilsTest.class); | 
|  | 74 | +            fail("Should throw exception for unsafe class"); | 
|  | 75 | +        } catch (IOException e) { | 
|  | 76 | +            assertEquals("Unauthorized serialization attempt " + SafeSerializationUtilsTest.class.getName(), e.getMessage()); | 
|  | 77 | +        } | 
|  | 78 | +    } | 
|  | 79 | + | 
|  | 80 | +    @Test | 
|  | 81 | +    public void testInheritance() { | 
|  | 82 | +        class CustomArrayList extends ArrayList<String> {} | 
|  | 83 | +        assertTrue(SafeSerializationUtils.isSafeClass(CustomArrayList.class)); | 
|  | 84 | + | 
|  | 85 | +        class CustomMap extends HashMap<String, Integer> {} | 
|  | 86 | +        assertTrue(SafeSerializationUtils.isSafeClass(CustomMap.class)); | 
|  | 87 | +    } | 
|  | 88 | + | 
|  | 89 | +    @Test | 
|  | 90 | +    public void testCaching() { | 
|  | 91 | +        // First call should compute the result | 
|  | 92 | +        boolean result1 = SafeSerializationUtils.isSafeClass(String.class); | 
|  | 93 | +        assertTrue(result1); | 
|  | 94 | + | 
|  | 95 | +        // Second call should use cached result | 
|  | 96 | +        boolean result2 = SafeSerializationUtils.isSafeClass(String.class); | 
|  | 97 | +        assertTrue(result2); | 
|  | 98 | + | 
|  | 99 | +        // Verify that the cache was used (size should be 1) | 
|  | 100 | +        assertEquals(1, SafeSerializationUtils.safeClassCache.size()); | 
|  | 101 | + | 
|  | 102 | +        // Third call for a different class | 
|  | 103 | +        boolean result3 = SafeSerializationUtils.isSafeClass(Integer.class); | 
|  | 104 | +        assertTrue(result3); | 
|  | 105 | +        // Verify that the cache was updated | 
|  | 106 | +        assertEquals(2, SafeSerializationUtils.safeClassCache.size()); | 
|  | 107 | +    } | 
|  | 108 | +} | 
0 commit comments