Skip to content

Commit fb34b18

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 d9aca6d commit fb34b18

3 files changed

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

0 commit comments

Comments
 (0)