|
| 1 | +/* |
| 2 | +Copyright IBM Corp. 2017 All Rights Reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package gossip |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | +) |
| 25 | + |
| 26 | +func TestToString(t *testing.T) { |
| 27 | + // Ensure we don't print the byte content when we |
| 28 | + // log messages. |
| 29 | + // Each payload or signature contains '2' so we would've logged |
| 30 | + // them if not for the overloading of the String() method in SignedGossipMessage |
| 31 | + |
| 32 | + // The following line proves that the envelopes constructed in this test |
| 33 | + // have "2" in them when they are printed |
| 34 | + assert.Contains(t, fmt.Sprintf("%v", envelopes()[0]), "2") |
| 35 | + // and the following does the same for payloads: |
| 36 | + dMsg := &DataMessage{ |
| 37 | + Payload: &Payload{ |
| 38 | + SeqNum: 3, |
| 39 | + Data: []byte{2, 2, 2, 2, 2}, |
| 40 | + }, |
| 41 | + } |
| 42 | + assert.Contains(t, fmt.Sprintf("%v", dMsg), "2") |
| 43 | + |
| 44 | + // Now we construct all types of messages that have envelopes or payloads in them |
| 45 | + // and see that "2" is not outputted into their formatting even though it is found |
| 46 | + // as a sub-message of the outer message. |
| 47 | + |
| 48 | + sMsg := &SignedGossipMessage{ |
| 49 | + GossipMessage: &GossipMessage{ |
| 50 | + Tag: GossipMessage_EMPTY, |
| 51 | + Nonce: 5, |
| 52 | + Channel: []byte("A"), |
| 53 | + Content: &GossipMessage_DataMsg{ |
| 54 | + DataMsg: &DataMessage{ |
| 55 | + Payload: &Payload{ |
| 56 | + SeqNum: 3, |
| 57 | + Data: []byte{2, 2, 2, 2, 2}, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + Envelope: &Envelope{ |
| 63 | + Payload: []byte{0, 1, 2, 3, 4, 5, 6}, |
| 64 | + Signature: []byte{0, 1, 2}, |
| 65 | + SecretEnvelope: &SecretEnvelope{ |
| 66 | + Payload: []byte{0, 1, 2, 3, 4, 5}, |
| 67 | + Signature: []byte{0, 1, 2}, |
| 68 | + }, |
| 69 | + }, |
| 70 | + } |
| 71 | + assert.NotContains(t, fmt.Sprintf("%v", sMsg), "2") |
| 72 | + |
| 73 | + sMsg = &SignedGossipMessage{ |
| 74 | + GossipMessage: &GossipMessage{ |
| 75 | + Channel: []byte("A"), |
| 76 | + Tag: GossipMessage_EMPTY, |
| 77 | + Nonce: 5, |
| 78 | + Content: &GossipMessage_DataUpdate{ |
| 79 | + DataUpdate: &DataUpdate{ |
| 80 | + Nonce: 11, |
| 81 | + MsgType: PullMsgType_BlockMessage, |
| 82 | + Data: envelopes(), |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + Envelope: envelopes()[0], |
| 87 | + } |
| 88 | + assert.NotContains(t, fmt.Sprintf("%v", sMsg), "2") |
| 89 | + |
| 90 | + sMsg = &SignedGossipMessage{ |
| 91 | + GossipMessage: &GossipMessage{ |
| 92 | + Channel: []byte("A"), |
| 93 | + Tag: GossipMessage_EMPTY, |
| 94 | + Nonce: 5, |
| 95 | + Content: &GossipMessage_MemRes{ |
| 96 | + MemRes: &MembershipResponse{ |
| 97 | + Alive: envelopes(), |
| 98 | + Dead: envelopes(), |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + Envelope: envelopes()[0], |
| 103 | + } |
| 104 | + assert.NotContains(t, fmt.Sprintf("%v", sMsg), "2") |
| 105 | + |
| 106 | + sMsg = &SignedGossipMessage{ |
| 107 | + GossipMessage: &GossipMessage{ |
| 108 | + Channel: []byte("A"), |
| 109 | + Tag: GossipMessage_EMPTY, |
| 110 | + Nonce: 5, |
| 111 | + Content: &GossipMessage_StateSnapshot{ |
| 112 | + StateSnapshot: &StateInfoSnapshot{ |
| 113 | + Elements: envelopes(), |
| 114 | + }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + Envelope: envelopes()[0], |
| 118 | + } |
| 119 | + assert.NotContains(t, fmt.Sprintf("%v", sMsg), "2") |
| 120 | + |
| 121 | + sMsg = &SignedGossipMessage{ |
| 122 | + GossipMessage: &GossipMessage{ |
| 123 | + Channel: []byte("A"), |
| 124 | + Tag: GossipMessage_EMPTY, |
| 125 | + Nonce: 5, |
| 126 | + Content: &GossipMessage_StateResponse{ |
| 127 | + StateResponse: &RemoteStateResponse{ |
| 128 | + Payloads: []*Payload{ |
| 129 | + {Data: []byte{2, 2, 2}}, |
| 130 | + }, |
| 131 | + }, |
| 132 | + }, |
| 133 | + }, |
| 134 | + Envelope: envelopes()[0], |
| 135 | + } |
| 136 | + assert.NotContains(t, fmt.Sprintf("%v", sMsg), "2") |
| 137 | +} |
| 138 | + |
| 139 | +func envelopes() []*Envelope { |
| 140 | + return []*Envelope{ |
| 141 | + {Payload: []byte{2, 2, 2}, |
| 142 | + Signature: []byte{2, 2, 2}, |
| 143 | + SecretEnvelope: &SecretEnvelope{ |
| 144 | + Payload: []byte{2, 2, 2}, |
| 145 | + Signature: []byte{2, 2, 2}, |
| 146 | + }, |
| 147 | + }, |
| 148 | + } |
| 149 | +} |
0 commit comments