func buildIndexForFile(sib *indexer.SecureIndexBuilder, content string, docID int) index.SecureIndex {
	doc, err := ioutil.TempFile("", "indexTest")
	if err != nil {
		panic("cannot create the temporary test file")
	}
	defer os.Remove(doc.Name())  
	if _, err := doc.Write([]byte(content)); err != nil {
		panic("cannot write to the temporary test file")
	}
	if _, err := doc.Seek(0, 0); err != nil {
		panic("cannot rewind the temporary test file")
	}
	si := sib.BuildSecureIndex(docID, doc, len(content))
	return si
}
buildIndexForFile builds an index for a file with `content` and `docID` using
the SecureIndexBuilder `sib`.
package server
import (
	"bytes"
	"crypto/sha256"
	"io/ioutil"
	"os"
	"reflect"
	"search/prototype/index"
	"search/prototype/indexer"
	"search/prototype/util"
	"strconv"
	"testing"
)
func calculateMasterSecret(clientNum int, serverKeyHalf []byte) []byte {
	h := sha256.New()
	h.Write([]byte(strconv.Itoa(clientNum)))
	return util.XorBytes(h.Sum(nil), serverKeyHalf, len(serverKeyHalf))
}
func createTestServer(numClients, lenMS, lenSalt int, fpRate float64, numUniqWords uint64) (*Server, string) {
	dir, err := ioutil.TempDir("", "serverTest")
	if err != nil {
		panic("cannot create the temporary test directory")
	}
	s, err2 := CreateServer(numClients, lenMS, lenSalt, dir, fpRate, numUniqWords)
	if err2 != nil {
		panic("error when creating the server")
	}
	return s, dir
}
func TestCreateServer(t *testing.T) {
	numClients := 5
	lenMS := 8
	lenSalt := 8
	fpRate := 0.000001
	numUniqWords := uint64(100000)
	expectedR := 20
	expectedSize := uint64(2885391)
	s, dir := createTestServer(numClients, lenMS, lenSalt, fpRate, numUniqWords)
	defer os.RemoveAll(dir)
	if numClients != len(s.keyHalves) {
		t.Fatalf("incorrect number of server-side key halves")
	}
	if lenMS != len(s.keyHalves[0]) {
		t.Fatalf("incorrect length of the master secret being generated")
	}
	if lenMS != s.lenMS {
		t.Fatalf("incorrect length of the master secret")
	}
	if expectedR != len(s.salts) {
		t.Fatalf("incorrect number of salts generated")
	}
	if expectedSize != s.size {
		t.Fatalf("incorrect size of the server")
	}
	if lenSalt != len(s.salts[0]) {
		t.Fatalf("incorrect length of the salts")
	}
	if s.numFiles != 0 {
		t.Fatalf("number of files not initialized to zero")
	}
	if dir != s.directory {
		t.Fatalf("server directory not set up correctly")
	}
	ms := calculateMasterSecret(0, s.keyHalves[0])
	for i := 1; i < numClients; i++ {
		msCpy := calculateMasterSecret(i, s.keyHalves[i])
		if !bytes.Equal(ms, msCpy) {
			t.Fatalf("different master secrets derived from different clients")
		}
	}
}
func TestAddAndGetFile(t *testing.T) {
	s, dir := createTestServer(5, 8, 8, 0.000001, uint64(100000))
	defer os.RemoveAll(dir)
	files := [][]byte{
		[]byte("This is the very first file."),
		[]byte("Then comes the second."),
		[]byte("Now the third.")}
	for i := 0; i < 3; i++ {
		if docID, err := s.AddFile(files[i]); docID != i || err != nil {
			t.Fatalf("error when adding files: %s", err)
		}
	}
	for i := 0; i < 3; i++ {
		content, err := s.GetFile(i)
		if err != nil {
			t.Fatalf("error when getting the files: %s", err)
		}
		if !bytes.Equal(content, files[i]) {
			t.Fatalf("content in the file does not match")
		}
	}
}