Skip to content

Commit 68332fd

Browse files
committed
Use ResponseAPDU::isOK() instead generating compare operator
Signed-off-by: Raul Metsma <raul@metsma.ee>
1 parent 69be3b6 commit 68332fd

File tree

7 files changed

+25
-27
lines changed

7 files changed

+25
-27
lines changed

lib/libpcsc-cpp/include/pcsc-cpp/pcsc-cpp.hpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@
5454
#define PCSC_CPP_WARNING_DISABLE_MSVC(text)
5555
#endif
5656

57+
#ifdef __cpp_lib_constexpr_vector
58+
#define PCSC_CPP_CONSTEXPR_VECTOR constexpr
59+
#else
60+
#define PCSC_CPP_CONSTEXPR_VECTOR
61+
#endif
62+
5763
namespace pcsc_cpp
5864
{
5965

@@ -78,7 +84,7 @@ constexpr uint16_t toSW(byte_type sw1, byte_type sw2) noexcept
7884
/** Struct that wraps response APDUs. */
7985
struct ResponseApdu
8086
{
81-
enum Status {
87+
enum Status: byte_type {
8288
OK = 0x90,
8389
MORE_DATA_AVAILABLE = 0x61,
8490
VERIFICATION_FAILED = 0x63,
@@ -97,7 +103,7 @@ struct ResponseApdu
97103
static constexpr size_t MAX_DATA_SIZE = 256;
98104
static constexpr size_t MAX_SIZE = MAX_DATA_SIZE + 2; // + sw1 and sw2
99105

100-
static ResponseApdu fromBytes(byte_vector data)
106+
PCSC_CPP_CONSTEXPR_VECTOR static ResponseApdu fromBytes(byte_vector data)
101107
{
102108
if (data.size() < 2) {
103109
throw std::invalid_argument("Need at least 2 bytes for creating ResponseApdu");
@@ -129,8 +135,6 @@ struct ResponseApdu
129135

130136
constexpr bool isOK() const noexcept { return sw1 == OK && sw2 == 0x00; }
131137

132-
bool operator==(const ResponseApdu& other) const = default;
133-
134138
// TODO: friend function toString() in utilities.hpp
135139
};
136140

lib/libpcsc-cpp/src/SmartCard.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ class CardImpl
258258
{
259259
byte_vector getResponseCommand {0x00, 0xc0, 0x00, 0x00, 0x00};
260260

261-
ResponseApdu newResponse{response.sw1, response.sw2};
261+
ResponseApdu newResponse {response.sw1, response.sw2};
262262

263263
while (newResponse.sw1 == ResponseApdu::MORE_DATA_AVAILABLE) {
264264
getResponseCommand[4] = newResponse.sw2;

lib/libpcsc-cpp/src/utils.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,13 @@ const byte_type DER_TWO_BYTE_LENGTH = 0x82;
5454
class UnexpectedResponseError : public Error
5555
{
5656
public:
57-
explicit UnexpectedResponseError(const CommandApdu& command,
58-
const byte_vector& expectedResponseBytes,
59-
const ResponseApdu& response, const char* file, const int line,
57+
explicit UnexpectedResponseError(const CommandApdu& command, const ResponseApdu& response,
58+
const char* file, const int line,
6059
const char* callerFunctionName) :
6160
Error("transmitApduWithExpectedResponse(): Unexpected response to command '"s
62-
+ bytes2hexstr(command.toBytes()) + "' - expected '"s
63-
+ bytes2hexstr(expectedResponseBytes) + "', got '"s + bytes2hexstr(response.toBytes())
64-
+ " in " + removeAbsolutePathPrefix(file) + ':' + std::to_string(line) + ':'
65-
+ callerFunctionName)
61+
+ bytes2hexstr(command.toBytes()) + "' - expected '9000', got '"s
62+
+ bytes2hexstr(response.toBytes()) + " in " + removeAbsolutePathPrefix(file) + ':'
63+
+ std::to_string(line) + ':' + callerFunctionName)
6664
{
6765
}
6866
};
@@ -86,11 +84,9 @@ std::string bytes2hexstr(const byte_vector& bytes)
8684

8785
void transmitApduWithExpectedResponse(const SmartCard& card, const CommandApdu& command)
8886
{
89-
static const ResponseApdu APDU_RESPONSE_OK {ResponseApdu::OK, 0x00};
9087
const auto response = card.transmit(command);
91-
if (response != APDU_RESPONSE_OK) {
92-
throw UnexpectedResponseError(command, APDU_RESPONSE_OK.toBytes(), response, __FILE__,
93-
__LINE__, __func__);
88+
if (!response.isOK()) {
89+
throw UnexpectedResponseError(command, response, __FILE__, __LINE__, __func__);
9490
}
9591
}
9692

lib/libpcsc-cpp/tests/lib/libpcsc-mock/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.8.0)
1+
cmake_minimum_required(VERSION 3.10.0)
22
if(POLICY CMP0092)
33
cmake_policy(SET CMP0092 NEW)
44
endif()

lib/libpcsc-cpp/tests/lib/libpcsc-mock/scripts/clang-format.sh

100644100755
File mode changed.

lib/libpcsc-cpp/tests/lib/libpcsc-mock/src/pcsc-mock.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,19 @@ const PcscMock::string_t PcscMock::DEFAULT_READER_NAME {"PcscMock-reader"s};
8888
#endif
8989

9090
const PcscMock::byte_vector PcscMock::DEFAULT_COMMAND_APDU {0x2, 0x1, 0x3, 0x4};
91-
const PcscMock::byte_vector PcscMock::DEFAULT_RESPONSE_APDU {0x90, 0x3};
91+
const PcscMock::byte_vector PcscMock::DEFAULT_RESPONSE_APDU {0x90, 0x00};
9292

9393
const PcscMock::ApduScript PcscMock::DEFAULT_SCRIPT {{DEFAULT_COMMAND_APDU, DEFAULT_RESPONSE_APDU}};
9494

9595
#ifdef _MSC_VER
9696
#pragma warning(push)
9797
#pragma warning(disable : 4273)
98-
__declspec(dllexport) const SCARD_IO_REQUEST g_rgSCardT0Pci = {SCARD_PROTOCOL_T0,
99-
sizeof(SCARD_IO_REQUEST)};
100-
__declspec(dllexport) const SCARD_IO_REQUEST g_rgSCardT1Pci = {SCARD_PROTOCOL_T1,
101-
sizeof(SCARD_IO_REQUEST)};
102-
__declspec(dllexport) const SCARD_IO_REQUEST g_rgSCardRawPci = {SCARD_PROTOCOL_RAW,
103-
sizeof(SCARD_IO_REQUEST)};
98+
__declspec(dllexport)
99+
const SCARD_IO_REQUEST g_rgSCardT0Pci = {SCARD_PROTOCOL_T0, sizeof(SCARD_IO_REQUEST)};
100+
__declspec(dllexport)
101+
const SCARD_IO_REQUEST g_rgSCardT1Pci = {SCARD_PROTOCOL_T1, sizeof(SCARD_IO_REQUEST)};
102+
__declspec(dllexport)
103+
const SCARD_IO_REQUEST g_rgSCardRawPci = {SCARD_PROTOCOL_RAW, sizeof(SCARD_IO_REQUEST)};
104104
#pragma warning(pop)
105105
#else
106106
MOCK_CONST SCARD_IO_REQUEST g_rgSCardT0Pci = {SCARD_PROTOCOL_T0, sizeof(SCARD_IO_REQUEST)};

lib/libpcsc-cpp/tests/mock/test-connect-to-card-transmit-apdus.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,8 @@ TEST(pcsc_cpp_test, transmitApduSuccess)
5757
CommandApdu command {PcscMock::DEFAULT_COMMAND_APDU[0], PcscMock::DEFAULT_COMMAND_APDU[1],
5858
PcscMock::DEFAULT_COMMAND_APDU[2], PcscMock::DEFAULT_COMMAND_APDU[3]};
5959

60-
auto expectedResponse = ResponseApdu::fromBytes(PcscMock::DEFAULT_RESPONSE_APDU);
61-
6260
auto transactionGuard = card->beginTransaction();
6361
auto response = card->transmit(command);
6462

65-
EXPECT_EQ(response, expectedResponse);
63+
EXPECT_TRUE(response.isOK());
6664
}

0 commit comments

Comments
 (0)