Skip to content

Commit

Permalink
digest: Minor refactoring
Browse files Browse the repository at this point in the history
Docker-DCO-1.1-Signed-off-by: Josh Hawn <josh.hawn@docker.com> (github: jlhawn)
  • Loading branch information
Josh Hawn committed Mar 10, 2015
1 parent fe2fd6d commit 12db4ec
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 20 deletions.
4 changes: 2 additions & 2 deletions digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func NewDigestFromHex(alg, hex string) Digest {
}

// DigestRegexp matches valid digest types.
var DigestRegexp = regexp.MustCompile(`[a-zA-Z0-9-_+.]+:[a-zA-Z0-9-_+.=]+`)
var DigestRegexp = regexp.MustCompile(`[a-zA-Z0-9-_+.]+:[a-fA-F0-9]+`)

var (
// ErrDigestInvalidFormat returned when digest format invalid.
Expand Down Expand Up @@ -158,7 +158,7 @@ func (d Digest) sepIndex() int {
i := strings.Index(string(d), ":")

if i < 0 {
panic("invalid digest: " + d)
panic("could not find ':' in digest: " + d)
}

return i
Expand Down
8 changes: 3 additions & 5 deletions tarsum.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ type TarSumInfo struct {

// InvalidTarSumError provides informations about a TarSum that cannot be parsed
// by ParseTarSum.
type InvalidTarSumError struct {
TarSum string
}
type InvalidTarSumError string

func (e InvalidTarSumError) Error() string {
return fmt.Sprintf("invalid tarsum: %q", e.TarSum)
return fmt.Sprintf("invalid tarsum: %q", string(e))
}

// ParseTarSum parses a tarsum string into its components of interest. For
Expand All @@ -52,7 +50,7 @@ func ParseTarSum(tarSum string) (tsi TarSumInfo, err error) {
components := TarsumRegexpCapturing.FindStringSubmatch(tarSum)

if len(components) != 1+TarsumRegexpCapturing.NumSubexp() {
return TarSumInfo{}, InvalidTarSumError{TarSum: tarSum}
return TarSumInfo{}, InvalidTarSumError(tarSum)
}

return TarSumInfo{
Expand Down
4 changes: 2 additions & 2 deletions tarsum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ func TestParseTarSumComponents(t *testing.T) {
},
{
input: "",
err: InvalidTarSumError{},
err: InvalidTarSumError(""),
},
{
input: "purejunk",
err: InvalidTarSumError{TarSum: "purejunk"},
err: InvalidTarSumError("purejunk"),
},
{
input: "tarsum.v23+test:12341234123412341effefefe",
Expand Down
18 changes: 9 additions & 9 deletions verifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,27 @@ type Verifier interface {
// Verified will return true if the content written to Verifier matches
// the digest.
Verified() bool

// Planned methods:
// Err() error
// Reset()
}

// NewDigestVerifier returns a verifier that compares the written bytes
// against a passed in digest.
func NewDigestVerifier(d Digest) Verifier {
func NewDigestVerifier(d Digest) (Verifier, error) {
if err := d.Validate(); err != nil {
return nil, err
}

alg := d.Algorithm()
switch alg {
case "sha256", "sha384", "sha512":
return hashVerifier{
hash: newHash(alg),
digest: d,
}
}, nil
default:
// Assume we have a tarsum.
version, err := tarsum.GetVersionFromTarsum(string(d))
if err != nil {
panic(err) // Always assume valid tarsum at this point.
return nil, err
}

pr, pw := io.Pipe()
Expand All @@ -50,7 +50,7 @@ func NewDigestVerifier(d Digest) Verifier {

ts, err := tarsum.NewTarSum(pr, true, version)
if err != nil {
panic(err)
return nil, err
}

// TODO(sday): Ick! A goroutine per digest verification? We'll have to
Expand All @@ -65,7 +65,7 @@ func NewDigestVerifier(d Digest) Verifier {
ts: ts,
pr: pr,
pw: pw,
}
}, nil
}
}

Expand Down
12 changes: 10 additions & 2 deletions verifiers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ func TestDigestVerifier(t *testing.T) {
t.Fatalf("unexpected error digesting bytes: %#v", err)
}

verifier := NewDigestVerifier(digest)
verifier, err := NewDigestVerifier(digest)
if err != nil {
t.Fatalf("unexpected error getting digest verifier: %s", err)
}

io.Copy(verifier, bytes.NewReader(p))

if !verifier.Verified() {
Expand All @@ -45,7 +49,11 @@ func TestDigestVerifier(t *testing.T) {
// This is the most relevant example for the registry application. It's
// effectively a read through pipeline, where the final sink is the digest
// verifier.
verifier = NewDigestVerifier(digest)
verifier, err = NewDigestVerifier(digest)
if err != nil {
t.Fatalf("unexpected error getting digest verifier: %s", err)
}

lengthVerifier := NewLengthVerifier(expectedSize)
rd := io.TeeReader(tf, lengthVerifier)
io.Copy(verifier, rd)
Expand Down

0 comments on commit 12db4ec

Please sign in to comment.