Skip to content

Commit 32d1371

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 - file.root - exe.json - exe.json with POST data - item.json - cmd.json - multi.json Also verify basic functionality of TRootSniffer::DecodeUrlOptionValue method
1 parent e473667 commit 32d1371

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

0 commit comments

Comments
 (0)