Skip to content

Commit

Permalink
Merge libpcsc-cpp and libpcsc-mock repositories
Browse files Browse the repository at this point in the history
WE2-828

Signed-off-by: Raul Metsma <[email protected]>
  • Loading branch information
metsma authored and mrts committed Feb 8, 2024
1 parent 28a157e commit 4d935f1
Show file tree
Hide file tree
Showing 33 changed files with 4,141 additions and 49 deletions.
16 changes: 4 additions & 12 deletions .github/workflows/cmake-linux-ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,13 @@ jobs:
container: mrts/qt-cmake-gtest-valgrind-ubuntu:v1.8

steps:
- uses: actions/checkout@v2
with:
submodules: recursive

- name: Create build directory
run: mkdir build
- uses: actions/checkout@v4

- name: Configure CMake
working-directory: ${{github.workspace}}/build
run: cmake -DCMAKE_BUILD_TYPE=$BUILD_TYPE ..
run: cmake -DCMAKE_BUILD_TYPE=$BUILD_TYPE -S . -B build

- name: Build
working-directory: ${{github.workspace}}/build
run: cmake --build . --config $BUILD_TYPE
run: cmake --build build --config $BUILD_TYPE

- name: Test
working-directory: ${{github.workspace}}/build
run: ctest -V -C $BUILD_TYPE
run: ctest -V -C $BUILD_TYPE --test-dir build
20 changes: 5 additions & 15 deletions .github/workflows/cmake-macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,16 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v2
with:
submodules: recursive
uses: actions/checkout@v4

- name: Install libraries
run: |
brew install web-eid/gtest/gtest
ln -sf /usr/local/Cellar/[email protected]/1.1.1* /usr/local/opt/openssl
- name: Create build directory
run: mkdir build
run: brew install web-eid/gtest/gtest

- name: Configure CMake
working-directory: ${{github.workspace}}/build
run: cmake -DCMAKE_BUILD_TYPE=${BUILD_TYPE} ..
run: cmake -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -B build -S .

- name: Build
working-directory: ${{github.workspace}}/build
run: cmake --build . --config ${BUILD_TYPE}
run: cmake --build build --config ${BUILD_TYPE}

- name: Test
working-directory: ${{github.workspace}}/build
run: ctest -V -C ${BUILD_TYPE}
run: ctest -V -C ${BUILD_TYPE} --test-dir build
21 changes: 6 additions & 15 deletions .github/workflows/cmake-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,20 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v2
with:
submodules: recursive
uses: actions/checkout@v4

- name: Prepare vcpkg and libraries
uses: lukka/run-vcpkg@v6
uses: lukka/run-vcpkg@v7
with:
vcpkgArguments: gtest:x64-windows openssl:x64-windows
vcpkgTriplet: x64-windows
vcpkgDirectory: ${{runner.workspace}}/vcpkg/
vcpkgGitCommitId: 9b064ff07b987c8f213fdbf4358122fd4fe38ef1

- name: Create build directory
run: mkdir build
vcpkgGitCommitId: 1f619be01b436b796dab797dd1e1721c5676f8ac

- name: Configure CMake
working-directory: ${{github.workspace}}/build
run: cmake -A x64 "-DCMAKE_TOOLCHAIN_FILE=${{runner.workspace}}/vcpkg/scripts/buildsystems/vcpkg.cmake" "-DCMAKE_BUILD_TYPE=${env:BUILD_TYPE}" ..
run: cmake -A x64 "-DCMAKE_TOOLCHAIN_FILE=${env:VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" "-DCMAKE_BUILD_TYPE=${env:BUILD_TYPE}" -S . -B build

- name: Build
working-directory: ${{github.workspace}}/build
run: cmake --build . --config ${env:BUILD_TYPE}
run: cmake --build build --config ${env:BUILD_TYPE}

- name: Test
working-directory: ${{github.workspace}}/build
run: ctest -V -C ${env:BUILD_TYPE}
run: ctest -V -C ${env:BUILD_TYPE}--test-dir build
4 changes: 1 addition & 3 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
submodules: recursive
uses: actions/checkout@v4

- name: Initialize CodeQL
uses: github/codeql-action/init@v2
Expand Down
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
[submodule "lib/libpcsc-cpp"]
path = lib/libpcsc-cpp
url = ../libpcsc-cpp
1 change: 0 additions & 1 deletion lib/libpcsc-cpp
Submodule libpcsc-cpp deleted from fd79af
96 changes: 96 additions & 0 deletions lib/libpcsc-cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
cmake_minimum_required(VERSION 3.16)

project(pcsc-cpp)

# The libpcsc-cpp library itself.

add_library(${PROJECT_NAME}
STATIC
include/${PROJECT_NAME}/${PROJECT_NAME}.hpp
include/${PROJECT_NAME}/${PROJECT_NAME}-utils.hpp
include/${PROJECT_NAME}/comp_winscard.hpp
include/flag-set-cpp/flag_set.hpp
include/magic_enum/magic_enum.hpp
src/Context.hpp
src/Reader.cpp
src/SCardCall.hpp
src/SmartCard.cpp
src/listReaders.cpp
src/utils.cpp
)

target_include_directories(${PROJECT_NAME}
PUBLIC
include
)

target_compile_features(${PROJECT_NAME}
PUBLIC
cxx_std_17
)

target_compile_options(${PROJECT_NAME} PUBLIC
$<$<CXX_COMPILER_ID:MSVC>:/W4 /WX>
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wall -Wextra -pedantic -Werror>
)

target_compile_definitions(${PROJECT_NAME} PUBLIC
$<$<CXX_COMPILER_ID:MSVC>:WIN32_LEAN_AND_MEAN;UNICODE;_CRT_SECURE_NO_WARNINGS>
)

target_link_libraries(${PROJECT_NAME} PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:Ws2_32>
)

# PC/SC API dependencies.

add_library(pcsc INTERFACE)
if(MSVC)
target_link_libraries(pcsc INTERFACE winscard)
elseif(APPLE)
target_link_libraries(pcsc INTERFACE "-framework PCSC")
else()
find_package(PkgConfig REQUIRED)
pkg_check_modules(PCSC libpcsclite REQUIRED IMPORTED_TARGET)
target_include_directories(${PROJECT_NAME} PRIVATE ${PCSC_INCLUDE_DIRS})
target_link_libraries(pcsc INTERFACE PkgConfig::PCSC)
endif()

# Common testing options.

enable_testing()

find_package(GTest REQUIRED)

# Mock tests that use libpcsc-mock to mock PC/SC API calls.

set(MOCK_TEST_EXE lib${PROJECT_NAME}-test-mock)

add_subdirectory(tests/lib/libpcsc-mock)

add_executable(${MOCK_TEST_EXE}
tests/mock/test-select-card-reader-and-card.cpp
tests/mock/test-connect-to-card-transmit-apdus.cpp
)

target_link_libraries(${MOCK_TEST_EXE}
${PROJECT_NAME}
pcsc-mock
GTest::Main
)

add_test(${MOCK_TEST_EXE} ${MOCK_TEST_EXE})

# Integration tests that use the real operating system PC/SC service.

set(INTEGRATION_TEST_EXE lib${PROJECT_NAME}-test-integration)

add_executable(${INTEGRATION_TEST_EXE}
tests/integration/test-${PROJECT_NAME}.cpp
)

target_link_libraries(${INTEGRATION_TEST_EXE}
${PROJECT_NAME}
pcsc
GTest::Main
)
21 changes: 21 additions & 0 deletions lib/libpcsc-cpp/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020-2023 Estonian Information System Authority

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
48 changes: 48 additions & 0 deletions lib/libpcsc-cpp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# libpcsc-cpp

![European Regional Development Fund](https://github.com/open-eid/DigiDoc4-Client/blob/master/client/images/EL_Regionaalarengu_Fond.png)

C++ library for accessing smart cards using the PC/SC API.

## Usage

Example how to list available readers, connect to the smart card in first
reader and transmit an APDU:

auto readers = listReaders();
auto card = readers[0].connectToCard();
auto command = CommandApdu::fromBytes({0x2, 0x1, 0x3, 0x4});

auto transactionGuard = card->beginTransaction();
auto response = card->transmit(command);

See more examples in [tests](tests).

## Building

In Ubuntu:

apt install build-essential pkg-config cmake libgtest-dev valgrind libpcsclite-dev
sudo bash -c 'cd /usr/src/googletest && cmake . && cmake --build . --target install'

cd build
cmake .. # optionally with -DCMAKE_BUILD_TYPE=Debug
cmake --build . # optionally with VERBOSE=1

## Testing

Build as described above, then, inside the `build` directory, run:

ctest # or 'valgrind --leak-check=full ctest'

`ctest` runs tests that use the _libscard-mock_ library to mock PC/SC API calls.

There are also integration tests that use the real operating system PC/SC
service, run them inside `build` directory with:

./libpcsc-cpp-test-integration

## Development guidelines

- Format code with `scripts/clang-format.sh` before committing
- See [docs/DEVELOPMENT.md](docs/DEVELOPMENT.md)
8 changes: 8 additions & 0 deletions lib/libpcsc-cpp/docs/DEVELOPMENT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
TODO:

- coding conventions - Qt coding style and conventions
- gtest and winscard-mock usage
- API
- architecture
- technical design
- describe PIMPL (we don't want to leak `winscard.h` to clients)
Loading

0 comments on commit 4d935f1

Please sign in to comment.