github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/protocol/v1/security_reply_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  	"crypto/sha256"
     9  	"encoding/base64"
    10  
    11  	"github.com/choria-io/go-choria/inter"
    12  	imock "github.com/choria-io/go-choria/inter/imocks"
    13  	"github.com/choria-io/go-choria/protocol"
    14  	"github.com/golang/mock/gomock"
    15  	"github.com/tidwall/gjson"
    16  
    17  	. "github.com/onsi/ginkgo/v2"
    18  	. "github.com/onsi/gomega"
    19  )
    20  
    21  var _ = Describe("SecureReply", func() {
    22  	var mockctl *gomock.Controller
    23  	var security *imock.MockSecurityProvider
    24  
    25  	BeforeEach(func() {
    26  		mockctl = gomock.NewController(GinkgoT())
    27  		security = imock.NewMockSecurityProvider(mockctl)
    28  		security.EXPECT().BackingTechnology().Return(inter.SecurityTechnologyX509)
    29  	})
    30  
    31  	AfterEach(func() {
    32  		mockctl.Finish()
    33  	})
    34  
    35  	It("Should create valid replies", func() {
    36  		request, _ := NewRequest("test", "go.tests", "rip.mcollective", 120, "a2f0ca717c694f2086cfa81b6c494648", "mcollective")
    37  		request.SetMessage([]byte(`{"test":1}`))
    38  
    39  		reply, err := NewReply(request, "testing")
    40  		Expect(err).ToNot(HaveOccurred())
    41  
    42  		rj, err := reply.JSON()
    43  		Expect(err).ToNot(HaveOccurred())
    44  
    45  		sha := sha256.Sum256([]byte(rj))
    46  
    47  		security.EXPECT().ChecksumBytes([]byte(rj)).Return(sha[:]).AnyTimes()
    48  
    49  		sreply, _ := NewSecureReply(reply, security)
    50  		sj, err := sreply.JSON()
    51  		Expect(err).ToNot(HaveOccurred())
    52  
    53  		Expect(protocol.VersionFromJSON(sj)).To(Equal(protocol.SecureReplyV1))
    54  		Expect(gjson.GetBytes(sj, "message").String()).To(Equal(string(rj)))
    55  		Expect(gjson.GetBytes(sj, "hash").String()).To(Equal(base64.StdEncoding.EncodeToString(sha[:])))
    56  		Expect(sreply.Valid()).To(BeTrue())
    57  	})
    58  })