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 protocl 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, ParseTFTPNegative)
61 {
62     crow::Response res;
63     // No protocol, no schema
64     ASSERT_EQ(parseSimpleUpdateUrl("1.1.1.1/path", std::nullopt, res),
65               std::nullopt);
66     // No host
67     ASSERT_EQ(parseSimpleUpdateUrl("/path", "TFTP", res), std::nullopt);
68 
69     // No host
70     ASSERT_EQ(parseSimpleUpdateUrl("path", "TFTP", res), std::nullopt);
71 
72     // No path
73     ASSERT_EQ(parseSimpleUpdateUrl("tftp://1.1.1.1", "TFTP", res),
74               std::nullopt);
75     ASSERT_EQ(parseSimpleUpdateUrl("tftp://1.1.1.1/", "TFTP", res),
76               std::nullopt);
77 }
78 } // namespace
79 } // namespace redfish
80