Ejemplo n.º 1
0
func TestStaticSocket(t *testing.T) {
	tdir, err := ioutil.TempDir("", "schema-test-")
	if err != nil {
		t.Fatalf("ioutil.TempDir(): %v", err)
	}
	defer os.RemoveAll(tdir)

	sockPath := filepath.Join(tdir, "socket")
	err = osutil.Mksocket(sockPath)
	if err == osutil.ErrNotSupported {
		t.SkipNow()
	}
	if err != nil {
		t.Fatalf("osutil.Mksocket(): %v", err)
	}

	fi, err := os.Lstat(sockPath)
	if err != nil {
		t.Fatalf("os.Lstat(): %v", err)
	}

	bb := NewCommonFileMap(sockPath, fi)
	bb.SetType("socket")
	bb.SetFileName(sockPath)
	blob := bb.Blob()
	t.Logf("Got JSON for socket: %s\n", blob.JSON())

	sf, ok := blob.AsStaticFile()
	if !ok {
		t.Fatalf("Blob.AsStaticFile(): Expected true, got false")
	}
	_, ok = sf.AsStaticSocket()
	if !ok {
		t.Fatalf("StaticFile.AsStaticSocket(): Expected true, got false")
	}
}
Ejemplo n.º 2
0
func TestHandleGetViaSharing(t *testing.T) {
	sto := &test.Fetcher{}
	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())
	}

	get := func(path string) *shareError {
		wr = httptest.NewRecorder()
		req, _ := http.NewRequest("GET", "http://unused/"+path, nil)
		err := handler.serveHTTP(wr, req)
		if err != nil {
			return err.(*shareError)
		}
		return nil
	}

	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")

	var err *shareError

	if err = get(share.Blob().BlobRef().String()); err == nil || err.code != shareFetchFailed {
		t.Error("Expected missing blob error")
	}

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

	putRaw(linkRef, link)
	if err = get(linkRef.String()); err == nil || err.code != shareReadFailed {
		t.Error("Expected invalid share blob error")
	}

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

	share.SetShareIsTransitive(true)
	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")
	}

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

	share.SetShareExpiration(time.Now().Add(-time.Duration(10) * time.Minute))
	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")
	}

	share.SetShareExpiration(time.Now().Add(time.Duration(10) * time.Minute))
	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)
	}

	// TODO(aa): assemble
}
Ejemplo n.º 3
0
func TestHandleGetViaSharing(t *testing.T) {
	sto := &test.Fetcher{}
	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())
	}

	get := func(path string) *shareError {
		wr = httptest.NewRecorder()
		req, _ := http.NewRequest("GET", "http://unused/"+path, nil)
		err := handler.serveHTTP(wr, req)
		if err != nil {
			return err.(*shareError)
		}
		return nil
	}

	testGet := func(path string, expectedError errorCode) {
		err := get(path)
		if expectedError != noError {
			if err == nil || err.code != expectedError {
				t.Errorf("Fetching %s, expected error %#v, but got %#v", path, expectedError, err)
			}
		} else {
			if err != nil {
				t.Errorf("Fetching %s, expected success but got %#v", path, err)
			}
		}

		if wr.HeaderMap.Get("Access-Control-Allow-Origin") != "*" {
			t.Errorf("Fetching %s, share response did not contain expected CORS header", path)
		}
	}

	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, false).
		SetShareTarget(linkRef).
		SetSigner(blob.SHA1FromString("irrelevant")).
		SetRawStringField("camliSig", "alsounused")

	testGet(share.Blob().BlobRef().String(), shareFetchFailed)

	put(share.Blob())
	testGet(fmt.Sprintf("%s?via=%s", contentRef, share.Blob().BlobRef()), shareTargetInvalid)

	putRaw(linkRef, link)
	testGet(linkRef.String(), shareReadFailed)
	testGet(share.Blob().BlobRef().String(), noError)
	testGet(fmt.Sprintf("%s?via=%s", linkRef, share.Blob().BlobRef()), noError)
	testGet(fmt.Sprintf("%s?via=%s,%s", contentRef, share.Blob().BlobRef(), linkRef), shareNotTransitive)

	share.SetShareIsTransitive(true)
	put(share.Blob())
	testGet(fmt.Sprintf("%s?via=%s,%s", linkRef, share.Blob().BlobRef(), linkRef), viaChainInvalidLink)

	putRaw(contentRef, content)
	testGet(fmt.Sprintf("%s?via=%s,%s", contentRef, share.Blob().BlobRef(), linkRef), noError)

	share.SetShareExpiration(time.Now().Add(-time.Duration(10) * time.Minute))
	put(share.Blob())
	testGet(fmt.Sprintf("%s?via=%s,%s", contentRef, share.Blob().BlobRef(), linkRef), shareExpired)

	share.SetShareExpiration(time.Now().Add(time.Duration(10) * time.Minute))
	put(share.Blob())
	testGet(fmt.Sprintf("%s?via=%s,%s", contentRef, share.Blob().BlobRef(), linkRef), noError)

	// TODO(aa): assemble
}