This repository has been archived by the owner on Aug 2, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscid_test.go
290 lines (263 loc) · 7.71 KB
/
discid_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
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
289
290
// Copyright (C) 2020-2023 Philipp Wolfer <ph.wolfer@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package discid_test
import (
"fmt"
"log"
"strconv"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"go.uploadedlobster.com/discid"
)
func TestDefaultDevice(t *testing.T) {
device := discid.DefaultDevice()
if device == "" {
t.Errorf("TestDefaultDevice() is empty; expected device name")
}
}
func ExampleDefaultDevice() {
fmt.Printf("Default device: %v\n", discid.DefaultDevice())
}
func TestVersion(t *testing.T) {
version := discid.Version()
if !strings.HasPrefix(version, "libdiscid") {
t.Errorf("Version() = %v; expected starting with \"libdiscid\"", version)
}
}
func ExampleVersion() {
fmt.Printf("Version: %v\n", discid.Version())
}
func TestHasFeature(t *testing.T) {
result := discid.HasFeature(discid.FeatureRead)
if !result {
t.Errorf("HasFeature() = %v; expected true", result)
}
}
func ExampleHasFeature() {
if discid.HasFeature(discid.FeatureIsrc) {
fmt.Println("ISRC support available")
}
}
func TestReadInvalidDevice(t *testing.T) {
_, err := discid.Read("notadevice")
if err == nil {
t.Errorf("Expected error for accessing invalid device")
}
}
func ExampleRead() {
disc, err := discid.Read("") // Read from default device
if err != nil {
log.Fatal(err)
}
defer disc.Close()
fmt.Printf("Disc ID: %v\n", disc.Id())
}
func ExampleReadFeatures() {
// Read TOC and MCN from the disc in /dev/cdrom
disc, err := discid.ReadFeatures("/dev/cdrom", discid.FeatureRead|discid.FeatureMcn)
if err != nil {
log.Fatal(err)
}
defer disc.Close()
fmt.Printf("Disc ID: %v\n", disc.Id())
fmt.Printf("MCN : %v\n", disc.Mcn())
}
func TestPut(t *testing.T) {
assert := assert.New(t)
first := 1
offsets := []int{
206535, 150, 18901, 39738, 59557, 79152, 100126, 124833, 147278, 166336, 182560,
}
disc, err := discid.Put(first, offsets)
if err != nil {
t.Fatal(err)
}
defer disc.Close()
assert.Equal("Wn8eRBtfLDfM0qjYPdxrz.Zjs_U-", disc.Id())
assert.Equal(disc.Id(), fmt.Sprint(disc))
assert.Equal("830abf0a", disc.FreedbId())
assert.Equal(1, disc.FirstTrackNum())
assert.Equal(10, disc.LastTrackNum())
assert.Equal(206535, disc.Sectors())
assert.Equal(
"1 10 206535 150 18901 39738 59557 79152 100126 124833 147278 166336 182560",
disc.TocString())
assert.Equal(
"http://musicbrainz.org/cdtoc/attach?id=Wn8eRBtfLDfM0qjYPdxrz.Zjs_U-&tracks=10&toc=1+10+206535+150+18901+39738+59557+79152+100126+124833+147278+166336+182560",
disc.SubmissionUrl())
for i := disc.FirstTrackNum(); i <= disc.LastTrackNum(); i++ {
track := disc.Track(i)
offset := offsets[track.Number]
next := 0
if track.Number < disc.LastTrackNum() {
next = track.Number + 1
}
length := offsets[next] - offset
assert.Equal(i, track.Number)
assert.Equal(offset, track.Offset)
assert.Equal(length, track.Sectors)
assert.Equal("", track.Isrc)
}
}
func TestPutFirstTrackLargerOne(t *testing.T) {
assert := assert.New(t)
first := 3
offsets := []int{
206535, 150, 18901, 39738, 59557, 79152, 100126, 124833, 147278, 166336, 182560,
}
disc, err := discid.Put(first, offsets[0:])
if err != nil {
t.Fatal(err)
}
defer disc.Close()
assert.Equal("ByBKvJM1hBL7XtvsPyYtIjlX0Bw-", disc.Id())
assert.Equal(3, disc.FirstTrackNum())
assert.Equal(12, disc.LastTrackNum())
assert.Equal(206535, disc.Sectors())
}
func TestPutTooManyOffsets(t *testing.T) {
first := 1
offsets := [101]int{}
disc, err := discid.Put(first, offsets[0:])
assert.Empty(t, disc)
assert.NotEmpty(t, err)
if err.Error() != "Illegal track limits" {
t.Errorf("Expected error \"Illegal track limits\"")
}
}
func TestPutTooManyTracks(t *testing.T) {
// First track number is 82, 19 tracks
// => last track number would be 100, but 99 is max.
first := 82
offsets := [20]int{}
disc, err := discid.Put(first, offsets[0:])
assert.Empty(t, disc)
assert.NotEmpty(t, err)
if err.Error() != "Illegal track limits" {
t.Errorf("Expected error \"Illegal track limits\"")
}
}
func ExamplePut() {
first := 1
offsets := []int{
242457, 150, 44942, 61305, 72755, 96360, 130485, 147315, 164275, 190702, 205412, 220437,
}
disc, err := discid.Put(first, offsets)
if err != nil {
log.Fatal(err)
}
defer disc.Close()
fmt.Println(disc.Id())
// Output: lSOVc5h6IXSuzcamJS1Gp4_tRuA-
}
func ExampleParse() {
toc := "1 11 242457 150 44942 61305 72755 96360 130485 147315 164275 190702 205412 220437"
disc, err := discid.Parse(toc)
if err != nil {
log.Fatal(err)
}
defer disc.Close()
fmt.Println(disc.Id())
// Output: lSOVc5h6IXSuzcamJS1Gp4_tRuA-
}
func TestParseMinimal(t *testing.T) {
assert := assert.New(t)
toc := "1 1 44942 150"
disc, err := discid.Parse(toc)
if err != nil {
t.Fatal(err)
}
defer disc.Close()
assert.Equal("ANJa4DGYN_ktpzOwvVPtcjwP7mE-", disc.Id())
assert.Equal(toc, disc.TocString())
}
func TestParseFirstTrackNotOne(t *testing.T) {
assert := assert.New(t)
toc := "3 12 242457 150 18901 39738 59557 79152 100126 124833 147278 166336 182560"
disc, err := discid.Parse(toc)
if err != nil {
t.Fatal(err)
}
defer disc.Close()
assert.Equal("fC1yNbC5bVjbvphqlAY9JyYoWEY-", disc.Id())
assert.Equal(toc, disc.TocString())
}
func TestParseNaN(t *testing.T) {
toc := "1 2 242457 150 a"
_, err := discid.Parse(toc)
if assert.Error(t, err) {
if err.(*strconv.NumError).Err != strconv.ErrSyntax {
t.Errorf("Expected strconv.ErrSyntax, got \"%v\"", err)
}
}
}
func TestParseInvalidEmpty(t *testing.T) {
toc := ""
_, err := discid.Parse(toc)
if assert.Error(t, err) {
if err.(*strconv.NumError).Err != strconv.ErrSyntax {
t.Errorf("Expected strconv.ErrSyntax, got \"%v\"", err)
}
}
}
func TestParseTooManyOffsets(t *testing.T) {
assert := assert.New(t)
toc := "1 2 242457 150 200 300"
_, err := discid.Parse(toc)
assert.Error(err)
assert.Equal("TOC string contains too many offsets (max. 100)", err.Error())
}
func TestParseTooManyOffsetsTotal(t *testing.T) {
assert := assert.New(t)
indexes := [103]string{"1", "99", "20000"}
for i := 3; i < len(indexes); i++ {
indexes[i] = strconv.Itoa(i * 100)
}
toc := strings.Join(indexes[:], " ")
_, err := discid.Parse(toc)
assert.Error(err)
assert.Equal("TOC string contains too many offsets (max. 100)", err.Error())
}
func TestParseInvalidMissingOffsets(t *testing.T) {
assert := assert.New(t)
toc := "1 2 242457 150"
_, err := discid.Parse(toc)
assert.Error(err)
assert.Equal("Number of offsets 1 does not match track count 2", err.Error())
}
func TestParseInvalidNotEnoughElements(t *testing.T) {
assert := assert.New(t)
toc := "1"
_, err := discid.Parse(toc)
assert.Error(err)
assert.Equal("Invalid TOC string \"1\"", err.Error())
}
func TestTrackOutOfRange(t *testing.T) {
assert := assert.New(t)
first := 1
offsets := []int{
206535, 150, 18901, 39738, 59557, 79152, 100126, 124833, 147278, 166336, 182560,
}
disc, err := discid.Put(first, offsets)
if err != nil {
t.Fatal(err)
}
defer disc.Close()
assert.Panics(func() { disc.Track(disc.FirstTrackNum() - 1) })
assert.NotPanics(func() { disc.Track(disc.FirstTrackNum()) })
assert.NotPanics(func() { disc.Track(disc.LastTrackNum()) })
assert.Panics(func() { disc.Track(disc.LastTrackNum() + 1) })
}