2011-11-27 16:36:30 +00:00
/ *
Copyright 2011 Google Inc .
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
package index
import (
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
"errors"
2011-11-27 18:14:53 +00:00
"fmt"
2011-12-03 19:26:42 +00:00
"log"
2011-11-29 19:40:15 +00:00
"os"
2011-11-29 19:19:32 +00:00
"reflect"
2011-12-25 20:30:56 +00:00
"sync"
2011-11-27 16:36:30 +00:00
"testing"
2011-11-29 20:40:33 +00:00
"time"
2011-11-27 18:14:53 +00:00
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
"camlistore.org/pkg/blobref"
"camlistore.org/pkg/jsonsign"
"camlistore.org/pkg/schema"
"camlistore.org/pkg/search"
"camlistore.org/pkg/test"
2011-11-27 16:36:30 +00:00
)
2011-12-25 20:30:56 +00:00
var (
once sync . Once
mongoNotAvailable bool
)
2011-12-03 19:26:42 +00:00
2011-11-27 18:14:53 +00:00
type IndexDeps struct {
Index * Index
2011-12-03 19:26:42 +00:00
BlobSource * test . Fetcher
2011-11-27 18:14:53 +00:00
// Following three needed for signing:
PublicKeyFetcher * test . Fetcher
EntityFetcher jsonsign . EntityFetcher // fetching decrypted openpgp entities
SignerBlobRef * blobref . BlobRef
2011-11-28 01:37:14 +00:00
2012-02-20 12:32:46 +00:00
now time . Time // fake clock, nanos since epoch
2011-11-27 18:14:53 +00:00
}
2011-11-27 23:21:26 +00:00
func ( id * IndexDeps ) Get ( key string ) string {
v , _ := id . Index . s . Get ( key )
return v
}
2011-11-27 18:37:59 +00:00
func ( id * IndexDeps ) dumpIndex ( t * testing . T ) {
t . Logf ( "Begin index dump:" )
it := id . Index . s . Find ( "" )
for it . Next ( ) {
t . Logf ( " %q = %q" , it . Key ( ) , it . Value ( ) )
}
if err := it . Close ( ) ; err != nil {
t . Fatalf ( "iterator close = %v" , err )
}
t . Logf ( "End index dump." )
}
2011-11-27 18:14:53 +00:00
func ( id * IndexDeps ) uploadAndSignMap ( m map [ string ] interface { } ) * blobref . BlobRef {
m [ "camliSigner" ] = id . SignerBlobRef
unsigned , err := schema . MapToCamliJson ( m )
if err != nil {
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
panic ( "uploadAndSignMap: " + err . Error ( ) )
2011-11-27 18:14:53 +00:00
}
sr := & jsonsign . SignRequest {
UnsignedJson : unsigned ,
Fetcher : id . PublicKeyFetcher ,
EntityFetcher : id . EntityFetcher ,
}
signed , err := sr . Sign ( )
if err != nil {
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
panic ( "problem signing: " + err . Error ( ) )
2011-11-27 18:14:53 +00:00
}
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 )
}
2011-11-29 20:40:33 +00:00
func ( id * IndexDeps ) advanceTime ( ) string {
2012-02-20 12:32:46 +00:00
id . now = id . now . Add ( 1 * time . Second )
return schema . RFC3339FromTime ( id . now )
2011-11-28 01:37:14 +00:00
}
2012-02-21 10:01:27 +00:00
func ( id * IndexDeps ) lastTime ( ) time . Time {
2011-11-29 20:40:33 +00:00
return id . now
}
2011-11-27 23:21:26 +00:00
func ( id * IndexDeps ) SetAttribute ( permaNode * blobref . BlobRef , attr , value string ) * blobref . BlobRef {
m := schema . NewSetAttributeClaim ( permaNode , attr , value )
2011-11-29 20:40:33 +00:00
m [ "claimDate" ] = id . advanceTime ( )
2011-11-27 23:21:26 +00:00
return id . uploadAndSignMap ( m )
}
2011-12-03 19:26:42 +00:00
func ( id * IndexDeps ) UploadFile ( fileName string , contents string ) ( fileRef , wholeRef * blobref . BlobRef ) {
cb := & test . Blob { Contents : contents }
id . BlobSource . AddBlob ( cb )
wholeRef = cb . BlobRef ( )
_ , err := id . Index . ReceiveBlob ( wholeRef , cb . Reader ( ) )
if err != nil {
panic ( err )
}
m := schema . NewFileMap ( fileName )
schema . PopulateParts ( m , int64 ( len ( contents ) ) , [ ] schema . BytesPart {
schema . BytesPart {
Size : uint64 ( len ( contents ) ) ,
BlobRef : wholeRef ,
} } )
fjson , err := schema . MapToCamliJson ( m )
if err != nil {
panic ( err )
}
fb := & test . Blob { Contents : fjson }
log . Printf ( "Blob is: %s" , fjson )
id . BlobSource . AddBlob ( fb )
fileRef = fb . BlobRef ( )
_ , err = id . Index . ReceiveBlob ( fileRef , fb . Reader ( ) )
if err != nil {
panic ( err )
}
return
}
2011-12-25 20:30:56 +00:00
func NewIndexDeps ( index * Index ) * IndexDeps {
2012-02-21 10:51:33 +00:00
secretRingFile := "../jsonsign/testdata/test-secring.gpg"
2011-11-27 18:14:53 +00:00
pubKey := & test . Blob { Contents : ` -- -- - BEGIN PGP PUBLIC KEY BLOCK -- -- -
xsBNBEzgoVsBCAC / 56 aEJ9BNIGV9FVP + WzenTAkg12k86YqlwJVAB / VwdMlyXxvi
bCT1RVRfnYxscs14LLfcMWF3zMucw16mLlJCBSLvbZ0jn4h + / 8 vK5WuAdjw2YzLs
WtBcjWn3lV6tb4RJz5gtD / o1w8VWxwAnAVIWZntKAWmkcChCRgdUeWso76 + plxE5
aRYBJqdT1mctGqNEISd / WYPMgwnWXQsVi3x4z1dYu2tD9uO1dkAff12z1kyZQIBQ
rexKYRRRh9IKAayD4kgS0wdlULjBU98aeEaMz1ckuB46DX3lAYqmmTEL / Rl9cOI0
Enpn / oOOfYFa5h0AFndZd1blMvruXfdAobjVABEBAAE =
= 28 / 7
-- -- - END PGP PUBLIC KEY BLOCK -- -- - ` }
id := & IndexDeps {
2011-12-25 20:30:56 +00:00
Index : index ,
2011-12-03 19:26:42 +00:00
BlobSource : new ( test . Fetcher ) ,
2011-11-27 18:14:53 +00:00
PublicKeyFetcher : new ( test . Fetcher ) ,
EntityFetcher : & jsonsign . CachingEntityFetcher {
Fetcher : & jsonsign . FileEntityFetcher { File : secretRingFile } ,
} ,
SignerBlobRef : pubKey . BlobRef ( ) ,
2012-02-21 10:01:27 +00:00
now : time . Unix ( 1322443956 , 123456 ) ,
2011-11-27 18:14:53 +00:00
}
// 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 )
2011-11-27 23:21:26 +00:00
id . Index . KeyFetcher = id . PublicKeyFetcher
2011-12-03 19:26:42 +00:00
id . Index . BlobSource = id . BlobSource
2011-11-27 18:14:53 +00:00
return id
}
2011-12-25 20:30:56 +00:00
func checkMongoUp ( ) {
mgw := & MongoWrapper {
Servers : "localhost" ,
}
2012-02-21 14:25:29 +00:00
mongoNotAvailable = ! mgw . TestConnection ( 500 * time . Millisecond )
2011-12-25 20:30:56 +00:00
}
func initMongoIndex ( ) * Index {
mgw := & MongoWrapper {
Servers : "localhost" ,
Database : "camlitest" ,
Collection : "keys" ,
}
idx , err := newMongoIndex ( mgw )
if err != nil {
panic ( err )
}
err = idx . s . Delete ( "" )
if err != nil {
panic ( err )
}
return idx
}
2011-12-31 23:00:34 +00:00
type mongoTester struct { }
2011-12-25 20:30:56 +00:00
2011-12-31 23:00:34 +00:00
func ( mongoTester ) test ( t * testing . T , tfn func ( * testing . T , func ( ) * Index ) ) {
2011-12-25 20:30:56 +00:00
once . Do ( checkMongoUp )
if mongoNotAvailable {
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
err := errors . New ( "Not running; start a mongoDB daemon on the standard port (27017). The \"keys\" collection in the \"camlitest\" database will be used." )
2012-02-22 09:23:23 +00:00
t . Logf ( "Mongo not available locally for testing: %v" , err )
return
2011-12-25 20:30:56 +00:00
}
tfn ( t , initMongoIndex )
}
func TestIndex_Memory ( t * testing . T ) {
testIndex ( t , newMemoryIndex )
}
func TestIndex_Mongo ( t * testing . T ) {
2011-12-31 23:00:34 +00:00
mongoTester { } . test ( t , testIndex )
2011-12-25 20:30:56 +00:00
}
func testIndex ( t * testing . T , initIdx func ( ) * Index ) {
id := NewIndexDeps ( initIdx ( ) )
2011-11-27 18:14:53 +00:00
pn := id . NewPermanode ( )
t . Logf ( "uploaded permanode %q" , pn )
2011-11-28 01:37:14 +00:00
br1 := id . SetAttribute ( pn , "foo" , "foo1" )
2012-02-21 10:01:27 +00:00
br1Time := id . lastTime ( )
2011-11-27 23:21:26 +00:00
t . Logf ( "set attribute %q" , br1 )
2011-11-28 01:37:14 +00:00
br2 := id . SetAttribute ( pn , "foo" , "foo2" )
2012-02-21 10:01:27 +00:00
br2Time := id . lastTime ( )
2011-11-28 01:37:14 +00:00
t . Logf ( "set attribute %q" , br2 )
2011-11-28 03:29:23 +00:00
rootClaim := id . SetAttribute ( pn , "camliRoot" , "rootval" )
2012-02-21 10:01:27 +00:00
rootClaimTime := id . lastTime ( )
2011-11-28 03:29:23 +00:00
t . Logf ( "set attribute %q" , rootClaim )
2011-12-01 18:43:57 +00:00
2011-11-27 18:37:59 +00:00
id . dumpIndex ( t )
2011-11-27 23:21:26 +00:00
key := "signerkeyid:sha1-ad87ca5c78bd0ce1195c46f7c98e6025abbaf007"
if g , e := id . Get ( key ) , "2931A67C26F5ABDA" ; g != e {
t . Fatalf ( "%q = %q, want %q" , key , g , e )
}
2011-11-28 01:37:14 +00:00
2011-11-29 19:40:15 +00:00
key = "have:" + pn . String ( )
pnSizeStr := id . Get ( key )
if pnSizeStr == "" {
t . Fatalf ( "missing key %q" , key )
}
key = "meta:" + pn . String ( )
if g , e := id . Get ( key ) , pnSizeStr + "|application/json; camliType=permanode" ; g != e {
t . Errorf ( "key %q = %q, want %q" , key , g , e )
}
2011-11-29 19:19:32 +00:00
key = "recpn|2931A67C26F5ABDA|rt7988-88-71T98:67:62.999876543Z|" + br1 . String ( )
2011-11-28 01:37:14 +00:00
if g , e := id . Get ( key ) , pn . String ( ) ; g != e {
t . Fatalf ( "%q = %q, want %q (permanode)" , key , g , e )
}
2011-11-29 19:19:32 +00:00
key = "recpn|2931A67C26F5ABDA|rt7988-88-71T98:67:61.999876543Z|" + br2 . String ( )
2011-11-28 01:37:14 +00:00
if g , e := id . Get ( key ) , pn . String ( ) ; g != e {
t . Fatalf ( "%q = %q, want %q (permanode)" , key , g , e )
}
2011-11-28 03:29:23 +00:00
2011-11-29 19:40:15 +00:00
// PermanodeOfSignerAttrValue
{
gotPN , err := id . Index . PermanodeOfSignerAttrValue ( id . SignerBlobRef , "camliRoot" , "rootval" )
if err != nil {
t . Fatalf ( "id.Index.PermanodeOfSignerAttrValue = %v" , err )
}
if gotPN . String ( ) != pn . String ( ) {
t . Errorf ( "id.Index.PermanodeOfSignerAttrValue = %q, want %q" , gotPN , pn )
}
_ , err = id . Index . PermanodeOfSignerAttrValue ( id . SignerBlobRef , "camliRoot" , "MISSING" )
if err == nil {
t . Errorf ( "expected an error from PermanodeOfSignerAttrValue on missing value" )
}
2011-11-28 03:29:23 +00:00
}
2011-11-29 19:19:32 +00:00
// GetRecentPermanodes
{
ch := make ( chan * search . Result , 10 ) // only expect 1 result, but 3 if buggy.
2011-11-29 19:40:15 +00:00
err := id . Index . GetRecentPermanodes ( ch , id . SignerBlobRef , 50 )
2011-11-29 19:19:32 +00:00
if err != nil {
t . Fatalf ( "GetRecentPermanodes = %v" , err )
}
got := [ ] * search . Result { }
for r := range ch {
got = append ( got , r )
}
want := [ ] * search . Result {
& search . Result {
BlobRef : pn ,
Signer : id . SignerBlobRef ,
LastModTime : 1322443959 ,
} ,
}
if ! reflect . DeepEqual ( got , want ) {
t . Errorf ( "GetRecentPermanode results differ.\n got: %v\nwant: %v" ,
search . Results ( got ) , search . Results ( want ) )
}
}
2011-11-29 19:40:15 +00:00
// GetBlobMimeType
{
mime , size , err := id . Index . GetBlobMimeType ( pn )
if err != nil {
t . Errorf ( "GetBlobMimeType(%q) = %v" , pn , err )
} else {
if e := "application/json; camliType=permanode" ; mime != e {
t . Errorf ( "GetBlobMimeType(%q) mime = %q, want %q" , pn , mime , e )
}
if size == 0 {
t . Errorf ( "GetBlobMimeType(%q) size is zero" , pn )
}
}
_ , _ , err = id . Index . GetBlobMimeType ( blobref . Parse ( "abc-123" ) )
2012-02-20 12:32:46 +00:00
if err != os . ErrNotExist {
2011-11-29 19:40:15 +00:00
t . Errorf ( "GetBlobMimeType(dummy blobref) = %v; want os.ENOENT" , err )
}
}
2011-11-29 20:40:33 +00:00
// GetOwnerClaims
{
claims , err := id . Index . GetOwnerClaims ( pn , id . SignerBlobRef )
if err != nil {
t . Errorf ( "GetOwnerClaims = %v" , err )
} else {
want := search . ClaimList ( [ ] * search . Claim {
& search . Claim {
BlobRef : br1 ,
Permanode : pn ,
Signer : id . SignerBlobRef ,
2012-02-20 12:32:46 +00:00
Date : br1Time . UTC ( ) ,
2011-11-29 20:40:33 +00:00
Type : "set-attribute" ,
Attr : "foo" ,
Value : "foo1" ,
} ,
& search . Claim {
BlobRef : br2 ,
Permanode : pn ,
Signer : id . SignerBlobRef ,
2012-02-20 12:32:46 +00:00
Date : br2Time . UTC ( ) ,
2011-11-29 20:40:33 +00:00
Type : "set-attribute" ,
Attr : "foo" ,
Value : "foo2" ,
} ,
& search . Claim {
BlobRef : rootClaim ,
Permanode : pn ,
Signer : id . SignerBlobRef ,
2012-02-20 12:32:46 +00:00
Date : rootClaimTime . UTC ( ) ,
2011-11-29 20:40:33 +00:00
Type : "set-attribute" ,
Attr : "camliRoot" ,
Value : "rootval" ,
} ,
} )
if ! reflect . DeepEqual ( claims , want ) {
t . Errorf ( "GetOwnerClaims results differ.\n got: %v\nwant: %v" ,
claims , want )
}
}
}
2011-11-27 18:14:53 +00:00
}
2011-12-25 20:30:56 +00:00
func TestPathsOfSignerTarget_Memory ( t * testing . T ) {
testPathsOfSignerTarget ( t , newMemoryIndex )
}
func TestPathsOfSignerTarget_Mongo ( t * testing . T ) {
2011-12-31 23:00:34 +00:00
mongoTester { } . test ( t , testPathsOfSignerTarget )
2011-12-25 20:30:56 +00:00
}
func testPathsOfSignerTarget ( t * testing . T , initIdx func ( ) * Index ) {
id := NewIndexDeps ( initIdx ( ) )
2011-12-01 18:43:57 +00:00
pn := id . NewPermanode ( )
t . Logf ( "uploaded permanode %q" , pn )
claim1 := id . SetAttribute ( pn , "camliPath:somedir" , "targ-123" )
claim2 := id . SetAttribute ( pn , "camliPath:with|pipe" , "targ-124" )
t . Logf ( "made path claims %q and %q" , claim1 , claim2 )
id . dumpIndex ( t )
type test struct {
blobref string
2011-12-02 02:06:25 +00:00
want int
2011-12-01 18:43:57 +00:00
}
tests := [ ] test {
{ "targ-123" , 1 } ,
{ "targ-124" , 1 } ,
{ "targ-125" , 0 } ,
}
for _ , tt := range tests {
signer := id . SignerBlobRef
paths , err := id . Index . PathsOfSignerTarget ( signer , blobref . Parse ( tt . blobref ) )
if err != nil {
t . Fatalf ( "PathsOfSignerTarget(%q): %v" , tt . blobref , err )
}
if len ( paths ) != tt . want {
t . Fatalf ( "PathsOfSignerTarget(%q) got %d results; want %d" ,
tt . blobref , len ( paths ) , tt . want )
}
2011-12-02 01:28:32 +00:00
if tt . blobref == "targ-123" {
p := paths [ 0 ]
want := fmt . Sprintf (
"Path{Claim: %s, 2011-11-28T01:32:37.000123456Z; Base: %s + Suffix \"somedir\" => Target targ-123}" ,
claim1 , pn )
if g := p . String ( ) ; g != want {
t . Errorf ( "claim wrong.\n got: %s\nwant: %s" , g , want )
}
}
2011-12-01 18:43:57 +00:00
}
2011-12-02 02:06:25 +00:00
2012-02-20 12:32:46 +00:00
path , err := id . Index . PathLookup ( id . SignerBlobRef , pn , "with|pipe" , time . Now ( ) )
2011-12-02 02:06:25 +00:00
if err != nil {
t . Fatalf ( "PathLookup = %v" , err )
}
if g , e := path . Target . String ( ) , "targ-124" ; g != e {
t . Errorf ( "PathLookup = %q; want %q" , g , e )
}
2011-12-01 18:43:57 +00:00
}
2011-12-25 20:30:56 +00:00
func TestFiles_Memory ( t * testing . T ) {
testFiles ( t , newMemoryIndex )
}
func TestFiles_Mongo ( t * testing . T ) {
2011-12-31 23:00:34 +00:00
mongoTester { } . test ( t , testFiles )
2011-12-25 20:30:56 +00:00
}
func testFiles ( t * testing . T , initIdx func ( ) * Index ) {
id := NewIndexDeps ( initIdx ( ) )
2011-12-03 21:56:05 +00:00
fileRef , wholeRef := id . UploadFile ( "foo.html" , "<html>I am an html file.</html>" )
2011-12-03 19:26:42 +00:00
t . Logf ( "uploaded fileref %q, wholeRef %q" , fileRef , wholeRef )
id . dumpIndex ( t )
2011-12-03 21:56:05 +00:00
// ExistingFileSchemas
{
key := fmt . Sprintf ( "wholetofile|%s|%s" , wholeRef , fileRef )
if g , e := id . Get ( key ) , "1" ; g != e {
t . Fatalf ( "%q = %q, want %q" , key , g , e )
}
2011-12-03 19:26:42 +00:00
2011-12-03 21:56:05 +00:00
refs , err := id . Index . ExistingFileSchemas ( wholeRef )
if err != nil {
t . Fatalf ( "ExistingFileSchemas = %v" , err )
}
want := [ ] * blobref . BlobRef { fileRef }
if ! reflect . DeepEqual ( refs , want ) {
t . Errorf ( "ExistingFileSchemas got = %#v, want %#v" , refs , want )
}
2011-12-03 19:26:42 +00:00
}
2011-12-03 21:56:05 +00:00
// FileInfo
{
key := fmt . Sprintf ( "fileinfo|%s" , fileRef )
if g , e := id . Get ( key ) , "31|foo.html|text%2Fhtml" ; g != e {
t . Fatalf ( "%q = %q, want %q" , key , g , e )
}
fi , err := id . Index . GetFileInfo ( fileRef )
if err != nil {
t . Fatalf ( "GetFileInfo = %v" , err )
}
if g , e := fi . Size , int64 ( 31 ) ; g != e {
t . Errorf ( "Size = %d, want %d" , g , e )
}
if g , e := fi . FileName , "foo.html" ; g != e {
t . Errorf ( "FileName = %q, want %q" , g , e )
}
if g , e := fi . MimeType , "text/html" ; g != e {
t . Errorf ( "MimeType = %q, want %q" , g , e )
}
2011-12-03 19:26:42 +00:00
}
}
2011-11-27 16:36:30 +00:00
func TestReverseTimeString ( t * testing . T ) {
2011-11-27 16:41:20 +00:00
in := "2011-11-27T01:23:45Z"
got := reverseTimeString ( in )
2011-11-27 16:36:30 +00:00
want := "rt7988-88-72T98:76:54Z"
if got != want {
2011-11-27 16:41:20 +00:00
t . Fatalf ( "reverseTimeString = %q, want %q" , got , want )
}
back := unreverseTimeString ( got )
if back != in {
t . Fatalf ( "unreverseTimeString = %q, want %q" , back , in )
2011-11-27 16:36:30 +00:00
}
}