1 // Copyright 2024 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 //////////////////////////////////////////////////////////////////////////////// 16 17 package com.google.crypto.tink.internal; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import com.google.crypto.tink.testing.TestUtil; 22 import java.security.Provider; 23 import java.security.Security; 24 import org.conscrypt.Conscrypt; 25 import org.junit.BeforeClass; 26 import org.junit.Test; 27 import org.junit.runner.RunWith; 28 import org.junit.runners.JUnit4; 29 30 @RunWith(JUnit4.class) 31 public final class ConscryptUtilTest { 32 33 @BeforeClass setUp()34 public static void setUp() { 35 if (!TestUtil.isAndroid()) { 36 Conscrypt.checkAvailability(); 37 Security.addProvider(Conscrypt.newProvider()); 38 } 39 } 40 41 @Test providerOrNull_returnsConscryptProvider()42 public void providerOrNull_returnsConscryptProvider() throws Exception { 43 Provider provider = ConscryptUtil.providerOrNull(); 44 assertThat(provider).isNotNull(); 45 assertThat(ConscryptUtil.isConscryptProvider(provider)).isTrue(); 46 } 47 48 @Test providerWithReflectionOrNull_returnsConscryptProvider()49 public void providerWithReflectionOrNull_returnsConscryptProvider() throws Exception { 50 if (TestUtil.isAndroid()) { 51 // providerWithReflectionOrNull does not work on Android 52 Provider provider = ConscryptUtil.providerWithReflectionOrNull(); 53 assertThat(provider).isNull(); 54 } else { 55 Provider provider = ConscryptUtil.providerWithReflectionOrNull(); 56 assertThat(provider).isNotNull(); 57 assertThat(ConscryptUtil.isConscryptProvider(provider)).isTrue(); 58 } 59 } 60 61 @Test isConscryptProviderWithDifferentName_returnsFalse()62 public void isConscryptProviderWithDifferentName_returnsFalse() throws Exception { 63 if (TestUtil.isAndroid()) { 64 return; 65 } 66 // isConscryptProvider uses the name of the provider to determine if it is Conscrypt. 67 // We don't expect users to rename the provider. 68 Provider renamedProvider = Conscrypt.newProviderBuilder().setName("notConscrypt").build(); 69 assertThat(ConscryptUtil.isConscryptProvider(renamedProvider)).isFalse(); 70 } 71 } 72