dudeldu_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. fmt.Println(out.String())
  77. if testConn.Out.String() != ("ICY 200 OK\r\n" +
  78. "Content-Type: audio/mpeg\r\n" +
  79. "icy-name: /testpath\r\n" +
  80. "icy-metadata: 1\r\n" +
  81. "icy-metaint: 5\r\n" +
  82. "\r\n" +
  83. `cdefg` + string(0x02) + `StreamTitle='test2 - artist2';` + string([]byte{0x0, 0x0}) +
  84. `h1234` + string(0x02) + `StreamTitle='test3 - artist3';` + string([]byte{0x0, 0x0}) +
  85. `5???!` + string(0x02) + `StreamTitle='test3 - artist3';` + string([]byte{0x0, 0x0}) +
  86. `!!&&&` + string(0x02) + `StreamTitle='test3 - artist3';` + string([]byte{0x0, 0x0}) +
  87. `$$$`) {
  88. t.Error("Unexpected response:", testConn.Out.String())
  89. return
  90. }
  91. }
  92. func TestDudelDuMain(t *testing.T) {
  93. // Make the fatal a simple print
  94. fatal = print
  95. // Make sure out.txt and test.dpl are removed
  96. defer func() {
  97. if res, _ := fileutil.PathExists("out.txt"); res {
  98. os.Remove("out.txt")
  99. }
  100. if res, _ := fileutil.PathExists("test.dpl"); res {
  101. os.Remove("test.dpl")
  102. }
  103. }()
  104. // Reset flags
  105. flag.CommandLine = &flag.FlagSet{}
  106. // Test usage text
  107. os.Args = []string{"dudeldu", "-?", "-port", "9000", "test"}
  108. flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
  109. if ret, err := execMain(); err != nil || ret != `
  110. DudelDu `[1:]+dudeldu.ProductVersion+`
  111. Usage of dudeldu [options] <playlist>
  112. -? Show this help message
  113. -auth string
  114. Authentication as <user>:<pass>
  115. -debug
  116. Enable extra debugging output
  117. -fqs int
  118. Frame queue size (default 10000)
  119. -host string
  120. Server hostname to listen on (default "127.0.0.1")
  121. -loop
  122. Loop playlists
  123. -port string
  124. Server port to listen on (default "9091")
  125. -shuffle
  126. Shuffle playlists
  127. -tps int
  128. Thread pool size (default 10)
  129. ` {
  130. t.Error("Unexpected output:", ret, err)
  131. return
  132. }
  133. ioutil.WriteFile("test.dpl", []byte("{}"), 0644)
  134. os.Args = []string{"dudeldu", "-auth", "web:web", "-port", "-1", "test.dpl"}
  135. flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
  136. if ret, err := execMain(); err != nil || ret != `
  137. DudelDu `[1:]+dudeldu.ProductVersion+`
  138. Serving playlist test.dpl on 127.0.0.1:-1
  139. Thread pool size: 10
  140. Frame queue size: 10000
  141. Loop playlist: false
  142. Shuffle playlist: false
  143. Required authentication: web:web
  144. listen tcp: invalid port -1
  145. Shutting down
  146. ` && ret != `
  147. DudelDu `[1:]+dudeldu.ProductVersion+`
  148. Serving playlist test.dpl on 127.0.0.1:-1
  149. Thread pool size: 10
  150. Frame queue size: 10000
  151. Loop playlist: false
  152. Shuffle playlist: false
  153. Required authentication: web:web
  154. listen tcp: address -1: invalid port
  155. Shutting down
  156. ` {
  157. t.Error("Unexpected output:", ret, err)
  158. return
  159. }
  160. }
  161. /*
  162. Execute the main function and capture the output.
  163. */
  164. func execMain() (string, error) {
  165. // Exchange stderr to a file
  166. origStdErr := os.Stderr
  167. outFile, err := os.Create("out.txt")
  168. if err != nil {
  169. return "", err
  170. }
  171. defer func() {
  172. outFile.Close()
  173. os.RemoveAll("out.txt")
  174. // Put Stderr back
  175. os.Stderr = origStdErr
  176. }()
  177. os.Stderr = outFile
  178. main()
  179. outFile.Sync()
  180. out, err := ioutil.ReadFile("out.txt")
  181. if err != nil {
  182. return "", err
  183. }
  184. return string(out), nil
  185. }