Skip to content

Commit be09b77

Browse files
bitWarriorbitWarrior
authored andcommitted
Added FileUplinkOverwriteTests unit test.
1 parent 9aab0e7 commit be09b77

4 files changed

Lines changed: 188 additions & 0 deletions

File tree

Svc/FileUplink/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ set(UT_SOURCE_FILES
2727
"${CMAKE_CURRENT_LIST_DIR}/FileUplink.fpp"
2828
${CMAKE_CURRENT_LIST_DIR}/test/ut/FileUplinkTester.cpp
2929
${CMAKE_CURRENT_LIST_DIR}/test/ut/FileUplinkMain.cpp
30+
${CMAKE_CURRENT_LIST_DIR}/test/ut/FileUplinkOverwriteTests.cpp
3031
)
3132
register_fprime_ut()
3233
set (UT_TARGET_NAME "${FPRIME_CURRENT_MODULE}_ut_exe")
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// ======================================================================
2+
// \title FileUplinkOverwriteTests.cpp
3+
// \brief TEST_F entries for overwrite truncation regression tests.
4+
//
5+
// Placement: Svc/FileUplink/test/ut/FileUplinkOverwriteTests.cpp
6+
// Add to CMakeLists.txt sources alongside the other test .cpp files.
7+
// ======================================================================
8+
9+
#include "FileUplinkTester.hpp"
10+
#include <gtest/gtest.h>
11+
12+
namespace Svc {
13+
14+
// Fixture — identical pattern to the existing FileUplink fixture.
15+
class FileUplinkOverwrite : public ::testing::Test {
16+
protected:
17+
FileUplinkTester tester;
18+
};
19+
20+
TEST_F(FileUplinkOverwrite, OverwriteWithSmallerFile) {
21+
tester.overwriteWithSmallerFile();
22+
}
23+
24+
TEST_F(FileUplinkOverwrite, OverwriteSameSizeFile) {
25+
tester.overwriteSameSizeFile();
26+
}
27+
28+
TEST_F(FileUplinkOverwrite, OverwriteWithLargerFile) {
29+
tester.overwriteWithLargerFile();
30+
}
31+
32+
} // namespace Svc

Svc/FileUplink/test/ut/FileUplinkTester.cpp

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <cerrno>
1414
#include <cstring>
15+
#include <sys/stat.h>
1516

1617
#include "FileUplinkTester.hpp"
1718
#include "Fw/Com/ComPacket.hpp"
@@ -529,4 +530,142 @@ void FileUplinkTester ::removeFile(const char* const path) {
529530
}
530531
}
531532

533+
void FileUplinkTester ::verifyFileSize(const char* const path, const size_t expectedSize) {
534+
struct stat st;
535+
ASSERT_EQ(0, ::stat(path, &st)) << "Could not stat " << path;
536+
ASSERT_EQ(static_cast<size_t>(st.st_size), expectedSize)
537+
<< "File has " << st.st_size << " bytes on disk; expected " << expectedSize
538+
<< ". Os::File::open is likely not truncating stale content.";
539+
}
540+
541+
// ----------------------------------------------------------------------
542+
// Overwrite truncation tests
543+
// Regression coverage for the change in File.cpp:
544+
// Os::File::open(path, Os::File::OPEN_CREATE, Os::File::OverwriteType::OVERWRITE)
545+
// Without truncation, stale bytes from a previously-larger file survive
546+
// past the new logical EOF and corrupt the uplinked content.
547+
// ----------------------------------------------------------------------
548+
549+
void FileUplinkTester ::overwriteWithSmallerFile() {
550+
const char* const sourcePath = "source.bin";
551+
const char* const destPath = "overwrite_test.bin";
552+
553+
// --- First upload: 4 packets * PACKET_SIZE = 20 bytes of 0xAA ---
554+
const U32 numLargePackets = 4;
555+
const size_t largeFileSize = numLargePackets * PACKET_SIZE;
556+
U8 largeData[largeFileSize];
557+
memset(largeData, 0xAA, largeFileSize);
558+
559+
this->sendStartPacket(sourcePath, destPath, largeFileSize);
560+
for (U32 i = 0; i < numLargePackets; ++i) {
561+
this->sendDataPacket(i * PACKET_SIZE, &largeData[i * PACKET_SIZE]);
562+
}
563+
CFDP::Checksum largeChecksum;
564+
largeChecksum.update(largeData, 0, largeFileSize);
565+
this->sendEndPacket(largeChecksum);
566+
567+
this->verifyFileData(destPath, largeData, largeFileSize);
568+
this->verifyFileSize(destPath, largeFileSize);
569+
570+
// --- Second upload: 1 packet * PACKET_SIZE = 5 bytes of 0xBB ---
571+
const U32 numSmallPackets = 1;
572+
const size_t smallFileSize = numSmallPackets * PACKET_SIZE;
573+
U8 smallData[smallFileSize];
574+
memset(smallData, 0xBB, smallFileSize);
575+
576+
this->sendStartPacket(sourcePath, destPath, smallFileSize);
577+
for (U32 i = 0; i < numSmallPackets; ++i) {
578+
this->sendDataPacket(i * PACKET_SIZE, &smallData[i * PACKET_SIZE]);
579+
}
580+
CFDP::Checksum smallChecksum;
581+
smallChecksum.update(smallData, 0, smallFileSize);
582+
this->sendEndPacket(smallChecksum);
583+
584+
// Content must be the new data only — no 0xAA stale tail
585+
this->verifyFileData(destPath, smallData, smallFileSize);
586+
this->verifyFileSize(destPath, smallFileSize);
587+
588+
this->removeFile(destPath);
589+
}
590+
591+
void FileUplinkTester ::overwriteSameSizeFile() {
592+
const char* const sourcePath = "source.bin";
593+
const char* const destPath = "overwrite_test.bin";
594+
595+
const U32 numPackets = 2;
596+
const size_t fileSize = numPackets * PACKET_SIZE;
597+
598+
// --- First upload: 0x11 fill ---
599+
U8 firstData[fileSize];
600+
memset(firstData, 0x11, fileSize);
601+
602+
this->sendStartPacket(sourcePath, destPath, fileSize);
603+
for (U32 i = 0; i < numPackets; ++i) {
604+
this->sendDataPacket(i * PACKET_SIZE, &firstData[i * PACKET_SIZE]);
605+
}
606+
CFDP::Checksum firstChecksum;
607+
firstChecksum.update(firstData, 0, fileSize);
608+
this->sendEndPacket(firstChecksum);
609+
610+
this->verifyFileData(destPath, firstData, fileSize);
611+
612+
// --- Second upload: 0x22 fill, same path and size ---
613+
U8 secondData[fileSize];
614+
memset(secondData, 0x22, fileSize);
615+
616+
this->sendStartPacket(sourcePath, destPath, fileSize);
617+
for (U32 i = 0; i < numPackets; ++i) {
618+
this->sendDataPacket(i * PACKET_SIZE, &secondData[i * PACKET_SIZE]);
619+
}
620+
CFDP::Checksum secondChecksum;
621+
secondChecksum.update(secondData, 0, fileSize);
622+
this->sendEndPacket(secondChecksum);
623+
624+
// All bytes must reflect the new upload
625+
this->verifyFileData(destPath, secondData, fileSize);
626+
this->verifyFileSize(destPath, fileSize);
627+
628+
this->removeFile(destPath);
629+
}
630+
631+
void FileUplinkTester ::overwriteWithLargerFile() {
632+
const char* const sourcePath = "source.bin";
633+
const char* const destPath = "overwrite_test.bin";
634+
635+
// --- First upload: 1 packet * PACKET_SIZE = 5 bytes of 0x33 ---
636+
const U32 numSmallPackets = 1;
637+
const size_t smallFileSize = numSmallPackets * PACKET_SIZE;
638+
U8 smallData[smallFileSize];
639+
memset(smallData, 0x33, smallFileSize);
640+
641+
this->sendStartPacket(sourcePath, destPath, smallFileSize);
642+
for (U32 i = 0; i < numSmallPackets; ++i) {
643+
this->sendDataPacket(i * PACKET_SIZE, &smallData[i * PACKET_SIZE]);
644+
}
645+
CFDP::Checksum smallChecksum;
646+
smallChecksum.update(smallData, 0, smallFileSize);
647+
this->sendEndPacket(smallChecksum);
648+
649+
this->verifyFileData(destPath, smallData, smallFileSize);
650+
651+
// --- Second upload: 4 packets * PACKET_SIZE = 20 bytes of 0x44 ---
652+
const U32 numLargePackets = 4;
653+
const size_t largeFileSize = numLargePackets * PACKET_SIZE;
654+
U8 largeData[largeFileSize];
655+
memset(largeData, 0x44, largeFileSize);
656+
657+
this->sendStartPacket(sourcePath, destPath, largeFileSize);
658+
for (U32 i = 0; i < numLargePackets; ++i) {
659+
this->sendDataPacket(i * PACKET_SIZE, &largeData[i * PACKET_SIZE]);
660+
}
661+
CFDP::Checksum largeChecksum;
662+
largeChecksum.update(largeData, 0, largeFileSize);
663+
this->sendEndPacket(largeChecksum);
664+
665+
this->verifyFileData(destPath, largeData, largeFileSize);
666+
this->verifyFileSize(destPath, largeFileSize);
667+
668+
this->removeFile(destPath);
669+
}
670+
532671
} // namespace Svc

Svc/FileUplink/test/ut/FileUplinkTester.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ class FileUplinkTester : public FileUplinkGTestBase {
8787
//!
8888
void cancelPacketInDataMode();
8989

90+
//! Overwrite a larger file with a smaller one; verify no stale bytes remain
91+
//!
92+
void overwriteWithSmallerFile();
93+
94+
//! Overwrite a file with one of the same size; verify content changes
95+
//!
96+
void overwriteSameSizeFile();
97+
98+
//! Overwrite a smaller file with a larger one; verify full content written
99+
//!
100+
void overwriteWithLargerFile();
101+
90102
private:
91103
// ----------------------------------------------------------------------
92104
// Handlers for from ports
@@ -143,6 +155,10 @@ class FileUplinkTester : public FileUplinkGTestBase {
143155
//!
144156
void verifyFileData(const char* const path, const U8* const sentData, const size_t sentDataSize);
145157

158+
//! Verify on-disk file size equals expectedSize (catches stale tail bytes)
159+
//!
160+
void verifyFileSize(const char* const path, size_t expectedSize);
161+
146162
//! Remove a file
147163
//!
148164
void removeFile(const char* const path);

0 commit comments

Comments
 (0)