|
| 1 | +package multistream |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "crypto/sha256" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestAbbrevTreeAddProtocol(t *testing.T) { |
| 10 | + tree := &abbrevTree[string]{} |
| 11 | + |
| 12 | + proto1 := "protocol1" |
| 13 | + hash1 := sha256.Sum256([]byte(proto1)) |
| 14 | + proto2 := "protocol2" |
| 15 | + hash2 := sha256.Sum256([]byte(proto2)) |
| 16 | + proto3 := "protocol251" // this one has the same first byte as "protocol1" |
| 17 | + hash3 := sha256.Sum256([]byte(proto3)) |
| 18 | + |
| 19 | + // make sure we don't make mistakes on the hashes |
| 20 | + if hash1[0] == hash2[0] { |
| 21 | + t.Fatal("the first bytes of hash1 and hash2 should be different") |
| 22 | + } |
| 23 | + if hash1[0] != hash3[0] { |
| 24 | + t.Fatal("the first bytes of hash1 and hash3 should be the same") |
| 25 | + } |
| 26 | + if hash1[1] == hash3[1] { |
| 27 | + t.Fatal("the second bytes of hash1 and hash3 should be different") |
| 28 | + } |
| 29 | + |
| 30 | + // add only proto1 |
| 31 | + tree.AddProtocol(proto1) |
| 32 | + |
| 33 | + if tree.root == nil { |
| 34 | + t.Fatal("root should not be nil after adding protocol") |
| 35 | + } |
| 36 | + if tree.root.children[hash1[0]] == nil || tree.root.children[hash1[0]].p == nil { |
| 37 | + t.Fatal("the protocol was not added") |
| 38 | + } |
| 39 | + if tree.root.children[hash1[0]].p.protocolID != proto1 { |
| 40 | + t.Fatal("the protocol ID was wrong") |
| 41 | + } |
| 42 | + if tree.root.children[hash1[0]].p.tombstoneBit { |
| 43 | + t.Fatal("tombstoneBit of proto1 must not be set") |
| 44 | + } |
| 45 | + if !bytes.Equal(tree.Abbreviate(proto1), []byte{hash1[0]}) { |
| 46 | + t.Fatal("abbreviation of proto1 is incorrect") |
| 47 | + } |
| 48 | + |
| 49 | + // also add proto2 |
| 50 | + tree.AddProtocol(proto2) |
| 51 | + |
| 52 | + if tree.root.children[hash2[0]] == nil || tree.root.children[hash2[0]].p == nil { |
| 53 | + t.Fatal("the protocol was not added") |
| 54 | + } |
| 55 | + if tree.root.children[hash2[0]].p.protocolID != proto2 { |
| 56 | + t.Fatal("the protocol ID was wrong") |
| 57 | + } |
| 58 | + if tree.root.children[hash2[0]].p.tombstoneBit { |
| 59 | + t.Fatal("tombstoneBit of proto2 must not be set") |
| 60 | + } |
| 61 | + if !bytes.Equal(tree.Abbreviate(proto2), []byte{hash2[0]}) { |
| 62 | + t.Fatal("abbreviation of proto2 is incorrect") |
| 63 | + } |
| 64 | + |
| 65 | + // add proto3 which has the same first byte of the hash as proto1 |
| 66 | + tree.AddProtocol(proto3) |
| 67 | + |
| 68 | + n1 := tree.root.children[hash1[0]] |
| 69 | + // the node at the first level should still be proto1 |
| 70 | + if n1.p.protocolID != proto1 { |
| 71 | + t.Fatal("the node in the first level should not be modified") |
| 72 | + } |
| 73 | + // proto1 should be duplicated down |
| 74 | + if n1.children[hash1[1]] == nil || n1.children[hash1[1]].p == nil { |
| 75 | + t.Fatal("proto1 was not duplicated") |
| 76 | + } |
| 77 | + if n1.children[hash1[1]].p.tombstoneBit { |
| 78 | + t.Fatal("tombstoneBit of proto1 must not be set") |
| 79 | + } |
| 80 | + if !bytes.Equal(tree.Abbreviate(proto1), []byte{hash1[0], hash1[1]}) { |
| 81 | + t.Fatal("abbreviation of proto1 is incorrect") |
| 82 | + } |
| 83 | + // proto3 should be added in the second level |
| 84 | + if n1.children[hash3[1]] == nil || n1.children[hash3[1]].p == nil { |
| 85 | + t.Fatal("proto3 was not added") |
| 86 | + } |
| 87 | + if n1.children[hash3[1]].p.tombstoneBit { |
| 88 | + t.Fatal("tombstoneBit of proto3 must not be set") |
| 89 | + } |
| 90 | + if !bytes.Equal(tree.Abbreviate(proto3), []byte{hash3[0], hash3[1]}) { |
| 91 | + t.Fatal("abbreviation of proto3 is incorrect") |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func TestAbbrevTreeRemoveProtocol(t *testing.T) { |
| 96 | + tree := &abbrevTree[string]{} |
| 97 | + |
| 98 | + proto1 := "protocol1" |
| 99 | + hash1 := sha256.Sum256([]byte(proto1)) |
| 100 | + proto2 := "protocol2" |
| 101 | + proto3 := "protocol251" // this one has the same first byte as "protocol1" |
| 102 | + |
| 103 | + tree.AddProtocol(proto1) |
| 104 | + tree.AddProtocol(proto2) |
| 105 | + tree.AddProtocol(proto3) |
| 106 | + |
| 107 | + // remove only proto1 |
| 108 | + tree.RemoveProtocol(proto1) |
| 109 | + |
| 110 | + n1 := tree.root.children[hash1[0]] |
| 111 | + if !n1.p.tombstoneBit { |
| 112 | + t.Fatal("tombstoneBit of proto1 must be set") |
| 113 | + } |
| 114 | + if !n1.children[hash1[1]].p.tombstoneBit { |
| 115 | + t.Fatal("tombstoneBit of proto1 must be set") |
| 116 | + } |
| 117 | + if tree.Abbreviate(proto1) != nil { |
| 118 | + t.Fatal("abbreviation of proto1 should be nil") |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +func TestAbbrevTreeResurrectProtocol(t *testing.T) { |
| 123 | + tree := &abbrevTree[string]{} |
| 124 | + |
| 125 | + proto1 := "protocol1" |
| 126 | + hash1 := sha256.Sum256([]byte(proto1)) |
| 127 | + proto2 := "protocol2" |
| 128 | + proto3 := "protocol251" // this one has the same first byte as "protocol1" |
| 129 | + |
| 130 | + tree.AddProtocol(proto1) |
| 131 | + tree.AddProtocol(proto2) |
| 132 | + tree.AddProtocol(proto3) |
| 133 | + tree.RemoveProtocol(proto1) |
| 134 | + tree.AddProtocol(proto1) |
| 135 | + |
| 136 | + n1 := tree.root.children[hash1[0]] |
| 137 | + if n1.p.tombstoneBit { |
| 138 | + t.Fatal("tombstoneBit of proto1 must not be set") |
| 139 | + } |
| 140 | + if n1.children[hash1[1]].p.tombstoneBit { |
| 141 | + t.Fatal("tombstoneBit of proto1 must not be set") |
| 142 | + } |
| 143 | + |
| 144 | + // There should be another leaf node added for proto1 |
| 145 | + n2 := n1.children[hash1[1]] |
| 146 | + if n2.children[hash1[2]] == nil || n2.children[hash1[2]].p == nil { |
| 147 | + t.Fatal("proto1 was not added") |
| 148 | + } |
| 149 | + if n2.children[hash1[2]].p.tombstoneBit { |
| 150 | + t.Fatal("tombstoneBit of proto1 must not be set") |
| 151 | + } |
| 152 | + if !bytes.Equal(tree.Abbreviate(proto1), []byte{hash1[0], hash1[1], hash1[2]}) { |
| 153 | + t.Fatal("abbreviation of proto1 is incorrect") |
| 154 | + } |
| 155 | +} |
0 commit comments