github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/pkg/media/audio_test.go (about) 1 /* 2 Copyright 2014 The Camlistore Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package media 18 19 import ( 20 "io" 21 "os" 22 "path/filepath" 23 "testing" 24 "time" 25 ) 26 27 // openFile opens fn, a file within the testdata dir, and returns an FD and the file's size. 28 func openFile(fn string) (*os.File, int64, error) { 29 f, err := os.Open(filepath.Join("testdata", fn)) 30 if err != nil { 31 return nil, 0, err 32 } 33 s, err := f.Stat() 34 if err != nil { 35 f.Close() 36 return nil, 0, err 37 } 38 return f, s.Size(), nil 39 } 40 41 func TestHasID3v1Tag(t *testing.T) { 42 tests := []struct { 43 fn string 44 hasTag bool 45 }{ 46 {"xing_header.mp3", false}, 47 {"id3v1.mp3", true}, 48 } 49 for _, tt := range tests { 50 f, s, err := openFile(tt.fn) 51 if err != nil { 52 t.Fatal(err) 53 } 54 defer f.Close() 55 56 hasTag, err := HasID3v1Tag(io.NewSectionReader(f, 0, s)) 57 if err != nil { 58 t.Fatal(err) 59 } 60 if hasTag != tt.hasTag { 61 t.Errorf("Expected %v for %s but got %v", tt.hasTag, tt.fn, hasTag) 62 } 63 } 64 } 65 66 func TestGetMPEGAudioDuration(t *testing.T) { 67 tests := []struct { 68 fn string 69 d time.Duration 70 }{ 71 {"128_cbr.mp3", time.Duration(1088) * time.Millisecond}, 72 {"xing_header.mp3", time.Duration(1097) * time.Millisecond}, 73 } 74 for _, tt := range tests { 75 f, s, err := openFile(tt.fn) 76 if err != nil { 77 t.Fatal(err) 78 } 79 defer f.Close() 80 81 d, err := GetMPEGAudioDuration(io.NewSectionReader(f, 0, s)) 82 if err != nil { 83 t.Fatal(err) 84 } 85 if d != tt.d { 86 t.Errorf("Expected %d for %s but got %d", tt.d, tt.fn, d) 87 } 88 } 89 }