decred.org/dcrdex@v1.0.5/client/db/test/types_test.go (about)

     1  package dbtest
     2  
     3  import (
     4  	"bytes"
     5  	"os"
     6  	"testing"
     7  	"time"
     8  
     9  	"decred.org/dcrdex/client/db"
    10  )
    11  
    12  func TestAccountInfo(t *testing.T) {
    13  	spins := 10000
    14  	if testing.Short() {
    15  		spins = 1000
    16  	}
    17  	tStart := time.Now()
    18  	nTimes(spins, func(i int) {
    19  		ai := RandomAccountInfo()
    20  		aiB := ai.Encode()
    21  		reAI, err := db.DecodeAccountInfo(aiB)
    22  		if err != nil {
    23  			t.Fatalf("error decoding AccountInfo: %v", err)
    24  		}
    25  		MustCompareAccountInfo(t, ai, reAI)
    26  	})
    27  	t.Logf("encoded, decoded, and compared %d AccountInfo in %d ms", spins, time.Since(tStart)/time.Millisecond)
    28  }
    29  
    30  func TestMatchProof(t *testing.T) {
    31  	spins := 10000
    32  	if testing.Short() {
    33  		spins = 1000
    34  	}
    35  	proofs := make([]*db.MatchProof, 0, spins)
    36  	// Generate proofs with an average of 20% sparsity. Empty fields should not
    37  	// affect accurate encoding/decoding.
    38  	nTimes(spins, func(int) { proofs = append(proofs, RandomMatchProof(0.4)) })
    39  	tStart := time.Now()
    40  	nTimes(spins, func(i int) {
    41  		proof := proofs[i]
    42  		proofB := proof.Encode()
    43  		reProof, ver, err := db.DecodeMatchProof(proofB)
    44  		if err != nil {
    45  			t.Fatalf("match decode error: %v", err)
    46  		}
    47  		MustCompareMatchProof(t, proof, reProof)
    48  		if ver != db.MatchProofVer {
    49  			t.Errorf("wanted match proof ver %d, got %d", db.MatchProofVer, ver)
    50  		}
    51  	})
    52  	t.Logf("encoded, decoded, and compared %d MatchProof in %d ms", spins, time.Since(tStart)/time.Millisecond)
    53  }
    54  
    55  func TestOrderProof(t *testing.T) {
    56  	spins := 10000
    57  	if testing.Short() {
    58  		spins = 1000
    59  	}
    60  	proofs := make([]*db.OrderProof, 0, spins)
    61  	nTimes(spins, func(int) {
    62  		proofs = append(proofs, &db.OrderProof{
    63  			DEXSig:   randBytes(73),
    64  			Preimage: randBytes(32),
    65  		})
    66  	})
    67  	tStart := time.Now()
    68  	nTimes(spins, func(i int) {
    69  		proof := proofs[i]
    70  		proofB := proof.Encode()
    71  		reProof, err := db.DecodeOrderProof(proofB)
    72  		if err != nil {
    73  			t.Fatalf("decode error: %v", err)
    74  		}
    75  		MustCompareOrderProof(t, proof, reProof)
    76  	})
    77  	t.Logf("encoded, decoded, and compared %d OrderProof in %d ms", spins, time.Since(tStart)/time.Millisecond)
    78  }
    79  
    80  func nTimes(n int, f func(int)) {
    81  	for i := 0; i < n; i++ {
    82  		f(i)
    83  	}
    84  }
    85  
    86  func TestAccountsBackupAndRestore(t *testing.T) {
    87  	keyParams := randBytes(128)
    88  	count := 5
    89  	accts := make(map[string]*db.AccountInfo, count)
    90  	for i := 0; i < count; i++ {
    91  		acct := RandomAccountInfo()
    92  		accts[acct.Host] = acct
    93  	}
    94  
    95  	bkp := db.AccountBackup{
    96  		KeyParams: keyParams,
    97  	}
    98  	for _, acct := range accts {
    99  		bkp.Accounts = append(bkp.Accounts, acct)
   100  	}
   101  
   102  	// Save the account backup to file.
   103  	backupPath := "acct.backup"
   104  	err := bkp.Save(backupPath)
   105  	if err != nil {
   106  		t.Fatalf("[Save] unexpected error: %v", err)
   107  	}
   108  
   109  	defer os.Remove(backupPath)
   110  
   111  	// Restore accounts from backup.
   112  	restored, err := db.RestoreAccountBackup(backupPath)
   113  	if err != nil {
   114  		t.Fatalf("[RestoreAccountBackup] unexpected error: %v", err)
   115  	}
   116  
   117  	// Ensure restored account details are identical to the original.
   118  	if !bytes.Equal(restored.KeyParams, bkp.KeyParams) {
   119  		t.Fatalf("expected key params value of %x, got %x",
   120  			bkp.KeyParams, restored.KeyParams)
   121  	}
   122  
   123  	if len(restored.Accounts) != len(bkp.Accounts) {
   124  		t.Fatalf("expected %d restored accounts, got %x",
   125  			count, len(restored.Accounts))
   126  	}
   127  
   128  	for _, dexAcct := range restored.Accounts {
   129  		acct, ok := accts[dexAcct.Host]
   130  		if !ok {
   131  			t.Fatalf("no account found with url %s", dexAcct.Host)
   132  		}
   133  
   134  		if !bytes.Equal(dexAcct.LegacyEncKey, acct.LegacyEncKey) {
   135  			t.Fatalf("expected restored account %s with encryption key %x, "+
   136  				"got %x", dexAcct.Host, dexAcct.LegacyEncKey, acct.LegacyEncKey)
   137  		}
   138  		if !bytes.Equal(dexAcct.EncKeyV2, acct.EncKeyV2) {
   139  			t.Fatalf("expected restored account %s (v2) with encryption key %x, "+
   140  				"got %x", dexAcct.Host, dexAcct.EncKeyV2, acct.EncKeyV2)
   141  		}
   142  
   143  		if !dexAcct.DEXPubKey.IsEqual(acct.DEXPubKey) {
   144  			t.Fatalf("expected restored account %s with dex public key %x, "+
   145  				"got %x", dexAcct.Host, dexAcct.DEXPubKey.SerializeCompressed(),
   146  				acct.DEXPubKey.SerializeCompressed())
   147  		}
   148  	}
   149  }