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

randombit / botan / 11844561993

14 Nov 2024 07:58PM UTC coverage: 91.178% (+0.1%) from 91.072%
11844561993

Pull #4435

github

web-flow
Merge 81dcb29da into e430f157a
Pull Request #4435: Test duration values ​​are now presented in seconds with six digits of precision. Tests without time measurements have been edited.

91856 of 100744 relevant lines covered (91.18%)

9311006.71 hits per line

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

94.67
/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
#if defined(BOTAN_HAS_SOCKETS) && (defined(BOTAN_TARGET_OS_HAS_SOCKETS) || defined(BOTAN_TARGET_OS_HAS_WINSOCK2))
11

12
   #include <botan/internal/uri.h>
13

14
namespace Botan_Tests {
15

16
class URI_Tests final : public Test {
×
17
   private:
18
      static Test::Result test_uri_ctor() {
1✔
19
         Test::Result result("URI constructors");
1✔
20
         result.start_timer();
1✔
21
         Botan::URI uri(Botan::URI::Type::Domain, "localhost", 9000);
1✔
22
         result.confirm("type", uri.type() == Botan::URI::Type::Domain);
2✔
23
         result.test_eq("host", uri.host(), "localhost");
2✔
24
         result.test_eq("post", size_t(uri.port()), 9000);
1✔
25
         result.end_timer();
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
         result.start_timer();
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
         result.end_timer();
1✔
39
         return result;
1✔
40
      }
×
41

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

46
         struct {
5✔
47
               std::string uri;
48
               std::string host;
49
               Botan::URI::Type type;
50
               uint16_t port;
51
         } tests[]{
52
            {"localhost:80", "localhost", Botan::URI::Type::Domain, 80},
53
            {"www.example.com", "www.example.com", Botan::URI::Type::Domain, 0},
54
            {"192.168.1.1", "192.168.1.1", Botan::URI::Type::IPv4, 0},
55
            {"192.168.1.1:34567", "192.168.1.1", Botan::URI::Type::IPv4, 34567},
56
            {"[::1]:61234", "::1", Botan::URI::Type::IPv6, 61234},
57
         };
11✔
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 acceped 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
         result.end_timer();
1✔
93
         return result;
1✔
94
      }
6✔
95

96
      static Test::Result test_uri_parsing_invalid() {
1✔
97
         Test::Result result("URI parsing invalid");
1✔
98
         result.start_timer();
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
         result.end_timer();
1✔
119
         return result;
1✔
120
      }
1✔
121

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

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

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

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

137
}  // namespace Botan_Tests
138

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

© 2025 Coveralls, Inc