FreeRDP
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 #if __has_include(<filesystem>)
10 #include <filesystem>
11 namespace fs = std::filesystem;
12 #elif __has_include(<experimental/filesystem>)
13 #include <experimental/filesystem>
14 namespace fs = std::experimental::filesystem;
15 #else
16 #error Could not find system header "<filesystem>" or "<experimental/filesystem>"
17 #endif
18 
19 static std::shared_ptr<SdlPref> instance()
20 {
21 #if !defined(TEST_SRC_AREA)
22 #error "Please define TEST_SRC_AREA to the source directory of this test case"
23 #endif
24  fs::path src(TEST_SRC_AREA);
25  src /= "sdl-freerdp.json";
26  if (!fs::exists(src))
27  {
28  std::cerr << "[ERROR] test configuration file '" << src << "' does not exist" << std::endl;
29  return nullptr;
30  }
31 
32  return SdlPref::instance(src.string());
33 }
34 
35 int TestSDLPrefs(int argc, char* argv[])
36 {
37  WINPR_UNUSED(argc);
38  WINPR_UNUSED(argv);
39 
40 #if defined(WITH_WINPR_JSON)
41  printf("implementation: json\n");
42 #else
43  printf("implementation: fallback\n");
44 #endif
45 
46 #if defined(WITH_WINPR_JSON)
47  printf("config: %s\n", instance()->get_pref_file().c_str());
48 #endif
49 
50  auto string_value = instance()->get_string("string_key", "cba");
51 #if defined(WITH_WINPR_JSON)
52  if (string_value != "abc")
53  return -1;
54 #else
55  if (string_value != "cba")
56  return -1;
57 #endif
58 
59  auto string_value_nonexistent = instance()->get_string("string_key_nonexistent", "cba");
60  if (string_value_nonexistent != "cba")
61  return -1;
62 
63  auto int_value = instance()->get_int("int_key", 321);
64 #if defined(WITH_WINPR_JSON)
65  if (int_value != 123)
66  return -1;
67 #else
68  if (int_value != 321)
69  return -1;
70 #endif
71 
72  auto int_value_nonexistent = instance()->get_int("int_key_nonexistent", 321);
73  if (int_value_nonexistent != 321)
74  return -1;
75 
76  auto bool_value = instance()->get_bool("bool_key", false);
77 #if defined(WITH_WINPR_JSON)
78  if (!bool_value)
79  return -1;
80 #else
81  if (bool_value)
82  return -1;
83 #endif
84 
85  auto bool_value_nonexistent = instance()->get_bool("bool_key_nonexistent", false);
86  if (bool_value_nonexistent)
87  return -1;
88 
89  auto array_value = instance()->get_array("array_key", { "c", "b", "a" });
90  if (array_value.size() != 3)
91  return -1;
92 #if defined(WITH_WINPR_JSON)
93  if (array_value[0] != "a")
94  return -1;
95  if (array_value[1] != "b")
96  return -1;
97  if (array_value[2] != "c")
98  return -1;
99 #else
100  if (array_value[0] != "c")
101  return -1;
102  if (array_value[1] != "b")
103  return -1;
104  if (array_value[2] != "a")
105  return -1;
106 #endif
107 
108  auto array_value_nonexistent =
109  instance()->get_array("array_key_nonexistent", { "c", "b", "a" });
110  if (array_value_nonexistent.size() != 3)
111  return -1;
112 
113  if (array_value_nonexistent[0] != "c")
114  return -1;
115  if (array_value_nonexistent[1] != "b")
116  return -1;
117  if (array_value_nonexistent[2] != "a")
118  return -1;
119 
120  return 0;
121 }