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

randombit / botan / 5123321399

30 May 2023 04:06PM UTC coverage: 92.213% (+0.004%) from 92.209%
5123321399

Pull #3558

github

web-flow
Merge dd72f7389 into 057bcbc35
Pull Request #3558: Add braces around all if/else statements

75602 of 81986 relevant lines covered (92.21%)

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

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

93
         results.push_back(result);
1✔
94
      }
11✔
95

96
   public:
97
      std::vector<Test::Result> run() override {
1✔
98
         std::vector<Test::Result> results;
1✔
99

100
         test_uri_ctor(results);
1✔
101
         test_uri_tostring(results);
1✔
102
         test_uri_factories(results);
1✔
103

104
         return results;
1✔
105
      }
×
106
};
107

108
BOTAN_REGISTER_TEST("utils", "uri", URI_Tests);
109

110
}  // namespace Botan_Tests
111

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