from testtools import TestCase
from struct import unpack


class AdfTest(TestCase):
    def setUp(self):
        super(AdfTest, self).setUp()

    def tearDown(self):
        super(AdfTest, self).tearDown()

    def test_archive(self):
        from lacli.adf import Archive, make_adf
        self.assertEqual(make_adf(Archive('foo', {}), True), ADF_TEST_DATA_1)

    def test_meta(self):
        from lacli.adf import Meta, make_adf
        meta = make_adf(Meta(format='zip', cipher='aes-256-ctr'), True)
        self.assertEqual(meta, ADF_TEST_DATA_2)

    def test_minimal(self):
        from lacli.adf import load_all

        with open('../docs/minimal.adf') as f:
            archive, certificate, _ = load_all(f)
            self.assertEqual(archive.meta.cipher, 'aes-256-ctr')
            b = unpack("<LLLLLLLL", certificate.key)
            self.assertEqual(b[0], 1911376514)

    def test_sample(self):
        from lacli.adf import load_all

        with open('../docs/sample.adf') as f:
            archive, _, certificate, _ = load_all(f)
            self.assertEqual(archive.meta.cipher.mode, 'aes-256-ctr')
            b = unpack("<LLLLLLLL", archive.meta.cipher.input)
            self.assertEqual(b[0], 1911376514)
            b = unpack("<LLLLLLLL", certificate.keys[1].input)
            self.assertEqual(b[0], 1911376514)


ADF_TEST_DATA_1 = """---
!archive {
  ? !!str "description"
  : !!null "null",
  ? !!str "meta"
  : !!map {},
  ? !!str "tags"
  : !!seq [],
  ? !!str "title"
  : !!str "foo",
}
"""

ADF_TEST_DATA_2 = """---
!meta {
  ? !!str "cipher"
  : !!str "aes-256-ctr",
  ? !!str "format"
  : !!str "zip",
}
"""

from testtools import TestCase
from struct import unpack


class AdfTest(TestCase):
    def setUp(self):
        super(AdfTest, self).setUp()

    def tearDown(self):
        super(AdfTest, self).tearDown()

    def test_archive(self):
        from lacli.adf import Archive, make_adf
        self.assertEqual(make_adf(Archive('foo', {}), True), ADF_TEST_DATA_1)

    def test_unicode(self):
        from lacli.adf import Archive, make_adf
        self.assertEqual(make_adf(Archive(u'foo', {}), True), ADF_TEST_DATA_1)

    def test_meta(self):
        from lacli.adf import Meta, make_adf
        meta = make_adf(Meta(format='zip', cipher='aes-256-ctr'), True)
        self.assertEqual(meta, ADF_TEST_DATA_2)

    def test_minimal(self):
        from lacli.adf import load_all

        with open('../docs/minimal.adf') as f:
            archive, certificate, _ = load_all(f)
            self.assertEqual(archive.meta.cipher, 'aes-256-ctr')
            b = unpack("<LLLLLLLL", certificate.key)
            self.assertEqual(b[0], 1911376514)

    def test_sample(self):
        from lacli.adf import load_all

        with open('../docs/sample.adf') as f:
            archive, _, certificate, _ = load_all(f)
            self.assertEqual(archive.meta.cipher.mode, 'aes-256-ctr')
            b = unpack("<LLLLLLLL", archive.meta.cipher.input)
            self.assertEqual(b[0], 1911376514)
            b = unpack("<LLLLLLLL", certificate.keys[1].input)
            self.assertEqual(b[0], 1911376514)


ADF_TEST_DATA_1 = """---
!archive {
  ? !!str "description"
  : !!null "null",
  ? !!str "meta"
  : !!map {},
  ? !!str "tags"
  : !!seq [],
  ? !!str "title"
  : !!str "foo",
}
"""

ADF_TEST_DATA_2 = """---
!meta {
  ? !!str "cipher"
  : !!str "aes-256-ctr",
  ? !!str "format"
  : !!str "zip",
}
"""

add a test for unicode in adf