MQTTSuite
Loading...
Searching...
No Matches
string-format-check-test.cpp
Go to the documentation of this file.
1#include <iostream>
2
3#include <nlohmann/json-schema.hpp>
4
5/** @return number of failed tests */
6size_t
7testStringFormat(const std::string &format,
8 const std::vector<std::pair<std::string, bool>> &stringValidTests)
9{
10 size_t numberOfErrors = 0;
11
12 for (auto stringValid = stringValidTests.begin(); stringValid != stringValidTests.end(); ++stringValid) {
13 std::cout << "[INFO] Testing " << format << ": " << stringValid->first << "\n";
14
15 try {
17
18 if (!stringValid->second) {
19 ++numberOfErrors;
20 std::cerr << "[ERROR] String with " << format << " format '" << stringValid->first
21 << "' validated even though it should NOT!\n";
22 }
23 } catch (std::exception &exception) {
24 std::cout << "[INFO] Validation failed with: " << exception.what() << "\n";
25 if (stringValid->second) {
26 ++numberOfErrors;
27 std::cerr << "[ERROR] String with " << format << " format '" << stringValid->first
28 << "' did NOT validate even though it should!\n";
29 }
30 }
31 }
32
33 return numberOfErrors;
34}
35
36int main()
37{
38 size_t numberOfErrors = 0;
39
40 const std::vector<std::pair<std::string, bool>> dateTimeChecks{
41 {"1985-04-12T23:20:50.52Z", true},
42 {"1996-12-19T16:39:57-08:00", true},
43 {"1990-12-31T23:59:60Z", true},
44 {"1990-12-31T15:59:60-08:00", true},
45 {"1937-01-01T12:00:27.87+00:20", true},
46 {"1985-4-12T23:20:50.52Z", false},
47 {"1985-04-12T23:20:50.52", false},
48 {"1985-04-12T24:00:00", false},
49 {"", false},
50 {"2019-04-30T11:11:11+01:00", true},
51 {"2019-04-31T11:11:11+01:00", false},
52 {"2019-02-28T11:11:11+01:00", true},
53 {"2019-02-29T11:11:11+01:00", false},
54 {"2020-02-29T11:11:11+01:00", true},
55 {"2020-02-30T11:11:11+01:00", false},
56 {"2020-02-29T23:59:59+01:00", true},
57 {"2020-02-29T23:59:60+01:00", false},
58 {"2020-02-29T23:59:60+00:00", true},
59 {"2020-02-29T23:60:59+01:00", false},
60 {"2019-09-30T11:11:11+01:00", true},
61 {"2019-09-31T11:11:11+01:00", false},
62 {"2019-09-30T11:11:11+23:59", true},
63 {"2019-09-30T11:11:11+24:00", false}};
64
65 numberOfErrors += testStringFormat("date-time", dateTimeChecks);
66
67 const std::vector<std::pair<std::string, bool>> ipv4Checks{
68 {"", false},
69 {"x99.99.99.99", false},
70 {"99.99.99.99x", false},
71 {"192.168.0.1", true},
72 {"127.0.0", false},
73 {"127.0.0.1", true},
74 {"127.0.0.0.1", false},
75 {"255.255.255.255", true},
76 {"255.255.255.256", false},
77 {"255.255.256.255", false},
78 {"255.256.255.255", false},
79 {"256.255.255.255", false},
80 {"256.256.256.256", false},
81 {"0x7f000001", false}};
82
83 numberOfErrors += testStringFormat("ipv4", ipv4Checks);
84
85 const std::vector<std::pair<std::string, bool>> uriChecks{
86 {"http://www.google.com/search?q=regular%20expression", true},
87 {"http://www.google.com/", true},
88 {"http://www.google.com/search?q=regular%20expression", true},
89 {"www.google.com", false},
90 {"http://www.google.comj", true},
91 {"ldap://[2001:db8::7]/c=GB?objectClass?one", true},
92 {"mailto:John.Doe@example.com", true},
93 {"news:comp.infosystems.www.servers.unix", true},
94 {"https://john.doe@www.example.com:123/forum/questions/?tag=networking&order=newest#top", true},
95 {"tel:+1-816-555-1212", true},
96 {"telnet://192.0.2.16:80/", true},
97 {"urn:oasis:names:specification:docbook:dtd:xml:4.1.2", true}};
98
99 numberOfErrors += testStringFormat("uri", uriChecks);
100
101 return numberOfErrors;
102}
int main()
Definition format.cpp:34
void default_string_format_check(const std::string &format, const std::string &value)
size_t testStringFormat(const std::string &format, const std::vector< std::pair< std::string, bool > > &stringValidTests)