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

randombit / botan / 13210394651

08 Feb 2025 12:33AM UTC coverage: 91.656% (-0.006%) from 91.662%
13210394651

push

github

web-flow
Merge pull request #4642 from randombit/jack/target-info-header

Add internal target_info.h header

94837 of 103471 relevant lines covered (91.66%)

11243945.63 hits per line

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

94.03
/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/internal/uri.h>
15

16
namespace Botan_Tests {
17

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

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

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

37
         return result;
1✔
38
      }
×
39

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

43
         struct {
5✔
44
               std::string uri;
45
               std::string host;
46
               Botan::URI::Type type;
47
               uint16_t port;
48
         } tests[]{
49
            {"localhost:80", "localhost", Botan::URI::Type::Domain, 80},
50
            {"www.example.com", "www.example.com", Botan::URI::Type::Domain, 0},
51
            {"192.168.1.1", "192.168.1.1", Botan::URI::Type::IPv4, 0},
52
            {"192.168.1.1:34567", "192.168.1.1", Botan::URI::Type::IPv4, 34567},
53
            {"[::1]:61234", "::1", Botan::URI::Type::IPv6, 61234},
54
         };
11✔
55

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

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

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

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

89
         return result;
2✔
90
      }
6✔
91

92
      static Test::Result test_uri_parsing_invalid() {
1✔
93
         Test::Result result("URI parsing invalid");
1✔
94

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

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

116
   public:
117
      std::vector<Test::Result> run() override {
1✔
118
         std::vector<Test::Result> results;
1✔
119

120
         results.push_back(test_uri_ctor());
2✔
121
         results.push_back(test_uri_tostring());
2✔
122
         results.push_back(test_uri_parsing());
2✔
123
         results.push_back(test_uri_parsing_invalid());
2✔
124

125
         return results;
1✔
126
      }
×
127
};
128

129
BOTAN_REGISTER_TEST("utils", "uri", URI_Tests);
130

131
}  // namespace Botan_Tests
132

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