|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Alibaba Group Holding Ltd. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.alibaba.fluss.security.auth.sasl.gssapi; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.BeforeEach; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.junit.jupiter.params.ParameterizedTest; |
| 22 | +import org.junit.jupiter.params.provider.NullAndEmptySource; |
| 23 | + |
| 24 | +import javax.security.auth.callback.Callback; |
| 25 | +import javax.security.auth.callback.UnsupportedCallbackException; |
| 26 | +import javax.security.sasl.AuthorizeCallback; |
| 27 | + |
| 28 | +import java.io.IOException; |
| 29 | + |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; |
| 31 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 32 | + |
| 33 | +/** Unit tests for {@link GssapiServerCallbackHandler}. */ |
| 34 | +public class GssapiServerCallbackHandlerTest { |
| 35 | + |
| 36 | + private GssapiServerCallbackHandler handler; |
| 37 | + |
| 38 | + @BeforeEach |
| 39 | + public void setUp() { |
| 40 | + this.handler = new GssapiServerCallbackHandler(); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testSuccessfulAuthorization() throws IOException, UnsupportedCallbackException { |
| 45 | + String principal = "user-a@FLUSS.COM"; |
| 46 | + AuthorizeCallback cb = new AuthorizeCallback(principal, principal); |
| 47 | + |
| 48 | + handler.handle(new Callback[] {cb}); |
| 49 | + |
| 50 | + assertThat(cb.isAuthorized()).isTrue(); |
| 51 | + assertThat(cb.getAuthorizedID()).isEqualTo(principal); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testFailedAuthorizationOnIdMismatch() |
| 56 | + throws IOException, UnsupportedCallbackException { |
| 57 | + AuthorizeCallback cb = new AuthorizeCallback("user-a@FLUSS.COM", "user-b@FLUSS.COM"); |
| 58 | + |
| 59 | + handler.handle(new Callback[] {cb}); |
| 60 | + assertThat(cb.isAuthorized()).isFalse(); |
| 61 | + } |
| 62 | + |
| 63 | + @ParameterizedTest |
| 64 | + @NullAndEmptySource |
| 65 | + public void testInvalidAuthenticationIdThrowsException(String invalidAuthenticationId) { |
| 66 | + AuthorizeCallback cb = new AuthorizeCallback(invalidAuthenticationId, "user-b@FLUSS.COM"); |
| 67 | + assertThatThrownBy(() -> handler.handle(new Callback[] {cb})) |
| 68 | + .isInstanceOf(IOException.class) |
| 69 | + .hasMessageContaining("Authentication ID cannot be null or empty"); |
| 70 | + } |
| 71 | + |
| 72 | + @ParameterizedTest |
| 73 | + @NullAndEmptySource |
| 74 | + public void testInvalidAuthorizationIdThrowsException(String invalidAuthorizationId) { |
| 75 | + AuthorizeCallback cb = new AuthorizeCallback("user-a@FLUSS.com", invalidAuthorizationId); |
| 76 | + assertThatThrownBy(() -> handler.handle(new Callback[] {cb})) |
| 77 | + .isInstanceOf(IOException.class) |
| 78 | + .hasMessageContaining("Authorization ID cannot be null or empty"); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testUnsupportedCallbackThrowsException() { |
| 83 | + Callback unsupportedCallback = new javax.security.auth.callback.NameCallback("unsupported"); |
| 84 | + assertThatThrownBy(() -> handler.handle(new Callback[] {unsupportedCallback})) |
| 85 | + .isInstanceOf(UnsupportedCallbackException.class); |
| 86 | + } |
| 87 | +} |
0 commit comments