1 /* 2 * Copyright (C) 2020 The Android Open Source Project 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 android.net.ipsec.test.ike; 18 19 import static org.junit.Assert.assertEquals; 20 21 import android.net.ipsec.test.ike.exceptions.IkeException; 22 import android.net.ipsec.test.ike.exceptions.IkeProtocolException; 23 import android.net.ipsec.test.ike.exceptions.InvalidIkeSpiException; 24 25 import org.junit.Before; 26 import org.junit.Test; 27 28 import java.util.ArrayList; 29 import java.util.Arrays; 30 import java.util.List; 31 32 public final class IkeSessionCallbackTest { 33 private OldOnErrorIkeSessionCallback mOldCallback; 34 private UpdatedOnErrorIkeSessionCallback mUpdatedCallback; 35 private IkeProtocolException mIkeException; 36 37 @Before setUp()38 public void setUp() { 39 mOldCallback = new OldOnErrorIkeSessionCallback(); 40 mUpdatedCallback = new UpdatedOnErrorIkeSessionCallback(); 41 mIkeException = new InvalidIkeSpiException(); 42 } 43 44 @Test testOnErrorIkeExceptionNotOverridden()45 public void testOnErrorIkeExceptionNotOverridden() { 46 mOldCallback.onError((IkeException) mIkeException); 47 assertEquals(Arrays.asList(mIkeException), mOldCallback.mOnErrorIkeProtocolExceptions); 48 } 49 50 @Test testOnErrorIkeExceptionOverridden()51 public void testOnErrorIkeExceptionOverridden() { 52 mUpdatedCallback.onError((IkeException) mIkeException); 53 assertEquals(Arrays.asList(mIkeException), mUpdatedCallback.mOnErrorIkeExceptions); 54 } 55 56 private abstract class TestIkeSessionCallbackBase implements IkeSessionCallback { 57 @Override onOpened(IkeSessionConfiguration sessionConfiguration)58 public void onOpened(IkeSessionConfiguration sessionConfiguration) {} 59 60 @Override onClosed()61 public void onClosed() {} 62 63 @Override onClosedExceptionally(IkeException exception)64 public void onClosedExceptionally(IkeException exception) {} 65 66 @Override onIkeSessionConnectionInfoChanged(IkeSessionConnectionInfo connectionInfo)67 public void onIkeSessionConnectionInfoChanged(IkeSessionConnectionInfo connectionInfo) {} 68 } 69 70 private final class OldOnErrorIkeSessionCallback extends TestIkeSessionCallbackBase { 71 List<IkeProtocolException> mOnErrorIkeProtocolExceptions = new ArrayList<>(); 72 73 @Override onError(IkeProtocolException exception)74 public void onError(IkeProtocolException exception) { 75 mOnErrorIkeProtocolExceptions.add(exception); 76 } 77 } 78 79 private final class UpdatedOnErrorIkeSessionCallback extends TestIkeSessionCallbackBase { 80 List<IkeException> mOnErrorIkeExceptions = new ArrayList<>(); 81 82 @Override onError(IkeProtocolException exception)83 public void onError(IkeProtocolException exception) { 84 throw new UnsupportedOperationException(); 85 } 86 87 @Override onError(IkeException exception)88 public void onError(IkeException exception) { 89 mOnErrorIkeExceptions.add(exception); 90 } 91 } 92 } 93