Skip to content

Commit

Permalink
Merge pull request git-lfs#5868 from chrisd8088/test-updates-genrandom
Browse files Browse the repository at this point in the history
Use custom random data generator for all test objects and filenames
  • Loading branch information
chrisd8088 authored Sep 27, 2024
2 parents 17aaf6f + 6e76463 commit 2c3dbea
Showing 13 changed files with 158 additions and 78 deletions.
1 change: 1 addition & 0 deletions t/Makefile
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@ TEST_CMDS += ../bin/lfs-ssh-proxy-test$X
TEST_CMDS += ../bin/lfstest-badpathcheck$X
TEST_CMDS += ../bin/lfstest-count-tests$X
TEST_CMDS += ../bin/lfstest-customadapter$X
TEST_CMDS += ../bin/lfstest-genrandom$X
TEST_CMDS += ../bin/lfstest-gitserver$X
TEST_CMDS += ../bin/lfstest-nanomtime$X
TEST_CMDS += ../bin/lfstest-realpath$X
72 changes: 72 additions & 0 deletions t/cmd/lfstest-genrandom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//go:build testtools
// +build testtools

package main

import (
"crypto/rand"
"encoding/base64"
"encoding/binary"
"fmt"
"os"
"strconv"
)

const usageFmt = "Usage: %s [--base64|--base64url] [<size>]\n"

func main() {
offset := 1
b64 := false
b64url := false
if len(os.Args) > offset && (os.Args[offset] == "--base64" || os.Args[offset] == "--base64url") {
b64 = true
b64url = os.Args[offset] == "--base64url"
offset += 1
}

if len(os.Args) > offset+1 {
fmt.Fprintf(os.Stderr, usageFmt, os.Args[0])
os.Exit(2)
}

var count uint64 = ^uint64(0)
if len(os.Args) == offset+1 {
var err error
if count, err = strconv.ParseUint(os.Args[offset], 10, 64); err != nil {
fmt.Fprintf(os.Stderr, "Error reading size: %s, %v\n", os.Args[offset], err)
os.Exit(3)
}
}

b := make([]byte, 32)
bb := make([]byte, max(base64.RawStdEncoding.EncodedLen(len(b)), base64.RawURLEncoding.EncodedLen(len(b))))
for count > 0 {
n, err := rand.Read(b)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading random bytes: %v\n", err)
os.Exit(4)
}
if b64 {
if b64url {
base64.RawURLEncoding.Encode(bb, b[:n])
n = base64.RawURLEncoding.EncodedLen(n)
} else {
base64.RawStdEncoding.Encode(bb, b[:n])
n = base64.RawStdEncoding.EncodedLen(n)
}
}

num := min(uint64(n), count)
if b64 {
err = binary.Write(os.Stdout, binary.LittleEndian, bb[:num])
} else {
err = binary.Write(os.Stdout, binary.LittleEndian, b[:num])
}

if err != nil {
fmt.Fprintf(os.Stderr, "Error writing random bytes: %v\n", err)
os.Exit(5)
}
count -= num
}
}
Loading

0 comments on commit 2c3dbea

Please sign in to comment.