From de99d3768a721943efe0111af7fddb7c984330d1 Mon Sep 17 00:00:00 2001 From: number571 Date: Fri, 22 May 2026 12:13:06 +0700 Subject: [PATCH] update --- pkg/crypto/symmetric/list_ciphers.go | 4 +- pkg/crypto/symmetric/list_ciphers_test.go | 53 +++++++++++++++++++++++ test/result/badge_coverage.svg | 2 +- test/result/coverage.svg | 22 +++++----- 4 files changed, 67 insertions(+), 14 deletions(-) create mode 100644 pkg/crypto/symmetric/list_ciphers_test.go diff --git a/pkg/crypto/symmetric/list_ciphers.go b/pkg/crypto/symmetric/list_ciphers.go index 4cce4361..cc6dabfc 100644 --- a/pkg/crypto/symmetric/list_ciphers.go +++ b/pkg/crypto/symmetric/list_ciphers.go @@ -10,14 +10,14 @@ var ( type sListCiphers struct { fMtx *sync.RWMutex - fMap map[string]interface{} + fMap map[string]struct{} fList []ICipher } func NewListCiphers(pCiphers ...ICipher) IListCiphers { listCiphers := &sListCiphers{ fMtx: &sync.RWMutex{}, - fMap: make(map[string]interface{}, 128), + fMap: make(map[string]struct{}, 128), fList: make([]ICipher, 0, 128), } for _, v := range pCiphers { diff --git a/pkg/crypto/symmetric/list_ciphers_test.go b/pkg/crypto/symmetric/list_ciphers_test.go new file mode 100644 index 00000000..66e26bbb --- /dev/null +++ b/pkg/crypto/symmetric/list_ciphers_test.go @@ -0,0 +1,53 @@ +package symmetric + +import ( + "testing" + + "github.com/number571/go-peer/pkg/crypto/random" +) + +func TestListCiphersPanic(t *testing.T) { + t.Parallel() + + defer func() { + if r := recover(); r == nil { + t.Error("nothing panics") + return + } + }() + + cipher1 := NewCipherGCM(random.NewRandom().GetBytes(CCipherKeySize)) + list := NewListCiphers( + NewCipherGCM(random.NewRandom().GetBytes(CCipherKeySize)), + ) + + _list := list.(*sListCiphers) + _list.fMap[cipher1.ToString()] = struct{}{} + + _ = list.Del(cipher1) +} + +func TestListCiphers(t *testing.T) { + t.Parallel() + + cipher1 := NewCipherGCM(random.NewRandom().GetBytes(CCipherKeySize)) + list := NewListCiphers( + cipher1, + NewCipherGCM(random.NewRandom().GetBytes(CCipherKeySize)), + NewCipherGCM(random.NewRandom().GetBytes(CCipherKeySize)), + ) + + if ok := list.Add(cipher1); ok { + t.Fatal("success add double cipher") + } + if ok := list.Del(cipher1); !ok { + t.Fatal("failed delete exist cipher") + } + if ok := list.Del(cipher1); ok { + t.Fatal("success delete unknown cipher") + } + + if len(list.Get()) != 2 { + t.Fatal("invalid length of list") + } +} diff --git a/test/result/badge_coverage.svg b/test/result/badge_coverage.svg index 65824887..9fd7bd14 100644 --- a/test/result/badge_coverage.svg +++ b/test/result/badge_coverage.svg @@ -1 +1 @@ -coverage: 96%coverage96% \ No newline at end of file +coverage: 97%coverage97% \ No newline at end of file diff --git a/test/result/coverage.svg b/test/result/coverage.svg index bddf977b..e5914e4d 100644 --- a/test/result/coverage.svg +++ b/test/result/coverage.svg @@ -7,7 +7,7 @@ > - + - + - + - + - + - + - + symmetric @@ -419,7 +419,7 @@ - + - + list_ciphers.go