WIP: adding randommessage package for generating test data
This commit is contained in:
parent
169e0539f6
commit
06d39158a5
5
go.mod
5
go.mod
@ -6,7 +6,7 @@ require (
|
||||
github.com/rs/zerolog v1.34.0
|
||||
github.com/spf13/pflag v1.0.6
|
||||
github.com/spf13/viper v1.20.1
|
||||
golang.org/x/sys v0.32.0
|
||||
golang.org/x/sys v0.33.0
|
||||
)
|
||||
|
||||
require (
|
||||
@ -22,6 +22,7 @@ require (
|
||||
github.com/subosito/gotenv v1.6.0 // indirect
|
||||
go.uber.org/atomic v1.9.0 // indirect
|
||||
go.uber.org/multierr v1.9.0 // indirect
|
||||
golang.org/x/text v0.21.0 // indirect
|
||||
golang.org/x/crypto v0.38.0 // indirect
|
||||
golang.org/x/text v0.25.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
6
go.sum
6
go.sum
@ -54,13 +54,19 @@ go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
|
||||
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
||||
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
|
||||
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
|
||||
golang.org/x/crypto v0.38.0 h1:jt+WWG8IZlBnVbomuhg2Mdq0+BBQaHbtqHEFEigjUV8=
|
||||
golang.org/x/crypto v0.38.0/go.mod h1:MvrbAqul58NNYPKnOra203SB9vpuZW0e+RRZV+Ggqjw=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20=
|
||||
golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
|
||||
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
|
||||
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
|
||||
golang.org/x/text v0.25.0 h1:qVyWApTSYLk/drJRO5mDlNYskwQznZmkpV2c8q9zls4=
|
||||
golang.org/x/text v0.25.0/go.mod h1:WEdwpYrmk1qmdHvhkSTNPm3app7v4rsT8F2UD6+VHIA=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
|
||||
100
testlib/randommessage/randommessage.go
Normal file
100
testlib/randommessage/randommessage.go
Normal file
@ -0,0 +1,100 @@
|
||||
package randommessage
|
||||
|
||||
/*
|
||||
Copyright 2025 Suyono <suyono3484@gmail.com>
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"hash"
|
||||
"io"
|
||||
|
||||
"golang.org/x/crypto/blake2s"
|
||||
)
|
||||
|
||||
type StreamReader struct {
|
||||
pos int64
|
||||
size int64
|
||||
hashEngine hash.Hash
|
||||
}
|
||||
|
||||
var (
|
||||
ErrInvalidBufSize = errors.New("invalid buffer size")
|
||||
)
|
||||
|
||||
func NewStreamReader(size int64) (*StreamReader, error) {
|
||||
var (
|
||||
h hash.Hash
|
||||
err error
|
||||
)
|
||||
|
||||
if h, err = blake2s.New256(nil); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &StreamReader{
|
||||
pos: 0,
|
||||
size: size,
|
||||
hashEngine: h,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *StreamReader) Read(buf []byte) (int, error) {
|
||||
var (
|
||||
n int
|
||||
err error
|
||||
)
|
||||
|
||||
if s.pos == 0 {
|
||||
if len(buf) < 4 {
|
||||
return 0, ErrInvalidBufSize
|
||||
}
|
||||
binary.BigEndian.PutUint32(buf, uint32(s.size))
|
||||
|
||||
n, err = s.read(buf[4:])
|
||||
n += 4
|
||||
} else {
|
||||
n, err = s.read(buf)
|
||||
}
|
||||
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (s *StreamReader) read(buf []byte) (int, error) {
|
||||
var (
|
||||
n int
|
||||
limit int
|
||||
err error
|
||||
)
|
||||
|
||||
limit = len(buf)
|
||||
err = nil
|
||||
if s.size-s.pos < int64(len(buf)) {
|
||||
err = io.EOF
|
||||
limit = int(s.size - s.pos)
|
||||
if limit < 0 {
|
||||
panic("unexpected limit")
|
||||
}
|
||||
}
|
||||
|
||||
if limit > 0 {
|
||||
n, _ = rand.Read(buf[:limit])
|
||||
s.pos += int64(limit)
|
||||
s.hashEngine.Write(buf[:limit])
|
||||
}
|
||||
|
||||
return n, err
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user