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

randombit / botan / 21794448852

08 Feb 2026 12:09AM UTC coverage: 90.065% (-0.008%) from 90.073%
21794448852

push

github

web-flow
Merge pull request #5295 from randombit/jack/header-patrol-3

Reduce header dependencies in tests and cli

102230 of 113507 relevant lines covered (90.06%)

11492365.41 hits per line

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

95.65
/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.confirm("type", uri.type() == Botan::URI::Type::Domain);
2✔
25
         result.test_eq("host", uri.host(), "localhost");
2✔
26
         result.test_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_eq("domain", Botan::URI(Botan::URI::Type::Domain, "localhost", 23).to_string(), "localhost:23");
2✔
34
         result.test_eq("IPv4", Botan::URI(Botan::URI::Type::IPv4, "192.168.1.1", 25).to_string(), "192.168.1.1:25");
2✔
35
         result.test_eq("IPv6", Botan::URI(Botan::URI::Type::IPv6, "::1", 65535).to_string(), "[::1]:65535");
2✔
36
         result.test_eq("IPv6 no port", Botan::URI(Botan::URI::Type::IPv6, "::1", 0).to_string(), "::1");
2✔
37

38
         return result;
1✔
39
      }
×
40

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

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

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

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

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

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

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

92
         return result;
1✔
93
      }
3✔
94

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

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

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

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

123
         results.push_back(test_uri_ctor());
2✔
124
         results.push_back(test_uri_tostring());
2✔
125
         results.push_back(test_uri_parsing());
2✔
126
         results.push_back(test_uri_parsing_invalid());
2✔
127

128
         return results;
1✔
129
      }
×
130
};
131

132
BOTAN_REGISTER_TEST("utils", "uri", URI_Tests);
133

134
}  // namespace Botan_Tests
135

136
#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