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

randombit / botan / 15808738489

22 Jun 2025 04:44PM UTC coverage: 90.56% (-0.001%) from 90.561%
15808738489

push

github

web-flow
Merge pull request #4942 from KaganCanSit/cppcheck_warning_message_in_tests

Fix/reduce Cppcheck warnings in test code

98795 of 109094 relevant lines covered (90.56%)

12461219.98 hits per line

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

92.75
/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 URITestCase {
×
44
               std::string uri;
45
               std::string host;
46
               Botan::URI::Type type;
47
               uint16_t port;
48
         };
49

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

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

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

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

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

91
         return result;
1✔
92
      }
1✔
93

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

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

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

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

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

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

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

133
}  // namespace Botan_Tests
134

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