Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add state to integration tests #147

Merged
merged 4 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.yubico.yubikit.testing;

import com.yubico.yubikit.testing.mpe.MultiProtocolResetTests;
import com.yubico.yubikit.testing.oath.OathTests;
import com.yubico.yubikit.testing.openpgp.OpenPgpTests;
import com.yubico.yubikit.testing.piv.PivTests;
Expand All @@ -30,7 +31,8 @@
@Suite.SuiteClasses({
PivTests.class,
OpenPgpTests.class,
OathTests.class
OathTests.class,
MultiProtocolResetTests.class
})
public class DeviceTests {
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.yubico.yubikit.fido.ctap.PinUvAuthProtocol;
import com.yubico.yubikit.fido.ctap.PinUvAuthProtocolV1;
import com.yubico.yubikit.fido.ctap.PinUvAuthProtocolV2;
import com.yubico.yubikit.testing.PinComplexityDeviceTests;
import com.yubico.yubikit.testing.framework.FidoInstrumentedTests;

import org.junit.Test;
Expand Down Expand Up @@ -54,12 +53,6 @@ public void testSetPinProtocolV1() throws Throwable {
);
}

@Test
public void testPinComplexityPin() throws Throwable {
withCtap2Session(
PinComplexityDeviceTests::testFidoPinComplexity);
}

@Test
public void testSetPinProtocolV2() throws Throwable {
final PinUvAuthProtocol pinUvAuthProtocol = new PinUvAuthProtocolV2();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (C) 2024 Yubico.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.yubico.yubikit.testing.mpe;

import com.yubico.yubikit.core.smartcard.scp.ScpKid;
import com.yubico.yubikit.testing.framework.MpeInstrumentedTests;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;

import javax.annotation.Nullable;

@RunWith(Suite.class)
@Suite.SuiteClasses({
MultiProtocolResetTests.NoScpTests.class,
MultiProtocolResetTests.Scp11bTests.class,
})
public class MultiProtocolResetTests {
public static class NoScpTests extends MpeInstrumentedTests {
@Test
public void testSettingPivPinBlocksFidoReset() throws Throwable {
withPivSession(MultiProtocolResetDeviceTests::testSettingPivPinBlocksFidoReset);
}

@Test
public void testPivOperationBlocksFidoReset() throws Throwable {
withPivSession(MultiProtocolResetDeviceTests::testPivOperationBlocksFidoReset);
}

@Test
public void testSettingFidoPinBlocksPivReset() throws Throwable {
withCtap2Session(MultiProtocolResetDeviceTests::testSettingFidoPinBlocksPivReset);
}
}

public static class Scp11bTests extends NoScpTests {
@Nullable
@Override
protected Byte getScpKid() {
return ScpKid.SCP11b;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ public static class NoScpTests extends OathInstrumentedTests {
@Test
@Category(SmokeTest.class)
public void testChangePassword() throws Throwable {
withOathSession(OathDeviceTests::testChangePassword);
withOathSession(OathDeviceTests::testChangePasswordAfterReconnect);
withDevice(OathDeviceTests::testChangePassword);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

package com.yubico.yubikit.testing.openpgp;

import javax.annotation.Nullable;

import com.yubico.yubikit.core.smartcard.scp.ScpKid;
import com.yubico.yubikit.testing.PinComplexityDeviceTests;
import com.yubico.yubikit.testing.SlowTest;
import com.yubico.yubikit.testing.SmokeTest;
import com.yubico.yubikit.testing.framework.OpenPgpInstrumentedTests;
Expand All @@ -29,6 +26,8 @@
import org.junit.runner.RunWith;
import org.junit.runners.Suite;

import javax.annotation.Nullable;

@RunWith(Suite.class)
@Suite.SuiteClasses({
OpenPgpTests.NoScpTests.class,
Expand Down Expand Up @@ -141,7 +140,7 @@ public void testSetUif() throws Throwable {

@Test
public void testPinComplexity() throws Throwable {
withOpenPgpSession(PinComplexityDeviceTests::testOpenPgpPinComplexity);
withOpenPgpSession(OpenPgpDeviceTests::testPinComplexity);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.yubico.yubikit.testing.piv;

import com.yubico.yubikit.core.smartcard.scp.ScpKid;
import com.yubico.yubikit.testing.PinComplexityDeviceTests;
import com.yubico.yubikit.testing.SmokeTest;
import com.yubico.yubikit.testing.framework.PivInstrumentedTests;

Expand Down Expand Up @@ -71,7 +70,7 @@ public void testPutCompressedCertificate() throws Throwable {

@Test
public void testPinComplexity() throws Throwable {
withPivSession(PinComplexityDeviceTests::testPivPinComplexity);
withPivSession(PivPinComplexityDeviceTests::testPinComplexity);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2022-2024 Yubico.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.yubico.yubikit.testing.framework;

import com.yubico.yubikit.fido.ctap.Ctap2Session;
import com.yubico.yubikit.piv.PivSession;
import com.yubico.yubikit.testing.TestState;
import com.yubico.yubikit.testing.mpe.MpeTestState;

public class MpeInstrumentedTests extends YKInstrumentedTests {

protected void withPivSession(TestState.StatefulSessionCallback<PivSession, MpeTestState> callback) throws Throwable {
final MpeTestState state = new MpeTestState.Builder(device).scpKid(getScpKid()).build();
state.withPiv(callback);
}

protected void withCtap2Session(TestState.StatefulSessionCallback<Ctap2Session, MpeTestState> callback) throws Throwable {
final MpeTestState state = new MpeTestState.Builder(device).scpKid(getScpKid()).build();
state.withCtap2(callback);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,23 @@

package com.yubico.yubikit.testing.framework;

import com.yubico.yubikit.core.smartcard.SmartCardConnection;
import com.yubico.yubikit.oath.OathSession;
import com.yubico.yubikit.testing.StaticTestState;
import com.yubico.yubikit.testing.oath.OathTestUtils;

import org.junit.Before;
import com.yubico.yubikit.testing.TestState;
import com.yubico.yubikit.testing.oath.OathTestState;

public class OathInstrumentedTests extends YKInstrumentedTests {

public interface Callback {
void invoke(OathSession value) throws Throwable;
}

private boolean shouldVerifyAndSetupSession = true;
protected void withDevice(TestState.StatefulDeviceCallback<OathTestState> callback) throws Throwable {
final OathTestState state = new OathTestState.Builder(device)
.scpKid(getScpKid())
.reconnectDeviceCallback(this::reconnectDevice)
.build();

@Before
public void initializeDeviceTest() {
shouldVerifyAndSetupSession = true;
state.withDeviceCallback(callback);
}

/**
* This method can be called several times during one test.
* <p>
* It will reset and setup the OATH session only the first time it is called.
* The subsequent calls will not reset the device. This simulates YubiKey disconnecting/connecting.
*/
protected void withOathSession(Callback callback) throws Throwable {
if (shouldVerifyAndSetupSession) {
OathTestUtils.verifyAndSetup(device);
shouldVerifyAndSetupSession = false;
} else {
OathTestUtils.updateFipsApprovedValue(device);
}

try (SmartCardConnection connection = device.openConnection(SmartCardConnection.class)) {
callback.invoke(new OathSession(connection, StaticTestState.scpParameters.getKeyParams()));
}
protected void withOathSession(TestState.StatefulSessionCallback<OathSession, OathTestState> callback) throws Throwable {
final OathTestState state = new OathTestState.Builder(device).scpKid(getScpKid()).build();
state.withOath(callback);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,13 @@

package com.yubico.yubikit.testing.framework;

import com.yubico.yubikit.core.smartcard.SmartCardConnection;
import com.yubico.yubikit.openpgp.OpenPgpSession;
import com.yubico.yubikit.testing.StaticTestState;
import com.yubico.yubikit.testing.openpgp.OpenPgpTestUtils;
import com.yubico.yubikit.testing.TestState;
import com.yubico.yubikit.testing.openpgp.OpenPgpTestState;

public class OpenPgpInstrumentedTests extends YKInstrumentedTests {

public interface Callback {
void invoke(OpenPgpSession value) throws Throwable;
}

protected void withOpenPgpSession(Callback callback) throws Throwable {

OpenPgpTestUtils.verifyAndSetup(device);

try (SmartCardConnection connection = device.openConnection(SmartCardConnection.class)) {
callback.invoke(new OpenPgpSession(connection, StaticTestState.scpParameters.getKeyParams()));
}
protected void withOpenPgpSession(TestState.StatefulSessionCallback<OpenPgpSession, OpenPgpTestState> callback) throws Throwable {
final OpenPgpTestState state = new OpenPgpTestState.Builder(device).scpKid(getScpKid()).build();
state.withOpenPgp(callback);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2023 Yubico.
* Copyright (C) 2022-2024 Yubico.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,25 +16,13 @@

package com.yubico.yubikit.testing.framework;

import com.yubico.yubikit.core.smartcard.SmartCardConnection;
import com.yubico.yubikit.piv.PivSession;
import com.yubico.yubikit.testing.StaticTestState;
import com.yubico.yubikit.testing.piv.PivTestUtils;

import com.yubico.yubikit.testing.TestState;
import com.yubico.yubikit.testing.piv.PivTestState;

public class PivInstrumentedTests extends YKInstrumentedTests {

public interface Callback {
void invoke(PivSession value) throws Throwable;
}

protected void withPivSession(Callback callback) throws Throwable {

PivTestUtils.verifyAndSetup(device);

try (SmartCardConnection c = device.openConnection(SmartCardConnection.class)) {
PivSession pivSession = new PivSession(c, StaticTestState.scpParameters.getKeyParams());
callback.invoke(pivSession);
}
protected void withPivSession(TestState.StatefulSessionCallback<PivSession, PivTestState> callback) throws Throwable {
final PivTestState state = new PivTestState.Builder(device).scpKid(getScpKid()).build();
state.withPiv(callback);
}
}
Loading