-
Notifications
You must be signed in to change notification settings - Fork 910
GODRIVER-2349 Seed all pseudorandom number generators with a crypto-secure random number. #889
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
matthewdale
merged 4 commits into
mongodb:master
from
matthewdale:godriver2349-use-crypto-seed
Mar 30, 2022
+118
−15
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
32495c0
GODRIVER-2349 Seed all pseudorandom number generators with a crypto-s…
matthewdale 8e36c85
Add a unit test for the package-global UUID source.
matthewdale 7617e94
Add a unit test for CryptoSeed.
matthewdale a89b49a
Add license notices to new and modified files.
matthewdale File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright (C) MongoDB, Inc. 2022-present. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. You may obtain | ||
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
package randutil | ||
benjirewis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCryptoSeed(t *testing.T) { | ||
seeds := make(map[int64]bool) | ||
for i := 1; i < 1000000; i++ { | ||
s := CryptoSeed() | ||
require.False(t, seeds[s], "CryptoSeed returned a duplicate value %d", s) | ||
seeds[s] = true | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,56 @@ | ||
// Copyright (C) MongoDB, Inc. 2022-present. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. You may obtain | ||
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
package uuid | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNew(t *testing.T) { | ||
m := make(map[UUID]bool) | ||
for i := 1; i < 100; i++ { | ||
for i := 1; i < 1000000; i++ { | ||
uuid, err := New() | ||
assert.NoError(t, err, "New error") | ||
assert.False(t, m[uuid], "New returned a duplicate UUID %v", uuid) | ||
require.NoError(t, err, "New error") | ||
require.False(t, m[uuid], "New returned a duplicate UUID %v", uuid) | ||
m[uuid] = true | ||
} | ||
} | ||
|
||
// GODRIVER-2349 | ||
// Test that initializing many package-global UUID sources concurrently never leads to any duplicate | ||
// UUIDs being generated. | ||
func TestGlobalSource(t *testing.T) { | ||
// Create a slice of 1,000 sources and initialize them in 1,000 separate goroutines. The goal is | ||
// to emulate many separate Go driver processes starting at the same time and initializing the | ||
// uuid package at the same time. | ||
sources := make([]*source, 1000) | ||
var wg sync.WaitGroup | ||
for i := range sources { | ||
wg.Add(1) | ||
go func(i int) { | ||
defer wg.Done() | ||
sources[i] = newGlobalSource() | ||
}(i) | ||
} | ||
wg.Wait() | ||
|
||
// Read 1,000 UUIDs from each source and assert that there is never a duplicate value, either | ||
// from the same source or from separate sources. | ||
const iterations = 1000 | ||
uuids := make(map[UUID]bool, len(sources)*iterations) | ||
for i := 0; i < iterations; i++ { | ||
for j, s := range sources { | ||
uuid, err := s.new() | ||
require.NoError(t, err, "new() error") | ||
require.Falsef(t, uuids[uuid], "source %d returned a duplicate UUID on iteration %d: %v", j, i, uuid) | ||
uuids[uuid] = true | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.