-
Notifications
You must be signed in to change notification settings - Fork 28
/
util.go
44 lines (38 loc) · 1.23 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package internal
import (
"crypto/md5" //nolint:gosec
crand "crypto/rand"
"encoding/binary"
"math/rand"
"v.io/x/ref/runtime/internal/rpc/stress"
)
// newSumArg returns a randomly generated SumArg.
func newSumArg(maxPayloadSize int) (stress.SumArg, error) {
var arg stress.SumArg
arg.ABool = rand.Intn(2) == 0 //nolint:gosec
arg.AInt64 = rand.Int63() //nolint:gosec
arg.AListOfBytes = make([]byte, rand.Intn(maxPayloadSize)+1) //nolint:gosec
_, err := crand.Read(arg.AListOfBytes)
return arg, err
}
// lenSumArg returns the length of the SumArg in bytes.
func lenSumArg(arg *stress.SumArg) int {
// bool + uint64 + []byte
return 1 + 4 + len(arg.AListOfBytes)
}
// doSum returns the MD5 checksum of the SumArg.
func doSum(arg *stress.SumArg) ([]byte, error) {
h := md5.New() //nolint:gosec
if arg.ABool {
if err := binary.Write(h, binary.LittleEndian, arg.AInt64); err != nil {
return nil, err
}
}
if _, err := h.Write(arg.AListOfBytes); err != nil {
return nil, err
}
return h.Sum(nil), nil
}