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

randombit / botan / 5111374265

29 May 2023 11:19AM UTC coverage: 92.227% (+0.5%) from 91.723%
5111374265

push

github

randombit
Next release will be 3.1.0. Update release notes

75588 of 81959 relevant lines covered (92.23%)

11886470.91 hits per line

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

96.43
/src/tests/test_uri.cpp
1
/*
2
* (C) 2019 Nuno Goncalves <nunojpg@gmail.com>
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include "tests.h"
8

9
#if defined(BOTAN_HAS_SOCKETS) && (defined(BOTAN_TARGET_OS_HAS_SOCKETS) || defined(BOTAN_TARGET_OS_HAS_WINSOCK2))
10

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

13
namespace Botan_Tests {
14

15
class URI_Tests final : public Test {
×
16
      static void test_uri_ctor(std::vector<Test::Result>& results) {
1✔
17
         Test::Result result("uri constructors");
1✔
18
         Botan::URI uri(Botan::URI::Type::Domain, "localhost", 80);
1✔
19
         result.confirm("type", uri.type == Botan::URI::Type::Domain);
2✔
20
         result.test_eq("host", uri.host, "localhost");
2✔
21
         result.confirm("post", uri.port == 80);
2✔
22
         results.push_back(result);
1✔
23
      }
1✔
24

25
      static void test_uri_tostring(std::vector<Test::Result>& results) {
1✔
26
         Test::Result result("uri to_string");
1✔
27

28
         result.test_eq("domain", Botan::URI(Botan::URI::Type::Domain, "localhost", 80).to_string(), "localhost:80");
2✔
29
         result.test_eq("IPv4", Botan::URI(Botan::URI::Type::IPv4, "192.168.1.1", 80).to_string(), "192.168.1.1:80");
2✔
30
         result.test_eq("IPv6", Botan::URI(Botan::URI::Type::IPv6, "::1", 80).to_string(), "[::1]:80");
2✔
31
         result.test_eq("IPv6 no port", Botan::URI(Botan::URI::Type::IPv6, "::1", 0).to_string(), "::1");
2✔
32
         result.test_throws("invalid", []() { Botan::URI(Botan::URI::Type::NotSet, "", 0).to_string(); });
3✔
33

34
         results.push_back(result);
1✔
35
      }
1✔
36

37
      static void test_uri_factories(std::vector<Test::Result>& results) {
1✔
38
         Test::Result result("uri factories");
1✔
39

40
         struct {
1✔
41
               std::string uri;
42
               std::string host;
43
               Botan::URI::Type type;
44
               unsigned port;
45
         } tests[]{
46
            {"localhost::80", {}, Botan::URI::Type::NotSet, 0},
47
            {"localhost:70000", {}, Botan::URI::Type::NotSet, 0},
48
            {"[::1]:a", {}, Botan::URI::Type::NotSet, 0},
49
            {"[::1]:70000", {}, Botan::URI::Type::NotSet, 0},
50
            {"localhost:80", "localhost", Botan::URI::Type::Domain, 80},
51
            {"www.example.com", "www.example.com", Botan::URI::Type::Domain, 0},
52
            {"192.168.1.1", "192.168.1.1", Botan::URI::Type::IPv4, 0},
53
            {"192.168.1.1:34567", "192.168.1.1", Botan::URI::Type::IPv4, 34567},
54
            {"[::1]:61234", "::1", Botan::URI::Type::IPv6, 61234},
55
         };
10✔
56

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

63
            if(t.type != Botan::URI::Type::IPv4)
9✔
64
               result.test_throws("invalid", [&t]() { Botan::URI::fromIPv4(t.uri); });
28✔
65
            if(t.type != Botan::URI::Type::IPv6)
9✔
66
               result.test_throws("invalid", [&t]() { Botan::URI::fromIPv6(t.uri); });
32✔
67
            if(t.type != Botan::URI::Type::Domain)
9✔
68
               result.test_throws("invalid", [&t]() { Botan::URI::fromDomain(t.uri); });
28✔
69
            if(t.type == Botan::URI::Type::NotSet) {
9✔
70
               result.test_throws("invalid", [&t]() { Botan::URI::fromAny(t.uri); });
16✔
71
            } else {
72
               const auto any = Botan::URI::fromAny(t.uri);
5✔
73
               result.confirm("type any", 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::fromDomain(t.uri), t.host, t.port);
4✔
77
               } else if(t.type == Botan::URI::Type::IPv4) {
3✔
78
                  test_URI(Botan::URI::fromIPv4(t.uri), t.host, t.port);
4✔
79
               } else if(t.type == Botan::URI::Type::IPv6) {
1✔
80
                  test_URI(Botan::URI::fromIPv6(t.uri), t.host, t.port);
2✔
81
               }
82
            }
5✔
83
         }
84

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

90
         results.push_back(result);
1✔
91
      }
11✔
92

93
   public:
94
      std::vector<Test::Result> run() override {
1✔
95
         std::vector<Test::Result> results;
1✔
96

97
         test_uri_ctor(results);
1✔
98
         test_uri_tostring(results);
1✔
99
         test_uri_factories(results);
1✔
100

101
         return results;
1✔
102
      }
×
103
};
104

105
BOTAN_REGISTER_TEST("utils", "uri", URI_Tests);
106

107
}  // namespace Botan_Tests
108

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