github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/pkg/rand/random_test.go (about) 1 // Copyright 2019 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package rand 6 7 import ( 8 "context" 9 "testing" 10 "time" 11 ) 12 13 func TestRandomRead(t *testing.T) { 14 b := make([]byte, 5) 15 n, err := Read(b) 16 if err != nil { 17 t.Fatalf("got %v, expected nil err", err) 18 } 19 if n != 5 { 20 t.Fatalf("got %d bytes, expected 5 bytes", n) 21 } 22 } 23 24 func TestRandomReadContext(t *testing.T) { 25 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) 26 defer cancel() 27 28 b := make([]byte, 5) 29 n, err := ReadContext(ctx, b) 30 if err != nil { 31 t.Fatalf("got %v, expected nil err", err) 32 } 33 if n != 5 { 34 t.Fatalf("got %d bytes, expected 5 bytes", n) 35 } 36 }