Skip to content

Commit c4d0d1f

Browse files
committed
[test] add ROOT sniffer testing
Verify execution of several supported requests which can be handled by http server. Testing: - root.json - root.xml - exe.json - exe.json with POST data - item.json - cmd.json - multi.json Also verify basic functionality of TRootSniffer::DecodeUrlOptionValue method
1 parent 772aa0f commit c4d0d1f

3 files changed

Lines changed: 350 additions & 3 deletions

File tree

net/httpsniff/CMakeLists.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# Copyright (C) 1995-2019, Rene Brun and Fons Rademakers.
1+
# Copyright (C) 1995-2026, Rene Brun and Fons Rademakers.
22
# All rights reserved.
33
#
44
# For the licensing terms see $ROOTSYS/LICENSE.
55
# For the list of contributors see $ROOTSYS/README/CREDITS.
66

77
############################################################################
8-
# CMakeLists.txt file for building ROOT net/http package
9-
# @author Pere Mato, CERN
8+
# CMakeLists.txt file for building ROOT net/httpsniff package
9+
# @author Sergey Linev, GSI
1010
############################################################################
1111

1212
ROOT_STANDARD_LIBRARY_PACKAGE(RHTTPSniff
@@ -24,3 +24,5 @@ ROOT_STANDARD_LIBRARY_PACKAGE(RHTTPSniff
2424
Tree
2525
XMLIO
2626
)
27+
28+
ROOT_ADD_TEST_SUBDIRECTORY(test)

net/httpsniff/test/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Copyright (C) 1995-2026, Rene Brun and Fons Rademakers.
2+
# All rights reserved.
3+
#
4+
# For the licensing terms see $ROOTSYS/LICENSE.
5+
# For the list of contributors see $ROOTSYS/README/CREDITS.
6+
7+
############################################################################
8+
# CMakeLists.txt file for building ROOT net/http package
9+
# @author Sergey Linev, GSI
10+
############################################################################
11+
12+
ROOT_ADD_GTEST(testRootSniffer test_sniffer.cxx LIBRARIES RHTTPSniff)
Lines changed: 333 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,333 @@
1+
#include "gtest/gtest.h"
2+
3+
#include <string>
4+
5+
#include "TNamed.h"
6+
#include "TH1.h"
7+
#include "TBufferJSON.h"
8+
#include "THttpCallArg.h"
9+
#include "TROOT.h"
10+
#include "TRootSnifferFull.h"
11+
12+
#include "ROOT/TestSupport.hxx"
13+
14+
15+
// simple class to access protected method
16+
17+
class TDecodeTest : public TRootSniffer {
18+
public:
19+
std::string Decode(const char *value, Bool_t remove_quotes = kTRUE)
20+
{
21+
TString res = DecodeUrlOptionValue(value, remove_quotes);
22+
return res.Data();
23+
}
24+
};
25+
26+
// check basic URL parameters decoding
27+
TEST(TRootSniffer, decode_url_options)
28+
{
29+
TDecodeTest test;
30+
31+
EXPECT_EQ(test.Decode(""), "");
32+
33+
// single quote has to be escaped
34+
EXPECT_EQ(test.Decode("\""), "\\\"");
35+
36+
// single backalsh has to be escaped
37+
EXPECT_EQ(test.Decode("\\"), "\\\\");
38+
39+
// remove quotes
40+
EXPECT_EQ(test.Decode("\"\""), "");
41+
42+
// remove quotes and escape quotes
43+
EXPECT_EQ(test.Decode("\"\"\""), "\\\"");
44+
45+
// remove quotes and escape backslah
46+
EXPECT_EQ(test.Decode("\"\\\""), "\\\\");
47+
48+
// remove quotes and remove special charsescape backslah
49+
EXPECT_EQ(test.Decode("\"abc\njkl\t\""), "abcjkl");
50+
51+
// keep quotes
52+
EXPECT_EQ(test.Decode("\"\"", kFALSE), "\"\"");
53+
54+
// keep quotes and escape inside quotes
55+
EXPECT_EQ(test.Decode("\"\"\"", kFALSE), "\"\\\"\"");
56+
57+
// keep quotes and keep german letters - remove new line
58+
EXPECT_EQ(test.Decode("\"Gänse\nfüßchen\"", kFALSE), "\"Gänsefüßchen\"");
59+
}
60+
61+
// check JSON representation for the objects
62+
TEST(TRootSniffer, root_json)
63+
{
64+
TNamed obj("obj", "title");
65+
66+
TRootSnifferFull sniffer;
67+
68+
sniffer.RegisterObject("/", &obj);
69+
70+
std::string res;
71+
sniffer.Produce("/obj", "root.json", "", res);
72+
EXPECT_EQ(res, "{\n"
73+
" \"_typename\" : \"TNamed\",\n"
74+
" \"fUniqueID\" : 0,\n"
75+
" \"fBits\" : 8,\n"
76+
" \"fName\" : \"obj\",\n"
77+
" \"fTitle\" : \"title\"\n"
78+
"}");
79+
}
80+
81+
// check XML representation for the objects
82+
TEST(TRootSniffer, root_xml)
83+
{
84+
TNamed obj("obj", "title");
85+
86+
TRootSnifferFull sniffer;
87+
88+
sniffer.RegisterObject("/", &obj);
89+
90+
std::string res;
91+
sniffer.Produce("/obj", "root.xml", "", res);
92+
EXPECT_EQ(res, "<Object class=\"TNamed\">\n"
93+
" <TNamed version=\"1\">\n"
94+
" <TObject fUniqueID=\"0\" fBits=\"8\"/>\n"
95+
" <fName str=\"obj\"/>\n"
96+
" <fTitle str=\"title\"/>\n"
97+
" </TNamed>\n"
98+
"</Object>\n");
99+
}
100+
101+
// check BINARY representation for the objects
102+
TEST(TRootSniffer, root_bin)
103+
{
104+
TNamed obj("obj", "title");
105+
106+
TRootSnifferFull sniffer;
107+
108+
sniffer.RegisterObject("/", &obj);
109+
110+
std::string res;
111+
sniffer.Produce("/obj", "root.bin", "", res);
112+
// keep minimal margin for binary format change
113+
EXPECT_NEAR(res.length(), 26, 4);
114+
}
115+
116+
// check root file creation for the objects
117+
TEST(TRootSniffer, file_root)
118+
{
119+
TNamed obj("obj", "title");
120+
121+
TRootSnifferFull sniffer;
122+
123+
sniffer.RegisterObject("/", &obj);
124+
125+
std::string res;
126+
sniffer.Produce("/obj", "file.root", "", res);
127+
// TODO: anlyze why so big size for small object
128+
EXPECT_NEAR(res.length(), 2097152, 10000);
129+
}
130+
131+
// check hierarchy request
132+
TEST(TRootSniffer, item_json)
133+
{
134+
TNamed obj("obj", "title");
135+
136+
TRootSnifferFull sniffer;
137+
138+
sniffer.RegisterObject("/", &obj);
139+
140+
std::string res;
141+
sniffer.Produce("/obj", "item.json", "", res);
142+
143+
EXPECT_EQ(res, "{\n"
144+
" \"_name\" : \"obj\",\n"
145+
" \"_root_version\" : " + std::to_string(gROOT->GetVersionCode()) + ",\n"
146+
" \"_kind\" : \"ROOT.TNamed\",\n"
147+
" \"_title\" : \"title\"\n"
148+
"}");
149+
}
150+
151+
// simple method execution
152+
TEST(TRootSniffer, exe_json)
153+
{
154+
TNamed obj("obj","title");
155+
156+
TRootSnifferFull sniffer;
157+
158+
sniffer.RegisterObject("/", &obj);
159+
160+
std::string res0;
161+
// by default methods execution is not allowed
162+
sniffer.Produce("/obj", "exe.json", "method=GetTitle", res0);
163+
EXPECT_EQ(res0, "");
164+
165+
// disable readonly to get method executed
166+
sniffer.SetReadOnly(kFALSE);
167+
168+
// only now one can execute method
169+
std::string res1;
170+
sniffer.Produce("/obj", "exe.json", "method=GetTitle", res1);
171+
EXPECT_EQ(res1, "\"title\"");
172+
}
173+
174+
175+
// execute method with post data - lot of gymnastic around
176+
TEST(TRootSniffer, exe_post_json)
177+
{
178+
TH1I hist("hist", "title", 10, 0, 10);
179+
hist.SetDirectory(nullptr);
180+
hist.SetBinContent(5, 10);
181+
182+
std::string json;
183+
{
184+
// only temporary to create json
185+
TH1I hist2("hist", "title", 10, 0, 10);
186+
hist.SetDirectory(nullptr);
187+
hist2.SetBinContent(5, 20);
188+
json = TBufferJSON::ToJSON(&hist2).Data();
189+
}
190+
191+
192+
TRootSnifferFull sniffer;
193+
194+
sniffer.RegisterObject("/", &hist);
195+
196+
// disable readonly to execute method
197+
sniffer.SetReadOnly(kFALSE);
198+
// allow use of POST data to decode object from JSON
199+
sniffer.SetAllowPostObject(kTRUE);
200+
201+
THttpCallArg arg;
202+
arg.SetPostData(std::move(json));
203+
sniffer.SetCurrentCallArg(&arg);
204+
205+
// before execution content is 10
206+
EXPECT_EQ(hist.GetBinContent(5), 10);
207+
208+
209+
std::string res;
210+
sniffer.Produce("/hist", "exe.json", "method=Add&prototype='const TH1*,Double_t'&h1=_post_object_json_&_destroy_post_", res);
211+
EXPECT_EQ(res, "1");
212+
213+
// and now most important - bin content has to change
214+
EXPECT_EQ(hist.GetBinContent(5), 30);
215+
}
216+
217+
// changing object title
218+
TEST(TRootSniffer, set_title)
219+
{
220+
TNamed obj("obj", "title");
221+
222+
TRootSnifferFull sniffer;
223+
// disable readonly to get method executed
224+
sniffer.SetReadOnly(kFALSE);
225+
226+
sniffer.RegisterObject("/", &obj);
227+
228+
std::string res;
229+
230+
sniffer.Produce("/obj", "exe.json", "method=SetTitle&title=NewTitle", res);
231+
EXPECT_EQ(res, "null");
232+
EXPECT_EQ(std::string("NewTitle"), obj.GetTitle());
233+
234+
res = "";
235+
sniffer.Produce("/obj", "exe.json", "method=SetTitle&title=\"QuotedTitle\"", res);
236+
EXPECT_EQ(res, "null");
237+
EXPECT_EQ(std::string("QuotedTitle"), obj.GetTitle());
238+
239+
res = "";
240+
sniffer.Produce("/obj", "exe.json", "method=SetTitle&title=%22UrlStyleQuotedTitle%22", res);
241+
EXPECT_EQ(res, "null");
242+
EXPECT_EQ(std::string("UrlStyleQuotedTitle"), obj.GetTitle());
243+
244+
res = "";
245+
sniffer.Produce("/obj", "exe.json", "method=SetTitle&title=Mail\"Formed\"Title", res);
246+
EXPECT_EQ(res, "null");
247+
EXPECT_EQ(std::string("Mail\"Formed\"Title"), obj.GetTitle());
248+
}
249+
250+
// testing command execution with different signatures
251+
TEST(TRootSniffer, cmd_json)
252+
{
253+
TNamed obj("obj", "title");
254+
255+
TRootSnifferFull sniffer;
256+
sniffer.SetReadOnly(kFALSE);
257+
258+
sniffer.RegisterObject("/", &obj);
259+
sniffer.RegisterCommand("/Print1", "/obj/->Print(%arg1%)", "");
260+
sniffer.RegisterCommand("/Print2", "/obj/->Print(\"%arg1%\")", "");
261+
sniffer.RegisterCommand("/GetSize", "/obj/->Sizeof()", "");
262+
263+
std::string res;
264+
// quotes are in URL
265+
sniffer.Produce("/Print1", "cmd.json", "arg1=%22*%22", res);
266+
EXPECT_EQ(res, "0");
267+
268+
res = "";
269+
// skipping quotes from URL - when they are necessary
270+
// sniffer should have add them automatically
271+
sniffer.Produce("/Print1", "cmd.json", "arg1=*", res);
272+
EXPECT_EQ(res, "0");
273+
274+
res = "";
275+
// skipping quotes from URL - when they are necessary
276+
// while value looks like number, sniffer will not quote it
277+
// result of process line is not result is
278+
sniffer.Produce("/Print1", "cmd.json", "arg1=0", res);
279+
EXPECT_EQ(res, "0");
280+
281+
res = "";
282+
// quotes are in command definition
283+
sniffer.Produce("/Print2", "cmd.json", "arg1=*", res);
284+
EXPECT_EQ(res, "0");
285+
286+
res = "";
287+
// quotes are in command definition but we try to add our own
288+
// sniffer will remove them
289+
sniffer.Produce("/Print2", "cmd.json", "arg1=\"*\"", res);
290+
EXPECT_EQ(res, "0");
291+
292+
res = "";
293+
// Execute command which returns some value
294+
sniffer.Produce("/GetSize", "cmd.json", "", res);
295+
// returns only strings sizes
296+
EXPECT_EQ(res, "10");
297+
}
298+
299+
300+
301+
// check JSON representation for the objects
302+
TEST(TRootSniffer, multi_json)
303+
{
304+
TNamed obj1("obj1", "title1");
305+
TNamed obj2("obj2", "title2");
306+
307+
TRootSnifferFull sniffer;
308+
309+
sniffer.RegisterObject("/", &obj1);
310+
sniffer.RegisterObject("/", &obj2);
311+
312+
std::string items = "/obj1/root.json\n/obj2/root.json\n";
313+
314+
THttpCallArg arg;
315+
arg.SetPostData(std::move(items));
316+
sniffer.SetCurrentCallArg(&arg);
317+
318+
std::string res;
319+
sniffer.Produce("", "multi.json", "number=2", res);
320+
EXPECT_EQ(res, "[{\n"
321+
" \"_typename\" : \"TNamed\",\n"
322+
" \"fUniqueID\" : 0,\n"
323+
" \"fBits\" : 8,\n"
324+
" \"fName\" : \"obj1\",\n"
325+
" \"fTitle\" : \"title1\"\n"
326+
"}, {\n"
327+
" \"_typename\" : \"TNamed\",\n"
328+
" \"fUniqueID\" : 0,\n"
329+
" \"fBits\" : 8,\n"
330+
" \"fName\" : \"obj2\",\n"
331+
" \"fTitle\" : \"title2\"\n"
332+
"}]");
333+
}

0 commit comments

Comments
 (0)