-
Notifications
You must be signed in to change notification settings - Fork 669
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal/sdk: Fix SDK's UUID utility to handle partial read (#536)
Fixes the SDK's UUID utility to correctly handle partial reads from its crypto rand source. This error was sometimes causing the SDK's InvocationID value to fail to be obtained, due to a partial read from crypto.Rand. Fix #534
- Loading branch information
Showing
3 changed files
with
78 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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 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 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,19 +1,75 @@ | ||
package sdk | ||
|
||
import "testing" | ||
import ( | ||
"io" | ||
"strings" | ||
"testing" | ||
"testing/iotest" | ||
|
||
func TestUUIDVersion4(t *testing.T) { | ||
uuid := uuidVersion4(make([]byte, 16)) | ||
if e, a := `00000000-0000-4000-8000-000000000000`, uuid; e != a { | ||
t.Errorf("expect %v uuid, got %v", e, a) | ||
"github.com/aws/aws-sdk-go-v2/internal/rand" | ||
) | ||
|
||
type byteReader byte | ||
|
||
func (b byteReader) Read(p []byte) (n int, err error) { | ||
for i := 0; i < len(p); i++ { | ||
p[i] = byte(b) | ||
} | ||
return len(p), nil | ||
} | ||
|
||
type errorReader struct{ err error } | ||
|
||
func (e errorReader) Read(p []byte) (n int, err error) { | ||
return 0, e.err | ||
} | ||
|
||
func TestUUIDVersion4(t *testing.T) { | ||
origReader := rand.Reader | ||
defer func() { rand.Reader = origReader }() | ||
|
||
b := make([]byte, 16) | ||
for i := 0; i < len(b); i++ { | ||
b[i] = 1 | ||
cases := map[string]struct { | ||
Expect string | ||
Reader io.Reader | ||
Err string | ||
}{ | ||
"0x00": { | ||
Expect: `00000000-0000-4000-8000-000000000000`, | ||
Reader: byteReader(0), | ||
}, | ||
"0x01": { | ||
Expect: `01010101-0101-4101-8101-010101010101`, | ||
Reader: byteReader(1), | ||
}, | ||
"partial": { | ||
Expect: `01010101-0101-4101-8101-010101010101`, | ||
Reader: iotest.HalfReader(byteReader(1)), | ||
}, | ||
"error": { | ||
Reader: errorReader{err: io.ErrUnexpectedEOF}, | ||
Err: io.ErrUnexpectedEOF.Error(), | ||
}, | ||
} | ||
uuid = uuidVersion4(b) | ||
if e, a := `01010101-0101-4101-8101-010101010101`, uuid; e != a { | ||
t.Errorf("expect %v uuid, got %v", e, a) | ||
|
||
for name, c := range cases { | ||
t.Run(name, func(t *testing.T) { | ||
rand.Reader = c.Reader | ||
|
||
uuid, err := UUIDVersion4() | ||
if len(c.Err) != 0 { | ||
if err == nil { | ||
t.Fatalf("expect error, got none") | ||
} | ||
if e, a := c.Err, err.Error(); !strings.Contains(a, e) { | ||
t.Fatalf("expect %q in error, %q", e, a) | ||
} | ||
} else if err != nil { | ||
t.Fatalf("expect no error, got %v", err) | ||
} | ||
|
||
if e, a := c.Expect, uuid; e != a { | ||
t.Errorf("expect %v uuid, got %v", e, a) | ||
} | ||
}) | ||
} | ||
} |