index: more test work

Change-Id: I28a162647648253682470b10f4fb72e1c491c106
This commit is contained in:
Brad Fitzpatrick 2011-11-27 13:14:53 -05:00
parent 676e6cf100
commit 4c121d95b7
2 changed files with 88 additions and 4 deletions

View File

@ -17,9 +17,90 @@ limitations under the License.
package index
import (
"fmt"
"testing"
"camli/blobref"
"camli/jsonsign"
"camli/schema"
"camli/test"
)
type IndexDeps struct {
Index *Index
// Following three needed for signing:
PublicKeyFetcher *test.Fetcher
EntityFetcher jsonsign.EntityFetcher // fetching decrypted openpgp entities
SignerBlobRef *blobref.BlobRef
}
func (id *IndexDeps) uploadAndSignMap(m map[string]interface{}) *blobref.BlobRef {
m["camliSigner"] = id.SignerBlobRef
unsigned, err := schema.MapToCamliJson(m)
if err != nil {
panic("uploadAndSignMap: " + err.String())
}
sr := &jsonsign.SignRequest{
UnsignedJson: unsigned,
Fetcher: id.PublicKeyFetcher,
EntityFetcher: id.EntityFetcher,
}
signed, err := sr.Sign()
if err != nil {
panic("problem signing: " + err.String())
}
tb := &test.Blob{Contents: signed}
_, err = id.Index.ReceiveBlob(tb.BlobRef(), tb.Reader())
if err != nil {
panic(fmt.Sprintf("problem indexing blob: %v\nblob was:\n%s", err, signed))
}
return tb.BlobRef()
}
// NewPermanode creates (& signs) a new permanode and adds it
// to the index, returning its blobref.
func (id *IndexDeps) NewPermanode() *blobref.BlobRef {
unsigned := schema.NewUnsignedPermanode()
return id.uploadAndSignMap(unsigned)
}
func NewIndexDeps() *IndexDeps {
secretRingFile := "../../../../lib/go/camli/jsonsign/testdata/test-secring.gpg"
pubKey := &test.Blob{Contents: `-----BEGIN PGP PUBLIC KEY BLOCK-----
xsBNBEzgoVsBCAC/56aEJ9BNIGV9FVP+WzenTAkg12k86YqlwJVAB/VwdMlyXxvi
bCT1RVRfnYxscs14LLfcMWF3zMucw16mLlJCBSLvbZ0jn4h+/8vK5WuAdjw2YzLs
WtBcjWn3lV6tb4RJz5gtD/o1w8VWxwAnAVIWZntKAWmkcChCRgdUeWso76+plxE5
aRYBJqdT1mctGqNEISd/WYPMgwnWXQsVi3x4z1dYu2tD9uO1dkAff12z1kyZQIBQ
rexKYRRRh9IKAayD4kgS0wdlULjBU98aeEaMz1ckuB46DX3lAYqmmTEL/Rl9cOI0
Enpn/oOOfYFa5h0AFndZd1blMvruXfdAobjVABEBAAE=
=28/7
-----END PGP PUBLIC KEY BLOCK-----`}
id := &IndexDeps{
Index: newMemoryIndex(),
PublicKeyFetcher: new(test.Fetcher),
EntityFetcher: &jsonsign.CachingEntityFetcher{
Fetcher: &jsonsign.FileEntityFetcher{File: secretRingFile},
},
SignerBlobRef: pubKey.BlobRef(),
}
// Add dev-camput's test key public key, keyid 26F5ABDA,
// blobref sha1-ad87ca5c78bd0ce1195c46f7c98e6025abbaf007
if id.SignerBlobRef.String() != "sha1-ad87ca5c78bd0ce1195c46f7c98e6025abbaf007" {
panic("unexpected signer blobref")
}
id.PublicKeyFetcher.AddBlob(pubKey)
return id
}
func TestIndexPopulation(t *testing.T) {
id := NewIndexDeps()
pn := id.NewPermanode()
t.Logf("uploaded permanode %q", pn)
}
func TestReverseTimeString(t *testing.T) {
in := "2011-11-27T01:23:45Z"
got := reverseTimeString(in)

View File

@ -32,13 +32,14 @@ func init() {
blobserver.StorageConstructor(newMemoryIndexFromConfig))
}
func newMemoryIndexFromConfig(ld blobserver.Loader, config jsonconfig.Obj) (blobserver.Storage, os.Error) {
blobPrefix := config.RequiredString("blobSource")
func newMemoryIndex() *Index {
db := memdb.New(nil)
memStorage := &memKeys{db: db}
return New(memStorage)
}
ix := New(memStorage)
func newMemoryIndexFromConfig(ld blobserver.Loader, config jsonconfig.Obj) (blobserver.Storage, os.Error) {
blobPrefix := config.RequiredString("blobSource")
if err := config.Validate(); err != nil {
return nil, err
}
@ -46,6 +47,8 @@ func newMemoryIndexFromConfig(ld blobserver.Loader, config jsonconfig.Obj) (blob
if err != nil {
return nil, err
}
ix := newMemoryIndex()
ix.BlobSource = sto
// Good enough, for now: