This commit is contained in:
number571 2023-12-09 00:41:02 +07:00
parent fdf9a47a2c
commit dfffe377be
5 changed files with 775 additions and 703 deletions

View File

@ -7,6 +7,8 @@ import (
"github.com/number571/go-peer/pkg/client/message"
"github.com/number571/go-peer/pkg/crypto/asymmetric"
"github.com/number571/go-peer/pkg/crypto/random"
"github.com/number571/go-peer/pkg/crypto/symmetric"
"github.com/number571/go-peer/pkg/encoding"
"github.com/number571/go-peer/pkg/payload"
testutils "github.com/number571/go-peer/test/_data"
)
@ -51,7 +53,7 @@ var (
}
)
func TestClientWithMessageSize(t *testing.T) {
func TestClientPanicWithMessageSize(t *testing.T) {
t.Parallel()
defer func() {
@ -69,6 +71,24 @@ func TestClientWithMessageSize(t *testing.T) {
)
}
func TestClientPanicWithKeySize(t *testing.T) {
t.Parallel()
defer func() {
if r := recover(); r == nil {
t.Error("nothing panics")
return
}
}()
_ = NewClient(
message.NewSettings(&message.SSettings{
FMessageSizeBytes: tcMessageSize,
FKeySizeBits: 4096,
}),
tgPrivKey,
)
}
func TestEncrypt(t *testing.T) {
t.Parallel()
@ -141,7 +161,8 @@ func TestDecrypt(t *testing.T) {
}
sMsg5 := *sMsg
sMsg5.FPubKey = "11111111111111111111111111111111"
randomBytes := random.NewStdPRNG().GetBytes(symmetric.CAESBlockSize + tcKeySizeBits + 10)
sMsg5.FPubKey = encoding.HexEncode(randomBytes)
if _, _, err := client1.DecryptMessage(&sMsg5); err == nil {
t.Error("success decrypt message with incorrect sender key (public key is nil)")
return

View File

@ -6,13 +6,16 @@ import (
)
type tsMessage struct {
FResult string `json:"result"`
FReturn int `json:"return"`
FResult string `yaml:"result" json:"result"`
FReturn int `yaml:"return" json:"return"`
}
const (
tgBytesInNum = uint64(0xABCDEF0123456789)
tcJSON = `{"result":"hello","return":5}`
tcYaml = `result: hello
return: 5
`
)
var (
@ -44,7 +47,7 @@ func TestBytes(t *testing.T) {
}
}
func TestSerialize(t *testing.T) {
func TestSerializeJSON(t *testing.T) {
t.Parallel()
if string(SerializeJSON(tgMessage)) != tcJSON {
@ -69,3 +72,29 @@ func TestSerialize(t *testing.T) {
return
}
}
func TestSerializeYAML(t *testing.T) {
t.Parallel()
if string(SerializeYAML(tgMessage)) != tcYaml {
t.Error("serialize string is invalid (non indent)")
return
}
res := new(tsMessage)
if err := DeserializeYAML([]byte(tcYaml), res); err != nil {
t.Error(err)
return
}
if res.FResult != "hello" || res.FReturn != 5 {
t.Error("fields not equals")
return
}
if err := DeserializeYAML([]byte(`qwerty`), res); err == nil {
t.Error("success deserialize invalid data")
return
}
}

View File

@ -29,6 +29,28 @@ func testSettings(t *testing.T, n int) {
}
}
func TestQueuePanic(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("nothing panics")
return
}
}()
queueSet := NewQueueSet(
NewSettings(&SSettings{
FCapacity: 3,
}),
)
sQueueSet := queueSet.(*sQueueSet)
sQueueSet.fMap["abc"] = []byte{123}
sQueueSet.fQueue[sQueueSet.fIndex] = "abc"
sQueueSet.fIndex++
_ = queueSet.GetQueueKeys()
}
func TestQueueSet(t *testing.T) {
queueSet := NewQueueSet(
NewSettings(&SSettings{

View File

@ -1 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="96" height="20" role="img" aria-label="coverage: 46%"><title>coverage: 46%</title><linearGradient id="s" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="r"><rect width="96" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#r)"><rect width="61" height="20" fill="#555"/><rect x="61" width="35" height="20" fill="#e05d44"/><rect width="96" height="20" fill="url(#s)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="315" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="510">coverage</text><text x="315" y="140" transform="scale(.1)" fill="#fff" textLength="510">coverage</text><text aria-hidden="true" x="775" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="250">46%</text><text x="775" y="140" transform="scale(.1)" fill="#fff" textLength="250">46%</text></g></svg>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="96" height="20" role="img" aria-label="coverage: 47%"><title>coverage: 47%</title><linearGradient id="s" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="r"><rect width="96" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#r)"><rect width="61" height="20" fill="#555"/><rect x="61" width="35" height="20" fill="#e05d44"/><rect width="96" height="20" fill="url(#s)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="315" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="510">coverage</text><text x="315" y="140" transform="scale(.1)" fill="#fff" textLength="510">coverage</text><text aria-hidden="true" x="775" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="250">47%</text><text x="775" y="140" transform="scale(.1)" fill="#fff" textLength="250">47%</text></g></svg>

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

File diff suppressed because it is too large Load Diff