Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compatibility tests from spec #7

Merged
merged 1 commit into from
Feb 2, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 73 additions & 11 deletions tests/BrancaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,79 @@ public function testShouldBeTrue()
$this->assertTrue(true);
}

/* These are the tests each implementation should have. */
public function testShouldHaveHelloWorldWithZeroTimestamp()
{
$token = "870S4BYjk7NvyViEjUNsTEmGXbARAX9PamXZg0b3JyeIdGyZkFJhNsOQW6m0K9KnXt3ZUBqDB6hF4";
$branca = new Branca("supersecretkeyyoushouldnotcommit");
$decoded = $branca->decode($token);

$this->assertEquals("Hello world!", $decoded);
$this->assertEquals(0, $branca->timestamp($token));
}

public function testShouldHaveHelloWorldWithMaxTimestamp()
{
$token = "89i7YCwtsSiYfXvOKlgkCyElnGCOEYG7zLCjUp4MuDIZGbkKJgt79Sts9RdW2Yo4imonXsILmqtNb";
$branca = new Branca("supersecretkeyyoushouldnotcommit");
$decoded = $branca->decode($token);

$this->assertEquals("Hello world!", $decoded);
$this->assertEquals(4294967295, $branca->timestamp($token));
}

public function testShouldHaveHelloWorldWithNov27Timestamp()
{
$token = "875GH234UdXU6PkYq8g7tIM80XapDQOH72bU48YJ7SK1iHiLkrqT8Mly7P59TebOxCyQeqpMJ0a7a";
$branca = new Branca("supersecretkeyyoushouldnotcommit");
$decoded = $branca->decode($token);

$this->assertEquals("Hello world!", $decoded);
$this->assertEquals(123206400, $branca->timestamp($token));
}

public function testShouldHaveEightNullBytesWithZeroTimestamp()
{
$token = "1jIBheHWEwYIP59Wpm4QkjkIKuhc12NcYdp9Y60B6av7sZc3vJ5wBwmKJyQzGfJCrvuBgGnf";
$branca = new Branca("supersecretkeyyoushouldnotcommit");
$decoded = $branca->decode($token);

$this->assertEquals("0000000000000000", bin2hex($decoded));
$this->assertEquals(0, $branca->timestamp($token));
}

public function testShouldHaveEightNullBytesWithMaxTimestamp()
{
$token = "1jrx6DUq9HmXvYdmhWMhXzx3klRzhlAjsc3tUFxDPCvZZLm16GYOzsBG4KwF1djjW1yTeZ2B";
$branca = new Branca("supersecretkeyyoushouldnotcommit");
$decoded = $branca->decode($token);

$this->assertEquals("0000000000000000", bin2hex($decoded));
$this->assertEquals(4294967295, $branca->timestamp($token));
}

public function testShouldHaveEightNullBytesWithNov27Timestamp()
{
$token = "1jJDJOEfuc4uBJh5ivaadjo6UaBZJDZ1NsWixVCz2mXw3824JRDQZIgflRqCNKz6yC7a0JKC";
$branca = new Branca("supersecretkeyyoushouldnotcommit");
$decoded = $branca->decode($token);

$this->assertEquals("0000000000000000", bin2hex($decoded));
$this->assertEquals(123206400, $branca->timestamp($token));
}

public function testShouldThrowWithWrongVersion()
{
$this->expectException(\RuntimeException::class);

/* This token has version 0xBB. */
$token = "89mvl3RkwXjpEj5WMxK7GUDEHEeeeZtwjMIOogTthvr44qBfYtQSIZH5MHOTC0GzoutDIeoPVZk3w";

$branca = new Branca("supersecretkeyyoushouldnotcommit");
$decoded = $branca->decode($token);
}

/* These are the PHP implementation specific tests. */
public function testShouldCreateTokenWithTimestamp()
{
$timestamp = 123206400;
Expand All @@ -72,17 +145,6 @@ public function testShouldCreateTokenWithZeroTimestamp()
$this->assertEquals($timestamp, $branca->timestamp($token));
}

public function testShouldThrowWithWrongVersion()
{
$this->expectException(\RuntimeException::class);

/* This token has version 0xBB. */
$token = "89mvl3RZe7RwH2x4azVg5V2B7X2NtG4V2YLxHAB3oFc6gyeICmCKAOCQ7Y0n08klY33eQWACd7cSZ";

$branca = new Branca("supersecretkeyyoushouldnotcommit");
$decoded = $branca->decode($token);
}

public function testShouldThrowWhenTtlExpired()
{
$this->expectException(\RuntimeException::class);
Expand Down