Skip to content

Commit a9c706e

Browse files
committed
[ntuple] Add schema version check when opening attribute sets
1 parent c35e196 commit a9c706e

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-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
#include <algorithm>
1415
#include <cstddef>

tree/ntuple/test/ntuple_attributes.cxx

Lines changed: 96 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
{
@@ -946,3 +947,98 @@ TEST(RNTupleAttributes, AccessAttrSetWriterAfterClosingMainWriter)
946947
EXPECT_THROW(attrSet->CommitRange(std::move(attrEntry)), ROOT::RException);
947948
}
948949
}
950+
951+
TEST(RNTupleAttributes, ReadAttributesUnknownMajor)
952+
{
953+
// Try reading an attribute set with an unknown Major schema version and verify it fails.
954+
FileRaii fileGuard("test_ntuple_attrs_futuremajorschema.root");
955+
956+
ROOT::TestSupport::CheckDiagsRAII diagsRaii;
957+
diagsRaii.requiredDiag(kWarning, "ROOT.NTuple", "RNTuple Attributes are experimental", false);
958+
959+
// Write a regular RNTuple with attributes
960+
{
961+
auto file = std::unique_ptr<TFile>(TFile::Open(fileGuard.GetPath().c_str(), "RECREATE"));
962+
auto opts = RNTupleWriteOptions();
963+
opts.SetCompression(0);
964+
auto writer = RNTupleWriter::Append(RNTupleModel::Create(), "ntpl", *file, opts);
965+
auto attrSet = writer->CreateAttributeSet(RNTupleModel::Create(), "MyAttrSet");
966+
967+
auto attrRange = attrSet->BeginRange();
968+
for (int i = 0; i < 10; ++i) {
969+
writer->Fill();
970+
}
971+
attrSet->CommitRange(std::move(attrRange));
972+
}
973+
974+
// Get metadata about the main footer that we're gonna patch.
975+
std::uint64_t footerSeek = 0, footerNBytes = 0;
976+
{
977+
auto file = std::unique_ptr<TFile>(TFile::Open(fileGuard.GetPath().c_str()));
978+
const ROOT::RNTuple *mainAnchor = file->Get<ROOT::RNTuple>("ntpl");
979+
ASSERT_NE(mainAnchor, nullptr);
980+
981+
footerSeek = mainAnchor->GetSeekFooter();
982+
footerNBytes = mainAnchor->GetNBytesFooter() - 8;
983+
}
984+
985+
// Patch the attribute schema version (and update the footer checksum)
986+
const std::uint64_t majorOff = 0xA0;
987+
const std::byte newMajorVersion{0x99};
988+
PatchRNTupleSection(fileGuard.GetPath(), footerSeek, footerNBytes, majorOff, &newMajorVersion,
989+
sizeof(newMajorVersion), EEndianness::LE);
990+
991+
auto reader = RNTupleReader::Open("ntpl", fileGuard.GetPath());
992+
993+
// Fetch an attribute set
994+
try {
995+
auto attrSet = reader->OpenAttributeSet("MyAttrSet");
996+
FAIL() << "opening an attribute set with an unknown major version should fail.";
997+
} catch (const ROOT::RException &ex) {
998+
EXPECT_THAT(ex.what(), testing::HasSubstr("unsupported attribute schema version"));
999+
}
1000+
}
1001+
1002+
TEST(RNTupleAttributes, ReadAttributesUnknownMinor)
1003+
{
1004+
// Try reading an attribute set with an unknown Minor schema version and verify it works.
1005+
FileRaii fileGuard("test_ntuple_attrs_futureminorschema.root");
1006+
1007+
ROOT::TestSupport::CheckDiagsRAII diagsRaii;
1008+
diagsRaii.requiredDiag(kWarning, "ROOT.NTuple", "RNTuple Attributes are experimental", false);
1009+
1010+
// Write a regular RNTuple with attributes
1011+
{
1012+
auto file = std::unique_ptr<TFile>(TFile::Open(fileGuard.GetPath().c_str(), "RECREATE"));
1013+
auto opts = RNTupleWriteOptions();
1014+
opts.SetCompression(0);
1015+
auto writer = RNTupleWriter::Append(RNTupleModel::Create(), "ntpl", *file, opts);
1016+
auto attrSet = writer->CreateAttributeSet(RNTupleModel::Create(), "MyAttrSet");
1017+
1018+
auto attrRange = attrSet->BeginRange();
1019+
for (int i = 0; i < 10; ++i) {
1020+
writer->Fill();
1021+
}
1022+
attrSet->CommitRange(std::move(attrRange));
1023+
}
1024+
1025+
// Get metadata about the main footer that we're gonna patch.
1026+
std::uint64_t footerSeek = 0, footerNBytes = 0;
1027+
{
1028+
auto file = std::unique_ptr<TFile>(TFile::Open(fileGuard.GetPath().c_str()));
1029+
const ROOT::RNTuple *mainAnchor = file->Get<ROOT::RNTuple>("ntpl");
1030+
ASSERT_NE(mainAnchor, nullptr);
1031+
1032+
footerSeek = mainAnchor->GetSeekFooter();
1033+
footerNBytes = mainAnchor->GetNBytesFooter() - 8;
1034+
}
1035+
1036+
// Patch the attribute schema version (and update the footer checksum)
1037+
const std::uint64_t majorOff = 0xA2;
1038+
const std::byte newMinorVersion{0x99};
1039+
PatchRNTupleSection(fileGuard.GetPath(), footerSeek, footerNBytes, majorOff, &newMinorVersion,
1040+
sizeof(newMinorVersion), EEndianness::LE);
1041+
1042+
auto reader = RNTupleReader::Open("ntpl", fileGuard.GetPath());
1043+
EXPECT_NO_THROW(reader->OpenAttributeSet("MyAttrSet"));
1044+
}

0 commit comments

Comments
 (0)