"time"
)
+import "crypto/internal/boring"
+
const urandomDevice = "/dev/urandom"
- // Easy implementation: read from /dev/urandom.
- // This is sufficient on Linux, OS X, and FreeBSD.
-
func init() {
- if runtime.GOOS == "plan9" {
- Reader = newReader(nil)
- } else {
- Reader = &devReader{name: urandomDevice}
- }
+ if boring.Enabled {
+ Reader = boring.RandReader
+ return
+ }
+ Reader = &reader{}
}
- // A devReader satisfies reads by reading the file named name.
- type devReader struct {
- name string
+ // A reader satisfies reads by reading from urandomDevice
+ type reader struct {
f io.Reader
mu sync.Mutex
- used int32 // atomic; whether this devReader has been used
+ used int32 // atomic; whether this reader has been used
}
// altGetRandom if non-nil specifies an OS-specific function to get
println("crypto/rand: blocked for 60 seconds waiting to read random data from the kernel")
}
- func (r *devReader) Read(b []byte) (n int, err error) {
+ func (r *reader) Read(b []byte) (n int, err error) {
+ boring.Unreachable()
if atomic.CompareAndSwapInt32(&r.used, 0, 1) {
// First use of randomness. Start timer to warn about
// being blocked on entropy not being available.
}
}
-
+ func TestIntJSONEncodingNil(t *testing.T) {
+ var x *Int
+ b, err := x.MarshalJSON()
+ if err != nil {
+ t.Fatalf("marshaling of nil failed: %s", err)
+ }
+ got := string(b)
+ want := "null"
+ if got != want {
+ t.Fatalf("marshaling of nil failed: got %s want %s", got, want)
+ }
+ }
+
func TestIntXMLEncoding(t *testing.T) {
for _, test := range encodingTests {
for _, sign := range []string{"", "+", "-"} {