-
Notifications
You must be signed in to change notification settings - Fork 17
/
test-win.c
173 lines (131 loc) · 4.07 KB
/
test-win.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* @file test-linux.c
* @brief cfgpath.h test code for the Linux platform.
*
* Copyright (C) 2013 Adam Nielsen <malvineous@shikadi.net>
*
* This code is placed in the public domain. You are free to use it for any
* purpose. If you add new platform support, please contribute a patch!
*/
#include <string.h>
#include <stdio.h>
#define SHGetFolderPath test_SHGetFolderPath
#define mkdir test_mkdir
#undef __linux__
#ifndef WIN32
#define WIN32
#endif
#include "cfgpath.h"
const char *set_appdata;
const char *set_appdata_local;
int set_retval;
/* Fake implementation of SHGetFolderPath() that fails when and how we want */
int test_SHGetFolderPath(void *hwndOwner, int nFolder, void *hToken,
int dwFlags, char *pszPath)
{
/* Hopefully trigger an error if the buffer is too small */
pszPath[MAX_PATH - 1] = 0;
switch (nFolder) {
case CSIDL_APPDATA:
if (set_appdata) {
strcpy(pszPath, set_appdata);
return set_retval;
}
break;
case CSIDL_LOCAL_APPDATA:
if (set_appdata_local) {
strcpy(pszPath, set_appdata_local);
return set_retval;
}
break;
}
return E_FAIL;
}
int test_mkdir(const char *path)
{
return 0;
}
#define TOSTRING_X(x) #x
#define TOSTRING(x) TOSTRING_X(x)
#define RUN_TEST(result, msg) \
TEST_FUNC(buffer, sizeof(buffer), "test-win"); \
CHECK_RESULT(result, msg)
#define CHECK_RESULT(result, msg) \
if (strcmp(buffer, result) != 0) { \
printf("FAIL: %s:%d " TOSTRING(TEST_FUNC) "() " msg " Test failed, " \
"returning the wrong value.\n" \
"Expected: %s\nGot: %s\n", __FILE__, __LINE__, result, buffer); \
return 1; \
} else { \
printf("PASS: " TOSTRING(TEST_FUNC) "() " msg "\n"); \
}
int main(int argc, char *argv[])
{
char buffer[256];
/*
* get_user_config_file()
*/
#define TEST_RESULT "C:\\Users\\test-win\\AppData\\Roaming\\test-win.ini"
#define TEST_FUNC get_user_config_file
set_retval = S_OK;
set_appdata = "C:\\Users\\test-win\\AppData\\Roaming";
TEST_FUNC(buffer, 5, "test-win");
CHECK_RESULT("", "returns empty string when buffer is too small.");
RUN_TEST(TEST_RESULT, "works with CSIDL_APPDATA.");
set_retval = S_FALSE;
RUN_TEST(TEST_RESULT, "works if folder doesn't exist (S_FALSE).");
set_appdata = NULL;
RUN_TEST("", "fails with missing CSIDL_APPDATA.");
#undef TEST_FUNC
#undef TEST_RESULT
/*
* get_user_config_folder()
*/
#define TEST_RESULT "C:\\Users\\test-win\\AppData\\Roaming\\test-win\\"
#define TEST_FUNC get_user_config_folder
set_retval = S_OK;
set_appdata = "C:\\Users\\test-win\\AppData\\Roaming";
TEST_FUNC(buffer, 5, "test-win");
CHECK_RESULT("", "returns empty string when buffer is too small.");
RUN_TEST(TEST_RESULT, "works with CSIDL_APPDATA.");
set_retval = S_FALSE;
RUN_TEST(TEST_RESULT, "works if folder doesn't exist (S_FALSE).");
set_appdata = NULL;
RUN_TEST("", "fails with missing CSIDL_APPDATA.");
#undef TEST_FUNC
#undef TEST_RESULT
/*
* get_user_data_folder()
*/
#define TEST_RESULT "C:\\Users\\test-win\\AppData\\Roaming\\test-win\\"
#define TEST_FUNC get_user_data_folder
set_retval = S_OK;
set_appdata = "C:\\Users\\test-win\\AppData\\Roaming";
TEST_FUNC(buffer, 5, "test-win");
CHECK_RESULT("", "returns empty string when buffer is too small.");
RUN_TEST(TEST_RESULT, "works with CSIDL_APPDATA.");
set_retval = S_FALSE;
RUN_TEST(TEST_RESULT, "works if folder doesn't exist (S_FALSE).");
set_appdata = NULL;
RUN_TEST("", "fails with missing CSIDL_APPDATA.");
#undef TEST_FUNC
#undef TEST_RESULT
/*
* get_user_cache_folder()
*/
#define TEST_RESULT "C:\\Users\\test-win\\AppData\\Local\\test-win\\"
#define TEST_FUNC get_user_cache_folder
set_retval = S_OK;
set_appdata_local = "C:\\Users\\test-win\\AppData\\Local";
TEST_FUNC(buffer, 5, "test-win");
CHECK_RESULT("", "returns empty string when buffer is too small.");
RUN_TEST(TEST_RESULT, "works with CSIDL_LOCAL_APPDATA.");
set_retval = S_FALSE;
RUN_TEST(TEST_RESULT, "works if folder doesn't exist (S_FALSE).");
set_appdata_local = NULL;
RUN_TEST("", "fails with missing CSIDL_LOCAL_APPDATA.");
#undef TEST_FUNC
#undef TEST_RESULT
printf("All tests passed for platform: Windows.\n");
return 0;
}