Skip to content

Commit 0ea0b7c

Browse files
committed
firewalldb: return consistent errors for priv map tests
In preparation for adding a different implementation of the privacy mapper DB, we make sure that the unit tests are expecting consistent errors.
1 parent ad02aa6 commit 0ea0b7c

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

firewalldb/privacy_mapper.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"crypto/rand"
66
"encoding/binary"
77
"encoding/hex"
8+
"errors"
89
"fmt"
910
"math/big"
1011
"strconv"
@@ -14,6 +15,20 @@ import (
1415
"github.com/lightninglabs/lightning-terminal/session"
1516
)
1617

18+
var (
19+
// ErrDuplicateRealValue is returned when an attempt is made to insert
20+
// a new real-pseudo pair into the db, but the real value already exists
21+
// in the db.
22+
ErrDuplicateRealValue = errors.New("an entry with the given real " +
23+
"value already exists")
24+
25+
// ErrDuplicatePseudoValue is returned when an attempt is made to
26+
// insert a new real-pseudo pair into the db, but the pseudo value
27+
// already exists in the db.
28+
ErrDuplicatePseudoValue = errors.New("an entry with the given pseudo " +
29+
"value already exists")
30+
)
31+
1732
// NewPrivacyMapDB is a function type that takes a group ID and uses it to
1833
// construct a new PrivacyMapDB.
1934
type NewPrivacyMapDB func(groupID session.ID) PrivacyMapDB

firewalldb/privacy_mapper_kvdb.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,12 @@ func (p *privacyMapTx) NewPair(_ context.Context, real, pseudo string) error {
7777
}
7878

7979
if len(realToPseudoBucket.Get([]byte(real))) != 0 {
80-
return fmt.Errorf("an entry already exists for real "+
81-
"value: %x", real)
80+
return fmt.Errorf("%w, real: %v", ErrDuplicateRealValue, real)
8281
}
8382

8483
if len(pseudoToRealBucket.Get([]byte(pseudo))) != 0 {
85-
return fmt.Errorf("an entry already exists for pseudo "+
86-
"value: %x", pseudo)
84+
return fmt.Errorf("%w, pseudo: %v", ErrDuplicatePseudoValue,
85+
pseudo)
8786
}
8887

8988
err = realToPseudoBucket.Put([]byte(real), []byte(pseudo))

firewalldb/privacy_mapper_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,12 @@ func TestPrivacyMapStorage(t *testing.T) {
9696
// Try to add a new pair that has the same real value as the
9797
// first pair. This should fail.
9898
err = tx.NewPair(ctx, "real 1", "pseudo 2")
99-
require.ErrorContains(t, err, "an entry already exists for "+
100-
"real value")
99+
require.ErrorIs(t, err, ErrDuplicateRealValue)
101100

102101
// Try to add a new pair that has the same pseudo value as the
103102
// first pair. This should fail.
104103
err = tx.NewPair(ctx, "real 2", "pseudo 1")
105-
require.ErrorContains(t, err, "an entry already exists for "+
106-
"pseudo value")
104+
require.ErrorIs(t, err, ErrDuplicatePseudoValue)
107105

108106
// Add a few more pairs.
109107
err = tx.NewPair(ctx, "real 2", "pseudo 2")

0 commit comments

Comments
 (0)