mirror of
https://codeberg.org/cunicu/cunicu.git
synced 2025-10-04 16:32:57 +08:00
52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
package signaling_test
|
|
|
|
import (
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
"github.com/stv0g/cunicu/pkg/crypto"
|
|
"github.com/stv0g/cunicu/test"
|
|
|
|
protoepdisc "github.com/stv0g/cunicu/pkg/proto/feature/epdisc"
|
|
signalingproto "github.com/stv0g/cunicu/pkg/proto/signaling"
|
|
)
|
|
|
|
var _ = Describe("message encryption", func() {
|
|
var c protoepdisc.Candidate
|
|
var ourKP, theirKP *crypto.KeyPair
|
|
var em signalingproto.EncryptedMessage
|
|
|
|
BeforeEach(func() {
|
|
var err error
|
|
|
|
c = protoepdisc.Candidate{
|
|
Foundation: "1234",
|
|
}
|
|
|
|
ourKP, theirKP, err = test.GenerateKeyPairs()
|
|
Expect(err).To(Succeed())
|
|
|
|
em = signalingproto.EncryptedMessage{}
|
|
err = em.Marshal(&c, ourKP)
|
|
Expect(err).To(Succeed(), "Failed to encrypt message: %s", err)
|
|
})
|
|
|
|
It("can en/decrypt a message", func() {
|
|
c2 := protoepdisc.Candidate{}
|
|
err := em.Unmarshal(&c2, theirKP)
|
|
|
|
Expect(err).To(Succeed(), "Failed to decrypt message: %s", err)
|
|
Expect(proto.Equal(&c, &c2)).To(BeTrue())
|
|
})
|
|
|
|
It("fails to decrypt an altered message", func() {
|
|
em.Body[0] ^= 1
|
|
|
|
c2 := protoepdisc.Candidate{}
|
|
err := em.Unmarshal(&c2, theirKP)
|
|
|
|
Expect(err).To(HaveOccurred(), "Decrypted invalid message: %s", err)
|
|
})
|
|
})
|