hwanju commented on a change in pull request #14499: URL: https://github.com/apache/flink/pull/14499#discussion_r555381940
########## File path: flink-runtime/src/test/java/org/apache/flink/runtime/security/FlinkUserSecurityManagerTest.java ########## @@ -0,0 +1,187 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.runtime.security; + +import org.apache.flink.configuration.Configuration; +import org.apache.flink.configuration.IllegalConfigurationException; +import org.apache.flink.configuration.SecurityOptions; +import org.apache.flink.runtime.UserSystemExitException; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +/** + * Tests for {@code FlinkUserSecurityManager}. + */ +public class FlinkUserSecurityManagerTest extend TestLogger { + + private static final int TEST_EXIT_CODE = 123; + private FlinkUserSecurityManager flinkUserSecurityManager; + + @Before + public void setUp() { + flinkUserSecurityManager = new FlinkUserSecurityManager(FlinkUserSecurityManager.CheckExitMode.THROW); + System.setSecurityManager(flinkUserSecurityManager); + } + + @After + public void tearDown() { + if (flinkUserSecurityManager != null) { + System.setSecurityManager(flinkUserSecurityManager.getOriginalSecurityManager()); + } + } + + @Test(expected = UserSystemExitException.class) + public void testThrowUserExit() { + flinkUserSecurityManager.monitorSystemExit(); + flinkUserSecurityManager.checkExit(TEST_EXIT_CODE); + } + + @Test + public void testToggleUserExit() { + flinkUserSecurityManager.checkExit(TEST_EXIT_CODE); + flinkUserSecurityManager.monitorSystemExit(); + try { + flinkUserSecurityManager.checkExit(TEST_EXIT_CODE); + fail(); + } catch (UserSystemExitException ignored) { } + flinkUserSecurityManager.unmonitorSystemExit(); + flinkUserSecurityManager.checkExit(TEST_EXIT_CODE); + } + + @Test + public void testPerThreadThrowUserExit() throws Exception { + ExecutorService executorService = Executors.newSingleThreadExecutor(); + // Async thread test before enabling monitoring ensures it does not throw while prestarting + // worker thread, which is to be unmonitored and tested after enabling monitoring enabled. + CompletableFuture<Void> future = + CompletableFuture.runAsync(() -> flinkUserSecurityManager.checkExit(TEST_EXIT_CODE), + executorService); + future.get(); + flinkUserSecurityManager.monitorSystemExit(); + try { + flinkUserSecurityManager.checkExit(TEST_EXIT_CODE); + fail(); + } catch (UserSystemExitException ignored) { } + // This threaded exit should be allowed as thread is not spawned while monitor is enabled. + future = CompletableFuture.runAsync(() -> flinkUserSecurityManager.checkExit(TEST_EXIT_CODE), + executorService); + future.get(); + } + + @Test + public void testInheritedThrowUserExit() throws Exception { + flinkUserSecurityManager.monitorSystemExit(); + try { + flinkUserSecurityManager.checkExit(TEST_EXIT_CODE); + fail(); + } catch (UserSystemExitException ignored) { } + Thread thread = new Thread(() -> { + try { + flinkUserSecurityManager.checkExit(TEST_EXIT_CODE); + fail(); + } catch (UserSystemExitException ignored) { + } catch (Throwable t) { + fail(); Review comment: Thanks @zentol and I followed your suggestion. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org