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

randombit / botan / 11582876813

29 Oct 2024 09:37PM UTC coverage: 91.073% (+0.003%) from 91.07%
11582876813

push

github

web-flow
Merge pull request #4418 from randombit/jack/new-uri

Refactor and fixes for URI type

90391 of 99251 relevant lines covered (91.07%)

9485591.44 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
#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
         Botan::URI uri(Botan::URI::Type::Domain, "localhost", 9000);
1✔
21
         result.confirm("type", uri.type() == Botan::URI::Type::Domain);
2✔
22
         result.test_eq("host", uri.host(), "localhost");
2✔
23
         result.test_eq("post", size_t(uri.port()), 9000);
1✔
24
         return result;
1✔
25
      }
1✔
26

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

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

35
         return result;
1✔
36
      }
×
37

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

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

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

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

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

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

87
         return result;
2✔
88
      }
6✔
89

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

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

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

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

118
         results.push_back(test_uri_ctor());
2✔
119
         results.push_back(test_uri_tostring());
2✔
120
         results.push_back(test_uri_parsing());
2✔
121
         results.push_back(test_uri_parsing_invalid());
2✔
122

123
         return results;
1✔
124
      }
×
125
};
126

127
BOTAN_REGISTER_TEST("utils", "uri", URI_Tests);
128

129
}  // namespace Botan_Tests
130

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