Ejemplo n.º 1
0
    def test_cksum(self):
        with open("/etc/passwd", "rb") as fp:
            datain = fp.read()

        content = self.content()
        content.write(datain)
        with content.open() as fp:
            dataout = fp.read()

        sumin = hashlib.sha256(datain).hexdigest()
        sumout = hashlib.sha256(dataout).hexdigest()

        self.assertEqual(len(datain), len(dataout))
        self.assertEqual(datain, dataout)
        self.assertEqual(sumin, sumout)

        has_size = False
        has_cksum = False
        for key, val in content.headers():
            if key == "Size":
                self.assertFalse(has_size)
                self.assertEqual(len(datain), int(val))
                has_size = True
            if key == "Checksum-SHA256":
                self.assertFalse(has_cksum)
                self.assertEqual(sumin, val)
                has_cksum = True

        self.assertTrue(has_size)
        self.assertTrue(has_cksum)
Ejemplo n.º 2
0
 def test_file(self):
     content = self.content()
     with open("/etc/passwd", "rb") as fp:
         content.write(fp)
     with content.open() as fp:
         with open("/etc/passwd", "rb") as fp2:
             self.assertEqual(fp.read(),fp2.read())
Ejemplo n.º 3
0
 def test_empty(self):
     content = self.content()
     content.write(b'')
     with content.open() as fp:
         data = fp.read()
     self.assertIsInstance(data, bytes)
     self.assertEqual(data, b'')
Ejemplo n.º 4
0
 def test_meta(self):
     content = self.content()
     content.write(b'', meta = [
         ("Foo", "Bar"),
         ("Baz", 4),
         ("Foo2", b"Bar"),
     ])
     for key, val in content.headers():
         self.assertIsInstance(key, str)
         self.assertIsInstance(val, str)
Ejemplo n.º 5
0
    def test_bad_meta(self):
        content = self.content()

        with self.assertRaises(ValueError):
            content.write(b'', meta = [ ("Foo:", "Bar") ])

        with self.assertRaises(ValueError):
            content.write(b'', meta = [ ("Foo\n", "") ])

        with self.assertRaises(ValueError):
            content.write(b'', meta = [ ("Foo", "Bax\n") ])

        with self.assertRaises(ValueError):
            content.write(b'', meta = [ ("Foo", "Bax\n") ])
Ejemplo n.º 6
0
 def test_headers(self):
     content = self.content()
     content.write(b'')
     for key, val in content.headers():
         self.assertIsInstance(key, str)
         self.assertIsInstance(val, str)
Ejemplo n.º 7
0
 def test_too_large(self):
     content = self.content()
     with self.assertRaises(app.content.HeaderTooLarge):
         content.write(b'', meta = [ ("Foo", "Bar" * 999999) ])