Skip to content

Commit 3a9548c

Browse files
committed
Define Protocol enumeration
Electrum client supports TCP and SSL connections to the Electrum server. This enumeration will be used to handle the protocol types.
1 parent 0d471d2 commit 3a9548c

2 files changed

Lines changed: 107 additions & 0 deletions

File tree

pkg/bitcoin/electrum/protocol.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package electrum
2+
3+
import "strings"
4+
5+
// Protocol is a type used for electrum protocol enumeration.
6+
type Protocol int
7+
8+
// Electrum protocols enumeration.
9+
const (
10+
Unknown Protocol = iota
11+
TCP
12+
SSL
13+
)
14+
15+
var (
16+
protocolMap = map[string]Protocol{
17+
"tcp": TCP,
18+
"ssl": SSL,
19+
}
20+
)
21+
22+
func (n Protocol) String() string {
23+
return []string{"unknown", "tcp", "ssl"}[n]
24+
}
25+
26+
// ParseProtocol parses string
27+
func ParseProtocol(str string) (Protocol, bool) {
28+
c, ok := protocolMap[strings.ToLower(str)]
29+
return c, ok
30+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package electrum
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestParseProtocol(t *testing.T) {
8+
tests := map[string]struct {
9+
value string
10+
expectedResult Protocol
11+
expectedOk bool
12+
}{
13+
"empty": {
14+
value: "",
15+
expectedResult: Unknown,
16+
expectedOk: false,
17+
},
18+
"lower case: tcp": {
19+
value: "tcp",
20+
expectedResult: TCP,
21+
expectedOk: true,
22+
},
23+
"upper case: TCP": {
24+
value: "TCP",
25+
expectedResult: TCP,
26+
expectedOk: true,
27+
},
28+
"mixed case: Tcp": {
29+
value: "Tcp",
30+
expectedResult: TCP,
31+
expectedOk: true,
32+
},
33+
"lower case: ssl": {
34+
value: "ssl",
35+
expectedResult: SSL,
36+
expectedOk: true,
37+
},
38+
"upper case: SSL": {
39+
value: "SSL",
40+
expectedResult: SSL,
41+
expectedOk: true,
42+
},
43+
"mixed case: SsL": {
44+
value: "SsL",
45+
expectedResult: SSL,
46+
expectedOk: true,
47+
},
48+
"invalid: tls": {
49+
value: "tls",
50+
expectedResult: Unknown,
51+
expectedOk: false,
52+
},
53+
}
54+
55+
for testName, test := range tests {
56+
t.Run(testName, func(t *testing.T) {
57+
actualResult, actualOk := ParseProtocol(test.value)
58+
59+
if test.expectedResult != actualResult {
60+
t.Errorf(
61+
"unexpected result\nexpected: [%+v]\nactual: [%+v]",
62+
test.expectedResult,
63+
actualResult,
64+
)
65+
}
66+
67+
if test.expectedOk != actualOk {
68+
t.Errorf(
69+
"unexpected ok\nexpected: [%+v]\nactual: [%+v]",
70+
test.expectedOk,
71+
actualOk,
72+
)
73+
}
74+
75+
})
76+
}
77+
}

0 commit comments

Comments
 (0)