• 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.aead;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.junit.runners.JUnit4;
24 
25 @RunWith(JUnit4.class)
26 public final class XChaCha20Poly1305ParametersTest {
27   private static final XChaCha20Poly1305Parameters.Variant NO_PREFIX =
28       XChaCha20Poly1305Parameters.Variant.NO_PREFIX;
29   private static final XChaCha20Poly1305Parameters.Variant TINK =
30       XChaCha20Poly1305Parameters.Variant.TINK;
31   private static final XChaCha20Poly1305Parameters.Variant CRUNCHY =
32       XChaCha20Poly1305Parameters.Variant.CRUNCHY;
33 
34   @Test
buildParametersAndGetProperties()35   public void buildParametersAndGetProperties() throws Exception {
36     XChaCha20Poly1305Parameters parameters = XChaCha20Poly1305Parameters.create();
37     assertThat(parameters.getVariant()).isEqualTo(NO_PREFIX);
38     assertThat(parameters.hasIdRequirement()).isFalse();
39   }
40 
41   @Test
buildParameters_setVariantExplicitly()42   public void buildParameters_setVariantExplicitly() throws Exception {
43     XChaCha20Poly1305Parameters parameters =
44         XChaCha20Poly1305Parameters.create(XChaCha20Poly1305Parameters.Variant.NO_PREFIX);
45     assertThat(parameters.getVariant()).isEqualTo(NO_PREFIX);
46     assertThat(parameters.hasIdRequirement()).isFalse();
47   }
48 
49   @Test
buildParameters_tink()50   public void buildParameters_tink() throws Exception {
51     XChaCha20Poly1305Parameters parameters =
52         XChaCha20Poly1305Parameters.create(XChaCha20Poly1305Parameters.Variant.TINK);
53     assertThat(parameters.getVariant()).isEqualTo(TINK);
54     assertThat(parameters.hasIdRequirement()).isTrue();
55   }
56 
57   @Test
buildParameters_crunchy()58   public void buildParameters_crunchy() throws Exception {
59     XChaCha20Poly1305Parameters parameters =
60         XChaCha20Poly1305Parameters.create(XChaCha20Poly1305Parameters.Variant.CRUNCHY);
61     assertThat(parameters.getVariant()).isEqualTo(CRUNCHY);
62     assertThat(parameters.hasIdRequirement()).isTrue();
63   }
64 
65   @Test
testEqualsAndEqualHashCode_noPrefix()66   public void testEqualsAndEqualHashCode_noPrefix() throws Exception {
67     XChaCha20Poly1305Parameters parametersNoPrefix0 = XChaCha20Poly1305Parameters.create();
68     XChaCha20Poly1305Parameters parametersNoPrefix1 =
69         XChaCha20Poly1305Parameters.create(XChaCha20Poly1305Parameters.Variant.NO_PREFIX);
70     assertThat(parametersNoPrefix0).isEqualTo(parametersNoPrefix1);
71     assertThat(parametersNoPrefix0.hashCode()).isEqualTo(parametersNoPrefix1.hashCode());
72   }
73 
74   @Test
testEqualsAndEqualHashCode_tink()75   public void testEqualsAndEqualHashCode_tink() throws Exception {
76     XChaCha20Poly1305Parameters parametersTink0 =
77         XChaCha20Poly1305Parameters.create(XChaCha20Poly1305Parameters.Variant.TINK);
78     XChaCha20Poly1305Parameters parametersTink1 =
79         XChaCha20Poly1305Parameters.create(XChaCha20Poly1305Parameters.Variant.TINK);
80     assertThat(parametersTink0).isEqualTo(parametersTink1);
81     assertThat(parametersTink0.hashCode()).isEqualTo(parametersTink1.hashCode());
82   }
83 
84   @Test
testEqualsAndEqualHashCode_crunchy()85   public void testEqualsAndEqualHashCode_crunchy() throws Exception {
86     XChaCha20Poly1305Parameters parametersCrunchy0 =
87         XChaCha20Poly1305Parameters.create(XChaCha20Poly1305Parameters.Variant.CRUNCHY);
88     XChaCha20Poly1305Parameters parametersCrunchy1 =
89         XChaCha20Poly1305Parameters.create(XChaCha20Poly1305Parameters.Variant.CRUNCHY);
90     assertThat(parametersCrunchy0).isEqualTo(parametersCrunchy1);
91     assertThat(parametersCrunchy0.hashCode()).isEqualTo(parametersCrunchy1.hashCode());
92   }
93 
94   @Test
testEqualsAndEqualHashCode_different()95   public void testEqualsAndEqualHashCode_different() throws Exception {
96     XChaCha20Poly1305Parameters parametersNoPrefix = XChaCha20Poly1305Parameters.create();
97 
98     XChaCha20Poly1305Parameters parametersTink =
99         XChaCha20Poly1305Parameters.create(XChaCha20Poly1305Parameters.Variant.TINK);
100 
101     XChaCha20Poly1305Parameters parametersCrunchy =
102         XChaCha20Poly1305Parameters.create(XChaCha20Poly1305Parameters.Variant.CRUNCHY);
103 
104     assertThat(parametersNoPrefix).isNotEqualTo(parametersTink);
105     assertThat(parametersNoPrefix.hashCode()).isNotEqualTo(parametersTink.hashCode());
106 
107     assertThat(parametersNoPrefix).isNotEqualTo(parametersCrunchy);
108     assertThat(parametersNoPrefix.hashCode()).isNotEqualTo(parametersCrunchy.hashCode());
109 
110     assertThat(parametersTink).isNotEqualTo(parametersNoPrefix);
111     assertThat(parametersTink.hashCode()).isNotEqualTo(parametersNoPrefix.hashCode());
112 
113     assertThat(parametersTink).isNotEqualTo(parametersCrunchy);
114     assertThat(parametersTink.hashCode()).isNotEqualTo(parametersCrunchy.hashCode());
115 
116     assertThat(parametersCrunchy).isNotEqualTo(parametersNoPrefix);
117     assertThat(parametersCrunchy.hashCode()).isNotEqualTo(parametersNoPrefix.hashCode());
118 
119     assertThat(parametersCrunchy).isNotEqualTo(parametersTink);
120     assertThat(parametersCrunchy.hashCode()).isNotEqualTo(parametersTink.hashCode());
121   }
122 }
123