Esempio n. 1
0
// To make sure we don't regress into issue 881: i.e. a permanode with no attr
// should not lead us to call index.claimsIntfAttrValue with a nil claims argument.
func TestDescribePermNoAttr(t *testing.T) {
	ix := index.NewMemoryIndex()
	ctx := context.Background()
	h := search.NewHandler(ix, owner)
	corpus, err := ix.KeepInMemory()
	if err != nil {
		t.Fatal(err)
	}
	h.SetCorpus(corpus)
	id := indextest.NewIndexDeps(ix)
	br := id.NewPlannedPermanode("noattr-0")

	ix.RLock()
	defer ix.RUnlock()

	res, err := h.Describe(ctx, &search.DescribeRequest{
		BlobRef: br,
		Depth:   1,
	})
	if err != nil {
		t.Fatalf("Describe for %v failed: %v", br, err)
	}
	db := res.Meta[br.String()]
	if db == nil {
		t.Fatalf("Describe result for %v is missing", br)
	}
}
Esempio n. 2
0
// should be run with -race
func TestDescribeRace(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping in short mode")
	}
	idx := index.NewMemoryIndex()
	idxd := indextest.NewIndexDeps(idx)
	idxd.Fataler = t
	corpus, err := idxd.Index.KeepInMemory()
	if err != nil {
		t.Fatalf("error slurping index to memory: %v", err)
	}
	h := search.NewHandler(idx, idxd.SignerBlobRef)
	h.SetCorpus(corpus)
	donec := make(chan struct{})
	headstart := 500
	blobrefs := make([]blob.Ref, headstart)
	headstartc := make(chan struct{})
	go func() {
		for i := 0; i < headstart*2; i++ {
			nth := fmt.Sprintf("%d", i)
			// No need to lock the index here. It is already done within NewPlannedPermanode,
			// because it calls idxd.Index.ReceiveBlob.
			pn := idxd.NewPlannedPermanode(nth)
			idxd.SetAttribute(pn, "tag", nth)
			if i > headstart {
				continue
			}
			if i == headstart {
				headstartc <- struct{}{}
				continue
			}
			blobrefs[i] = pn
		}
	}()
	<-headstartc
	ctx := context.Background()
	go func() {
		for i := 0; i < headstart; i++ {
			br := blobrefs[i]
			res, err := h.Describe(ctx, &search.DescribeRequest{
				BlobRef: br,
				Depth:   1,
			})
			if err != nil {
				t.Fatal(err)
			}
			_, ok := res.Meta[br.String()]
			if !ok {
				t.Errorf("permanode %v wasn't in Describe response", br)
			}
		}
		donec <- struct{}{}
	}()
	<-donec
}
Esempio n. 3
0
func TestDescribeLocation(t *testing.T) {
	tests := []struct {
		ref       string
		lat, long float64
		hasNoLoc  bool
	}{
		{ref: "filewithloc-0", lat: 45, long: 56},
		{ref: "location-0", lat: 45, long: 56},
		{ref: "locationpriority-1", lat: 67, long: 78},
		{ref: "locationpriority-2", lat: 12, long: 34},
		{ref: "locationoverride-1", lat: 67, long: 78},
		{ref: "locationoverride-2", lat: 67, long: 78},
		{ref: "homedir-0", hasNoLoc: true},
	}

	ix := searchDescribeSetup(test.NewFakeIndex())
	ctx := context.Background()
	h := search.NewHandler(ix, owner)

	ix.RLock()
	defer ix.RUnlock()

	for _, tt := range tests {
		var err error
		br := blob.MustParse(tt.ref)
		res, err := h.Describe(ctx, &search.DescribeRequest{
			BlobRef: br,
			Depth:   1,
		})
		if err != nil {
			t.Errorf("Describe for %v failed: %v", br, err)
			continue
		}
		db := res.Meta[br.String()]
		if db == nil {
			t.Errorf("Describe result for %v is missing", br)
			continue
		}
		loc := db.Location
		if tt.hasNoLoc {
			if loc != nil {
				t.Errorf("got location for %v, should have no location", br)
			}
		} else {
			if loc == nil {
				t.Errorf("no location in result for %v", br)
				continue
			}
			if loc.Latitude != tt.lat || loc.Longitude != tt.long {
				t.Errorf("location for %v invalid, got %f,%f want %f,%f",
					tt.ref, loc.Latitude, loc.Longitude, tt.lat, tt.long)
			}
		}
	}
}
Esempio n. 4
0
func TestPublishURLs(t *testing.T) {
	rootName := "foo"
	idxd := setupContent(rootName)
	sh := search.NewHandler(idxd.Index, idxd.SignerBlobRef)
	corpus, err := idxd.Index.KeepInMemory()
	if err != nil {
		t.Fatalf("error slurping index to memory: %v", err)
	}
	sh.SetCorpus(corpus)
	cl := camliClient.New("http://whatever.fake")
	fcl := &fakeClient{cl, sh}
	ph := &publishHandler{
		rootName: rootName,
		cl:       fcl,
	}
	if err := ph.initRootNode(); err != nil {
		t.Fatalf("initRootNode: %v", err)
	}

	for ti, tt := range publishURLTests {
		rw := httptest.NewRecorder()
		if !strings.HasPrefix(tt.path, "/pics/") {
			panic("expected /pics/ prefix on " + tt.path)
		}
		req, _ := http.NewRequest("GET", "http://foo.com"+tt.path, nil)

		pfxh := &httputil.PrefixHandler{
			Prefix: "/",
			Handler: http.HandlerFunc(func(_ http.ResponseWriter, req *http.Request) {
				// Because the app handler strips the prefix before passing it on to the app
				req.URL.Path = strings.TrimPrefix(req.URL.Path, "/pics/")
				pr, err := ph.NewRequest(rw, req)
				if err != nil {
					t.Fatalf("test #%d, NewRequest: %v", ti, err)
				}

				err = pr.findSubject()
				if tt.subject != "" {
					if err != nil {
						t.Errorf("test #%d, findSubject: %v", ti, err)
						return
					}
					if pr.subject.String() != tt.subject {
						t.Errorf("test #%d, got subject %q, want %q", ti, pr.subject, tt.subject)
					}
				}
				if pr.subres != tt.subres {
					t.Errorf("test #%d, got subres %q, want %q", ti, pr.subres, tt.subres)
				}
			}),
		}
		pfxh.ServeHTTP(rw, req)
	}
}
Esempio n. 5
0
func TestPublishURLs(t *testing.T) {
	rootName := "foo"
	idxd := setupContent(rootName)
	sh := search.NewHandler(idxd.Index, idxd.SignerBlobRef)
	corpus, err := idxd.Index.KeepInMemory()
	if err != nil {
		t.Fatalf("error slurping index to memory: %v", err)
	}
	sh.SetCorpus(corpus)
	ph := &PublishHandler{
		RootName: rootName,
		Search:   sh,
	}

	for ti, tt := range publishURLTests {
		rw := httptest.NewRecorder()
		if !strings.HasPrefix(tt.path, "/pics/") {
			panic("expected /pics/ prefix on " + tt.path)
		}
		req, _ := http.NewRequest("GET", "http://foo.com"+tt.path, nil)

		pfxh := &httputil.PrefixHandler{
			Prefix: "/pics/",
			Handler: http.HandlerFunc(func(_ http.ResponseWriter, req *http.Request) {
				pr := ph.NewRequest(rw, req)

				err := pr.findSubject()
				if tt.subject != "" {
					if err != nil {
						t.Errorf("test #%d, findSubject: %v", ti, err)
						return
					}
					if pr.subject.String() != tt.subject {
						t.Errorf("test #%d, got subject %q, want %q", ti, pr.subject, tt.subject)
					}
				}
				if pr.subres != tt.subres {
					t.Errorf("test #%d, got subres %q, want %q", ti, pr.subres, tt.subres)
				}
			}),
		}
		pfxh.ServeHTTP(rw, req)
	}
}
Esempio n. 6
0
func TestPublishURLs(t *testing.T) {

	owner := blob.MustParse("owner-1234")
	rootName := "foo"

	for ti, tt := range publishURLTests {
		idx := setupContent(owner, rootName)

		sh := search.NewHandler(idx, owner)
		ph := &PublishHandler{
			RootName: rootName,
			Search:   sh,
		}

		rw := httptest.NewRecorder()
		if !strings.HasPrefix(tt.path, "/pics/") {
			panic("expected /pics/ prefix on " + tt.path)
		}
		req, _ := http.NewRequest("GET", "http://foo.com"+tt.path, nil)

		pfxh := &httputil.PrefixHandler{
			Prefix: "/pics/",
			Handler: http.HandlerFunc(func(_ http.ResponseWriter, req *http.Request) {
				pr := ph.NewRequest(rw, req)

				err := pr.findSubject()
				if tt.subject != "" {
					if err != nil {
						t.Errorf("test #%d, findSubject: %v", ti, err)
						return
					}
					if pr.subject.String() != tt.subject {
						t.Errorf("test #%d, got subject %q, want %q", ti, pr.subject, tt.subject)
					}
				}
				if pr.subres != tt.subres {
					t.Errorf("test #%d, got subres %q, want %q", ti, pr.subres, tt.subres)
				}
			}),
		}
		pfxh.ServeHTTP(rw, req)
	}
}
Esempio n. 7
0
func TestPublishMembers(t *testing.T) {
	rootName := "foo"
	idxd := setupContent(rootName)

	sh := search.NewHandler(idxd.Index, idxd.SignerBlobRef)
	corpus, err := idxd.Index.KeepInMemory()
	if err != nil {
		t.Fatalf("error slurping index to memory: %v", err)
	}
	sh.SetCorpus(corpus)
	cl := camliClient.New("http://whatever.fake")
	fcl := &fakeClient{cl, sh}
	ph := &publishHandler{
		rootName: rootName,
		cl:       fcl,
	}

	rw := httptest.NewRecorder()
	req, _ := http.NewRequest("GET", "http://foo.com/pics", nil)

	pfxh := &httputil.PrefixHandler{
		Prefix: "/pics/",
		Handler: http.HandlerFunc(func(_ http.ResponseWriter, req *http.Request) {
			pr, err := ph.NewRequest(rw, req)
			if err != nil {
				t.Fatalf("NewRequest: %v", err)
			}

			res, err := pr.ph.deepDescribe(pr.subject)
			if err != nil {
				t.Fatalf("deepDescribe: %v", err)
			}

			members, err := pr.subjectMembers(res.Meta)
			if len(members.Members) != 2 {
				t.Errorf("Expected two members in publish root (one camlipath, one camlimember), got %d", len(members.Members))
			}
		}),
	}
	pfxh.ServeHTTP(rw, req)
}
Esempio n. 8
0
func TestPublishMembers(t *testing.T) {
	rootName := "foo"
	idxd := setupContent(rootName)

	sh := search.NewHandler(idxd.Index, idxd.SignerBlobRef)
	corpus, err := idxd.Index.KeepInMemory()
	if err != nil {
		t.Fatalf("error slurping index to memory: %v", err)
	}
	sh.SetCorpus(corpus)
	ph := &PublishHandler{
		RootName: rootName,
		Search:   sh,
	}

	rw := httptest.NewRecorder()
	req, _ := http.NewRequest("GET", "http://foo.com/pics", nil)

	pfxh := &httputil.PrefixHandler{
		Prefix: "/pics/",
		Handler: http.HandlerFunc(func(_ http.ResponseWriter, req *http.Request) {
			pr := ph.NewRequest(rw, req)

			dr := pr.ph.Search.NewDescribeRequest()
			dr.Describe(pr.subject, 3)
			res, err := dr.Result()
			if err != nil {
				t.Errorf("Result: %v", err)
				return
			}

			members, err := pr.subjectMembers(res)
			if len(members.Members) != 2 {
				t.Errorf("Expected two members in publish root (one camlipath, one camlimember), got %d", len(members.Members))
			}
		}),
	}
	pfxh.ServeHTTP(rw, req)
}
Esempio n. 9
0
func TestPublishMembers(t *testing.T) {
	owner := blob.MustParse("owner-1234")
	rootName := "foo"

	idx := setupContent(owner, rootName)

	sh := search.NewHandler(idx, owner)
	ph := &PublishHandler{
		RootName: rootName,
		Search:   sh,
	}

	rw := httptest.NewRecorder()
	req, _ := http.NewRequest("GET", "http://foo.com/pics", nil)

	pfxh := &httputil.PrefixHandler{
		Prefix: "/pics/",
		Handler: http.HandlerFunc(func(_ http.ResponseWriter, req *http.Request) {
			pr := ph.NewRequest(rw, req)

			dr := pr.ph.Search.NewDescribeRequest()
			dr.Describe(pr.subject, 3)
			res, err := dr.Result()
			if err != nil {
				t.Errorf("Result: %v", err)
				return
			}

			members, err := pr.subjectMembers(res)
			if len(members.Members) != 2 {
				t.Errorf("Expected two members in publish root (one camlipath, one camlimember), got %d", len(members.Members))
			}
		}),
	}
	pfxh.ServeHTTP(rw, req)
}
Esempio n. 10
0
func TestPublishURLs(t *testing.T) {
	owner := blobref.MustParse("owner-123")
	picNode := blobref.MustParse("picpn-123")
	galRef := blobref.MustParse("gal-123")
	rootRef := blobref.MustParse("root-abc")
	camp0 := blobref.MustParse("picpn-98765432100")
	camp1 := blobref.MustParse("picpn-98765432111")
	camp0f := blobref.MustParse("picfile-f00f00f00a5")
	camp1f := blobref.MustParse("picfile-f00f00f00b6")

	rootName := "foo"

	for ti, tt := range publishURLTests {
		idx := test.NewFakeIndex()
		idx.AddSignerAttrValue(owner, "camliRoot", rootName, rootRef)
		sh := search.NewHandler(idx, owner)
		ph := &PublishHandler{
			RootName: rootName,
			Search:   sh,
		}

		idx.AddMeta(owner, "text/x-openpgp-public-key", 100)
		for _, br := range []*blobref.BlobRef{picNode, galRef, rootRef, camp0, camp1} {
			idx.AddMeta(br, "application/json; camliType=permanode", 100)
		}
		for _, br := range []*blobref.BlobRef{camp0f, camp1f} {
			idx.AddMeta(br, "application/json; camliType=file", 100)
		}

		idx.AddClaim(owner, rootRef, "set-attribute", "camliPath:singlepic", picNode.String())
		idx.AddClaim(owner, rootRef, "set-attribute", "camliPath:camping", galRef.String())
		idx.AddClaim(owner, galRef, "add-attribute", "camliMember", camp0.String())
		idx.AddClaim(owner, galRef, "add-attribute", "camliMember", camp1.String())
		idx.AddClaim(owner, camp0, "set-attribute", "camliContent", camp0f.String())
		idx.AddClaim(owner, camp1, "set-attribute", "camliContent", camp1f.String())

		rw := httptest.NewRecorder()
		if !strings.HasPrefix(tt.path, "/pics/") {
			panic("expected /pics/ prefix on " + tt.path)
		}
		req, _ := http.NewRequest("GET", "http://foo.com"+tt.path, nil)

		pfxh := &httputil.PrefixHandler{
			Prefix: "/pics/",
			Handler: http.HandlerFunc(func(_ http.ResponseWriter, req *http.Request) {
				pr := ph.NewRequest(rw, req)

				err := pr.findSubject()
				if tt.subject != "" {
					if err != nil {
						t.Errorf("test #%d, findSubject: %v", ti, err)
						return
					}
					if pr.subject.String() != tt.subject {
						t.Errorf("test #%d, got subject %q, want %q", ti, pr.subject, tt.subject)
					}
				}
				if pr.subres != tt.subres {
					t.Errorf("test #%d, got subres %q, want %q", ti, pr.subres, tt.subres)
				}
			}),
		}
		pfxh.ServeHTTP(rw, req)
	}
}