1// Copyright 2015 Keybase, Inc. All rights reserved. Use of
2// this source code is governed by the included BSD license.
3
4// +build !windows
5
6package libkb
7
8import (
9	"os"
10	"path"
11	"strings"
12	"testing"
13
14	"github.com/stretchr/testify/require"
15)
16
17func TestPosix(t *testing.T) {
18	hf := NewHomeFinder("tester", nil, nil, nil, "posix", func() RunMode { return ProductionRunMode },
19		makeLogGetter(t), nil)
20	d := hf.CacheDir()
21	if !strings.Contains(d, ".cache/tester") {
22		t.Errorf("Bad Cache dir: %s", d)
23	}
24	d = hf.DataDir()
25	if !strings.Contains(d, ".local/share/tester") {
26		t.Errorf("Bad Data dir: %s", d)
27	}
28	d = hf.ConfigDir()
29	if !strings.Contains(d, ".config/tester") {
30		t.Errorf("Bad Config dir: %s", d)
31	}
32}
33
34func TestDarwinHomeFinder(t *testing.T) {
35	hf := NewHomeFinder("keybase", nil, nil, nil, "darwin", func() RunMode { return ProductionRunMode }, makeLogGetter(t), nil)
36	d := hf.ConfigDir()
37	if !strings.HasSuffix(d, "Library/Application Support/Keybase") {
38		t.Errorf("Bad config dir: %s", d)
39	}
40	d = hf.CacheDir()
41	if !strings.HasSuffix(d, "Library/Caches/Keybase") {
42		t.Errorf("Bad cache dir: %s", d)
43	}
44	hfInt := NewHomeFinder("keybase", func() string { return "home" }, nil, func() string { return "mobilehome" },
45		"darwin", func() RunMode { return ProductionRunMode }, makeLogGetter(t), nil)
46	hfDarwin := hfInt.(Darwin)
47	hfDarwin.forceIOS = true
48	hf = hfDarwin
49	d = hf.ConfigDir()
50	require.True(t, strings.HasSuffix(d, "Library/Application Support/Keybase"))
51	require.True(t, strings.HasPrefix(d, "mobilehome"))
52	d = hf.DataDir()
53	require.True(t, strings.HasSuffix(d, "Library/Application Support/Keybase"))
54	require.False(t, strings.HasPrefix(d, "mobilehome"))
55	require.True(t, strings.HasPrefix(d, "home"))
56
57}
58
59func TestDarwinHomeFinderInDev(t *testing.T) {
60	devHomeFinder := NewHomeFinder("keybase", nil, nil, nil, "darwin", func() RunMode { return DevelRunMode }, makeLogGetter(t), nil)
61	configDir := devHomeFinder.ConfigDir()
62	if !strings.HasSuffix(configDir, "Library/Application Support/KeybaseDevel") {
63		t.Errorf("Bad config dir: %s", configDir)
64	}
65	cacheDir := devHomeFinder.CacheDir()
66	if !strings.HasSuffix(cacheDir, "Library/Caches/KeybaseDevel") {
67		t.Errorf("Bad cache dir: %s", cacheDir)
68	}
69}
70
71func TestPosixRuntimeDir(t *testing.T) {
72	var cmdHome string
73	env := make(map[string]string)
74	ge := func(s string) string { return env[s] }
75	hf := NewHomeFinder("tester", func() string { return cmdHome }, nil, nil, "posix", func() RunMode { return ProductionRunMode }, makeLogGetter(t), ge)
76
77	origHomeEnv := os.Getenv("HOME")
78
79	// Custom env, custom cmd, XDG set
80	cmdHome = "/footown"
81	env["HOME"] = "/yoyo"
82	env["XDG_RUNTIME_DIR"] = "/barland"
83	require.Equal(t, "/footown/.config/tester", hf.RuntimeDir(), "expect custom cmd to win")
84
85	// Custom env, no cmd, XDG set
86	cmdHome = ""
87	env["HOME"] = "/yoyo"
88	env["XDG_RUNTIME_DIR"] = "/barland"
89	require.Equal(t, "/yoyo/.config/tester", hf.RuntimeDir(), "expect custom env to win")
90
91	// Standard env, no cmd, XDG set
92	cmdHome = ""
93	env["HOME"] = origHomeEnv
94	env["XDG_RUNTIME_DIR"] = "/barland"
95	require.Equal(t, "/barland/tester", hf.RuntimeDir(), "expect xdg to win")
96
97	// Standard env, no cmd, XDG unset
98	cmdHome = ""
99	env["HOME"] = origHomeEnv
100	delete(env, "XDG_RUNTIME_DIR")
101	require.Equal(t, path.Join(origHomeEnv, ".config", "tester"), hf.RuntimeDir(), "expect home to win")
102}
103