Belle II Software light-2406-ragdoll
stream.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#include <framework/utilities/Stream.h>
9#include <framework/dataobjects/RelationContainer.h>
10#include <framework/dataobjects/RelationElement.h>
11#include <framework/datastore/DataStore.h>
12#include <framework/gearbox/Gearbox.h>
13#include <framework/gearbox/GearDir.h>
14
15#include <TVector3.h>
16
17#include <fstream>
18#include <gtest/gtest.h>
19
20using namespace std;
21using namespace Belle2;
22
23namespace {
25 RelationContainer* createObject()
26 {
27 auto* relCont = new RelationContainer;
28 relCont->setFromName("a");
29 relCont->setToName("b");
30 relCont->setFromDurability(DataStore::c_Event);
31 relCont->setToDurability(DataStore::c_Event);
32 TClonesArray& relations = relCont->elements();
33 //let's make this a bit larger (very small objects are never compressed)
34 for (int i = 0; i < 100; i++)
35 new (relations.AddrAt(relations.GetLast() + 1)) RelationElement(0, i + 1, 42.0);
36
37 return relCont;
38 }
39
41 void checkObject(const RelationContainer* rel)
42 {
43 ASSERT_NE(rel, nullptr);
44 EXPECT_TRUE(rel->getFromName() == "a");
45 EXPECT_TRUE(rel->getToName() == "b");
46 EXPECT_EQ(rel->getEntries(), 100);
47 for (int i = 0; i < 100; i++) {
48 EXPECT_EQ(rel->getElement(i).getToIndex(0), (unsigned int)(i + 1));
49 EXPECT_DOUBLE_EQ(rel->getElement(i).getWeight(0), 42.0);
50 }
51 }
52
54 void checkGbContents()
55 {
56 const Gearbox& gb = Gearbox::getInstance();
57
58 checkObject(dynamic_cast<const RelationContainer*>(gb.getTObject("/A/RelationContainer")));
59
60 //Access from GearDir should work
61 GearDir detector("/A/");
62 checkObject(dynamic_cast<const RelationContainer*>(detector.getTObject("RelationContainer")));
63 }
64
66 TEST(StreamTest, raw)
67 {
68 TVector3 v(1.0, 2.0, 3.0);
69 std::string vStr = Stream::serializeAndEncode(&v);
70 //B2INFO(vStr);
71
72 //restore
73 TObject* obj = Stream::deserializeEncodedRawData(vStr);
74 auto* v2 = dynamic_cast<TVector3*>(obj);
75 ASSERT_NE(v2, nullptr);
76 EXPECT_TRUE(*v2 == v);
77
78 //something more complex
79 RelationContainer* relCont = createObject();
80
81 std::string relStr = Stream::serializeAndEncode(relCont);
82 //B2INFO(relStr);
84 checkObject(dynamic_cast<const RelationContainer*>(obj));
85
86 //creating file for next test..
87 /*
88 std::ofstream file("object_base64.xml");
89 file << Stream::escapeXML(relStr);
90 */
91
92 // Given random input, TBase64 will most likely just crash, this specific truncation seems ok.
93 // Note that the string length needs to be a multiple of 4 because
94 // otherwise TBase64 will definitely access memory it should not as it just
95 // assumes multiple of four and then performs an invalid read of a char
96 // which it casts to unsigned int.
97 std::string truncated = vStr.substr(0, 12);
98 TObject* broken_obj = Stream::deserializeEncodedRawData(truncated);
99 EXPECT_TRUE(broken_obj == nullptr);
100 }
101
103 TEST(StreamTest, GearboxRaw)
104 {
106 vector<string> backends;
107 backends.emplace_back("file:/framework/tests/");
108 gb.setBackends(backends);
109 gb.open("object_base64.xml");
110
111 checkGbContents();
112 }
113
114} // namespace
@ c_Event
Different object in each event, all objects/arrays are invalidated after event() function has been ca...
Definition: DataStore.h:59
GearDir is the basic class used for accessing the parameter store.
Definition: GearDir.h:31
Singleton class responsible for loading detector parameters from an XML file.
Definition: Gearbox.h:34
Class to store relations between StoreArrays in the DataStore.
const std::string & getFromName() const
Get name of the StoreArray we relate from.
const RelationElement & getElement(int i) const
Get reference to RelationElement at index i.
const std::string & getToName() const
Get name of the StoreArray we relate to.
int getEntries() const
Get number of elements.
void setFromName(const std::string &name)
Set name of the StoreArray we relate from.
Class to store a single element of a relation.
index_type getToIndex(size_t n=0) const
Get nth index we point to.
weight_type getWeight(size_t n=0) const
Get nth weight we point to.
static Gearbox & getInstance()
Return reference to the Gearbox instance.
Definition: Gearbox.cc:81
virtual const TObject * getTObject(const std::string &path) const noexcept(false) override
Get the parameter path as a TObject.
Definition: Gearbox.cc:295
void setBackends(const std::vector< std::string > &backends)
Select the backends to use to find resources.
Definition: Gearbox.cc:99
void open(const std::string &name="Belle2.xml", size_t cacheSize=c_DefaultCacheSize)
Open connection to backend and parse tree.
Definition: Gearbox.cc:133
std::string serializeAndEncode(const TObject *obj)
Convert given TObject into encoded byte stream (for storing in XML).
Definition: Stream.cc:32
TObject * deserializeEncodedRawData(const std::string &base64Data)
Convert given serialized raw data back into TObject.
Definition: Stream.cc:72
Abstract base class for different kinds of events.
Definition: ClusterUtils.h:24
STL namespace.