package com.foxx.androidcast.entitlements;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class EntitlementPolicyTest {
    @Test
    public void fileStaleAfterTtl() {
        long now = 500_000_000_000L;
        long fresh = now - EntitlementPolicy.FILE_TTL_MS + 60_000L;
        long stale = now - EntitlementPolicy.FILE_TTL_MS - 60_000L;
        assertFalse(EntitlementPolicy.isFileStale(fresh, now, EntitlementPolicy.FILE_TTL_MS));
        assertTrue(EntitlementPolicy.isFileStale(stale, now, EntitlementPolicy.FILE_TTL_MS));
    }

    @Test
    public void clockRollbackDetected() {
        long lastOk = 5_000_000L;
        long nowOk = lastOk + 1_000L;
        long nowRollback = lastOk - EntitlementPolicy.CLOCK_ROLLBACK_TOLERANCE_MS - 1;
        assertFalse(EntitlementPolicy.isClockRollback(nowOk, lastOk,
                EntitlementPolicy.CLOCK_ROLLBACK_TOLERANCE_MS));
        assertTrue(EntitlementPolicy.isClockRollback(nowRollback, lastOk,
                EntitlementPolicy.CLOCK_ROLLBACK_TOLERANCE_MS));
    }

    @Test
    public void parseKeyHex() {
        assertEquals(0xcafebabedeadbeefL, EntitlementPolicy.parseKeyHex("cafebabedeadbeef"));
        assertEquals(42L, EntitlementPolicy.parseKeyHex("42"));
        assertEquals(0L, EntitlementPolicy.parseKeyHex(""));
        assertEquals(0L, EntitlementPolicy.parseKeyHex(null));
    }

    @Test
    public void devAllPayloadHasBits() {
        assertTrue(EntitlementFlags.hasAnyPaidBit(EntitlementFlags.DEV_ALL_PAYLOAD));
    }
}
