github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/hyperledger/fabric-config/configtx/channel_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package configtx 8 9 import ( 10 "bytes" 11 "github.com/hellobchain/third_party/hyperledger/fabric-config/protolator" 12 "github.com/hellobchain/third_party/hyperledger/fabric-config/protolator/protoext/commonext" 13 "testing" 14 15 cb "github.com/hyperledger/fabric-protos-go/common" 16 . "github.com/onsi/gomega" 17 ) 18 19 func TestChannelCapabilities(t *testing.T) { 20 t.Parallel() 21 22 gt := NewGomegaWithT(t) 23 24 expectedCapabilities := []string{"V1_3"} 25 26 config := &cb.Config{ 27 ChannelGroup: &cb.ConfigGroup{ 28 Values: map[string]*cb.ConfigValue{}, 29 }, 30 } 31 32 err := setValue(config.ChannelGroup, capabilitiesValue(expectedCapabilities), AdminsPolicyKey) 33 gt.Expect(err).NotTo(HaveOccurred()) 34 35 c := New(config) 36 37 channelCapabilities, err := c.Channel().Capabilities() 38 gt.Expect(err).NotTo(HaveOccurred()) 39 gt.Expect(channelCapabilities).To(Equal(expectedCapabilities)) 40 41 // Delete the capabilities key and assert retrieval to return nil 42 delete(c.Channel().channelGroup.Values, CapabilitiesKey) 43 channelCapabilities, err = c.Channel().Capabilities() 44 gt.Expect(err).NotTo(HaveOccurred()) 45 gt.Expect(channelCapabilities).To(BeNil()) 46 } 47 48 func TestSetChannelCapability(t *testing.T) { 49 t.Parallel() 50 51 gt := NewGomegaWithT(t) 52 53 config := &cb.Config{ 54 ChannelGroup: &cb.ConfigGroup{ 55 Values: map[string]*cb.ConfigValue{ 56 CapabilitiesKey: {}, 57 }, 58 }, 59 } 60 61 c := New(config) 62 63 expectedConfigGroupJSON := `{ 64 "groups": {}, 65 "mod_policy": "", 66 "policies": {}, 67 "values": { 68 "Capabilities": { 69 "mod_policy": "Admins", 70 "value": { 71 "capabilities": { 72 "V3_0": {} 73 } 74 }, 75 "version": "0" 76 } 77 }, 78 "version": "0" 79 } 80 ` 81 82 err := c.Channel().AddCapability("V3_0") 83 gt.Expect(err).NotTo(HaveOccurred()) 84 85 buf := bytes.Buffer{} 86 err = protolator.DeepMarshalJSON(&buf, &commonext.DynamicChannelGroup{ConfigGroup: c.Channel().channelGroup}) 87 gt.Expect(err).NotTo(HaveOccurred()) 88 89 gt.Expect(buf.String()).To(Equal(expectedConfigGroupJSON)) 90 } 91 92 func TestSetChannelCapabilityFailures(t *testing.T) { 93 t.Parallel() 94 95 tests := []struct { 96 testName string 97 capability string 98 config *cb.Config 99 expectedErr string 100 }{ 101 { 102 testName: "when retrieving existing capabilities", 103 capability: "V2_0", 104 config: &cb.Config{ 105 ChannelGroup: &cb.ConfigGroup{ 106 Values: map[string]*cb.ConfigValue{ 107 CapabilitiesKey: { 108 Value: []byte("foobar"), 109 }, 110 }, 111 }, 112 }, 113 expectedErr: "retrieving channel capabilities: unmarshaling capabilities: proto: can't skip unknown wire type 6", 114 }, 115 } 116 117 for _, tt := range tests { 118 tt := tt 119 t.Run(tt.testName, func(t *testing.T) { 120 t.Parallel() 121 122 gt := NewGomegaWithT(t) 123 124 c := New(tt.config) 125 126 err := c.Channel().AddCapability(tt.capability) 127 gt.Expect(err).To(MatchError(tt.expectedErr)) 128 }) 129 } 130 } 131 132 func TestRemoveChannelCapability(t *testing.T) { 133 t.Parallel() 134 135 gt := NewGomegaWithT(t) 136 137 config := &cb.Config{ 138 ChannelGroup: &cb.ConfigGroup{ 139 Values: map[string]*cb.ConfigValue{ 140 CapabilitiesKey: { 141 Value: marshalOrPanic(&cb.Capabilities{Capabilities: map[string]*cb.Capability{ 142 "V3_0": {}, 143 }}), 144 ModPolicy: AdminsPolicyKey, 145 }, 146 }, 147 }, 148 } 149 150 c := New(config) 151 152 expectedConfigGroupJSON := `{ 153 "groups": {}, 154 "mod_policy": "", 155 "policies": {}, 156 "values": { 157 "Capabilities": { 158 "mod_policy": "Admins", 159 "value": { 160 "capabilities": {} 161 }, 162 "version": "0" 163 } 164 }, 165 "version": "0" 166 } 167 ` 168 169 err := c.Channel().RemoveCapability("V3_0") 170 gt.Expect(err).NotTo(HaveOccurred()) 171 172 buf := bytes.Buffer{} 173 err = protolator.DeepMarshalJSON(&buf, &commonext.DynamicChannelGroup{ConfigGroup: c.Channel().channelGroup}) 174 gt.Expect(err).NotTo(HaveOccurred()) 175 176 gt.Expect(buf.String()).To(Equal(expectedConfigGroupJSON)) 177 } 178 179 func TestRemoveChannelCapabilityFailures(t *testing.T) { 180 t.Parallel() 181 182 tests := []struct { 183 testName string 184 capability string 185 config *cb.Config 186 expectedErr string 187 }{ 188 { 189 testName: "when capability does not exist", 190 capability: "V2_0", 191 config: &cb.Config{ 192 ChannelGroup: &cb.ConfigGroup{ 193 Values: map[string]*cb.ConfigValue{ 194 CapabilitiesKey: { 195 ModPolicy: AdminsPolicyKey, 196 }, 197 }, 198 }, 199 }, 200 expectedErr: "capability not set", 201 }, 202 { 203 testName: "when retrieving existing capabilities", 204 capability: "V2_0", 205 config: &cb.Config{ 206 ChannelGroup: &cb.ConfigGroup{ 207 Values: map[string]*cb.ConfigValue{ 208 CapabilitiesKey: { 209 Value: []byte("foobar"), 210 }, 211 }, 212 }, 213 }, 214 expectedErr: "retrieving channel capabilities: unmarshaling capabilities: proto: can't skip unknown wire type 6", 215 }, 216 } 217 218 for _, tt := range tests { 219 tt := tt 220 t.Run(tt.testName, func(t *testing.T) { 221 t.Parallel() 222 223 gt := NewGomegaWithT(t) 224 225 c := New(tt.config) 226 227 err := c.Channel().RemoveCapability(tt.capability) 228 gt.Expect(err).To(MatchError(tt.expectedErr)) 229 }) 230 } 231 } 232 233 func TestSetChannelModPolicy(t *testing.T) { 234 t.Parallel() 235 gt := NewGomegaWithT(t) 236 237 channel, _, err := baseApplicationChannelGroup(t) 238 gt.Expect(err).NotTo(HaveOccurred()) 239 240 config := &cb.Config{ 241 ChannelGroup: channel, 242 } 243 c := New(config) 244 245 err = c.Channel().SetModPolicy("TestModPolicy") 246 gt.Expect(err).NotTo(HaveOccurred()) 247 248 updatedChannelModPolicy := c.Channel().channelGroup.GetModPolicy() 249 250 gt.Expect(updatedChannelModPolicy).To(Equal("TestModPolicy")) 251 } 252 253 func TestSetChannelModPolicFailure(t *testing.T) { 254 t.Parallel() 255 gt := NewGomegaWithT(t) 256 257 channel, _, err := baseApplicationChannelGroup(t) 258 gt.Expect(err).NotTo(HaveOccurred()) 259 260 config := &cb.Config{ 261 ChannelGroup: channel, 262 } 263 c := New(config) 264 265 err = c.Channel().SetModPolicy("") 266 gt.Expect(err).To(MatchError("non empty mod policy is required")) 267 } 268 269 func TestSetChannelPolicy(t *testing.T) { 270 t.Parallel() 271 gt := NewGomegaWithT(t) 272 273 channel, _, err := baseApplicationChannelGroup(t) 274 gt.Expect(err).NotTo(HaveOccurred()) 275 276 config := &cb.Config{ 277 ChannelGroup: channel, 278 } 279 c := New(config) 280 281 expectedPolicies := map[string]Policy{ 282 "TestPolicy": {Type: ImplicitMetaPolicyType, Rule: "ANY Readers", ModPolicy: AdminsPolicyKey}, 283 } 284 285 err = c.Channel().SetPolicy("TestPolicy", Policy{Type: ImplicitMetaPolicyType, Rule: "ANY Readers"}) 286 gt.Expect(err).NotTo(HaveOccurred()) 287 288 updatedChannelPolicy, err := getPolicies(c.updated.ChannelGroup.Policies) 289 gt.Expect(err).NotTo(HaveOccurred()) 290 gt.Expect(updatedChannelPolicy).To(Equal(expectedPolicies)) 291 292 baseChannel := c.original.ChannelGroup 293 gt.Expect(baseChannel.Policies).To(HaveLen(0)) 294 gt.Expect(baseChannel.Policies["TestPolicy"]).To(BeNil()) 295 } 296 297 func TestSetChannelPolicies(t *testing.T) { 298 t.Parallel() 299 gt := NewGomegaWithT(t) 300 301 channel, _, err := baseApplicationChannelGroup(t) 302 gt.Expect(err).NotTo(HaveOccurred()) 303 304 config := &cb.Config{ 305 ChannelGroup: channel, 306 } 307 basePolicies := standardPolicies() 308 basePolicies["TestPolicy_Remove"] = Policy{Type: ImplicitMetaPolicyType, Rule: "ANY Readers"} 309 err = setPolicies(channel, basePolicies) 310 gt.Expect(err).NotTo(HaveOccurred()) 311 312 c := New(config) 313 314 newPolicies := map[string]Policy{ 315 ReadersPolicyKey: { 316 Type: ImplicitMetaPolicyType, 317 Rule: "ANY Readers", 318 ModPolicy: AdminsPolicyKey, 319 }, 320 WritersPolicyKey: { 321 Type: ImplicitMetaPolicyType, 322 Rule: "ANY Writers", 323 ModPolicy: AdminsPolicyKey, 324 }, 325 AdminsPolicyKey: { 326 Type: ImplicitMetaPolicyType, 327 Rule: "ANY Admins", 328 ModPolicy: AdminsPolicyKey, 329 }, 330 "TestPolicy_Add1": { 331 Type: ImplicitMetaPolicyType, 332 Rule: "ANY Readers", 333 ModPolicy: AdminsPolicyKey, 334 }, 335 "TestPolicy_Add2": { 336 Type: ImplicitMetaPolicyType, 337 Rule: "ANY Writers", 338 ModPolicy: AdminsPolicyKey, 339 }, 340 } 341 342 err = c.Channel().SetPolicies(newPolicies) 343 gt.Expect(err).NotTo(HaveOccurred()) 344 345 updatedChannelPolicies, err := c.Channel().Policies() 346 gt.Expect(err).NotTo(HaveOccurred()) 347 gt.Expect(updatedChannelPolicies).To(Equal(newPolicies)) 348 349 originalChannel := c.original.ChannelGroup 350 gt.Expect(originalChannel.Policies).To(Equal(config.ChannelGroup.Policies)) 351 } 352 353 func TestSetChannelPoliciesFailures(t *testing.T) { 354 t.Parallel() 355 gt := NewGomegaWithT(t) 356 357 channel, _, err := baseApplicationChannelGroup(t) 358 gt.Expect(err).NotTo(HaveOccurred()) 359 360 config := &cb.Config{ 361 ChannelGroup: channel, 362 } 363 c := New(config) 364 365 newPolicies := map[string]Policy{ 366 ReadersPolicyKey: { 367 Type: ImplicitMetaPolicyType, 368 Rule: "ANY Readers", 369 }, 370 WritersPolicyKey: { 371 Type: ImplicitMetaPolicyType, 372 Rule: "ANY Writers", 373 }, 374 AdminsPolicyKey: { 375 Type: ImplicitMetaPolicyType, 376 Rule: "MAJORITY Admins", 377 }, 378 "TestPolicy": {}, 379 } 380 381 err = c.Channel().SetPolicies(newPolicies) 382 gt.Expect(err).To(MatchError("unknown policy type: ")) 383 } 384 385 func TestRemoveChannelPolicy(t *testing.T) { 386 t.Parallel() 387 gt := NewGomegaWithT(t) 388 389 channel, _, err := baseApplicationChannelGroup(t) 390 gt.Expect(err).NotTo(HaveOccurred()) 391 392 config := &cb.Config{ 393 ChannelGroup: channel, 394 } 395 policies := standardPolicies() 396 err = setPolicies(channel, policies) 397 gt.Expect(err).NotTo(HaveOccurred()) 398 c := New(config) 399 400 expectedPolicies := map[string]Policy{ 401 "Admins": { 402 Type: "ImplicitMeta", 403 Rule: "MAJORITY Admins", 404 ModPolicy: AdminsPolicyKey, 405 }, 406 "Writers": { 407 Type: "ImplicitMeta", 408 Rule: "ANY Writers", 409 ModPolicy: AdminsPolicyKey, 410 }, 411 } 412 413 err = c.Channel().RemovePolicy(ReadersPolicyKey) 414 gt.Expect(err).NotTo(HaveOccurred()) 415 416 updatedChannelPolicy, err := c.Channel().Policies() 417 gt.Expect(err).NotTo(HaveOccurred()) 418 gt.Expect(updatedChannelPolicy).To(Equal(expectedPolicies)) 419 420 originalChannel := c.original.ChannelGroup 421 gt.Expect(originalChannel.Policies).To(HaveLen(3)) 422 gt.Expect(originalChannel.Policies[ReadersPolicyKey]).ToNot(BeNil()) 423 } 424 425 func TestRemoveChannelPolicyFailures(t *testing.T) { 426 t.Parallel() 427 gt := NewGomegaWithT(t) 428 429 channel, _, err := baseApplicationChannelGroup(t) 430 gt.Expect(err).NotTo(HaveOccurred()) 431 432 config := &cb.Config{ 433 ChannelGroup: channel, 434 } 435 policies := standardPolicies() 436 err = setPolicies(channel, policies) 437 gt.Expect(err).NotTo(HaveOccurred()) 438 channel.Policies[ReadersPolicyKey] = &cb.ConfigPolicy{ 439 Policy: &cb.Policy{ 440 Type: 15, 441 }, 442 } 443 c := New(config) 444 445 err = c.Channel().RemovePolicy(ReadersPolicyKey) 446 gt.Expect(err).To(MatchError("unknown policy type: 15")) 447 } 448 449 func TestRemoveLegacyOrdererAddresses(t *testing.T) { 450 t.Parallel() 451 gt := NewGomegaWithT(t) 452 453 config := &cb.Config{ 454 ChannelGroup: &cb.ConfigGroup{ 455 Values: map[string]*cb.ConfigValue{ 456 OrdererAddressesKey: { 457 ModPolicy: AdminsPolicyKey, 458 Value: marshalOrPanic(&cb.OrdererAddresses{ 459 Addresses: []string{"127.0.0.1:8050"}, 460 }), 461 }, 462 }, 463 }, 464 } 465 466 c := New(config) 467 468 c.Channel().RemoveLegacyOrdererAddresses() 469 470 _, exists := c.Channel().channelGroup.Values[OrdererAddressesKey] 471 gt.Expect(exists).To(BeFalse()) 472 } 473 474 func TestConfigurationFailures(t *testing.T) { 475 t.Parallel() 476 477 tests := []struct { 478 testName string 479 config *cb.Config 480 expectedErr string 481 }{ 482 { 483 testName: "when retrieving existing Consortium", 484 config: &cb.Config{ 485 ChannelGroup: &cb.ConfigGroup{ 486 Values: map[string]*cb.ConfigValue{ 487 ConsortiumKey: { 488 Value: []byte("foobar"), 489 }, 490 }, 491 }, 492 }, 493 expectedErr: "unmarshaling Consortium: proto: can't skip unknown wire type 6", 494 }, 495 { 496 testName: "when retrieving existing orderer group", 497 config: &cb.Config{ 498 ChannelGroup: &cb.ConfigGroup{ 499 Groups: map[string]*cb.ConfigGroup{ 500 OrdererGroupKey: {}, 501 }, 502 }, 503 }, 504 expectedErr: "cannot determine consensus type of orderer", 505 }, 506 { 507 testName: "when retrieving existing application group", 508 config: &cb.Config{ 509 ChannelGroup: &cb.ConfigGroup{ 510 Groups: map[string]*cb.ConfigGroup{ 511 ApplicationGroupKey: { 512 Groups: map[string]*cb.ConfigGroup{ 513 "Org1": { 514 Values: map[string]*cb.ConfigValue{ 515 "foobar": { 516 Value: []byte("foobar"), 517 }, 518 }, 519 }, 520 }, 521 }, 522 }, 523 }, 524 }, 525 expectedErr: "retrieving application org Org1: config does not contain value for MSP", 526 }, 527 { 528 testName: "when retrieving existing consortiums group", 529 config: &cb.Config{ 530 ChannelGroup: &cb.ConfigGroup{ 531 Groups: map[string]*cb.ConfigGroup{ 532 ConsortiumsGroupKey: { 533 Groups: map[string]*cb.ConfigGroup{ 534 "Consortium1": { 535 Groups: map[string]*cb.ConfigGroup{ 536 "Org1": { 537 Values: map[string]*cb.ConfigValue{ 538 "foobar": { 539 Value: []byte("foobar"), 540 }, 541 }, 542 }, 543 }, 544 }, 545 }, 546 }, 547 }, 548 }, 549 }, 550 expectedErr: "failed to retrieve organization Org1 from consortium Consortium1: ", 551 }, 552 { 553 testName: "when retrieving existing policies", 554 config: &cb.Config{ 555 ChannelGroup: &cb.ConfigGroup{ 556 Policies: map[string]*cb.ConfigPolicy{ 557 AdminsPolicyKey: { 558 Policy: &cb.Policy{ 559 Value: []byte("foobar"), 560 }, 561 }, 562 }, 563 }, 564 }, 565 expectedErr: "unknown policy type: 0", 566 }, 567 { 568 testName: "when retrieving existing capabilities", 569 config: &cb.Config{ 570 ChannelGroup: &cb.ConfigGroup{ 571 Values: map[string]*cb.ConfigValue{ 572 CapabilitiesKey: { 573 Value: []byte("foobar"), 574 }, 575 }, 576 }, 577 }, 578 expectedErr: "retrieving channel capabilities: unmarshaling capabilities: proto: can't skip unknown wire type 6", 579 }, 580 } 581 582 for _, tt := range tests { 583 tt := tt 584 t.Run(tt.testName, func(t *testing.T) { 585 t.Parallel() 586 587 gt := NewGomegaWithT(t) 588 589 c := New(tt.config) 590 591 _, err := c.Channel().Configuration() 592 gt.Expect(err).To(MatchError(tt.expectedErr)) 593 }) 594 } 595 }