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

import (
"testing"

"github.com/edgenesis/shifu/pkg/deviceshifu/deviceshifubase"
)

func TestCreateLwM2MInstructions(t *testing.T) {
tests := []struct {
InstructionName string
beingStrongeryqqq marked this conversation as resolved.
Show resolved Hide resolved
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
InstructionName: "Valid instructions and enableObserveStr",
input: &deviceshifubase.DeviceShifuInstructions{
Instructions: map[string]*deviceshifubase.DeviceShifuInstruction{
"instruction": {
DeviceShifuProtocolProperties: map[string]string{
objectIdStr: "1",
enableObserveStr: "true",
},
},
},
},
expected: &LwM2MInstruction{
Instructions: map[string]*LwM2MProtocolProperty{
"instruction": {
ObjectId: "1",
EnableObserve: true,
},
},
},
expectingPanic: false,
},
{
InstructionName: "Valid instructions and disableObserveStr",
input: &deviceshifubase.DeviceShifuInstructions{
Instructions: map[string]*deviceshifubase.DeviceShifuInstruction{
"instruction": {
DeviceShifuProtocolProperties: map[string]string{
objectIdStr: "1",
enableObserveStr: "false",
},
},
},
},
expected: &LwM2MInstruction{
Instructions: map[string]*LwM2MProtocolProperty{
"instruction": {
ObjectId: "1",
EnableObserve: false,
},
},
},
expectingPanic: false,
},
}

beingStrongeryqqq marked this conversation as resolved.
Show resolved Hide resolved
for _, tt := range tests {
t.Run(tt.InstructionName, func(t *testing.T) {
// CreateLwM2MInstructions's logger.Fatalf will end program with no panic
res := CreateLwM2MInstructions(tt.input)
if !tt.expectingPanic && !compareLwM2MInstructions(res, tt.expected) {
t.Errorf("Test case %s failed: expected %v, got %v", tt.InstructionName, tt.expected, res)
}
})
}
}

func compareLwM2MInstructions(a, b *LwM2MInstruction) bool {
beingStrongeryqqq marked this conversation as resolved.
Show resolved Hide resolved
if len(a.Instructions) != len(b.Instructions) {
return false
}
for key, valA := range a.Instructions {
valB, ok := b.Instructions[key]
if !ok || valA.ObjectId != valB.ObjectId || valA.EnableObserve != valB.EnableObserve {
return false
}
}
return true
}
94 changes: 94 additions & 0 deletions pkg/deviceshifu/deviceshifulwm2m/lwm2m/ciphersuite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package lwm2m

import (
"testing"

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

var cipherSuiteStrs = []v1alpha1.CipherSuite{
v1alpha1.CipherSuite_TLS_ECDHE_ECDSA_WITH_AES_128_CCM,
v1alpha1.CipherSuite_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8,
v1alpha1.CipherSuite_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
v1alpha1.CipherSuite_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
v1alpha1.CipherSuite_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
v1alpha1.CipherSuite_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
v1alpha1.CipherSuite_TLS_PSK_WITH_AES_128_CCM,
v1alpha1.CipherSuite_TLS_PSK_WITH_AES_128_CCM_8,
v1alpha1.CipherSuite_TLS_PSK_WITH_AES_256_CCM_8,
v1alpha1.CipherSuite_TLS_PSK_WITH_AES_128_GCM_SHA256,
v1alpha1.CipherSuite_TLS_PSK_WITH_AES_128_CBC_SHA256,
v1alpha1.CipherSuite_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
v1alpha1.CipherSuite_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
v1alpha1.CipherSuite_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256,
}

func TestCipherSuiteStringToCode(t *testing.T) {
var cipherSuiteMap = map[v1alpha1.CipherSuite]dtls.CipherSuiteID{
v1alpha1.CipherSuite_TLS_ECDHE_ECDSA_WITH_AES_128_CCM: TLS_ECDHE_ECDSA_WITH_AES_128_CCM,
v1alpha1.CipherSuite_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8: TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8,
v1alpha1.CipherSuite_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256: TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
v1alpha1.CipherSuite_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256: TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
v1alpha1.CipherSuite_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA: TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
v1alpha1.CipherSuite_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA: TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
v1alpha1.CipherSuite_TLS_PSK_WITH_AES_128_CCM: TLS_PSK_WITH_AES_128_CCM,
v1alpha1.CipherSuite_TLS_PSK_WITH_AES_128_CCM_8: TLS_PSK_WITH_AES_128_CCM_8,
v1alpha1.CipherSuite_TLS_PSK_WITH_AES_256_CCM_8: TLS_PSK_WITH_AES_256_CCM_8,
v1alpha1.CipherSuite_TLS_PSK_WITH_AES_128_GCM_SHA256: TLS_PSK_WITH_AES_128_GCM_SHA256,
v1alpha1.CipherSuite_TLS_PSK_WITH_AES_128_CBC_SHA256: TLS_PSK_WITH_AES_128_CBC_SHA256,
v1alpha1.CipherSuite_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384: TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
v1alpha1.CipherSuite_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384: TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
v1alpha1.CipherSuite_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256: TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256,
}

for _, cipherSuite := range cipherSuiteStrs {
res, err := CipherSuiteStringToCode(cipherSuite)
if err != nil {
t.Errorf("unknown cipher suite: %v", err)
}
if res != cipherSuiteMap[cipherSuite] {
t.Errorf("Error in mapping cipher suite: %v", cipherSuite)
}
}
}
beingStrongeryqqq marked this conversation as resolved.
Show resolved Hide resolved

var want = map[dtls.CipherSuiteID]bool{
TLS_ECDHE_ECDSA_WITH_AES_128_CCM: true,
TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8: true,
TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256: true,
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256: true,
TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384: true,
TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384: true,
TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA: true,
TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA: true,
TLS_PSK_WITH_AES_128_CCM: true,
TLS_PSK_WITH_AES_128_CCM_8: true,
TLS_PSK_WITH_AES_256_CCM_8: true,
TLS_PSK_WITH_AES_128_GCM_SHA256: true,
TLS_PSK_WITH_AES_128_CBC_SHA256: true,
TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256: true,
}

func TestCipherSuiteStringsToCodes(t *testing.T) {
beingStrongeryqqq marked this conversation as resolved.
Show resolved Hide resolved
res, err := CipherSuiteStringsToCodes(cipherSuiteStrs)
if err != nil {
t.Errorf("unknown cipher suite: %v", err)
}

if len(res) != len(want) {
t.Errorf("Error in mapping cipher suite: %v", res)
}

for _, v := range res {
t.Run("compare", func(t *testing.T) {
if !compareCipherSuites(v) {
t.Errorf("Error in mapping cipher suite")
}
})
}
}

func compareCipherSuites(a dtls.CipherSuiteID) bool {
return want[a]
}
Loading