Project
Loading...
Searching...
No Matches
test_Objects.cxx
Go to the documentation of this file.
1// Copyright 2019-2026 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12#include "AODNative/Errors.h"
13#include "AODNative/Objects.h"
14#include "Cursor.h"
15#include "RootFraming.h"
16#include <catch_amalgamated.hpp>
17
18using namespace o2::aod::native;
19using namespace o2::aod::native::test;
20
21TEST_CASE("Cursor reads big-endian integers")
22{
23 std::vector<uint8_t> buf{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
24 std::string file = "memory";
25 Cursor c{buf, file};
26 CHECK(c.u16() == 0x1234);
27 CHECK(c.u32() == 0x56789abc);
28 CHECK(c.u8() == 0xde);
29 CHECK(c.u64() == 0xf001020304050607ULL);
30 CHECK(c.remaining() == 0);
31 CHECK_THROWS_AS(c.u8(), CorruptData);
32}
33
34TEST_CASE("Cursor reads both TString encodings")
35{
37 w.string("short");
38 w.bytes.push_back(0xFF);
39 w.put<uint32_t>(300);
40 w.bytes.insert(w.bytes.end(), 300, 'x');
41 std::string file = "memory";
42 Cursor c{w.bytes, file};
43 CHECK(c.string() == "short");
44 CHECK(c.string() == std::string(300, 'x'));
45 CHECK(c.remaining() == 0);
46}
47
48TEST_CASE("Cursor never reads out of bounds")
49{
50 std::vector<uint8_t> buf{0x05, 'a', 'b'}; // TString claiming 5 bytes, only 2 there
51 std::string file = "memory";
52 Cursor c{buf, file};
53 CHECK_THROWS_AS(c.string(), CorruptData);
54 Cursor d{buf, file};
55 CHECK_THROWS_AS(d.seek(4), CorruptData);
56 CHECK_THROWS_AS(d.skip(4), CorruptData);
57 Cursor e{buf, file};
58 CHECK_THROWS_AS(e.cstring(), CorruptData);
59}
60
61TEST_CASE("Cursor reports displacement relative to the key")
62{
63 std::vector<uint8_t> buf(10);
64 std::string file = "memory";
65 Cursor c{buf, file, 1000, 64};
66 c.skip(4);
67 CHECK(c.displacement() == 68);
68}
69
70TEST_CASE("truncated tree payloads raise errors, never crash")
71{
72 // Arbitrary bytes, then every truncation of them: each must fail cleanly.
73 std::vector<uint8_t> junk(200);
74 for (size_t i = 0; i < junk.size(); ++i) {
75 junk[i] = (uint8_t)(i * 37 + 11);
76 }
77 // A plausible start: byte count with the flag set, TTree version 20.
78 junk[0] = 0x40;
79 junk[4] = 0x00;
80 junk[5] = 20;
81 for (size_t n = 0; n <= junk.size(); n += 7) {
82 std::span<const uint8_t> part{junk.data(), n};
83 CHECK_THROWS_AS(readTreePayload(part, 64, "memory", 0), FormatError);
84 }
85}
86
87TEST_CASE("unknown class versions raise UnsupportedVersion")
88{
90 w.put<uint32_t>(0x40000000 | 100); // byte count
91 w.put<uint16_t>(19); // TTree version 19: not supported
92 w.bytes.resize(104);
93 CHECK_THROWS_AS(readTreePayload(w.bytes, 64, "memory", 0), UnsupportedVersion);
94}
95
96TEST_CASE("leaf element sizes")
97{
98 CHECK(leafElementSize('O') == 1);
99 CHECK(leafElementSize('B') == 1);
100 CHECK(leafElementSize('S') == 2);
101 CHECK(leafElementSize('I') == 4);
102 CHECK(leafElementSize('F') == 4);
103 CHECK(leafElementSize('L') == 8);
104 CHECK(leafElementSize('D') == 8);
105 CHECK_THROWS_AS(leafElementSize('C'), UnsupportedLayout);
106}
int32_t i
uint32_t c
Definition RawData.h:2
std::span< const uint8_t > bytes(size_t n)
Definition Cursor.h:60
#define CHECK
GLdouble n
Definition glcorearb.h:1982
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition glcorearb.h:2514
GLubyte GLubyte GLubyte GLubyte w
Definition glcorearb.h:852
TreeInfo readTreePayload(std::span< const uint8_t > payload, uint16_t keylen, std::string const &file, uint64_t fileOffset)
Definition Objects.cxx:544
size_t leafElementSize(char leafClass)
Size in bytes of one value of a leaf class on disk.
Definition Objects.cxx:503
A class version we have no hardcoded layout for.
Definition Errors.h:29
void string(std::string const &s)
TEST_CASE("Cursor reads big-endian integers")