1 
2 #include "http_response.hpp"
3 #include "update_service.hpp"
4 
5 #include <boost/url/url.hpp>
6 
7 #include <optional>
8 
9 #include <gtest/gtest.h>
10 
11 namespace redfish
12 {
13 namespace
14 {
15 
16 TEST(UpdateService, ParseTFTPPostitive)
17 {
18     crow::Response res;
19     {
20         // No protocol, schema on url
21         std::optional<boost::urls::url> ret =
22             parseSimpleUpdateUrl("tftp://1.1.1.1/path", std::nullopt, res);
23         ASSERT_TRUE(ret);
24         if (!ret)
25         {
26             return;
27         }
28         EXPECT_EQ(ret->encoded_host_and_port(), "1.1.1.1");
29         EXPECT_EQ(ret->encoded_path(), "/path");
30         EXPECT_EQ(ret->scheme(), "tftp");
31     }
32     {
33         // Protocol, no schema on url
34         std::optional<boost::urls::url> ret =
35             parseSimpleUpdateUrl("1.1.1.1/path", "TFTP", res);
36         ASSERT_TRUE(ret);
37         if (!ret)
38         {
39             return;
40         }
41         EXPECT_EQ(ret->encoded_host_and_port(), "1.1.1.1");
42         EXPECT_EQ(ret->encoded_path(), "/path");
43         EXPECT_EQ(ret->scheme(), "tftp");
44     }
45     {
46         // Both protocol and schema on url
47         std::optional<boost::urls::url> ret =
48             parseSimpleUpdateUrl("tftp://1.1.1.1/path", "TFTP", res);
49         ASSERT_TRUE(ret);
50         if (!ret)
51         {
52             return;
53         }
54         EXPECT_EQ(ret->encoded_host_and_port(), "1.1.1.1");
55         EXPECT_EQ(ret->encoded_path(), "/path");
56         EXPECT_EQ(ret->scheme(), "tftp");
57     }
58 }
59 
60 TEST(UpdateService, ParseHTTPSPostitive)
61 {
62     crow::Response res;
63     {
64         // No protocol, schema on url
65         std::optional<boost::urls::url> ret =
66             parseSimpleUpdateUrl("https://1.1.1.1/path", std::nullopt, res);
67         ASSERT_TRUE(ret);
68         if (!ret)
69         {
70             return;
71         }
72         EXPECT_EQ(ret->encoded_host_and_port(), "1.1.1.1");
73         EXPECT_EQ(ret->encoded_path(), "/path");
74         EXPECT_EQ(ret->scheme(), "https");
75     }
76     {
77         // Protocol, no schema on url
78         std::optional<boost::urls::url> ret =
79             parseSimpleUpdateUrl("1.1.1.1/path", "HTTPS", res);
80         ASSERT_TRUE(ret);
81         if (!ret)
82         {
83             return;
84         }
85         EXPECT_EQ(ret->encoded_host_and_port(), "1.1.1.1");
86         EXPECT_EQ(ret->encoded_path(), "/path");
87         EXPECT_EQ(ret->scheme(), "https");
88     }
89     {
90         // Both protocol and schema on url with path
91         std::optional<boost::urls::url> ret =
92             parseSimpleUpdateUrl("https://1.1.1.1/path", "HTTPS", res);
93         ASSERT_TRUE(ret);
94         if (!ret)
95         {
96             return;
97         }
98         EXPECT_EQ(ret->encoded_host_and_port(), "1.1.1.1");
99         EXPECT_EQ(ret->encoded_path(), "/path");
100         EXPECT_EQ(ret->scheme(), "https");
101     }
102     {
103         // Both protocol and schema on url without path
104         std::optional<boost::urls::url> ret =
105             parseSimpleUpdateUrl("https://1.1.1.1", "HTTPS", res);
106         ASSERT_TRUE(ret);
107         if (!ret)
108         {
109             return;
110         }
111         EXPECT_EQ(ret->encoded_host_and_port(), "1.1.1.1");
112         EXPECT_EQ(ret->encoded_path(), "/");
113         EXPECT_EQ(ret->scheme(), "https");
114     }
115     {
116         // Both protocol and schema on url without path
117         std::optional<boost::urls::url> ret =
118             parseSimpleUpdateUrl("https://[2001:db8::1]", "HTTPS", res);
119         ASSERT_TRUE(ret);
120         if (!ret)
121         {
122             return;
123         }
124         EXPECT_EQ(ret->encoded_host_and_port(), "[2001:db8::1]");
125         EXPECT_EQ(ret->encoded_path(), "/");
126         EXPECT_EQ(ret->scheme(), "https");
127     }
128 }
129 
130 TEST(UpdateService, ParseTFTPNegative)
131 {
132     crow::Response res;
133     // No protocol, no schema
134     ASSERT_EQ(parseSimpleUpdateUrl("1.1.1.1/path", std::nullopt, res),
135               std::nullopt);
136     // No host
137     ASSERT_EQ(parseSimpleUpdateUrl("/path", "TFTP", res), std::nullopt);
138 
139     // No host
140     ASSERT_EQ(parseSimpleUpdateUrl("path", "TFTP", res), std::nullopt);
141 
142     // No path
143     ASSERT_EQ(parseSimpleUpdateUrl("tftp://1.1.1.1", "TFTP", res),
144               std::nullopt);
145     ASSERT_EQ(parseSimpleUpdateUrl("tftp://1.1.1.1/", "TFTP", res),
146               std::nullopt);
147 }
148 } // namespace
149 } // namespace redfish
150