Test bad inputs

This commit is contained in:
Chris McCord 2021-12-14 21:07:02 -05:00
parent 816eb78064
commit 803f0b52cc
2 changed files with 20 additions and 4 deletions

View file

@ -36,11 +36,19 @@ defmodule LiveBeats.MP3Stat do
def parse(path) do def parse(path) do
{tag_info, rest} = parse_tag(File.read!(path)) {tag_info, rest} = parse_tag(File.read!(path))
duration = parse_frame(rest, 0, 0, 0) duration = parse_frame(rest, 0, 0, 0)
case duration do
duration when is_float(duration) and duration > 0 ->
title = Enum.at(tag_info["TIT2"] || [], 0) title = Enum.at(tag_info["TIT2"] || [], 0)
artist = Enum.at(tag_info["TPE1"] || [], 0) artist = Enum.at(tag_info["TPE1"] || [], 0)
seconds = round(duration)
{:ok, {:ok,
%MP3Stat{duration: round(duration), path: path, tags: tag_info, title: title, artist: artist}} %MP3Stat{duration: seconds, path: path, tags: tag_info, title: title, artist: artist}}
_other ->
{:error, :bad_file}
end
rescue rescue
_ -> {:error, :bad_file} _ -> {:error, :bad_file}
end end

View file

@ -9,4 +9,12 @@ defmodule LiveBeats.MP3StatTest do
assert stat.title == "Silence" assert stat.title == "Silence"
assert stat.artist == "Anon" assert stat.artist == "Anon"
end end
test "parse/1 with invalid mp3" do
assert {:error, :bad_file} = MP3Stat.parse("mix.exs")
end
test "parse/1 with missing file" do
assert {:error, :bad_file} = MP3Stat.parse("lsfjslkfjslkfjs")
end
end end