-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokens_test.go
More file actions
111 lines (97 loc) · 3.36 KB
/
Copy pathtokens_test.go
File metadata and controls
111 lines (97 loc) · 3.36 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
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2026 Hesham Karm
package slacktokens
import (
"path/filepath"
"testing"
"github.com/syndtr/goleveldb/leveldb"
)
// stageLevelDB writes a temporary Chromium-style LevelDB with one entry whose
// key contains "localConfig_v2" and value is the supplied bytes. Returns the
// LevelDB directory path.
func stageLevelDB(t *testing.T, value []byte) string {
t.Helper()
dir := filepath.Join(t.TempDir(), "leveldb")
stageLevelDBAt(t, dir, value)
return dir
}
func stageLevelDBAt(t *testing.T, dir string, value []byte) {
t.Helper()
db, err := leveldb.OpenFile(dir, nil)
if err != nil {
t.Fatalf("open: %v", err)
}
// Mirror Chromium's localStorage map-key shape:
// '_' + origin + '\x00' + format-byte + key-name
key := append([]byte("_https://app.slack.com\x00\x01"), []byte(localConfigKey)...)
if err := db.Put(key, value, nil); err != nil {
t.Fatalf("put: %v", err)
}
if err := db.Close(); err != nil {
t.Fatalf("close: %v", err)
}
}
func TestReadTokensFrom_TwoTeams(t *testing.T) {
json := `{"teams":{"T1":{"url":"https://a.slack.com","token":"xoxc-1","name":"A"},` +
`"T2":{"url":"https://b.slack.com","token":"xoxc-2","name":"B"}}}`
// Prefix with 0x01 (Latin-1 / ASCII) like Chromium does.
dir := stageLevelDB(t, append([]byte{0x01}, []byte(json)...))
got, err := readTokensFrom(dir)
if err != nil {
t.Fatalf("readTokensFrom: %v", err)
}
if len(got) != 2 {
t.Fatalf("want 2 teams, got %d: %#v", len(got), got)
}
if got["https://a.slack.com"].Token != "xoxc-1" {
t.Fatalf("team A token wrong: %#v", got["https://a.slack.com"])
}
if got["https://b.slack.com"].Name != "B" {
t.Fatalf("team B name wrong: %#v", got["https://b.slack.com"])
}
}
func TestReadTokensFrom_MissingLocalConfig(t *testing.T) {
dir := filepath.Join(t.TempDir(), "leveldb")
db, err := leveldb.OpenFile(dir, nil)
if err != nil {
t.Fatalf("open: %v", err)
}
if err := db.Put([]byte("some-other-key"), []byte("v"), nil); err != nil {
t.Fatalf("put: %v", err)
}
db.Close()
if _, err := readTokensFrom(dir); err == nil {
t.Fatal("expected ErrLocalConfigMissing")
}
}
func TestReadTokensFrom_BadJSON(t *testing.T) {
dir := stageLevelDB(t, []byte("\x01not json"))
if _, err := readTokensFrom(dir); err == nil {
t.Fatal("expected parse error")
}
}
// Simulates Slack holding the LevelDB lock: a writer keeps the store open
// while readTokensFrom is invoked. The transparent snapshot fallback should
// kick in and return the staged data.
func TestReadTokensFrom_SnapshotFallbackWhenLocked(t *testing.T) {
json := `{"teams":{"T1":{"url":"https://a.slack.com","token":"xoxc-1","name":"A"}}}`
dir := stageLevelDB(t, append([]byte{0x01}, []byte(json)...))
// Reopen as writer to acquire the on-disk lock, mirroring a running Slack.
holder, err := leveldb.OpenFile(dir, nil)
if err != nil {
t.Fatalf("acquire lock: %v", err)
}
defer func() { _ = holder.Close() }()
// Direct open should fail with ErrLocalStorageLocked.
if _, err := openAndExtractTokens(dir); err == nil {
t.Fatal("expected lock error from direct open while writer holds DB")
}
// readTokensFrom should transparently snapshot and succeed.
got, err := readTokensFrom(dir)
if err != nil {
t.Fatalf("readTokensFrom with locked DB: %v", err)
}
if got["https://a.slack.com"].Token != "xoxc-1" {
t.Fatalf("snapshot read returned unexpected data: %#v", got)
}
}