Skip to content

Commit c830ccc

Browse files
authored
Merge pull request #1127 from webauthn4j/refactor-test-code
Polish test code part.2
2 parents d52cb40 + 8693960 commit c830ccc

9 files changed

Lines changed: 489 additions & 265 deletions

webauthn4j-core/src/test/java/com/webauthn4j/authenticator/AuthenticatorImplTest.java

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,19 @@
3939
import static org.junit.jupiter.api.Assertions.assertThrows;
4040
import static org.mockito.Mockito.mock;
4141

42+
@Deprecated
4243
class AuthenticatorImplTest {
4344

4445
@Test
45-
void constructor_test() {
46+
void shouldCreateAuthenticatorWithCorrectAttributes() {
47+
// Given
4648
AttestedCredentialData attestedCredentialData = TestDataUtil.createAttestedCredentialData();
4749
AttestationStatement attestationStatement = TestAttestationStatementUtil.createFIDOU2FAttestationStatement();
50+
51+
// When
4852
CredentialRecord authenticator = TestDataUtil.createCredentialRecord(attestedCredentialData, attestationStatement);
4953

54+
// Then
5055
assertAll(
5156
() -> assertThat(authenticator.getAttestedCredentialData()).isEqualTo(attestedCredentialData),
5257
() -> assertThat(authenticator.getAttestationStatement()).isEqualTo(attestationStatement),
@@ -55,15 +60,28 @@ void constructor_test() {
5560
}
5661

5762
@Test
58-
void createFromRegistrationData_test() {
63+
void shouldCreateAuthenticatorFromRegistrationData() {
64+
// Given
5965
AttestationObject attestationObject = TestDataUtil.createAttestationObjectWithFIDOU2FAttestationStatement();
6066
byte[] attestationObjectBytes = new byte[32];
6167
CollectedClientData collectedClientData = mock(CollectedClientData.class);
6268
byte[] collectedClientDataBytes = new byte[128];
63-
AuthenticationExtensionsClientOutputs<RegistrationExtensionClientOutput> authenticationExtensionsClientOutputs = new AuthenticationExtensionsClientOutputs.BuilderForRegistration().build();
69+
AuthenticationExtensionsClientOutputs<RegistrationExtensionClientOutput> authenticationExtensionsClientOutputs =
70+
new AuthenticationExtensionsClientOutputs.BuilderForRegistration().build();
6471
Set<AuthenticatorTransport> transports = Collections.emptySet();
65-
RegistrationData registrationData = new RegistrationData(attestationObject, attestationObjectBytes, collectedClientData, collectedClientDataBytes, authenticationExtensionsClientOutputs, transports);
72+
RegistrationData registrationData = new RegistrationData(
73+
attestationObject,
74+
attestationObjectBytes,
75+
collectedClientData,
76+
collectedClientDataBytes,
77+
authenticationExtensionsClientOutputs,
78+
transports
79+
);
80+
81+
// When
6682
AuthenticatorImpl authenticator = AuthenticatorImpl.createFromRegistrationData(registrationData);
83+
84+
// Then
6785
assertThat(authenticator.getAttestedCredentialData()).isEqualTo(attestationObject.getAuthenticatorData().getAttestedCredentialData());
6886
assertThat(authenticator.getAttestationStatement()).isEqualTo(attestationObject.getAttestationStatement());
6987
assertThat(authenticator.getTransports()).isEqualTo(transports);
@@ -73,20 +91,30 @@ void createFromRegistrationData_test() {
7391
}
7492

7593
@Test
76-
void getter_setter_test() {
94+
void shouldUpdateAuthenticatorPropertiesCorrectly() {
95+
// Given
7796
AttestedCredentialData attestedCredentialData = TestDataUtil.createAttestedCredentialData();
7897
AttestationStatement attestationStatement = TestAttestationStatementUtil.createFIDOU2FAttestationStatement();
79-
AuthenticatorImpl authenticator = new AuthenticatorImpl(TestDataUtil.createAttestedCredentialData(), TestAttestationStatementUtil.createBasicPackedAttestationStatement(), 0);
80-
AuthenticationExtensionsAuthenticatorOutputs<RegistrationExtensionAuthenticatorOutput> authenticatorExtensions = new AuthenticationExtensionsAuthenticatorOutputs<>();
81-
AuthenticationExtensionsClientOutputs<RegistrationExtensionClientOutput> clientExtensions = new AuthenticationExtensionsClientOutputs<>();
98+
AuthenticatorImpl authenticator = new AuthenticatorImpl(
99+
TestDataUtil.createAttestedCredentialData(),
100+
TestAttestationStatementUtil.createBasicPackedAttestationStatement(),
101+
0
102+
);
103+
AuthenticationExtensionsAuthenticatorOutputs<RegistrationExtensionAuthenticatorOutput> authenticatorExtensions =
104+
new AuthenticationExtensionsAuthenticatorOutputs<>();
105+
AuthenticationExtensionsClientOutputs<RegistrationExtensionClientOutput> clientExtensions =
106+
new AuthenticationExtensionsClientOutputs<>();
82107
Set<AuthenticatorTransport> transports = Collections.singleton(AuthenticatorTransport.USB);
108+
109+
// When
83110
authenticator.setAttestedCredentialData(attestedCredentialData);
84111
authenticator.setAttestationStatement(attestationStatement);
85112
authenticator.setTransports(transports);
86113
authenticator.setCounter(1);
87114
authenticator.setAuthenticatorExtensions(authenticatorExtensions);
88115
authenticator.setClientExtensions(clientExtensions);
89116

117+
// Then
90118
assertAll(
91119
() -> assertThat(authenticator.getAttestedCredentialData()).isEqualTo(attestedCredentialData),
92120
() -> assertThat(authenticator.getAttestationStatement()).isEqualTo(attestationStatement),
@@ -98,24 +126,34 @@ void getter_setter_test() {
98126
}
99127

100128
@Test
101-
void setCounter_range_test() {
102-
AuthenticatorImpl authenticator = new AuthenticatorImpl(TestDataUtil.createAttestedCredentialData(), TestAttestationStatementUtil.createBasicPackedAttestationStatement(), 0);
129+
void shouldRejectInvalidCounterValues() {
130+
// Given
131+
AuthenticatorImpl authenticator = new AuthenticatorImpl(
132+
TestDataUtil.createAttestedCredentialData(),
133+
TestAttestationStatementUtil.createBasicPackedAttestationStatement(),
134+
0
135+
);
103136

137+
// Then
104138
assertAll(
139+
// When counter is negative
105140
() -> assertThrows(IllegalArgumentException.class,
106141
() -> authenticator.setCounter(-1)
107142
),
143+
// When counter exceeds maximum value
108144
() -> assertThrows(IllegalArgumentException.class,
109145
() -> authenticator.setCounter(4294967296L)
110146
)
111147
);
112148
}
113149

114150
@Test
115-
void equals_hashCode_test() {
151+
void shouldHaveCorrectEqualsAndHashCodeImplementation() {
152+
// Given
116153
Authenticator authenticatorA = TestDataUtil.createCredentialRecord();
117154
Authenticator authenticatorB = TestDataUtil.createCredentialRecord();
118155

156+
// Then
119157
assertAll(
120158
() -> assertThat(authenticatorA).isEqualTo(authenticatorB),
121159
() -> assertThat(authenticatorA).hasSameHashCodeAs(authenticatorB)

webauthn4j-core/src/test/java/com/webauthn4j/authenticator/AuthenticatorTest.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,17 @@
3838
import java.util.Set;
3939

4040
import static org.assertj.core.api.Assertions.assertThat;
41+
import static org.junit.jupiter.api.Assertions.assertAll;
4142

43+
/**
44+
* Test for {@link Authenticator} interface implementation
45+
*/
46+
@Deprecated
4247
class AuthenticatorTest {
4348

4449
@Test
45-
void serialization_deserialization_test() {
50+
void shouldSerializeAndDeserializeCorrectly() {
51+
// Given
4652
ObjectConverter objectConverter = new ObjectConverter();
4753
CborConverter cborConverter = objectConverter.getCborConverter();
4854

@@ -53,12 +59,26 @@ void serialization_deserialization_test() {
5359
Collections.emptySet(),
5460
null,
5561
null);
62+
63+
// When
5664
byte[] serialized = cborConverter.writeValueAsBytes(original);
5765
TestAuthenticator deserialized = cborConverter.readValue(serialized, TestAuthenticator.class);
5866

59-
assertThat(deserialized).isEqualTo(original);
67+
// Then
68+
assertAll(
69+
() -> assertThat(deserialized).isEqualTo(original),
70+
() -> assertThat(deserialized.getAttestedCredentialData()).isEqualTo(original.getAttestedCredentialData()),
71+
() -> assertThat(deserialized.getAttestationStatement()).isEqualTo(original.getAttestationStatement()),
72+
() -> assertThat(deserialized.getCounter()).isEqualTo(original.getCounter()),
73+
() -> assertThat(deserialized.getTransports()).isEqualTo(original.getTransports()),
74+
() -> assertThat(deserialized.getClientExtensions()).isEqualTo(original.getClientExtensions()),
75+
() -> assertThat(deserialized.getAuthenticatorExtensions()).isEqualTo(original.getAuthenticatorExtensions())
76+
);
6077
}
6178

79+
/**
80+
* Test implementation of {@link Authenticator} interface for serialization/deserialization testing
81+
*/
6282
private static class TestAuthenticator implements Authenticator {
6383

6484
private final AttestedCredentialData attestedCredentialData;
@@ -146,5 +166,4 @@ public int hashCode() {
146166
return Objects.hash(attestedCredentialData, attestationStatement, transports, counter, clientExtensions, authenticatorExtensions);
147167
}
148168
}
149-
150169
}

webauthn4j-core/src/test/java/com/webauthn4j/authenticator/CoreAuthenticatorImplTest.java

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,56 @@
1818

1919
import com.webauthn4j.data.CoreRegistrationData;
2020
import com.webauthn4j.data.attestation.AttestationObject;
21+
import com.webauthn4j.data.attestation.authenticator.AttestedCredentialData;
2122
import com.webauthn4j.test.TestDataUtil;
2223
import org.junit.jupiter.api.Test;
2324

2425
import static org.assertj.core.api.Assertions.assertThat;
2526
import static org.assertj.core.api.Assertions.assertThatCode;
27+
import static org.junit.jupiter.api.Assertions.assertAll;
2628

29+
/**
30+
* Test for {@link CoreAuthenticatorImpl} class
31+
*/
32+
@Deprecated
2733
class CoreAuthenticatorImplTest {
2834

2935
@Test
30-
void constructor_attestationStatement_null_test(){
31-
assertThatCode(()->{
32-
new CoreAuthenticatorImpl(TestDataUtil.createAttestedCredentialData(), null, 0, null);
36+
void constructorShouldAcceptNullAttestationStatement() {
37+
// Given
38+
AttestedCredentialData attestedCredentialData = TestDataUtil.createAttestedCredentialData();
39+
40+
// When/Then
41+
assertThatCode(() -> {
42+
new CoreAuthenticatorImpl(attestedCredentialData, null, 0, null);
3343
}).doesNotThrowAnyException();
3444
}
3545

3646
@Test
37-
void createFromRegistrationData_test() {
47+
void shouldCreateFromCoreRegistrationDataCorrectly() {
48+
// Given
3849
AttestationObject attestationObject = TestDataUtil.createAttestationObjectWithFIDOU2FAttestationStatement();
3950
byte[] attestationObjectBytes = new byte[32];
4051
byte[] clientDataHash = new byte[32];
41-
CoreRegistrationData registrationData = new CoreRegistrationData(attestationObject, attestationObjectBytes, clientDataHash);
42-
CoreAuthenticator authenticator = AuthenticatorImpl.createFromCoreRegistrationData(registrationData);
43-
assertThat(authenticator.getAttestedCredentialData()).isEqualTo(attestationObject.getAuthenticatorData().getAttestedCredentialData());
44-
assertThat(authenticator.getAttestationStatement()).isEqualTo(attestationObject.getAttestationStatement());
45-
assertThat(authenticator.getCounter()).isEqualTo(attestationObject.getAuthenticatorData().getSignCount());
46-
assertThat(authenticator.getAuthenticatorExtensions()).isEqualTo(attestationObject.getAuthenticatorData().getExtensions());
52+
CoreRegistrationData registrationData = new CoreRegistrationData(
53+
attestationObject,
54+
attestationObjectBytes,
55+
clientDataHash
56+
);
57+
58+
// When
59+
CoreAuthenticator authenticator = CoreAuthenticatorImpl.createFromCoreRegistrationData(registrationData);
60+
61+
// Then
62+
assertAll(
63+
() -> assertThat(authenticator.getAttestedCredentialData())
64+
.isEqualTo(attestationObject.getAuthenticatorData().getAttestedCredentialData()),
65+
() -> assertThat(authenticator.getAttestationStatement())
66+
.isEqualTo(attestationObject.getAttestationStatement()),
67+
() -> assertThat(authenticator.getCounter())
68+
.isEqualTo(attestationObject.getAuthenticatorData().getSignCount()),
69+
() -> assertThat(authenticator.getAuthenticatorExtensions())
70+
.isEqualTo(attestationObject.getAuthenticatorData().getExtensions())
71+
);
4772
}
4873
}

webauthn4j-core/src/test/java/com/webauthn4j/converter/AttestedCredentialDataConverterTest.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,36 +33,41 @@ class AttestedCredentialDataConverterTest {
3333
private final AttestedCredentialDataConverter target = new AttestedCredentialDataConverter(objectConverter);
3434

3535
@Test
36-
void convert_test() {
37-
//Given
36+
void shouldConvertFromBytesToAttestedCredentialData() {
37+
// Given
3838
//noinspection SpellCheckingInspection
3939
String input = "VQ5LVKpHQJ-alRq3bBMBMQAgcSLOLIaiEIVRz-EklkZ21K71OGcRvvgro1kLdT4pvCClAQIDJiABIVggLDjE-Yci-q4NHPYpTPLJCVkWFkxuL6Zz9jKUvWjnmM8iWCAZAjkRJgA59HxAzqq5NBKjKGNkRPzToDfI6gJR7YBYkQ";
40-
//When
40+
41+
// When
4142
AttestedCredentialData attestedCredentialData = target.convert(Base64UrlUtil.decode(input));
4243

44+
// Then
4345
assertThat(attestedCredentialData.getAaguid().getBytes()).isEqualTo(Base64UrlUtil.decode("VQ5LVKpHQJ-alRq3bBMBMQ"));
4446
assertThat(attestedCredentialData.getCredentialId()).isEqualTo(Base64UrlUtil.decode("cSLOLIaiEIVRz-EklkZ21K71OGcRvvgro1kLdT4pvCA"));
45-
4647
}
4748

4849
@Test
49-
void convert_null_test() {
50+
void shouldThrowExceptionWhenConvertingNullValues() {
51+
// Given
52+
// No setup needed
53+
54+
// When/Then
5055
assertThatThrownBy(() -> target.convert((AttestedCredentialData) null)).isInstanceOf(DataConversionException.class);
5156
assertThatThrownBy(() -> target.convert((ByteBuffer) null)).isInstanceOf(DataConversionException.class);
5257
assertThatThrownBy(() -> target.convert((byte[]) null)).isInstanceOf(DataConversionException.class);
5358
}
5459

5560
@Test
56-
void extractCredentialId_test() {
57-
//Given
61+
void shouldExtractCredentialIdFromAttestedCredentialData() {
62+
// Given
5863
//noinspection SpellCheckingInspection
5964
String input = "VQ5LVKpHQJ-alRq3bBMBMQAgcSLOLIaiEIVRz-EklkZ21K71OGcRvvgro1kLdT4pvCC_YTFhMmEzJmItMQFiLTJYICw4xPmHIvquDRz2KUzyyQlZFhZMbi-mc_YylL1o55jPYi0zWCAZAjkRJgA59HxAzqq5NBKjKGNkRPzToDfI6gJR7YBYkWExAv8";
6065

61-
//When
66+
// When
6267
byte[] result = target.extractCredentialId(Base64UrlUtil.decode(input));
6368

69+
// Then
6470
assertThat(result).isEqualTo(Base64UrlUtil.decode("cSLOLIaiEIVRz-EklkZ21K71OGcRvvgro1kLdT4pvCA"));
65-
6671
}
6772

6873
}

webauthn4j-core/src/test/java/com/webauthn4j/converter/AuthenticationExtensionsClientInputsConverterTest.java

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,47 @@ class AuthenticationExtensionsClientInputsConverterTest {
3535

3636
@SuppressWarnings("ConstantConditions")
3737
@Test
38-
void convertRegistrationExtensions_null_test() {
38+
void shouldThrowExceptionWhenConvertingNullRegistrationExtensions() {
39+
// Given
40+
// No setup needed
41+
42+
// When/Then
3943
assertThatThrownBy(() -> target.convert(null)).isInstanceOf(DataConversionException.class);
4044
}
4145

4246
@Test
43-
void convertAuthenticationExtensionsToString_test() {
47+
void shouldConvertAuthenticationExtensionsToJsonString() {
48+
// Given
4449
AuthenticationExtensionsClientInputs.BuilderForAuthentication builder = new AuthenticationExtensionsClientInputs.BuilderForAuthentication();
4550
builder.setAppid("test");
4651
AuthenticationExtensionsClientInputs<AuthenticationExtensionClientInput> extensions = builder.build();
47-
assertThat(target.convertToString(extensions)).isEqualTo("{\"appid\":\"test\"}");
52+
53+
// When
54+
String result = target.convertToString(extensions);
55+
56+
// Then
57+
assertThat(result).isEqualTo("{\"appid\":\"test\"}");
4858
}
4959

5060
@SuppressWarnings("ConstantConditions")
5161
@Test
52-
void convertAuthenticationExtensionsToString_null_test() {
62+
void shouldThrowExceptionWhenConvertingNullExtensionsToString() {
63+
// Given
64+
// No setup needed
65+
66+
// When/Then
5367
assertThatThrownBy(() -> target.convertToString(null)).isInstanceOf(DataConversionException.class);
5468
}
5569

5670
@Test
57-
void convert_test() {
71+
void shouldConvertJsonStringToAuthenticationExtensionsClientInputs() {
72+
// Given
5873
String source = "{\"appid\":\"testappid\",\"appidExclude\":\"testappidexclude\",\"uvm\":true,\"hmacGetSecret\":{\"salt1\":\"AA\",\"salt2\":\"AQ\"},\"myextension\":\"test\"}";
74+
75+
// When
5976
AuthenticationExtensionsClientInputs<AuthenticationExtensionClientInput> clientInputs = target.convert(source);
77+
78+
// Then
6079
assertThat(clientInputs.getExtension(FIDOAppIDExtensionClientInput.class)).isEqualTo(new FIDOAppIDExtensionClientInput("testappid"));
6180
assertThat(clientInputs.getExtension(FIDOAppIDExclusionExtensionClientInput.class)).isEqualTo(new FIDOAppIDExclusionExtensionClientInput("testappidexclude"));
6281
assertThat(clientInputs.getExtension(UserVerificationMethodExtensionClientInput.class)).isEqualTo(new UserVerificationMethodExtensionClientInput(true));
@@ -65,18 +84,27 @@ void convert_test() {
6584
}
6685

6786
@Test
68-
void convert_null_test() {
87+
void shouldReturnNullWhenConvertingNullJsonString() {
88+
// Given
6989
String source = "null";
90+
91+
// When
7092
AuthenticationExtensionsClientInputs<AuthenticationExtensionClientInput> clientInputs = target.convert(source);
93+
94+
// Then
7195
assertThat(clientInputs).isNull();
7296
}
7397

7498
@SuppressWarnings("unused")
7599
@Test
76-
void convert_with_invalid_extension_test() {
100+
void shouldHandleInvalidExtensionsInJsonString() {
101+
// Given
77102
String source = "{\"invalid\":\"\"}";
103+
104+
// When
78105
AuthenticationExtensionsClientInputs<RegistrationExtensionClientInput> extensions = target.convert(source);
106+
107+
// Then
79108
assertThat(extensions.getUnknownKeys()).contains("invalid");
80-
81109
}
82110
}

0 commit comments

Comments
 (0)