itest: refactor testDeleteMacaroonID

This commit is contained in:
yyforyongyu 2022-08-09 14:35:45 +08:00
parent 64bf6f4e26
commit 4b469eb0c5
No known key found for this signature in database
GPG key ID: 9BCD95C4FF296868
3 changed files with 27 additions and 27 deletions

View file

@ -353,4 +353,8 @@ var allTestCasesTemp = []*lntemp.TestCase{
Name: "bake macaroon", Name: "bake macaroon",
TestFunc: testBakeMacaroon, TestFunc: testBakeMacaroon,
}, },
{
Name: "delete macaroon id",
TestFunc: testDeleteMacaroonID,
},
} }

View file

@ -520,26 +520,26 @@ func testBakeMacaroon(ht *lntemp.HarnessTest) {
// specified ID and invalidates all macaroons derived from the key with that ID. // specified ID and invalidates all macaroons derived from the key with that ID.
// Also, it checks deleting the reserved marcaroon ID, DefaultRootKeyID or is // Also, it checks deleting the reserved marcaroon ID, DefaultRootKeyID or is
// forbidden. // forbidden.
func testDeleteMacaroonID(net *lntest.NetworkHarness, t *harnessTest) { func testDeleteMacaroonID(ht *lntemp.HarnessTest) {
var ( var (
ctxb = context.Background() ctxb = ht.Context()
testNode = net.Alice testNode = ht.Alice
) )
ctxt, cancel := context.WithTimeout(ctxb, defaultTimeout) ctxt, cancel := context.WithTimeout(ctxb, defaultTimeout)
defer cancel() defer cancel()
// Use admin macaroon to create a connection. // Use admin macaroon to create a connection.
adminMac, err := testNode.ReadMacaroon( adminMac, err := testNode.ReadMacaroon(
testNode.AdminMacPath(), defaultTimeout, testNode.Cfg.AdminMacPath, defaultTimeout,
) )
require.NoError(t.t, err) require.NoError(ht, err)
cleanup, client := macaroonClientOld(t.t, testNode, adminMac) cleanup, client := macaroonClient(ht.T, testNode, adminMac)
defer cleanup() defer cleanup()
// Record the number of macaroon IDs before creation. // Record the number of macaroon IDs before creation.
listReq := &lnrpc.ListMacaroonIDsRequest{} listReq := &lnrpc.ListMacaroonIDsRequest{}
listResp, err := client.ListMacaroonIDs(ctxt, listReq) listResp, err := client.ListMacaroonIDs(ctxt, listReq)
require.NoError(t.t, err) require.NoError(ht, err)
numMacIDs := len(listResp.RootKeyIds) numMacIDs := len(listResp.RootKeyIds)
// Create macaroons for testing. // Create macaroons for testing.
@ -554,17 +554,17 @@ func testDeleteMacaroonID(net *lntest.NetworkHarness, t *harnessTest) {
}}, }},
} }
resp, err := client.BakeMacaroon(ctxt, req) resp, err := client.BakeMacaroon(ctxt, req)
require.NoError(t.t, err) require.NoError(ht, 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 = client.ListMacaroonIDs(ctxt, listReq) listResp, err = client.ListMacaroonIDs(ctxt, listReq)
require.NoError(t.t, err) require.NoError(ht, err)
// The number of macaroon IDs should be increased by len(rootKeyIDs). // The number of macaroon IDs should be increased by len(rootKeyIDs).
require.Equal(t.t, numMacIDs+len(rootKeyIDs), len(listResp.RootKeyIds)) require.Equal(ht, numMacIDs+len(rootKeyIDs), len(listResp.RootKeyIds))
// First test: check deleting the DefaultRootKeyID returns an error. // First test: check deleting the DefaultRootKeyID returns an error.
defaultID, _ := strconv.ParseUint( defaultID, _ := strconv.ParseUint(
@ -574,38 +574,38 @@ func testDeleteMacaroonID(net *lntest.NetworkHarness, t *harnessTest) {
RootKeyId: defaultID, RootKeyId: defaultID,
} }
_, err = client.DeleteMacaroonID(ctxt, req) _, err = client.DeleteMacaroonID(ctxt, req)
require.Error(t.t, err) require.Error(ht, err)
require.Contains( require.Contains(ht, err.Error(),
t.t, err.Error(), macaroons.ErrDeletionForbidden.Error(), macaroons.ErrDeletionForbidden.Error())
)
// Second test: check deleting the customized ID returns success. // Second test: check deleting the customized ID returns success.
req = &lnrpc.DeleteMacaroonIDRequest{ req = &lnrpc.DeleteMacaroonIDRequest{
RootKeyId: rootKeyIDs[0], RootKeyId: rootKeyIDs[0],
} }
resp, err := client.DeleteMacaroonID(ctxt, req) resp, err := client.DeleteMacaroonID(ctxt, req)
require.NoError(t.t, err) require.NoError(ht, err)
require.True(t.t, resp.Deleted) require.True(ht, resp.Deleted)
// Check that the deletion is successful. // Check that the deletion is successful.
listReq = &lnrpc.ListMacaroonIDsRequest{} listReq = &lnrpc.ListMacaroonIDsRequest{}
listResp, err = client.ListMacaroonIDs(ctxt, listReq) listResp, err = client.ListMacaroonIDs(ctxt, listReq)
require.NoError(t.t, err) require.NoError(ht, err)
// The number of macaroon IDs should be decreased by 1. // The number of macaroon IDs should be decreased by 1.
require.Equal(t.t, numMacIDs+len(rootKeyIDs)-1, len(listResp.RootKeyIds)) require.Equal(ht, numMacIDs+len(rootKeyIDs)-1, len(listResp.RootKeyIds))
// 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(ht, err)
cleanup, client = macaroonClientOld(t.t, testNode, deletedMac) cleanup, client = macaroonClient(ht.T, testNode, deletedMac)
defer cleanup() defer cleanup()
// 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 = client.ListMacaroonIDs(ctxt, listReq) _, err = client.ListMacaroonIDs(ctxt, listReq)
require.Error(t.t, err) require.Error(ht, err)
require.Contains(t.t, err.Error(), "cannot get macaroon") require.Contains(ht, err.Error(), "cannot get macaroon")
} }
// testStatelessInit checks that the stateless initialization of the daemon // testStatelessInit checks that the stateless initialization of the daemon

View file

@ -52,10 +52,6 @@ var allTestCases = []*testCase{
name: "cpfp", name: "cpfp",
test: testCPFP, test: testCPFP,
}, },
{
name: "delete macaroon id",
test: testDeleteMacaroonID,
},
{ {
name: "psbt channel funding", name: "psbt channel funding",
test: testPsbtChanFunding, test: testPsbtChanFunding,