Skip to content

Commit 6846294

Browse files
authored
Merge pull request #9 from martelkr/6-add-gtest-and-ctest-usage
6 add gtest and ctest usage
2 parents d7b2a63 + 9332f3b commit 6846294

File tree

9 files changed

+473
-343
lines changed

9 files changed

+473
-343
lines changed

.github/workflows/clang.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: cmake
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
pull_request:
7+
branches: [ "main" ]
8+
9+
env:
10+
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
11+
BUILD_TYPE: Release
12+
13+
jobs:
14+
Linux-clang:
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- uses: actions/checkout@v3
19+
20+
- name: Install gtest manually
21+
run: sudo apt-get install libgtest-dev && cd /usr/src/gtest && sudo cmake CMakeLists.txt && sudo make && sudo cp lib/*.a /usr/lib && sudo ln -s /usr/lib/libgtest.a /usr/local/lib/libgtest.a && sudo ln -s /usr/lib/libgtest_main.a /usr/local/lib/libgtest_main.a
22+
23+
- name: Write files
24+
env:
25+
FILE_ONE: ${{ secrets.KRM_MAIN_FILE }}
26+
FILE_TWO: ${{ secrets.KRM_SCND_FILE }}
27+
run: |
28+
echo "${FILE_ONE}" | base64 --decode > ${HOME}/key.pem |
29+
echo "${FILE_TWO}" | base64 --decode > ${HOME}/scert.crt
30+
31+
- name: Build and Test
32+
run: |
33+
cmake . -B ${{github.workspace}}/build -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
34+
cmake --build ${{github.workspace}}/build
35+
ctest -VV --test-dir ${{github.workspace}}/build
36+
37+
- name: Generate a code coverage report
38+
uses: threeal/gcovr-action@latest
39+
with:
40+
gcov-executable: llvm-cov gcov
41+

.github/workflows/cmake.yml

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: CMake
1+
name: cmake
22

33
on:
44
push:
@@ -11,27 +11,66 @@ env:
1111
BUILD_TYPE: Release
1212

1313
jobs:
14-
build:
15-
# The CMake configure and build commands are platform agnostic and should work equally well on Windows or Mac.
16-
# You can convert this to a matrix build if you need cross-platform coverage.
17-
# See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix
14+
Linux:
1815
runs-on: ubuntu-latest
1916

2017
steps:
2118
- uses: actions/checkout@v3
2219

23-
- name: Configure CMake
24-
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
25-
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
26-
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
20+
- name: Install gtest
21+
run: sudo apt-get install libgtest-dev && cd /usr/src/gtest && sudo cmake CMakeLists.txt && sudo make && sudo cp lib/*.a /usr/lib && sudo ln -s /usr/lib/libgtest.a /usr/local/lib/libgtest.a && sudo ln -s /usr/lib/libgtest_main.a /usr/local/lib/libgtest_main.a
2722

28-
- name: Build
29-
# Build your program with the given configuration
30-
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
23+
- name: Write files
24+
env:
25+
FILE_ONE: ${{ secrets.KRM_MAIN_FILE }}
26+
FILE_TWO: ${{ secrets.KRM_SCND_FILE }}
27+
run: |
28+
echo "${FILE_ONE}" | base64 --decode > ${HOME}/key.pem |
29+
echo "${FILE_TWO}" | base64 --decode > ${HOME}/scert.crt
3130
32-
- name: Test
33-
working-directory: ${{github.workspace}}/build
34-
# Execute tests defined by the CMake configuration.
35-
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
36-
run: ctest -C ${{env.BUILD_TYPE}}
31+
- name: Build and test
32+
run: |
33+
cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
34+
cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
35+
ctest -VV --test-dir ${{github.workspace}}/build -C ${{env.BUILD_TYPE}}
3736
37+
Windows:
38+
runs-on: windows-latest
39+
40+
steps:
41+
- uses: actions/checkout@v3
42+
- uses: ilammy/setup-nasm@v1
43+
- uses: ilammy/msvc-dev-cmd@v1
44+
45+
- name: Write files
46+
env:
47+
FILE_ONE: ${{ secrets.KRM_WIN_MAIN_FILE }}
48+
FILE_TWO: ${{ secrets.KRM_WIN_SCND_FILE }}
49+
shell: bash
50+
run: |
51+
echo "${FILE_ONE}" | openssl base64 -d > ${HOME}/privatekey.key |
52+
echo "${FILE_TWO}" | openssl base64 -d > ${HOME}/certificate.crt
53+
54+
- name: Clone openssl
55+
uses: actions/checkout@v3
56+
with:
57+
repository: openssl/openssl
58+
path: D:/a/cppsocket/cppsocket/openssl
59+
ref: openssl-3.1.0
60+
61+
- name: Build and install OpenSSL
62+
working-directory: D:/a/cppsocket/cppsocket/openssl
63+
run: |
64+
perl Configure VC-WIN64A
65+
nmake
66+
nmake install
67+
68+
- name: Install gtest
69+
uses: MarkusJx/googletest-installer@v1.1.1
70+
71+
- name: Build and Test
72+
run: |
73+
cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
74+
cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
75+
ctest -VV --test-dir ${{github.workspace}}/build -C ${{env.BUILD_TYPE}}
76+

.github/workflows/profile.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: cmake
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
pull_request:
7+
branches: [ "main" ]
8+
9+
env:
10+
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
11+
BUILD_TYPE: Release
12+
13+
jobs:
14+
Profile:
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- uses: actions/checkout@v3
19+
20+
- name: Install gtest
21+
run: sudo apt-get install libgtest-dev && cd /usr/src/gtest && sudo cmake CMakeLists.txt && sudo make && sudo cp lib/*.a /usr/lib && sudo ln -s /usr/lib/libgtest.a /usr/local/lib/libgtest.a && sudo ln -s /usr/lib/libgtest_main.a /usr/local/lib/libgtest_main.a
22+
23+
- name: Write files
24+
env:
25+
FILE_ONE: ${{ secrets.KRM_MAIN_FILE }}
26+
FILE_TWO: ${{ secrets.KRM_SCND_FILE }}
27+
run: |
28+
echo "${FILE_ONE}" | base64 --decode > ${HOME}/key.pem |
29+
echo "${FILE_TWO}" | base64 --decode > ${HOME}/scert.crt
30+
31+
- name: Profile build and test
32+
run: |
33+
cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=PROFILE .
34+
cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
35+
ctest -VV ${{github.workspace}}/build -C ${{env.BUILD_TYPE}}
36+
37+
- name: Generate Test Report
38+
uses: threeal/gcovr-action@latest
39+
with:
40+
coveralls-send: true
41+
github-token: ${{ secrets.GITHUB_TOKEN }}
42+

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ CMakeLists.txt
4242
*.vcxproj*
4343
*.sln
4444
test/Debug
45+
test/Release
4546
test/bin
4647
x64
4748
/bin
48-
test/testcppsocket.dir
49+
test/testcppsocket.dir
50+
CTestTestfile.cmake
51+
test/CTestTestfile.cmake
52+
Testing
53+
test/Testing

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ project(cppsocket VERSION ${project_version} LANGUAGES CXX)
1313

1414
if (CMAKE_COMPILER_IS_GNUCXX)
1515
add_definitions(-DLINUX)
16+
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
17+
add_definitions(-DLINUX)
1618
else()
1719
add_definitions(-DWINDOWS)
1820
endif()
1921

2022
include_directories(inc)
2123

24+
enable_testing()
2225
add_subdirectory(test)

README.md

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1-
# TCP C++ client/server with SSL/TLS support (header file only)
1+
# C++ client/server with SSL/TLS support (header file only)
22
[![MIT license](https://img.shields.io/badge/license-MIT-blue.svg)](http://opensource.org/licenses/MIT)
3-
![Cmake Build](https://github.com/martelkr/cppsocket/actions/workflows/cmake.yml/badge.svg)
3+
![cmake Build](https://github.com/martelkr/cppsocket/actions/workflows/cmake.yml/badge.svg)
4+
![clang Build](https://github.com/martelkr/cppsocket/actions/workflows/clang.yml/badge.svg)
5+
[![Coverage Status](https://coveralls.io/repos/github/martelkr/cppsocket/badge.svg)](https://coveralls.io/github/martelkr/cppsocket)
46

57
## About
6-
This is a header file only implementation of a C++ TCP client/server with or without SSL/TLS for Linux only.
8+
This is a header file only implementation of a C++ client/server with or without SSL/TLS/DTLS.
79
The implementation uses OpenSSL and BSD API to implement the underlying socket interfaces.
810

911
Compilation has been tested with:
1012
- GCC 11.3.0 (GNU/Linux Ubuntu 22.04.1 LTS)
13+
- cmake 3.22.1
14+
- googletest 1.11.0-3
15+
- Visual Studio Community 2022 17.4.4 (64-bit) (Windows 11)
16+
- cmake 3.26.0-rc1
17+
- googletest 1.13.0
1118

1219
## Usage
20+
21+
### TCP server/client
22+
1323
Create a TCP server object for accepting TCP connections.
1424

1525
```cpp
@@ -20,7 +30,7 @@ TCPServer(void);
2030
TCPServer(const std::string& keyFile, const std::string& certFile);
2131

2232
// No SSL and IP/port bound
23-
explicit TCPServer(const uint16_t port, const std::string& ip = "", const int backlog = 3);
33+
explicit TCPServer(const uint16_t port, const std::string& ip = "0.0.0.0", const int backlog = 3);
2434

2535
/// SSL and IP/port bound
2636
TCPServer(const uint16_t port, const std::string& ip, const std::string& keyFile, const std::string& certFile, const int backlog = 3);
@@ -39,7 +49,7 @@ For a BSD-like approach, the following sequence can be followed:
3949
// Server
4050

4151
// create server socket
42-
TCPServer s;
52+
TCPServer s; // add key file and cert file here for secure connection
4353

4454
// bind to port 54321 on IP 0.0.0.0
4555
s.bindAndListen(54321);
@@ -51,20 +61,82 @@ TCPClient c = s.accept();
5161
// Client
5262

5363
// Connect to TCP server on IP 127.0.0.1 and port 54321
54-
TCPClient c("127.0.0.1", 54321);
64+
TCPClient c("127.0.0.1", 54321); // add key file and cert file here for secure connection
65+
```
66+
67+
### UDP server/client
68+
69+
Create a UDP server object for accepting UDP connections.
70+
71+
```cpp
72+
// default constructor creates unbound unsecure UDP server socket
73+
UDPServer(void);
74+
75+
// default DTLS constructor create unbound UDP server socket ready for DTLS
76+
// NOTE: UDPServer s("", ""); results in unbound unsecure UDP server socket
77+
UDPServer(const std::string& keyFile, const std::string& certFile);
78+
79+
// creates unsecure UDP server socket bound to specific port and IP address (default all host IP)
80+
explicit UDPServer(const uint16_t port, const std::string& ip = "0.0.0.0");
81+
82+
// creates bound UDP server socket ready for DTLS
83+
// NOTE: UDPServer s("", ""); results in unbound unsecure UDP server socket
84+
UDPServer(const uint16_t port, const std::string& ip, const std::string& keyFile, const std::string& certFile);
85+
```
86+
87+
Create a UDP client object to connect to a known UDP server.
88+
89+
```cpp
90+
91+
// default constructor creates unconnected UDP client socket
92+
UDPClient(void);
93+
94+
// creates UDP client socket connected to UDP server
95+
UDPClient(const std::string& remoteIp, const uint16_t remotePort);
96+
97+
// creates unconnected UDP client socket for DTLS communication
98+
UDPClient(const std::string& keyFile, const std::string& certFile);
99+
100+
// created UDP client socket connected to UDP server using DTLS
101+
UDPClient(const std::string& remoteIp, const uint16_t remotePort, const std::string& keyFile, const std::string& certFile);
102+
```
103+
104+
For a BSD-like approach, the following sequence can be followed:
105+
106+
```cpp
107+
// Server
108+
109+
// create server socket
110+
UDPServer s; // add key file and cert file here for secure connection
111+
112+
// bind to port 54321 on IP 0.0.0.0
113+
s.bind(54321);
114+
115+
// following not needed for unsecure connection but is needed for DTLS connection
116+
s.accept();
117+
```
118+
119+
```cpp
120+
// Client
121+
122+
// Connect to UDP server on IP 127.0.0.1 and port 54321
123+
UDPClient c("127.0.0.1", 54321); // add key file and cert file here for secure connection
55124
```
56125
57126
## Thread Safety
58127
59-
Do not share TCPClient objects across threads unless you provide your own thread safety on the send/read calls.
128+
Do not share TCPClient, UDPClient or UDPServer objects across threads unless you provide your own thread safety on the send/read calls.
60129
61130
## Installation
62131
63132
Use the `cppsocket.hpp` file in your source tree and include it in the file that need to use it.
64133
65134
## Run Unit Tests
66135
67-
There are two basic unit tests included in `test/TestCppSocket.cpp` to test basic data passing for secure and unsecure TCP sockets.
136+
Unit tests run with ctest:
137+
```
138+
ctest -C debug
139+
```
68140
69141
## CppCheck Compliancy
70142

0 commit comments

Comments
 (0)