From 353e1e8873585943c6323650b335463a314ae64a Mon Sep 17 00:00:00 2001 From: mpl Date: Wed, 29 Mar 2017 20:46:37 +0200 Subject: [PATCH] pkg/schema: make sure claim date becomes signature time It seems that signing a claim schema does not automatically set the claim date to the signature time, which I believe should be the case. This change adds a test to demonstrate the issue, and the fix that makes the test pass. Fixes #917 Change-Id: I55d6c75cbe3c3c4c1d6e5b69f6d26277d0d12728 --- pkg/schema/blob.go | 6 +++++- pkg/schema/sign_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) 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) + } +}