2013-09-21 08:54:07 +00:00
|
|
|
/*
|
2013-09-24 08:38:38 +00:00
|
|
|
Copyright 2013 The Camlistore Authors.
|
2013-09-21 08:54:07 +00:00
|
|
|
|
|
|
|
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 server
|
|
|
|
|
|
|
|
import (
|
2013-09-24 08:38:38 +00:00
|
|
|
"fmt"
|
2013-09-21 08:54:07 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"camlistore.org/pkg/blob"
|
2013-09-24 08:38:38 +00:00
|
|
|
"camlistore.org/pkg/blobserver"
|
2013-09-21 08:54:07 +00:00
|
|
|
"camlistore.org/pkg/schema"
|
|
|
|
"camlistore.org/pkg/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestHandleGetViaSharing(t *testing.T) {
|
|
|
|
sto := &test.Fetcher{}
|
2013-09-24 08:38:38 +00:00
|
|
|
handler := &shareHandler{fetcher: sto}
|
|
|
|
var wr *httptest.ResponseRecorder
|
|
|
|
|
|
|
|
putRaw := func(ref blob.Ref, data string) {
|
|
|
|
if _, err := blobserver.Receive(sto, ref, strings.NewReader(data)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
put := func(blob *schema.Blob) {
|
|
|
|
putRaw(blob.BlobRef(), blob.JSON())
|
|
|
|
}
|
2013-09-21 08:54:07 +00:00
|
|
|
|
2013-09-24 08:38:38 +00:00
|
|
|
get := func(path string) *shareError {
|
2013-09-21 08:54:07 +00:00
|
|
|
wr = httptest.NewRecorder()
|
|
|
|
req, _ := http.NewRequest("GET", "http://unused/"+path, nil)
|
2013-09-24 08:38:38 +00:00
|
|
|
err := handler.serveHTTP(wr, req)
|
|
|
|
if err != nil {
|
|
|
|
return err.(*shareError)
|
|
|
|
}
|
|
|
|
return nil
|
2013-09-21 08:54:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
content := "monkey"
|
|
|
|
contentRef := blob.SHA1FromString(content)
|
|
|
|
|
|
|
|
// For the purposes of following the via chain, the only thing that
|
|
|
|
// matters is that the content of each link contains the name of the
|
|
|
|
// next link.
|
|
|
|
link := contentRef.String()
|
|
|
|
linkRef := blob.SHA1FromString(link)
|
|
|
|
|
|
|
|
share := schema.NewShareRef(schema.ShareHaveRef, linkRef, false).
|
|
|
|
SetSigner(blob.SHA1FromString("irrelevant")).
|
|
|
|
SetRawStringField("camliSig", "alsounused")
|
|
|
|
|
2013-09-24 08:38:38 +00:00
|
|
|
var err *shareError
|
2013-09-21 08:54:07 +00:00
|
|
|
|
2013-09-24 08:38:38 +00:00
|
|
|
if err = get(share.Blob().BlobRef().String()); err == nil || err.code != shareFetchFailed {
|
|
|
|
t.Error("Expected missing blob error")
|
|
|
|
}
|
2013-09-21 08:54:07 +00:00
|
|
|
|
2013-09-24 08:38:38 +00:00
|
|
|
put(share.Blob())
|
|
|
|
if err = get(fmt.Sprintf("%s?via=%s", contentRef, share.Blob().BlobRef())); err == nil || err.code != shareTargetInvalid {
|
|
|
|
t.Error("Expected invalid target error")
|
|
|
|
}
|
2013-09-21 08:54:07 +00:00
|
|
|
|
2013-09-24 08:38:38 +00:00
|
|
|
putRaw(linkRef, link)
|
|
|
|
if err = get(linkRef.String()); err == nil || err.code != shareReadFailed {
|
|
|
|
t.Error("Expected invalid share blob error")
|
|
|
|
}
|
2013-09-21 08:54:07 +00:00
|
|
|
|
2013-09-24 08:38:38 +00:00
|
|
|
if err = get(share.Blob().BlobRef().String()); err != nil {
|
|
|
|
t.Error("Expected to successfully fetch share, but got: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = get(fmt.Sprintf("%s?via=%s", linkRef, share.Blob().BlobRef())); err != nil {
|
|
|
|
t.Error("Expected to successfully fetch link via share, but got: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = get(fmt.Sprintf("%s?via=%s,%s", contentRef, share.Blob().BlobRef(), linkRef)); err == nil || err.code != shareNotTransitive {
|
|
|
|
t.Error("Expected share not transitive error")
|
|
|
|
}
|
2013-09-21 08:54:07 +00:00
|
|
|
|
|
|
|
share.SetShareIsTransitive(true)
|
2013-09-24 08:38:38 +00:00
|
|
|
put(share.Blob())
|
|
|
|
if err = get(fmt.Sprintf("%s?via=%s,%s", linkRef, share.Blob().BlobRef(), linkRef)); err == nil || err.code != viaChainInvalidLink {
|
|
|
|
t.Error("Expected via chain invalid link err")
|
|
|
|
}
|
2013-09-21 08:54:07 +00:00
|
|
|
|
2013-09-24 08:38:38 +00:00
|
|
|
putRaw(contentRef, content)
|
|
|
|
if err = get(fmt.Sprintf("%s?via=%s,%s", contentRef, share.Blob().BlobRef(), linkRef)); err != nil {
|
|
|
|
t.Error("Expected to succesfully fetch via link via share, but got: %s", err)
|
|
|
|
}
|
2013-09-21 08:54:07 +00:00
|
|
|
|
|
|
|
share.SetShareExpiration(time.Now().Add(-time.Duration(10) * time.Minute))
|
2013-09-24 08:38:38 +00:00
|
|
|
put(share.Blob())
|
|
|
|
if err = get(fmt.Sprintf("%s?via=%s,%s", contentRef, share.Blob().BlobRef(), linkRef)); err == nil || err.code != shareExpired {
|
|
|
|
t.Error("Expected share expired error")
|
|
|
|
}
|
2013-09-21 08:54:07 +00:00
|
|
|
|
|
|
|
share.SetShareExpiration(time.Now().Add(time.Duration(10) * time.Minute))
|
2013-09-24 08:38:38 +00:00
|
|
|
put(share.Blob())
|
|
|
|
if err = get(fmt.Sprintf("%s?via=%s,%s", contentRef, share.Blob().BlobRef(), linkRef)); err != nil {
|
|
|
|
t.Error("Expected to successfully fetch unexpired share, but got: %s", err)
|
|
|
|
}
|
2013-09-21 08:54:07 +00:00
|
|
|
|
|
|
|
// TODO(aa): assemble
|
|
|
|
}
|