1package engine
2
3import (
4	"os"
5	"testing"
6
7	"golang.org/x/net/context"
8
9	"github.com/keybase/client/go/libkb"
10	"github.com/stretchr/testify/require"
11)
12
13func TestBootstrap(t *testing.T) {
14	tc := SetupEngineTest(t, "login")
15	defer tc.Cleanup()
16
17	u1 := CreateAndSignupFakeUser(tc, "login")
18	Logout(tc)
19	u1.LoginOrBust(tc)
20
21	// do a upak load to make sure it is cached
22	arg := libkb.NewLoadUserByUIDArg(context.TODO(), tc.G, u1.UID())
23	_, _, err := tc.G.GetUPAKLoader().Load(arg)
24	require.NoError(t, err)
25
26	// get the status values
27	uid := tc.G.Env.GetUID()
28	username := tc.G.Env.GetUsername()
29	deviceID := tc.G.Env.GetDeviceID()
30
31	// Simulate restarting the service by wiping out the
32	// passphrase stream cache and cached secret keys
33	clearCaches(tc.G)
34	tc.G.GetUPAKLoader().ClearMemory()
35
36	// set server uri to nonexistent ip so api calls will fail
37	prev := os.Getenv("KEYBASE_SERVER_URI")
38	os.Setenv("KEYBASE_SERVER_URI", "http://127.0.0.127:3333")
39	defer os.Setenv("KEYBASE_SERVER_URI", prev)
40	err = tc.G.ConfigureAPI()
41	require.NoError(t, err)
42	tc.G.ConnectivityMonitor = OfflineConnectivityMonitor{}
43
44	eng := NewLoginOffline(tc.G)
45	m := NewMetaContextForTest(tc)
46	if err := RunEngine2(m, eng); err != nil {
47		t.Fatal(err)
48	}
49
50	beng := NewBootstrap(tc.G)
51	if err := RunEngine2(m, beng); err != nil {
52		t.Fatal(err)
53	}
54	status := beng.Status()
55
56	if !status.Registered {
57		t.Error("registered false")
58	}
59	if !status.LoggedIn {
60		t.Error("not logged in")
61	}
62	if status.Uid.IsNil() {
63		t.Errorf("uid nil")
64	}
65	if !status.Uid.Equal(uid) {
66		t.Errorf("uid: %s, expected %s", status.Uid, uid)
67	}
68	if status.Username == "" {
69		t.Errorf("username empty")
70	}
71	if status.Username != username.String() {
72		t.Errorf("username: %q, expected %q", status.Username, username)
73	}
74	if !status.DeviceID.Eq(deviceID) {
75		t.Errorf("device id: %q, expected %q", status.DeviceID, deviceID)
76	}
77	if status.DeviceName != defaultDeviceName {
78		t.Errorf("device name: %q, expected %q", status.DeviceName, defaultDeviceName)
79	}
80}
81
82func TestBootstrapAfterSignup(t *testing.T) {
83	tc := SetupEngineTest(t, "login")
84	defer tc.Cleanup()
85
86	u1 := CreateAndSignupFakeUser(tc, "login")
87
88	beng := NewBootstrap(tc.G)
89	m := NewMetaContextForTest(tc)
90	if err := RunEngine2(m, beng); err != nil {
91		t.Fatal(err)
92	}
93	status := beng.Status()
94
95	uid := tc.G.Env.GetUID()
96	deviceID := tc.G.Env.GetDeviceID()
97
98	if !status.Registered {
99		t.Error("registered false")
100	}
101	if !status.LoggedIn {
102		t.Error("not logged in")
103	}
104	if status.Uid.IsNil() {
105		t.Errorf("uid nil")
106	}
107	if !status.Uid.Equal(uid) {
108		t.Errorf("uid: %s, expected %s", status.Uid, uid)
109	}
110	if status.Username == "" {
111		t.Errorf("username empty")
112	}
113	if status.Username != u1.Username {
114		t.Errorf("username: %q, expected %q", status.Username, u1.Username)
115	}
116	if !status.DeviceID.Eq(deviceID) {
117		t.Errorf("device id: %q, expected %q", status.DeviceID, deviceID)
118	}
119	if status.DeviceName != defaultDeviceName {
120		t.Errorf("device name: %q, expected %q", status.DeviceName, defaultDeviceName)
121	}
122}
123
124type OfflineConnectivityMonitor struct {
125}
126
127func (s OfflineConnectivityMonitor) IsConnected(ctx context.Context) libkb.ConnectivityMonitorResult {
128	return libkb.ConnectivityMonitorNo
129}
130
131func (s OfflineConnectivityMonitor) CheckReachability(ctx context.Context) error {
132	return nil
133}
134
135var _ libkb.ConnectivityMonitor = OfflineConnectivityMonitor{}
136