11// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
22
3- #include < fmt/format.h>
43#include < gtest/gtest.h>
54
5+ #include < folly/FileUtil.h>
6+ #include < folly/testing/TestUtil.h>
7+
68#include " fboss/platform/weutil/FbossEepromInterface.h"
79#include " fboss/platform/weutil/if/gen-cpp2/eeprom_contents_types.h"
810
@@ -13,7 +15,7 @@ using EepromData = std::vector<uint8_t>;
1315
1416// Based on the Spec for V5 EEPROM:
1517// https://github.com/facebook/fboss/blob/main/fboss/docs/meta_eeprom_format_v5.md
16- EepromData kEepromV5 = {
18+ const EepromData kEepromV5 = {
1719 0xfb , 0xfb , 0x05 , 0xff , 0x01 , 0x0d , 0x46 , 0x49 , 0x52 , 0x53 , 0x54 , 0x5f ,
1820 0x53 , 0x51 , 0x55 , 0x45 , 0x45 , 0x5a , 0x45 , 0x02 , 0x08 , 0x32 , 0x30 , 0x31 ,
1921 0x32 , 0x33 , 0x34 , 0x35 , 0x36 , 0x03 , 0x08 , 0x53 , 0x59 , 0x53 , 0x41 , 0x31 ,
@@ -36,7 +38,7 @@ EepromData kEepromV5 = {
3638
3739// EEPROM V5 with wrong CRC Programmed (same as the one above, but last 2
3840// bytes have wrong CRC value programmed.)
39- EepromData kEepromV5WrongCrc = {
41+ const EepromData kEepromV5WrongCrc = {
4042 0xfb , 0xfb , 0x05 , 0xff , 0x01 , 0x0d , 0x46 , 0x49 , 0x52 , 0x53 , 0x54 , 0x5f ,
4143 0x53 , 0x51 , 0x55 , 0x45 , 0x45 , 0x5a , 0x45 , 0x02 , 0x08 , 0x32 , 0x30 , 0x31 ,
4244 0x32 , 0x33 , 0x34 , 0x35 , 0x36 , 0x03 , 0x08 , 0x53 , 0x59 , 0x53 , 0x41 , 0x31 ,
@@ -59,7 +61,7 @@ EepromData kEepromV5WrongCrc = {
5961
6062// Based on the Spec for V6 EEPROM:
6163// https://github.com/facebook/fboss/blob/main/fboss/docs/meta_eeprom_format_v6.md
62- EepromData kEepromV6 = {
64+ const EepromData kEepromV6 = {
6365 0xfb , 0xfb , 0x06 , 0xff , 0x01 , 0x0d , 0x46 , 0x49 , 0x52 , 0x53 , 0x54 , 0x5f ,
6466 0x53 , 0x51 , 0x55 , 0x45 , 0x45 , 0x5a , 0x45 , 0x02 , 0x08 , 0x32 , 0x30 , 0x31 ,
6567 0x32 , 0x33 , 0x34 , 0x35 , 0x36 , 0x03 , 0x08 , 0x53 , 0x59 , 0x53 , 0x41 , 0x31 ,
@@ -82,6 +84,14 @@ EepromData kEepromV6 = {
8284 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff ,
8385 0xff };
8486
87+ FbossEepromInterface createFbossEepromInterface (const EepromData& data) {
88+ folly::test::TemporaryDirectory tmpDir = folly::test::TemporaryDirectory ();
89+ std::string fileName = tmpDir.path ().string () + " /eepromContent" ;
90+ folly::writeFile (data, fileName.c_str ());
91+
92+ return FbossEepromInterface (fileName, 0 );
93+ }
94+
8595constexpr auto kProductName = " FIRST_SQUEEZE" ;
8696constexpr auto kProductPartNumber = " 20123456" ;
8797constexpr auto kSystemAssemblyPartNumber = " SYSA1234" ;
@@ -106,10 +116,9 @@ constexpr auto kRma = "1";
106116constexpr auto kVendorDefinedField1 = " 0x0101010101" ;
107117constexpr auto kVendorDefinedField2 = " 0x48656c6c6f" ;
108118constexpr auto kVendorDefinedField3 = " " ;
109- constexpr auto kCrc16V5 = " 0xd5c6" ;
110- constexpr auto kCrc16V6 = " 0x4a05" ;
111- constexpr auto kCrcCorrectTemplate = " {} (CRC Matched)" ;
112- constexpr auto kCrc16WrongTemplate = " 0xa6b7 (CRC Mismatch. Expected {})" ;
119+ constexpr auto kCrc16V5 = " 0xd5c6 (CRC Matched)" ;
120+ constexpr auto kCrc16V6 = " 0x4a05 (CRC Matched)" ;
121+ constexpr auto kCrc16V5Wrong = " 0xa6b7 (CRC Mismatch. Expected 0xd5c6)" ;
113122
114123EepromContents createEepromContents (int version, bool crcMatched = true ) {
115124 EepromContents result;
@@ -134,15 +143,13 @@ EepromContents createEepromContents(int version, bool crcMatched = true) {
134143 result.bmcMac () = kBmcMac ;
135144 result.switchAsicMac () = kSwitchAsicMac ;
136145 result.metaReservedMac () = kMetaReservedMac ;
137- const std::string crc16 = version == 5 ? kCrc16V5 : kCrc16V6 ;
138146
139- if (crcMatched ) {
140- result.crc16 () = fmt::format ( kCrcCorrectTemplate , crc16) ;
141- } else {
142- result.crc16 () = fmt::format ( kCrc16WrongTemplate , crc16) ;
147+ if (version == 5 ) {
148+ result.crc16 () = crcMatched ? kCrc16V5 : kCrc16V5Wrong ;
149+ } else if (version == 6 ) {
150+ result.crc16 () = kCrc16V6 ;
143151 }
144152
145- // V6 unique fields
146153 if (version == 6 ) {
147154 result.rma () = kRma ;
148155 result.vendorDefinedField1 () = kVendorDefinedField1 ;
@@ -153,94 +160,62 @@ EepromContents createEepromContents(int version, bool crcMatched = true) {
153160 return result;
154161};
155162
156- struct CommonEepromFields {
157- std::string productName;
158- std::string productPartNumber;
159- std::string productionState;
160- std::string productionSubState;
161- std::string variantIndicator;
162- std::string productSerialNumber;
163-
164- bool operator ==(const CommonEepromFields& other) const = default ;
165- };
166-
167- CommonEepromFields getCommonFields (const FbossEepromInterface& eeprom) {
168- return CommonEepromFields{
169- .productName = eeprom.getProductName (),
170- .productPartNumber = eeprom.getProductPartNumber (),
171- .productionState = eeprom.getProductionState (),
172- .productionSubState = eeprom.getProductionSubState (),
173- .variantIndicator = eeprom.getVariantVersion (),
174- .productSerialNumber = eeprom.getProductSerialNumber (),
175- };
176- }
177-
178- const CommonEepromFields kExpectedCommonFields {
179- .productName = kProductName ,
180- .productPartNumber = kProductPartNumber ,
181- .productionState = kProductionState ,
182- .productionSubState = kProductionSubState ,
183- .variantIndicator = kVariantIndicator ,
184- .productSerialNumber = kProductSerialNumber ,
185- };
186-
187- // Helper to verify common EEPROM fields parsed correctly
188- void verifyCommonEepromFields (const FbossEepromInterface& eeprom) {
189- EXPECT_EQ (getCommonFields (eeprom), kExpectedCommonFields );
190- }
191-
192163} // namespace
193164
194- TEST (FbossEepromInterfaceTest, V5WithBufferConstructor) {
195- FbossEepromInterface eeprom (kEepromV5 );
196- EXPECT_EQ (eeprom.getVersion (), 5 );
197- verifyCommonEepromFields (eeprom);
165+ TEST (FbossEepromInterfaceTest, V5) {
166+ auto eeprom = createFbossEepromInterface (kEepromV5 );
167+
168+ EXPECT_EQ (eeprom.getProductName (), kProductName );
169+ EXPECT_EQ (eeprom.getProductPartNumber (), kProductPartNumber );
170+ EXPECT_EQ (eeprom.getProductionState (), kProductionState );
171+ EXPECT_EQ (eeprom.getProductionSubState (), kProductionSubState );
172+ EXPECT_EQ (eeprom.getVariantVersion (), kVariantIndicator );
173+ EXPECT_EQ (eeprom.getProductSerialNumber (), kProductSerialNumber );
198174}
199175
200176TEST (FbossEepromInterfaceTest, V5WrongCRC) {
201- FbossEepromInterface eeprom (kEepromV5WrongCrc );
202- EXPECT_EQ (eeprom.getVersion (), 5 );
203- verifyCommonEepromFields (eeprom);
177+ auto eeprom = createFbossEepromInterface (kEepromV5WrongCrc );
178+ EXPECT_EQ (eeprom.getProductName (), kProductName );
179+ EXPECT_EQ (eeprom.getProductPartNumber (), kProductPartNumber );
180+ EXPECT_EQ (eeprom.getProductionState (), kProductionState );
181+ EXPECT_EQ (eeprom.getProductionSubState (), kProductionSubState );
182+ EXPECT_EQ (eeprom.getVariantVersion (), kVariantIndicator );
183+ EXPECT_EQ (eeprom.getProductSerialNumber (), kProductSerialNumber );
204184}
205185
206- TEST (FbossEepromInterfaceTest, V6WithBufferConstructor) {
207- FbossEepromInterface eeprom (kEepromV6 );
208- EXPECT_EQ (eeprom.getVersion (), 6 );
209- verifyCommonEepromFields (eeprom);
186+ TEST (FbossEepromInterfaceTest, V6) {
187+ auto eeprom = createFbossEepromInterface (kEepromV6 );
188+ EXPECT_EQ (eeprom.getProductName (), kProductName );
189+ EXPECT_EQ (eeprom.getProductPartNumber (), kProductPartNumber );
190+ EXPECT_EQ (eeprom.getProductionState (), kProductionState );
191+ EXPECT_EQ (eeprom.getProductionSubState (), kProductionSubState );
192+ EXPECT_EQ (eeprom.getVariantVersion (), kVariantIndicator );
193+ EXPECT_EQ (eeprom.getProductSerialNumber (), kProductSerialNumber );
210194}
211195
212196TEST (FbossEepromInterfaceTest, V5Object) {
213- FbossEepromInterface eepromInterface (kEepromV5 );
214- auto actualObj = eepromInterface.getEepromContents ();
197+ auto eepromInterace = createFbossEepromInterface (kEepromV5 );
198+ auto actualObj = eepromInterace.getEepromContents ();
199+
215200 EepromContents expectedObj = createEepromContents (5 );
216201
217202 EXPECT_EQ (actualObj, expectedObj);
218203}
219204
220205TEST (FbossEepromInterfaceTest, V6Object) {
221- FbossEepromInterface eepromInterface (kEepromV6 );
222- auto actualObj = eepromInterface .getEepromContents ();
206+ auto eepromInterace = createFbossEepromInterface (kEepromV6 );
207+ auto actualObj = eepromInterace .getEepromContents ();
223208 EepromContents expectedObj = createEepromContents (6 );
224209
225210 EXPECT_EQ (actualObj, expectedObj);
226211}
227212
228213TEST (FbossEepromInterfaceTest, V5ObjWrongCrc) {
229- FbossEepromInterface eeprom (kEepromV5WrongCrc );
214+ auto eeprom = createFbossEepromInterface (kEepromV5WrongCrc );
230215 auto actualObj = eeprom.getEepromContents ();
231216 EepromContents expectedObj = createEepromContents (5 , false );
232217
233218 EXPECT_EQ (actualObj, expectedObj);
234219}
235220
236- TEST (FbossEepromInterfaceTest, InvalidEepromSize) {
237- std::vector<uint8_t > tooSmall = {0xfb , 0xfb };
238- EXPECT_THROW ((FbossEepromInterface{tooSmall}), std::runtime_error);
239- }
240-
241- TEST (FbossEepromInterfaceTest, InvalidVersion) {
242- std::vector<uint8_t > badVersion = {0xfb , 0xfb , 0x99 , 0xff };
243- EXPECT_THROW ((FbossEepromInterface{badVersion}), std::runtime_error);
244- }
245-
246221} // namespace facebook::fboss::platform
0 commit comments