dudeldu_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * DudelDu
  3. *
  4. * Copyright 2016 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package main
  11. import (
  12. "bytes"
  13. "flag"
  14. "fmt"
  15. "io/ioutil"
  16. "log"
  17. "os"
  18. "testing"
  19. "devt.de/krotik/common/fileutil"
  20. "devt.de/krotik/common/testutil"
  21. "devt.de/krotik/dudeldu"
  22. "devt.de/krotik/dudeldu/playlist"
  23. )
  24. const testFilePlaylist = `
  25. /*
  26. Test comment
  27. */
  28. {
  29. "/testpath" : [
  30. {
  31. "artist" : "artist1", // 1234
  32. "title" : "test1",
  33. "path" : "playlisttest/test1.mp3"
  34. },
  35. {
  36. "artist" : "artist2",
  37. "title" : "test2",
  38. "path" : "playlisttest/test2.mp4"
  39. },
  40. {
  41. "artist" : "artist3",
  42. "title" : "test3",
  43. "path" : "playlisttest/test3.mp3"
  44. }
  45. ]
  46. }`
  47. const pdir = "playlisttest"
  48. func TestRequestHandlerFilePlaylist(t *testing.T) {
  49. var out bytes.Buffer
  50. // Collect the print output
  51. dudeldu.Print = func(v ...interface{}) {
  52. out.WriteString(fmt.Sprint(v...))
  53. out.WriteString("\n")
  54. }
  55. defer func() {
  56. dudeldu.Print = log.Print
  57. }()
  58. os.Mkdir(pdir, 0770)
  59. defer func() {
  60. os.RemoveAll(pdir)
  61. }()
  62. ioutil.WriteFile(pdir+"/test.dpl", []byte(testFilePlaylist), 0644)
  63. ioutil.WriteFile(pdir+"/test1.mp3", []byte("abcdefgh"), 0644)
  64. ioutil.WriteFile(pdir+"/test2.mp4", []byte("12345"), 0644)
  65. ioutil.WriteFile(pdir+"/test3.mp3", []byte("???!!!&&&$$$"), 0644)
  66. fac, err := playlist.NewFilePlaylistFactory(pdir+"/test.dpl", "")
  67. if err != nil {
  68. t.Error(err)
  69. return
  70. }
  71. drh := dudeldu.NewDefaultRequestHandler(fac, false, false, "")
  72. testConn := &testutil.ErrorTestingConnection{}
  73. dudeldu.MetaDataInterval = 5
  74. playlist.FrameSize = 5
  75. drh.ServeRequest(testConn, "/testpath", true, 2, "")
  76. if testConn.Out.String() != ("ICY 200 OK\r\n" +
  77. "Content-Type: audio/mpeg\r\n" +
  78. "icy-name: /testpath\r\n" +
  79. "icy-metadata: 1\r\n" +
  80. "icy-metaint: 5\r\n" +
  81. "\r\n" +
  82. `cdefg` + string(0x02) + `StreamTitle='test2 - artist2';` + string([]byte{0x0, 0x0}) +
  83. `h1234` + string(0x02) + `StreamTitle='test3 - artist3';` + string([]byte{0x0, 0x0}) +
  84. `5???!` + string(0x02) + `StreamTitle='test3 - artist3';` + string([]byte{0x0, 0x0}) +
  85. `!!&&&` + string(0x02) + `StreamTitle='test3 - artist3';` + string([]byte{0x0, 0x0}) +
  86. `$$$`) {
  87. t.Error("Unexpected response:", testConn.Out.String())
  88. return
  89. }
  90. }
  91. func TestDudelDuMain(t *testing.T) {
  92. // Make the fatal a simple print
  93. fatal = print
  94. // Make sure out.txt and test.dpl are removed
  95. defer func() {
  96. if res, _ := fileutil.PathExists("out.txt"); res {
  97. os.Remove("out.txt")
  98. }
  99. if res, _ := fileutil.PathExists("test.dpl"); res {
  100. os.Remove("test.dpl")
  101. }
  102. }()
  103. // Reset flags
  104. flag.CommandLine = &flag.FlagSet{}
  105. // Test usage text
  106. os.Args = []string{"dudeldu", "-?", "-port", "9000", "test"}
  107. flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
  108. if ret, err := execMain(); err != nil || ret != `
  109. DudelDu `[1:]+dudeldu.ProductVersion+`
  110. Usage of dudeldu [options] <playlist>
  111. -? Show this help message
  112. -auth string
  113. Authentication as <user>:<pass>
  114. -debug
  115. Enable extra debugging output
  116. -fqs int
  117. Frame queue size (default 10000)
  118. -host string
  119. Server hostname to listen on (default "127.0.0.1")
  120. -loop
  121. Loop playlists
  122. -port string
  123. Server port to listen on (default "9091")
  124. -pp string
  125. Prefix all paths with a string
  126. -shuffle
  127. Shuffle playlists
  128. -tps int
  129. Thread pool size (default 10)
  130. ` {
  131. t.Error("Unexpected output:", "#"+ret+"#", err)
  132. return
  133. }
  134. ioutil.WriteFile("test.dpl", []byte("{}"), 0644)
  135. os.Args = []string{"dudeldu", "-auth", "web:web", "-port", "-1", "test.dpl"}
  136. flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
  137. if ret, err := execMain(); err != nil || ret != `
  138. DudelDu `[1:]+dudeldu.ProductVersion+`
  139. Serving playlist test.dpl on 127.0.0.1:-1
  140. Thread pool size: 10
  141. Frame queue size: 10000
  142. Loop playlist: false
  143. Shuffle playlist: false
  144. Path prefix:
  145. Required authentication: web:web
  146. listen tcp: invalid port -1
  147. Shutting down
  148. ` && ret != `
  149. DudelDu `[1:]+dudeldu.ProductVersion+`
  150. Serving playlist test.dpl on 127.0.0.1:-1
  151. Thread pool size: 10
  152. Frame queue size: 10000
  153. Loop playlist: false
  154. Shuffle playlist: false
  155. Path prefix:
  156. Required authentication: web:web
  157. listen tcp: address -1: invalid port
  158. Shutting down
  159. ` {
  160. t.Error("Unexpected output:", ret, err)
  161. return
  162. }
  163. }
  164. /*
  165. Execute the main function and capture the output.
  166. */
  167. func execMain() (string, error) {
  168. // Exchange stderr to a file
  169. origStdErr := os.Stderr
  170. outFile, err := os.Create("out.txt")
  171. if err != nil {
  172. return "", err
  173. }
  174. defer func() {
  175. outFile.Close()
  176. os.RemoveAll("out.txt")
  177. // Put Stderr back
  178. os.Stderr = origStdErr
  179. }()
  180. os.Stderr = outFile
  181. main()
  182. outFile.Sync()
  183. out, err := ioutil.ReadFile("out.txt")
  184. if err != nil {
  185. return "", err
  186. }
  187. return string(out), nil
  188. }