Ejemplo n.º 1
0
    def test_stream_append_to_existing(self):
        s = stream.Stream()
        public_key, private_key = crypto.make_key_pair()
        s.set_stream_signature_keys(public_key, private_key)
        s.create_with_handle(self.filehandle)
        s.append(b"1")
        s.append(b"2")
        s.append(b"3")
        s.close()

        s = stream.Stream(self.filehandle.name, append=True)
        try:
            s.verify()
            self.assertEquals(s.state, stream.Stream.STATE_VERIFIED)
        except (stream.InvalidKeyPair, stream.StreamCorrupt, stream.HashFailure, stream.SignatureFailure):
            self.fail("Stream failed to verify")
            return

        s.set_stream_signature_keys(public_key, private_key)
        self.assertEquals(s.state, stream.Stream.STATE_WRITE_VERIFIED)
        s.append(b"4")
        s.close()

        s = stream.Stream(self.filehandle.name)
        self.assertEqual(s.verify(), 5)
        s.close()
Ejemplo n.º 2
0
    def test_file_existence_override(self):

        s = stream.Stream()
        s.set_stream_signature_keys(crypto.make_key_pair())
        try:
            s.create(self.filehandle.name, force=True)
        except IOError as err:
            self.fail("Force overwrite file does not throw the expected exception.")
            return
Ejemplo n.º 3
0
    def test_stream_append_while_not_at_end(self):

        s = stream.Stream()
        s.set_stream_signature_keys(crypto.make_key_pair())
        s.create_with_handle(self.filehandle)
        s.append(b"1")
        s.append(b"2")
        s.read(1)
        s.append(b"3")
        s.close()
Ejemplo n.º 4
0
    def setUp(self):
        self.dir = mkdtemp()
        app.config['STREAM_DIR'] = self.dir
        ServerTestCase.setUp(self)

        s = stream.Stream()
        self.public_key, self.private_key = crypto.make_key_pair()
        s.set_stream_signature_keys(self.public_key, self.private_key)
        self.uuid = s.uuid
        s.create(os.path.join(self.dir, "%s.iw" % self.uuid))
        s.append(b"1")
        s.append(b"2")
        s.append(b"3")
        s.close()
Ejemplo n.º 5
0
    def test_stream_id(self):

        s = stream.Stream()
        first_uuid = s.uuid
        s.set_stream_signature_keys(crypto.make_key_pair())
        s.create_with_handle(self.filehandle)
        self.assertEqual(len(s.uuid), 32)
        self.assertNotEqual(first_uuid, s.uuid)
        s.close()

        s = stream.Stream(self.filehandle.name)
        s.verify()
        self.assertEqual(len(s.uuid), 32)
        s.close()
Ejemplo n.º 6
0
    def test_file_content_keys(self):

        with TemporaryDirectory() as tmpdirname:
            public_key, private_key = crypto.make_key_pair()
            with open(os.path.join(tmpdirname, "__test-public.pem"),
                      "wb") as f:
                f.write(crypto.get_public_key_pem(public_key))
            with open(os.path.join(tmpdirname, "__test-private.pem"),
                      "wb") as f:
                f.write(crypto.get_private_key_pem(private_key))

            s = stream.Stream()
            s.set_stream_content_keys_filename(
                os.path.join(tmpdirname, "__test-public.pem"),
                os.path.join(tmpdirname, "__test-private.pem"))
Ejemplo n.º 7
0
    def test_block_seek(self):
        s = stream.Stream()
        s.set_stream_signature_keys(crypto.make_key_pair())
        s.create_with_handle(self.filehandle)
        s.append(b"1")
        s.append(b"2")
        s.append(b"3")
        s.close()

        s = stream.Stream(self.filehandle.name)
        self.assertEqual(s.read(1)[1], b"1")
        self.assertEqual(s.read(2)[1], b"2")
        self.assertEqual(s.read(3)[1], b"3")
        self.assertEqual(s.read(2)[1], b"2")
        self.assertEqual(s.read(3)[1], b"3")
        s.close()
Ejemplo n.º 8
0
    def test_create_and_verify(self):
        blocks = [b"random", b"crap"]
        s = stream.Stream()
        s.set_stream_signature_keys(crypto.make_key_pair())
        s.create_with_handle(self.filehandle)
        for b in blocks:
            s.append(content=b)
        s.close()

        reads = []
        with stream.Stream(self.filehandle.name) as s:
            block_index = 1
            while True:
                metadata, content = s.read(block_index)
                if not content:
                    break

                reads.append(content)
                block_index += 1

        assert_equals(blocks, reads)
Ejemplo n.º 9
0
def make_keys(force, base_filename):

    public_key, private_key = crypto.make_key_pair()
    pem = crypto.get_private_key_pem(private_key)

    private_filename = base_filename + "-private.pem"
    public_filename = base_filename + "-public.pem"
    if os.path.exists(private_filename) and not force:
        print("key %s exists. Use --force to overwrite." % private_filename)
        return

    if os.path.exists(public_filename) and not force:
        print("key %s exists. Use --force to overwrite." % public_filename)
        return

    with open(base_filename + "-private.pem", "w") as f:
        f.write(pem.decode("utf-8")),

    pem = crypto.get_public_key_pem(public_key)
    with open(base_filename + "-public.pem", "w") as f:
        f.write(pem.decode("utf-8")),
Ejemplo n.º 10
0
    def test_metadata(self):

        metadata = {'foo': 'bar'}
        metadata2 = {'foo': 'bar'}

        s = stream.Stream()
        s.set_stream_signature_keys(crypto.make_key_pair())
        s.create_with_handle(self.filehandle)
        s.append(b"1")
        s.append(b"2", metadata2)
        s.append(b"3")
        s.close()

        s = stream.Stream(self.filehandle.name, append=True)
        s.verify()
        (read_metadata, content) = s.read(0)
        if stream.MANIFEST_METADATA_STREAM_SIGNATURE_PUBLIC_KEY not in read_metadata:
            self.fail(
                "Stream signature public key not found in manifest data.")

        (read_metadata, content) = s.read(2)
        self.assertEqual(read_metadata, metadata2)
        s.close()
Ejemplo n.º 11
0
 def test_file_existence(self):
     s = stream.Stream()
     s.set_stream_signature_keys(crypto.make_key_pair())
     self.assertRaises(IOError, s.create, self.filehandle.name)
Ejemplo n.º 12
0
 def test_mismatched_keys(self):
     s = stream.Stream()
     public_key, private_key = crypto.make_key_pair()
     public_key2, private_key2 = crypto.make_key_pair()
     self.assertRaises(exc.InvalidKeyPair, s.set_stream_signature_keys,
                       public_key2, private_key)