MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / TestEncryptDecrypt

Function TestEncryptDecrypt

pkg/crypto/encryption_test.go:28–70  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

26)
27
28func TestEncryptDecrypt(t *testing.T) {
29 for _, tc := range []struct {
30 Name string
31 Message []byte
32 }{
33 {
34 Name: "SmallMessage",
35 Message: []byte("hello"),
36 },
37 {
38 Name: "APIKey",
39 Message: []byte("NNSXS.W5A2226537J2PWZ7YOCJZPUUSFKM6RJ4RGCL2YY.AICDXTLG7CIEDTP3JD4JVYTMGZN75DU5WSKU7RNRZELAJFRWQCHA"),
40 },
41 {
42 Name: "PrivateKey",
43 Message: []byte("-----BEGIN EC PRIVATE KEY-----MHcCAQEEIBVXljefOUPY++0sovcF0dboOLEJz4eZ9DoUE8o9Y7GHoAoGCCqGSM49AwEHoUQDQgAEjf3zZPXlc/sseTt7YzF0o61feXvk98JFyy+s/j0gzMzUjEka7+WzTPERi9uMQjERns1qXG/9DJLe/Qxi0r84hA==-----END EC PRIVATE KEY-----"),
44 },
45 } {
46 t.Run(fmt.Sprintf("%s", tc.Name), func(t *testing.T) {
47 a := assertions.New(t)
48 var key types.AES128Key
49 _, err := rand.Read(key[:])
50 if !a.So(err, should.BeNil) {
51 t.Fatalf("Failed to generate key: %v", err)
52 }
53 encrypted, err := Encrypt(key, tc.Message)
54 if !a.So(err, should.BeNil) {
55 t.Fatalf("Failed to encrypt message: %v", err)
56 }
57 expectedCipherLen := 12 + 16 + len(tc.Message) // Nonce + Tag + Message
58 if !a.So(len(encrypted), should.Equal, expectedCipherLen) {
59 t.Fatalf("Invalid cipher length: %v", len(encrypted))
60 }
61 decrypted, err := Decrypt(key, encrypted)
62 if !a.So(err, should.BeNil) {
63 t.Fatalf("Failed to decrypt message: %v", err)
64 }
65 if !a.So(tc.Message, should.Resemble, decrypted) {
66 t.Fatalf("Failed to decrypt message: %v", err)
67 }
68 })
69 }
70}

Callers

nothing calls this directly

Calls 6

EncryptFunction · 0.85
DecryptFunction · 0.85
RunMethod · 0.65
NewMethod · 0.65
FatalfMethod · 0.65
ReadMethod · 0.45

Tested by

no test coverage detected