diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000000000000000000000000000000000000..6f35ada2ca5f210505520aab5288777ee3d538d9 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,56 @@ +image: rikorose/gcc-cmake + +stages: + - build + - test + +before_script: + - g++ --version + +build: + stage: build + script: + - cmake . -DCMAKE_CXX_FLAGS="-Werror" + - make -j4 + - make clean + - cmake . -DCMAKE_CXX_FLAGS="-Wall -Wextra" # Build again, but with more warnings + - make -j4 + +tests: + stage: test + variables: + GIT_SUBMODULE_STRATEGY: normal + script: + - cmake . + - make tests -j4 + - ./tests --use-colour yes + after_script: + - ./tests -r junit -o junit.xml + artifacts: + reports: + junit: junit.xml + +cppcheck: + stage: build + before_script: + - apt-get update -qq && apt-get install -y -qq cppcheck + - cppcheck --version + script: + - bash -x ci/cppcheck.sh + +vera: + stage: build + before_script: + - apt-get update -qq && apt-get install -y -qq vera++ + - vera++ --version + - cp ci/vera.profile /usr/lib/vera++/profiles/custom + script: + - bash -x ci/vera.sh + +clang-tidy: + stage: build + before_script: + - apt-get update -qq && apt-get install -y -qq clang-tidy-4.0 + - clang-tidy-4.0 --version + script: + - bash -x ci/clang-tidy.sh diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000000000000000000000000000000000000..d49fb0620ae4893df6f92e7f878d7202c37cf84b --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "lib/Catch2"] + path = lib/Catch2 + url = https://github.com/catchorg/Catch2.git diff --git a/.idea/misc.xml b/.idea/misc.xml index 79b3c94830bab93d40d0770f2765540fe24ed423..91d4361702769bdfe6326464a61298263c79e5e3 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,13 @@ <?xml version="1.0" encoding="UTF-8"?> <project version="4"> <component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" /> + <component name="CidrRootsConfiguration"> + <sourceRoots> + <file path="$PROJECT_DIR$/inc" /> + <file path="$PROJECT_DIR$/src" /> + </sourceRoots> + <libraryRoots> + <file path="$PROJECT_DIR$/lib" /> + </libraryRoots> + </component> </project> \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 94a25f7f4cb416c083d265558da75d457237d671..76ade6edd581314bf32ea0f3fd0e76c366ce9100 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -2,5 +2,6 @@ <project version="4"> <component name="VcsDirectoryMappings"> <mapping directory="$PROJECT_DIR$" vcs="Git" /> + <mapping directory="$PROJECT_DIR$/lib/Catch2" vcs="Git" /> </component> </project> \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 3e3cca1af12985cbef3777b6b3c8e1ea2c50ad61..da82b96913a0d19e345db0e26e4fab77509130e3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,3 +15,9 @@ add_custom_target(check # Specify the .cpp files for the executables add_executable(ecss_services src/main.cpp src/Message.cpp src/Service.cpp src/Services/TestService.cpp src/Services/MemMangService.cpp) + +IF(EXISTS "${PROJECT_SOURCE_DIR}/lib/Catch2/CMakeLists.txt") +add_subdirectory(lib/Catch2) +add_executable(tests src/Message.cpp src/Services/TestService.cpp test/tests.cpp test/Message.cpp test/TestPlatform.cpp test/Services/TestService.cpp) +target_link_libraries(tests Catch2::Catch2) +ENDIF() diff --git a/ci/clang-tidy.sh b/ci/clang-tidy.sh index 8a0c19301fe7e7efc7ff350cd9119e5c4b9c97a5..1e676ce2d6926d700576c3a0fe69bad656c16389 100755 --- a/ci/clang-tidy.sh +++ b/ci/clang-tidy.sh @@ -11,4 +11,4 @@ echo -e "\033[0;34mRunning clang-tidy...\033[0m" cd "$(dirname "$0")" clang-tidy-4.0 `find ../src/ -type f -regextype posix-egrep -regex '.*\.(cpp|hpp|c|h)'` \ - -- -std=c++14 -I../inc + -extra-arg=-fcolor-diagnostics -- -std=c++14 -I../inc diff --git a/ci/cppcheck.sh b/ci/cppcheck.sh index a8335c61296643714ec50b402b322253be41c254..6da4d33694f44e13b0d05a789f359ce84a2e70f5 100755 --- a/ci/cppcheck.sh +++ b/ci/cppcheck.sh @@ -10,4 +10,4 @@ echo -e "\033[0;34mRunning cppcheck...\033[0m" cd "$(dirname "$0")/.." -cppcheck --enable=all --error-exitcode=1 -I inc src +cppcheck --enable=all --error-exitcode=1 -I inc src tests diff --git a/ci/vera.sh b/ci/vera.sh index 9f7d0b6a391c7554bc73fe6b1baab1278e49e868..b758842ff8b395ee548e6389a8f6b08426fbaf87 100755 --- a/ci/vera.sh +++ b/ci/vera.sh @@ -10,4 +10,4 @@ echo -e "\033[0;34mRunning vera++...\033[0m" cd "$(dirname "$0")/.." -vera++ --error --profile custom `find src inc -type f -regextype posix-egrep -regex '.*\.(cpp|hpp|c|h)'` +vera++ --error --profile custom `find src inc tests -type f -regextype posix-egrep -regex '.*\.(cpp|hpp|c|h)'` diff --git a/inc/Message.hpp b/inc/Message.hpp index 268af02f6780a11e17d3a655e3fce29c96589358..28ba738b09ab3babeb2173514da8704d892a5301 100644 --- a/inc/Message.hpp +++ b/inc/Message.hpp @@ -335,6 +335,8 @@ public: * Fetches an 4-byte single-precision floating point number from the current position in the * message * + * @todo Check if endianness matters for this + * * PTC = 5, PFC = 1 */ float readFloat() { @@ -344,6 +346,11 @@ public: uint32_t value = readWord(); return reinterpret_cast<float &>(value); } + + /** + * Reset the message reading status, and start reading data from it again + */ + void resetRead(); }; diff --git a/lib/Catch2 b/lib/Catch2 new file mode 160000 index 0000000000000000000000000000000000000000..77f29c2f1cde8bd2e17f06cc04092b990d2acc2c --- /dev/null +++ b/lib/Catch2 @@ -0,0 +1 @@ +Subproject commit 77f29c2f1cde8bd2e17f06cc04092b990d2acc2c diff --git a/src/Message.cpp b/src/Message.cpp index d17eb0f5c003792d37fe66975425ae15d4e7a92d..292ec6953655cc1bb6efb00b792eaf094fd583fc 100644 --- a/src/Message.cpp +++ b/src/Message.cpp @@ -139,3 +139,8 @@ void Message::readString(char *string, uint8_t size) { readPosition += size; } + +void Message::resetRead() { + readPosition = 0; + currentBit = 0; +} diff --git a/test/Message.cpp b/test/Message.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4a745e04e5c1bdf8cf449996f5bff135fb1312bf --- /dev/null +++ b/test/Message.cpp @@ -0,0 +1,173 @@ +#include <catch2/catch.hpp> +#include <Message.hpp> + +TEST_CASE("Message is usable", "[message]") { + Message message(5, 17, Message::TC, 3); + + REQUIRE(message.serviceType == 5); + REQUIRE(message.messageType == 17); + REQUIRE(message.packetType == Message::TC); + REQUIRE(message.applicationId == 3); + REQUIRE(message.dataSize == 0); + + message.appendByte(250); + REQUIRE(message.dataSize == 1); + REQUIRE(message.readByte() == 250); +} + +TEST_CASE("Bit manipulations", "[message]") { + Message message(0, 0, Message::TC, 0); + + message.appendBits(10, 0x357); + message.appendBits(4, 0xb); + message.appendBits(2, 0); + message.appendByte(248); + message.appendBits(7, 0x16); + message.appendBits(1, 0x1); + message.appendBits(8, 0xff); + + REQUIRE(message.dataSize == 5); + + CHECK(message.readBits(10) == 0x357); + CHECK(message.readBits(4) == 0xb); + CHECK(message.readBits(2) == 0); + CHECK(message.readBits(5) == 0x1f); + CHECK(message.readBits(3) == 0); + CHECK(message.readByte() == 0x2d); + CHECK(message.readByte() == 0xff); + + message.resetRead(); + + CHECK(message.readUint32() == 0xd5ecf82d); + CHECK(message.readBits(8) == 0xff); +} + +TEST_CASE("Requirement 5.3.1", "[message][ecss]") { + SECTION("5.3.1a") { + + } + + SECTION("5.3.1b") { + REQUIRE(sizeof(Message::serviceType) == 1); + } + + SECTION("5.3.1c") { + // TODO: Unimplemented + } + + SECTION("5.3.1d") { + // TODO: Unimplemented + } +} + +TEST_CASE("Requirement 7.3.2 (Boolean)", "[message][ecss]") { + Message message(0, 0, Message::TC, 0); + + message.appendBoolean(false); + message.appendBoolean(true); + + REQUIRE(message.dataSize == 2); + + CHECK_FALSE(message.readBoolean()); + CHECK(message.readBoolean()); +} + +TEST_CASE("Requirement 7.3.3 (Enumerated)", "[message][ecss]") { + Message message(0, 0, Message::TC, 0); + + message.appendEnum8(230); + message.appendEnum16(15933); + message.appendEnum32(2000001); + message.appendEnumerated(12, 2052); + message.appendEnumerated(4, 10); + + REQUIRE(message.dataSize == 1 + 2 + 4 + 2); + + CHECK(message.readEnum8() == 230); + CHECK(message.readEnum16() == 15933); + CHECK(message.readEnum32() == 2000001); + CHECK(message.readEnumerated(12) == 2052); + CHECK(message.readEnumerated(4) == 10); +} + +TEST_CASE("Requirement 7.3.4 (Unsigned integer)", "[message][ecss]") { + Message message(0, 0, Message::TC, 0); + + message.appendUint8(230); + message.appendUint16(15933); + message.appendUint32(2000001); + + REQUIRE(message.dataSize == 1 + 2 + 4); + + CHECK(message.readUint8() == 230); + CHECK(message.readUint16() == 15933); + CHECK(message.readUint32() == 2000001); + + SECTION("7.4.3") { + /** + * Make sure the endianness of the message data is correct. + * As per the ECSS standard, stored data should be big-endian. However, ARM and x86 + * processors store data in little endian format. As a result, special care needs to be + * taken for compliance. + */ + CHECK(message.data[1] == 0x3e); + CHECK(message.data[2] == 0x3d); + } +} + +TEST_CASE("Requirement 7.3.5 (Signed integer)", "[message][ecss]") { + Message message(0, 0, Message::TC, 0); + + message.appendSint8(-16); + message.appendSint16(-7009); + message.appendSint32(-2000001); + message.appendSint32(15839011); + + REQUIRE(message.dataSize == 1 + 2 + 4 + 4); + + CHECK(message.readSint8() == -16); + CHECK(message.readSint16() == -7009); + CHECK(message.readSint32() == -2000001); + CHECK(message.readSint32() == 15839011); + + SECTION("7.4.3") { + // Make sure the endianness of the message data is correct + // As per the ECSS standard, stored data should be big-endian. However, ARM and x86 + // processors store data in little endian format. As a result, special care needs to be + // taken for compliance. + CHECK(message.data[1] == 0xe4); + CHECK(message.data[2] == 0x9f); + } +} + +TEST_CASE("Requirement 7.3.6 (Real)", "[message][ecss]") { + Message message(0, 0, Message::TC, 0); + + message.appendFloat(7.209f); + message.appendFloat(-9003.53135f); + + REQUIRE(message.dataSize == 8); + + CHECK(message.readFloat() == 7.209f); + CHECK(message.readFloat() == -9003.53135f); +} + +TEST_CASE("Requirement 7.3.8 (Octet-string)", "[message][ecss]") { + Message message(0, 0, Message::TC, 0); + + message.appendString(4, "test"); + + REQUIRE(message.dataSize == 4); + + char string[5]; + message.readString(string, 4); + CHECK_THAT(string, Catch::Matchers::Equals("test")); +} + +TEST_CASE("Requirement 7.3.13 (Packet)", "[message][ecss]") { + Message telemetry(0, 0, Message::TM, 0); + Message telecommand(0, 0, Message::TC, 0); + + CHECK(static_cast<int>(telemetry.packetType) == 0); + CHECK(static_cast<int>(telecommand.packetType) == 1); +} diff --git a/test/Services/ServiceTests.hpp b/test/Services/ServiceTests.hpp new file mode 100644 index 0000000000000000000000000000000000000000..a160558a0aac56d215ae6af329ce7b690c0b2f4d --- /dev/null +++ b/test/Services/ServiceTests.hpp @@ -0,0 +1,53 @@ +#ifndef ECSS_SERVICES_TESTS_SERVICES_SERVICETESTS_HPP +#define ECSS_SERVICES_TESTS_SERVICES_SERVICETESTS_HPP + +#include <vector> +#include <Message.hpp> + +/** + * Supporting class for tests against ECSS services + * + * @todo See if members of this class can be made non-static + */ +class ServiceTests { + static std::vector<Message> queuedMessages; + +public: + /** + * Get a message from the list of queued messages to send + * @param number The number of the message, starting from 0 in chronological order + */ + static Message &get(uint64_t number) { + return queuedMessages.at(number); + } + + /** + * Add a message to the queue of messages to be sent + */ + static void queue(const Message &message) { + queuedMessages.push_back(message); + } + + /** + * Counts the number of messages in the queue + */ + static uint64_t count() { + return queuedMessages.size(); + } + + /** + * Checks that there is *exactly* one message in the list of queued messages + */ + static bool hasOneMessage() { + return count() == 1; + } + + /** + * Remove all the queued messages from the list, starting over from 0 items again + */ + static void reset() { + queuedMessages.clear(); + } +}; + +#endif //ECSS_SERVICES_TESTS_SERVICES_SERVICETESTS_HPP diff --git a/test/Services/TestService.cpp b/test/Services/TestService.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a94934ad20836620ec2ab1779d4ce0107798d08c --- /dev/null +++ b/test/Services/TestService.cpp @@ -0,0 +1,32 @@ +#include <catch2/catch.hpp> +#include <Services/TestService.hpp> +#include <Message.hpp> +#include "ServiceTests.hpp" + +TEST_CASE("TM[17,1]", "[service][st17]") { + TestService testService; + + Message receivedPacket = Message(17, 1, Message::TC, 1); + testService.areYouAlive(receivedPacket); + REQUIRE(ServiceTests::hasOneMessage()); + + Message response = ServiceTests::get(0); + CHECK(response.serviceType == 17); + CHECK(response.messageType == 2); + REQUIRE(response.dataSize == 0); +} + +TEST_CASE("TM[17,3]", "[service][st17]") { + TestService testService; + + Message receivedPacket = Message(17, 3, Message::TC, 1); + receivedPacket.appendEnum16(40); + testService.onBoardConnection(receivedPacket); + REQUIRE(ServiceTests::hasOneMessage()); + + Message response = ServiceTests::get(0); + CHECK(response.serviceType == 17); + CHECK(response.messageType == 4); + REQUIRE(response.dataSize == 2); + CHECK(response.readEnum16() == 40); +} diff --git a/test/TestPlatform.cpp b/test/TestPlatform.cpp new file mode 100644 index 0000000000000000000000000000000000000000..10f9cdf366fd4950138248a6564b33c7d8dd0a10 --- /dev/null +++ b/test/TestPlatform.cpp @@ -0,0 +1,22 @@ +#define CATCH_CONFIG_EXTERNAL_INTERFACES + +#include <catch2/catch.hpp> +#include <Message.hpp> +#include <Service.hpp> +#include "Services/ServiceTests.hpp" + +std::vector<Message> ServiceTests::queuedMessages = std::vector<Message>(); + +void Service::storeMessage(const Message &message) { + ServiceTests::queue(message); +} + +struct ServiceTestsListener : Catch::TestEventListenerBase { + using TestEventListenerBase::TestEventListenerBase; // inherit constructor + + void testCaseEnded(Catch::TestCaseStats const &testCaseStats) override { + // Tear-down after a test case is run + ServiceTests::reset(); + } +}; +CATCH_REGISTER_LISTENER(ServiceTestsListener) diff --git a/test/tests.cpp b/test/tests.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f481b16fe25e9b620f1dcf078a53f4f357d466c5 --- /dev/null +++ b/test/tests.cpp @@ -0,0 +1,3 @@ +#define CATCH_CONFIG_MAIN + +#include <catch2/catch.hpp>