github.com/decred/dcrlnd@v0.7.6/lnwire/channel_id_test.go (about)

     1  package lnwire
     2  
     3  import "testing"
     4  
     5  // TestChannelIDOutPointConversion ensures that the IsChanPoint always
     6  // recognizes its seed OutPoint for all possible values of an output index.
     7  func TestChannelIDOutPointConversion(t *testing.T) {
     8  	t.Parallel()
     9  
    10  	testChanPoint := *outpoint1
    11  
    12  	// For a given OutPoint, we'll run through all the possible output
    13  	// index values, mutating our test outpoint to match that output index.
    14  	var prevChanID ChannelID
    15  	for i := uint32(0); i < MaxFundingTxOutputs; i++ {
    16  		testChanPoint.Index = i
    17  
    18  		// With the output index mutated, we'll convert it into a
    19  		// ChannelID.
    20  		cid := NewChanIDFromOutPoint(&testChanPoint)
    21  
    22  		// Once the channel point has been converted to a channelID, it
    23  		// should recognize its original outpoint.
    24  		if !cid.IsChanPoint(&testChanPoint) {
    25  			t.Fatalf("channelID not recognized as seed channel "+
    26  				"point: cid=%v, op=%v", cid, testChanPoint)
    27  		}
    28  
    29  		// We also ensure that the channel ID itself have changed
    30  		// between iterations. This case is meant to catch an issue
    31  		// where the transformation function itself is a no-op.
    32  		if prevChanID == cid {
    33  			t.Fatalf("#%v: channelID not modified: old=%v, new=%v",
    34  				i, prevChanID, cid)
    35  		}
    36  
    37  		prevChanID = cid
    38  	}
    39  }
    40  
    41  // TestGenPossibleOutPoints ensures that the GenPossibleOutPoints generates a
    42  // valid set of outpoints for a channelID. A set of outpoints is valid iff, the
    43  // root outpoint (the outpoint that generated the ChannelID) is included in the
    44  // returned set of outpoints.
    45  func TestGenPossibleOutPoints(t *testing.T) {
    46  	t.Parallel()
    47  
    48  	// We'll first convert out test outpoint into a ChannelID.
    49  	testChanPoint := *outpoint1
    50  	testChanPoint.Index = 24
    51  	chanID := NewChanIDFromOutPoint(&testChanPoint)
    52  
    53  	// With the chan ID created, we'll generate all possible root outpoints
    54  	// given this channel ID.
    55  	possibleOutPoints := chanID.GenPossibleOutPoints()
    56  
    57  	// If we run through the generated possible outpoints, the original
    58  	// root outpoint MUST be found in this generated set.
    59  	var opFound bool
    60  	for _, op := range possibleOutPoints {
    61  		if op == testChanPoint {
    62  			opFound = true
    63  			break
    64  		}
    65  	}
    66  
    67  	// If we weren't able to locate the original outpoint in the set of
    68  	// possible outpoints, then we'll fail the test.
    69  	if !opFound {
    70  		t.Fatalf("possible outpoints did not contain the root outpoint")
    71  	}
    72  }