Merge "veyron/services/mgmt: port of binaryd integration test to Go"
diff --git a/lib/testutil/integration/util.go b/lib/testutil/integration/util.go
index 0c06c57..d5e2cf4 100644
--- a/lib/testutil/integration/util.go
+++ b/lib/testutil/integration/util.go
@@ -97,7 +97,7 @@
 	if err := cmd.Start(); err != nil {
 		return nil, fmt.Errorf("%q failed: %v", strings.Join(cmd.Args, " "), err)
 	}
-	// Wait for the server to mount itself.
+	// Wait for the server to mount both its tcp and ws endpoint.
 	ready := make(chan struct{}, 1)
 	go func() {
 		defer outPipe.Close()
diff --git a/services/mgmt/binary/binaryd/main.go b/services/mgmt/binary/binaryd/main.go
index 16ac319..3ac83da 100644
--- a/services/mgmt/binary/binaryd/main.go
+++ b/services/mgmt/binary/binaryd/main.go
@@ -20,9 +20,9 @@
 const defaultDepth = 3
 
 var (
-	name     = flag.String("name", "", "name to mount the binary repository as")
-	rootFlag = flag.String("root", "", "root directory for the binary repository")
-	httpAddr = flag.String("http", ":0", "TCP address on which the HTTP server runs")
+	name        = flag.String("name", "", "name to mount the binary repository as")
+	rootDirFlag = flag.String("root_dir", "", "root directory for the binary repository")
+	httpAddr    = flag.String("http", ":0", "TCP address on which the HTTP server runs")
 )
 
 // toIPPort tries to swap in the 'best' accessible IP for the host part of the
@@ -53,32 +53,31 @@
 	}
 	defer runtime.Cleanup()
 
-	root, err := impl.SetupRoot(*rootFlag)
+	rootDir, err := impl.SetupRootDir(*rootDirFlag)
 	if err != nil {
-		vlog.Errorf("SetupRoot(%q) failed: %v", *rootFlag, err)
+		vlog.Errorf("SetupRootDir(%q) failed: %v", *rootDirFlag, err)
 		return
 	}
-	vlog.Infof("Binary repository rooted at %v", root)
-
-	state, err := impl.NewState(root, defaultDepth)
-	if err != nil {
-		vlog.Errorf("NewState(%v, %v) failed: %v", root, defaultDepth, err)
-		return
-	}
+	vlog.Infof("Binary repository rooted at %v", rootDir)
 
 	listener, err := net.Listen("tcp", *httpAddr)
 	if err != nil {
 		vlog.Errorf("Listen(%s) failed: %v", *httpAddr, err)
 		os.Exit(1)
 	}
-	vlog.Infof("Binary repository HTTP server at: %q", toIPPort(listener.Addr().String()))
+	rootURL := toIPPort(listener.Addr().String())
+	state, err := impl.NewState(rootDir, rootURL, defaultDepth)
+	if err != nil {
+		vlog.Errorf("NewState(%v, %v, %v) failed: %v", rootDir, rootURL, defaultDepth, err)
+		return
+	}
+	vlog.Infof("Binary repository HTTP server at: %q", rootURL)
 	go func() {
 		if err := http.Serve(listener, http.FileServer(impl.NewHTTPRoot(state))); err != nil {
 			vlog.Errorf("Serve() failed: %v", err)
 			os.Exit(1)
 		}
 	}()
-
 	server, err := runtime.NewServer()
 	if err != nil {
 		vlog.Errorf("NewServer() failed: %v", err)
diff --git a/services/mgmt/binary/binaryd/test.sh b/services/mgmt/binary/binaryd/test.sh
deleted file mode 100755
index 17347fa..0000000
--- a/services/mgmt/binary/binaryd/test.sh
+++ /dev/null
@@ -1,81 +0,0 @@
-#!/bin/bash
-
-# Test the binary repository daemon.
-#
-# This test starts a binary repository daemon and uses the binary
-# repository client to verify that <binary>.Upload(),
-# <binary>.Download(), and <binary>.Delete() work as expected.
-
-source "${VEYRON_ROOT}/scripts/lib/shell_test.sh"
-
-readonly WORKDIR="${shell_test_WORK_DIR}"
-
-build() {
-  BINARYD_BIN="$(shell_test::build_go_binary 'veyron.io/veyron/veyron/services/mgmt/binary/binaryd')"
-  BINARY_BIN="$(shell_test::build_go_binary 'veyron.io/veyron/veyron/tools/binary')"
-}
-
-main() {
-  cd "${WORKDIR}"
-  build
-
-  shell_test::setup_server_test
-
-  # Start the binary repository daemon.
-  local -r REPO="binaryd-test-repo"
-  shell_test::start_server "${BINARYD_BIN}" --name="${REPO}" --veyron.tcp.address=127.0.0.1:0 --http=127.0.0.1:0 \
-    || shell_test::fail "line ${LINENO} failed to start binaryd"
-  local -r HTTP_ADDR=$(grep 'HTTP server at: "' "${START_SERVER_LOG_FILE}" | sed -e 's/^.*HTTP server at: "//' | sed -e 's/"$//')
-
-  # Create a binary file.
-  local -r BINARY_SUFFIX="test-binary"
-  local -r BINARY="${REPO}/${BINARY_SUFFIX}"
-  local -r BINARY_FILE="${WORKDIR}/bin1"
-  dd if=/dev/urandom of="${BINARY_FILE}" bs=1000000 count=16 \
-    || shell_test::fail "line ${LINENO}: faile to create a random binary file"
-  "${BINARY_BIN}" upload "${BINARY}" "${BINARY_FILE}" || shell_test::fail "line ${LINENO}: 'upload' failed"
-
-  # Create TAR file.
-  local -r TAR="${REPO}/tarobj"
-  local -r TAR_FILE="${WORKDIR}/bin1.tar.gz"
-  tar zcvf "${TAR_FILE}" "${BINARY_FILE}"
-  "${BINARY_BIN}" upload "${TAR}" "${TAR_FILE}" || shell_test::fail "line ${LINENO}: 'upload' failed"
-
-  # Download the binary file.
-  local -r BINARY_FILE2="${WORKDIR}/bin2"
-  "${BINARY_BIN}" download "${BINARY}" "${BINARY_FILE2}" || shell_test::fail "line ${LINENO}: 'RPC download' failed"
-  if [[ $(cmp "${BINARY_FILE}" "${BINARY_FILE2}" &> /dev/null) ]]; then
-    shell_test::fail "mismatching binary file downloaded via RPC"
-  fi
-  local -r BINARY_FILE2_INFO=$(cat "${BINARY_FILE2}.__info")
-  shell_test::assert_eq "${BINARY_FILE2_INFO}" '{"Type":"application/octet-stream","Encoding":""}' "${LINENO}"
-
-  # Download the tar file.
-  local -r TAR_FILE2="${WORKDIR}/downloadedtar"
-  "${BINARY_BIN}" download "${TAR}" "${TAR_FILE2}" || shell_test::fail "line ${LINENO}: 'RPC download' failed"
-  if [[ $(cmp "${TAR_FILE}" "${TAR_FILE2}" &> /dev/null) ]]; then
-    shell_test::fail "mismatching tar file downloaded via RPC"
-  fi
-  local -r TAR_FILE2_INFO=$(cat "${TAR_FILE2}.__info")
-  shell_test::assert_eq "${TAR_FILE2_INFO}" '{"Type":"application/x-tar","Encoding":"gzip"}' "${LINENO}"
-
-  local -r BINARY_FILE3="${WORKDIR}/bin3"
-  curl -f -o "${BINARY_FILE3}" "http://${HTTP_ADDR}/${BINARY_SUFFIX}" || shell_test::fail "line ${LINENO}: 'HTTP download' failed"
-  if [[ $(cmp "${BINARY_FILE}" "${BINARY_FILE3}" &> /dev/null) ]]; then
-    shell_test::fail "mismatching binary file downloaded via HTTP"
-  fi
-
-  # Remove the files.
-  "${BINARY_BIN}" delete "${BINARY}" || shell_test::fail "line ${LINENO}: 'delete' failed"
-  "${BINARY_BIN}" delete "${TAR}" || shell_test::fail "line ${LINENO}: 'delete' failed"
-
-  # Check the files no longer exist.
-  local RESULT=$(shell::check_result "${BINARY_BIN}" download "${BINARY}" "${BINARY_FILE2}")
-  shell_test::assert_ne "${RESULT}" "0" "${LINENO}"
-  RESULT=$(shell::check_result "${BINARY_BIN}" download "${TAR}" "${TAR_FILE2}")
-  shell_test::assert_ne "${RESULT}" "0" "${LINENO}"
-
-  shell_test::pass
-}
-
-main "$@"
diff --git a/services/mgmt/binary/binaryd/testdata/integration_test.go b/services/mgmt/binary/binaryd/testdata/integration_test.go
new file mode 100644
index 0000000..7bd82f6
--- /dev/null
+++ b/services/mgmt/binary/binaryd/testdata/integration_test.go
@@ -0,0 +1,242 @@
+package integration_test
+
+import (
+	"bytes"
+	"fmt"
+	"io/ioutil"
+	"os"
+	"os/exec"
+	"path/filepath"
+	"strings"
+	"testing"
+
+	"veyron.io/veyron/veyron/lib/modules"
+	"veyron.io/veyron/veyron/lib/testutil"
+	"veyron.io/veyron/veyron/lib/testutil/integration"
+	"veyron.io/veyron/veyron/lib/testutil/security"
+	_ "veyron.io/veyron/veyron/profiles"
+	"veyron.io/veyron/veyron2/naming"
+)
+
+func init() {
+	testutil.Init()
+}
+
+var binPkgs = []string{
+	"veyron.io/veyron/veyron/services/mgmt/binary/binaryd",
+	"veyron.io/veyron/veyron/tools/binary",
+}
+
+func checkFileType(t *testing.T, file, typeString string) {
+	var catOut bytes.Buffer
+	catCmd := exec.Command("cat", file+".__info")
+	catCmd.Stdout = &catOut
+	catCmd.Stderr = &catOut
+	if err := catCmd.Run(); err != nil {
+		t.Fatalf("%q failed: %v\n%v", strings.Join(catCmd.Args, " "), err, catOut.String())
+	}
+	if got, want := strings.TrimSpace(catOut.String()), typeString; got != want {
+		t.Fatalf("unexpect file type: got %v, want %v", got, want)
+	}
+}
+
+func compareFiles(t *testing.T, f1, f2 string) {
+	var cmpOut bytes.Buffer
+	cmpCmd := exec.Command("cmp", f1, f2)
+	cmpCmd.Stdout = &cmpOut
+	cmpCmd.Stderr = &cmpOut
+	if err := cmpCmd.Run(); err != nil {
+		t.Fatalf("%q failed: %v\n%v", strings.Join(cmpCmd.Args, " "), err, cmpOut.String())
+	}
+}
+
+func deleteFile(t *testing.T, binDir, credentials, mt, name, suffix string) {
+	var deleteOut bytes.Buffer
+	deleteArgs := []string{
+		"-veyron.credentials=" + credentials,
+		"-veyron.namespace.root=" + mt,
+		"delete", naming.Join(name, suffix),
+	}
+	deleteCmd := exec.Command(filepath.Join(binDir, "binary"), deleteArgs...)
+	deleteCmd.Stdout = &deleteOut
+	deleteCmd.Stderr = &deleteOut
+	if err := deleteCmd.Run(); err != nil {
+		t.Fatalf("%q failed: %v\n%v", strings.Join(deleteCmd.Args, " "), err, deleteOut.String())
+	}
+}
+
+func downloadFile(t *testing.T, expectError bool, binDir, credentials, mt, name, path, suffix string) {
+	var downloadOut bytes.Buffer
+	downloadArgs := []string{
+		"-veyron.credentials=" + credentials,
+		"-veyron.namespace.root=" + mt,
+		"download", naming.Join(name, suffix), path,
+	}
+	downloadCmd := exec.Command(filepath.Join(binDir, "binary"), downloadArgs...)
+	downloadCmd.Stdout = &downloadOut
+	downloadCmd.Stderr = &downloadOut
+	err := downloadCmd.Run()
+	if err != nil && !expectError {
+		t.Fatalf("%q failed: %v\n%v", strings.Join(downloadCmd.Args, " "), err, downloadOut.String())
+	}
+	if err == nil && expectError {
+		t.Fatalf("%q did not fail when it should", strings.Join(downloadCmd.Args, " "))
+	}
+}
+
+func downloadURL(t *testing.T, path, rootURL, suffix string) {
+	var curlOut bytes.Buffer
+	curlCmd := exec.Command("curl", "-f", "-o", path, fmt.Sprintf("%v/%v", rootURL, suffix))
+	curlCmd.Stdout = &curlOut
+	curlCmd.Stderr = &curlOut
+	if err := curlCmd.Run(); err != nil {
+		t.Fatalf("%q failed: %v\n%v", strings.Join(curlCmd.Args, " "), err, curlOut.String())
+	}
+}
+
+func rootURL(t *testing.T, binDir, credentials, mt, name string) string {
+	var rootOut bytes.Buffer
+	rootArgs := []string{
+		"-veyron.credentials=" + credentials,
+		"-veyron.namespace.root=" + mt,
+		"url", name,
+	}
+	rootCmd := exec.Command(filepath.Join(binDir, "binary"), rootArgs...)
+	rootCmd.Stdout = &rootOut
+	rootCmd.Stderr = &rootOut
+	if err := rootCmd.Run(); err != nil {
+		t.Fatalf("%q failed: %v\n%v", strings.Join(rootCmd.Args, " "), err, rootOut.String())
+	}
+	return strings.TrimSpace(rootOut.String())
+}
+
+func uploadFile(t *testing.T, binDir, credentials, mt, name, path, suffix string) {
+	var uploadOut bytes.Buffer
+	uploadArgs := []string{
+		"-veyron.credentials=" + credentials,
+		"-veyron.namespace.root=" + mt,
+		"upload", naming.Join(name, suffix), path,
+	}
+	uploadCmd := exec.Command(filepath.Join(binDir, "binary"), uploadArgs...)
+	uploadCmd.Stdout = &uploadOut
+	uploadCmd.Stderr = &uploadOut
+	if err := uploadCmd.Run(); err != nil {
+		t.Fatalf("%q failed: %v\n%v", strings.Join(uploadCmd.Args, " "), err, uploadOut.String())
+	}
+}
+
+func TestHelperProcess(t *testing.T) {
+	modules.DispatchInTest()
+}
+
+func TestBinaryRepositoryIntegration(t *testing.T) {
+	// Build the required binaries.
+	binDir, cleanup, err := integration.BuildPkgs(binPkgs)
+	if err != nil {
+		t.Fatalf("%v", err)
+	}
+	defer cleanup()
+
+	// Start a root mount table.
+	shell, err := modules.NewShell(nil)
+	if err != nil {
+		t.Fatalf("NewShell() failed: %v", err)
+	}
+	defer shell.Cleanup(os.Stdin, os.Stderr)
+	handle, mt, err := integration.StartRootMT(shell)
+	if err != nil {
+		t.Fatalf("%v", err)
+	}
+	defer handle.CloseStdin()
+
+	// Generate credentials.
+	principal := security.NewPrincipal("root")
+	credentials := security.NewVeyronCredentials(principal, "test-credentials")
+	defer os.RemoveAll(credentials)
+
+	// Start the build server.
+	binaryRepoBin := filepath.Join(binDir, "binaryd")
+	binaryRepoName := "test-binary-repository"
+	args := []string{
+		"-name=" + binaryRepoName,
+		"-http=127.0.0.1:0",
+		"-veyron.tcp.address=127.0.0.1:0",
+		"-veyron.credentials=" + credentials,
+		"-veyron.namespace.root=" + mt,
+	}
+	serverProcess, err := integration.StartServer(binaryRepoBin, args)
+	if err != nil {
+		t.Fatalf("%v", err)
+	}
+	defer serverProcess.Kill()
+
+	// Upload a random binary file.
+	binFile, err := ioutil.TempFile("", "")
+	if err != nil {
+		t.Fatalf("TempFile() failed: %v", err)
+	}
+	defer binFile.Close()
+	defer os.Remove(binFile.Name())
+	if _, err := binFile.Write(testutil.RandomBytes(16 * 1000 * 1000)); err != nil {
+		t.Fatalf("Write() failed: %v", err)
+	}
+	binSuffix := "test-binary"
+	uploadFile(t, binDir, credentials, mt, binaryRepoName, binFile.Name(), binSuffix)
+
+	// Upload a compressed version of the binary file.
+	tarFile := binFile.Name() + ".tar.gz"
+	var tarOut bytes.Buffer
+	tarCmd := exec.Command("tar", "zcvf", tarFile, binFile.Name())
+	tarCmd.Stdout = &tarOut
+	tarCmd.Stderr = &tarOut
+	if err := tarCmd.Run(); err != nil {
+		t.Fatalf("%q failed: %v\n%v", strings.Join(tarCmd.Args, " "), err, tarOut.String())
+	}
+	defer os.Remove(tarFile)
+	tarSuffix := "test-compressed-file"
+	uploadFile(t, binDir, credentials, mt, binaryRepoName, tarFile, tarSuffix)
+
+	// Download the binary file and check that it matches the
+	// original one and that it has the right file type.
+	downloadedBinFile := binFile.Name() + "-downloaded"
+	defer os.Remove(downloadedBinFile)
+	downloadFile(t, false, binDir, credentials, mt, binaryRepoName, downloadedBinFile, binSuffix)
+	compareFiles(t, binFile.Name(), downloadedBinFile)
+	checkFileType(t, downloadedBinFile, `{"Type":"application/octet-stream","Encoding":""}`)
+
+	// Download the compressed version of the binary file and
+	// check that it matches the original one and that it has the
+	// right file type.
+	downloadedTarFile := binFile.Name() + "-downloaded.tar.gz"
+	defer os.Remove(downloadedTarFile)
+	downloadFile(t, false, binDir, credentials, mt, binaryRepoName, downloadedTarFile, tarSuffix)
+	compareFiles(t, tarFile, downloadedTarFile)
+	checkFileType(t, downloadedTarFile, `{"Type":"application/x-tar","Encoding":"gzip"}`)
+
+	// Fetch the root URL of the HTTP server used by the binary
+	// repository to serve URLs.
+	root := rootURL(t, binDir, credentials, mt, binaryRepoName)
+
+	// Download the binary file using the HTTP protocol and check
+	// that it matches the original one.
+	downloadedBinFileURL := binFile.Name() + "-downloaded-url"
+	defer os.Remove(downloadedBinFileURL)
+	downloadURL(t, downloadedBinFileURL, root, binSuffix)
+	compareFiles(t, downloadedBinFile, downloadedBinFileURL)
+
+	// Download the compressed version of the binary file using
+	// the HTTP protocol and check that it matches the original
+	// one.
+	downloadedTarFileURL := binFile.Name() + "-downloaded-url.tar.gz"
+	defer os.Remove(downloadedTarFileURL)
+	downloadURL(t, downloadedTarFileURL, root, tarSuffix)
+	compareFiles(t, downloadedTarFile, downloadedTarFileURL)
+
+	// Delete the files.
+	deleteFile(t, binDir, credentials, mt, binaryRepoName, binSuffix)
+	deleteFile(t, binDir, credentials, mt, binaryRepoName, tarSuffix)
+
+	// Check the files no longer exist.
+	downloadFile(t, true, binDir, credentials, mt, binaryRepoName, downloadedBinFile, binSuffix)
+	downloadFile(t, true, binDir, credentials, mt, binaryRepoName, downloadedTarFile, tarSuffix)
+}
diff --git a/services/mgmt/binary/impl/fs_utils.go b/services/mgmt/binary/impl/fs_utils.go
index 62eaab8..3240d31 100644
--- a/services/mgmt/binary/impl/fs_utils.go
+++ b/services/mgmt/binary/impl/fs_utils.go
@@ -92,7 +92,7 @@
 // createObjectNameTree returns a tree of all the valid object names in the
 // repository.
 func (i *binaryService) createObjectNameTree() *treeNode {
-	pattern := i.state.root
+	pattern := i.state.rootDir
 	for d := 0; d < i.state.depth; d++ {
 		pattern = filepath.Join(pattern, "*")
 	}
diff --git a/services/mgmt/binary/impl/impl_test.go b/services/mgmt/binary/impl/impl_test.go
index 6f603e0..b336cd1 100644
--- a/services/mgmt/binary/impl/impl_test.go
+++ b/services/mgmt/binary/impl/impl_test.go
@@ -105,11 +105,11 @@
 // startServer starts the binary repository server.
 func startServer(t *testing.T, depth int) (repository.BinaryClientMethods, string, string, func()) {
 	// Setup the root of the binary repository.
-	root, err := ioutil.TempDir("", veyronPrefix)
+	rootDir, err := ioutil.TempDir("", veyronPrefix)
 	if err != nil {
 		t.Fatalf("TempDir() failed: %v", err)
 	}
-	path, perm := filepath.Join(root, VersionFile), os.FileMode(0600)
+	path, perm := filepath.Join(rootDir, VersionFile), os.FileMode(0600)
 	if err := ioutil.WriteFile(path, []byte(Version), perm); err != nil {
 		vlog.Fatalf("WriteFile(%v, %v, %v) failed: %v", path, Version, perm, err)
 	}
@@ -118,14 +118,14 @@
 	if err != nil {
 		t.Fatalf("NewServer() failed: %v", err)
 	}
-	state, err := NewState(root, depth)
-	if err != nil {
-		t.Fatalf("NewState(%v, %v) failed: %v", root, depth, err)
-	}
 	listener, err := net.Listen("tcp", "127.0.0.1:0")
 	if err != nil {
 		t.Fatal(err)
 	}
+	state, err := NewState(rootDir, listener.Addr().String(), depth)
+	if err != nil {
+		t.Fatalf("NewState(%v, %v) failed: %v", rootDir, listener.Addr().String(), depth, err)
+	}
 	go func() {
 		if err := http.Serve(listener, http.FileServer(NewHTTPRoot(state))); err != nil {
 			vlog.Fatalf("Serve() failed: %v", err)
@@ -148,12 +148,10 @@
 			t.Fatalf("Stop() failed: %v", err)
 		}
 		if err := os.RemoveAll(path); err != nil {
-			t.Fatalf("Remove(%v) failed: %v", path, err)
+			t.Fatalf("RemoveAll(%v) failed: %v", path, err)
 		}
-		// Check that any directories and files that were created to
-		// represent the binary objects have been garbage collected.
-		if err := os.RemoveAll(root); err != nil {
-			t.Fatalf("Remove(%v) failed: %v", root, err)
+		if err := os.RemoveAll(rootDir); err != nil {
+			t.Fatalf("RemoveAll(%v) failed: %v", rootDir, err)
 		}
 	}
 }
diff --git a/services/mgmt/binary/impl/service.go b/services/mgmt/binary/impl/service.go
index e88028d..6249891 100644
--- a/services/mgmt/binary/impl/service.go
+++ b/services/mgmt/binary/impl/service.go
@@ -2,10 +2,11 @@
 // objects identified by object name suffixes using the local file
 // system. Given an object name suffix, the implementation computes an
 // MD5 hash of the suffix and generates the following path in the
-// local filesystem: /<root>/<dir_1>/.../<dir_n>/<hash>. The root and
-// the directory depth are parameters of the implementation. The
-// contents of the directory include the checksum and data for each of
-// the individual parts of the binary, and the name of the object:
+// local filesystem: /<root_dir>/<dir_1>/.../<dir_n>/<hash>. The root
+// directory and the directory depth are parameters of the
+// implementation. The contents of the directory include the checksum
+// and data for each of the individual parts of the binary, and the
+// name of the object:
 //
 // name
 // <part_1>/checksum
@@ -156,9 +157,9 @@
 	}
 	for {
 		// Remove the binary and all directories on the path back to the
-		// root that are left empty after the binary is removed.
+		// root directory that are left empty after the binary is removed.
 		path = filepath.Dir(path)
-		if i.state.root == path {
+		if i.state.rootDir == path {
 			break
 		}
 		if err := os.Remove(path); err != nil {
@@ -204,10 +205,11 @@
 	return nil
 }
 
+// TODO(jsimsa): Design and implement an access control mechanism for
+// the URL-based downloads.
 func (i *binaryService) DownloadURL(ipc.ServerContext) (string, int64, error) {
 	vlog.Infof("%v.DownloadURL()", i.suffix)
-	// TODO(jsimsa): Implement.
-	return "", 0, nil
+	return i.state.rootURL + "/" + i.suffix, 0, nil
 }
 
 func (i *binaryService) Stat(ipc.ServerContext) ([]binary.PartInfo, repository.MediaInfo, error) {
diff --git a/services/mgmt/binary/impl/setup.go b/services/mgmt/binary/impl/setup.go
index 12e43fa..2f96bf5 100644
--- a/services/mgmt/binary/impl/setup.go
+++ b/services/mgmt/binary/impl/setup.go
@@ -10,9 +10,9 @@
 
 const defaultRootPrefix = "veyron_binary_repository"
 
-// SetupRoot sets up the root directory if it doesn't already exist. If an
+// SetupRootDir sets up the root directory if it doesn't already exist. If an
 // empty string is used as root, create a new temporary directory.
-func SetupRoot(root string) (string, error) {
+func SetupRootDir(root string) (string, error) {
 	if root == "" {
 		var err error
 		if root, err = ioutil.TempDir("", defaultRootPrefix); err != nil {
diff --git a/services/mgmt/binary/impl/state.go b/services/mgmt/binary/impl/state.go
index cf6c3b1..757d3a6 100644
--- a/services/mgmt/binary/impl/state.go
+++ b/services/mgmt/binary/impl/state.go
@@ -29,21 +29,24 @@
 	// before its performance degrades allows the binary repository to
 	// store 16B objects.
 	depth int
-	// root identifies the local filesystem directory in which the
+	// rootDir identifies the local filesystem directory in which the
 	// binary repository stores its objects.
-	root string
+	rootDir string
+	// rootURL identifies the root URL of the HTTP server serving
+	// the download URLs.
+	rootURL string
 }
 
 // NewState creates a new state object for the binary service.  This
 // should be passed into both NewDispatcher and NewHTTPRoot.
-func NewState(root string, depth int) (*state, error) {
+func NewState(rootDir, rootURL string, depth int) (*state, error) {
 	if min, max := 0, md5.Size-1; min > depth || depth > max {
 		return nil, fmt.Errorf("Unexpected depth, expected a value between %v and %v, got %v", min, max, depth)
 	}
-	if _, err := os.Stat(root); err != nil {
-		return nil, fmt.Errorf("Stat(%v) failed: %v", root, err)
+	if _, err := os.Stat(rootDir); err != nil {
+		return nil, fmt.Errorf("Stat(%v) failed: %v", rootDir, err)
 	}
-	path := filepath.Join(root, VersionFile)
+	path := filepath.Join(rootDir, VersionFile)
 	output, err := ioutil.ReadFile(path)
 	if err != nil {
 		return nil, fmt.Errorf("ReadFile(%v) failed: %v", path, err)
@@ -52,8 +55,9 @@
 		return nil, fmt.Errorf("Unexpected version: expected %v, got %v", expected, got)
 	}
 	return &state{
-		depth: depth,
-		root:  root,
+		depth:   depth,
+		rootDir: rootDir,
+		rootURL: rootURL,
 	}, nil
 }
 
@@ -66,5 +70,5 @@
 	for j := 0; j < s.depth; j++ {
 		dir = filepath.Join(dir, hash[j*2:(j+1)*2])
 	}
-	return filepath.Join(s.root, dir, hash)
+	return filepath.Join(s.rootDir, dir, hash)
 }
diff --git a/services/mgmt/build/buildd/testdata/integration_test.go b/services/mgmt/build/buildd/testdata/integration_test.go
index 630a22f..cbcb948 100644
--- a/services/mgmt/build/buildd/testdata/integration_test.go
+++ b/services/mgmt/build/buildd/testdata/integration_test.go
@@ -47,7 +47,7 @@
 	modules.DispatchInTest()
 }
 
-func TestBuild(t *testing.T) {
+func TestBuildServerIntegration(t *testing.T) {
 	// Build the required binaries.
 	binDir, cleanup, err := integration.BuildPkgs(binPkgs)
 	if err != nil {
diff --git a/services/mgmt/lib/binary/impl.go b/services/mgmt/lib/binary/impl.go
index b68fe7f..2688fbd 100644
--- a/services/mgmt/lib/binary/impl.go
+++ b/services/mgmt/lib/binary/impl.go
@@ -176,6 +176,17 @@
 	return nil
 }
 
+func DownloadURL(ctx context.T, von string) (string, int64, error) {
+	ctx, cancel := ctx.WithTimeout(time.Minute)
+	defer cancel()
+	url, ttl, err := repository.BinaryClient(von).DownloadURL(ctx)
+	if err != nil {
+		vlog.Errorf("DownloadURL() failed: %v", err)
+		return "", 0, err
+	}
+	return url, ttl, nil
+}
+
 func upload(ctx context.T, r io.ReadSeeker, mediaInfo repository.MediaInfo, von string) error {
 	client := repository.BinaryClient(von)
 	offset, whence := int64(0), 2
diff --git a/services/mgmt/lib/binary/impl_test.go b/services/mgmt/lib/binary/impl_test.go
index ca0a76f..216cfd1 100644
--- a/services/mgmt/lib/binary/impl_test.go
+++ b/services/mgmt/lib/binary/impl_test.go
@@ -37,11 +37,11 @@
 
 func setupRepository(t *testing.T) (string, func()) {
 	// Setup the root of the binary repository.
-	root, err := ioutil.TempDir("", veyronPrefix)
+	rootDir, err := ioutil.TempDir("", veyronPrefix)
 	if err != nil {
 		t.Fatalf("TempDir() failed: %v", err)
 	}
-	path, perm := filepath.Join(root, impl.VersionFile), os.FileMode(0600)
+	path, perm := filepath.Join(rootDir, impl.VersionFile), os.FileMode(0600)
 	if err := ioutil.WriteFile(path, []byte(impl.Version), perm); err != nil {
 		vlog.Fatalf("WriteFile(%v, %v, %v) failed: %v", path, impl.Version, perm, err)
 	}
@@ -51,9 +51,9 @@
 		t.Fatalf("NewServer() failed: %v", err)
 	}
 	depth := 2
-	state, err := impl.NewState(root, depth)
+	state, err := impl.NewState(rootDir, "http://test-root-url", depth)
 	if err != nil {
-		t.Fatalf("NewState(%v, %v) failed: %v", root, depth, err)
+		t.Fatalf("NewState(%v, %v) failed: %v", rootDir, depth, err)
 	}
 	dispatcher := impl.NewDispatcher(state, nil)
 	endpoint, err := server.Listen(profiles.LocalListenSpec)
@@ -71,8 +71,8 @@
 		}
 		// Check that any directories and files that were created to
 		// represent the binary objects have been garbage collected.
-		if err := os.Remove(root); err != nil {
-			t.Fatalf("Remove(%v) failed: %v", root, err)
+		if err := os.RemoveAll(rootDir); err != nil {
+			t.Fatalf("Remove(%v) failed: %v", rootDir, err)
 		}
 		// Shutdown the binary repository server.
 		if err := server.Stop(); err != nil {
@@ -160,3 +160,17 @@
 		t.Errorf("Delete(%v) failed: %v", von, err)
 	}
 }
+
+// TestDownloadURL tests the binary repository client-side library
+// DownloadURL method.
+func TestDownloadURL(t *testing.T) {
+	von, cleanup := setupRepository(t)
+	defer cleanup()
+	url, _, err := DownloadURL(runtime.NewContext(), von)
+	if err != nil {
+		t.Fatalf("DownloadURL(%v) failed: %v", von, err)
+	}
+	if got, want := url, "http://test-root-url/test"; got != want {
+		t.Fatalf("unexpect output: got %v, want %v", got, want)
+	}
+}
diff --git a/services/mgmt/node/impl/impl_test.go b/services/mgmt/node/impl/impl_test.go
index f5dd9e2..eefe69e 100644
--- a/services/mgmt/node/impl/impl_test.go
+++ b/services/mgmt/node/impl/impl_test.go
@@ -745,11 +745,11 @@
 }
 
 func startRealBinaryRepository(t *testing.T) func() {
-	root, err := binaryimpl.SetupRoot("")
+	rootDir, err := binaryimpl.SetupRootDir("")
 	if err != nil {
-		t.Fatalf("binaryimpl.SetupRoot failed: %v", err)
+		t.Fatalf("binaryimpl.SetupRootDir failed: %v", err)
 	}
-	state, err := binaryimpl.NewState(root, 3)
+	state, err := binaryimpl.NewState(rootDir, "", 3)
 	if err != nil {
 		t.Fatalf("binaryimpl.NewState failed: %v", err)
 	}
@@ -774,8 +774,8 @@
 		if err := server.Stop(); err != nil {
 			t.Fatalf("server.Stop failed: %v", err)
 		}
-		if err := os.RemoveAll(root); err != nil {
-			t.Fatalf("os.RemoveAll(%q) failed: %v", root, err)
+		if err := os.RemoveAll(rootDir); err != nil {
+			t.Fatalf("os.RemoveAll(%q) failed: %v", rootDir, err)
 		}
 	}
 }
diff --git a/tools/binary/impl.go b/tools/binary/impl.go
index df34d6c..54d6d25 100644
--- a/tools/binary/impl.go
+++ b/tools/binary/impl.go
@@ -10,7 +10,7 @@
 var cmdDelete = &cmdline.Command{
 	Run:      runDelete,
 	Name:     "delete",
-	Short:    "Delete binary",
+	Short:    "Delete a binary",
 	Long:     "Delete connects to the binary repository and deletes the specified binary",
 	ArgsName: "<von>",
 	ArgsLong: "<von> is the veyron object name of the binary to delete",
@@ -31,7 +31,7 @@
 var cmdDownload = &cmdline.Command{
 	Run:   runDownload,
 	Name:  "download",
-	Short: "Download binary",
+	Short: "Download a binary",
 	Long: `
 Download connects to the binary repository, downloads the specified binary, and
 writes it to a file.
@@ -58,7 +58,7 @@
 var cmdUpload = &cmdline.Command{
 	Run:   runUpload,
 	Name:  "upload",
-	Short: "Upload binary",
+	Short: "Upload a binary",
 	Long: `
 Upload connects to the binary repository and uploads the binary of the specified
 file. When successful, it writes the name of the new binary to stdout.
@@ -83,6 +83,28 @@
 	return nil
 }
 
+var cmdURL = &cmdline.Command{
+	Run:      runURL,
+	Name:     "url",
+	Short:    "Fetch a download URL",
+	Long:     "Connect to the binary repository and fetch the download URL for the given veyron object name.",
+	ArgsName: "<von>",
+	ArgsLong: "<von> is the veyron object name of the binary repository",
+}
+
+func runURL(cmd *cmdline.Command, args []string) error {
+	if expected, got := 1, len(args); expected != got {
+		return cmd.UsageErrorf("rooturl: incorrect number of arguments, expected %d, got %d", expected, got)
+	}
+	von := args[0]
+	url, _, err := binary.DownloadURL(runtime.NewContext(), von)
+	if err != nil {
+		return err
+	}
+	fmt.Fprintf(cmd.Stdout(), "%v\n", url)
+	return nil
+}
+
 func root() *cmdline.Command {
 	return &cmdline.Command{
 		Name:  "binary",
@@ -90,6 +112,6 @@
 		Long: `
 The binary tool facilitates interaction with the veyron binary repository.
 `,
-		Children: []*cmdline.Command{cmdDelete, cmdDownload, cmdUpload},
+		Children: []*cmdline.Command{cmdDelete, cmdDownload, cmdUpload, cmdURL},
 	}
 }
diff --git a/tools/binary/impl_test.go b/tools/binary/impl_test.go
index 865c35d..ba314cc 100644
--- a/tools/binary/impl_test.go
+++ b/tools/binary/impl_test.go
@@ -50,7 +50,10 @@
 
 func (s *server) DownloadURL(ipc.ServerContext) (string, int64, error) {
 	vlog.Infof("DownloadURL() was called. suffix=%v", s.suffix)
-	return "", 0, nil
+	if s.suffix != "" {
+		return "", 0, fmt.Errorf("non-empty suffix: %v", s.suffix)
+	}
+	return "test-download-url", 0, nil
 }
 
 func (s *server) Stat(ipc.ServerContext) ([]binary.PartInfo, repository.MediaInfo, error) {
@@ -119,19 +122,20 @@
 		return
 	}
 	defer stopServer(t, server)
+
 	// Setup the command-line.
 	cmd := root()
-	var stdout, stderr bytes.Buffer
-	cmd.Init(nil, &stdout, &stderr)
+	var out bytes.Buffer
+	cmd.Init(nil, &out, &out)
 
 	// Test the 'delete' command.
 	if err := cmd.Execute([]string{"delete", naming.JoinAddressName(endpoint.String(), "exists")}); err != nil {
-		t.Fatalf("%v", err)
+		t.Fatalf("%v failed: %v\n%v", "delete", err, out.String())
 	}
-	if expected, got := "Binary deleted successfully", strings.TrimSpace(stdout.String()); got != expected {
+	if expected, got := "Binary deleted successfully", strings.TrimSpace(out.String()); got != expected {
 		t.Errorf("Got %q, expected %q", got, expected)
 	}
-	stdout.Reset()
+	out.Reset()
 
 	// Test the 'download' command.
 	dir, err := ioutil.TempDir("", "binaryimpltest")
@@ -142,9 +146,9 @@
 	file := path.Join(dir, "testfile")
 	defer os.Remove(file)
 	if err := cmd.Execute([]string{"download", naming.JoinAddressName(endpoint.String(), "exists"), file}); err != nil {
-		t.Fatalf("%v", err)
+		t.Fatalf("%v failed: %v\n%v", "download", err, out.String())
 	}
-	if expected, got := "Binary downloaded to file "+file, strings.TrimSpace(stdout.String()); got != expected {
+	if expected, got := "Binary downloaded to file "+file, strings.TrimSpace(out.String()); got != expected {
 		t.Errorf("Got %q, expected %q", got, expected)
 	}
 	buf, err := ioutil.ReadFile(file)
@@ -154,10 +158,19 @@
 	if expected := "HelloWorld"; string(buf) != expected {
 		t.Errorf("Got %q, expected %q", string(buf), expected)
 	}
-	stdout.Reset()
+	out.Reset()
 
 	// Test the 'upload' command.
 	if err := cmd.Execute([]string{"upload", naming.JoinAddressName(endpoint.String(), "exists"), file}); err != nil {
-		t.Fatalf("%v", err)
+		t.Fatalf("%v failed: %v\n%v", "upload", err, out.String())
+	}
+	out.Reset()
+
+	// Test the 'url' command.
+	if err := cmd.Execute([]string{"url", naming.JoinAddressName(endpoint.String(), "")}); err != nil {
+		t.Fatalf("%v failed: %v\n%v", "url", err, out.String())
+	}
+	if expected, got := "test-download-url", strings.TrimSpace(out.String()); got != expected {
+		t.Errorf("Got %q, expected %q", got, expected)
 	}
 }