github.com/decred/dcrlnd@v0.7.6/htlcswitch/hodl/mask_test.go (about) 1 package hodl_test 2 3 import ( 4 "testing" 5 6 "github.com/decred/dcrlnd/build" 7 "github.com/decred/dcrlnd/htlcswitch/hodl" 8 ) 9 10 var hodlMaskTests = []struct { 11 mask hodl.Mask 12 flags map[hodl.Flag]struct{} 13 }{ 14 { 15 // Check that the empty mask has no active flags. 16 mask: hodl.MaskNone, 17 flags: map[hodl.Flag]struct{}{}, 18 }, 19 { 20 // Check that passing no arguments to MaskFromFlags is 21 // equivalent to MaskNone. 22 mask: hodl.MaskFromFlags(), 23 flags: map[hodl.Flag]struct{}{}, 24 }, 25 26 { 27 // Check using Mask to convert a single flag into a Mask only 28 // reports that flag active. 29 mask: hodl.ExitSettle.Mask(), 30 flags: map[hodl.Flag]struct{}{ 31 hodl.ExitSettle: {}, 32 }, 33 }, 34 { 35 // Check that using MaskFromFlags on a single flag only reports 36 // that flag active. 37 mask: hodl.MaskFromFlags(hodl.Commit), 38 flags: map[hodl.Flag]struct{}{ 39 hodl.Commit: {}, 40 }, 41 }, 42 43 { 44 // Check that using MaskFromFlags on some-but-not-all flags 45 // reports the correct subset of flags as active. 46 mask: hodl.MaskFromFlags( 47 hodl.ExitSettle, 48 hodl.Commit, 49 hodl.AddIncoming, 50 hodl.SettleOutgoing, 51 ), 52 flags: map[hodl.Flag]struct{}{ 53 hodl.ExitSettle: {}, 54 hodl.Commit: {}, 55 hodl.AddIncoming: {}, 56 hodl.SettleOutgoing: {}, 57 }, 58 }, 59 { 60 // Check that using MaskFromFlags on all known flags reports 61 // those an no other flags. 62 mask: hodl.MaskFromFlags( 63 hodl.ExitSettle, 64 hodl.AddIncoming, 65 hodl.SettleIncoming, 66 hodl.FailIncoming, 67 hodl.AddOutgoing, 68 hodl.SettleOutgoing, 69 hodl.FailOutgoing, 70 hodl.Commit, 71 hodl.BogusSettle, 72 ), 73 flags: map[hodl.Flag]struct{}{ 74 hodl.ExitSettle: {}, 75 hodl.AddIncoming: {}, 76 hodl.SettleIncoming: {}, 77 hodl.FailIncoming: {}, 78 hodl.AddOutgoing: {}, 79 hodl.SettleOutgoing: {}, 80 hodl.FailOutgoing: {}, 81 hodl.Commit: {}, 82 hodl.BogusSettle: {}, 83 }, 84 }, 85 } 86 87 // TestMask iterates through all of the hodlMaskTests, checking that the mask 88 // correctly reports active for flags in the tests' expected flags, and inactive 89 // for all others. 90 func TestMask(t *testing.T) { 91 if !build.IsDevBuild() { 92 t.Fatalf("htlcswitch tests must be run with '-tags=dev'") 93 } 94 95 for i, test := range hodlMaskTests { 96 for j := uint32(0); i < 32; i++ { 97 flag := hodl.Flag(1 << j) 98 _, shouldBeActive := test.flags[flag] 99 100 switch { 101 case shouldBeActive && !test.mask.Active(flag): 102 t.Fatalf("hodl mask test #%d -- "+ 103 "expected flag %s to be active", 104 i, flag) 105 106 case !shouldBeActive && test.mask.Active(flag): 107 t.Fatalf("hodl mask test #%d -- "+ 108 "expected flag %s to be inactive", 109 i, flag) 110 } 111 } 112 } 113 }