itest: extract connection setup
This commit is contained in:
parent
17276e9a7f
commit
9862ee7cd6
@ -8,6 +8,7 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
"github.com/lightningnetwork/lnd/lnrpc"
|
"github.com/lightningnetwork/lnd/lnrpc"
|
||||||
"github.com/lightningnetwork/lnd/lntest"
|
"github.com/lightningnetwork/lnd/lntest"
|
||||||
@ -44,8 +45,8 @@ func testMacaroonAuthentication(net *lntest.NetworkHarness, t *harnessTest) {
|
|||||||
conn, err := testNode.ConnectRPC(false)
|
conn, err := testNode.ConnectRPC(false)
|
||||||
require.NoError(t.t, err)
|
require.NoError(t.t, err)
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
noMacConnection := lnrpc.NewLightningClient(conn)
|
client := lnrpc.NewLightningClient(conn)
|
||||||
_, err = noMacConnection.GetInfo(ctxt, infoReq)
|
_, err = client.GetInfo(ctxt, infoReq)
|
||||||
if err == nil || !errContains(err, "expected 1 macaroon") {
|
if err == nil || !errContains(err, "expected 1 macaroon") {
|
||||||
t.Fatalf("expected to get an error when connecting without " +
|
t.Fatalf("expected to get an error when connecting without " +
|
||||||
"macaroons")
|
"macaroons")
|
||||||
@ -56,11 +57,9 @@ func testMacaroonAuthentication(net *lntest.NetworkHarness, t *harnessTest) {
|
|||||||
[]byte("dummy_root_key"), []byte("0"), "itest",
|
[]byte("dummy_root_key"), []byte("0"), "itest",
|
||||||
macaroon.LatestVersion,
|
macaroon.LatestVersion,
|
||||||
)
|
)
|
||||||
conn, err = testNode.ConnectRPCWithMacaroon(invalidMac)
|
cleanup, client := macaroonClient(t.t, testNode, invalidMac)
|
||||||
require.NoError(t.t, err)
|
defer cleanup()
|
||||||
defer conn.Close()
|
_, err = client.GetInfo(ctxt, infoReq)
|
||||||
invalidMacConnection := lnrpc.NewLightningClient(conn)
|
|
||||||
_, err = invalidMacConnection.GetInfo(ctxt, infoReq)
|
|
||||||
if err == nil || !errContains(err, "cannot get macaroon") {
|
if err == nil || !errContains(err, "cannot get macaroon") {
|
||||||
t.Fatalf("expected to get an error when connecting with an " +
|
t.Fatalf("expected to get an error when connecting with an " +
|
||||||
"invalid macaroon")
|
"invalid macaroon")
|
||||||
@ -71,11 +70,9 @@ func testMacaroonAuthentication(net *lntest.NetworkHarness, t *harnessTest) {
|
|||||||
testNode.ReadMacPath(), defaultTimeout,
|
testNode.ReadMacPath(), defaultTimeout,
|
||||||
)
|
)
|
||||||
require.NoError(t.t, err)
|
require.NoError(t.t, err)
|
||||||
conn, err = testNode.ConnectRPCWithMacaroon(readonlyMac)
|
cleanup, client = macaroonClient(t.t, testNode, readonlyMac)
|
||||||
require.NoError(t.t, err)
|
defer cleanup()
|
||||||
defer conn.Close()
|
_, err = client.NewAddress(ctxt, newAddrReq)
|
||||||
readonlyMacConnection := lnrpc.NewLightningClient(conn)
|
|
||||||
_, err = readonlyMacConnection.NewAddress(ctxt, newAddrReq)
|
|
||||||
if err == nil || !errContains(err, "permission denied") {
|
if err == nil || !errContains(err, "permission denied") {
|
||||||
t.Fatalf("expected to get an error when connecting to " +
|
t.Fatalf("expected to get an error when connecting to " +
|
||||||
"write method with read-only macaroon")
|
"write method with read-only macaroon")
|
||||||
@ -87,11 +84,9 @@ func testMacaroonAuthentication(net *lntest.NetworkHarness, t *harnessTest) {
|
|||||||
readonlyMac, macaroons.TimeoutConstraint(-30),
|
readonlyMac, macaroons.TimeoutConstraint(-30),
|
||||||
)
|
)
|
||||||
require.NoError(t.t, err)
|
require.NoError(t.t, err)
|
||||||
conn, err = testNode.ConnectRPCWithMacaroon(timeoutMac)
|
cleanup, client = macaroonClient(t.t, testNode, timeoutMac)
|
||||||
require.NoError(t.t, err)
|
defer cleanup()
|
||||||
defer conn.Close()
|
_, err = client.GetInfo(ctxt, infoReq)
|
||||||
timeoutMacConnection := lnrpc.NewLightningClient(conn)
|
|
||||||
_, err = timeoutMacConnection.GetInfo(ctxt, infoReq)
|
|
||||||
if err == nil || !errContains(err, "macaroon has expired") {
|
if err == nil || !errContains(err, "macaroon has expired") {
|
||||||
t.Fatalf("expected to get an error when connecting with an " +
|
t.Fatalf("expected to get an error when connecting with an " +
|
||||||
"invalid macaroon")
|
"invalid macaroon")
|
||||||
@ -102,11 +97,9 @@ func testMacaroonAuthentication(net *lntest.NetworkHarness, t *harnessTest) {
|
|||||||
readonlyMac, macaroons.IPLockConstraint("1.1.1.1"),
|
readonlyMac, macaroons.IPLockConstraint("1.1.1.1"),
|
||||||
)
|
)
|
||||||
require.NoError(t.t, err)
|
require.NoError(t.t, err)
|
||||||
conn, err = testNode.ConnectRPCWithMacaroon(invalidIpAddrMac)
|
cleanup, client = macaroonClient(t.t, testNode, invalidIpAddrMac)
|
||||||
require.NoError(t.t, err)
|
defer cleanup()
|
||||||
defer conn.Close()
|
_, err = client.GetInfo(ctxt, infoReq)
|
||||||
invalidIpAddrMacConnection := lnrpc.NewLightningClient(conn)
|
|
||||||
_, err = invalidIpAddrMacConnection.GetInfo(ctxt, infoReq)
|
|
||||||
if err == nil || !errContains(err, "different IP address") {
|
if err == nil || !errContains(err, "different IP address") {
|
||||||
t.Fatalf("expected to get an error when connecting with an " +
|
t.Fatalf("expected to get an error when connecting with an " +
|
||||||
"invalid macaroon")
|
"invalid macaroon")
|
||||||
@ -124,12 +117,10 @@ func testMacaroonAuthentication(net *lntest.NetworkHarness, t *harnessTest) {
|
|||||||
macaroons.IPLockConstraint("127.0.0.1"),
|
macaroons.IPLockConstraint("127.0.0.1"),
|
||||||
)
|
)
|
||||||
require.NoError(t.t, err)
|
require.NoError(t.t, err)
|
||||||
conn, err = testNode.ConnectRPCWithMacaroon(adminMac)
|
cleanup, client = macaroonClient(t.t, testNode, adminMac)
|
||||||
require.NoError(t.t, err)
|
defer cleanup()
|
||||||
defer conn.Close()
|
res, err := client.NewAddress(ctxt, newAddrReq)
|
||||||
adminMacConnection := lnrpc.NewLightningClient(conn)
|
require.NoError(t.t, err, "get new address")
|
||||||
res, err := adminMacConnection.NewAddress(ctxt, newAddrReq)
|
|
||||||
require.NoError(t.t, err)
|
|
||||||
assert.Contains(t.t, res.Address, "bcrt1")
|
assert.Contains(t.t, res.Address, "bcrt1")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -151,11 +142,9 @@ func testBakeMacaroon(net *lntest.NetworkHarness, t *harnessTest) {
|
|||||||
testNode.AdminMacPath(), defaultTimeout,
|
testNode.AdminMacPath(), defaultTimeout,
|
||||||
)
|
)
|
||||||
require.NoError(t.t, err)
|
require.NoError(t.t, err)
|
||||||
conn, err := testNode.ConnectRPCWithMacaroon(adminMac)
|
cleanup, client := macaroonClient(t.t, testNode, adminMac)
|
||||||
require.NoError(t.t, err)
|
defer cleanup()
|
||||||
defer conn.Close()
|
_, err = client.BakeMacaroon(ctxt, req)
|
||||||
adminMacConnection := lnrpc.NewLightningClient(conn)
|
|
||||||
_, err = adminMacConnection.BakeMacaroon(ctxt, req)
|
|
||||||
if err == nil || !errContains(err, "permission list cannot be empty") {
|
if err == nil || !errContains(err, "permission list cannot be empty") {
|
||||||
t.Fatalf("expected an error, got %v", err)
|
t.Fatalf("expected an error, got %v", err)
|
||||||
}
|
}
|
||||||
@ -170,7 +159,7 @@ func testBakeMacaroon(net *lntest.NetworkHarness, t *harnessTest) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
_, err = adminMacConnection.BakeMacaroon(ctxt, req)
|
_, err = client.BakeMacaroon(ctxt, req)
|
||||||
if err == nil || !errContains(err, "invalid permission action") {
|
if err == nil || !errContains(err, "invalid permission action") {
|
||||||
t.Fatalf("expected an error, got %v", err)
|
t.Fatalf("expected an error, got %v", err)
|
||||||
}
|
}
|
||||||
@ -185,7 +174,7 @@ func testBakeMacaroon(net *lntest.NetworkHarness, t *harnessTest) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
_, err = adminMacConnection.BakeMacaroon(ctxt, req)
|
_, err = client.BakeMacaroon(ctxt, req)
|
||||||
if err == nil || !errContains(err, "invalid permission entity") {
|
if err == nil || !errContains(err, "invalid permission entity") {
|
||||||
t.Fatalf("expected an error, got %v", err)
|
t.Fatalf("expected an error, got %v", err)
|
||||||
}
|
}
|
||||||
@ -200,11 +189,11 @@ func testBakeMacaroon(net *lntest.NetworkHarness, t *harnessTest) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
_, err = adminMacConnection.BakeMacaroon(ctxt, req)
|
_, err = client.BakeMacaroon(ctxt, req)
|
||||||
require.NoError(t.t, err)
|
require.NoError(t.t, err)
|
||||||
|
|
||||||
listReq := &lnrpc.ListMacaroonIDsRequest{}
|
listReq := &lnrpc.ListMacaroonIDsRequest{}
|
||||||
resp, err := adminMacConnection.ListMacaroonIDs(ctxt, listReq)
|
resp, err := client.ListMacaroonIDs(ctxt, listReq)
|
||||||
require.NoError(t.t, err)
|
require.NoError(t.t, err)
|
||||||
if resp.RootKeyIds[0] != 0 {
|
if resp.RootKeyIds[0] != 0 {
|
||||||
t.Fatalf("expected ID to be 0, found: %v", resp.RootKeyIds)
|
t.Fatalf("expected ID to be 0, found: %v", resp.RootKeyIds)
|
||||||
@ -221,11 +210,11 @@ func testBakeMacaroon(net *lntest.NetworkHarness, t *harnessTest) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
bakeResp, err := adminMacConnection.BakeMacaroon(ctxt, req)
|
bakeResp, err := client.BakeMacaroon(ctxt, req)
|
||||||
require.NoError(t.t, err)
|
require.NoError(t.t, err)
|
||||||
|
|
||||||
listReq = &lnrpc.ListMacaroonIDsRequest{}
|
listReq = &lnrpc.ListMacaroonIDsRequest{}
|
||||||
resp, err = adminMacConnection.ListMacaroonIDs(ctxt, listReq)
|
resp, err = client.ListMacaroonIDs(ctxt, listReq)
|
||||||
require.NoError(t.t, err)
|
require.NoError(t.t, err)
|
||||||
|
|
||||||
// the ListMacaroonIDs should give a list of two IDs, the default ID 0, and
|
// the ListMacaroonIDs should give a list of two IDs, the default ID 0, and
|
||||||
@ -248,27 +237,25 @@ func testBakeMacaroon(net *lntest.NetworkHarness, t *harnessTest) {
|
|||||||
// should succeed in reading, and fail to write a macaroon.
|
// should succeed in reading, and fail to write a macaroon.
|
||||||
newMac, err := readMacaroonFromHex(bakeResp.Macaroon)
|
newMac, err := readMacaroonFromHex(bakeResp.Macaroon)
|
||||||
require.NoError(t.t, err)
|
require.NoError(t.t, err)
|
||||||
conn, err = testNode.ConnectRPCWithMacaroon(newMac)
|
cleanup, client = macaroonClient(t.t, testNode, newMac)
|
||||||
require.NoError(t.t, err)
|
defer cleanup()
|
||||||
defer conn.Close()
|
|
||||||
newMacConnection := lnrpc.NewLightningClient(conn)
|
|
||||||
|
|
||||||
// BakeMacaroon requires a write permission, so this call should return an
|
// BakeMacaroon requires a write permission, so this call should return an
|
||||||
// error.
|
// error.
|
||||||
_, err = newMacConnection.BakeMacaroon(ctxt, req)
|
_, err = client.BakeMacaroon(ctxt, req)
|
||||||
if err == nil || !errContains(err, "permission denied") {
|
if err == nil || !errContains(err, "permission denied") {
|
||||||
t.Fatalf("expected an error, got %v", err)
|
t.Fatalf("expected an error, got %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListMacaroon requires a read permission, so this call should succeed.
|
// ListMacaroon requires a read permission, so this call should succeed.
|
||||||
listReq = &lnrpc.ListMacaroonIDsRequest{}
|
listReq = &lnrpc.ListMacaroonIDsRequest{}
|
||||||
resp, err = newMacConnection.ListMacaroonIDs(ctxt, listReq)
|
resp, err = client.ListMacaroonIDs(ctxt, listReq)
|
||||||
require.NoError(t.t, err)
|
require.NoError(t.t, err)
|
||||||
|
|
||||||
// Current macaroon can only work on entity macaroon, so a GetInfo request
|
// Current macaroon can only work on entity macaroon, so a GetInfo request
|
||||||
// will fail.
|
// will fail.
|
||||||
infoReq := &lnrpc.GetInfoRequest{}
|
infoReq := &lnrpc.GetInfoRequest{}
|
||||||
_, err = newMacConnection.GetInfo(ctxt, infoReq)
|
_, err = client.GetInfo(ctxt, infoReq)
|
||||||
if err == nil || !errContains(err, "permission denied") {
|
if err == nil || !errContains(err, "permission denied") {
|
||||||
t.Fatalf("expected error not returned, got %v", err)
|
t.Fatalf("expected error not returned, got %v", err)
|
||||||
}
|
}
|
||||||
@ -291,14 +278,12 @@ func testDeleteMacaroonID(net *lntest.NetworkHarness, t *harnessTest) {
|
|||||||
testNode.AdminMacPath(), defaultTimeout,
|
testNode.AdminMacPath(), defaultTimeout,
|
||||||
)
|
)
|
||||||
require.NoError(t.t, err)
|
require.NoError(t.t, err)
|
||||||
conn, err := testNode.ConnectRPCWithMacaroon(adminMac)
|
cleanup, client := macaroonClient(t.t, testNode, adminMac)
|
||||||
require.NoError(t.t, err)
|
defer cleanup()
|
||||||
defer conn.Close()
|
|
||||||
adminMacConnection := lnrpc.NewLightningClient(conn)
|
|
||||||
|
|
||||||
// Record the number of macaroon IDs before creation.
|
// Record the number of macaroon IDs before creation.
|
||||||
listReq := &lnrpc.ListMacaroonIDsRequest{}
|
listReq := &lnrpc.ListMacaroonIDsRequest{}
|
||||||
listResp, err := adminMacConnection.ListMacaroonIDs(ctxt, listReq)
|
listResp, err := client.ListMacaroonIDs(ctxt, listReq)
|
||||||
require.NoError(t.t, err)
|
require.NoError(t.t, err)
|
||||||
numMacIDs := len(listResp.RootKeyIds)
|
numMacIDs := len(listResp.RootKeyIds)
|
||||||
|
|
||||||
@ -313,14 +298,14 @@ func testDeleteMacaroonID(net *lntest.NetworkHarness, t *harnessTest) {
|
|||||||
Action: "read",
|
Action: "read",
|
||||||
}},
|
}},
|
||||||
}
|
}
|
||||||
resp, err := adminMacConnection.BakeMacaroon(ctxt, req)
|
resp, err := client.BakeMacaroon(ctxt, req)
|
||||||
require.NoError(t.t, err)
|
require.NoError(t.t, err)
|
||||||
macList = append(macList, resp.Macaroon)
|
macList = append(macList, resp.Macaroon)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check that the creation is successful.
|
// Check that the creation is successful.
|
||||||
listReq = &lnrpc.ListMacaroonIDsRequest{}
|
listReq = &lnrpc.ListMacaroonIDsRequest{}
|
||||||
listResp, err = adminMacConnection.ListMacaroonIDs(ctxt, listReq)
|
listResp, err = client.ListMacaroonIDs(ctxt, listReq)
|
||||||
require.NoError(t.t, err)
|
require.NoError(t.t, err)
|
||||||
|
|
||||||
// The number of macaroon IDs should be increased by len(rootKeyIDs).
|
// The number of macaroon IDs should be increased by len(rootKeyIDs).
|
||||||
@ -333,7 +318,7 @@ func testDeleteMacaroonID(net *lntest.NetworkHarness, t *harnessTest) {
|
|||||||
req := &lnrpc.DeleteMacaroonIDRequest{
|
req := &lnrpc.DeleteMacaroonIDRequest{
|
||||||
RootKeyId: defaultID,
|
RootKeyId: defaultID,
|
||||||
}
|
}
|
||||||
_, err = adminMacConnection.DeleteMacaroonID(ctxt, req)
|
_, err = client.DeleteMacaroonID(ctxt, req)
|
||||||
require.Error(t.t, err)
|
require.Error(t.t, err)
|
||||||
require.Contains(
|
require.Contains(
|
||||||
t.t, err.Error(), macaroons.ErrDeletionForbidden.Error(),
|
t.t, err.Error(), macaroons.ErrDeletionForbidden.Error(),
|
||||||
@ -343,13 +328,13 @@ func testDeleteMacaroonID(net *lntest.NetworkHarness, t *harnessTest) {
|
|||||||
req = &lnrpc.DeleteMacaroonIDRequest{
|
req = &lnrpc.DeleteMacaroonIDRequest{
|
||||||
RootKeyId: rootKeyIDs[0],
|
RootKeyId: rootKeyIDs[0],
|
||||||
}
|
}
|
||||||
resp, err := adminMacConnection.DeleteMacaroonID(ctxt, req)
|
resp, err := client.DeleteMacaroonID(ctxt, req)
|
||||||
require.NoError(t.t, err)
|
require.NoError(t.t, err)
|
||||||
require.True(t.t, resp.Deleted)
|
require.True(t.t, resp.Deleted)
|
||||||
|
|
||||||
// Check that the deletion is successful.
|
// Check that the deletion is successful.
|
||||||
listReq = &lnrpc.ListMacaroonIDsRequest{}
|
listReq = &lnrpc.ListMacaroonIDsRequest{}
|
||||||
listResp, err = adminMacConnection.ListMacaroonIDs(ctxt, listReq)
|
listResp, err = client.ListMacaroonIDs(ctxt, listReq)
|
||||||
require.NoError(t.t, err)
|
require.NoError(t.t, err)
|
||||||
|
|
||||||
// The number of macaroon IDs should be decreased by 1.
|
// The number of macaroon IDs should be decreased by 1.
|
||||||
@ -358,14 +343,12 @@ func testDeleteMacaroonID(net *lntest.NetworkHarness, t *harnessTest) {
|
|||||||
// Check that the deleted macaroon can no longer access macaroon:read.
|
// Check that the deleted macaroon can no longer access macaroon:read.
|
||||||
deletedMac, err := readMacaroonFromHex(macList[0])
|
deletedMac, err := readMacaroonFromHex(macList[0])
|
||||||
require.NoError(t.t, err)
|
require.NoError(t.t, err)
|
||||||
conn, err = testNode.ConnectRPCWithMacaroon(deletedMac)
|
cleanup, client = macaroonClient(t.t, testNode, deletedMac)
|
||||||
require.NoError(t.t, err)
|
defer cleanup()
|
||||||
defer conn.Close()
|
|
||||||
deletedMacConnection := lnrpc.NewLightningClient(conn)
|
|
||||||
|
|
||||||
// Because the macaroon is deleted, it will be treated as an invalid one.
|
// Because the macaroon is deleted, it will be treated as an invalid one.
|
||||||
listReq = &lnrpc.ListMacaroonIDsRequest{}
|
listReq = &lnrpc.ListMacaroonIDsRequest{}
|
||||||
_, err = deletedMacConnection.ListMacaroonIDs(ctxt, listReq)
|
_, err = client.ListMacaroonIDs(ctxt, listReq)
|
||||||
require.Error(t.t, err)
|
require.Error(t.t, err)
|
||||||
require.Contains(t.t, err.Error(), "cannot get macaroon")
|
require.Contains(t.t, err.Error(), "cannot get macaroon")
|
||||||
}
|
}
|
||||||
@ -383,3 +366,16 @@ func readMacaroonFromHex(macHex string) (*macaroon.Macaroon, error) {
|
|||||||
}
|
}
|
||||||
return mac, nil
|
return mac, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func macaroonClient(t *testing.T, testNode *lntest.HarnessNode,
|
||||||
|
mac *macaroon.Macaroon) (func(), lnrpc.LightningClient) {
|
||||||
|
|
||||||
|
conn, err := testNode.ConnectRPCWithMacaroon(mac)
|
||||||
|
require.NoError(t, err, "connect to alice")
|
||||||
|
|
||||||
|
cleanup := func() {
|
||||||
|
err := conn.Close()
|
||||||
|
require.NoError(t, err, "close")
|
||||||
|
}
|
||||||
|
return cleanup, lnrpc.NewLightningClient(conn)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user