forked from warthog618/sms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimestamp_test.go
195 lines (188 loc) · 4.32 KB
/
timestamp_test.go
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// SPDX-License-Identifier: MIT
//
// Copyright © 2018 Kent Gibson <warthog618@gmail.com>.
package tpdu_test
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/warthog618/sms/encoding/bcd"
"github.com/warthog618/sms/encoding/tpdu"
)
type marshalTimestampPattern struct {
name string
in tpdu.Timestamp
out []byte
err error
}
func TestMarhalBinary(t *testing.T) {
patterns := []marshalTimestampPattern{
{
"19700101",
tpdu.Timestamp{
Time: time.Date(1970, time.January, 1, 1, 2, 3, 0, time.UTC),
},
[]byte{0x07, 0x10, 0x10, 0x10, 0x20, 0x30, 0x00},
nil,
},
{
"19991231",
tpdu.Timestamp{
Time: time.Date(1999, time.December, 31, 23, 59, 59, 0, time.FixedZone("SCTS", -15*60)),
},
[]byte{0x99, 0x21, 0x13, 0x32, 0x95, 0x95, 0x18},
nil,
},
{
"20001231",
tpdu.Timestamp{Time: time.Date(2000, time.December, 31, 23, 59, 59, 0, time.FixedZone("SCTS", 15*60))},
[]byte{0x00, 0x21, 0x13, 0x32, 0x95, 0x95, 0x10},
nil,
},
{
"20170831",
tpdu.Timestamp{
Time: time.Date(2017, time.August, 31, 11, 21, 54, 0, time.FixedZone("any", 8*3600)),
},
[]byte{0x71, 0x80, 0x13, 0x11, 0x12, 0x45, 0x23},
nil,
},
{
"20700101",
tpdu.Timestamp{
Time: time.Date(2070, time.January, 1, 1, 2, 3, 0, time.UTC),
},
[]byte{0x07, 0x10, 0x10, 0x10, 0x20, 0x30, 0x00},
nil,
},
{
"21001231",
tpdu.Timestamp{
Time: time.Date(2100, time.December, 31, 23, 59, 59, 0, time.FixedZone("SCTS", 15*60)),
},
[]byte{0x00, 0x21, 0x13, 0x32, 0x95, 0x95, 0x10},
nil,
},
{
"20701231",
tpdu.Timestamp{
Time: time.Date(2070, time.December, 31, 23, 59, 59, 0, time.FixedZone("SCTS", 24*3600)),
},
nil,
bcd.ErrInvalidInteger(96),
},
// how to trigger invalid integer in date (other than tz)??
}
for _, p := range patterns {
f := func(t *testing.T) {
b, err := p.in.MarshalBinary()
require.Equal(t, p.err, err)
assert.Equal(t, p.out, b)
}
t.Run(p.name, f)
}
}
func TestUnmarhalBinary(t *testing.T) {
patterns := []struct {
name string
in []byte
out tpdu.Timestamp
err error
}{
{
"19700101",
[]byte{0x07, 0x10, 0x10, 0x10, 0x20, 0x30, 0x00},
tpdu.Timestamp{
Time: time.Date(1970, time.January, 1, 1, 2, 3, 0, time.UTC),
},
nil,
},
{
"19991231",
[]byte{0x99, 0x21, 0x13, 0x32, 0x95, 0x95, 0x18},
tpdu.Timestamp{
Time: time.Date(1999, time.December, 31, 23, 59, 59, 0, time.FixedZone("SCTS", -15*60)),
},
nil,
},
{
"20001231",
[]byte{0x00, 0x21, 0x13, 0x32, 0x95, 0x95, 0x10},
tpdu.Timestamp{
Time: time.Date(2000, time.December, 31, 23, 59, 59, 0, time.FixedZone("SCTS", 15*60)),
},
nil,
},
{
"20170831",
[]byte{0x71, 0x80, 0x13, 0x11, 0x12, 0x45, 0x23},
tpdu.Timestamp{
Time: time.Date(2017, time.August, 31, 11, 21, 54, 0, time.FixedZone("SCTS", 8*3600)),
},
nil,
},
{
"short",
[]byte{0x71, 0x80, 0x13, 0x11, 0x12, 0x45},
tpdu.Timestamp{},
tpdu.ErrUnderflow,
},
{
"invalid digit",
[]byte{0xa1, 0x80, 0x13, 0x11, 0x12, 0x45, 0x00},
tpdu.Timestamp{},
bcd.ErrInvalidOctet(0xa1),
},
{
"invalid signed digit",
[]byte{0x71, 0x80, 0x13, 0x11, 0x12, 0x45, 0xa0},
tpdu.Timestamp{},
bcd.ErrInvalidOctet(0xa0),
},
}
for _, p := range patterns {
f := func(t *testing.T) {
s := tpdu.Timestamp{}
err := s.UnmarshalBinary(p.in)
if err != p.err {
t.Fatalf("error unmarshalling %v: %v", p.in, err)
}
if !s.Equal(p.out.Time) {
t.Fatalf("failed to unmarshal %v: expected %v, got %v", p.in, p.out, s)
}
szn, szo := s.Zone()
ozn, ozo := p.out.Zone()
assert.Equal(t, ozn, szn)
assert.Equal(t, ozo, szo)
}
t.Run(p.name, f)
}
}
func TestTimestampString(t *testing.T) {
patterns := []struct {
in time.Time
out string
}{
{
time.Date(2000, 11, 2, 3, 4, 5, 65, time.FixedZone("ABC", 22800)),
"2000-11-02 03:04:05 +0620",
},
{
time.Date(2000, 11, 2, 3, 4, 5, 0, time.FixedZone("ABC", 22800)),
"2000-11-02 03:04:05 +0620",
},
{
time.Date(2000, 11, 2, 3, 4, 5, 0, time.FixedZone("TEST", 0)),
"2000-11-02 03:04:05 +0000",
},
}
for _, p := range patterns {
f := func(t *testing.T) {
out := tpdu.Timestamp{p.in}.String()
assert.Equal(t, p.out, out)
}
t.Run(fmt.Sprintf("%02x", p.in), f)
}
}