Skip to content

Commit 3d046a9

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 5f5ca3c commit 3d046a9

3 files changed

Lines changed: 348 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: 331 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,331 @@
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+
// escape quotes in the middle
52+
EXPECT_EQ(test.Decode("someFunc(\"someArg\");someArray[3];"), "someFunc(\\\"someArg\\\");someArray[3];");
53+
54+
// keep quotes
55+
EXPECT_EQ(test.Decode("\"\"", kFALSE), "\"\"");
56+
57+
// keep quotes and escape inside quotes
58+
EXPECT_EQ(test.Decode("\"\"\"", kFALSE), "\"\\\"\"");
59+
60+
// keep quotes and keep german letters - remove new line
61+
EXPECT_EQ(test.Decode("\"Gänse\nfüßchen\"", kFALSE), "\"Gänsefüßchen\"");
62+
}
63+
64+
// check JSON representation for the objects
65+
TEST(TRootSniffer, root_json)
66+
{
67+
TNamed obj("obj", "title");
68+
69+
TRootSnifferFull sniffer;
70+
71+
sniffer.RegisterObject("/", &obj);
72+
73+
std::string res;
74+
sniffer.Produce("/obj", "root.json", "", res);
75+
EXPECT_EQ(res, "{\n"
76+
" \"_typename\" : \"TNamed\",\n"
77+
" \"fUniqueID\" : 0,\n"
78+
" \"fBits\" : 8,\n"
79+
" \"fName\" : \"obj\",\n"
80+
" \"fTitle\" : \"title\"\n"
81+
"}");
82+
}
83+
84+
// check XML representation for the objects
85+
TEST(TRootSniffer, root_xml)
86+
{
87+
TNamed obj("obj", "title");
88+
89+
TRootSnifferFull sniffer;
90+
91+
sniffer.RegisterObject("/", &obj);
92+
93+
std::string res;
94+
sniffer.Produce("/obj", "root.xml", "", res);
95+
EXPECT_EQ(res, "<Object class=\"TNamed\">\n"
96+
" <TNamed version=\"1\">\n"
97+
" <TObject fUniqueID=\"0\" fBits=\"8\"/>\n"
98+
" <fName str=\"obj\"/>\n"
99+
" <fTitle str=\"title\"/>\n"
100+
" </TNamed>\n"
101+
"</Object>\n");
102+
}
103+
104+
// check BINARY representation for the objects
105+
TEST(TRootSniffer, root_bin)
106+
{
107+
TNamed obj("obj", "title");
108+
109+
TRootSnifferFull sniffer;
110+
111+
sniffer.RegisterObject("/", &obj);
112+
113+
std::string res;
114+
sniffer.Produce("/obj", "root.bin", "", res);
115+
// keep minimal margin for binary format change
116+
EXPECT_NEAR(res.length(), 26, 4);
117+
}
118+
119+
// check root file creation for the objects
120+
TEST(TRootSniffer, file_root)
121+
{
122+
TNamed obj("obj", "title");
123+
124+
TRootSnifferFull sniffer;
125+
126+
sniffer.RegisterObject("/", &obj);
127+
128+
std::string res;
129+
sniffer.Produce("/obj", "file.root", "", res);
130+
EXPECT_NEAR(res.length(), 2097152, 10000) << "size of file.root request";
131+
}
132+
133+
// check hierarchy request
134+
TEST(TRootSniffer, item_json)
135+
{
136+
TNamed obj("obj", "title");
137+
138+
TRootSnifferFull sniffer;
139+
140+
sniffer.RegisterObject("/", &obj);
141+
142+
std::string res;
143+
sniffer.Produce("/obj", "item.json", "", res);
144+
145+
EXPECT_EQ(res, "{\n"
146+
" \"_name\" : \"obj\",\n"
147+
" \"_root_version\" : " + std::to_string(gROOT->GetVersionCode()) + ",\n"
148+
" \"_kind\" : \"ROOT.TNamed\",\n"
149+
" \"_title\" : \"title\"\n"
150+
"}") << "return value of item.json";
151+
}
152+
153+
// simple method execution
154+
TEST(TRootSniffer, exe_json)
155+
{
156+
TNamed obj("obj","title");
157+
158+
TRootSnifferFull sniffer;
159+
160+
sniffer.RegisterObject("/", &obj);
161+
162+
std::string res0;
163+
// by default methods execution is not allowed
164+
sniffer.Produce("/obj", "exe.json", "method=GetTitle", res0);
165+
EXPECT_EQ(res0, "") << "return value of exe.json in readonly";
166+
167+
// disable readonly to get method executed
168+
sniffer.SetReadOnly(kFALSE);
169+
170+
// only now one can execute method
171+
std::string res1;
172+
sniffer.Produce("/obj", "exe.json", "method=GetTitle", res1);
173+
EXPECT_EQ(res1, "\"title\"") << "return value of exe.json for GetTitle";
174+
}
175+
176+
177+
// execute method with post data - lot of gymnastic around
178+
TEST(TRootSniffer, exe_post_json)
179+
{
180+
TH1I hist("hist", "title", 10, 0, 10);
181+
hist.SetDirectory(nullptr);
182+
hist.SetBinContent(5, 10);
183+
184+
std::string json;
185+
{
186+
// only temporary to create json
187+
TH1I hist2("hist", "title", 10, 0, 10);
188+
hist.SetDirectory(nullptr);
189+
hist2.SetBinContent(5, 20);
190+
json = TBufferJSON::ToJSON(&hist2).Data();
191+
}
192+
193+
TRootSnifferFull sniffer;
194+
195+
sniffer.RegisterObject("/", &hist);
196+
197+
// disable readonly to execute method
198+
sniffer.SetReadOnly(kFALSE);
199+
// allow use of POST data to decode object from JSON
200+
sniffer.SetAllowPostObject(kTRUE);
201+
202+
THttpCallArg arg;
203+
arg.SetPostData(std::move(json));
204+
sniffer.SetCurrentCallArg(&arg);
205+
206+
// before execution content is 10
207+
EXPECT_EQ(hist.GetBinContent(5), 10);
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") << "return value of exe.json";
212+
213+
// and now most important - bin content has to change
214+
EXPECT_EQ(hist.GetBinContent(5), 30) << "check of histogram content";
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") << "return value of exe.json when methout return void";
232+
EXPECT_EQ(std::string("NewTitle"), obj.GetTitle()) << "compare object title with applied value";
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()) << "compare object title with applied value";
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()) << "compare object title with applied value";
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()) << "compare object title with applied value";
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") << "return value of cmd.json";
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") << "return value of cmd.json";
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") << "return value of cmd.json";
280+
281+
res = "";
282+
// quotes are in command definition
283+
sniffer.Produce("/Print2", "cmd.json", "arg1=*", res);
284+
EXPECT_EQ(res, "0") << "return value of cmd.json";
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") << "return value of cmd.json";
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") << "return value of cmd.json with object size";
297+
}
298+
299+
// check JSON representation for the objects
300+
TEST(TRootSniffer, multi_json)
301+
{
302+
TNamed obj1("obj1", "title1");
303+
TNamed obj2("obj2", "title2");
304+
305+
TRootSnifferFull sniffer;
306+
307+
sniffer.RegisterObject("/", &obj1);
308+
sniffer.RegisterObject("/", &obj2);
309+
310+
std::string items = "/obj1/root.json\n/obj2/root.json\n";
311+
312+
THttpCallArg arg;
313+
arg.SetPostData(std::move(items));
314+
sniffer.SetCurrentCallArg(&arg);
315+
316+
std::string res;
317+
sniffer.Produce("", "multi.json", "number=2", res);
318+
EXPECT_EQ(res, "[{\n"
319+
" \"_typename\" : \"TNamed\",\n"
320+
" \"fUniqueID\" : 0,\n"
321+
" \"fBits\" : 8,\n"
322+
" \"fName\" : \"obj1\",\n"
323+
" \"fTitle\" : \"title1\"\n"
324+
"}, {\n"
325+
" \"_typename\" : \"TNamed\",\n"
326+
" \"fUniqueID\" : 0,\n"
327+
" \"fBits\" : 8,\n"
328+
" \"fName\" : \"obj2\",\n"
329+
" \"fTitle\" : \"title2\"\n"
330+
"}]") << "return value of multi.json";
331+
}

0 commit comments

Comments
 (0)