-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsplitsms_test.go
More file actions
288 lines (221 loc) · 12.2 KB
/
splitsms_test.go
File metadata and controls
288 lines (221 loc) · 12.2 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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package splitsms
import (
"sync"
"testing"
)
// Unicode charset detected
func TestIsGSM7(t *testing.T) {
sms := "\n\f\r !\\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_abcdefghijklmnopqrstuvwxyz{|}~¡£¤¥§¿ÄÅÆÇÉÑÖØÜßàäåæèéìñòöøùüΓΔΘΛΞΠΣΦΨΩ€\f[\\]^{|}~€ê"
if IsGSM7(sms) {
t.Error("Charset SMS is Unicode")
}
}
// GSM charset detected
func TestIsUnicode(t *testing.T) {
sms := "\n\f\r !\\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_abcdefghijklmnopqrstuvwxyz{|}~¡£¤¥§¿ÄÅÆÇÉÑÖØÜßàäåæèéìñòöøùüΓΔΘΛΞΠΣΦΨΩ€\f[\\]^{|}~€"
if IsUnicode(sms) {
t.Error("Charset SMS is GSM 7")
}
}
// Split for GSM 7
func TestSplitGSM(t *testing.T) {
// 160 characters , 0 remaining
msg := Message{FullContent: "----------------------------------------------------------------------------------------------------------------------------------------------------------------"}
split, _ := msg.Split()
if split.Length != 160 || split.RemainingChars != 0 {
t.Errorf("This message contain %d characters and %d characters remaining", split.Length, split.RemainingChars)
}
// 161 characters and 145 characters remaining on second SMS - UDH 6 bytes
msg = Message{FullContent: "-----------------------------------------------------------------------------------------------------------------------------------------------------------------"}
split, _ = msg.Split()
if split.Length != 161 || split.RemainingChars != 145 {
t.Errorf("This message contain %d characters and %d characters remaining on second SMS", split.Length, split.RemainingChars)
}
// 306 chars on 2 SMS - UDH 6 bytes
msg = Message{FullContent: "------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------"}
split, _ = msg.Split()
if split.Length != 306 || split.CountParts != 2 {
t.Errorf("This message contain %d characters on %d SMS", split.Length, split.CountParts)
}
// 159 characters with one character one 2 bytes (160 bytes) - UDH 6 bytes
msg = Message{FullContent: "--------------------------------------------------------------------------------------------------------------------------------------------------------€------"}
split, _ = msg.Split()
if split.Length != 159 || split.Bytes != 160 {
t.Errorf("This message contain %d characters on %d bytes", split.Length, split.Bytes)
}
// 170 characters with one character one 2 bytes - position 153 - UDH 6 bytes
msg = Message{FullContent: "--------------------------------------------------------------------------------------------------------------------------------------------------------€-----------------"}
split, _ = msg.Split()
if split.Parts[0].Length != 152 || split.Length != 170 {
t.Errorf("This message contain %d characters on %d bytes", split.Parts[0].Length, split.Length)
}
}
// Split for Unicode
func TestSplitUnicode(t *testing.T) {
// 70 characters , 0 remaining
msg := Message{FullContent: "°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°"}
split, _ := msg.Split()
if split.Length != 70 || split.RemainingChars != 0 {
t.Errorf("This message contain %d characters and %d characters remaining", split.Length, split.RemainingChars)
}
// 71 characters and 63 characters remaining on second SMS - UDH 6 bytes
msg = Message{FullContent: "°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°"}
split, _ = msg.Split()
if split.Length != 71 || split.RemainingChars != 63 {
t.Errorf("This message contain %d characters and %d characters remaining on second SMS", split.Length, split.RemainingChars)
}
// 60 characters splited on 2 SMS - UDH 6 bytes
msg = Message{FullContent: "°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°"}
split, _ = msg.Split()
if split.Length != 134 || split.CountParts != 2 {
t.Errorf("This message contain %d characters on %d parts", split.Length, split.CountParts)
}
// 69 characters with one character one 4 bytes (140 bytes) - UDH 6 bytes
msg = Message{FullContent: "°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°🐿°°"}
split, _ = msg.Split()
if split.Length != 69 || split.Bytes != 140 {
t.Errorf("This message contain %d characters on %d bytes", split.Length, split.Bytes)
}
// 70 characters with one character one 4 bytes - position 67 - UDH 6 bytes
msg = Message{FullContent: "°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°🐿°°°"}
split, _ = msg.Split()
if split.Parts[0].Length != 66 || split.Length != 70 {
t.Errorf("This message contain %d characters on %d bytes", split.Parts[0].Length, split.Length)
}
}
func TestForcesCharset(t *testing.T) {
// 70 GSM characters forced in Unicode , 0 remaining
msg := Message{FullContent: "---------------------------------------------------------------------€", Charset: "Unicode"}
split, _ := msg.Split()
if split.Length != 70 || split.RemainingChars != 0 || split.Charset != "Unicode" {
t.Errorf("This message contain %d characters and %d characters remaining, charset : %s", split.Length, split.RemainingChars, split.Charset)
}
// 160 Unicode characters forced in GSM 7 , 0 remaining
msg = Message{FullContent: "°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°", Charset: "GSM"}
split, _ = msg.Split()
if split.Length != 160 || split.RemainingChars != 0 || split.Charset != "GSM" {
t.Errorf("This message contain %d characters and %d characters remaining, charset : %s", split.Length, split.RemainingChars, split.Charset)
}
// Charset not supported
msg = Message{FullContent: "°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°", Charset: "XXXX"}
_, err := msg.Split()
if err == nil {
t.Error("Charset not supported : accepted GSM or Unicode")
}
}
func TestUDH7(t *testing.T) {
// 306 chars on 3 SMS - UDH 7 bytes
msg := Message{FullContent: "------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------", UDH: 7}
split, _ := msg.Split()
if split.Length != 306 || split.CountParts != 3 {
t.Errorf("This message contain %d characters splited on %d SMS", split.Length, split.CountParts)
}
// 134 characters in Unicode splited on 3 SMS - UDH 7 bytes
msg = Message{FullContent: "°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°", UDH: 7}
split, _ = msg.Split()
if split.Length != 134 || split.CountParts != 3 {
t.Errorf("This message contain %d characters on %d parts", split.Length, split.CountParts)
}
}
// TestConcurrentSplitDifferentUDH tests thread safety with different UDH values
func TestConcurrentSplitDifferentUDH(t *testing.T) {
const goroutines = 100
var wg sync.WaitGroup
wg.Add(goroutines)
errors := make(chan error, goroutines)
for i := 0; i < goroutines; i++ {
go func(idx int) {
defer wg.Done()
var msg Message
var expectedParts int
if idx%2 == 0 {
// UDH 6 bytes: 306 chars = 2 SMS
msg = Message{FullContent: "------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------", UDH: 6}
expectedParts = 2
} else {
// UDH 7 bytes: 306 chars = 3 SMS
msg = Message{FullContent: "------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------", UDH: 7}
expectedParts = 3
}
split, err := msg.Split()
if err != nil {
errors <- err
return
}
if split.CountParts != expectedParts {
t.Errorf("UDH=%d: expected %d parts, got %d", msg.UDH, expectedParts, split.CountParts)
}
}(i)
}
wg.Wait()
close(errors)
for err := range errors {
t.Errorf("Concurrent split error: %v", err)
}
}
// TestConcurrentSplitMixedCharsets tests thread safety with GSM and Unicode in parallel
func TestConcurrentSplitMixedCharsets(t *testing.T) {
const goroutines = 100
var wg sync.WaitGroup
wg.Add(goroutines)
for i := 0; i < goroutines; i++ {
go func(idx int) {
defer wg.Done()
var msg Message
var expectedCharset string
if idx%2 == 0 {
// GSM message
msg = Message{FullContent: "----------------------------------------------------------------------------------------------------------------------------------------------------------------"}
expectedCharset = "GSM"
} else {
// Unicode message
msg = Message{FullContent: "°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°"}
expectedCharset = "Unicode"
}
split, err := msg.Split()
if err != nil {
t.Errorf("Split error: %v", err)
return
}
if split.Charset != expectedCharset {
t.Errorf("Expected charset %s, got %s", expectedCharset, split.Charset)
}
}(i)
}
wg.Wait()
}
// TestConcurrentSplitStress stress test with many concurrent calls
func TestConcurrentSplitStress(t *testing.T) {
const goroutines = 500
var wg sync.WaitGroup
wg.Add(goroutines)
messages := []struct {
msg Message
expectedLength int
expectedParts int
}{
{Message{FullContent: "Hello World"}, 11, 1},
{Message{FullContent: "----------------------------------------------------------------------------------------------------------------------------------------------------------------"}, 160, 1},
{Message{FullContent: "-----------------------------------------------------------------------------------------------------------------------------------------------------------------"}, 161, 2},
{Message{FullContent: "°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°"}, 70, 1},
{Message{FullContent: "°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°"}, 71, 2},
}
for i := 0; i < goroutines; i++ {
go func(idx int) {
defer wg.Done()
tc := messages[idx%len(messages)]
split, err := tc.msg.Split()
if err != nil {
t.Errorf("Split error: %v", err)
return
}
if split.Length != tc.expectedLength {
t.Errorf("Expected length %d, got %d", tc.expectedLength, split.Length)
}
if split.CountParts != tc.expectedParts {
t.Errorf("Expected %d parts, got %d", tc.expectedParts, split.CountParts)
}
}(i)
}
wg.Wait()
}