• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 22034777194

15 Feb 2026 11:21AM UTC coverage: 89.999% (-0.001%) from 90.0%
22034777194

Pull #5338

github

web-flow
Merge 8493f6ee7 into 2993205af
Pull Request #5338: Rename and cleanup tests.h comparators for strings

102267 of 113631 relevant lines covered (90.0%)

11490211.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.77
/src/tests/test_uri.cpp
1
/*
2
* (C) 2019 Nuno Goncalves <nunojpg@gmail.com>
3
*     2023,2024 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include "tests.h"
9

10
#include <botan/internal/target_info.h>
11

12
#if defined(BOTAN_HAS_SOCKETS) && (defined(BOTAN_TARGET_OS_HAS_SOCKETS) || defined(BOTAN_TARGET_OS_HAS_WINSOCK2))
13

14
   #include <botan/exceptn.h>
15
   #include <botan/internal/uri.h>
16

17
namespace Botan_Tests {
18

19
class URI_Tests final : public Test {
1✔
20
   private:
21
      static Test::Result test_uri_ctor() {
1✔
22
         Test::Result result("URI constructors");
1✔
23
         const Botan::URI uri(Botan::URI::Type::Domain, "localhost", 9000);
1✔
24
         result.test_is_true("type", uri.type() == Botan::URI::Type::Domain);
1✔
25
         result.test_str_eq("host", uri.host(), "localhost");
1✔
26
         result.test_sz_eq("post", size_t(uri.port()), 9000);
1✔
27
         return result;
1✔
28
      }
1✔
29

30
      static Test::Result test_uri_tostring() {
1✔
31
         Test::Result result("URI to_string");
1✔
32

33
         result.test_str_eq(
2✔
34
            "domain", Botan::URI(Botan::URI::Type::Domain, "localhost", 23).to_string(), "localhost:23");
2✔
35
         result.test_str_eq(
2✔
36
            "IPv4", Botan::URI(Botan::URI::Type::IPv4, "192.168.1.1", 25).to_string(), "192.168.1.1:25");
2✔
37
         result.test_str_eq("IPv6", Botan::URI(Botan::URI::Type::IPv6, "::1", 65535).to_string(), "[::1]:65535");
1✔
38
         result.test_str_eq("IPv6 no port", Botan::URI(Botan::URI::Type::IPv6, "::1", 0).to_string(), "::1");
1✔
39

40
         return result;
1✔
41
      }
×
42

43
      static Test::Result test_uri_parsing() {
1✔
44
         Test::Result result("URI parsing");
1✔
45

46
         struct URITestCase {
10✔
47
               std::string uri;
48
               std::string host;
49
               Botan::URI::Type type;
50
               uint16_t port;
51
         };
52

53
         const std::vector<URITestCase> tests{
1✔
54
            URITestCase{"localhost:80", "localhost", Botan::URI::Type::Domain, 80},
1✔
55
            URITestCase{"www.example.com", "www.example.com", Botan::URI::Type::Domain, 0},
56
            URITestCase{"192.168.1.1", "192.168.1.1", Botan::URI::Type::IPv4, 0},
57
            URITestCase{"192.168.1.1:34567", "192.168.1.1", Botan::URI::Type::IPv4, 34567},
58
            URITestCase{"[::1]:61234", "::1", Botan::URI::Type::IPv6, 61234},
59
         };
6✔
60

61
         for(const auto& t : tests) {
6✔
62
            auto test_URI = [&result](const Botan::URI& uri, const std::string& host, const uint16_t port) {
15✔
63
               result.test_str_eq("host", uri.host(), host);
10✔
64
               result.test_u16_eq("port", uri.port(), port);
10✔
65
            };
15✔
66

67
            if(t.type != Botan::URI::Type::IPv4) {
5✔
68
               result.test_throws("invalid", [&t]() { Botan::URI::from_ipv4(t.uri); });
6✔
69
            }
70
            if(t.type != Botan::URI::Type::IPv6) {
5✔
71
               result.test_throws("invalid", [&t]() { Botan::URI::from_ipv6(t.uri); });
8✔
72
            }
73
            if(t.type != Botan::URI::Type::Domain) {
5✔
74
               result.test_throws("invalid", [&t]() { Botan::URI::from_domain(t.uri); });
6✔
75
            }
76

77
            const auto any = Botan::URI::from_any(t.uri);
5✔
78
            result.test_is_true("from_any type is expected", any.type() == t.type);
5✔
79
            test_URI(any, t.host, t.port);
5✔
80
            if(t.type == Botan::URI::Type::Domain) {
5✔
81
               test_URI(Botan::URI::from_domain(t.uri), t.host, t.port);
4✔
82
            } else if(t.type == Botan::URI::Type::IPv4) {
3✔
83
               test_URI(Botan::URI::from_ipv4(t.uri), t.host, t.port);
4✔
84
            } else if(t.type == Botan::URI::Type::IPv6) {
1✔
85
               test_URI(Botan::URI::from_ipv6(t.uri), t.host, t.port);
2✔
86
            }
87
         }
5✔
88

89
         //since GCC 4.8 does not support regex this would possibly be accepted as valid domains,
90
         //but we just want to test IPv6 parsing, so the test needs to be individual
91
         result.test_throws("invalid IPv6", []() { Botan::URI::from_ipv6("]"); });
2✔
92
         result.test_throws("invalid IPv6", []() { Botan::URI::from_ipv6("[::1]1"); });
2✔
93

94
         return result;
1✔
95
      }
3✔
96

97
      static Test::Result test_uri_parsing_invalid() {
1✔
98
         Test::Result result("URI parsing invalid");
1✔
99

100
         const std::vector<std::string> invalid_uris = {
1✔
101
            "localhost::80",
102
            "localhost:70000",
103
            "[::1]:a",
104
            "[::1]:70000",
105
            "hello..com",
106
            ".leading.dot",
107
            "yeah.i.thought.so.",
108
         };
1✔
109

110
         for(const auto& invalid_uri : invalid_uris) {
8✔
111
            try {
7✔
112
               auto uri = Botan::URI::from_any(invalid_uri);
7✔
113
               result.test_failure("Failed to reject invalid URI '" + invalid_uri + "'");
×
114
            } catch(Botan::Invalid_Argument&) {
7✔
115
               result.test_success("Rejected invalid URI");
7✔
116
            }
7✔
117
         }
118
         return result;
1✔
119
      }
1✔
120

121
   public:
122
      std::vector<Test::Result> run() override {
1✔
123
         std::vector<Test::Result> results;
1✔
124

125
         results.push_back(test_uri_ctor());
2✔
126
         results.push_back(test_uri_tostring());
2✔
127
         results.push_back(test_uri_parsing());
2✔
128
         results.push_back(test_uri_parsing_invalid());
2✔
129

130
         return results;
1✔
131
      }
×
132
};
133

134
BOTAN_REGISTER_TEST("utils", "uri", URI_Tests);
135

136
}  // namespace Botan_Tests
137

138
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc