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

randombit / botan / 23225340130

18 Mar 2026 01:53AM UTC coverage: 89.677% (-0.001%) from 89.678%
23225340130

push

github

web-flow
Merge pull request #5456 from randombit/jack/clang-tidy-22

Fix various warnings from clang-tidy 22

104438 of 116460 relevant lines covered (89.68%)

11819947.55 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
namespace {
20

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

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

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

42
         return result;
1✔
43
      }
×
44

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

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

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

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

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

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

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

96
         return result;
1✔
97
      }
3✔
98

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

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

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

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

127
         results.push_back(test_uri_ctor());
2✔
128
         results.push_back(test_uri_tostring());
2✔
129
         results.push_back(test_uri_parsing());
2✔
130
         results.push_back(test_uri_parsing_invalid());
2✔
131

132
         return results;
1✔
133
      }
×
134
};
135

136
BOTAN_REGISTER_TEST("utils", "uri", URI_Tests);
137

138
}  // namespace
139

140
}  // namespace Botan_Tests
141

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