|
| 1 | +package initialization |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/rand" |
| 5 | + "encoding/json" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + "go.uber.org/zap/zaptest" |
| 12 | +) |
| 13 | + |
| 14 | +func Fuzz_MigrateMetadata(f *testing.F) { |
| 15 | + nodeId := make([]byte, 32) |
| 16 | + rand.Read(nodeId) |
| 17 | + |
| 18 | + commitmentAtxId := make([]byte, 32) |
| 19 | + rand.Read(commitmentAtxId) |
| 20 | + |
| 21 | + f.Add(make([]byte, 32), make([]byte, 32), []byte{1}, uint64(67), uint64(1024), uint64(1024*1024), uint64(1024), uint32(4)) |
| 22 | + f.Add(nodeId, commitmentAtxId, []byte{1, 23}, uint64(128), uint64(1024*1024), uint64(1024*1024*1024), uint64(2389712), uint32(16)) |
| 23 | + |
| 24 | + f.Fuzz(func(t *testing.T, nodeId, commitmentAtxId, nonceValue []byte, nonce, labelsPerUnit, maxFileSize, lastPosition uint64, numUnits uint32) { |
| 25 | + if len(nodeId) != 32 || len(commitmentAtxId) != 32 { |
| 26 | + return |
| 27 | + } |
| 28 | + |
| 29 | + path := t.TempDir() |
| 30 | + |
| 31 | + f, err := os.Create(filepath.Join(path, MetadataFileName)) |
| 32 | + require.NoError(t, err) |
| 33 | + defer f.Close() |
| 34 | + |
| 35 | + old := postMetadataV0{ |
| 36 | + NodeId: nodeId, |
| 37 | + CommitmentAtxId: commitmentAtxId, |
| 38 | + LabelsPerUnit: labelsPerUnit, |
| 39 | + NumUnits: numUnits, |
| 40 | + MaxFileSize: maxFileSize, |
| 41 | + Nonce: &nonce, |
| 42 | + NonceValue: nonceValue, |
| 43 | + LastPosition: &lastPosition, |
| 44 | + } |
| 45 | + |
| 46 | + if len(nonceValue) == 0 { |
| 47 | + old.NonceValue = nil |
| 48 | + old.Nonce = nil |
| 49 | + } |
| 50 | + |
| 51 | + require.NoError(t, json.NewEncoder(f).Encode(old)) |
| 52 | + |
| 53 | + log := zaptest.NewLogger(t) |
| 54 | + require.NoError(t, MigratePoST(path, log)) |
| 55 | + |
| 56 | + metadata, err := LoadMetadata(path) |
| 57 | + require.NoError(t, err) |
| 58 | + |
| 59 | + require.Equal(t, 1, metadata.Version) |
| 60 | + |
| 61 | + require.Equal(t, nodeId, metadata.NodeId) |
| 62 | + require.Equal(t, commitmentAtxId, metadata.CommitmentAtxId) |
| 63 | + require.Equal(t, labelsPerUnit, metadata.LabelsPerUnit) |
| 64 | + require.Equal(t, numUnits, metadata.NumUnits) |
| 65 | + require.Equal(t, maxFileSize, metadata.MaxFileSize) |
| 66 | + if old.NonceValue == nil { |
| 67 | + require.Nil(t, metadata.NonceValue) |
| 68 | + require.Nil(t, metadata.Nonce) |
| 69 | + } else { |
| 70 | + require.Equal(t, old.NonceValue, metadata.NonceValue) |
| 71 | + require.Equal(t, *old.Nonce, *metadata.Nonce) |
| 72 | + } |
| 73 | + require.Equal(t, lastPosition, *metadata.LastPosition) |
| 74 | + |
| 75 | + require.NotNil(t, metadata.Scrypt) |
| 76 | + }) |
| 77 | +} |
| 78 | + |
| 79 | +func Test_Migrate_MissingMetadataFile(t *testing.T) { |
| 80 | + path := t.TempDir() |
| 81 | + log := zaptest.NewLogger(t) |
| 82 | + require.ErrorIs(t, MigratePoST(path, log), ErrStateMetadataFileMissing) |
| 83 | +} |
| 84 | + |
| 85 | +func Test_Migrate_Adds_NonceValue(t *testing.T) { |
| 86 | + nonce := uint64(10) |
| 87 | + old := postMetadataV0{ |
| 88 | + NodeId: make([]byte, 32), |
| 89 | + CommitmentAtxId: make([]byte, 32), |
| 90 | + LabelsPerUnit: 1024, |
| 91 | + NumUnits: 4, |
| 92 | + MaxFileSize: 1024 * 1014, |
| 93 | + Nonce: &nonce, |
| 94 | + NonceValue: nil, |
| 95 | + LastPosition: nil, |
| 96 | + } |
| 97 | + |
| 98 | + path := t.TempDir() |
| 99 | + f, err := os.Create(filepath.Join(path, MetadataFileName)) |
| 100 | + require.NoError(t, err) |
| 101 | + defer f.Close() |
| 102 | + require.NoError(t, json.NewEncoder(f).Encode(old)) |
| 103 | + |
| 104 | + log := zaptest.NewLogger(t) |
| 105 | + require.NoError(t, MigratePoST(path, log)) |
| 106 | + |
| 107 | + metadata, err := LoadMetadata(path) |
| 108 | + require.NoError(t, err) |
| 109 | + |
| 110 | + require.Equal(t, 1, metadata.Version) |
| 111 | + |
| 112 | + require.NotNil(t, metadata.NonceValue) |
| 113 | + require.NotNil(t, metadata.Nonce) |
| 114 | +} |
0 commit comments