From f11eb5b421ed1025ea1afafe3bc361798abfc77a Mon Sep 17 00:00:00 2001 From: cesaaargm Date: Tue, 14 May 2024 16:15:17 +0200 Subject: [PATCH] [TAS-199] Add TagVerification test. Bugfix for verify_tag and osnma_helper. --- src/core/libs/osnma_msg_receiver.cc | 12 ++-- src/core/libs/osnma_msg_receiver.h | 1 + src/core/system_parameters/osnma_helper.cc | 32 ++++++++-- src/core/system_parameters/osnma_helper.h | 2 + src/tests/single_test_main.cc | 1 - .../osnma/osnma_msg_receiver_test.cc | 58 +++++++++++++++++-- 6 files changed, 91 insertions(+), 15 deletions(-) diff --git a/src/core/libs/osnma_msg_receiver.cc b/src/core/libs/osnma_msg_receiver.cc index 29f896f8b..024455597 100644 --- a/src/core/libs/osnma_msg_receiver.cc +++ b/src/core/libs/osnma_msg_receiver.cc @@ -873,6 +873,7 @@ void osnma_msg_receiver::process_mack_message() if (ret || d_flag_debug){ for(std::size_t i = 0; i < mack->tag_and_info.size(); ++i) { + // add tags of current mack to the verification queue auto& tag = mack->tag_and_info[i]; Tag t(tag, mack->TOW, mack->WN, mack->PRNa, i + 2); // tag0 (mack header) has CTR1, so first tag of MTI has CTR = 2. d_tags_awaiting_verify.insert(std::pair(mack->TOW, t)); @@ -1002,7 +1003,7 @@ bool osnma_msg_receiver::verify_tag(Tag& tag) std::vector m; m.push_back(static_cast(tag.PRN_d)); m.push_back(static_cast(tag.PRNa)); - uint32_t GST = d_helper->compute_gst(tag.TOW, tag.WN); + uint32_t GST = d_helper->compute_gst( tag.WN,tag.TOW); std::vector GST_uint8 = d_helper->gst_to_uint8(GST); m.insert(m.end(),GST_uint8.begin(),GST_uint8.end()); m.push_back(tag.CTR); @@ -1012,8 +1013,8 @@ bool osnma_msg_receiver::verify_tag(Tag& tag) m.push_back(two_bits_nmas); // convert std::string to vector - std::string ephemeris_iono_vector_2 = d_satellite_nav_data[tag.TOW][tag.PRNa].ephemeris_iono_vector_2; - std::vector ephemeris_iono_vector_2_bytes(ephemeris_iono_vector_2.begin(), ephemeris_iono_vector_2.end()); + std::string ephemeris_iono_vector_2 = d_satellite_nav_data[tag.PRNa][tag.TOW].ephemeris_iono_vector_2; + std::vector ephemeris_iono_vector_2_bytes = d_helper->bytes(ephemeris_iono_vector_2); // Convert and add ephemeris_iono_vector_2 into the vector for (uint8_t byte : ephemeris_iono_vector_2_bytes) { @@ -1205,7 +1206,8 @@ void osnma_msg_receiver::control_tags_awaiting_verify_size() /** * @brief Verifies the MACSEQ of a received MACK_message. * - * \details checks for each tag in the retrieved mack message if its flexible (MACSEQ) or not (MACSEQ/MACLT depending on configuration param. (TODO) + * \details checks for each tag in the retrieved mack message if its flexible (MACSEQ) or not (MACSEQ/MACLT depending on configuration param, and + * verifies according to Eqs. 20, 21 SIS ICD. * @param message The MACK_message to verify. * @return True if the MACSEQ is valid, false otherwise. */ @@ -1218,7 +1220,7 @@ bool osnma_msg_receiver::verify_macseq(const MACK_message& mack) std::vector sq2{}; std::vector applicable_sequence; const auto it = OSNMA_TABLE_16.find(d_osnma_data.d_dsm_kroot_message.maclt); - // TODO as per RG example appears that the seq. q shall also be validated ageints either next or former Sf (depending on GST) + // TODO as per RG example appears that the seq. q shall also be validated against either next or former Sf (depending on GST) if (it != OSNMA_TABLE_16.cend()) { sq1 = it->second.sequence1; diff --git a/src/core/libs/osnma_msg_receiver.h b/src/core/libs/osnma_msg_receiver.h index 8137c9824..b655094df 100644 --- a/src/core/libs/osnma_msg_receiver.h +++ b/src/core/libs/osnma_msg_receiver.h @@ -122,6 +122,7 @@ private: FRIEND_TEST(OsnmaMsgReceiverTest, TeslaKeyVerification); FRIEND_TEST(OsnmaMsgReceiverTest, OsnmaTestVectorsSimulation); + FRIEND_TEST(OsnmaMsgReceiverTest, TagVerification); }; diff --git a/src/core/system_parameters/osnma_helper.cc b/src/core/system_parameters/osnma_helper.cc index 462fd5d34..b67454ddd 100644 --- a/src/core/system_parameters/osnma_helper.cc +++ b/src/core/system_parameters/osnma_helper.cc @@ -15,6 +15,7 @@ */ #include "osnma_helper.h" +#include uint32_t Osnma_Helper::compute_gst(uint32_t WN, uint32_t TOW) const { @@ -24,11 +25,32 @@ uint32_t Osnma_Helper::compute_gst(uint32_t WN, uint32_t TOW) const std::vector Osnma_Helper::gst_to_uint8(uint32_t GST) const { - std::vector res(4); + std::vector res; - res[1] = static_cast((GST & 0xFF000000) >> 24); - res[2] = static_cast((GST & 0x00FF0000) >> 16); - res[3] = static_cast((GST & 0x0000FF00) >> 8); - res[4] = static_cast(GST & 0x000000FF); + res.push_back(static_cast((GST & 0xFF000000) >> 24)); + res.push_back(static_cast((GST & 0x00FF0000) >> 16)); + res.push_back(static_cast((GST & 0x0000FF00) >> 8)); + res.push_back(static_cast(GST & 0x000000FF)); return res; } + +std::vector Osnma_Helper::bytes(const std::string& binaryString) { + std::vector bytes; + + // Determine the size of the padding needed. + size_t padding_size = binaryString.size() % 8; + + std::string padded_binary = binaryString; + + if (padding_size != 0) { + padding_size = 8 - padding_size; // Compute padding size + padded_binary.append(padding_size, '0'); // Append zeros to the binary string + } + + for (size_t i = 0; i < padded_binary.size(); i += 8) { + uint8_t byte = std::bitset<8>(padded_binary.substr(i, 8)).to_ulong(); + bytes.push_back(byte); + } + + return bytes; +} \ No newline at end of file diff --git a/src/core/system_parameters/osnma_helper.h b/src/core/system_parameters/osnma_helper.h index 0a1b72270..9bf1d860d 100644 --- a/src/core/system_parameters/osnma_helper.h +++ b/src/core/system_parameters/osnma_helper.h @@ -19,6 +19,7 @@ #include +#include #include class Osnma_Helper { @@ -27,6 +28,7 @@ public: ~Osnma_Helper() = default; uint32_t compute_gst(uint32_t WN, uint32_t TOW) const; std::vector gst_to_uint8(uint32_t GST) const; + std::vector bytes(const std::string& binaryString); }; diff --git a/src/tests/single_test_main.cc b/src/tests/single_test_main.cc index 2bea64518..de02b421c 100644 --- a/src/tests/single_test_main.cc +++ b/src/tests/single_test_main.cc @@ -78,7 +78,6 @@ Concurrent_Map global_gps_acq_assist_map; int main(int argc, char **argv) { #if USE_GLOG_AND_GFLAGS - gflags::ParseCommandLineFlags(&argc, &argv, true); try { testing::InitGoogleTest(&argc, argv); diff --git a/src/tests/unit-tests/signal-processing-blocks/osnma/osnma_msg_receiver_test.cc b/src/tests/unit-tests/signal-processing-blocks/osnma/osnma_msg_receiver_test.cc index f251756e8..2b29ee3cf 100644 --- a/src/tests/unit-tests/signal-processing-blocks/osnma/osnma_msg_receiver_test.cc +++ b/src/tests/unit-tests/signal-processing-blocks/osnma/osnma_msg_receiver_test.cc @@ -58,6 +58,7 @@ public: TEST_F(OsnmaMsgReceiverTest, TeslaKeyVerification) { // Arrange + // ---------- osnma->d_tesla_key_verified = false; osnma->d_osnma_data.d_dsm_kroot_message.kroot = {0x5B, 0xF8, 0xC9, 0xCB, 0xFC, 0xF7, 0x04, 0x22, 0x08, 0x14, 0x75, 0xFD, 0x44, 0x5D, 0xF0, 0xFF}; // Kroot, TOW 345570 GST_0 - 30 osnma->d_osnma_data.d_dsm_kroot_message.ks = 4; // TABLE 10 --> 128 bits @@ -75,6 +76,7 @@ TEST_F(OsnmaMsgReceiverTest, TeslaKeyVerification) { // Act + // ---------- bool result = osnma->verify_tesla_key(key, TOW); @@ -82,7 +84,8 @@ TEST_F(OsnmaMsgReceiverTest, TeslaKeyVerification) { // Assert - ASSERT_TRUE(result); // Adjust this according to what you expect + // ---------- + ASSERT_TRUE(result); } @@ -342,7 +345,6 @@ std::vector OsnmaMsgReceiverTest::extract_page_bytes(const TestVector& return extracted_bytes; } - /** * @brief Sets the time based on the given input. * @@ -374,7 +376,6 @@ void OsnmaMsgReceiverTest::set_time(std::tm& input) } - void OsnmaMsgReceiverTest::initializeGoogleLog() { google::InitGoogleLogging(log_name.c_str()); @@ -416,4 +417,53 @@ void OsnmaMsgReceiverTest::initializeGoogleLog() throw; } } -} \ No newline at end of file +} + + +TEST_F(OsnmaMsgReceiverTest, TagVerification) { + // Arrange + // ---------- + osnma->d_tesla_key_verified = false; + osnma->d_osnma_data.d_dsm_kroot_message.kroot = {0x5B, 0xF8, 0xC9, 0xCB, 0xFC, 0xF7, 0x04, 0x22, 0x08, 0x14, 0x75, 0xFD, 0x44, 0x5D, 0xF0, 0xFF}; // Kroot, TOW 345570 GST_0 - 30 + osnma->d_osnma_data.d_dsm_kroot_message.ks = 4; // TABLE 10 --> 128 bits + osnma->d_osnma_data.d_dsm_kroot_message.alpha = 0x610BDF26D77B; + // local_time_verification would do this operation. TODO - eliminate duplication. + osnma->d_GST_SIS = (1248 & 0x00000FFF) << 20 | (345630 & 0x000FFFFF); + osnma->d_GST_0 = ((1248 & 0x00000FFF) << 20 | (345600 & 0x000FFFFF)); // applicable time (GST_Kroot + 30) + osnma->d_receiver_time = osnma->d_GST_0 + 30 * std::floor((osnma->d_GST_SIS - osnma->d_GST_0) / 30); // Eq. 3 R.G.//345630; + + osnma->d_tesla_keys.insert((std::pair>(345600,{0xEF, 0xF9, 0x99, 0x04, 0x0E, 0x19, 0xB5, 0x70, 0x83, 0x50, 0x60, 0xBE, 0xBD, 0x23, 0xED, 0x92}))); // K1, not needed, just for reference. + std::vector key = {0x2D, 0xC3, 0xA3, 0xCD, 0xB1, 0x17, 0xFA, 0xAD, 0xB8, 0x3B, 0x5F, 0x0B, 0x6F, 0xEA, 0x88, 0xEB}; // K2 + + + uint32_t TOW = 345630; + uint32_t WN = 1248; + uint32_t PRNa = 0; + uint8_t CTR = 2; + + osnma->d_osnma_data.d_dsm_kroot_message.ts = 0x00; + osnma->d_tesla_keys[TOW] = {0xEF, 0xF9}; + osnma->d_osnma_data.d_dsm_kroot_message.mf = 0x00; + osnma->d_satellite_nav_data[PRNa][TOW].ephemeris_iono_vector_2 = ""; + osnma->d_osnma_data.d_nma_header.nmas = 0b00000010; + + MACK_tag_and_info MTI; + MTI.tag = 0x00; + MTI.tag_info.PRN_d = 0; + MTI.tag_info.ADKD = 0; + MTI.tag_info.cop = 0; + Tag t(MTI, TOW, WN, PRNa, CTR); + + // Act + // ---------- + bool result = osnma->verify_tag(t); + + + + + + // Assert + // ---------- + ASSERT_TRUE(result); + +}