Skip to content
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

Test checksum writing/reading #216

Merged
merged 3 commits into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ if (BUILD_TESTING)
target_link_libraries(test_macros PUBLIC ebml)
add_test(test_macros test_macros)

add_executable(test_crc test/test_crc.cxx)
target_link_libraries(test_crc PUBLIC ebml)
add_test(test_crc test_crc)

endif(BUILD_TESTING)


Expand Down
14 changes: 9 additions & 5 deletions src/EbmlMaster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,16 @@ filepos_t EbmlMaster::WriteHead(IOCallback & output, int nSizeLength, ShouldWrit
return RenderHead(output, false, writeFilter);
}

/*!
\todo this code is very suspicious !
*/
filepos_t EbmlMaster::ReadData(IOCallback & input, ScopeMode /* ReadFully */)
filepos_t EbmlMaster::ReadData(IOCallback & input, ScopeMode scope)
{
input.setFilePointer(GetSize(), seek_current);
int upper = 0;
EbmlStream aStream(input);
EbmlElement *Parent = nullptr;
Read(aStream, Context(), upper, Parent, false, scope);
assert(Parent == nullptr);
assert(upper == 0);

input.setFilePointer(GetEndPosition(), seek_beginning);
return GetSize();
}

Expand Down
61 changes: 61 additions & 0 deletions test/test_crc.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright © 2024 Steve Lhomme.
// SPDX-License-Identifier: ISC

#include <ebml/EbmlHead.h>
#include <ebml/EbmlSubHead.h>
#include <ebml/EbmlMaster.h>
#include <ebml/EbmlStream.h>
#include <ebml/MemIOCallback.h>

int main(int /*argc*/, char** /*argv*/)
{
///// Writing test
libebml::MemIOCallback Ebml_file;
libebml::EbmlHead TestHead;

TestHead.EnableChecksum();

libebml::EDocTypeVersion & MyDocTypeVer = libebml::GetChild<libebml::EDocTypeVersion>(TestHead);
MyDocTypeVer.SetValue(1);

libebml::GetChild<libebml::EMaxSizeLength>(TestHead).SetValue(7);

auto length = TestHead.Render(Ebml_file, libebml::EbmlElement::WriteAll);
if (length != 46)
return 1;

///// Reading test
Ebml_file.setFilePointer(0);
libebml::EbmlStream aStream(Ebml_file);
libebml::EbmlElement * ElementLevel0;

// find the EBML head in the file
ElementLevel0 = aStream.FindNextID(EBML_INFO(libebml::EbmlHead), 0xFFFFFFFFL);
if (ElementLevel0 == nullptr)
return 1;

if (ElementLevel0->GetClassId() != libebml::EbmlHead::ClassId())
return 1;

libebml::EbmlHead &ReadHead = static_cast<libebml::EbmlHead &>(*ElementLevel0);

ReadHead.ReadData(aStream, libebml::SCOPE_ALL_DATA);

if (!ReadHead.VerifyChecksum())
return 1;

libebml::EDocType & ReadDocType = libebml::GetChild<libebml::EDocType>(ReadHead);
const std::string & DocTypeStr = static_cast<const std::string &>(ReadDocType);
if (DocTypeStr != "matroska")
return 1;

auto & ReadReadVersion = libebml::GetChild<const libebml::EDocTypeReadVersion>(ReadHead);
if (static_cast<std::uint64_t>(ReadReadVersion) != 1)
return 1;

auto & ReadMaxLength = libebml::GetChild<const libebml::EMaxSizeLength>(ReadHead);
if (static_cast<std::uint64_t>(ReadMaxLength) != 7)
return 1;

return 0;
}
3 changes: 3 additions & 0 deletions test/test_header.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ int main(int /*argc*/, char** /*argv*/)
if (upper != 0)
return 1;

if (!ReadHead.VerifyChecksum())
return 1;

libebml::EDocType & ReadDocType = libebml::GetChild<libebml::EDocType>(ReadHead);
const std::string & DocTypeStr = static_cast<const std::string &>(ReadDocType);
if (DocTypeStr != "webm")
Expand Down