2015-03-30 00:42:26 +00:00
|
|
|
package token
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2015-05-12 05:22:33 +00:00
|
|
|
"runtime"
|
2015-03-30 00:42:26 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2015-12-14 21:23:04 +00:00
|
|
|
func TestExternalTokenHelperPath(t *testing.T) {
|
|
|
|
cases := map[string]string{}
|
2015-05-12 05:22:33 +00:00
|
|
|
|
|
|
|
unixCases := map[string]string{
|
2015-03-30 17:11:17 +00:00
|
|
|
"/foo": "/foo",
|
2015-05-12 05:22:33 +00:00
|
|
|
}
|
|
|
|
windowsCases := map[string]string{
|
|
|
|
"C:/foo": "C:/foo",
|
|
|
|
`C:\Program Files`: `C:\Program Files`,
|
|
|
|
}
|
|
|
|
|
|
|
|
var runtimeCases map[string]string
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
runtimeCases = windowsCases
|
|
|
|
} else {
|
|
|
|
runtimeCases = unixCases
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range runtimeCases {
|
|
|
|
cases[k] = v
|
2015-03-30 17:11:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range cases {
|
2015-12-14 21:23:04 +00:00
|
|
|
actual, err := ExternalTokenHelperPath(k)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error getting external helper path: %v", err)
|
|
|
|
}
|
2015-03-30 17:11:17 +00:00
|
|
|
if actual != v {
|
|
|
|
t.Fatalf(
|
|
|
|
"input: %s, expected: %s, got: %s",
|
|
|
|
k, v, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-14 21:23:04 +00:00
|
|
|
func TestExternalTokenHelper(t *testing.T) {
|
|
|
|
Test(t, testExternalTokenHelper(t))
|
2015-03-30 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
2015-12-14 21:23:04 +00:00
|
|
|
func testExternalTokenHelper(t *testing.T) *ExternalTokenHelper {
|
|
|
|
return &ExternalTokenHelper{BinaryPath: helperPath("helper"), Env: helperEnv()}
|
2015-03-30 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func helperPath(s ...string) string {
|
2015-12-14 21:23:04 +00:00
|
|
|
cs := []string{"-test.run=TestExternalTokenHelperProcess", "--"}
|
2015-05-12 05:22:33 +00:00
|
|
|
cs = append(cs, s...)
|
|
|
|
return fmt.Sprintf(
|
|
|
|
"%s %s",
|
|
|
|
os.Args[0],
|
|
|
|
strings.Join(cs, " "))
|
|
|
|
}
|
|
|
|
|
|
|
|
func helperEnv() []string {
|
|
|
|
var env []string
|
|
|
|
|
2015-03-30 00:42:26 +00:00
|
|
|
tf, err := ioutil.TempFile("", "vault")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
tf.Close()
|
|
|
|
|
2015-05-12 05:22:33 +00:00
|
|
|
env = append(env, "GO_HELPER_PATH="+tf.Name(), "GO_WANT_HELPER_PROCESS=1")
|
|
|
|
return env
|
2015-03-30 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is not a real test. This is just a helper process kicked off by tests.
|
2015-12-14 21:23:04 +00:00
|
|
|
func TestExternalTokenHelperProcess(*testing.T) {
|
2015-03-30 00:42:26 +00:00
|
|
|
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer os.Exit(0)
|
|
|
|
|
|
|
|
args := os.Args
|
|
|
|
for len(args) > 0 {
|
|
|
|
if args[0] == "--" {
|
|
|
|
args = args[1:]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
args = args[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
fmt.Fprintf(os.Stderr, "No command\n")
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd, args := args[0], args[1:]
|
|
|
|
switch cmd {
|
|
|
|
case "helper":
|
|
|
|
path := os.Getenv("GO_HELPER_PATH")
|
|
|
|
|
|
|
|
switch args[0] {
|
|
|
|
case "erase":
|
|
|
|
os.Remove(path)
|
|
|
|
case "get":
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Err: %s\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
io.Copy(os.Stdout, f)
|
|
|
|
case "store":
|
|
|
|
f, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Err: %s\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
io.Copy(f, os.Stdin)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
fmt.Fprintf(os.Stderr, "Unknown command: %q\n", cmd)
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
}
|