|
| 1 | +package address |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/hex" |
| 6 | + "github.com/btcsuite/btcd/btcec/v2" |
| 7 | + "github.com/btcsuite/btcd/btcutil" |
| 8 | + "github.com/lightninglabs/loop/loopdb" |
| 9 | + "github.com/lightninglabs/loop/swapserverrpc" |
| 10 | + "github.com/lightninglabs/loop/test" |
| 11 | + "github.com/stretchr/testify/mock" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + "google.golang.org/grpc" |
| 14 | + "testing" |
| 15 | +) |
| 16 | + |
| 17 | +var ( |
| 18 | + defaultAddresssId = mustDecodeID("17cecc61ab4aafebdc0542dabdae0d0cb8907ec1c9c8ae387bc5a3309ca8b600") |
| 19 | + |
| 20 | + defaultPubkeyBytes, _ = hex.DecodeString("021c97a90a411ff2b10dc2a8e32de2f29d2fa49d41bfbb52bd416e460db0747d0d") |
| 21 | + |
| 22 | + defaultPubkey, _ = btcec.ParsePubKey(defaultPubkeyBytes) |
| 23 | + |
| 24 | + defaultValue = btcutil.Amount(100) |
| 25 | + |
| 26 | + defaultExpiry = uint32(100) |
| 27 | +) |
| 28 | + |
| 29 | +type mockStaticAddressClient struct { |
| 30 | + mock.Mock |
| 31 | +} |
| 32 | + |
| 33 | +type mockSwapServerClient struct { |
| 34 | + mock.Mock |
| 35 | +} |
| 36 | + |
| 37 | +type mockSwapClient struct { |
| 38 | + mock.Mock |
| 39 | + Server *mockSwapServerClient |
| 40 | +} |
| 41 | + |
| 42 | +func (m *mockStaticAddressClient) ServerNewAddress(ctx context.Context, |
| 43 | + in *swapserverrpc.ServerNewAddressRequest, opts ...grpc.CallOption) ( |
| 44 | + *swapserverrpc.ServerNewAddressResponse, error) { |
| 45 | + |
| 46 | + args := m.Called(ctx, in, opts) |
| 47 | + return args.Get(0).(*swapserverrpc.ServerNewAddressResponse), |
| 48 | + args.Error(1) |
| 49 | +} |
| 50 | + |
| 51 | +func (m *mockSwapServerClient) FetchL402(ctx context.Context, |
| 52 | + in *swapserverrpc.FetchL402Request, |
| 53 | + opts ...grpc.CallOption) (*swapserverrpc.FetchL402Response, error) { |
| 54 | + |
| 55 | + args := m.Called(ctx, in, opts) |
| 56 | + |
| 57 | + return args.Get(0).(*swapserverrpc.FetchL402Response), |
| 58 | + args.Error(1) |
| 59 | +} |
| 60 | + |
| 61 | +func TestManager(t *testing.T) { |
| 62 | + ctxb, cancel := context.WithCancel(context.Background()) |
| 63 | + defer cancel() |
| 64 | + |
| 65 | + testContext := newManagerTestContext(t) |
| 66 | + |
| 67 | + // Start the manager. |
| 68 | + go func() { |
| 69 | + err := testContext.manager.Run(ctxb) |
| 70 | + require.NoError(t, err) |
| 71 | + }() |
| 72 | + |
| 73 | + // Create a new reservation. |
| 74 | + _, err := testContext.manager.NewAddress(ctxb) |
| 75 | + require.NoError(t, err) |
| 76 | +} |
| 77 | + |
| 78 | +// ManagerTestContext is a helper struct that contains all the necessary |
| 79 | +// components to test the reservation manager. |
| 80 | +type ManagerTestContext struct { |
| 81 | + manager *Manager |
| 82 | + context test.Context |
| 83 | + mockLnd *test.LndMockServices |
| 84 | + reservationNotificationChan chan *swapserverrpc.ServerReservationNotification |
| 85 | + mockStaticAddressClient *mockStaticAddressClient |
| 86 | +} |
| 87 | + |
| 88 | +// newManagerTestContext creates a new test context for the reservation manager. |
| 89 | +func newManagerTestContext(t *testing.T) *ManagerTestContext { |
| 90 | + mockLnd := test.NewMockLnd() |
| 91 | + lndContext := test.NewContext(t, mockLnd) |
| 92 | + |
| 93 | + dbFixture := loopdb.NewTestDB(t) |
| 94 | + |
| 95 | + store := NewSqlStore(dbFixture.BaseDB) |
| 96 | + |
| 97 | + mockStaticAddressClient := new(mockStaticAddressClient) |
| 98 | + mockSwapClient := new(mockSwapClient) |
| 99 | + |
| 100 | + sendChan := make(chan *swapserverrpc.ServerReservationNotification) |
| 101 | + |
| 102 | + mockStaticAddressClient.On( |
| 103 | + "ReservationNotificationStream", mock.Anything, mock.Anything, |
| 104 | + mock.Anything, |
| 105 | + ).Return( |
| 106 | + &dummyReservationNotificationServer{ |
| 107 | + SendChan: sendChan, |
| 108 | + }, nil, |
| 109 | + ) |
| 110 | + |
| 111 | + mockStaticAddressClient.On( |
| 112 | + "OpenReservation", mock.Anything, mock.Anything, mock.Anything, |
| 113 | + ).Return( |
| 114 | + &swapserverrpc.ServerOpenReservationResponse{}, nil, |
| 115 | + ) |
| 116 | + |
| 117 | + cfg := &ManagerConfig{ |
| 118 | + SwapClient: mockSwapClient, |
| 119 | + Store: store, |
| 120 | + WalletKit: mockLnd.WalletKit, |
| 121 | + ChainParams: mockLnd.ChainParams, |
| 122 | + AddressClient: mockStaticAddressClient, |
| 123 | + } |
| 124 | + |
| 125 | + manager := NewManager(cfg) |
| 126 | + |
| 127 | + return &ManagerTestContext{ |
| 128 | + manager: manager, |
| 129 | + context: lndContext, |
| 130 | + mockLnd: mockLnd, |
| 131 | + mockStaticAddressClient: mockStaticAddressClient, |
| 132 | + reservationNotificationChan: sendChan, |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +type dummyReservationNotificationServer struct { |
| 137 | + grpc.ClientStream |
| 138 | + |
| 139 | + // SendChan is the channel that is used to send notifications. |
| 140 | + SendChan chan *swapserverrpc.ServerReservationNotification |
| 141 | +} |
| 142 | + |
| 143 | +func (d *dummyReservationNotificationServer) Recv() ( |
| 144 | + *swapserverrpc.ServerReservationNotification, error) { |
| 145 | + |
| 146 | + return <-d.SendChan, nil |
| 147 | +} |
| 148 | + |
| 149 | +func mustDecodeID(id string) []byte { |
| 150 | + bytes, err := hex.DecodeString(id) |
| 151 | + if err != nil { |
| 152 | + panic(err) |
| 153 | + } |
| 154 | + var decoded []byte |
| 155 | + copy(decoded[:], bytes) |
| 156 | + return decoded |
| 157 | +} |
0 commit comments