You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

github.com/tunabay/go-randdata

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/tunabay/go-randdata

v0.1.0
Source
Go
Version published
Created
Source

go-randdata

GitHub GoDoc Go Report Card codecov

go-randdata is a Go package providing a mechanism for unit testing to generate and verify reproducible pseudo-random byte sequences.

Reader is the pseudo-random byte sequence generator. It implements the io.Reader interface and can be Read the generated byte sequence. Verifier is the Reader companion object that implements the io.Writer interface. It verifies that the data written is exactly the same as the byte sequence generated by the Reader.

import (
	"fmt"
	"github.com/tunabay/go-randdata"
)

func main() {
        // 5 MB pseudo-random byte sequence, using random seed 123
        r := randdata.New(randdata.Binary, 123, 5000000)

        // paired verifier
        v := r.NewVerifier()

        // read and veriry data
        buf := make([]byte, 256)
        for {
                n, err := r.Read(buf)
                if 0 < n {
                        if _, err := v.Write(buf[:n]); err != nil {
                                fmt.Println(err)
                                break
                        }
                }
                if err != nil {
                        if err != io.EOF {
                                fmt.Println(err)
                        }
                        break
                }
        }

        // verify that written data is enough
        if err := v.Close(); err != nil {
                fmt.Println(err)
        }

        fmt.Println("Read:", r.TotalRead())
}

Run in Go Playground

The Reader also generates "jitter" to reading operation. In the above example, calling Read method with the 256 bytes buffer returns randomly shorter written length. While the Read method of the io.Reader interface can return shorter length than passed buffer, program should be able to handle that.

Documentation

See also

License

go-randdata is available under the MIT license. See the LICENSE file for more information.

FAQs

Package last updated on 29 Apr 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.