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