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
This commit is contained in:
mpl 2017-03-29 20:46:37 +02:00
parent 1486c78dec
commit 353e1e8873
2 changed files with 44 additions and 1 deletions

View File

@ -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)
}

View File

@ -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)
}
}