mirror of
https://codeberg.org/cunicu/cunicu.git
synced 2025-09-27 05:06:02 +08:00
102 lines
1.8 KiB
Go
102 lines
1.8 KiB
Go
// SPDX-FileCopyrightText: 2023-2025 Steffen Vogel <post@steffenvogel.de>
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// Package test implements universal helpers for unit and integration tests
|
|
package test
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/rand"
|
|
"math"
|
|
"math/big"
|
|
"os"
|
|
"sync"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
"cunicu.li/cunicu/pkg/crypto"
|
|
epdiscproto "cunicu.li/cunicu/pkg/proto/feature/epdisc"
|
|
"cunicu.li/cunicu/pkg/signaling"
|
|
)
|
|
|
|
func GenerateKeyPairs() (*crypto.KeyPair, *crypto.KeyPair, error) {
|
|
ourKey, err := crypto.GeneratePrivateKey()
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
theirKey, err := crypto.GeneratePrivateKey()
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
return &crypto.KeyPair{
|
|
Ours: ourKey,
|
|
Theirs: theirKey.PublicKey(),
|
|
}, &crypto.KeyPair{
|
|
Ours: theirKey,
|
|
Theirs: ourKey.PublicKey(),
|
|
}, nil
|
|
}
|
|
|
|
func GenerateSignalingMessage() *signaling.Message {
|
|
r, err := rand.Int(rand.Reader, big.NewInt(math.MaxInt64))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return &signaling.Message{
|
|
Candidate: &epdiscproto.Candidate{
|
|
Port: int32(r.Int64()), //nolint:gosec
|
|
},
|
|
}
|
|
}
|
|
|
|
func Entropy(data []byte) float64 {
|
|
if len(data) == 0 {
|
|
return 0
|
|
}
|
|
|
|
length := float64(len(data))
|
|
entropy := 0.0
|
|
|
|
for i := range 256 {
|
|
if p := float64(bytes.Count(data, []byte{byte(i)})) / length; p > 0 {
|
|
entropy += -p * math.Log2(p)
|
|
}
|
|
}
|
|
|
|
return entropy
|
|
}
|
|
|
|
func IsCI() bool {
|
|
return os.Getenv("CI") == "true"
|
|
}
|
|
|
|
func ParallelNew[T any](cnt int, ctor func(i int) (T, error)) ([]T, error) {
|
|
ts := []T{}
|
|
mu := sync.Mutex{}
|
|
|
|
eg := errgroup.Group{}
|
|
for i := 1; i <= cnt; i++ {
|
|
eg.Go(func() error {
|
|
t, err := ctor(i)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
mu.Lock()
|
|
ts = append(ts, t)
|
|
mu.Unlock()
|
|
|
|
return nil
|
|
})
|
|
}
|
|
|
|
if err := eg.Wait(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return ts, nil
|
|
}
|