def test_consumer_should_be_able_to_change_segments_encryption(tmpdir):
    m3u8_uri = M3U8_SERVER + '/crypto.m3u8'
    playlist = m3u8.load(m3u8_uri)

    original_dir = tmpdir.join('original')
    hlsclient.consumer.consume(m3u8_uri, str(original_dir))
    playlist.key.key_value = original_dir.join('key.bin').read()

    new_dir = tmpdir.join('new')
    hlsclient.consumer.consume(m3u8_uri, str(new_dir), True)

    original = original_dir.join('encrypted1.ts').read()
    new = new_dir.join('encrypted2.ts').read()
    m3u8_content = new_dir.join('crypto.m3u8').read()

    assert new_dir.join("crypto.bin").check()
    assert 'URI="crypto.bin"' in m3u8_content

    new_key = crypto.get_key_from_disk("crypto.bin", str(new_dir))
    assert crypto.decrypt(original, playlist.key) == crypto.decrypt(new, new_key)
def test_consumer_should_be_able_to_decrypt_segments(tmpdir):
    m3u8_uri = M3U8_SERVER + '/crypto.m3u8'
    playlist = m3u8.load(m3u8_uri)

    encrypted_dir = tmpdir.join('encrypted')
    hlsclient.consumer.consume(m3u8_uri, str(encrypted_dir))
    playlist.key.key_value = encrypted_dir.join('key.bin').read()

    plain_dir = tmpdir.join('plain')
    hlsclient.consumer.consume(m3u8_uri, str(plain_dir), None)

    plain = plain_dir.join('encrypted1.ts').read()
    encrypted = encrypted_dir.join('encrypted2.ts').read()
    m3u8_content = plain_dir.join('crypto.m3u8').read()

    assert plain == crypto.decrypt(encrypted, playlist.key)
    assert "#EXT-X-KEY" not in m3u8_content
def test_consumer_should_be_able_to_encrypt_segments(tmpdir):
    plain_dir = tmpdir.join('plain')
    hlsclient.consumer.consume(M3U8_SERVER + '/low.m3u8', str(plain_dir))

    encrypted_dir = tmpdir.join('encrypted')
    hlsclient.consumer.consume(M3U8_SERVER + '/low.m3u8', str(encrypted_dir), True)

    plain = plain_dir.join('low1.ts').read()
    encrypted = encrypted_dir.join('low1.ts').read()
    m3u8_content = encrypted_dir.join('low.m3u8').read()

    assert encrypted_dir.join("low.bin").check()
    assert 'URI="low.bin"' in m3u8_content
    assert "#EXT-X-VERSION:2" in m3u8_content

    new_key = crypto.get_key_from_disk("low.bin", str(encrypted_dir))
    assert plain == crypto.decrypt(encrypted, new_key)
def test_consumer_should_be_able_to_encrypt_and_decrypt_content(tmpdir):
    content = "blabla"
    fake_key = crypto.get_key("fake_key.bin", str(tmpdir))
    assert content == crypto.decrypt(crypto.encrypt(content, fake_key), fake_key)

    # Generate some big data and try it out
    bigcontent = content * 2 * 1024 + content
    bigcontent = '0123456789abcdef'

    contentf = StringIO.StringIO(bigcontent)
    encryptf = crypto.Encrypt(contentf, fake_key)
    decryptf = crypto.Decrypt(encryptf, fake_key)

    decrypted = ''
    data = decryptf.read(1024)
    while data:
        decrypted += data
        data = decryptf.read(1024)

    assert bigcontent == decrypted
def test_consumer_should_reuse_existant_key(tmpdir):
    plain_dir = tmpdir.join('plain')
    hlsclient.consumer.consume(M3U8_SERVER + '/live/low.m3u8', str(plain_dir))

    encrypted_dir = tmpdir.join('encrypted')

    new_key = crypto.create_key('low.bin')
    os.makedirs(str(encrypted_dir.join('live')))
    crypto.save_new_key(new_key, str(encrypted_dir.join('live')))

    hlsclient.consumer.consume(M3U8_SERVER + '/live/low.m3u8', str(encrypted_dir), True)

    plain = plain_dir.join('live').join('low1.ts').read()
    encrypted = encrypted_dir.join('live').join('low1.ts').read()
    m3u8_content = encrypted_dir.join('live').join('low.m3u8').read()

    assert encrypted_dir.join('live').join("low.bin").check()
    assert 'URI="low.bin"' in m3u8_content
    assert "#EXT-X-VERSION:2" in m3u8_content
    assert plain == crypto.decrypt(encrypted, new_key)