This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathtest_system-library.cpp
More file actions
243 lines (197 loc) · 7.27 KB
/
test_system-library.cpp
File metadata and controls
243 lines (197 loc) · 7.27 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* Copyright (C) 2017 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include "gtest/gtest.h"
#include "foundation-system.h"
static const char *kNonExistantLibrary = "libihopethisdoesntexist.badextension";
#if defined(__MAC__)
static const char *kExistingStaticLibrary = nullptr;
static const char *kExistingStaticLibrarySymbol = nullptr;
static const char *kExistingDlHandleLibrary = "libz.dylib";
static const char *kExistingDlHandleLibrarySymbol = "inflate";
static const char *kExistingCFBundleRefLibrary = "CoreGraphics.framework";
static const char *kExistingCFBundleRefLibrarySymbol = "CGBitmapContextCreate";
static const char *kExistingHMODULELibrary = nullptr;
static const char *kExistingHMODULELibrarySymbol = nullptr;
#elif defined(__WINDOWS__)
static const char *kExistingStaticLibrary = nullptr;
static const char *kExistingStaticLibrarySymbol = nullptr;
static const char *kExistingDlHandleLibrary = nullptr;
static const char *kExistingDlHandleLibrarySymbol = nullptr;
static const char *kExistingCFBundleRefLibrary = nullptr;
static const char *kExistingCFBundleRefLibrarySymbol = nullptr;
static const char *kExistingHMODULELibrary = "user32.dll";
static const char *kExistingHMODULELibrarySymbol = "CreateWindowExW";
#elif defined(__LINUX__)
static const char *kExistingStaticLibrary = nullptr;
static const char *kExistingStaticLibrarySymbol = nullptr;
static const char *kExistingDlHandleLibrary = "libz.so";
static const char *kExistingDlHandleLibrarySymbol = "inflate";
static const char *kExistingCFBundleRefLibrary = nullptr;
static const char *kExistingCFBundleRefLibrarySymbol = nullptr;
static const char *kExistingHMODULELibrary = nullptr;
static const char *kExistingHMODULELibrarySymbol = nullptr;
#endif
static bool
test_createwithpath(const char *p_lib)
{
if (p_lib == nullptr)
return true;
MCSAutoLibraryRef t_library;
return MCSLibraryCreateWithPath(MCSTR(p_lib),
&t_library);
}
static void
test_copypath_roundtrips(const char *p_lib)
{
if (p_lib == nullptr)
return;
MCSAutoLibraryRef t_library;
ASSERT_TRUE(
MCSLibraryCreateWithPath(MCSTR(p_lib),
&t_library)
);
MCAutoStringRef t_path;
ASSERT_TRUE(
MCSLibraryCopyPath(*t_library,
&t_path));
MCSAutoLibraryRef t_other_library;
ASSERT_TRUE(
MCSLibraryCreateWithPath(*t_path,
&t_other_library));
ASSERT_TRUE(MCValueIsEqualTo(*t_library,
*t_other_library));
}
static void
test_lookup_existing_symbol(const char *p_lib, const char *p_symbol)
{
if (p_lib == nullptr)
return;
MCSAutoLibraryRef t_library;
ASSERT_TRUE(
MCSLibraryCreateWithPath(MCSTR(p_lib),
&t_library)
);
void *t_symbol =
MCSLibraryLookupSymbol(*t_library,
MCSTR(p_symbol));
ASSERT_NE(t_symbol,
nullptr);
}
static void
test_createwithaddress_roundtrips(const char *p_lib,
const char *p_symbol)
{
if (p_lib == nullptr)
return;
MCSAutoLibraryRef t_library;
ASSERT_TRUE(
MCSLibraryCreateWithPath(MCSTR(p_lib),
&t_library)
);
void *t_symbol =
MCSLibraryLookupSymbol(*t_library,
MCSTR(p_symbol));
ASSERT_NE(t_symbol,
nullptr);
MCSAutoLibraryRef t_other_library;
ASSERT_TRUE(
MCSLibraryCreateWithAddress(t_symbol,
&t_other_library));
ASSERT_TRUE(MCValueIsEqualTo(*t_library,
*t_other_library));
}
//// Non existant path tests
//
// These check that creating a library from a non-existing path throws the
// correct error.
TEST(system_library, createwithpath_non_existing_path)
{
ASSERT_FALSE(test_createwithpath(kNonExistantLibrary));
// TODO: Test error
}
//// Existing paths tests
//
// These check that libraries create from paths correctly.
TEST(system_library, createwithpath_existing_static)
{
ASSERT_TRUE(test_createwithpath(kExistingStaticLibrary));
}
TEST(system_library, createwithpath_existing_dlhandle)
{
ASSERT_TRUE(test_createwithpath(kExistingDlHandleLibrary));
}
TEST(system_library, createwithpath_existing_cfbundleref)
{
ASSERT_TRUE(test_createwithpath(kExistingCFBundleRefLibrary));
}
TEST(system_library, createwithpath_existing_hmodule)
{
ASSERT_TRUE(test_createwithpath(kExistingHMODULELibrary));
}
//// CopyPath roundtripping tests
//
// These check that creating a library from a path, then creating another
// library from the path copied from the original results in the same library.
TEST(system_library, copypath_static_roundtrips)
{
test_copypath_roundtrips(kExistingStaticLibrary);
}
TEST(system_library, copypath_dlhandle_roundtrips)
{
test_copypath_roundtrips(kExistingDlHandleLibrary);
}
TEST(system_library, copypath_cfbundleref_roundtrips)
{
test_copypath_roundtrips(kExistingCFBundleRefLibrary);
}
TEST(system_library, copypath_hmodule_roundtrips)
{
test_copypath_roundtrips(kExistingHMODULELibrary);
}
//// LookupSymbol existing symbol tests
//
// These check that looking up a known symbol in a library returns non-null.
TEST(system_library, lookupsymbol_existing_static)
{
test_lookup_existing_symbol(kExistingStaticLibrary,
kExistingStaticLibrarySymbol);
}
TEST(system_library, lookupsymbol_existing_dlhandle)
{
test_lookup_existing_symbol(kExistingDlHandleLibrary,
kExistingDlHandleLibrarySymbol);
}
TEST(system_library, lookupsymbol_existing_cfbundleref)
{
test_lookup_existing_symbol(kExistingCFBundleRefLibrary,
kExistingCFBundleRefLibrarySymbol);
}
TEST(system_library, lookupsymbol_existing_hmodule)
{
test_lookup_existing_symbol(kExistingHMODULELibrary,
kExistingHMODULELibrarySymbol);
}
//// CreateWithAddress roundtrips
//
// These check that creating a library from a path, resolving a symbol and then
// creating a library from the symbol address ends up with the same library
// handle.
TEST(system_library, createwithaddress_dlhandle_roundtrips)
{
test_createwithaddress_roundtrips(kExistingDlHandleLibrary,
kExistingDlHandleLibrarySymbol);
}
TEST(system_library, createwithaddress_hmodule_roundtrips)
{
test_createwithaddress_roundtrips(kExistingHMODULELibrary,
kExistingHMODULELibrarySymbol);
}