• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2023 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.Security;
23 import java.util.ArrayList;
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 RandomTest {
32 
33   static boolean conscryptProviderAdded;
34 
35   @BeforeClass
setUp()36   public static void setUp() {
37     try {
38       Conscrypt.checkAvailability();
39       Security.addProvider(Conscrypt.newProvider());
40       conscryptProviderAdded = true;
41     } catch (Exception | UnsatisfiedLinkError e) {
42       conscryptProviderAdded = false;
43     }
44   }
45 
46   @Test
validateUsesConscrypt_doesNotThrowIfConscryptProviderIsAdded()47   public void validateUsesConscrypt_doesNotThrowIfConscryptProviderIsAdded() throws Exception {
48     if (TestUtil.isAndroid()) {
49       // Android uses Conscrypt by default, but trying to add it manually fails.
50       assertThat(conscryptProviderAdded).isFalse();
51     } else {
52       assertThat(conscryptProviderAdded).isTrue();
53     }
54     Random.validateUsesConscrypt();
55   }
56 
57   @Test
randBytes_works()58   public void randBytes_works() throws Exception {
59     assertThat(Random.randBytes(10)).hasLength(10);
60   }
61 
62   @Test
randIntWithMax_works()63   public void randIntWithMax_works() throws Exception {
64     assertThat(Random.randInt(5)).isLessThan(5);
65   }
66 
67   @Test
randInt_works()68   public void randInt_works() throws Exception {
69     int unused = Random.randInt();
70   }
71 
72   @Test
randBytes_areDifferent()73   public void randBytes_areDifferent() throws Exception {
74     assertThat(Random.randBytes(32)).isNotEqualTo(Random.randBytes(32));
75   }
76 
77   @Test
randomBytesInDifferentThreads_areDifferent()78   public void randomBytesInDifferentThreads_areDifferent() throws Exception {
79     ArrayList<Thread> threads = new ArrayList<>();
80     final byte[] b0 = new byte[10];
81     final byte[] b1 = new byte[10];
82     threads.add(
83         new Thread() {
84           @Override
85           public void run() {
86             System.arraycopy(Random.randBytes(10), 0, b0, 0, 10);
87           }
88         });
89     threads.add(
90         new Thread() {
91           @Override
92           public void run() {
93             System.arraycopy(Random.randBytes(10), 0, b1, 0, 10);
94           }
95         });
96     for (Thread thread : threads) {
97       thread.start();
98     }
99     for (Thread thread : threads) {
100       thread.join();
101     }
102     assertThat(b0).isNotEqualTo(b1);
103   }
104 }
105