1 #include <algorithm>
2 #include <blobs-ipmid/manager.hpp>
3 #include <blobs-ipmid/test/blob_mock.hpp>
4 #include <string>
5 #include <vector>
6 
7 #include <gtest/gtest.h>
8 
9 namespace blobs
10 {
11 
12 using ::testing::Return;
13 
14 TEST(BlobsTest, RegisterNullPointerFails)
15 {
16     // The only invalid pointer really is a null one.
17 
18     BlobManager mgr;
19     EXPECT_FALSE(mgr.registerHandler(nullptr));
20 }
21 
22 TEST(BlobsTest, RegisterNonNullPointerPasses)
23 {
24     // Test that the valid pointer is boringly registered.
25 
26     BlobManager mgr;
27     std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
28     EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
29 }
30 
31 TEST(BlobsTest, GetCountNoBlobsRegistered)
32 {
33     // Request the Blob Count when there are no blobs.
34 
35     BlobManager mgr;
36     EXPECT_EQ(0, mgr.buildBlobList());
37 }
38 
39 TEST(BlobsTest, GetCountBlobRegisteredReturnsOne)
40 {
41     // Request the blob count and verify the list is of length one.
42 
43     BlobManager mgr;
44     std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
45     auto m1ptr = m1.get();
46     std::vector<std::string> v = {"item"};
47 
48     EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
49 
50     // We expect it to ask for the list.
51     EXPECT_CALL(*m1ptr, getBlobIds()).WillOnce(Return(v));
52 
53     EXPECT_EQ(1, mgr.buildBlobList());
54 }
55 
56 TEST(BlobsTest, GetCountBlobsRegisteredEachReturnsOne)
57 {
58     // Request the blob count and verify the list is of length two.
59 
60     BlobManager mgr;
61     std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
62     std::unique_ptr<BlobMock> m2 = std::make_unique<BlobMock>();
63     auto m1ptr = m1.get();
64     auto m2ptr = m2.get();
65     std::vector<std::string> v1, v2;
66 
67     v1.push_back("asdf");
68     v2.push_back("ghjk");
69 
70     EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
71     EXPECT_TRUE(mgr.registerHandler(std::move(m2)));
72 
73     // We expect it to ask for the list.
74     EXPECT_CALL(*m1ptr, getBlobIds()).WillOnce(Return(v1));
75     EXPECT_CALL(*m2ptr, getBlobIds()).WillOnce(Return(v2));
76 
77     EXPECT_EQ(2, mgr.buildBlobList());
78 }
79 
80 TEST(BlobsTest, EnumerateBlobZerothEntry)
81 {
82     // Validate that you can read back the 0th blobId.
83 
84     BlobManager mgr;
85     std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
86     std::unique_ptr<BlobMock> m2 = std::make_unique<BlobMock>();
87     auto m1ptr = m1.get();
88     auto m2ptr = m2.get();
89     std::vector<std::string> v1, v2;
90 
91     v1.push_back("asdf");
92     v2.push_back("ghjk");
93 
94     EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
95     EXPECT_TRUE(mgr.registerHandler(std::move(m2)));
96 
97     // We expect it to ask for the list.
98     EXPECT_CALL(*m1ptr, getBlobIds()).WillOnce(Return(v1));
99     EXPECT_CALL(*m2ptr, getBlobIds()).WillOnce(Return(v2));
100 
101     EXPECT_EQ(2, mgr.buildBlobList());
102 
103     std::string result = mgr.getBlobId(0);
104     // The exact order the blobIds is returned is not guaranteed to never
105     // change.
106     EXPECT_TRUE("asdf" == result || "ghjk" == result);
107 }
108 
109 TEST(BlobsTest, EnumerateBlobFirstEntry)
110 {
111     // Validate you can read back the two real entries.
112 
113     BlobManager mgr;
114     std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
115     std::unique_ptr<BlobMock> m2 = std::make_unique<BlobMock>();
116     auto m1ptr = m1.get();
117     auto m2ptr = m2.get();
118     std::vector<std::string> v1, v2;
119 
120     v1.push_back("asdf");
121     v2.push_back("ghjk");
122 
123     // Presently the list of blobs is read and appended in a specific order,
124     // but I don't want to rely on that.
125     EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
126     EXPECT_TRUE(mgr.registerHandler(std::move(m2)));
127 
128     // We expect it to ask for the list.
129     EXPECT_CALL(*m1ptr, getBlobIds()).WillOnce(Return(v1));
130     EXPECT_CALL(*m2ptr, getBlobIds()).WillOnce(Return(v2));
131 
132     EXPECT_EQ(2, mgr.buildBlobList());
133 
134     // Try to grab the two blobIds and verify they're in the list.
135     std::vector<std::string> results;
136     results.push_back(mgr.getBlobId(0));
137     results.push_back(mgr.getBlobId(1));
138     EXPECT_EQ(2, results.size());
139     EXPECT_TRUE(std::find(results.begin(), results.end(), "asdf") !=
140                 results.end());
141     EXPECT_TRUE(std::find(results.begin(), results.end(), "ghjk") !=
142                 results.end());
143 }
144 
145 TEST(BlobTest, EnumerateBlobInvalidEntry)
146 {
147     // Validate trying to read an invalid entry fails expectedly.
148 
149     BlobManager mgr;
150     std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
151     std::unique_ptr<BlobMock> m2 = std::make_unique<BlobMock>();
152     auto m1ptr = m1.get();
153     auto m2ptr = m2.get();
154     std::vector<std::string> v1, v2;
155 
156     v1.push_back("asdf");
157     v2.push_back("ghjk");
158 
159     EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
160     EXPECT_TRUE(mgr.registerHandler(std::move(m2)));
161 
162     // We expect it to ask for the list.
163     EXPECT_CALL(*m1ptr, getBlobIds()).WillOnce(Return(v1));
164     EXPECT_CALL(*m2ptr, getBlobIds()).WillOnce(Return(v2));
165 
166     EXPECT_EQ(2, mgr.buildBlobList());
167 
168     // Grabs the third entry which isn't valid.
169     EXPECT_STREQ("", mgr.getBlobId(2).c_str());
170 }
171 } // namespace blobs
172