1 /* 2 * Copyright (C) 2024 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.ssl; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertNotNull; 21 import static org.junit.Assert.assertNull; 22 import static org.junit.Assert.assertThrows; 23 24 import android.platform.test.annotations.RequiresFlagsEnabled; 25 26 import org.junit.Test; 27 import org.junit.runner.RunWith; 28 import org.junit.runners.JUnit4; 29 30 import java.security.InvalidParameterException; 31 32 @RunWith(JUnit4.class) 33 public class PakeOptionTest { 34 @Test 35 @RequiresFlagsEnabled(com.android.org.conscrypt.flags.Flags.FLAG_SPAKE2PLUS_API) testBuilder_valid()36 public void testBuilder_valid() { 37 PakeOption option = new PakeOption.Builder("SPAKE2PLUS_PRERELEASE") 38 .addMessageComponent("password", new byte[] {1, 2, 3}) 39 .build(); 40 assertEquals("SPAKE2PLUS_PRERELEASE", option.getAlgorithm()); 41 assertNotNull(option.getMessageComponent("password")); 42 } 43 44 @Test 45 @RequiresFlagsEnabled(com.android.org.conscrypt.flags.Flags.FLAG_SPAKE2PLUS_API) testBuilder_invalidAlgorithm()46 public void testBuilder_invalidAlgorithm() { 47 assertThrows(InvalidParameterException.class, () -> new PakeOption.Builder(null)); 48 } 49 50 @Test 51 @RequiresFlagsEnabled(com.android.org.conscrypt.flags.Flags.FLAG_SPAKE2PLUS_API) testBuilder_emptyAlgorithm()52 public void testBuilder_emptyAlgorithm() { 53 assertThrows(InvalidParameterException.class, () -> new PakeOption.Builder("")); 54 } 55 56 @Test 57 @RequiresFlagsEnabled(com.android.org.conscrypt.flags.Flags.FLAG_SPAKE2PLUS_API) testBuilder_noComponents()58 public void testBuilder_noComponents() { 59 assertThrows( 60 InvalidParameterException.class, 61 () -> new PakeOption.Builder("SPAKE2PLUS_PRERELEASE").build()); 62 } 63 64 @Test 65 @RequiresFlagsEnabled(com.android.org.conscrypt.flags.Flags.FLAG_SPAKE2PLUS_API) testBuilder_invalidKey()66 public void testBuilder_invalidKey() { 67 assertThrows(InvalidParameterException.class, () -> new PakeOption.Builder("SPAKE2PLUS_PRERELEASE") 68 .addMessageComponent(null, new byte[] {1, 2, 3})); 69 } 70 71 @Test 72 @RequiresFlagsEnabled(com.android.org.conscrypt.flags.Flags.FLAG_SPAKE2PLUS_API) testBuilder_emptyKey()73 public void testBuilder_emptyKey() { 74 assertThrows(InvalidParameterException.class, () -> new PakeOption.Builder("SPAKE2PLUS_PRERELEASE") 75 .addMessageComponent("", new byte[] {1, 2, 3})); 76 } 77 78 @Test 79 @RequiresFlagsEnabled(com.android.org.conscrypt.flags.Flags.FLAG_SPAKE2PLUS_API) testBuilder_invalidSpake2Plus_passwordWithContext()80 public void testBuilder_invalidSpake2Plus_passwordWithContext() { 81 PakeOption option = new PakeOption.Builder("SPAKE2PLUS_PRERELEASE") 82 .addMessageComponent("password", new byte[] {1, 2, 3}) 83 .addMessageComponent("context", new byte[] {4, 2, 3}) 84 .build(); 85 assertNotNull(option.getMessageComponent("password")); 86 assertNotNull(option.getMessageComponent("context")); 87 assertNull(option.getMessageComponent("non_existing_key")); 88 } 89 90 @Test 91 @RequiresFlagsEnabled(com.android.org.conscrypt.flags.Flags.FLAG_SPAKE2PLUS_API) testBuilder_spake2Plus_passwordWithHandshakeLimits()92 public void testBuilder_spake2Plus_passwordWithHandshakeLimits() { 93 PakeOption option = new PakeOption.Builder("SPAKE2PLUS_PRERELEASE") 94 .addMessageComponent("password", new byte[] {1, 2, 3}) 95 .addMessageComponent("server-handshake-limit", new byte[] {16}) 96 .addMessageComponent("client-handshake-limit", new byte[] {16}) 97 .build(); 98 assertNotNull(option.getMessageComponent("password")); 99 assertNotNull(option.getMessageComponent("server-handshake-limit")); 100 assertNotNull(option.getMessageComponent("client-handshake-limit")); 101 } 102 103 @Test 104 @RequiresFlagsEnabled(com.android.org.conscrypt.flags.Flags.FLAG_SPAKE2PLUS_API) testBuilder_spake2Plus_passwordInvalidServerHandshakeLimit1()105 public void testBuilder_spake2Plus_passwordInvalidServerHandshakeLimit1() { 106 assertThrows(InvalidParameterException.class, () -> new PakeOption.Builder("SPAKE2PLUS_PRERELEASE") 107 .addMessageComponent("password", new byte[] {1, 2, 3}) 108 .addMessageComponent("server-handshake-limit", new byte[] {64}) 109 .addMessageComponent("client-handshake-limit", new byte[] {16}) 110 .build()); 111 } 112 113 @Test 114 @RequiresFlagsEnabled(com.android.org.conscrypt.flags.Flags.FLAG_SPAKE2PLUS_API) testBuilder_spake2Plus_passwordInvalidServerHandshakeLimit2()115 public void testBuilder_spake2Plus_passwordInvalidServerHandshakeLimit2() { 116 assertThrows(InvalidParameterException.class, () -> new PakeOption.Builder("SPAKE2PLUS_PRERELEASE") 117 .addMessageComponent("password", new byte[] {1, 2, 3}) 118 .addMessageComponent("server-handshake-limit", new byte[] {0}) 119 .addMessageComponent("client-handshake-limit", new byte[] {16}) 120 .build()); 121 } 122 123 @Test 124 @RequiresFlagsEnabled(com.android.org.conscrypt.flags.Flags.FLAG_SPAKE2PLUS_API) testBuilder_spake2Plus_passwordInvalidClientHandshakeLimit()125 public void testBuilder_spake2Plus_passwordInvalidClientHandshakeLimit() { 126 assertThrows(InvalidParameterException.class, () ->new PakeOption.Builder("SPAKE2PLUS_PRERELEASE") 127 .addMessageComponent("password", new byte[] {1, 2, 3}) 128 .addMessageComponent("server-handshake-limit", new byte[] {16}) 129 .addMessageComponent("client-handshake-limit", new byte[] {64}) 130 .build()); 131 } 132 133 @Test 134 @RequiresFlagsEnabled(com.android.org.conscrypt.flags.Flags.FLAG_SPAKE2PLUS_API) testBuilder_invalidSpake2Plus_noPassword()135 public void testBuilder_invalidSpake2Plus_noPassword() { 136 assertThrows(InvalidParameterException.class, () -> new PakeOption.Builder("SPAKE2PLUS_PRERELEASE") 137 .addMessageComponent("w0", new byte[] {1, 2, 3}) 138 .build()); 139 } 140 } 141