github.com/decred/dcrlnd@v0.7.6/chanbackup/multi_test.go (about) 1 package chanbackup 2 3 import ( 4 "bytes" 5 "net" 6 "testing" 7 ) 8 9 // TestMultiPackUnpack... 10 func TestMultiPackUnpack(t *testing.T) { 11 t.Parallel() 12 13 var multi Multi 14 numSingles := 10 15 originalSingles := make([]Single, 0, numSingles) 16 for i := 0; i < numSingles; i++ { 17 channel, err := genRandomOpenChannelShell() 18 if err != nil { 19 t.Fatalf("unable to gen channel: %v", err) 20 } 21 22 single := NewSingle(channel, []net.Addr{addr1, addr2}) 23 24 originalSingles = append(originalSingles, single) 25 multi.StaticBackups = append(multi.StaticBackups, single) 26 } 27 28 keyRing := &mockKeyRing{} 29 30 versionTestCases := []struct { 31 // version is the pack/unpack version that we should use to 32 // decode/encode the final SCB. 33 version MultiBackupVersion 34 35 // valid tests us if this test case should pass or not. 36 valid bool 37 }{ 38 // The default version, should pack/unpack with no problem. 39 { 40 version: DefaultSingleVersion, 41 valid: true, 42 }, 43 44 // A non-default version, atm this should result in a failure. 45 { 46 version: 99, 47 valid: false, 48 }, 49 } 50 for i, versionCase := range versionTestCases { 51 multi.Version = versionCase.version 52 53 var b bytes.Buffer 54 err := multi.PackToWriter(&b, keyRing) 55 switch { 56 // If this is a valid test case, and we failed, then we'll 57 // return an error. 58 case err != nil && versionCase.valid: 59 t.Fatalf("#%v, unable to pack multi: %v", i, err) 60 61 // If this is an invalid test case, and we passed it, then 62 // we'll return an error. 63 case err == nil && !versionCase.valid: 64 t.Fatalf("#%v got nil error for invalid pack: %v", 65 i, err) 66 } 67 68 // If this is a valid test case, then we'll continue to ensure 69 // we can unpack it, and also that if we mutate the packed 70 // version, then we trigger an error. 71 if versionCase.valid { 72 var unpackedMulti Multi 73 err = unpackedMulti.UnpackFromReader(&b, keyRing) 74 if err != nil { 75 t.Fatalf("#%v unable to unpack multi: %v", 76 i, err) 77 } 78 79 // First, we'll ensure that the unpacked version of the 80 // packed multi is the same as the original set. 81 if len(originalSingles) != 82 len(unpackedMulti.StaticBackups) { 83 t.Fatalf("expected %v singles, got %v", 84 len(originalSingles), 85 len(unpackedMulti.StaticBackups)) 86 } 87 for i := 0; i < numSingles; i++ { 88 assertSingleEqual( 89 t, originalSingles[i], 90 unpackedMulti.StaticBackups[i], 91 ) 92 } 93 94 // Next, we'll make a fake packed multi, it'll have an 95 // unknown version relative to what's implemented atm. 96 var fakePackedMulti bytes.Buffer 97 fakeRawMulti := bytes.NewBuffer( 98 bytes.Repeat([]byte{99}, 20), 99 ) 100 err := encryptPayloadToWriter( 101 *fakeRawMulti, &fakePackedMulti, keyRing, 102 ) 103 if err != nil { 104 t.Fatalf("unable to pack fake multi; %v", err) 105 } 106 107 // We should reject this fake multi as it contains an 108 // unknown version. 109 err = unpackedMulti.UnpackFromReader( 110 &fakePackedMulti, keyRing, 111 ) 112 if err == nil { 113 t.Fatalf("#%v unpack with unknown version "+ 114 "should have failed", i) 115 } 116 } 117 } 118 } 119 120 // TestPackedMultiUnpack tests that we're able to properly unpack a typed 121 // packed multi. 122 func TestPackedMultiUnpack(t *testing.T) { 123 t.Parallel() 124 125 keyRing := &mockKeyRing{} 126 127 // First, we'll make a new unpacked multi with a random channel. 128 testChannel, err := genRandomOpenChannelShell() 129 if err != nil { 130 t.Fatalf("unable to gen random channel: %v", err) 131 } 132 var multi Multi 133 multi.StaticBackups = append( 134 multi.StaticBackups, NewSingle(testChannel, nil), 135 ) 136 137 // Now that we have our multi, we'll pack it into a new buffer. 138 var b bytes.Buffer 139 if err := multi.PackToWriter(&b, keyRing); err != nil { 140 t.Fatalf("unable to pack multi: %v", err) 141 } 142 143 // We should be able to properly unpack this typed packed multi. 144 packedMulti := PackedMulti(b.Bytes()) 145 unpackedMulti, err := packedMulti.Unpack(keyRing) 146 if err != nil { 147 t.Fatalf("unable to unpack multi: %v", err) 148 } 149 150 // Finally, the versions should match, and the unpacked singles also 151 // identical. 152 if multi.Version != unpackedMulti.Version { 153 t.Fatalf("version mismatch: expected %v got %v", 154 multi.Version, unpackedMulti.Version) 155 } 156 assertSingleEqual( 157 t, multi.StaticBackups[0], unpackedMulti.StaticBackups[0], 158 ) 159 }