xref: /openbmc/entity-manager/test/test_gzip-utils.cpp (revision cefe4bb6b95624a4d1691cfdba075fff4fdb39f8)
1 #include "fru_device/gzip_utils.hpp"
2 
3 #include <algorithm>
4 #include <array>
5 #include <iterator>
6 #include <ranges>
7 
8 #include "gmock/gmock.h"
9 #include "gtest/gtest.h"
10 
11 TEST(GzipUtils, EmptyCompressed)
12 {
13     std::vector<uint8_t> compressed = {};
14     std::optional<std::string> uncompressed = gzipInflate(compressed);
15     EXPECT_EQ(uncompressed, std::nullopt);
16 }
17 
18 TEST(GzipUtils, GoodCompressed)
19 {
20     // Empty file created with
21     // touch foo && gzip -c foo | xxd -i
22     std::array<uint8_t, 24> emptyCompressed{
23         0x1f, 0x8b, 0x08, 0x08, 0x16, 0x37, 0xdc, 0x68, 0x00, 0x03, 0x66, 0x6f,
24         0x6f, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
25     std::optional<std::string> uncompressed = gzipInflate(emptyCompressed);
26     ASSERT_NE(uncompressed, std::nullopt);
27     if (uncompressed)
28     {
29         EXPECT_EQ(uncompressed->size(), 0U);
30     }
31 }
32 
33 TEST(GzipUtils, 100kcompressedZeros)
34 {
35     // File created with
36     // dd if=/dev/zero of=10kfile bs=10k count=1 && gzip -c 10kfile | xxd -i
37     std::array<uint8_t, 53> emptyCompressed{
38         0x1f, 0x8b, 0x08, 0x08, 0xcb, 0x37, 0xdc, 0x68, 0x00, 0x03, 0x31,
39         0x30, 0x6b, 0x66, 0x69, 0x6c, 0x65, 0x00, 0xed, 0xc1, 0x01, 0x0d,
40         0x00, 0x00, 0x00, 0xc2, 0xa0, 0xf7, 0x4f, 0x6d, 0x0e, 0x37, 0xa0,
41         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x37,
42         0x03, 0x9a, 0xde, 0x1d, 0x27, 0x00, 0x28, 0x00, 0x00};
43     std::optional<std::string> uncompressed = gzipInflate(emptyCompressed);
44     ASSERT_NE(uncompressed, std::nullopt);
45     if (uncompressed)
46     {
47         EXPECT_EQ(uncompressed->size(), 10240U);
48         EXPECT_TRUE(std::ranges::all_of(*uncompressed, [](uint8_t c) {
49             return c == 0U;
50         }));
51     }
52 }
53 
54 using ::testing::ElementsAre;
55 using ::testing::IsEmpty;
56 
57 TEST(GzipUtils, getNodeFromXml)
58 {
59     constexpr std::string_view xml = R"(<?xml version="1.0" encoding="UTF-8"?>
60         <bookstore>
61           <book category="cooking">
62             <title lang="en">Everyday Italian</title>
63             <author>Giada De Laurentiis</author>
64             <year>2005</year>
65             <price>30.00</price>
66           </book>
67         </bookstore>
68     )";
69 
70     EXPECT_THAT(getNodeFromXml(xml, "/bookstore/book/title"),
71                 ElementsAre("Everyday Italian"));
72     EXPECT_THAT(getNodeFromXml(xml, "/bookstore/book/author"),
73                 ElementsAre("Giada De Laurentiis"));
74     EXPECT_THAT(getNodeFromXml(xml, "/bookstore/book/year"),
75                 ElementsAre("2005"));
76     EXPECT_THAT(getNodeFromXml(xml, "/bookstore/book/price"),
77                 ElementsAre("30.00"));
78     EXPECT_THAT(getNodeFromXml(xml, "//book/title"),
79                 ElementsAre("Everyday Italian"));
80     EXPECT_THAT(getNodeFromXml(xml, "//book/author"),
81                 ElementsAre("Giada De Laurentiis"));
82     EXPECT_THAT(getNodeFromXml(xml, "//book/year"), ElementsAre("2005"));
83     EXPECT_THAT(getNodeFromXml(xml, "//book/price"), ElementsAre("30.00"));
84     EXPECT_THAT(getNodeFromXml(xml, "///title"),
85                 ElementsAre("Everyday Italian"));
86     EXPECT_THAT(getNodeFromXml(xml, "///author"),
87                 ElementsAre("Giada De Laurentiis"));
88     EXPECT_THAT(getNodeFromXml(xml, "///year"), ElementsAre("2005"));
89     EXPECT_THAT(getNodeFromXml(xml, "///price"), ElementsAre("30.00"));
90 
91     EXPECT_THAT(getNodeFromXml(xml, "/bookstore/book/noexist"), IsEmpty());
92     EXPECT_THAT(getNodeFromXml(xml, "/bookstore/noexist/title"), IsEmpty());
93     EXPECT_THAT(getNodeFromXml(xml, "/noexist/book/title"), IsEmpty());
94     EXPECT_THAT(getNodeFromXml(xml, "//book/noexist"), IsEmpty());
95     EXPECT_THAT(getNodeFromXml(xml, "//noexist/title"), IsEmpty());
96     EXPECT_THAT(getNodeFromXml(xml, "///noexist"), IsEmpty());
97 
98     // invalid xpath
99     EXPECT_THAT(getNodeFromXml(xml, "foo"), IsEmpty());
100     EXPECT_THAT(getNodeFromXml(xml, "foo/bar"), IsEmpty());
101     EXPECT_THAT(getNodeFromXml(xml, "?"), IsEmpty());
102 }
103