FreeRDP
Loading...
Searching...
No Matches
TestSDLPrefs.cpp
1#include "../sdl_prefs.hpp"
2
3#include <iostream>
4#include <fstream>
5
6#include <winpr/config.h>
7#include <winpr/winpr.h>
8
9#include "../filesystem.hpp"
10
11static std::shared_ptr<SdlPref> instance()
12{
13#if !defined(TEST_SRC_AREA)
14#error "Please define TEST_SRC_AREA to the source directory of this test case"
15#endif
16 fs::path src(TEST_SRC_AREA);
17 src /= "sdl-freerdp.json";
18 if (!fs::exists(src))
19 {
20 std::cerr << "[ERROR] test configuration file '" << src << "' does not exist" << std::endl;
21 return nullptr;
22 }
23
24 return SdlPref::instance(src.string());
25}
26
27#if defined(WITH_WINPR_JSON)
28static std::string test_file(const char* name)
29{
30 fs::path src(TEST_SRC_AREA);
31 src /= name;
32 return src.string();
33}
34#endif
35
36/* user and system configuration together: the user file wins, the system file fills in, and
37 * the system file alone decides the locks. */
38static bool test_system_config()
39{
40#if defined(WITH_WINPR_JSON)
41 auto prefs =
42 SdlPref::instance(test_file("sdl-freerdp-user.json"), test_file("sdl-freerdp-system.json"));
43 if (prefs->get_string("string_key", "x") != "user")
44 return false;
45 if (prefs->get_int("int_key", 0) != 1)
46 return false;
47 if (prefs->get_bool("bool_key", true))
48 return false;
49 if (!prefs->get_bool("system_bool_key", false))
50 return false;
51 if (prefs->get_string("string_key", "x", true) != "system")
52 return false;
53 /* only the system file may set this, the user file sets it to false */
54 if (!prefs->is_commandline_override_allowed())
55 return false;
56
57 prefs = SdlPref::instance(test_file("sdl-freerdp-user.json"),
58 test_file("sdl-freerdp-system-locked.json"));
59 if (prefs->get_string("string_key", "x") != "system")
60 return false;
61 if (prefs->get_int("int_key", 0) != 0)
62 return false;
63 if (prefs->is_commandline_override_allowed())
64 return false;
65#endif
66 return true;
67}
68
69int TestSDLPrefs(int argc, char* argv[])
70{
71 WINPR_UNUSED(argc);
72 WINPR_UNUSED(argv);
73
74#if defined(WITH_WINPR_JSON)
75 printf("implementation: json\n");
76#else
77 printf("implementation: fallback\n");
78#endif
79
80#if defined(WITH_WINPR_JSON)
81 printf("config: %s\n", instance()->get_pref_file().c_str());
82#endif
83
84 auto string_value = instance()->get_string("string_key", "cba");
85#if defined(WITH_WINPR_JSON)
86 if (string_value != "abc")
87 return -1;
88#else
89 if (string_value != "cba")
90 return -1;
91#endif
92
93 auto string_value_nonexistent = instance()->get_string("string_key_nonexistent", "cba");
94 if (string_value_nonexistent != "cba")
95 return -1;
96
97 auto int_value = instance()->get_int("int_key", 321);
98#if defined(WITH_WINPR_JSON)
99 if (int_value != 123)
100 return -1;
101#else
102 if (int_value != 321)
103 return -1;
104#endif
105
106 auto int_value_nonexistent = instance()->get_int("int_key_nonexistent", 321);
107 if (int_value_nonexistent != 321)
108 return -1;
109
110 auto bool_value = instance()->get_bool("bool_key", false);
111#if defined(WITH_WINPR_JSON)
112 if (!bool_value)
113 return -1;
114#else
115 if (bool_value)
116 return -1;
117#endif
118
119 auto bool_value_nonexistent = instance()->get_bool("bool_key_nonexistent", false);
120 if (bool_value_nonexistent)
121 return -1;
122
123 auto array_value = instance()->get_array("array_key", { "c", "b", "a" });
124 if (array_value.size() != 3)
125 return -1;
126#if defined(WITH_WINPR_JSON)
127 if (array_value.at(0) != "a")
128 return -1;
129 if (array_value.at(1) != "b")
130 return -1;
131 if (array_value.at(2) != "c")
132 return -1;
133#else
134 if (array_value.at(0) != "c")
135 return -1;
136 if (array_value.at(1) != "b")
137 return -1;
138 if (array_value.at(2) != "a")
139 return -1;
140#endif
141
142 auto array_value_nonexistent =
143 instance()->get_array("array_key_nonexistent", { "c", "b", "a" });
144 if (array_value_nonexistent.size() != 3)
145 return -1;
146
147 if (array_value_nonexistent.at(0) != "c")
148 return -1;
149 if (array_value_nonexistent.at(1) != "b")
150 return -1;
151 if (array_value_nonexistent.at(2) != "a")
152 return -1;
153
154 if (!test_system_config())
155 return -1;
156
157 return 0;
158}