supplement of digest refactoring

This commit is contained in:
xiekeyang 2015-03-11 11:02:30 +08:00
parent fb81ecb0fa
commit 04f8bfbede
2 changed files with 9 additions and 1 deletions

View File

@ -51,6 +51,9 @@ func NewDigestFromHex(alg, hex string) Digest {
// DigestRegexp matches valid digest types. // DigestRegexp matches valid digest types.
var DigestRegexp = regexp.MustCompile(`[a-zA-Z0-9-_+.]+:[a-fA-F0-9]+`) var DigestRegexp = regexp.MustCompile(`[a-zA-Z0-9-_+.]+:[a-fA-F0-9]+`)
// DigestRegexpAnchored matches valid digest types, anchored to the start and end of the match.
var DigestRegexpAnchored = regexp.MustCompile(`^` + DigestRegexp.String() + `$`)
var ( var (
// ErrDigestInvalidFormat returned when digest format invalid. // ErrDigestInvalidFormat returned when digest format invalid.
ErrDigestInvalidFormat = fmt.Errorf("invalid checksum digest format") ErrDigestInvalidFormat = fmt.Errorf("invalid checksum digest format")
@ -114,7 +117,7 @@ func (d Digest) Validate() error {
// Continue on for general parser // Continue on for general parser
if !DigestRegexp.MatchString(s) { if !DigestRegexpAnchored.MatchString(s) {
return ErrDigestInvalidFormat return ErrDigestInvalidFormat
} }

View File

@ -48,6 +48,11 @@ func TestParseDigest(t *testing.T) {
input: "d41d8cd98f00b204e9800998ecf8427e", input: "d41d8cd98f00b204e9800998ecf8427e",
err: ErrDigestInvalidFormat, err: ErrDigestInvalidFormat,
}, },
{
// not hex
input: "sha256:d41d8cd98f00b204e9800m98ecf8427e",
err: ErrDigestInvalidFormat,
},
{ {
input: "foo:d41d8cd98f00b204e9800998ecf8427e", input: "foo:d41d8cd98f00b204e9800998ecf8427e",
err: ErrDigestUnsupported, err: ErrDigestUnsupported,