FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
uap-cpp/UaParserTest.cpp at master · ua-parser/uap-cpp · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
ua-parser
/
uap-cpp
Public
Notifications
You must be signed in to change notification settings
Fork
34
Star
41
Code
Issues
2
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
uap-cpp
/
UaParserTest.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
351 lines (303 loc) · 11.8 KB
Breadcrumbs
uap-cpp
/
UaParserTest.cpp
Copy path
File metadata and controls
351 lines (303 loc) · 11.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#
include
<
gtest/gtest.h
>
#
include
<
yaml-cpp/yaml.h
>
#
include
"
UaParser
"
#
include
"
internal/AlternativeExpander.h
"
#
include
"
internal/Pattern.h
"
#
include
"
internal/ReplaceTemplate.h
"
#
include
"
internal/SnippetIndex.h
"
#
ifdef
WITH_MT_TEST
#
include
<
future
>
#
endif
//
WITH_MT_TEST
namespace
{
const
std::string
UA_CORE_DIR
=
"
./uap-core
"
;
const
uap_cpp::UserAgentParser
g_ua_parser
(
UA_CORE_DIR
+
"
/regexes.yaml
"
);
TEST
(UserAgentParser, basic) {
const
auto
uagent = g_ua_parser.
parse
(
"
Mozilla/5.0 (iPhone; CPU iPhone OS 5_1_1 like Mac OS X)
"
"
AppleWebKit/534.46
"
"
(KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3
"
);
ASSERT_EQ
(
"
Mobile Safari
"
, uagent.
browser
.
family
);
ASSERT_EQ
(
"
5
"
, uagent.
browser
.
major
);
ASSERT_EQ
(
"
1
"
, uagent.
browser
.
minor
);
ASSERT_EQ
(
"
"
, uagent.
browser
.
patch
);
ASSERT_EQ
(
"
Mobile Safari 5.1.0
"
, uagent.
browser
.
toString
());
ASSERT_EQ
(
"
5.1.0
"
, uagent.
browser
.
toVersionString
());
ASSERT_EQ
(
"
iOS
"
, uagent.
os
.
family
);
ASSERT_EQ
(
"
5
"
, uagent.
os
.
major
);
ASSERT_EQ
(
"
1
"
, uagent.
os
.
minor
);
ASSERT_EQ
(
"
1
"
, uagent.
os
.
patch
);
ASSERT_EQ
(
"
iOS 5.1.1
"
, uagent.
os
.
toString
());
ASSERT_EQ
(
"
5.1.1
"
, uagent.
os
.
toVersionString
());
ASSERT_EQ
(
"
Mobile Safari 5.1.0/iOS 5.1.1
"
, uagent.
toFullString
());
ASSERT_EQ
(
"
iPhone
"
, uagent.
device
.
family
);
ASSERT_FALSE
(uagent.
isSpider
());
}
TEST
(UserAgentParser, DeviceTypeMobile) {
EXPECT_TRUE
(
uap_cpp::UserAgentParser::device_type
(
"
Mozilla/5.0 (iPhone; CPU iPhone OS 5_1_1 like Mac OS X)
"
"
AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1
"
"
Mobile/9B206 Safari/7534.48.3
"
) ==
uap_cpp::DeviceType::
kMobile
);
EXPECT_TRUE
(
uap_cpp::UserAgentParser::device_type
(
"
Mozilla/5.0 (Linux; Android 8.0.0; SAMSUNG SM-J330FN Build/R16NW)
"
"
AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/7.2
"
"
Chrome/11.1.1111.111 Mobile Safari/537.36
"
) ==
uap_cpp::DeviceType::
kMobile
);
}
TEST
(UserAgentParser, DeviceTypeTablet) {
EXPECT_TRUE
(
uap_cpp::UserAgentParser::device_type
(
"
Mozilla/5.0 (Linux; U; en-us; KFTT Build/IML74K)
"
"
AppleWebKit/535.19 (KHTML, like Gecko) Silk/2.0
"
"
Safari/535.19 Silk-Accelerated=false
"
) ==
uap_cpp::DeviceType::
kTablet
);
EXPECT_TRUE
(
uap_cpp::UserAgentParser::device_type
(
"
Mozilla/5.0 (Linux; Android 9; SHT-AL09 Build/HUAWEISHT-AL09; wv)
"
"
AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0
"
"
Chrome/79.0.3945.136 Safari/537.36
"
) ==
uap_cpp::DeviceType::
kTablet
);
}
TEST
(UserAgentParser, DeviceTypeDesktop) {
ASSERT_TRUE
(
uap_cpp::UserAgentParser::device_type
(
"
Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.4 (KHTML, like
"
"
Gecko) Chrome/98 Safari/537.4 (StatusCake)
"
) ==
uap_cpp::DeviceType::
kDesktop
);
}
TEST
(UserAgentParser, DeviceTypeMobileOrTablet) {
const
auto
test_cases =
YAML::LoadFile
(
"
./test_device_type_mobile.yaml
"
);
for
(
const
auto
& test : test_cases) {
const
auto
device_type =
uap_cpp::UserAgentParser::device_type
(test.
as
<std::string>());
ASSERT_TRUE
(device_type == uap_cpp::DeviceType::
kMobile
||
device_type == uap_cpp::DeviceType::
kTablet
);
}
}
bool
has_field
(
const
YAML
::Node& root,
const
std::string& fname) {
const
auto
& yaml_field = root[fname];
return
yaml_field.
IsDefined
();
}
std::string
string_field
(
const
YAML
::Node& root,
const
std::string& fname) {
const
auto
& yaml_field = root[fname];
return
yaml_field.
IsNull
() ?
"
"
: yaml_field.
as
<std::string>();
}
void
test_browser_or_os
(
const
std::string file_path,
const
bool
browser) {
auto
root =
YAML::LoadFile
(file_path);
const
auto
& test_cases = root[
"
test_cases
"
];
for
(
const
auto
& test : test_cases) {
const
auto
major =
string_field
(test,
"
major
"
);
const
auto
minor =
string_field
(test,
"
minor
"
);
const
auto
patch =
string_field
(test,
"
patch
"
);
const
bool
has_patch_minor =
has_field
(test,
"
patch_minor
"
);
const
auto
patch_minor =
has_patch_minor ?
string_field
(test,
"
patch_minor
"
) :
"
"
;
const
auto
family =
string_field
(test,
"
family
"
);
const
auto
unparsed =
string_field
(test,
"
user_agent_string
"
);
const
auto
uagent = g_ua_parser.
parse
(unparsed);
const
auto
& agent = browser ? uagent.
browser
: uagent.
os
;
EXPECT_EQ
(major, agent.
major
);
EXPECT_EQ
(minor, agent.
minor
);
EXPECT_EQ
(patch, agent.
patch
);
if
(has_patch_minor && !browser) {
EXPECT_EQ
(patch_minor, agent.
patch_minor
);
}
EXPECT_EQ
(family, agent.
family
);
}
}
void
test_device
(
const
std::string file_path) {
auto
root =
YAML::LoadFile
(file_path);
const
auto
& test_cases = root[
"
test_cases
"
];
for
(
const
auto
& test : test_cases) {
const
auto
unparsed =
string_field
(test,
"
user_agent_string
"
);
const
auto
uagent = g_ua_parser.
parse
(unparsed);
const
auto
family =
string_field
(test,
"
family
"
);
const
auto
brand =
string_field
(test,
"
brand
"
);
const
auto
model =
string_field
(test,
"
model
"
);
EXPECT_EQ
(family, uagent.
device
.
family
);
EXPECT_EQ
(brand, uagent.
device
.
brand
);
EXPECT_EQ
(model, uagent.
device
.
model
);
}
}
TEST
(OsVersion, test_os) {
test_browser_or_os
(
UA_CORE_DIR
+
"
/tests/test_os.yaml
"
,
false
);
}
TEST
(OsVersion, test_ua) {
test_browser_or_os
(
UA_CORE_DIR
+
"
/tests/test_ua.yaml
"
,
true
);
}
TEST
(BrowserVersion, firefox_user_agent_strings) {
test_browser_or_os
(
UA_CORE_DIR
+
"
/test_resources/firefox_user_agent_strings.yaml
"
,
true
);
}
TEST
(BrowserVersion, opera_mini_user_agent_strings) {
test_browser_or_os
(
UA_CORE_DIR
+
"
/test_resources/opera_mini_user_agent_strings.yaml
"
,
true
);
}
TEST
(BrowserVersion, pgts_browser_list) {
test_browser_or_os
(
UA_CORE_DIR
+
"
/test_resources/pgts_browser_list.yaml
"
,
true
);
}
TEST
(OsVersion, additional_os_tests) {
test_browser_or_os
(
UA_CORE_DIR
+
"
/test_resources/additional_os_tests.yaml
"
,
false
);
}
TEST
(BrowserVersion, podcasting_user_agent_strings) {
test_browser_or_os
(
UA_CORE_DIR
+
"
/test_resources/podcasting_user_agent_strings.yaml
"
,
true
);
}
TEST
(DeviceFamily, test_device) {
test_device
(
UA_CORE_DIR
+
"
/tests/test_device.yaml
"
);
}
#
ifdef
WITH_MT_TEST
namespace
{
void
do_multithreaded_test
(
const
std::function<
void
()>& work) {
static
constexpr
int
NUM_WORKERS
=
4
;
std::vector<std::future<
void
>> workers;
for
(
int
i =
0
; i <
NUM_WORKERS
; ++i) {
workers.
push_back
(
std::async
(std::launch::async, [&work]() {
work
(); }));
}
for
(
auto
& worker : workers) {
worker.
wait
();
}
}
}
//
namespace
TEST
(OsVersion, test_os_mt) {
do_multithreaded_test
(
[] {
test_browser_or_os
(
UA_CORE_DIR
+
"
/tests/test_os.yaml
"
,
false
); });
}
TEST
(OsVersion, test_ua_mt) {
do_multithreaded_test
(
[] {
test_browser_or_os
(
UA_CORE_DIR
+
"
/tests/test_ua.yaml
"
,
true
); });
}
TEST
(BrowserVersion, firefox_user_agent_strings_mt) {
do_multithreaded_test
([] {
test_browser_or_os
(
UA_CORE_DIR
+
"
/test_resources/firefox_user_agent_strings.yaml
"
,
true
);
});
}
TEST
(BrowserVersion, opera_mini_user_agent_strings_mt) {
do_multithreaded_test
([] {
test_browser_or_os
(
UA_CORE_DIR
+
"
/test_resources/opera_mini_user_agent_strings.yaml
"
,
true
);
});
}
TEST
(BrowserVersion, pgts_browser_list_mt) {
do_multithreaded_test
([] {
test_browser_or_os
(
UA_CORE_DIR
+
"
/test_resources/pgts_browser_list.yaml
"
,
true
);
});
}
TEST
(OsVersion, additional_os_tests_mt) {
do_multithreaded_test
([] {
test_browser_or_os
(
UA_CORE_DIR
+
"
/test_resources/additional_os_tests.yaml
"
,
false
);
});
}
TEST
(BrowserVersion, podcasting_user_agent_strings_mt) {
do_multithreaded_test
([] {
test_browser_or_os
(
UA_CORE_DIR
+
"
/test_resources/podcasting_user_agent_strings.yaml
"
,
true
);
});
}
TEST
(DeviceFamily, test_device_mt) {
do_multithreaded_test
(
[] {
test_device
(
UA_CORE_DIR
+
"
/tests/test_device.yaml
"
); });
}
#
endif
//
WITH_MT_TEST
void
test_snippets
(
const
std::string& expression,
std::vector<std::string> should_match) {
uap_cpp::SnippetIndex index;
auto
snippet_set = index.
registerSnippets
(expression);
auto
snippet_string_map = index.
getRegisteredSnippets
();
std::vector<std::string> snippet_strings;
for
(
auto
snippetId : snippet_set) {
auto
it = snippet_string_map.
find
(snippetId);
ASSERT_NE
(it, snippet_string_map.
end
());
snippet_strings.
emplace_back
(it->
second
);
}
EXPECT_EQ
(snippet_strings, should_match);
}
TEST
(SnippetIndex, snippets) {
test_snippets
(
"
foo.bar
"
, {
"
foo
"
,
"
bar
"
});
test_snippets
(
"
foodbar
"
, {
"
foodbar
"
});
test_snippets
(
"
food?bar
"
, {
"
foo
"
,
"
bar
"
});
test_snippets
(
"
foo.?bar
"
, {
"
foo
"
,
"
bar
"
});
test_snippets
(
"
foo
\\
.bar
"
, {
"
foo
"
,
"
.bar
"
});
test_snippets
(
"
(foo)
"
, {
"
foo
"
});
test_snippets
(
"
toto(foo|bar)tata
"
, {
"
toto
"
,
"
tata
"
});
test_snippets
(
"
toto(foo)tata
"
, {
"
toto
"
,
"
foo
"
,
"
tata
"
});
test_snippets
(
"
toto(foo)?tata
"
, {
"
toto
"
,
"
tata
"
});
test_snippets
(
"
toto(foo)*tata
"
, {
"
toto
"
,
"
tata
"
});
test_snippets
(
"
toto(foo){0,10}tata
"
, {
"
toto
"
,
"
tata
"
});
test_snippets
(
"
toto(foo){0,}tata
"
, {
"
toto
"
,
"
tata
"
});
test_snippets
(
"
toto(foo){1,10}tata
"
, {
"
toto
"
,
"
foo
"
,
"
tata
"
});
test_snippets
(
"
toto(foo){1,}tata
"
, {
"
toto
"
,
"
foo
"
,
"
tata
"
});
test_snippets
(
"
foo[abc]bar
"
, {
"
foo
"
,
"
bar
"
});
test_snippets
(
"
foo[abc]+bar
"
, {
"
foo
"
,
"
bar
"
});
test_snippets
(
"
foo|bar
"
, {});
test_snippets
(
"
foo[|]bar
"
, {
"
foo
"
,
"
bar
"
});
test_snippets
(
"
[(foo)]bar
"
, {
"
bar
"
});
test_snippets
(
"
[]foo]bar
"
, {
"
bar
"
});
test_snippets
(
"
(foo[abc)])bar
"
, {
"
foo
"
,
"
bar
"
});
test_snippets
(
"
(foo(abc))bar
"
, {
"
foo
"
,
"
abc
"
,
"
bar
"
});
test_snippets
(
"
Foo.bAR
"
, {
"
foo
"
,
"
bar
"
});
test_snippets
(
"
/(
\\
d+)
\\
.?foo(
\\
d+)
"
, {
"
foo
"
});
test_snippets
(
"
(?:foo|bar);.*(baz)/(
\\
d+)
\\
.(
\\
d+)
"
, {
"
baz
"
});
}
void
test_expand
(
const
std::string& expression,
std::vector<std::string> should_match) {
EXPECT_EQ
(
uap_cpp::AlternativeExpander::expand
(expression), should_match);
}
TEST
(AlternativeExpander, expansions) {
test_expand
(
"
a
"
, {
"
a
"
});
test_expand
(
"
(a)
"
, {
"
(a)
"
});
test_expand
(
"
(a|b)
"
, {
"
(a)
"
,
"
(b)
"
});
test_expand
(
"
(?:a|b)
"
, {
"
(?:a)
"
,
"
(?:b)
"
});
test_expand
(
"
(a|b)?
"
, {
"
(a|b)?
"
});
test_expand
(
"
(a|b)+
"
, {
"
(a)+
"
,
"
(b)+
"
});
test_expand
(
"
(a|b)*
"
, {
"
(a|b)*
"
});
test_expand
(
"
(a|b){0,2}
"
, {
"
(a|b){0,2}
"
});
test_expand
(
"
a|b
"
, {
"
a
"
,
"
b
"
});
test_expand
(
"
x(a|b)y
"
, {
"
x(a)y
"
,
"
x(b)y
"
});
test_expand
(
"
(a|b)yz
"
, {
"
(a)yz
"
,
"
(b)yz
"
});
test_expand
(
"
xa|by
"
, {
"
xa
"
,
"
by
"
});
test_expand
(
"
(a|b)(c|d)
"
, {
"
(a)(c)
"
,
"
(a)(d)
"
,
"
(b)(c)
"
,
"
(b)(d)
"
});
test_expand
(
"
(a(b|c)|d)
"
, {
"
(a(b))
"
,
"
(a(c))
"
,
"
(d)
"
});
test_expand
(
"
(a
"
, {
"
(a
"
});
test_expand
(
"
(a|b
"
, {
"
(a|b
"
});
test_expand
(
"
((a
"
, {
"
((a
"
});
test_expand
(
"
((a|b
"
, {
"
((a|b
"
});
test_expand
(
"
(a((a|b
"
, {
"
(a((a|b
"
});
test_expand
(
"
[ab]
"
, {
"
[ab]
"
});
test_expand
(
"
[(|)]
"
, {
"
[(|)]
"
});
test_expand
(
"
[(a|a)]
"
, {
"
[(a|a)]
"
});
test_expand
(
"
a(|)b
"
, {
"
a()b
"
,
"
a()b
"
});
test_expand
(
"
([ab]|[bc])
"
, {
"
([ab])
"
,
"
([bc])
"
});
test_expand
(
"
[[](a|b)
"
, {
"
[[](a)
"
,
"
[[](b)
"
});
test_expand
(
"
[]abc](a|b)
"
, {
"
[]abc](a)
"
,
"
[]abc](b)
"
});
}
std::string
match_and_expand
(
const
std::string& expression,
const
std::string& input_string,
const
std::string& replace_template) {
uap_cpp::Pattern
p
(expression);
uap_cpp::Match m;
if
(p.
match
(input_string, m)) {
return
uap_cpp::ReplaceTemplate
(replace_template).
expand
(m);
}
return
"
"
;
}
TEST
(ReplaceTemplate, expansions) {
EXPECT_EQ
(
match_and_expand
(
"
something
"
,
"
other
"
,
"
foo
"
),
"
"
);
EXPECT_EQ
(
match_and_expand
(
"
something
"
,
"
something
"
,
"
foo
"
),
"
foo
"
);
EXPECT_EQ
(
match_and_expand
(
"
some(thing)
"
,
"
something
"
,
"
no$1
"
),
"
nothing
"
);
EXPECT_EQ
(
match_and_expand
(
"
so([Mm])eth(ing)
"
,
"
that's something!
"
,
"
$1$2$3
"
),
"
ming
"
);
EXPECT_EQ
(
match_and_expand
(
"
([^ ]+) (.+)
"
,
"
a b
"
,
"
$2-$1
"
),
"
b-a
"
);
}
}
//
namespace
int
main
(
int
argc,
char
** argv) {
testing::InitGoogleTest
(&argc, argv);
return
RUN_ALL_TESTS
();
}
Back
|
FazBrowse Home
|
New Git URL