From ee16f8f5a9566d783386282077ba80cc9a21c437 Mon Sep 17 00:00:00 2001
From: "Richard T. Carback III" <rick.carback@gmail.com>
Date: Tue, 19 Dec 2023 19:07:20 +0000
Subject: [PATCH] Fix issue with test using DeepEqual in backup module. The
 data structure should be a plain old object (no underlying structures) but it
 was too big of a fix to correct. Instead, we just encrypt with the same
 RNG/key/etc to make sure we get the same result after a round trip of
 encrypt->decrypt. There might be issues with serialization that this doesn't
 catch but we are at least checking that it's reproducible.

---
 backup/backup_test.go | 45 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 44 insertions(+), 1 deletion(-)

diff --git a/backup/backup_test.go b/backup/backup_test.go
index cdd6e8562..8a29a67f3 100644
--- a/backup/backup_test.go
+++ b/backup/backup_test.go
@@ -13,6 +13,7 @@ import (
 	"testing"
 	"time"
 
+	"github.com/stretchr/testify/require"
 	"gitlab.com/elixxir/client/v4/xxdk"
 
 	"gitlab.com/elixxir/client/v4/collective/versioned"
@@ -175,7 +176,26 @@ func TestBackup_TriggerBackup(t *testing.T) {
 		err := receivedCollatedBackup.Decrypt(password, r)
 		if err != nil {
 			t.Errorf("Failed to decrypt collated backup: %+v", err)
-		} else if !reflect.DeepEqual(collatedBackup, receivedCollatedBackup) {
+		}
+		// Because of the pointers, a direct DeepEqual doesn't work, so
+		// we will check by encrypting with the same RNG and key instead
+		rng := NewCountingReader()
+		key := make([]byte, keyLen)
+		salt := make([]byte, saltLen)
+		rng.Read(key)
+		rng.Read(salt)
+
+		p := backup.DefaultParams()
+
+		expected, err := collatedBackup.Encrypt(NewCountingReader(),
+			key, salt, p)
+		require.NoError(t, err)
+		received, err := receivedCollatedBackup.Encrypt(
+			NewCountingReader(),
+			key, salt, p)
+		require.NoError(t, err)
+
+		if !reflect.DeepEqual(expected, received) {
 			t.Errorf("Unexpected decrypted collated backup."+
 				"\nexpected: %#v\nreceived: %#v",
 				collatedBackup, receivedCollatedBackup)
@@ -424,3 +444,26 @@ func Benchmark_InitializeBackup(t *testing.B) {
 		}
 	}
 }
+
+// CountingReader is a platform-independent deterministic RNG that adheres to
+// io.Reader.
+type CountingReader struct {
+	count uint8
+}
+
+func NewCountingReader() csprng.Source {
+	return &CountingReader{count: 0}
+}
+
+// Read just counts until 254 then starts over again
+func (c *CountingReader) Read(b []byte) (int, error) {
+	for i := 0; i < len(b); i++ {
+		c.count = (c.count + 1) % 255
+		b[i] = c.count
+	}
+	return len(b), nil
+}
+
+func (c *CountingReader) SetSeed(s []byte) error {
+	return nil
+}
-- 
GitLab