github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/protocol/v1/transport_test.go (about)

     1  // Copyright (c) 2017-2022, R.I. Pienaar and the Choria Project contributors
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package v1
     6  
     7  import (
     8  	"github.com/choria-io/go-choria/inter"
     9  	imock "github.com/choria-io/go-choria/inter/imocks"
    10  	"github.com/choria-io/go-choria/protocol"
    11  	"github.com/golang/mock/gomock"
    12  	. "github.com/onsi/ginkgo/v2"
    13  	. "github.com/onsi/gomega"
    14  	"github.com/tidwall/gjson"
    15  )
    16  
    17  var _ = Describe("TransportMessage", func() {
    18  	var mockctl *gomock.Controller
    19  	var security *imock.MockSecurityProvider
    20  
    21  	BeforeEach(func() {
    22  		mockctl = gomock.NewController(GinkgoT())
    23  		security = imock.NewMockSecurityProvider(mockctl)
    24  		security.EXPECT().BackingTechnology().Return(inter.SecurityTechnologyX509)
    25  	})
    26  
    27  	AfterEach(func() {
    28  		mockctl.Finish()
    29  	})
    30  
    31  	It("Should support reply data", func() {
    32  		security.EXPECT().ChecksumBytes(gomock.Any()).Return([]byte("stub checksum")).AnyTimes()
    33  
    34  		request, err := NewRequest("test", "go.tests", "rip.mcollective", 120, "a2f0ca717c694f2086cfa81b6c494648", "mcollective")
    35  		Expect(err).ToNot(HaveOccurred())
    36  		request.SetMessage([]byte(`{"message":1}`))
    37  		reply, err := NewReply(request, "testing")
    38  		Expect(err).ToNot(HaveOccurred())
    39  		sreply, err := NewSecureReply(reply, security)
    40  		Expect(err).ToNot(HaveOccurred())
    41  		treply, err := NewTransportMessage("rip.mcollective")
    42  		Expect(err).ToNot(HaveOccurred())
    43  		err = treply.SetReplyData(sreply)
    44  		Expect(err).ToNot(HaveOccurred())
    45  
    46  		sj, err := sreply.JSON()
    47  		Expect(err).ToNot(HaveOccurred())
    48  
    49  		j, err := treply.JSON()
    50  		Expect(err).ToNot(HaveOccurred())
    51  
    52  		Expect(protocol.VersionFromJSON(j)).To(Equal(protocol.TransportV1))
    53  		Expect(gjson.GetBytes(j, "headers.mc_sender").String()).To(Equal("rip.mcollective"))
    54  
    55  		d, err := treply.Message()
    56  		Expect(err).ToNot(HaveOccurred())
    57  
    58  		Expect(d).To(Equal(sj))
    59  	})
    60  
    61  	It("Should support request data", func() {
    62  		security.EXPECT().PublicCertBytes().Return([]byte("stub cert"), nil).AnyTimes()
    63  		security.EXPECT().SignBytes(gomock.Any()).Return([]byte("stub sig"), nil).AnyTimes()
    64  
    65  		request, err := NewRequest("test", "go.tests", "rip.mcollective", 120, "a2f0ca717c694f2086cfa81b6c494648", "mcollective")
    66  		Expect(err).ToNot(HaveOccurred())
    67  		request.SetMessage([]byte(`{"message":1}`))
    68  		srequest, err := NewSecureRequest(request, security)
    69  		Expect(err).ToNot(HaveOccurred())
    70  		trequest, err := NewTransportMessage("rip.mcollective")
    71  		Expect(err).ToNot(HaveOccurred())
    72  		trequest.SetRequestData(srequest)
    73  
    74  		sj, err := srequest.JSON()
    75  		Expect(err).ToNot(HaveOccurred())
    76  		j, err := trequest.JSON()
    77  		Expect(err).ToNot(HaveOccurred())
    78  
    79  		Expect(protocol.VersionFromJSON(j)).To(Equal(protocol.TransportV1))
    80  		Expect(gjson.GetBytes(j, "headers.mc_sender").String()).To(Equal("rip.mcollective"))
    81  
    82  		d, err := trequest.Message()
    83  		Expect(err).ToNot(HaveOccurred())
    84  
    85  		Expect(d).To(Equal(sj))
    86  	})
    87  
    88  	It("Should support creation from JSON data", func() {
    89  		protocol.ClientStrictValidation = true
    90  		defer func() { protocol.ClientStrictValidation = false }()
    91  
    92  		security.EXPECT().PublicCertBytes().Return([]byte("stub cert"), nil).AnyTimes()
    93  		security.EXPECT().SignBytes(gomock.Any()).Return([]byte("stub sig"), nil).AnyTimes()
    94  
    95  		request, err := NewRequest("test", "go.tests", "rip.mcollective", 120, "a2f0ca717c694f2086cfa81b6c494648", "mcollective")
    96  		Expect(err).ToNot(HaveOccurred())
    97  		request.SetMessage([]byte("hello world"))
    98  		srequest, err := NewSecureRequest(request, security)
    99  		Expect(err).ToNot(HaveOccurred())
   100  		trequest, err := NewTransportMessage("rip.mcollective")
   101  		Expect(err).ToNot(HaveOccurred())
   102  
   103  		Expect(trequest.SetRequestData(srequest)).To(Succeed())
   104  
   105  		j, err := trequest.JSON()
   106  		Expect(err).ToNot(HaveOccurred())
   107  
   108  		_, err = NewTransportFromJSON(j)
   109  		Expect(err).ToNot(HaveOccurred())
   110  
   111  		_, err = NewTransportFromJSON([]byte(`{"protocol": 1}`))
   112  		Expect(err).To(MatchError("supplied JSON document is not a valid Transport message: missing properties: 'data', 'headers', /protocol: expected string, but got number"))
   113  	})
   114  })