# PPI_Geotag tests

############
############
+ PPI Geotags tests

= Import PPI Geotag

from scapy.contrib.ppi_geotag import *

= Test GPS dissection

assert raw(GPS()) == b'2u\x08\x00\x02\x00\x08\x00\x00\x00\x00\x00'

= Test Vector dissection

assert raw(Vector()) == b'3u\x08\x00\x02\x00\x08\x00\x00\x00\x00\x00'

= Test Sensor dissection

assert raw(Sensor()) == b'4u\x08\x00\x02\x00\x08\x00\x00\x00\x00\x00'

= Test Antenna dissection

assert raw(Antenna()) == b'5u\x08\x00\x02\x00\x08\x00\x00\x00\x00\x00'

= Test GPSTime_Field time handling

assert GPSTime_Field("GPSTime", None).delta == 0.0

= Define local_to_utc

def local_to_utc(local_time):
    # Let's handle all cases, even with summer time not matching UTC...
    utc_time_clock = time.gmtime(time.mktime(local_time))
    utc_time_clock = list(utc_time_clock.__reduce__()[1][0])
    if local_time.tm_isdst:
        utc_time_clock[3] = (utc_time_clock[3]+1)%24
    return time.struct_time(tuple(utc_time_clock))

= Test UTCTimeField with time values

local_time = time.localtime()
utc_time = UTCTimeField("Test", None, epoch=local_time)
assert time.localtime(utc_time.epoch) == local_time
assert time.mktime(time.gmtime(utc_time.delta)) == time.mktime(local_time)
strft_time = time.strftime("%a, %d %b %Y %H:%M:%S +0000", local_time)
strft_time_sum = time.strftime("%a, %d %b %Y %H:%M:%S +0000", local_to_utc(local_time))

# Backup: also test summer time bug
expected = [(strft_time + " (" + str(int(utc_time.delta)) + ")"), (strft_time_sum + " (" + str(int(utc_time.delta)) + ")")]
result = utc_time.i2repr(None, None)
result
expected
assert result in expected

= Test LETimeField with time values

local_time = time.localtime()
lme_time = LETimeField("Test", None, epoch=local_time)
assert time.localtime(lme_time.epoch) == local_time
assert time.mktime(time.gmtime(lme_time.delta)) == time.mktime(local_time)
strft_time = time.strftime("%a, %d %b %Y %H:%M:%S +0000", local_time)
strft_time_sum = time.strftime("%a, %d %b %Y %H:%M:%S +0000", local_to_utc(local_time))

# Backup: also test summer time bug
expected = [(strft_time + " (" + str(int(lme_time.delta)) + ")"), (strft_time_sum + " (" + str(int(lme_time.delta)) + ")")]
result = lme_time.i2repr(None, None)
result
expected
assert result in expected
