github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/orderer/kafka/util_test.go (about) 1 /* 2 Copyright IBM Corp. 2016 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 kafka 18 19 import ( 20 "testing" 21 22 "github.com/Shopify/sarama" 23 "github.com/hyperledger/fabric/common/configtx/tool/provisional" 24 "github.com/hyperledger/fabric/orderer/localconfig" 25 "github.com/hyperledger/fabric/orderer/mocks/util" 26 "github.com/stretchr/testify/assert" 27 ) 28 29 func TestProducerConfigMessageMaxBytes(t *testing.T) { 30 broker := sarama.NewMockBroker(t, 1) 31 defer func() { 32 broker.Close() 33 }() 34 broker.SetHandlerByMap(map[string]sarama.MockResponse{ 35 "MetadataRequest": sarama.NewMockMetadataResponse(t). 36 SetBroker(broker.Addr(), broker.BrokerID()). 37 SetLeader(cp.Topic(), cp.Partition(), broker.BrokerID()), 38 "ProduceRequest": sarama.NewMockProduceResponse(t), 39 }) 40 41 mockTLS := config.TLS{Enabled: false} 42 config := newBrokerConfig(testConf.Kafka.Version, rawPartition, mockTLS) 43 producer, err := sarama.NewSyncProducer([]string{broker.Addr()}, config) 44 if err != nil { 45 t.Fatal(err) 46 } 47 defer func() { 48 producer.Close() 49 }() 50 51 testCases := []struct { 52 name string 53 size int 54 err error 55 }{ 56 {"TypicalDeploy", 8 * 1024 * 1024, nil}, 57 {"TooBig", 100*1024*1024 + 1, sarama.ErrMessageSizeTooLarge}, 58 } 59 60 for _, tc := range testCases { 61 t.Run(tc.name, func(t *testing.T) { 62 _, _, err = producer.SendMessage(&sarama.ProducerMessage{ 63 Topic: cp.Topic(), 64 Value: sarama.ByteEncoder(make([]byte, tc.size)), 65 }) 66 if err != tc.err { 67 t.Fatal(err) 68 } 69 }) 70 71 } 72 } 73 74 func TestNewBrokerConfig(t *testing.T) { 75 // Use a partition ID that is not the 'default' (rawPartition) 76 var differentPartition int32 = 2 77 cp = newChainPartition(provisional.TestChainID, differentPartition) 78 79 // Setup a mock broker that reports that it has 3 partitions for the topic 80 broker := sarama.NewMockBroker(t, 1) 81 defer func() { 82 broker.Close() 83 }() 84 broker.SetHandlerByMap(map[string]sarama.MockResponse{ 85 "MetadataRequest": sarama.NewMockMetadataResponse(t). 86 SetBroker(broker.Addr(), broker.BrokerID()). 87 SetLeader(cp.Topic(), 0, broker.BrokerID()). 88 SetLeader(cp.Topic(), 1, broker.BrokerID()). 89 SetLeader(cp.Topic(), 2, broker.BrokerID()), 90 "ProduceRequest": sarama.NewMockProduceResponse(t), 91 }) 92 93 config := newBrokerConfig(testConf.Kafka.Version, differentPartition, config.TLS{Enabled: false}) 94 producer, err := sarama.NewSyncProducer([]string{broker.Addr()}, config) 95 if err != nil { 96 t.Fatal("Failed to create producer:", err) 97 } 98 defer func() { 99 producer.Close() 100 }() 101 102 for i := 0; i < 10; i++ { 103 assignedPartition, _, err := producer.SendMessage(&sarama.ProducerMessage{Topic: cp.Topic()}) 104 if err != nil { 105 t.Fatal("Failed to send message:", err) 106 } 107 if assignedPartition != differentPartition { 108 t.Fatalf("Message wasn't posted to the right partition - expected: %d, got %v", differentPartition, assignedPartition) 109 } 110 } 111 } 112 113 func TestTLSConfigEnabled(t *testing.T) { 114 publicKey, privateKey, err := util.GenerateMockPublicPrivateKeyPairPEM(false) 115 if err != nil { 116 t.Fatalf("Enable to generate a public/private key pair: %v", err) 117 } 118 caPublicKey, _, err := util.GenerateMockPublicPrivateKeyPairPEM(true) 119 if err != nil { 120 t.Fatalf("Enable to generate a signer certificate: %v", err) 121 } 122 123 config := newBrokerConfig(testConf.Kafka.Version, 0, config.TLS{ 124 Enabled: true, 125 PrivateKey: privateKey, 126 Certificate: publicKey, 127 RootCAs: []string{caPublicKey}, 128 }) 129 130 assert.True(t, config.Net.TLS.Enable) 131 assert.NotNil(t, config.Net.TLS.Config) 132 assert.Len(t, config.Net.TLS.Config.Certificates, 1) 133 assert.Len(t, config.Net.TLS.Config.RootCAs.Subjects(), 1) 134 assert.Equal(t, uint16(0), config.Net.TLS.Config.MaxVersion) 135 assert.Equal(t, uint16(0), config.Net.TLS.Config.MinVersion) 136 } 137 138 func TestTLSConfigDisabled(t *testing.T) { 139 publicKey, privateKey, err := util.GenerateMockPublicPrivateKeyPairPEM(false) 140 if err != nil { 141 t.Fatalf("Enable to generate a public/private key pair: %v", err) 142 } 143 caPublicKey, _, err := util.GenerateMockPublicPrivateKeyPairPEM(true) 144 if err != nil { 145 t.Fatalf("Enable to generate a signer certificate: %v", err) 146 } 147 148 config := newBrokerConfig(testConf.Kafka.Version, 0, config.TLS{ 149 Enabled: false, 150 PrivateKey: privateKey, 151 Certificate: publicKey, 152 RootCAs: []string{caPublicKey}, 153 }) 154 155 assert.False(t, config.Net.TLS.Enable) 156 assert.Zero(t, config.Net.TLS.Config) 157 158 } 159 160 func TestTLSConfigBadCert(t *testing.T) { 161 publicKey, privateKey, err := util.GenerateMockPublicPrivateKeyPairPEM(false) 162 if err != nil { 163 t.Fatalf("Enable to generate a public/private key pair: %v", err) 164 } 165 caPublicKey, _, err := util.GenerateMockPublicPrivateKeyPairPEM(true) 166 if err != nil { 167 t.Fatalf("Enable to generate a signer certificate: %v", err) 168 } 169 170 t.Run("BadPrivateKey", func(t *testing.T) { 171 assert.Panics(t, func() { 172 newBrokerConfig(testConf.Kafka.Version, 0, config.TLS{ 173 Enabled: true, 174 PrivateKey: privateKey, 175 Certificate: "TRASH", 176 RootCAs: []string{caPublicKey}, 177 }) 178 }) 179 }) 180 t.Run("BadPublicKey", func(t *testing.T) { 181 assert.Panics(t, func() { 182 newBrokerConfig(testConf.Kafka.Version, 0, config.TLS{ 183 Enabled: true, 184 PrivateKey: "TRASH", 185 Certificate: publicKey, 186 RootCAs: []string{caPublicKey}, 187 }) 188 }) 189 }) 190 t.Run("BadRootCAs", func(t *testing.T) { 191 assert.Panics(t, func() { 192 newBrokerConfig(testConf.Kafka.Version, 0, config.TLS{ 193 Enabled: true, 194 PrivateKey: privateKey, 195 Certificate: publicKey, 196 RootCAs: []string{"TRASH"}, 197 }) 198 }) 199 }) 200 }