|
| 1 | +package network |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/ethereum/go-ethereum/rlp" |
| 8 | +) |
| 9 | + |
| 10 | +// TestCapabilitySetUnset tests that setting and unsetting bits yield expected results |
| 11 | +func TestCapabilitySetUnset(t *testing.T) { |
| 12 | + firstSet := []bool{ |
| 13 | + true, false, false, false, false, false, true, true, false, |
| 14 | + } // 1000 0011 0 |
| 15 | + firstResult := firstSet |
| 16 | + secondSet := []bool{ |
| 17 | + false, true, false, true, false, false, true, false, true, |
| 18 | + } // 0101 0010 1 |
| 19 | + secondResult := []bool{ |
| 20 | + true, true, false, true, false, false, true, true, true, |
| 21 | + } // 1101 0011 1 |
| 22 | + thirdUnset := []bool{ |
| 23 | + true, false, true, true, false, false, true, false, true, |
| 24 | + } // 1011 0010 1 |
| 25 | + thirdResult := []bool{ |
| 26 | + false, true, false, false, false, false, false, true, false, |
| 27 | + } // 0100 0001 0 |
| 28 | + |
| 29 | + c := NewCapability(42, 9) |
| 30 | + for i, b := range firstSet { |
| 31 | + if b { |
| 32 | + c.Set(i) |
| 33 | + } |
| 34 | + } |
| 35 | + if !isSameBools(c.Cap, firstResult) { |
| 36 | + t.Fatalf("first set result mismatch, expected %v, got %v", firstResult, c.Cap) |
| 37 | + } |
| 38 | + |
| 39 | + for i, b := range secondSet { |
| 40 | + if b { |
| 41 | + c.Set(i) |
| 42 | + } |
| 43 | + } |
| 44 | + if !isSameBools(c.Cap, secondResult) { |
| 45 | + t.Fatalf("second set result mismatch, expected %v, got %v", secondResult, c.Cap) |
| 46 | + } |
| 47 | + |
| 48 | + for i, b := range thirdUnset { |
| 49 | + if b { |
| 50 | + c.Unset(i) |
| 51 | + } |
| 52 | + } |
| 53 | + if !isSameBools(c.Cap, thirdResult) { |
| 54 | + t.Fatalf("second set result mismatch, expected %v, got %v", thirdResult, c.Cap) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// TestCapabilitiesControl tests that the methods for manipulating the capabilities bitvectors set values correctly and return errors when they should |
| 59 | +func TestCapabilitiesControl(t *testing.T) { |
| 60 | + |
| 61 | + // Initialize capability |
| 62 | + caps := NewCapabilities() |
| 63 | + |
| 64 | + // Register module. Should succeed |
| 65 | + c1 := NewCapability(1, 16) |
| 66 | + err := caps.add(c1) |
| 67 | + if err != nil { |
| 68 | + t.Fatalf("RegisterCapabilityModule fail: %v", err) |
| 69 | + } |
| 70 | + |
| 71 | + // Fail if capability id already exists |
| 72 | + c2 := NewCapability(1, 1) |
| 73 | + err = caps.add(c2) |
| 74 | + if err == nil { |
| 75 | + t.Fatalf("Expected RegisterCapabilityModule call with existing id to fail") |
| 76 | + } |
| 77 | + |
| 78 | + // More than one capabilities flag vector should be possible |
| 79 | + c3 := NewCapability(2, 1) |
| 80 | + err = caps.add(c3) |
| 81 | + if err != nil { |
| 82 | + t.Fatalf("RegisterCapabilityModule (second) fail: %v", err) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// TestCapabilitiesString checks that the string representation of the capabilities is correct |
| 87 | +func TestCapabilitiesString(t *testing.T) { |
| 88 | + sets1 := []bool{ |
| 89 | + false, false, true, |
| 90 | + } |
| 91 | + c1 := NewCapability(42, len(sets1)) |
| 92 | + for i, b := range sets1 { |
| 93 | + if b { |
| 94 | + c1.Set(i) |
| 95 | + } |
| 96 | + } |
| 97 | + sets2 := []bool{ |
| 98 | + true, false, false, false, true, false, true, false, true, |
| 99 | + } |
| 100 | + c2 := NewCapability(666, len(sets2)) |
| 101 | + for i, b := range sets2 { |
| 102 | + if b { |
| 103 | + c2.Set(i) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + caps := NewCapabilities() |
| 108 | + caps.add(c1) |
| 109 | + caps.add(c2) |
| 110 | + |
| 111 | + correctString := "42:001,666:100010101" |
| 112 | + if correctString != caps.String() { |
| 113 | + t.Fatalf("Capabilities string mismatch; expected %s, got %s", correctString, caps) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +// TestCapabilitiesRLP ensures that a round of serialization and deserialization of Capabilities object |
| 118 | +// results in the correct data |
| 119 | +func TestCapabilitiesRLP(t *testing.T) { |
| 120 | + c := NewCapabilities() |
| 121 | + cap1 := &Capability{ |
| 122 | + Id: 42, |
| 123 | + Cap: []bool{true, false, true}, |
| 124 | + } |
| 125 | + c.add(cap1) |
| 126 | + cap2 := &Capability{ |
| 127 | + Id: 666, |
| 128 | + Cap: []bool{true, false, true, false, true, true, false, false, true}, |
| 129 | + } |
| 130 | + c.add(cap2) |
| 131 | + buf := bytes.NewBuffer(nil) |
| 132 | + err := rlp.Encode(buf, &c) |
| 133 | + if err != nil { |
| 134 | + t.Fatal(err) |
| 135 | + } |
| 136 | + |
| 137 | + cRestored := NewCapabilities() |
| 138 | + err = rlp.Decode(buf, &cRestored) |
| 139 | + if err != nil { |
| 140 | + t.Fatal(err) |
| 141 | + } |
| 142 | + |
| 143 | + cap1Restored := cRestored.get(cap1.Id) |
| 144 | + if cap1Restored.Id != cap1.Id { |
| 145 | + t.Fatalf("cap 1 id not correct, expected %d, got %d", cap1.Id, cap1Restored.Id) |
| 146 | + } |
| 147 | + if !cap1.IsSameAs(cap1Restored) { |
| 148 | + t.Fatalf("cap 1 caps not correct, expected %v, got %v", cap1.Cap, cap1Restored.Cap) |
| 149 | + } |
| 150 | + |
| 151 | + cap2Restored := cRestored.get(cap2.Id) |
| 152 | + if cap2Restored.Id != cap2.Id { |
| 153 | + t.Fatalf("cap 1 id not correct, expected %d, got %d", cap2.Id, cap2Restored.Id) |
| 154 | + } |
| 155 | + if !cap2.IsSameAs(cap2Restored) { |
| 156 | + t.Fatalf("cap 1 caps not correct, expected %v, got %v", cap2.Cap, cap2Restored.Cap) |
| 157 | + } |
| 158 | +} |
0 commit comments