diff --git a/pkg/schema/blob.go b/pkg/schema/blob.go index 4174d0f44..2b93d5b1d 100644 --- a/pkg/schema/blob.go +++ b/pkg/schema/blob.go @@ -429,7 +429,7 @@ func (bb *Builder) SetSigner(signer blob.Ref) *Builder { return bb } -// SignAt sets the blob builder's camliSigner field with SetSigner +// Sign sets the blob builder's camliSigner field with SetSigner // and returns the signed JSON using the provided signer. func (bb *Builder) Sign(signer *Signer) (string, error) { return bb.SignAt(signer, time.Time{}) @@ -445,6 +445,10 @@ func (bb *Builder) SignAt(signer *Signer, sigTime time.Time) (string, error) { default: return "", fmt.Errorf("can't sign camliType %q", bb.Type()) } + if sigTime.IsZero() { + sigTime = time.Now() + } + bb.SetClaimDate(sigTime) return signer.SignJSON(bb.SetSigner(signer.pubref).Blob().JSON(), sigTime) } diff --git a/pkg/schema/sign_test.go b/pkg/schema/sign_test.go index 92de1f497..2f343e69f 100644 --- a/pkg/schema/sign_test.go +++ b/pkg/schema/sign_test.go @@ -17,8 +17,10 @@ limitations under the License. package schema import ( + "encoding/json" "strings" "testing" + "time" "camlistore.org/pkg/blob" "camlistore.org/pkg/jsonsign" @@ -49,3 +51,40 @@ func TestSigner(t *testing.T) { t.Errorf("Permanode doesn't look signed: %v", pn) } } + +// TestClaimDate makes sure that when we sign a schema, we set the claimDate to +// the time of the signature. +// It demonstrates that issue #917 is fixed. +func TestClaimDate(t *testing.T) { + ent, err := jsonsign.NewEntity() + if err != nil { + t.Fatal(err) + } + armorPub, err := jsonsign.ArmoredPublicKey(ent) + if err != nil { + t.Fatal(err) + } + pubRef := blob.SHA1FromString(armorPub) + sig, err := NewSigner(pubRef, strings.NewReader(armorPub), ent) + if err != nil { + t.Fatalf("NewSigner: %v", err) + } + + sigTime, err := time.Parse(time.RFC3339, "2006-01-02T15:04:05Z") + if err != nil { + t.Fatal(err) + } + share := NewShareRef(ShareHaveRef, true).SetShareTarget(pubRef) + signed, err := share.SignAt(sig, sigTime) + if err != nil { + t.Fatal(err) + } + + ss := &superset{} + if err := json.NewDecoder(strings.NewReader(signed)).Decode(ss); err != nil { + t.Fatal(err) + } + if !strings.HasPrefix(ss.ClaimDate.String(), "2006-01-02") { + t.Fatalf("wrong claimDate in superset: got %q, wanted %q", ss.ClaimDate, sigTime) + } +}