@@ -204,3 +204,64 @@ func TestCollectDeviceData_linux(t *testing.T) {
204204 })
205205 }
206206}
207+
208+ func TestParseDMIReadOutput (t * testing.T ) {
209+ const validDMIJson = `{"ProductName":"21J50013US","ProductSerial":"PF0A0AAA","BoardSerial":"L1AA00A00A0","ChassisAssetTag":"SomeAssetTag"}`
210+ testCases := []struct {
211+ name string
212+ input []byte
213+ checkError require.ErrorAssertionFunc
214+ expected * linux.DMIInfo
215+ }{
216+ {
217+ name : "clean" ,
218+ input : []byte (validDMIJson ),
219+ checkError : require .NoError ,
220+ expected : & linux.DMIInfo {
221+ ProductName : "21J50013US" ,
222+ ProductSerial : "PF0A0AAA" ,
223+ BoardSerial : "L1AA00A00A0" ,
224+ ChassisAssetTag : "SomeAssetTag" ,
225+ },
226+ }, {
227+ name : "prefixed" ,
228+ input : []byte ("somerandomgibberish" + validDMIJson ),
229+ checkError : require .NoError ,
230+ expected : & linux.DMIInfo {
231+ ProductName : "21J50013US" ,
232+ ProductSerial : "PF0A0AAA" ,
233+ BoardSerial : "L1AA00A00A0" ,
234+ ChassisAssetTag : "SomeAssetTag" ,
235+ },
236+ }, {
237+ name : "invalid utf8 prefix" ,
238+ input : []byte ("\xe2 \x80 \x83 " + validDMIJson ),
239+ checkError : require .NoError ,
240+ expected : & linux.DMIInfo {
241+ ProductName : "21J50013US" ,
242+ ProductSerial : "PF0A0AAA" ,
243+ BoardSerial : "L1AA00A00A0" ,
244+ ChassisAssetTag : "SomeAssetTag" ,
245+ },
246+ }, {
247+ name : "invalid json" ,
248+ input : []byte ("{" + validDMIJson ),
249+ checkError : require .Error ,
250+ }, {
251+ name : "empty input" ,
252+ input : []byte {},
253+ checkError : require .Error ,
254+ }, {
255+ name : "nil input" ,
256+ input : nil ,
257+ checkError : require .Error ,
258+ },
259+ }
260+ for _ , testCase := range testCases {
261+ t .Run (testCase .name , func (t * testing.T ) {
262+ out , err := parseDMIReadOutput (testCase .input )
263+ testCase .checkError (t , err )
264+ require .Equal (t , testCase .expected , out )
265+ })
266+ }
267+ }
0 commit comments