|
| 1 | +package db_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + consensusdb "github.com/NethermindEth/juno/consensus/db" |
| 9 | + "github.com/NethermindEth/juno/consensus/types" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +const testPruneWatermarkHeight = types.Height(1) |
| 14 | + |
| 15 | +func TestPruneWatermarkRoundTrips(t *testing.T) { |
| 16 | + walDir := t.TempDir() |
| 17 | + |
| 18 | + height, err := consensusdb.LoadPruneWatermark(walDir) |
| 19 | + require.NoError(t, err) |
| 20 | + require.Zero(t, height) |
| 21 | + |
| 22 | + require.NoError(t, consensusdb.WritePruneWatermark(walDir, testPruneWatermarkHeight)) |
| 23 | + height, err = consensusdb.LoadPruneWatermark(walDir) |
| 24 | + require.NoError(t, err) |
| 25 | + require.Equal(t, testPruneWatermarkHeight, height) |
| 26 | +} |
| 27 | + |
| 28 | +func TestLoadPruneWatermarkRejectsCorruptFile(t *testing.T) { |
| 29 | + walDir := t.TempDir() |
| 30 | + require.NoError(t, consensusdb.WritePruneWatermark(walDir, testPruneWatermarkHeight)) |
| 31 | + validWatermark, err := os.ReadFile(filepath.Join(walDir, "prune-watermark")) |
| 32 | + require.NoError(t, err) |
| 33 | + |
| 34 | + wrongHeader := make([]byte, len(validWatermark)) |
| 35 | + copy(wrongHeader, "wrong-header") |
| 36 | + |
| 37 | + tests := map[string][]byte{ |
| 38 | + "wrong size": []byte("short"), |
| 39 | + "bad header": wrongHeader, |
| 40 | + } |
| 41 | + |
| 42 | + for name, contents := range tests { |
| 43 | + t.Run(name, func(t *testing.T) { |
| 44 | + walDir := t.TempDir() |
| 45 | + path := filepath.Join(walDir, "prune-watermark") |
| 46 | + require.NoError(t, os.WriteFile(path, contents, 0o644)) |
| 47 | + |
| 48 | + _, err := consensusdb.LoadPruneWatermark(walDir) |
| 49 | + require.Error(t, err) |
| 50 | + }) |
| 51 | + } |
| 52 | +} |
0 commit comments