-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapi_wallet_unload_test.go
More file actions
144 lines (123 loc) · 3.75 KB
/
api_wallet_unload_test.go
File metadata and controls
144 lines (123 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"encoding/json"
"net/http"
"path/filepath"
"testing"
"blocknet/wallet"
)
func TestHandleUnloadWallet_NoWallet503(t *testing.T) {
s := NewAPIServer(nil, nil, nil, t.TempDir(), nil)
resp := mustMakeHTTPJSONRequest(
t,
http.HandlerFunc(s.handleUnloadWallet),
http.MethodPost,
"/api/wallet/unload",
nil,
nil,
)
if resp.Code != http.StatusServiceUnavailable {
t.Fatalf("expected 503 with no wallet, got %d: %s", resp.Code, resp.Body.String())
}
var body map[string]string
if err := json.Unmarshal(resp.Body.Bytes(), &body); err != nil {
t.Fatalf("response is not valid JSON: %v", err)
}
if body["error"] != "no wallet loaded" {
t.Fatalf("unexpected error message: %q", body["error"])
}
}
func TestHandleUnloadWallet_Success(t *testing.T) {
chain, _, cleanup := mustCreateTestChain(t)
defer cleanup()
mustAddGenesisBlock(t, chain)
daemon, stopDaemon := mustStartTestDaemon(t, chain)
defer stopDaemon()
walletFile := filepath.Join(t.TempDir(), "wallet.dat")
w, err := wallet.NewWallet(walletFile, []byte("pw"), defaultWalletConfig())
if err != nil {
t.Fatalf("failed to create wallet: %v", err)
}
s := NewAPIServer(daemon, w, nil, t.TempDir(), []byte("pw"))
s.cli = &CLI{walletFile: walletFile}
resp := mustMakeHTTPJSONRequest(
t,
http.HandlerFunc(s.handleUnloadWallet),
http.MethodPost,
"/api/wallet/unload",
nil,
nil,
)
if resp.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", resp.Code, resp.Body.String())
}
var body map[string]bool
if err := json.Unmarshal(resp.Body.Bytes(), &body); err != nil {
t.Fatalf("response is not valid JSON: %v", err)
}
if !body["unloaded"] {
t.Fatalf("expected unloaded=true, got %v", body)
}
// Server state is fully cleared.
s.mu.RLock()
walletNil := s.wallet == nil
scannerNil := s.scanner == nil
locked := s.locked
hashSet := s.passwordHashSet
var zeroHash [32]byte
hash := s.passwordHash
s.mu.RUnlock()
if !walletNil {
t.Error("expected s.wallet to be nil after unload")
}
if !scannerNil {
t.Error("expected s.scanner to be nil after unload")
}
if !locked {
t.Error("expected s.locked to be true after unload")
}
if hashSet {
t.Error("expected s.passwordHashSet to be false after unload")
}
if hash != zeroHash {
t.Error("expected s.passwordHash to be zeroed after unload")
}
// CLI references are also cleared.
s.cli.mu.RLock()
cliWalletNil := s.cli.wallet == nil
cliScannerNil := s.cli.scanner == nil
cliHashSet := s.cli.passwordHashSet
s.cli.mu.RUnlock()
if !cliWalletNil {
t.Error("expected cli.wallet to be nil after unload")
}
if !cliScannerNil {
t.Error("expected cli.scanner to be nil after unload")
}
if cliHashSet {
t.Error("expected cli.passwordHashSet to be false after unload")
}
}
func TestHandleUnloadWallet_SecondCallReturns503(t *testing.T) {
chain, _, cleanup := mustCreateTestChain(t)
defer cleanup()
mustAddGenesisBlock(t, chain)
daemon, stopDaemon := mustStartTestDaemon(t, chain)
defer stopDaemon()
walletFile := filepath.Join(t.TempDir(), "wallet.dat")
w, err := wallet.NewWallet(walletFile, []byte("pw"), defaultWalletConfig())
if err != nil {
t.Fatalf("failed to create wallet: %v", err)
}
s := NewAPIServer(daemon, w, nil, t.TempDir(), []byte("pw"))
s.cli = &CLI{walletFile: walletFile}
handler := http.HandlerFunc(s.handleUnloadWallet)
first := mustMakeHTTPJSONRequest(t, handler, http.MethodPost, "/api/wallet/unload", nil, nil)
if first.Code != http.StatusOK {
t.Fatalf("first unload: expected 200, got %d: %s", first.Code, first.Body.String())
}
second := mustMakeHTTPJSONRequest(t, handler, http.MethodPost, "/api/wallet/unload", nil, nil)
if second.Code != http.StatusServiceUnavailable {
t.Fatalf("second unload: expected 503, got %d: %s", second.Code, second.Body.String())
}
}