Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lwm2m2 unitest #1021

Closed
wants to merge 14 commits into from
54 changes: 54 additions & 0 deletions pkg/deviceshifu/deviceshifulwm2m/deviceshifulwm2mconfig_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package deviceshifulwm2m

import (
"reflect"
"testing"

"github.com/edgenesis/shifu/pkg/deviceshifu/deviceshifubase"
"github.com/stretchr/testify/assert"
)

func TestCreateLwM2MInstructions(t *testing.T) {
tests := []struct {
name string
input *deviceshifubase.DeviceShifuInstructions
expected *LwM2MInstruction
expectingPanic bool
beingStrongeryqqq marked this conversation as resolved.
Show resolved Hide resolved
}{
{
beingStrongeryqqq marked this conversation as resolved.
Show resolved Hide resolved
name: "Valid Instructions",
input: &deviceshifubase.DeviceShifuInstructions{
Instructions: map[string]*deviceshifubase.DeviceShifuInstruction{
"instruction1": {
DeviceShifuProtocolProperties: map[string]string{
objectIdStr: "1",
enableObserveStr: "true",
},
},
},
},
expected: &LwM2MInstruction{
Instructions: map[string]*LwM2MProtocolProperty{
"instruction1": {
ObjectId: "1",
EnableObserve: true,
},
},
},
expectingPanic: false,
},
}

beingStrongeryqqq marked this conversation as resolved.
Show resolved Hide resolved
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.expectingPanic {
assert.Panics(t, func() { CreateLwM2MInstructions(tt.input) })
} else {
result := CreateLwM2MInstructions(tt.input)
if !reflect.DeepEqual(tt.expected, result) {
t.Errorf("expected %v, got %v", tt.expected, result)
}
beingStrongeryqqq marked this conversation as resolved.
Show resolved Hide resolved
}
})
}
}
66 changes: 66 additions & 0 deletions pkg/deviceshifu/deviceshifulwm2m/lwm2m/ciphersuite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package lwm2m

import (
"reflect"
"testing"

"github.com/edgenesis/shifu/pkg/k8s/api/v1alpha1"
"github.com/pion/dtls/v2"
)

func TestCipherSuiteStringsToCodes(t *testing.T) {
beingStrongeryqqq marked this conversation as resolved.
Show resolved Hide resolved
tests := []struct {
name string
input []v1alpha1.CipherSuite
expectedOutput []dtls.CipherSuiteID
expectError bool
}{
{
name: "Valid cipher suites",
input: []v1alpha1.CipherSuite{
v1alpha1.CipherSuite_TLS_ECDHE_ECDSA_WITH_AES_128_CCM,
v1alpha1.CipherSuite_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
},
expectedOutput: []dtls.CipherSuiteID{
TLS_ECDHE_ECDSA_WITH_AES_128_CCM,
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
},
expectError: false,
},
{
name: "Invalid cipher suite",
input: []v1alpha1.CipherSuite{
v1alpha1.CipherSuite("INVALID_CIPHER_SUITE"),
},
expectedOutput: nil,
expectError: true,
},
{
name: "Mixed valid and invalid cipher suites",
input: []v1alpha1.CipherSuite{
v1alpha1.CipherSuite_TLS_ECDHE_ECDSA_WITH_AES_128_CCM,
v1alpha1.CipherSuite("INVALID_CIPHER_SUITE"),
},
expectedOutput: nil,
expectError: true,
},
{
name: "Empty input",
input: []v1alpha1.CipherSuite{},
expectedOutput: []dtls.CipherSuiteID{},
expectError: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
output, err := CipherSuiteStringsToCodes(tt.input)
if (err != nil) != tt.expectError {
t.Errorf("expected error: %v, got: %v", tt.expectError, err)
}
if !tt.expectError && !reflect.DeepEqual(output, tt.expectedOutput) {
t.Errorf("expected output: %v, got: %v", tt.expectedOutput, output)
}
})
}
}
Loading