1// Copyright 2015 Keybase, Inc. All rights reserved. Use of
2// this source code is governed by the included BSD license.
3
4package engine
5
6import (
7	"testing"
8
9	"github.com/keybase/client/go/libkb"
10)
11
12func assertStreamCache(tc libkb.TestContext, valid bool) bool {
13	var ppsValid bool
14	if ps := tc.G.ActiveDevice.PassphraseStreamCache(); ps != nil {
15		ppsValid = ps.Valid()
16	}
17	return valid == ppsValid
18}
19
20func TestUnlock(t *testing.T) {
21	tc := SetupEngineTest(t, "template")
22	defer tc.Cleanup()
23
24	fu := CreateAndSignupFakeUser(tc, "login")
25
26	if !assertStreamCache(tc, true) {
27		t.Fatal("expected valid stream cache after sign up")
28	}
29
30	uis := libkb.UIs{
31		LogUI:    tc.G.UI.GetLogUI(),
32		LoginUI:  &libkb.TestLoginUI{},
33		SecretUI: fu.NewSecretUI(),
34	}
35
36	tc.G.ActiveDevice.ClearPassphraseStreamCache()
37
38	if !assertStreamCache(tc, false) {
39		t.Fatal("expected invalid stream cache after clear")
40	}
41
42	eng := NewUnlock(tc.G)
43	m := NewMetaContextForTest(tc).WithUIs(uis)
44	if err := RunEngine2(m, eng); err != nil {
45		t.Fatal(err)
46	}
47
48	if !assertStreamCache(tc, true) {
49		t.Fatal("expected valid stream cache after unlock")
50	}
51}
52
53func TestUnlockNoop(t *testing.T) {
54	tc := SetupEngineTest(t, "template")
55	defer tc.Cleanup()
56
57	fu := CreateAndSignupFakeUser(tc, "login")
58
59	if !assertStreamCache(tc, true) {
60		t.Fatal("expected valid stream cache after sign up")
61	}
62
63	uis := libkb.UIs{
64		LogUI:    tc.G.UI.GetLogUI(),
65		LoginUI:  &libkb.TestLoginUI{},
66		SecretUI: fu.NewSecretUI(),
67	}
68
69	eng := NewUnlock(tc.G)
70	m := NewMetaContextForTest(tc).WithUIs(uis)
71	if err := RunEngine2(m, eng); err != nil {
72		t.Fatal(err)
73	}
74
75	if !assertStreamCache(tc, true) {
76		t.Fatal("expected valid stream cache after unlock")
77	}
78}
79
80func TestUnlockWithPassphrase(t *testing.T) {
81	tc := SetupEngineTest(t, "template")
82	defer tc.Cleanup()
83
84	fu := CreateAndSignupFakeUser(tc, "login")
85
86	if !assertStreamCache(tc, true) {
87		t.Fatal("expected valid stream cache after sign up")
88	}
89
90	uis := libkb.UIs{
91		LogUI:   tc.G.UI.GetLogUI(),
92		LoginUI: &libkb.TestLoginUI{},
93		// No SecretUI here!
94	}
95
96	tc.G.ActiveDevice.ClearPassphraseStreamCache()
97
98	if !assertStreamCache(tc, false) {
99		t.Fatal("expected invalid stream cache after clear")
100	}
101
102	eng := NewUnlockWithPassphrase(tc.G, fu.Passphrase)
103	m := NewMetaContextForTest(tc).WithUIs(uis)
104	if err := RunEngine2(m, eng); err != nil {
105		t.Fatal(err)
106	}
107
108	if !assertStreamCache(tc, true) {
109		t.Fatal("expected valid stream cache after unlock")
110	}
111}
112