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
60 changes: 60 additions & 0 deletions pkg/deviceshifu/deviceshifulwm2m/deviceshifulwm2mconfig_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package deviceshifulwm2m

import (
"testing"

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

func TestCreateLwM2MInstructions(t *testing.T) {
// Initialize test data
dsInstructions := &deviceshifubase.DeviceShifuInstructions{
Instructions: map[string]*deviceshifubase.DeviceShifuInstruction{
"instruction1": {
DeviceShifuProtocolProperties: map[string]string{
objectIdStr: "123",
enableObserveStr: "true",
},
},
"instruction2": {
DeviceShifuProtocolProperties: map[string]string{
objectIdStr: "456",
enableObserveStr: "false",
},
},
},
}

beingStrongeryqqq marked this conversation as resolved.
Show resolved Hide resolved
// Call the function under test
result := CreateLwM2MInstructions(dsInstructions)

// Assert that the result is not nil
assert.NotNil(t, result)
assert.Equal(t, 2, len(result.Instructions))

// Check if each instruction's properties are correctly mapped
instruction1 := result.Instructions["instruction1"]
assert.NotNil(t, instruction1)
assert.Equal(t, "123", instruction1.ObjectId)
assert.True(t, instruction1.EnableObserve)

instruction2 := result.Instructions["instruction2"]
assert.NotNil(t, instruction2)
assert.Equal(t, "456", instruction2.ObjectId)
assert.False(t, instruction2.EnableObserve)
beingStrongeryqqq marked this conversation as resolved.
Show resolved Hide resolved
}

func TestCreateLwM2MInstructions_EmptyInstructions(t *testing.T) {
// Test the case of an empty instruction map
dsInstructions := &deviceshifubase.DeviceShifuInstructions{
Instructions: map[string]*deviceshifubase.DeviceShifuInstruction{},
}

// Call the function under test
result := CreateLwM2MInstructions(dsInstructions)

// Assert that the result is not nil and the instruction map is empty
assert.NotNil(t, result)
assert.Equal(t, 0, len(result.Instructions))
}
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"

Check failure on line 8 in pkg/deviceshifu/deviceshifulwm2m/lwm2m/ciphersuite_test.go

View workflow job for this annotation

GitHub Actions / build

no required module provides package github.com/pion/dtls/v2; to add it:
)

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

Check failure on line 15 in pkg/deviceshifu/deviceshifulwm2m/lwm2m/ciphersuite_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: dtls (typecheck)
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{

Check failure on line 24 in pkg/deviceshifu/deviceshifulwm2m/lwm2m/ciphersuite_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: dtls (typecheck)
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{},

Check failure on line 50 in pkg/deviceshifu/deviceshifulwm2m/lwm2m/ciphersuite_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: dtls (typecheck)
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