|
| 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("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("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("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 hierarchy request |
| 122 | +TEST(TRootSniffer, item_json) |
| 123 | +{ |
| 124 | + TNamed obj("obj", "title"); |
| 125 | + |
| 126 | + TRootSnifferFull sniffer("sniffer"); |
| 127 | + |
| 128 | + sniffer.RegisterObject("/", &obj); |
| 129 | + |
| 130 | + std::string res; |
| 131 | + sniffer.Produce("/obj", "item.json", "", res); |
| 132 | + |
| 133 | + EXPECT_EQ(res, "{\n" |
| 134 | + " \"_name\" : \"obj\",\n" |
| 135 | + " \"_root_version\" : " + std::to_string(gROOT->GetVersionCode()) + ",\n" |
| 136 | + " \"_kind\" : \"ROOT.TNamed\",\n" |
| 137 | + " \"_title\" : \"title\"\n" |
| 138 | + "}") << "return value of item.json"; |
| 139 | +} |
| 140 | + |
| 141 | +// simple method execution |
| 142 | +TEST(TRootSniffer, exe_json) |
| 143 | +{ |
| 144 | + TNamed obj("obj","title"); |
| 145 | + |
| 146 | + TRootSnifferFull sniffer("sniffer"); |
| 147 | + |
| 148 | + sniffer.RegisterObject("/", &obj); |
| 149 | + |
| 150 | + std::string res0; |
| 151 | + // by default methods execution is not allowed |
| 152 | + sniffer.Produce("/obj", "exe.json", "method=GetTitle", res0); |
| 153 | + EXPECT_EQ(res0, "") << "return value of exe.json in readonly"; |
| 154 | + |
| 155 | + // disable readonly to get method executed |
| 156 | + sniffer.SetReadOnly(kFALSE); |
| 157 | + |
| 158 | + // only now one can execute method |
| 159 | + std::string res1; |
| 160 | + sniffer.Produce("/obj", "exe.json", "method=GetTitle", res1); |
| 161 | + EXPECT_EQ(res1, "\"title\"") << "return value of exe.json for GetTitle"; |
| 162 | +} |
| 163 | + |
| 164 | + |
| 165 | +// execute method with post data - lot of gymnastic around |
| 166 | +TEST(TRootSniffer, exe_post_json) |
| 167 | +{ |
| 168 | + TH1I hist("hist", "title", 10, 0, 10); |
| 169 | + hist.SetDirectory(nullptr); |
| 170 | + hist.SetBinContent(5, 10); |
| 171 | + |
| 172 | + std::string json; |
| 173 | + { |
| 174 | + // only temporary to create json |
| 175 | + TH1I hist2("hist", "title", 10, 0, 10); |
| 176 | + hist.SetDirectory(nullptr); |
| 177 | + hist2.SetBinContent(5, 20); |
| 178 | + json = TBufferJSON::ToJSON(&hist2).Data(); |
| 179 | + } |
| 180 | + |
| 181 | + TRootSnifferFull sniffer("sniffer"); |
| 182 | + |
| 183 | + sniffer.RegisterObject("/", &hist); |
| 184 | + |
| 185 | + // disable readonly to execute method |
| 186 | + sniffer.SetReadOnly(kFALSE); |
| 187 | + // allow use of POST data to decode object from JSON |
| 188 | + sniffer.SetAllowPostObject(kTRUE); |
| 189 | + |
| 190 | + THttpCallArg arg; |
| 191 | + arg.SetPostData(std::move(json)); |
| 192 | + sniffer.SetCurrentCallArg(&arg); |
| 193 | + |
| 194 | + // before execution content is 10 |
| 195 | + EXPECT_EQ(hist.GetBinContent(5), 10); |
| 196 | + |
| 197 | + std::string res; |
| 198 | + sniffer.Produce("/hist", "exe.json", "method=Add&prototype='const TH1*,Double_t'&h1=_post_object_json_&_destroy_post_", res); |
| 199 | + EXPECT_EQ(res, "1") << "return value of exe.json"; |
| 200 | + |
| 201 | + // and now most important - bin content has to change |
| 202 | + EXPECT_EQ(hist.GetBinContent(5), 30) << "check of histogram content"; |
| 203 | +} |
| 204 | + |
| 205 | +// changing object title |
| 206 | +TEST(TRootSniffer, set_title) |
| 207 | +{ |
| 208 | + TNamed obj("obj", "title"); |
| 209 | + |
| 210 | + TRootSnifferFull sniffer("sniffer"); |
| 211 | + // disable readonly to get method executed |
| 212 | + sniffer.SetReadOnly(kFALSE); |
| 213 | + |
| 214 | + sniffer.RegisterObject("/", &obj); |
| 215 | + |
| 216 | + std::string res; |
| 217 | + |
| 218 | + sniffer.Produce("/obj", "exe.json", "method=SetTitle&title=NewTitle", res); |
| 219 | + EXPECT_EQ(res, "null") << "return value of exe.json when methout return void"; |
| 220 | + EXPECT_EQ(std::string("NewTitle"), obj.GetTitle()) << "compare object title with applied value"; |
| 221 | + |
| 222 | + res = ""; |
| 223 | + sniffer.Produce("/obj", "exe.json", "method=SetTitle&title=\"QuotedTitle\"", res); |
| 224 | + EXPECT_EQ(res, "null"); |
| 225 | + EXPECT_EQ(std::string("QuotedTitle"), obj.GetTitle()) << "compare object title with applied value"; |
| 226 | + |
| 227 | + res = ""; |
| 228 | + sniffer.Produce("/obj", "exe.json", "method=SetTitle&title=%22UrlStyleQuotedTitle%22", res); |
| 229 | + EXPECT_EQ(res, "null"); |
| 230 | + EXPECT_EQ(std::string("UrlStyleQuotedTitle"), obj.GetTitle()) << "compare object title with applied value"; |
| 231 | + |
| 232 | + res = ""; |
| 233 | + sniffer.Produce("/obj", "exe.json", "method=SetTitle&title=Mail\"Formed\"Title", res); |
| 234 | + EXPECT_EQ(res, "null"); |
| 235 | + EXPECT_EQ(std::string("Mail\"Formed\"Title"), obj.GetTitle()) << "compare object title with applied value"; |
| 236 | +} |
| 237 | + |
| 238 | +// testing command execution with different signatures |
| 239 | +TEST(TRootSniffer, cmd_json) |
| 240 | +{ |
| 241 | + TNamed obj("obj", "title"); |
| 242 | + |
| 243 | + TRootSnifferFull sniffer("sniffer"); |
| 244 | + sniffer.SetReadOnly(kFALSE); |
| 245 | + |
| 246 | + sniffer.RegisterObject("/", &obj); |
| 247 | + sniffer.RegisterCommand("/Print1", "/obj/->Print(%arg1%)", ""); |
| 248 | + sniffer.RegisterCommand("/Print2", "/obj/->Print(\"%arg1%\")", ""); |
| 249 | + sniffer.RegisterCommand("/GetSize", "/obj/->Sizeof()", ""); |
| 250 | + |
| 251 | + std::string res; |
| 252 | + // quotes are in URL |
| 253 | + sniffer.Produce("/Print1", "cmd.json", "arg1=%22*%22", res); |
| 254 | + EXPECT_EQ(res, "0") << "return value of cmd.json"; |
| 255 | + |
| 256 | + res = ""; |
| 257 | + // skipping quotes from URL - when they are necessary |
| 258 | + // sniffer should have add them automatically |
| 259 | + sniffer.Produce("/Print1", "cmd.json", "arg1=*", res); |
| 260 | + EXPECT_EQ(res, "0") << "return value of cmd.json"; |
| 261 | + |
| 262 | + res = ""; |
| 263 | + // skipping quotes from URL - when they are necessary |
| 264 | + // while value looks like number, sniffer will not quote it |
| 265 | + // result of process line is not result is |
| 266 | + sniffer.Produce("/Print1", "cmd.json", "arg1=0", res); |
| 267 | + EXPECT_EQ(res, "0") << "return value of cmd.json"; |
| 268 | + |
| 269 | + res = ""; |
| 270 | + // quotes are in command definition |
| 271 | + sniffer.Produce("/Print2", "cmd.json", "arg1=*", res); |
| 272 | + EXPECT_EQ(res, "0") << "return value of cmd.json"; |
| 273 | + |
| 274 | + res = ""; |
| 275 | + // quotes are in command definition but we try to add our own |
| 276 | + // sniffer will remove them |
| 277 | + sniffer.Produce("/Print2", "cmd.json", "arg1=\"*\"", res); |
| 278 | + EXPECT_EQ(res, "0") << "return value of cmd.json"; |
| 279 | + |
| 280 | + res = ""; |
| 281 | + // Execute command which returns some value |
| 282 | + sniffer.Produce("/GetSize", "cmd.json", "", res); |
| 283 | + // returns only strings sizes |
| 284 | + EXPECT_EQ(res, "10") << "return value of cmd.json with object size"; |
| 285 | +} |
| 286 | + |
| 287 | +// check JSON representation for the objects |
| 288 | +TEST(TRootSniffer, multi_json) |
| 289 | +{ |
| 290 | + TNamed obj1("obj1", "title1"); |
| 291 | + TNamed obj2("obj2", "title2"); |
| 292 | + |
| 293 | + TRootSnifferFull sniffer("sniffer"); |
| 294 | + |
| 295 | + sniffer.RegisterObject("/", &obj1); |
| 296 | + sniffer.RegisterObject("/", &obj2); |
| 297 | + |
| 298 | + std::string items = "/obj1/root.json\n/obj2/root.json\n"; |
| 299 | + |
| 300 | + THttpCallArg arg; |
| 301 | + arg.SetPostData(std::move(items)); |
| 302 | + sniffer.SetCurrentCallArg(&arg); |
| 303 | + |
| 304 | + std::string res; |
| 305 | + sniffer.Produce("", "multi.json", "number=2", res); |
| 306 | + EXPECT_EQ(res, "[{\n" |
| 307 | + " \"_typename\" : \"TNamed\",\n" |
| 308 | + " \"fUniqueID\" : 0,\n" |
| 309 | + " \"fBits\" : 8,\n" |
| 310 | + " \"fName\" : \"obj1\",\n" |
| 311 | + " \"fTitle\" : \"title1\"\n" |
| 312 | + "}, {\n" |
| 313 | + " \"_typename\" : \"TNamed\",\n" |
| 314 | + " \"fUniqueID\" : 0,\n" |
| 315 | + " \"fBits\" : 8,\n" |
| 316 | + " \"fName\" : \"obj2\",\n" |
| 317 | + " \"fTitle\" : \"title2\"\n" |
| 318 | + "}]") << "return value of multi.json"; |
| 319 | +} |
0 commit comments