// WAL can read entries are not wrapped, but not encrypted func TestReadAllWrappedNoEncryption(t *testing.T) { metadata, entries, snapshot := makeWALData(1, 1) wrappedEntries := make([]raftpb.Entry, len(entries)) for i, entry := range entries { r := api.MaybeEncryptedRecord{Data: entry.Data} data, err := r.Marshal() require.NoError(t, err) entry.Data = data wrappedEntries[i] = entry } tempdir := createWithWAL(t, OriginalWAL, metadata, snapshot, wrappedEntries) defer os.RemoveAll(tempdir) c := NewWALFactory(encryption.NoopCrypter, encryption.NoopCrypter) wrapped, err := c.Open(tempdir, snapshot) require.NoError(t, err) defer wrapped.Close() metaW, _, entsW, err := wrapped.ReadAll() require.NoError(t, err) require.NoError(t, wrapped.Close()) require.Equal(t, metadata, metaW) require.Equal(t, entries, entsW) }
// If decrypting a snapshot fails, the error is propagated func TestSnapshotterLoadDecryptingFail(t *testing.T) { tempdir, err := ioutil.TempDir("", "snapwrap") require.NoError(t, err) defer os.RemoveAll(tempdir) crypter := &meowCrypter{} ogSnap := OriginalSnap.New(tempdir) r := api.MaybeEncryptedRecord{ Data: fakeSnapshotData.Data, Algorithm: crypter.Algorithm(), } data, err := r.Marshal() require.NoError(t, err) emptyEncryptionFakeData := fakeSnapshotData emptyEncryptionFakeData.Data = data require.NoError(t, ogSnap.SaveSnap(emptyEncryptionFakeData)) c := NewSnapFactory(encryption.NoopCrypter, crypter) wrapped := c.New(tempdir) _, err = wrapped.Load() require.Error(t, err) require.Contains(t, err.Error(), "not meowcoded") }
// When reading WAL, if the decrypter can't read the encryption type, errors func TestReadAllNoSupportedDecrypter(t *testing.T) { metadata, entries, snapshot := makeWALData(1, 1) for i, entry := range entries { r := api.MaybeEncryptedRecord{Data: entry.Data, Algorithm: api.MaybeEncryptedRecord_Algorithm(-3)} data, err := r.Marshal() require.NoError(t, err) entries[i].Data = data } tempdir := createWithWAL(t, OriginalWAL, metadata, snapshot, entries) defer os.RemoveAll(tempdir) c := NewWALFactory(encryption.NoopCrypter, encryption.NoopCrypter) wrapped, err := c.Open(tempdir, snapshot) require.NoError(t, err) defer wrapped.Close() _, _, _, err = wrapped.ReadAll() require.Error(t, err) defer wrapped.Close() }
// When reading WAL, if a decrypter is available for the encryption type but any // entry is incorrectly encryptd, an error is returned func TestReadAllEntryIncorrectlyEncrypted(t *testing.T) { crypter := &meowCrypter{} metadata, entries, snapshot := makeWALData(1, 1) // metadata is correctly encryptd, but entries are not meow-encryptd for i, entry := range entries { r := api.MaybeEncryptedRecord{Data: entry.Data, Algorithm: crypter.Algorithm()} data, err := r.Marshal() require.NoError(t, err) entries[i].Data = data } tempdir := createWithWAL(t, OriginalWAL, metadata, snapshot, entries) defer os.RemoveAll(tempdir) c := NewWALFactory(encryption.NoopCrypter, crypter) wrapped, err := c.Open(tempdir, snapshot) require.NoError(t, err) _, _, _, err = wrapped.ReadAll() require.Error(t, err) require.Contains(t, err.Error(), "not meowcoded") require.NoError(t, wrapped.Close()) }
// Snapshotter can read snapshots that are wrapped, but not encrypted func TestSnapshotterLoadNotEncryptedSnapshot(t *testing.T) { tempdir, err := ioutil.TempDir("", "snapwrap") require.NoError(t, err) defer os.RemoveAll(tempdir) ogSnap := OriginalSnap.New(tempdir) r := api.MaybeEncryptedRecord{ Data: fakeSnapshotData.Data, } data, err := r.Marshal() require.NoError(t, err) emptyEncryptionFakeData := fakeSnapshotData emptyEncryptionFakeData.Data = data require.NoError(t, ogSnap.SaveSnap(emptyEncryptionFakeData)) c := NewSnapFactory(encryption.NoopCrypter, encryption.NoopCrypter) wrapped := c.New(tempdir) readSnap, err := wrapped.Load() require.NoError(t, err) require.Equal(t, fakeSnapshotData, *readSnap) }