def test_bad_parameters(tmpdir): mar_p = tmpdir.join('test.mar') f = mar_p.open('w+b') with pytest.raises(ValueError): MarWriter(f, productversion='foo') with pytest.raises(ValueError): MarWriter(f, channel='bar') with pytest.raises(ValueError): MarWriter(f, signing_key='SECRET')
def create_mar(source_path, destination, channel_id, product_version=None): # TODO decision path between binary mar and python mar source_path = Path(source_path) destination = Path(destination) if product_version is None: product_version = get_option(source_path, filename="application.ini", section="App", option="Version") files = find_files(source_path) log.info("Creating %s from %s", destination, source_path) with destination.open(mode="w+b") as fh: with MarWriter( fh, productversion=product_version, channel=channel_id, signing_key=None, signing_algorithm=None, ) as m: for f in files: log.debug("Adding %s", f) # TODO we're already compressing these files earlier! do we need to? m.add(f, compress="xz")
def test_signing(tmpdir, key_size, algo_id, test_keys): private_key, public_key = test_keys[key_size] message_p = tmpdir.join('message.txt') message_p.write('hello world') mar_p = tmpdir.join('test.mar') with mar_p.open('w+b') as f: with MarWriter(f, signing_key=private_key, channel='release', productversion='99.9', signing_algorithm=algo_id) as m: with tmpdir.as_cwd(): m.add('message.txt') assert mar_p.size() > 0 with mar_p.open('rb') as f: with MarReader(f) as m: assert m.mardata.additional.count == 1 assert m.mardata.signatures.count == 1 assert len(m.mardata.index.entries) == 1 assert m.mardata.index.entries[0].name == 'message.txt' m.extract(str(tmpdir.join('extracted'))) assert (tmpdir.join('extracted', 'message.txt').read('rb') == b'hello world') assert m.verify(public_key)
def test_padding(tmpdir): """Check that adding a signature preserves the original padding""" message_p = tmpdir.join('message.txt') message_p.write('hello world') def padded_write(self, productversion, channel): self.fileobj.seek(self.additional_offset) extras = extras_header.build( dict( count=1, sections=[ dict( channel=six.u(channel), productversion=six.u(productversion), size=len(channel) + len(productversion) + 2 + 8 + 10, padding=b'\x00' * 10, ) ], )) self.fileobj.write(extras) self.last_offset = self.fileobj.tell() with patch.object(MarWriter, 'write_additional', padded_write): mar_p = tmpdir.join('test.mar') with mar_p.open('w+b') as f: with MarWriter(f, productversion='99.0', channel='1') as m: with tmpdir.as_cwd(): m.add('message.txt', compress='bz2') with mar_p.open('rb') as f: with MarReader(f) as m: assert m.mardata.additional.sections[0].padding == b'\x00' * 10
def test_bad_parameters(tmpdir): mar_p = tmpdir.join('test.mar') f = mar_p.open('w+b') with pytest.raises(ValueError): MarWriter(f, productversion='foo') with pytest.raises(ValueError): MarWriter(f, channel='bar') with pytest.raises(ValueError): MarWriter(f, signing_key='SECRET') with pytest.raises(ValueError): MarWriter(f, signing_algorithm='crc') with pytest.raises(ValueError): message_p = tmpdir.join('message.txt') message_p.write('hello world') with MarWriter(f) as m: with tmpdir.as_cwd(): m.add_file('message.txt', compress='deflate')
def test_addfile_as_dir(tmpdir): message_p = tmpdir.join('message.txt') message_p.write('hello world') mar_p = tmpdir.join('test.mar') with mar_p.open('wb') as f: with MarWriter(f) as m: with tmpdir.as_cwd(): with pytest.raises(ValueError): m.add_dir('message.txt', None)
def test_empty_mar(tmpdir): mar_p = tmpdir.join('test.mar') with mar_p.open('w+b') as f: with MarWriter(f) as m: pass with mar_p.open('rb') as f: with MarReader(f) as m: assert len(m.mardata.index.entries) == 0 assert not m.mardata.signatures
def test_writer_badmode(tmpdir, test_keys): private_key, public_key = test_keys[2048] mar_p = tmpdir.join('test.mar') with mar_p.open('wb') as f: with pytest.raises(ValueError): MarWriter(f, signing_key=private_key, channel='release', productversion='99.9', signing_algorithm='sha1')
def do_create(marfile, files, compress, productversion=None, channel=None, signing_key=None, signing_algorithm=None): """Create a new MAR file.""" with open(marfile, 'w+b') as f: with MarWriter(f, productversion=productversion, channel=channel, signing_key=signing_key, signing_algorithm=signing_algorithm, ) as m: for f in files: m.add(f, compress=compress)
def mar_uu(tmpdir_factory): """Uncompressed and unsigned MAR""" tmpdir = tmpdir_factory.mktemp('data') message_p = tmpdir.join('message.txt') message_p.write('hello world') mar_p = tmpdir.join('test_uu.mar') with mar_p.open('wb') as f: with MarWriter(f) as m: with tmpdir.as_cwd(): m.add('message.txt', compress=None) return mar_p
def mar_cue(tmpdir_factory): """Compressed and unsigned MAR with extra information""" tmpdir = tmpdir_factory.mktemp('data') message_p = tmpdir.join('message.txt') message_p.write('hello world') mar_p = tmpdir.join('test_cue.mar') with mar_p.open('w+b') as f: with MarWriter(f, productversion='99.0', channel='1') as m: with tmpdir.as_cwd(): m.add('message.txt', compress='bz2') return mar_p
def mar_sha384(tmpdir_factory): """MAR signed with SHA384""" tmpdir = tmpdir_factory.mktemp('data') message_p = tmpdir.join('message.txt') message_p.write('hello world') mar_p = tmpdir.join('test_sha384.mar') private_key, public_key = make_rsa_keypair(4096) with mar_p.open('w+b') as f: with MarWriter(f, signing_key=private_key, channel='release', productversion='99.9', signing_algorithm='sha384') as m: with tmpdir.as_cwd(): m.add('message.txt') return mar_p
def test_writer_uncompressed(tmpdir): message_p = tmpdir.join('message.txt') message_p.write('hello world') mar_p = tmpdir.join('test.mar') with mar_p.open('wb') as f: with MarWriter(f) as m: with tmpdir.as_cwd(): m.add('message.txt', compress=None) assert mar_p.size() > 0 with mar_p.open('rb') as f: with MarReader(f) as m: assert m.mardata.additional is None assert m.mardata.signatures is None assert len(m.mardata.index.entries) == 1 assert m.mardata.index.entries[0].name == 'message.txt' m.extract(str(tmpdir.join('extracted'))) assert (tmpdir.join('extracted', 'message.txt').read('rb') == b'hello world')
def test_additional(tmpdir): message_p = tmpdir.join('message.txt') message_p.write('hello world') mar_p = tmpdir.join('test.mar') with mar_p.open('w+b') as f: with MarWriter(f, productversion='99.9', channel='release') as m: with tmpdir.as_cwd(): m.add('message.txt') assert mar_p.size() > 0 with mar_p.open('rb') as f: with MarReader(f) as m: assert m.mardata.additional.count == 1 assert m.mardata.additional.sections[0].productversion == '99.9' assert m.mardata.additional.sections[0].channel == 'release' assert m.mardata.signatures.count == 0 assert len(m.mardata.index.entries) == 1 assert m.mardata.index.entries[0].name == 'message.txt' m.extract(str(tmpdir.join('extracted'))) assert (tmpdir.join('extracted', 'message.txt').read('rb') == b'hello world')