-
Notifications
You must be signed in to change notification settings - Fork 0
Add StyxSerializer C++ tests #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
native/V2StyxLib/modules/l5/include/serialization/BufferReaderImpl.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 7 additions & 6 deletions
13
native/V2StyxLib/modules/l5/include/serialization/IDataSerializer.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,14 @@ | ||
| #pragma once | ||
| #include "l5/messages/StyxMessage.h" | ||
| #include "l5/structs/StyxStat.h" | ||
| #include "l5/serialization/IBufferWritter.h" | ||
| #include "messages/StyxMessage.h" | ||
| #include "structs/StyxStat.h" | ||
| #include "serialization/IBufferWriter.h" | ||
|
|
||
| class IDataSerializer { | ||
| public: | ||
| virtual void serialize(const StyxMessage &message, IBufferWriter *output) = 0; | ||
| virtual void serializeStat(const StyxStat &stat, IBufferWriter *output) = 0; | ||
| virtual void serialize(const styxlib::messages::StyxMessage &message, | ||
| IBufferWriter &output) = 0; | ||
| virtual void serializeStat(const StyxStat &stat, IBufferWriter &output) = 0; | ||
| virtual int getStatSerializedSize(const StyxStat &stat) = 0; | ||
| virtual int getQidSize() = 0; | ||
| virtual void serializeQid(const StyxQID &qid, IBufferWriter *output) = 0; | ||
| virtual void serializeQid(const StyxQID &qid, IBufferWriter &output) = 0; | ||
| }; |
15 changes: 15 additions & 0 deletions
15
native/V2StyxLib/modules/l5/include/serialization/StyxSerializerImpl.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| #pragma once | ||
|
|
||
| #include "serialization/IDataSerializer.h" | ||
|
|
||
| class StyxSerializerImpl : public IDataSerializer { | ||
| public: | ||
| StyxSerializerImpl() = default; | ||
| ~StyxSerializerImpl() = default; | ||
| void serialize(const styxlib::messages::StyxMessage &message, | ||
| IBufferWriter &output) override; | ||
| void serializeStat(const StyxStat &stat, IBufferWriter &output) override; | ||
| int getStatSerializedSize(const StyxStat &stat) override; | ||
| int getQidSize() override; | ||
| void serializeQid(const StyxQID &qid, IBufferWriter &output) override; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| #include "structs/StyxQID.h" | ||
| #include "enums/QidType.h" | ||
|
|
||
| const StyxQID StyxQID::EMPTY = {styxlib::enums::QTFILE, 0, 0}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| #include "serialization/StyxSerializerImpl.h" | ||
|
|
||
| void StyxSerializerImpl::serialize(const styxlib::messages::StyxMessage &message, | ||
| IBufferWriter &output) { | ||
|
|
||
| } | ||
|
|
||
| void StyxSerializerImpl::serializeStat(const StyxStat &stat, IBufferWriter &output) | ||
| { | ||
| size_t size = getStatSerializedSize(stat); | ||
| output.writeUInt16(size - 2); // total size except first 2 bytes with size | ||
| output.writeUInt16(stat.type); | ||
| output.writeUInt32(stat.dev); | ||
| serializeQid(stat.QID, output); | ||
| output.writeUInt32(stat.mode); | ||
| output.writeUInt32(stat.accessTime); | ||
| output.writeUInt32(stat.modificationTime); | ||
| output.writeUInt64(stat.length); | ||
| output.writeUTFString(stat.name); | ||
| output.writeUTFString(stat.userName); | ||
| output.writeUTFString(stat.groupName); | ||
| output.writeUTFString(stat.modificationUser); | ||
| } | ||
|
|
||
| int StyxSerializerImpl::getStatSerializedSize(const StyxStat &stat) | ||
| { | ||
| return 28 + getQidSize() | ||
| + 2 + stat.name.length() | ||
| + 2 + stat.userName.length() | ||
| + 2 + stat.groupName.length() | ||
| + 2 + stat.modificationUser.length(); | ||
| } | ||
|
|
||
| int StyxSerializerImpl::getQidSize() | ||
| { | ||
| return 13; // Size of StyxQID: 1 byte type, 4 bytes version, 8 bytes path | ||
| } | ||
|
|
||
| void StyxSerializerImpl::serializeQid(const StyxQID &qid, IBufferWriter &output) | ||
| { | ||
| output.writeUInt8(qid.type); | ||
| output.writeUInt32(qid.version); | ||
| output.writeUInt64(qid.path); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| #include "structs/StyxStat.h" | ||
|
|
||
| const StyxStat StyxStat::EMPTY = { | ||
| .type = 0, | ||
| .dev = 0, | ||
| .QID = StyxQID::EMPTY, | ||
| .mode = 0, | ||
| .accessTime = 0, | ||
| .modificationTime = 0, | ||
| .length = 0, | ||
| .name = "", | ||
| .userName = "", | ||
| .groupName = "", | ||
| .modificationUser = "" | ||
| }; |
82 changes: 82 additions & 0 deletions
82
native/V2StyxLib/modules/l5/test/test_StyxSerializerImpl.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| #include <catch2/catch_test_macros.hpp> | ||
| #include "serialization/StyxSerializerImpl.h" | ||
| #include "structs/StyxStat.h" | ||
| #include "enums/QidType.h" | ||
| #include "serialization/BufferWriterImpl.h" | ||
|
|
||
| TEST_CASE("testGetStyxStatSize", "[StyxSerializationImpl]") { | ||
| StyxSerializerImpl impl; | ||
| REQUIRE(28 + 13 + 2 + 2 + 2 + 2 == impl.getStatSerializedSize(StyxStat::EMPTY)); | ||
| } | ||
|
|
||
| TEST_CASE("testGetQidSize", "[StyxSerializationImpl]") { | ||
| StyxSerializerImpl impl; | ||
| REQUIRE(13 == impl.getQidSize()); | ||
| } | ||
|
|
||
| TEST_CASE("testQidSerialization", "[StyxSerializationImpl]") { | ||
| StyxSerializerImpl serializer; | ||
| StyxQID qid( | ||
| styxlib::enums::QTDIR, | ||
| 0x6A7470F1, | ||
| 0x12309E51049E5104L | ||
| ); | ||
| uint8_t expected[] = { | ||
| styxlib::enums::QTDIR, | ||
| 0xF1, 0x70, 0x74, 0x6A, //9: qid.version[4] 0x6A7470F1 | ||
| 0x04, 0x51, 0x9E, 0x04, 0x51, 0x9E, 0x30, 0x12 //13: qid.path[8] 0x12309E51049E5104L | ||
| }; | ||
| BufferWriterImpl outputBuffer(8192); | ||
| serializer.serializeQid(qid, outputBuffer); | ||
|
|
||
| REQUIRE(outputBuffer.getPosition() == serializer.getQidSize()); | ||
| StyxBuffer buffer = outputBuffer.getBuffer(); | ||
| REQUIRE(std::equal(std::begin(expected), std::end(expected), buffer)); | ||
| } | ||
|
|
||
| TEST_CASE("testSerializeStat", "[StyxSerializationImpl]") { | ||
| StyxSerializerImpl serializer; | ||
|
|
||
| StyxStat stat { | ||
| .type = 1, | ||
| .dev = 2, | ||
| .QID = StyxQID(styxlib::enums::QTFILE, 0x80, 0x90), | ||
| .mode = 0x01, | ||
| .accessTime = 1717171717, // fixed date for reproducibility | ||
| .modificationTime = 1717171717, | ||
| .length = 0x123, | ||
| .name = "file", | ||
| .userName = "user", | ||
| .groupName = "group", | ||
| .modificationUser = "editor" | ||
| }; | ||
| BufferWriterImpl output(8192); | ||
| serializer.serializeStat(stat, output); | ||
|
|
||
| // Validate buffer size and some expected values | ||
| StyxBuffer buffer = output.getBuffer(); | ||
|
|
||
| REQUIRE(serializer.getStatSerializedSize(stat) == output.getPosition()); | ||
|
|
||
| uint8_t expected[] = { | ||
| 66, 0x00, // size - 2 | ||
| 1, 0x00, // type | ||
| 0x02, 0x00, 0x00, 0x00, // dev | ||
| styxlib::enums::QTFILE, | ||
| 0x80, 0x00, 0x00, 0x00, //9: qid.version[4] | ||
| 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //13: qid.path[8] 0x12309E51049E5104L | ||
| 0x01, 0x00, 0x00, 0x00, // mode | ||
| 0x05, 0xF6, 89, 102, // atime | ||
| 0x05, 0xF6, 89, 102, // mtime | ||
| 0x23, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // length | ||
| 0x04, 0x00, // name length | ||
| 'f', 'i', 'l', 'e', // name | ||
| 0x04, 0x00, // uid length | ||
| 'u', 's', 'e', 'r', // uid | ||
| 0x05, 0x00, // gid length | ||
| 'g', 'r', 'o', 'u', 'p', // gid | ||
| 0x06, 0x00, // muid length | ||
| 'e', 'd', 'i', 't', 'o', 'r' // muid | ||
| }; | ||
| REQUIRE(std::equal(std::begin(expected), std::end(expected), buffer)); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The serialize function is currently an empty stub. Consider adding a TODO comment or implementation details to clarify if future work is expected.