test_metaflac.sh 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. #!/bin/sh -e
  2. # FLAC - Free Lossless Audio Codec
  3. # Copyright (C) 2002-2009 Josh Coalson
  4. # Copyright (C) 2011-2023 Xiph.Org Foundation
  5. #
  6. # This file is part the FLAC project. FLAC is comprised of several
  7. # components distributed under different licenses. The codec libraries
  8. # are distributed under Xiph.Org's BSD-like license (see the file
  9. # COPYING.Xiph in this distribution). All other programs, libraries, and
  10. # plugins are distributed under the GPL (see COPYING.GPL). The documentation
  11. # is distributed under the Gnu FDL (see COPYING.FDL). Each file in the
  12. # FLAC distribution contains at the top the terms under which it may be
  13. # distributed.
  14. #
  15. # Since this particular file is relevant to all components of FLAC,
  16. # it may be distributed under the Xiph.Org license, which is the least
  17. # restrictive of those mentioned above. See the file COPYING.Xiph in this
  18. # distribution.
  19. . ./common.sh
  20. PATH="$(pwd)/../src/flac:$PATH"
  21. PATH="$(pwd)/../src/metaflac:$PATH"
  22. PATH="$(pwd)/../objs/$BUILD/bin:$PATH"
  23. if echo a | (grep -E '(a|b)') >/dev/null 2>&1
  24. then EGREP='grep -E'
  25. else EGREP='egrep'
  26. fi
  27. testdir="metaflac-test-files"
  28. flacfile="metaflac1.flac"
  29. flac${EXE} --help 1>/dev/null 2>/dev/null || die "ERROR can't find flac executable"
  30. metaflac${EXE} --help 1>/dev/null 2>/dev/null || die "ERROR can't find metaflac executable"
  31. run_flac ()
  32. {
  33. if [ "$FLAC__TEST_WITH_VALGRIND" = yes ] ; then
  34. echo "valgrind --leak-check=yes --show-reachable=yes --num-callers=50 flac $*" >>test_metaflac.valgrind.log
  35. valgrind --leak-check=yes --show-reachable=yes --num-callers=50 --log-fd=4 flac${EXE} ${TOTALLY_SILENT} --no-error-on-compression-fail $* 4>>test_metaflac.valgrind.log
  36. else
  37. flac${EXE} ${TOTALLY_SILENT} --no-error-on-compression-fail $*
  38. fi
  39. }
  40. run_metaflac ()
  41. {
  42. if [ "$FLAC__TEST_WITH_VALGRIND" = yes ] ; then
  43. echo "valgrind --leak-check=yes --show-reachable=yes --num-callers=50 metaflac $*" >>test_metaflac.valgrind.log
  44. valgrind --leak-check=yes --show-reachable=yes --num-callers=50 --log-fd=4 metaflac${EXE} $* 4>>test_metaflac.valgrind.log
  45. else
  46. metaflac${EXE} $*
  47. fi
  48. }
  49. run_metaflac_silent ()
  50. {
  51. if [ -z "$SILENT" ] ; then
  52. run_metaflac $*
  53. else
  54. if [ "$FLAC__TEST_WITH_VALGRIND" = yes ] ; then
  55. echo "valgrind --leak-check=yes --show-reachable=yes --num-callers=50 metaflac $*" >>test_metaflac.valgrind.log
  56. valgrind --leak-check=yes --show-reachable=yes --num-callers=50 --log-fd=4 metaflac${EXE} $* 2>/dev/null 4>>test_metaflac.valgrind.log
  57. else
  58. metaflac${EXE} $* 2>/dev/null
  59. fi
  60. fi
  61. }
  62. run_metaflac_to_metaflac_silent ()
  63. {
  64. if [ "$FLAC__TEST_WITH_VALGRIND" = yes ] ; then
  65. echo "valgrind --leak-check=yes --show-reachable=yes --num-callers=50 metaflac $*" >>test_metaflac.valgrind.log
  66. valgrind --leak-check=yes --show-reachable=yes --num-callers=50 --log-fd=4 metaflac${EXE} $* 2>/dev/null 4>>test_metaflac.valgrind.log
  67. else
  68. metaflac${EXE} $1 | metaflac${EXE} $2 2>/dev/null
  69. fi
  70. }
  71. check_flac ()
  72. {
  73. run_flac --silent --test $flacfile || die "ERROR in $flacfile" 1>&2
  74. }
  75. echo "Generating stream..."
  76. bytes=80000
  77. if dd if=/dev/zero ibs=1 count=$bytes 2>/dev/null | flac${EXE} ${TOTALLY_SILENT} --force --verify -0 --input-size=$bytes --output-name=$flacfile --force-raw-format --endian=big --sign=signed --channels=1 --bps=8 --sample-rate=8000 - ; then
  78. chmod +w $flacfile
  79. else
  80. die "ERROR during generation"
  81. fi
  82. check_flac
  83. testdatadir=${top_srcdir}/test/metaflac-test-files
  84. filter ()
  85. {
  86. # minor danger, changing vendor strings will change the length of the
  87. # VORBIS_COMMENT block, but if we add "^ length: " to the patterns,
  88. # we lose info about PADDING size that we need
  89. # grep pattern 1: remove vendor string
  90. # grep pattern 2: remove minimum/maximum frame and block size from STREAMINFO
  91. # grep pattern 3: remove hexdump data from PICTURE metadata blocks
  92. # sed pattern 1: remove stream offset values from SEEKTABLE points
  93. $EGREP -v '^ vendor string: |^ m..imum .....size: |^ 0000[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]: ' \
  94. | sed -e 's/, stream_offset.*//'
  95. }
  96. metaflac_test ()
  97. {
  98. case="$testdatadir/$1"
  99. desc="$2"
  100. args="$3"
  101. expect="$case-expect.meta"
  102. echo $ECHO_N "test $1: $desc... " $ECHO_C
  103. run_metaflac $args $flacfile | filter > $testdir/out1.meta || die "ERROR running metaflac"
  104. # Ignore lengths which can be affected by the version string.
  105. sed "s/length:.*/length: XXX/" $testdir/out1.meta > $testdir/out.meta
  106. diff -w $expect $testdir/out.meta > /dev/null 2>&1 || die "ERROR: metadata does not match expected $expect"
  107. # To blindly accept (and check later): cp -f $testdir/out.meta $expect
  108. echo OK
  109. }
  110. metaflac_test_nofilter ()
  111. {
  112. case="$testdatadir/$1"
  113. desc="$2"
  114. args="$3"
  115. expect="$case-expect.meta"
  116. echo $ECHO_N "test $1: $desc... " $ECHO_C
  117. run_metaflac $args $flacfile > $testdir/out.meta || die "ERROR running metaflac"
  118. diff -w $expect $testdir/out.meta || die "ERROR: metadata does not match expected $expect"
  119. echo OK
  120. }
  121. metaflac_test_binary ()
  122. {
  123. case="$testdatadir/$1"
  124. desc="$2"
  125. args="$3"
  126. expect="$case-expect.meta"
  127. echo $ECHO_N "test $1: $desc... " $ECHO_C
  128. run_metaflac $args $flacfile > $testdir/out.meta || die "ERROR running metaflac"
  129. cmp $expect $testdir/out.meta || die "ERROR: metadata does not match expected $expect"
  130. echo OK
  131. }
  132. metaflac_test case00 "--list" "--list"
  133. metaflac_test case01 "STREAMINFO --show-* shortcuts" "
  134. --show-md5sum
  135. --show-min-blocksize
  136. --show-max-blocksize
  137. --show-min-framesize
  138. --show-max-framesize
  139. --show-sample-rate
  140. --show-channels
  141. --show-bps
  142. --show-total-samples"
  143. run_metaflac --preserve-modtime --add-padding=12345 $flacfile
  144. check_flac
  145. metaflac_test case02 "--add-padding" "--list"
  146. # some flavors of /bin/sh (e.g. Darwin's) won't even handle quoted spaces, so we underscore:
  147. run_metaflac --set-tag="ARTIST=The_artist_formerly_known_as_the_artist..." $flacfile
  148. check_flac
  149. metaflac_test case03 "--set-tag=ARTIST" "--list"
  150. run_metaflac --set-tag="ARTIST=Chuck_Woolery" $flacfile
  151. check_flac
  152. metaflac_test case04 "--set-tag=ARTIST" "--list"
  153. run_metaflac --set-tag="ARTIST=Vern" $flacfile
  154. check_flac
  155. metaflac_test case05 "--set-tag=ARTIST" "--list"
  156. run_metaflac --set-tag="TITLE=He_who_smelt_it_dealt_it" $flacfile
  157. check_flac
  158. metaflac_test case06 "--set-tag=TITLE" "--list"
  159. if [ ! $git_commit_version_hash ] ; then
  160. metaflac_test case07 "--show-vendor-tag --show-tag=ARTIST" "--show-vendor-tag --show-tag=ARTIST"
  161. else
  162. echo "test case07 is skipped because version is taken from git"
  163. fi
  164. run_metaflac --remove-first-tag=ARTIST $flacfile
  165. check_flac
  166. metaflac_test case08 "--remove-first-tag=ARTIST" "--list"
  167. run_metaflac --remove-tag=ARTIST $flacfile
  168. check_flac
  169. metaflac_test case09 "--remove-tag=ARTIST" "--list"
  170. metaflac_test case10 "--list --block-type=VORBIS_COMMENT" "--list --block-type=VORBIS_COMMENT"
  171. metaflac_test case11 "--list --block-number=0" "--list --block-number=0"
  172. metaflac_test case12 "--list --block-number=1,2,999" "--list --block-number=1,2,999"
  173. metaflac_test case13 "--list --block-type=VORBIS_COMMENT,PADDING" "--list --block-type=VORBIS_COMMENT,PADDING"
  174. metaflac_test case14 "--list --except-block-type=SEEKTABLE,VORBIS_COMMENT" "--list --except-block-type=SEEKTABLE,VORBIS_COMMENT"
  175. metaflac_test case15 "--list --except-block-type=STREAMINFO" "--list --except-block-type=STREAMINFO"
  176. run_metaflac --add-padding=4321 $flacfile $flacfile
  177. check_flac
  178. metaflac_test case16 "--add-padding=4321 * 2" "--list"
  179. run_metaflac --merge-padding $flacfile
  180. check_flac
  181. metaflac_test case17 "--merge-padding" "--list"
  182. run_metaflac --add-padding=0 $flacfile
  183. check_flac
  184. metaflac_test case18 "--add-padding=0" "--list"
  185. run_metaflac --sort-padding $flacfile
  186. check_flac
  187. metaflac_test case19 "--sort-padding" "--list"
  188. run_metaflac --add-padding=0 $flacfile
  189. check_flac
  190. metaflac_test case20 "--add-padding=0" "--list"
  191. run_metaflac --remove-all-tags $flacfile
  192. check_flac
  193. metaflac_test case21 "--remove-all-tags" "--list"
  194. run_metaflac --remove --block-number=1,99 --dont-use-padding $flacfile
  195. check_flac
  196. metaflac_test case22 "--remove --block-number=1,99 --dont-use-padding" "--list"
  197. run_metaflac --remove --block-number=99 --dont-use-padding $flacfile
  198. check_flac
  199. metaflac_test case23 "--remove --block-number=99 --dont-use-padding" "--list"
  200. run_metaflac --remove --block-type=PADDING $flacfile
  201. check_flac
  202. metaflac_test case24 "--remove --block-type=PADDING" "--list"
  203. run_metaflac --remove --block-type=PADDING --dont-use-padding $flacfile
  204. check_flac
  205. metaflac_test case25 "--remove --block-type=PADDING --dont-use-padding" "--list"
  206. run_metaflac --add-padding=0 $flacfile $flacfile
  207. check_flac
  208. metaflac_test case26 "--add-padding=0 * 2" "--list"
  209. run_metaflac --remove --except-block-type=PADDING $flacfile
  210. check_flac
  211. metaflac_test case27 "--remove --except-block-type=PADDING" "--list"
  212. run_metaflac --remove-all $flacfile
  213. check_flac
  214. metaflac_test case28 "--remove-all" "--list"
  215. run_metaflac --remove-all --dont-use-padding $flacfile
  216. check_flac
  217. metaflac_test case29 "--remove-all --dont-use-padding" "--list"
  218. run_metaflac --remove-all --dont-use-padding $flacfile
  219. check_flac
  220. metaflac_test case30 "--remove-all --dont-use-padding" "--list"
  221. run_metaflac --set-tag="f=0123456789abcdefghij" $flacfile
  222. check_flac
  223. metaflac_test case31 "--set-tag=..." "--list"
  224. run_metaflac --remove-all-tags --set-tag="f=0123456789abcdefghi" $flacfile
  225. check_flac
  226. metaflac_test case32 "--remove-all-tags --set-tag=..." "--list"
  227. run_metaflac --remove-all-tags --set-tag="f=0123456789abcde" $flacfile
  228. check_flac
  229. metaflac_test case33 "--remove-all-tags --set-tag=..." "--list"
  230. run_metaflac --remove-all-tags --set-tag="f=0" $flacfile
  231. check_flac
  232. metaflac_test case34 "--remove-all-tags --set-tag=..." "--list"
  233. run_metaflac --remove-all-tags --set-tag="f=0123456789" $flacfile
  234. check_flac
  235. metaflac_test case35 "--remove-all-tags --set-tag=..." "--list"
  236. run_metaflac --remove-all-tags --set-tag="f=0123456789abcdefghi" $flacfile
  237. check_flac
  238. metaflac_test case36 "--remove-all-tags --set-tag=..." "--list"
  239. run_metaflac --remove-all-tags --set-tag="f=0123456789" $flacfile
  240. check_flac
  241. metaflac_test case37 "--remove-all-tags --set-tag=..." "--list"
  242. run_metaflac --remove-all-tags --set-tag="f=0123456789abcdefghij" $flacfile
  243. check_flac
  244. metaflac_test case38 "--remove-all-tags --set-tag=..." "--list"
  245. echo "TITLE=Tittle" | run_metaflac --import-tags-from=- $flacfile
  246. check_flac
  247. metaflac_test case39 "--import-tags-from=-" "--list"
  248. cat > vc.txt << EOF
  249. artist=Fartist
  250. artist=artits
  251. EOF
  252. run_metaflac --import-tags-from=vc.txt $flacfile
  253. check_flac
  254. metaflac_test case40 "--import-tags-from=[FILE]" "--list"
  255. rm vc.txt
  256. run_metaflac --add-replay-gain $flacfile
  257. check_flac
  258. metaflac_test case41 "--add-replay-gain" "--list"
  259. run_metaflac --remove-replay-gain $flacfile
  260. check_flac
  261. metaflac_test case42 "--remove-replay-gain" "--list"
  262. run_metaflac --scan-replay-gain $flacfile
  263. check_flac
  264. metaflac_test case42 "--scan-replay-gain" "--list"
  265. # CUESHEET blocks
  266. cs_in=${top_srcdir}/test/cuesheets/good.000.cue
  267. cs_out=metaflac.cue
  268. cs_out2=metaflac2.cue
  269. run_metaflac --import-cuesheet-from="$cs_in" $flacfile
  270. check_flac
  271. metaflac_test case43 "--import-cuesheet-from" "--list"
  272. run_metaflac --export-cuesheet-to=$cs_out $flacfile
  273. run_metaflac --remove --block-type=CUESHEET $flacfile
  274. check_flac
  275. metaflac_test case44 "--remove --block-type=CUESHEET" "--list"
  276. run_metaflac --import-cuesheet-from=$cs_out $flacfile
  277. check_flac
  278. metaflac_test case45 "--import-cuesheet-from" "--list"
  279. run_metaflac --export-cuesheet-to=$cs_out2 $flacfile
  280. echo "comparing cuesheets:"
  281. diff $cs_out $cs_out2 || die "ERROR, cuesheets should be identical"
  282. echo identical
  283. rm -f $cs_out $cs_out2
  284. # PICTURE blocks
  285. ncase=46
  286. for f in \
  287. 0.gif \
  288. 1.gif \
  289. 2.gif \
  290. ; do
  291. run_metaflac --import-picture-from="|image/gif|$f||${top_srcdir}/test/pictures/$f" $flacfile
  292. check_flac
  293. metaflac_test "case$ncase" "--import-picture-from" "--list"
  294. ncase=$((ncase + 1))
  295. done
  296. for f in \
  297. 0.jpg \
  298. 4.jpg \
  299. ; do
  300. run_metaflac --import-picture-from="4|image/jpeg|$f||${top_srcdir}/test/pictures/$f" $flacfile
  301. check_flac
  302. metaflac_test "case$ncase" "--import-picture-from" "--list"
  303. ncase=$((ncase + 1))
  304. done
  305. for f in \
  306. 0.png \
  307. 1.png \
  308. 2.png \
  309. 3.png \
  310. 4.png \
  311. 5.png \
  312. 6.png \
  313. 7.png \
  314. 8.png \
  315. ; do
  316. run_metaflac --import-picture-from="5|image/png|$f||${top_srcdir}/test/pictures/$f" $flacfile
  317. check_flac
  318. metaflac_test "case$ncase" "--import-picture-from" "--list"
  319. ncase=$((ncase + 1))
  320. done
  321. [ $ncase = 60 ] || die "expected case# to be 60"
  322. fn=export-picture-check
  323. echo $ECHO_N "Testing --export-picture-to... " $ECHO_C
  324. run_metaflac --export-picture-to=$fn $flacfile
  325. check_flac
  326. cmp $fn ${top_srcdir}/test/pictures/0.gif || die "ERROR, exported picture file and original differ"
  327. echo OK
  328. rm -f $fn
  329. echo $ECHO_N "Testing --block-number --export-picture-to... " $ECHO_C
  330. run_metaflac --block-number=9 --export-picture-to=$fn $flacfile
  331. check_flac
  332. cmp $fn ${top_srcdir}/test/pictures/0.png || die "ERROR, exported picture file and original differ"
  333. echo OK
  334. rm -f $fn
  335. run_metaflac --remove --block-type=PICTURE $flacfile
  336. check_flac
  337. metaflac_test case60 "--remove --block-type=PICTURE" "--list"
  338. run_metaflac --import-picture-from="1|image/png|standard_icon|32x32x24|${top_srcdir}/test/pictures/0.png" $flacfile
  339. check_flac
  340. metaflac_test case61 "--import-picture-from" "--list"
  341. run_metaflac --import-picture-from="2|image/png|icon|64x64x24|${top_srcdir}/test/pictures/1.png" $flacfile
  342. check_flac
  343. metaflac_test case62 "--import-picture-from" "--list"
  344. run_metaflac --remove-all-tags-except=artist=title $flacfile
  345. check_flac
  346. metaflac_test case63 "--remove-all-tags-except=artist=title" "--list"
  347. metaflac_test case64 "--export-tags-to=-" "--export-tags-to=-"
  348. metaflac_test case64 "--show-all-tags" "--show-all-tags"
  349. run_flac ${top_srcdir}/test/foreign-metadata-test-files/AIFF-ID3.aiff --keep-foreign-metadata -f -o $flacfile
  350. metaflac_test_binary case65 "--data-format=binary" "--list --data-format=binary-headerless --block-type=APPLICATION:aiff"
  351. # UNKNOWN blocks
  352. flacfile=metaflac2.flac
  353. echo $ECHO_N "Testing FLAC file with unknown metadata... " $ECHO_C
  354. cp -p ${top_srcdir}/test/metaflac.flac.in $flacfile
  355. # remove the VORBIS_COMMENT block so vendor string changes don't interfere with the comparison:
  356. run_metaflac --remove --block-type=VORBIS_COMMENT --dont-use-padding $flacfile
  357. cmp $flacfile ${top_srcdir}/test/metaflac.flac.ok || die "ERROR, $flacfile and metaflac.flac.ok differ"
  358. echo OK
  359. flacfile=metaflac3.flac
  360. cp -p ${top_srcdir}/test/metaflac.flac.in $flacfile
  361. flacfile2=metaflac4.flac
  362. cp $flacfile $flacfile2
  363. run_metaflac --remove-all --dont-use-padding $flacfile
  364. echo $ECHO_N "Appending a streaminfo metadata block... " $ECHO_C
  365. if run_metaflac_to_metaflac_silent "--list --data-format=binary $flacfile2" "--append $flacfile" ; then
  366. die "ERROR: it should have failed but didn't"
  367. else
  368. echo "OK, it failed as it should"
  369. fi
  370. echo $ECHO_N "Appending a seektable metadata block... " $ECHO_C
  371. if run_metaflac_to_metaflac_silent "--list --data-format=binary --except-block-type=STREAMINFO $flacfile2" "--append $flacfile" ; then
  372. die "ERROR: it should have failed but didn't"
  373. else
  374. echo "OK, it failed as it should"
  375. fi
  376. run_metaflac --add-seekpoint=0 $flacfile
  377. echo $ECHO_N "Appending a vorbis comment metadata block... " $ECHO_C
  378. if run_metaflac_to_metaflac_silent "--list --data-format=binary --block-type=VORBIS_COMMENT $flacfile2" "--append $flacfile" ; then
  379. echo "OK"
  380. else
  381. die "ERROR, couldn't add vorbis comment metadata block"
  382. fi
  383. echo $ECHO_N "Appending another vorbis comment metadata block... " $ECHO_C
  384. if run_metaflac_to_metaflac_silent "--list --data-format=binary --block-type=VORBIS_COMMENT $flacfile2" "--append $flacfile" ; then
  385. die "ERROR: it should have failed but didn't"
  386. else
  387. echo "OK, it failed as it should"
  388. fi
  389. if run_metaflac_to_metaflac_silent "--list --data-format=binary --except-block-type=STREAMINFO,SEEKTABLE,VORBIS_COMMENT $flacfile2" "--append $flacfile" ; then
  390. :
  391. else
  392. die "ERROR, couldn't add vorbis comment metadata block"
  393. fi
  394. metaflac_test_nofilter case66 "--append" "--list"
  395. if run_metaflac_to_metaflac_silent "--list --data-format=binary --except-block-type=STREAMINFO,SEEKTABLE,VORBIS_COMMENT $flacfile2" "--append --block-number=0 $flacfile" ; then
  396. :
  397. else
  398. die "ERROR, couldn't add vorbis comment metadata block"
  399. fi
  400. metaflac_test_nofilter case67 "--append --block-number=0" "--list"
  401. rm -f metaflac-test-files/out.meta metaflac-test-files/out1.meta