Skip to content

Commit 40cfdc9

Browse files
committed
[ntuple] Add schema version check when opening attribute sets
1 parent 7a12b90 commit 40cfdc9

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

tree/ntuple/src/RNTupleAttrReading.cxx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <ROOT/RNTupleAttrUtils.hxx>
1010
#include <ROOT/RNTupleReader.hxx>
1111
#include <ROOT/RNTupleModel.hxx>
12+
#include <ROOT/RLogger.hxx>
1213

1314
using namespace ROOT::Experimental::Internal::RNTupleAttributes;
1415

tree/ntuple/test/ntuple_attributes.cxx

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "ntuple_test.hxx"
22
#include <ROOT/RNTupleAttrWriting.hxx>
33
#include <ROOT/RNTupleAttrReading.hxx>
4+
#include <TKey.h>
45

56
static std::size_t Count(ROOT::Experimental::RNTupleAttrEntryIterable iterable)
67
{
@@ -910,3 +911,99 @@ TEST(RNTupleAttributes, AccessAttrSetWriterAfterClosingMainWriter)
910911
EXPECT_THROW(attrSet->CommitRange(std::move(attrEntry)), ROOT::RException);
911912
}
912913
}
914+
915+
TEST(RNTupleAttributes, ReadAttributesUnknownMajor)
916+
{
917+
// Try reading an attribute set with an unknown Major schema version and verify it fails.
918+
FileRaii fileGuard("test_ntuple_attrs_futuremajorschema.root");
919+
920+
ROOT::TestSupport::CheckDiagsRAII diagsRaii;
921+
diagsRaii.requiredDiag(kWarning, "ROOT.NTuple", "RNTuple Attributes are experimental", false);
922+
923+
// Write a regular RNTuple with attributes
924+
{
925+
auto file = std::unique_ptr<TFile>(TFile::Open(fileGuard.GetPath().c_str(), "RECREATE"));
926+
auto opts = RNTupleWriteOptions();
927+
opts.SetCompression(0);
928+
auto writer = RNTupleWriter::Append(RNTupleModel::Create(), "ntpl", *file, opts);
929+
auto attrSet = writer->CreateAttributeSet(RNTupleModel::Create(), "MyAttrSet");
930+
931+
auto attrRange = attrSet->BeginRange();
932+
for (int i = 0; i < 10; ++i) {
933+
writer->Fill();
934+
}
935+
attrSet->CommitRange(std::move(attrRange));
936+
}
937+
938+
// Get metadata about the main footer that we're gonna patch.
939+
std::uint64_t footerSeek = 0, footerNBytes = 0;
940+
{
941+
auto file = std::unique_ptr<TFile>(TFile::Open(fileGuard.GetPath().c_str()));
942+
const ROOT::RNTuple *mainAnchor = file->Get<ROOT::RNTuple>("ntpl");
943+
ASSERT_NE(mainAnchor, nullptr);
944+
945+
footerSeek = mainAnchor->GetSeekFooter();
946+
footerNBytes = mainAnchor->GetNBytesFooter() - 8;
947+
}
948+
949+
// Patch the attribute schema version (and update the footer checksum)
950+
const std::uint64_t majorOff = 0xA0;
951+
const std::byte newMajorVersion{0x99};
952+
PatchRNTupleSection(fileGuard.GetPath(), footerSeek, footerNBytes, majorOff, &newMajorVersion,
953+
sizeof(newMajorVersion), EEndianness::LE);
954+
955+
auto reader = RNTupleReader::Open("ntpl", fileGuard.GetPath());
956+
957+
// Fetch an attribute set
958+
try {
959+
auto attrSet = reader->OpenAttributeSet("MyAttrSet");
960+
FAIL() << "opening an attribute set with an unknown major version should fail.";
961+
} catch (const ROOT::RException &ex) {
962+
EXPECT_THAT(ex.what(), testing::HasSubstr("unsupported attribute schema version"));
963+
}
964+
}
965+
966+
TEST(RNTupleAttributes, ReadAttributesUnknownMinor)
967+
{
968+
// Try reading an attribute set with an unknown Minor schema version and verify it works.
969+
FileRaii fileGuard("test_ntuple_attrs_futureminorschema.root");
970+
971+
ROOT::TestSupport::CheckDiagsRAII diagsRaii;
972+
diagsRaii.requiredDiag(kWarning, "ROOT.NTuple", "RNTuple Attributes are experimental", false);
973+
diagsRaii.requiredDiag(kWarning, "ROOT.NTuple", "schema minor version is greater", false);
974+
975+
// Write a regular RNTuple with attributes
976+
{
977+
auto file = std::unique_ptr<TFile>(TFile::Open(fileGuard.GetPath().c_str(), "RECREATE"));
978+
auto opts = RNTupleWriteOptions();
979+
opts.SetCompression(0);
980+
auto writer = RNTupleWriter::Append(RNTupleModel::Create(), "ntpl", *file, opts);
981+
auto attrSet = writer->CreateAttributeSet(RNTupleModel::Create(), "MyAttrSet");
982+
983+
auto attrRange = attrSet->BeginRange();
984+
for (int i = 0; i < 10; ++i) {
985+
writer->Fill();
986+
}
987+
attrSet->CommitRange(std::move(attrRange));
988+
}
989+
990+
// Get metadata about the main footer that we're gonna patch.
991+
std::uint64_t footerSeek = 0, footerNBytes = 0;
992+
{
993+
auto file = std::unique_ptr<TFile>(TFile::Open(fileGuard.GetPath().c_str()));
994+
const ROOT::RNTuple *mainAnchor = file->Get<ROOT::RNTuple>("ntpl");
995+
ASSERT_NE(mainAnchor, nullptr);
996+
997+
footerSeek = mainAnchor->GetSeekFooter();
998+
footerNBytes = mainAnchor->GetNBytesFooter() - 8;
999+
}
1000+
1001+
// Patch the attribute schema version (and update the footer checksum)
1002+
const std::uint64_t majorOff = 0xA2;
1003+
const std::byte newMinorVersion{0x99};
1004+
PatchRNTupleSection(fileGuard.GetPath(), footerSeek, footerNBytes, majorOff, &newMinorVersion,
1005+
sizeof(newMinorVersion), EEndianness::LE);
1006+
1007+
auto reader = RNTupleReader::Open("ntpl", fileGuard.GetPath());
1008+
EXPECT_NO_THROW(reader->OpenAttributeSet("MyAttrSet"));
1009+
}

0 commit comments

Comments
 (0)