Beispiel #1
0
        private static void TestTagUtilsTestCloneUsingRaw()
        {
            ID3.Tag tag = TestTags.CreateDemoTag(Version.v2_3);

            byte[] b0 = TagUtils.TagToRaw(tag);
            byte[] b1 = TagUtils.TagToRaw(tag.Clone());

            UnitTest.Test(ArrayUtils.IsEqual(b0, b1));
        }
Beispiel #2
0
        public static long OffsetToMpegHeader(FileInfo file)
        {
            long tagSize = TagUtils.TagSizeV2(file);

            using (Stream s = VirtualDrive.OpenInStream(file.FullName))
            {
                s.Seek(tagSize, SeekOrigin.Begin);
                return(tagSize + TagUtils.OffsetToFirstMpegHeader(s));
            }
        }
Beispiel #3
0
        private static void TestTagUtilsWriteRawBiggerExact()
        {
            string fileName = VirtualDrive.VirtualFileName("TestTagUtilsWriteRawBiggerExact\\t1.mp3");

            TestTags.WriteDemoMp3(fileName, TestTags.demoTag2_3);

            FileInfo fileInfo = new FileInfo(fileName);

            TagUtils.WriteTagV2(TestTags.demoTag2_4, fileInfo, Rewriter.Strategy.Exact);
            UnitTest.Test(VirtualDrive.FileLength(fileName) == TestTags.demoTag2_4.Length + TestTags.mpegDummy.Length);
            TagUtils.StripTagV2(fileInfo, 0);
            UnitTest.Test(ArrayUtils.IsEqual(VirtualDrive.Load(fileName), TestTags.mpegDummy));
        }
Beispiel #4
0
        private static void TestTagUtilsWriteRawSmallerNeverShrink()
        {
            string fileName = VirtualDrive.VirtualFileName("TestTagUtilsWriteRawSmallerNeverShrink\\t1.mp3");

            TestTags.WriteDemoMp3(fileName, TestTags.demoTag2_4);

            FileInfo fileInfo = new FileInfo(fileName);

            TagUtils.WriteTagV2(TestTags.demoTag2_3, fileInfo, Rewriter.Strategy.NeverShrink);
            UnitTest.Test(TagUtils.TagSizeV2(fileInfo) == TestTags.demoTag2_4.Length);
            TagUtils.StripTagV2(fileInfo, 0);
            UnitTest.Test(ArrayUtils.IsEqual(VirtualDrive.Load(fileName), TestTags.mpegDummy));
        }
Beispiel #5
0
        private static void TestTagUtilsWriteRawSmallerQuantizeAbove()
        {
            string fileName = VirtualDrive.VirtualFileName("TestTagUtilsWriteRawSmallerQuantizeAbove\\t1.mp3");

            TestTags.WriteDemoMp3(fileName, TestTags.demoTag2_4);

            FileInfo fileInfo = new FileInfo(fileName);

            TagUtils.WriteTagV2(TestTags.demoTag2_3, fileInfo, Rewriter.Strategy.Quantize);
            UnitTest.Test(TagUtils.TagSizeV2(new FileInfo(fileName)) % Rewriter.BlockSize == 0);
            TagUtils.StripTagV2(new FileInfo(fileName), 0);
            UnitTest.Test(ArrayUtils.IsEqual(VirtualDrive.Load(fileName), TestTags.mpegDummy));
        }
Beispiel #6
0
        private static void TestSetTagSizeRawV2()
        {
            int inc = 10;

            byte[] raw0 = (byte[])TestTags.demoTag2_3.Clone();

            int size0 = TagUtils.TagSize(raw0);
            int size1 = size0 + inc;

            byte[] raw1 = TagUtils.RecreateTagWithNewTagSize(raw0, size1);

            UnitTest.Test(raw1.Length == raw0.Length + inc);
            UnitTest.Test(TagUtils.TagSize(raw1) == size1);
        }
Beispiel #7
0
        public int Rewrite(int bytesRequired, FileInfo file)
        {
            int padding = CalculatePadding(bytesRequired, file);

            if (padding < bytesRequired)
            {
                throw new Exception("CalculatePadding failed! Padding too small!");
            }

            if (TagUtils.TagSizeV2(file) != padding)
            {
                TagUtils.StripTagV2(file, padding);
            }

            return(padding);
        }
Beispiel #8
0
        public static void CreateDemoTags(string path, int count, Action <TagEditor> action)
        {
            for (int i = 0; i < count; ++i)
            {
                Tag tag = TestTags.CreateDemoTag(ID3.Version.v2_3);

                TagEditor editor = new TagEditor(tag);
                editor.Title       = "Song No. " + (i + 1);
                editor.TrackNumber = (i + 1).ToString();
                editor.ReleaseYear = "1993";

                action(editor);

                TagUtils.WriteTag(tag, new FileInfo(Path.Combine(path, "Test" + i + ".mp3")));
            }
        }
Beispiel #9
0
        private static void TestTagUtilsWriteTagBiggerQuantize()
        {
            string fileName = VirtualDrive.VirtualFileName("TestTagUtilsWriteTagBiggerQuantize\\t1.mp3");

            TestTags.WriteDemoMp3(fileName, TestTags.demoTag2_3);
            FileInfo fileInfo = new FileInfo(fileName);
            Tag      tag      = TagUtils.ReadTag(fileInfo);
            Frame    f        = new Frame(tag.DescriptionMap, "MCDI");

            (f.Content as FrameContentBinary).Content = new byte[1024];
            tag.Add(f);

            TagUtils.WriteTagV2(tag, fileInfo, Rewriter.Strategy.Quantize);
            UnitTest.Test(VirtualDrive.FileLength(fileName) == 2048 + TestTags.mpegDummy.Length);
            TagUtils.StripTagV2(fileInfo, 0);
            UnitTest.Test(ArrayUtils.IsEqual(VirtualDrive.Load(fileName), TestTags.mpegDummy));
        }
Beispiel #10
0
        private static void TestTagUtilsReadWriteRaw()
        {
            int counter = 0;

            foreach (var demoTag in TestTags.Demotags)
            {
                FileInfo fileInfo = VirtualDrive.CreateVirtualFileInfo(
                    "TestTagUtilsReadWriteRaw\\" + counter + ".tag");

                TagUtils.WriteTag(demoTag, fileInfo);

                byte[] tagRaw = TagUtils.ReadTagRaw(fileInfo);

                UnitTest.Test(ArrayUtils.IsEqual(tagRaw, demoTag));

                counter++;
            }
        }
Beispiel #11
0
        private static void TestTagUtilsBuildByCodeAndSerializeInVirtualStore()
        {
            foreach (ID3.Version version in ID3.Version.Versions)
            {
                string filename = VirtualDrive.VirtualFileName(
                    "TestID3TagBuildByCodeAndSerializeInVirtualStore\\" + version.ToString() + ".tag");

                Tag tag0 = TestTags.CreateDemoTag(version);

                TagUtils.WriteTag(tag0, new FileInfo(filename));

                Tag tag1 = TagUtils.ReadTag(new FileInfo(filename));

                TagEditor editor0 = new TagEditor(tag0);
                TagEditor editor1 = new TagEditor(tag1);

                UnitTest.Test(editor1.Equals(editor0));
            }
        }
Beispiel #12
0
        public static long OffsetTagToMpegHeader(FileInfo file)
        {
            long tagSize = TagUtils.TagSizeV2(file);

            using (Stream s = VirtualDrive.OpenInStream(file.FullName))
            {
                s.Seek(tagSize, SeekOrigin.Begin);

                long offset = TagUtils.OffsetToFirstMpegHeader(s);

                if (offset > 0)
                {
                    Logger.WriteLine(Tokens.Warning, "Tag V2 has wrong size. Offset to MPEG header: " + offset
                                     + " in file " + file.FullName);
                }

                return(offset);
            }
        }
Beispiel #13
0
        private static void TestTagUtilsWriteTagSmallerExact()
        {
            Tag   tag = TagUtils.RawToTag(TestTags.demoTag2_3);
            Frame f   = new Frame(tag.DescriptionMap, "MCDI");

            (f.Content as FrameContentBinary).Content = new byte[1024];
            tag.Add(f);

            string fileName = VirtualDrive.VirtualFileName("TestTagUtilsWriteTagSmallerExact\\t1.mp3");

            TestTags.WriteDemoMp3(fileName, TagUtils.TagToRaw(tag));

            tag.Remove(f);
            FileInfo fileInfo = new FileInfo(fileName);

            TagUtils.WriteTagV2(tag, fileInfo, Rewriter.Strategy.Exact);
            UnitTest.Test(VirtualDrive.FileLength(fileName) == TagUtils.TagToRaw(tag).Length + TestTags.mpegDummy.Length);
            TagUtils.StripTagV2(fileInfo, 0);
            UnitTest.Test(ArrayUtils.IsEqual(VirtualDrive.Load(fileName), TestTags.mpegDummy));
        }
Beispiel #14
0
        private static void TestTagUtilsRewrite()
        {
            byte[] data     = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
            string fileName = VirtualDrive.VirtualFileName("TestID3TagUtilsRewrite\\t0.mp3");

            Id3FileUtils.SaveFileBinary(fileName, data);

            TagUtils.Rewrite(new FileInfo(fileName), 4, 3, 2, 1);

            Stream s = VirtualDrive.OpenInStream(fileName);

            UnitTest.Test(s.ReadByte() == 0);
            UnitTest.Test(s.ReadByte() == 0);
            UnitTest.Test(s.ReadByte() == 4);
            UnitTest.Test(s.ReadByte() == 5);
            UnitTest.Test(s.ReadByte() == 6);
            UnitTest.Test(s.ReadByte() == 7);
            UnitTest.Test(s.ReadByte() == 8);
            UnitTest.Test(s.ReadByte() == 0);
            UnitTest.Test(s.ReadByte() == -1);
            s.Close();
        }
Beispiel #15
0
        private static void TestTagUtilsReadWrite()
        {
            int counter = 0;

            foreach (var demoTag in TestTags.Demotags)
            {
                FileInfo fileInfo = VirtualDrive.CreateVirtualFileInfo(
                    "TestTagUtilsReadWrite\\" + counter + ".tag");

                TagUtils.WriteTag(demoTag, fileInfo);

                Tag tag0 = TagUtils.ReadTag(fileInfo);

                UnitTest.Test(TestTags.IsDemoTag(new TagEditor(tag0)));

                TagUtils.WriteTag(tag0, fileInfo);

                UnitTest.Test(ArrayUtils.IsEqual(TagUtils.ReadTagRaw(fileInfo), demoTag));

                counter++;
            }
        }
Beispiel #16
0
        private static void TestTagUtilsRewriteMP3()
        {
            byte[] tag20 = TagUtils.TagToRaw(TestTags.CreateDemoTag(Version.v2_3));
            byte[] tag10 = TestTags.demoTag1_0;

            string fileName = VirtualDrive.VirtualFileName("TestID3TagUtilsRewrite\\t1.mp3");

            using (Stream s = VirtualDrive.OpenOutStream(fileName))
            {
                s.Write(tag20, 0, tag20.Length);
                s.WriteByte(0);
                s.WriteByte(0);
                s.Write(TestTags.mpegDummy, 0, TestTags.mpegDummy.Length);
                s.Write(tag10, 0, tag10.Length);
            }

            long offset = TagUtils.OffsetTagToMpegHeader(new FileInfo(fileName));

            UnitTest.Test(offset == 2);

            TagUtils.StripTags(new FileInfo(fileName), 0, 0);

            UnitTest.Test(ArrayUtils.IsEqual(VirtualDrive.Load(fileName), TestTags.mpegDummy));
        }
Beispiel #17
0
        private int CalculatePadding(int bytesRequired, FileInfo file)
        {
            int padding = 0;

            switch (CurrentStrategy)
            {
            case Strategy.Exact:
                padding = bytesRequired;
                break;

            case Strategy.NeverShrink:
                padding = Math.Max(bytesRequired, TagUtils.TagSizeV2(file));
                break;

            case Strategy.Quantize:
                padding = CeilBlockSize(bytesRequired);
                break;

            default:
                throw new Exception("Unknown write strategy");
            }

            return(padding);
        }