|
1 | 1 | package collector |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "testing" |
5 | 6 |
|
6 | 7 | "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
7 | 9 | ) |
8 | 10 |
|
9 | 11 | func TestSmartInfo_Capacity(t *testing.T) { |
@@ -31,3 +33,92 @@ func TestSmartInfo_Capacity(t *testing.T) { |
31 | 33 | assert.Zero(t, smartInfo.Capacity()) |
32 | 34 | }) |
33 | 35 | } |
| 36 | + |
| 37 | +func TestSmartInfo_LargeLBAValues(t *testing.T) { |
| 38 | + // Test for GitHub issue #24 / upstream issue #800 |
| 39 | + // LBA values can be large unsigned 64-bit integers that overflow signed int |
| 40 | + t.Run("should parse large LBA values in selective self-test log", func(t *testing.T) { |
| 41 | + // This JSON contains LBA values that exceed int64 max (9223372036854775807) |
| 42 | + // Value 18446743534724713985 is a valid uint64 but overflows int64 |
| 43 | + jsonData := `{ |
| 44 | + "ata_smart_selective_self_test_log": { |
| 45 | + "revision": 1, |
| 46 | + "table": [ |
| 47 | + { |
| 48 | + "lba_min": 18446743534724713985, |
| 49 | + "lba_max": 7205816247684983039, |
| 50 | + "status": { |
| 51 | + "value": 0, |
| 52 | + "string": "Not_testing" |
| 53 | + } |
| 54 | + } |
| 55 | + ], |
| 56 | + "flags": { |
| 57 | + "value": 0, |
| 58 | + "remainder_scan_enabled": false |
| 59 | + }, |
| 60 | + "power_up_scan_resume_minutes": 0 |
| 61 | + } |
| 62 | + }` |
| 63 | + |
| 64 | + var smartInfo SmartInfo |
| 65 | + err := json.Unmarshal([]byte(jsonData), &smartInfo) |
| 66 | + require.NoError(t, err, "should unmarshal large LBA values without error") |
| 67 | + |
| 68 | + // Verify the values were parsed correctly |
| 69 | + require.Len(t, smartInfo.AtaSmartSelectiveSelfTestLog.Table, 1) |
| 70 | + assert.Equal(t, uint64(18446743534724713985), smartInfo.AtaSmartSelectiveSelfTestLog.Table[0].LbaMin) |
| 71 | + assert.Equal(t, uint64(7205816247684983039), smartInfo.AtaSmartSelectiveSelfTestLog.Table[0].LbaMax) |
| 72 | + }) |
| 73 | + |
| 74 | + t.Run("should parse large LBA values in error log", func(t *testing.T) { |
| 75 | + // LBA values in error logs can also be large |
| 76 | + jsonData := `{ |
| 77 | + "ata_smart_error_log": { |
| 78 | + "summary": { |
| 79 | + "revision": 1, |
| 80 | + "count": 1, |
| 81 | + "logged_count": 1, |
| 82 | + "table": [ |
| 83 | + { |
| 84 | + "error_number": 1, |
| 85 | + "lifetime_hours": 1000, |
| 86 | + "completion_registers": { |
| 87 | + "error": 0, |
| 88 | + "status": 0, |
| 89 | + "count": 0, |
| 90 | + "lba": 18446744073709551615, |
| 91 | + "device": 0 |
| 92 | + }, |
| 93 | + "error_description": "test", |
| 94 | + "previous_commands": [ |
| 95 | + { |
| 96 | + "registers": { |
| 97 | + "command": 0, |
| 98 | + "features": 0, |
| 99 | + "count": 0, |
| 100 | + "lba": 18446744073709551615, |
| 101 | + "device": 0, |
| 102 | + "device_control": 0 |
| 103 | + }, |
| 104 | + "powerup_milliseconds": 0, |
| 105 | + "command_name": "test" |
| 106 | + } |
| 107 | + ] |
| 108 | + } |
| 109 | + ] |
| 110 | + } |
| 111 | + } |
| 112 | + }` |
| 113 | + |
| 114 | + var smartInfo SmartInfo |
| 115 | + err := json.Unmarshal([]byte(jsonData), &smartInfo) |
| 116 | + require.NoError(t, err, "should unmarshal large LBA values in error log without error") |
| 117 | + |
| 118 | + // Verify the values were parsed correctly |
| 119 | + require.Len(t, smartInfo.AtaSmartErrorLog.Summary.Table, 1) |
| 120 | + assert.Equal(t, uint64(18446744073709551615), smartInfo.AtaSmartErrorLog.Summary.Table[0].CompletionRegisters.Lba) |
| 121 | + require.Len(t, smartInfo.AtaSmartErrorLog.Summary.Table[0].PreviousCommands, 1) |
| 122 | + assert.Equal(t, uint64(18446744073709551615), smartInfo.AtaSmartErrorLog.Summary.Table[0].PreviousCommands[0].Registers.Lba) |
| 123 | + }) |
| 124 | +} |
0 commit comments