1 #include "internal/include/fb_generated.h"
2 #include "internal/naive_policy_checker.hpp"
3 #include "internal/policy.hpp"
4 #include "internal/serializer.hpp"
5 #include "internal/storage_backend_serialized.hpp"
6 #include "internal/storage_backend_serialized.hpp"
7 #include "internal/tslog.hpp"
8 #include "libdbuspolicy1-private.h"
9 #include <dbuspolicy1/libdbuspolicy1.h>
13 #include <sys/types.h>
16 using namespace ldp_xml_parser;
17 using namespace ldp_serialized;
27 bool measure(std::function<bool()> func, size_t count, const char *desc) {
29 clock_t begin = clock();
30 for (size_t i = 0; i < count; i++)
32 clock_t end = clock();
34 std::cout << desc << ": " << static_cast<double>(end - begin)/CLOCKS_PER_SEC << std::endl;
38 bool run_xml(const char *conf_file) {
39 return policy_checker_system().initDb(conf_file);
42 bool run_xml_plus_fb(const char *conf_file, bool verify) {
43 Serializer serializer;
45 const uint8_t *buff = serializer.serialize(conf_file, size);
48 auto verifier = flatbuffers::Verifier(buff, size);
49 if (!FB::VerifyFileBuffer(verifier)) {
50 std::cout << "verification of serialized data: failed" << std::endl;
55 StorageBackendSerialized storage;
56 return storage.initFromData(buff);
59 bool run_fb(const char *conf_file, bool verify) {
60 StorageBackendSerialized sbs;
61 return sbs.init(conf_file, verify);
64 void run_tests(const char *conf_file, const char *conf_bin, size_t c, Choice ch, bool verify) {
65 if (ch == Choice::ALL || ch == Choice::XML) {
66 if (!measure([&conf_file, c]() { return run_xml(conf_file); }, c, "XML")) {
67 std::cout << "ERROR" << std::endl;
70 if (ch == Choice::ALL || ch == Choice::FB) {
71 if (!measure([&conf_bin, c, verify]() { return run_fb(conf_bin, verify); }, c, "FB")) {
72 std::cout << "ERROR" << std::endl;
75 if (ch == Choice::ALL || ch == Choice::XMLplusFB)
76 if (!measure([&conf_file, c, verify]() { return run_xml_plus_fb(conf_file, verify); }, c, "FB after XML")) {
77 std::cout << "ERROR" << std::endl;
81 void print_help(const char *name) {
82 std::cout << std::endl;
83 std::cout << "usage: " << name << " {-f <config_bin>|-x|-d|-a <config_bin>} {--system|--session|-c <config_xml>} <count>" << std::endl;
84 std::cout << std::endl;
85 std::cout << " -f <config_bin> - Flatbuffers" << std::endl;
86 std::cout << " -x - XML" << std::endl;
87 std::cout << " -d - FB after XML" << std::endl;
88 std::cout << " -a <config_bin> - All tests" << std::endl;
89 std::cout << " -v - Verify" << std::endl;
90 std::cout << std::endl;
93 static const struct option options[] {
94 {"system", no_argument, 0, 0},
95 {"session", no_argument, 0, 0}
98 int main(int argc, char *argv[])
101 std::string input_filename = system_bus_conf_file_primary();
102 std::string binary_file = "";
104 Choice choice = Choice::NONE;
109 c = getopt_long(argc, argv, "f:xda:c:v", options, &option_index);
114 if (option_index == 1)
115 input_filename = session_bus_conf_file_primary();
118 if (choice != Choice::NONE) {
122 choice = Choice::ALL;
123 binary_file = optarg;
126 if (choice != Choice::NONE) {
131 binary_file = optarg;
134 if (choice != Choice::NONE) {
138 choice = Choice::XML;
141 if (choice != Choice::NONE) {
145 choice = Choice::XMLplusFB;
148 input_filename = optarg;
157 count = std::stoi(argv[optind]);
164 run_tests(input_filename.c_str(), binary_file.c_str(), count, choice, verify);