def testEncodeDefault(self):
        obj = Timestamp()
        item = [
            0,
            0,
        ]
        self.assertEqual(self._encode(obj), item)

        self.assertEqual(0, obj.getfieldval('dtntime'))
        self.assertEqual(None, obj.dtntime)
        self.assertEqual(0, obj.seqno)
    def testEncodeParam(self):
        obj = Timestamp(dtntime=1000000, seqno=5)
        item = [
            1000000,
            5,
        ]
        self.assertEqual(self._encode(obj), item)

        self.assertEqual(1000000, obj.getfieldval('dtntime'))
        self.assertEqual(
            datetime.datetime(2000, 1, 1, 0, 16, 40, 0, datetime.timezone.utc),
            obj.dtntime)
        self.assertEqual(5, obj.seqno)
 def testDecodeNofragment(self):
     item = [
         7,
         0,
         2,
         [EidField.TypeCode.dtn, '//dst/'],
         [EidField.TypeCode.dtn, '//src/'],
         [EidField.TypeCode.dtn, '//rpt/'],
         [1000000, 5],
         300,
         None,
     ]
     blk = self._decode(PrimaryBlock, item)
     fields = dict(
         bp_version=7,
         bundle_flags=0,
         crc_type=2,
         destination='dtn://dst/',
         source='dtn://src/',
         report_to='dtn://rpt/',
         create_ts=Timestamp(dtntime=1000000, seqno=5),
         lifetime=300,
         crc_value=None,
     )
     self.assertEqual(blk.fields, fields)
    def testDecodeOnlyPrimary(self):
        item = [
            [
                7,
                0,
                0,
                DTN_NONE,
                DTN_NONE,
                DTN_NONE,
                [1000, 5],
                0,
            ],
        ]
        bdl = self._decode(Bundle, item)

        self.assertIsNotNone(bdl.primary)
        blk = bdl.primary
        fields = dict(
            bp_version=7,
            bundle_flags=0,
            crc_type=0,
            destination='dtn:none',
            source='dtn:none',
            report_to='dtn:none',
            create_ts=Timestamp(dtntime=1000, seqno=5),
            lifetime=0,
        )
        self.assertEqual(blk.fields, fields)

        self.assertEqual(len(bdl.blocks), 0)
    def testDecodePayload(self):
        pyld_data = cbor2.dumps(['some', 'data'])
        item = [
            [
                7,
                0,
                0,
                DTN_NONE,
                DTN_NONE,
                DTN_NONE,
                [0, 0],
                0,
            ],
            [
                1,
                8,
                0,
                0,
                pyld_data,
            ],
        ]
        bdl = self._decode(Bundle, item)

        blk = bdl.primary
        self.assertIsNotNone(blk)
        fields = dict(
            bp_version=7,
            bundle_flags=0,
            crc_type=0,
            destination='dtn:none',
            source='dtn:none',
            report_to='dtn:none',
            create_ts=Timestamp(dtntime=0, seqno=0),
            lifetime=0,
        )
        self.assertEqual(blk.fields, fields)

        self.assertEqual(len(bdl.blocks), 1)
        blk = bdl.blocks[0]
        fields = dict(type_code=1,
                      block_num=8,
                      block_flags=0,
                      crc_type=0,
                      btsd=pyld_data)
        self.assertEqual(blk.fields, fields)
 def testEncodeNofragment(self):
     blk = PrimaryBlock(
         crc_type=2,
         destination='dtn://dst/',
         source='dtn://src/',
         report_to='dtn://rpt/',
         create_ts=Timestamp(dtntime=1000000, seqno=5),
         lifetime=300,
     )
     self.assertEqual(self._encode(blk), [
         7,
         0,
         2,
         [EidField.TypeCode.dtn, '//dst/'],
         [EidField.TypeCode.dtn, '//src/'],
         [EidField.TypeCode.dtn, '//rpt/'],
         [1000000, 5],
         300,
         None,
     ])
 def testEncodeFragment(self):
     blk = PrimaryBlock(
         bundle_flags=PrimaryBlock.Flag.IS_FRAGMENT,
         crc_type=2,
         destination='dtn://dst/',
         source='dtn://src/',
         report_to='dtn://rpt/',
         create_ts=Timestamp(
             dtntime='2000-01-01T00:16:40+00:00',
             seqno=3,
         ),
         lifetime=300,
         fragment_offset=1000,
         total_app_data_len=2000,
     )
     self.assertEqual(self._encode(blk), [
         7, 1, 2, [EidField.TypeCode.dtn, '//dst/'],
         [EidField.TypeCode.dtn, '//src/'], [
             EidField.TypeCode.dtn, '//rpt/'
         ], [1000000, 3], 300, 1000, 2000, None
     ])
 def testDecodeFragment(self):
     item = [
         7, 1, 2, [EidField.TypeCode.dtn,
                   '//dst/'], [EidField.TypeCode.dtn, '//src/'],
         [EidField.TypeCode.dtn, '//rpt/'], [1000000, 3], 300, 1000, 2000,
         None
     ]
     blk = self._decode(PrimaryBlock, item)
     fields = dict(
         bp_version=7,
         bundle_flags=1,
         crc_type=2,
         destination='dtn://dst/',
         source='dtn://src/',
         report_to='dtn://rpt/',
         create_ts=Timestamp(dtntime=1000000, seqno=3),
         lifetime=300,
         fragment_offset=1000,
         total_app_data_len=2000,
         crc_value=None,
     )
     self.assertEqual(blk.fields, fields)