Merge pull request #255 from xiekeyang/digest-refactoring

supplement of digest refactoring
This commit is contained in:
Stephen Day 2015-03-10 23:56:00 -07:00
commit 0c130dff5b
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,