forked from TheThingsNetwork/lorawan-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheui_test.go
More file actions
120 lines (94 loc) · 3.25 KB
/
eui_test.go
File metadata and controls
120 lines (94 loc) · 3.25 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
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package types_test
import (
"encoding/json"
"testing"
"github.com/smartystreets/assertions"
"go.thethings.network/lorawan-stack/v3/pkg/config"
. "go.thethings.network/lorawan-stack/v3/pkg/types"
"go.thethings.network/lorawan-stack/v3/pkg/util/test/assertions/should"
)
var _ config.Configurable = &EUI64Prefix{}
var _ config.Stringer = EUI64Prefix{}
func TestEUI64(t *testing.T) {
a := assertions.New(t)
eui := EUI64{0x26, 0x12, 0x34, 0x56, 0x42, 0x42, 0x42, 0x42}
prefix := EUI64Prefix{EUI64{0x26}, 7}
a.So(prefix.Matches(eui), should.BeTrue)
// Prefix list
{
addr := EUI64{1, 2, 3, 4}
a.So(addr.HasPrefix(EUI64Prefix{EUI64{0, 0, 0, 0}, 0}), should.BeTrue)
a.So(addr.HasPrefix(EUI64Prefix{EUI64{1, 2, 3, 0}, 24}), should.BeTrue)
a.So(addr.HasPrefix(EUI64Prefix{EUI64{2, 2, 3, 4}, 31}), should.BeFalse)
a.So(addr.HasPrefix(EUI64Prefix{EUI64{1, 1, 3, 4}, 31}), should.BeFalse)
a.So(addr.HasPrefix(EUI64Prefix{EUI64{1, 1, 1, 1}, 15}), should.BeFalse)
}
t.Run("JSON", func(t *testing.T) {
a := assertions.New(t)
const encodedEUI = `"2612345642424242"`
b, err := json.Marshal(eui)
if a.So(err, should.BeNil) {
a.So(string(b), should.Equal, encodedEUI)
}
var decodedEUI EUI64
err = json.Unmarshal([]byte(encodedEUI), &decodedEUI)
if a.So(err, should.BeNil) {
a.So(decodedEUI, should.Equal, eui)
}
const encodedPrefix = `"2600000000000000/7"`
b, err = json.Marshal(prefix)
if a.So(err, should.BeNil) {
a.So(string(b), should.Equal, encodedPrefix)
}
var decodedPrefix EUI64Prefix
err = json.Unmarshal([]byte(encodedPrefix), &decodedPrefix)
if a.So(err, should.BeNil) {
a.So(decodedPrefix, should.Equal, prefix)
}
})
t.Run("Text", func(t *testing.T) {
a := assertions.New(t)
const encodedEUI = `2612345642424242`
b, err := eui.MarshalText()
if a.So(err, should.BeNil) {
a.So(string(b), should.Equal, encodedEUI)
}
var decodedEUI EUI64
err = decodedEUI.UnmarshalText([]byte(encodedEUI))
if a.So(err, should.BeNil) {
a.So(decodedEUI, should.Equal, eui)
}
const encodedPrefix = `2600000000000000/7`
b, err = prefix.MarshalText()
if a.So(err, should.BeNil) {
a.So(string(b), should.Equal, encodedPrefix)
}
var decodedPrefix EUI64Prefix
err = decodedPrefix.UnmarshalText([]byte(encodedPrefix))
if a.So(err, should.BeNil) {
a.So(decodedPrefix, should.Equal, prefix)
}
})
t.Run("Number", func(t *testing.T) {
a := assertions.New(t)
const encodedEUI uint64 = 2743312668105523778
n := eui.MarshalNumber()
a.So(n, should.Resemble, encodedEUI)
var decodedEUI EUI64
decodedEUI.UnmarshalNumber(encodedEUI)
a.So(decodedEUI, should.Equal, eui)
})
}