# Path: chirp/common/mp3_header_test.py
# VALID_MP3_HEADERS = {
#     '\xff\xfb\x90\x64':
#         mp3_header.MP3Header(bit_rate_kbps=128, sampling_rate_hz=44100,
#                              channels=mp3_header.JOINT_STEREO,
#                              padding=False, protected=False),
#     '\xff\xfb\xb0\x00': 
#         mp3_header.MP3Header(bit_rate_kbps=192, sampling_rate_hz=44100,
#                              channels=mp3_header.STEREO,
#                              padding=False, protected=False),
#     '\xff\xfb\xb2\x00': 
#         mp3_header.MP3Header(bit_rate_kbps=192, sampling_rate_hz=44100,
#                              channels=mp3_header.STEREO,
#                              padding=True, protected=False),
#     }
# INVALID_MP3_HEADERS = (
#     '',
#     'GARBAGE',
#     '\xff\xf0\0\0',
#     '\xff', '\xff' * 2, '\xff' * 3, '\xff' * 4,
#     )
# class ParseHeaderTest(unittest.TestCase):
#         class MockInfo(mutagen.mp3.MPEGInfo):
#     def test_is_complete(self):
#     def test_parse(self):
#     def test_match(self):
#     def test_find(self):
#     def test_from_mutagen(self):
#             def __init__(self):
# 
# Path: chirp/library/fingerprint.py
# def compute(file_obj):
# def is_valid(fingerprint_str):


import cStringIO
import hashlib
import unittest
from chirp.common import mp3_header_test
from chirp.library import fingerprint
class FingerprintTest(unittest.TestCase):

    def test_compute(self):
        raw_hdr, hdr = mp3_header_test.VALID_MP3_HEADERS.items()[0]
        frame_data = raw_hdr + ("x" * (hdr.frame_size - len(raw_hdr)))

        for i, seq in enumerate((
            [ frame_data ],
            [ "junk", frame_data ],
            [ frame_data, "junk" ],
            [ "junk", frame_data, "junk" ],
            [ frame_data, frame_data ],
            [ "junk", frame_data, frame_data ],
            [ frame_data, "junk", frame_data ],
            [ frame_data, frame_data, "junk" ],
            [ "junk", frame_data, "junk", frame_data ],
            [ frame_data, "junk", frame_data, "junk" ],
            [ "junk", frame_data, frame_data, "junk" ],
            [ "junk", frame_data, "junk", frame_data, "junk" ],
            )):
            sha1_calc = hashlib.sha1()
            for data in seq:
                if data == frame_data:
                    sha1_calc.update(data)
            expected_fingerprint = sha1_calc.hexdigest()

            stream = cStringIO.StringIO(''.join(seq))
            actual_fingerprint = fingerprint.compute(stream)
            self.assertEqual(expected_fingerprint, actual_fingerprint,
                             msg="Case #%d failed" % i)

# Path: chirp/common/mp3_header_test.py
# VALID_MP3_HEADERS = {
#     '\xff\xfb\x90\x64':
#         mp3_header.MP3Header(bit_rate_kbps=128, sampling_rate_hz=44100,
#                              channels=mp3_header.JOINT_STEREO,
#                              padding=False, protected=False),
#     '\xff\xfb\xb0\x00': 
#         mp3_header.MP3Header(bit_rate_kbps=192, sampling_rate_hz=44100,
#                              channels=mp3_header.STEREO,
#                              padding=False, protected=False),
#     '\xff\xfb\xb2\x00': 
#         mp3_header.MP3Header(bit_rate_kbps=192, sampling_rate_hz=44100,
#                              channels=mp3_header.STEREO,
#                              padding=True, protected=False),
#     }
# INVALID_MP3_HEADERS = (
#     '',
#     'GARBAGE',
#     '\xff\xf0\0\0',
#     '\xff', '\xff' * 2, '\xff' * 3, '\xff' * 4,
#     )
# class ParseHeaderTest(unittest.TestCase):
#         class MockInfo(mutagen.mp3.MPEGInfo):
#     def test_is_complete(self):
#     def test_parse(self):
#     def test_match(self):
#     def test_find(self):
#     def test_from_mutagen(self):
#             def __init__(self):
# 
# Path: chirp/library/fingerprint.py
# def compute(file_obj):
# def is_valid(fingerprint_str):


# Path: chirp/library/fingerprint_test.py
import cStringIO
import hashlib
import unittest
from chirp.common import mp3_header_test
from chirp.library import fingerprint

class FingerprintTest(unittest.TestCase):

    def test_compute(self):
        raw_hdr, hdr = mp3_header_test.VALID_MP3_HEADERS.items()[0]
        frame_data = raw_hdr + ("x" * (hdr.frame_size - len(raw_hdr)))

        for i, seq in enumerate((
            [ frame_data ],
            [ "junk", frame_data ],
            [ frame_data, "junk" ],
            [ "junk", frame_data, "junk" ],
            [ frame_data, frame_data ],
            [ "junk", frame_data, frame_data ],
            [ frame_data, "junk", frame_data ],
            [ frame_data, frame_data, "junk" ],
            [ "junk", frame_data, "junk", frame_data ],
            [ frame_data, "junk", frame_data, "junk" ],
            [ "junk", frame_data, frame_data, "junk" ],
            [ "junk", frame_data, "junk", frame_data, "junk" ],
            )):
            sha1_calc = hashlib.sha1()
            for data in seq:
                if data == frame_data:
                    sha1_calc.update(data)
            expected_fingerprint = sha1_calc.hexdigest()

            stream = cStringIO.StringIO(''.join(seq))
            actual_fingerprint = fingerprint.compute(stream)
            self.assertEqual(expected_fingerprint, actual_fingerprint,
                             msg="Case #%d failed" % i)

            self.assertTrue(fingerprint.is_valid(actual_fingerprint))