Skip to content

Commit 3f3327e

Browse files
committed
use type safe assertions
1 parent 8bf962e commit 3f3327e

File tree

6 files changed

+60
-60
lines changed

6 files changed

+60
-60
lines changed

tests/Providers/Qr/IQRCodeProviderTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ public function testTotpUriIsCorrect(): void
2020

2121
$tfa = new TwoFactorAuth('Test&Issuer', 6, 30, Algorithm::Sha1, $qr);
2222
$data = $this->DecodeDataUri($tfa->getQRCodeImageAsDataUri('Test&Label', 'VMR466AB62ZBOKHE'));
23-
$this->assertEquals('test/test', $data['mimetype']);
24-
$this->assertEquals('base64', $data['encoding']);
25-
$this->assertEquals('otpauth://totp/Test%26Label?secret=VMR466AB62ZBOKHE&issuer=Test%26Issuer&period=30&algorithm=SHA1&digits=6@200', $data['data']);
23+
$this->assertSame('test/test', $data['mimetype']);
24+
$this->assertSame('base64', $data['encoding']);
25+
$this->assertSame('otpauth://totp/Test%26Label?secret=VMR466AB62ZBOKHE&issuer=Test%26Issuer&period=30&algorithm=SHA1&digits=6@200', $data['data']);
2626
}
2727

2828
public function testTotpUriIsCorrectNoIssuer(): void
@@ -36,9 +36,9 @@ public function testTotpUriIsCorrectNoIssuer(): void
3636

3737
$tfa = new TwoFactorAuth(null, 6, 30, Algorithm::Sha1, $qr);
3838
$data = $this->DecodeDataUri($tfa->getQRCodeImageAsDataUri('Test&Label', 'VMR466AB62ZBOKHE'));
39-
$this->assertEquals('test/test', $data['mimetype']);
40-
$this->assertEquals('base64', $data['encoding']);
41-
$this->assertEquals('otpauth://totp/Test%26Label?secret=VMR466AB62ZBOKHE&issuer=&period=30&algorithm=SHA1&digits=6@200', $data['data']);
39+
$this->assertSame('test/test', $data['mimetype']);
40+
$this->assertSame('base64', $data['encoding']);
41+
$this->assertSame('otpauth://totp/Test%26Label?secret=VMR466AB62ZBOKHE&issuer=&period=30&algorithm=SHA1&digits=6@200', $data['data']);
4242
}
4343

4444
public function testGetQRCodeImageAsDataUriThrowsOnInvalidSize(): void

tests/Providers/Rng/CSRNGProviderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function testCSRNGProvidersReturnExpectedNumberOfBytes(): void
1919
if (function_exists('random_bytes')) {
2020
$rng = new CSRNGProvider();
2121
foreach ($this->rngTestLengths as $l) {
22-
$this->assertEquals($l, strlen($rng->getRandomBytes($l)));
22+
$this->assertSame($l, strlen($rng->getRandomBytes($l)));
2323
}
2424
$this->assertTrue($rng->isCryptographicallySecure());
2525
} else {

tests/Providers/Rng/HashRNGProviderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function testHashRNGProvidersReturnExpectedNumberOfBytes()
1818
{
1919
$rng = new HashRNGProvider();
2020
foreach ($this->rngTestLengths as $l) {
21-
$this->assertEquals($l, strlen($rng->getRandomBytes($l)));
21+
$this->assertSame($l, strlen($rng->getRandomBytes($l)));
2222
}
2323

2424
$this->assertFalse($rng->isCryptographicallySecure());

tests/Providers/Rng/IRNGProviderTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,27 @@ public function testCreateSecretOverrideSecureDoesNotThrowOnInsecureRNG(): void
2626
$rng = new TestRNGProvider();
2727

2828
$tfa = new TwoFactorAuth('Test', 6, 30, Algorithm::Sha1, null, $rng);
29-
$this->assertEquals('ABCDEFGHIJKLMNOP', $tfa->createSecret(80, false));
29+
$this->assertSame('ABCDEFGHIJKLMNOP', $tfa->createSecret(80, false));
3030
}
3131

3232
public function testCreateSecretDoesNotThrowOnSecureRNGProvider(): void
3333
{
3434
$rng = new TestRNGProvider(true);
3535

3636
$tfa = new TwoFactorAuth('Test', 6, 30, Algorithm::Sha1, null, $rng);
37-
$this->assertEquals('ABCDEFGHIJKLMNOP', $tfa->createSecret());
37+
$this->assertSame('ABCDEFGHIJKLMNOP', $tfa->createSecret());
3838
}
3939

4040
public function testCreateSecretGeneratesDesiredAmountOfEntropy(): void
4141
{
4242
$rng = new TestRNGProvider(true);
4343

4444
$tfa = new TwoFactorAuth('Test', 6, 30, Algorithm::Sha1, null, $rng);
45-
$this->assertEquals('A', $tfa->createSecret(5));
46-
$this->assertEquals('AB', $tfa->createSecret(6));
47-
$this->assertEquals('ABCDEFGHIJKLMNOPQRSTUVWXYZ', $tfa->createSecret(128));
48-
$this->assertEquals('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', $tfa->createSecret(160));
49-
$this->assertEquals('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', $tfa->createSecret(320));
50-
$this->assertEquals('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567ABCDEFGHIJKLMNOPQRSTUVWXYZ234567A', $tfa->createSecret(321));
45+
$this->assertSame('A', $tfa->createSecret(5));
46+
$this->assertSame('AB', $tfa->createSecret(6));
47+
$this->assertSame('ABCDEFGHIJKLMNOPQRSTUVWXYZ', $tfa->createSecret(128));
48+
$this->assertSame('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', $tfa->createSecret(160));
49+
$this->assertSame('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', $tfa->createSecret(320));
50+
$this->assertSame('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567ABCDEFGHIJKLMNOPQRSTUVWXYZ234567A', $tfa->createSecret(321));
5151
}
5252
}

tests/Providers/Rng/OpenSSLRNGProviderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function testStrongOpenSSLRNGProvidersReturnExpectedNumberOfBytes()
1818
{
1919
$rng = new OpenSSLRNGProvider(true);
2020
foreach ($this->rngTestLengths as $l) {
21-
$this->assertEquals($l, strlen($rng->getRandomBytes($l)));
21+
$this->assertSame($l, strlen($rng->getRandomBytes($l)));
2222
}
2323

2424
$this->assertTrue($rng->isCryptographicallySecure());
@@ -31,7 +31,7 @@ public function testNonStrongOpenSSLRNGProvidersReturnExpectedNumberOfBytes()
3131
{
3232
$rng = new OpenSSLRNGProvider(false);
3333
foreach ($this->rngTestLengths as $l) {
34-
$this->assertEquals($l, strlen($rng->getRandomBytes($l)));
34+
$this->assertSame($l, strlen($rng->getRandomBytes($l)));
3535
}
3636

3737
$this->assertFalse($rng->isCryptographicallySecure());

tests/TwoFactorAuthTest.php

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public function testConstructorThrowsOnInvalidPeriod(): void
3131
public function testGetCodeReturnsCorrectResults(): void
3232
{
3333
$tfa = new TwoFactorAuth('Test');
34-
$this->assertEquals('543160', $tfa->getCode('VMR466AB62ZBOKHE', 1426847216));
35-
$this->assertEquals('538532', $tfa->getCode('VMR466AB62ZBOKHE', 0));
34+
$this->assertSame('543160', $tfa->getCode('VMR466AB62ZBOKHE', 1426847216));
35+
$this->assertSame('538532', $tfa->getCode('VMR466AB62ZBOKHE', 0));
3636
}
3737

3838
public function testEnsureAllTimeProvidersReturnCorrectTime(): void
@@ -74,23 +74,23 @@ public function testVerifyCorrectTimeSliceIsReturned(): void
7474
// We test with discrepancy 3 (so total of 7 codes: c-3, c-2, c-1, c, c+1, c+2, c+3
7575
// Ensure each corresponding timeslice is returned correctly
7676
$this->assertTrue($tfa->verifyCode('VMR466AB62ZBOKHE', '534113', 3, 1426847190, $timeslice1));
77-
$this->assertEquals(47561570, $timeslice1);
77+
$this->assertSame(47561570, $timeslice1);
7878
$this->assertTrue($tfa->verifyCode('VMR466AB62ZBOKHE', '819652', 3, 1426847190, $timeslice2));
79-
$this->assertEquals(47561571, $timeslice2);
79+
$this->assertSame(47561571, $timeslice2);
8080
$this->assertTrue($tfa->verifyCode('VMR466AB62ZBOKHE', '915954', 3, 1426847190, $timeslice3));
81-
$this->assertEquals(47561572, $timeslice3);
81+
$this->assertSame(47561572, $timeslice3);
8282
$this->assertTrue($tfa->verifyCode('VMR466AB62ZBOKHE', '543160', 3, 1426847190, $timeslice4));
83-
$this->assertEquals(47561573, $timeslice4);
83+
$this->assertSame(47561573, $timeslice4);
8484
$this->assertTrue($tfa->verifyCode('VMR466AB62ZBOKHE', '348401', 3, 1426847190, $timeslice5));
85-
$this->assertEquals(47561574, $timeslice5);
85+
$this->assertSame(47561574, $timeslice5);
8686
$this->assertTrue($tfa->verifyCode('VMR466AB62ZBOKHE', '648525', 3, 1426847190, $timeslice6));
87-
$this->assertEquals(47561575, $timeslice6);
87+
$this->assertSame(47561575, $timeslice6);
8888
$this->assertTrue($tfa->verifyCode('VMR466AB62ZBOKHE', '170645', 3, 1426847190, $timeslice7));
89-
$this->assertEquals(47561576, $timeslice7);
89+
$this->assertSame(47561576, $timeslice7);
9090

9191
// Incorrect code should return false and a 0 timeslice
9292
$this->assertFalse($tfa->verifyCode('VMR466AB62ZBOKHE', '111111', 3, 1426847190, $timeslice8));
93-
$this->assertEquals(0, $timeslice8);
93+
$this->assertSame(0, $timeslice8);
9494
}
9595

9696
public function testGetCodeThrowsOnInvalidBase32String1(): void
@@ -130,13 +130,13 @@ public function testKnownBase32DecodeTestVectors(): void
130130
$method->setAccessible(true);
131131

132132
// Test vectors from: https://tools.ietf.org/html/rfc4648#page-12
133-
$this->assertEquals('', $method->invoke($tfa, ''));
134-
$this->assertEquals('f', $method->invoke($tfa, 'MY======'));
135-
$this->assertEquals('fo', $method->invoke($tfa, 'MZXQ===='));
136-
$this->assertEquals('foo', $method->invoke($tfa, 'MZXW6==='));
137-
$this->assertEquals('foob', $method->invoke($tfa, 'MZXW6YQ='));
138-
$this->assertEquals('fooba', $method->invoke($tfa, 'MZXW6YTB'));
139-
$this->assertEquals('foobar', $method->invoke($tfa, 'MZXW6YTBOI======'));
133+
$this->assertSame('', $method->invoke($tfa, ''));
134+
$this->assertSame('f', $method->invoke($tfa, 'MY======'));
135+
$this->assertSame('fo', $method->invoke($tfa, 'MZXQ===='));
136+
$this->assertSame('foo', $method->invoke($tfa, 'MZXW6==='));
137+
$this->assertSame('foob', $method->invoke($tfa, 'MZXW6YQ='));
138+
$this->assertSame('fooba', $method->invoke($tfa, 'MZXW6YTB'));
139+
$this->assertSame('foobar', $method->invoke($tfa, 'MZXW6YTBOI======'));
140140
}
141141

142142
public function testKnownBase32DecodeUnpaddedTestVectors(): void
@@ -151,51 +151,51 @@ public function testKnownBase32DecodeUnpaddedTestVectors(): void
151151
$method->setAccessible(true);
152152

153153
// Test vectors from: https://tools.ietf.org/html/rfc4648#page-12
154-
$this->assertEquals('', $method->invoke($tfa, ''));
155-
$this->assertEquals('f', $method->invoke($tfa, 'MY'));
156-
$this->assertEquals('fo', $method->invoke($tfa, 'MZXQ'));
157-
$this->assertEquals('foo', $method->invoke($tfa, 'MZXW6'));
158-
$this->assertEquals('foob', $method->invoke($tfa, 'MZXW6YQ'));
159-
$this->assertEquals('fooba', $method->invoke($tfa, 'MZXW6YTB'));
160-
$this->assertEquals('foobar', $method->invoke($tfa, 'MZXW6YTBOI'));
154+
$this->assertSame('', $method->invoke($tfa, ''));
155+
$this->assertSame('f', $method->invoke($tfa, 'MY'));
156+
$this->assertSame('fo', $method->invoke($tfa, 'MZXQ'));
157+
$this->assertSame('foo', $method->invoke($tfa, 'MZXW6'));
158+
$this->assertSame('foob', $method->invoke($tfa, 'MZXW6YQ'));
159+
$this->assertSame('fooba', $method->invoke($tfa, 'MZXW6YTB'));
160+
$this->assertSame('foobar', $method->invoke($tfa, 'MZXW6YTBOI'));
161161
}
162162

163163
public function testKnownTestVectors_sha1(): void
164164
{
165165
//Known test vectors for SHA1: https://tools.ietf.org/html/rfc6238#page-15
166166
$secret = 'GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ'; //== base32encode('12345678901234567890')
167167
$tfa = new TwoFactorAuth('Test', 8, 30, Algorithm::Sha1);
168-
$this->assertEquals('94287082', $tfa->getCode($secret, 59));
169-
$this->assertEquals('07081804', $tfa->getCode($secret, 1111111109));
170-
$this->assertEquals('14050471', $tfa->getCode($secret, 1111111111));
171-
$this->assertEquals('89005924', $tfa->getCode($secret, 1234567890));
172-
$this->assertEquals('69279037', $tfa->getCode($secret, 2000000000));
173-
$this->assertEquals('65353130', $tfa->getCode($secret, 20000000000));
168+
$this->assertSame('94287082', $tfa->getCode($secret, 59));
169+
$this->assertSame('07081804', $tfa->getCode($secret, 1111111109));
170+
$this->assertSame('14050471', $tfa->getCode($secret, 1111111111));
171+
$this->assertSame('89005924', $tfa->getCode($secret, 1234567890));
172+
$this->assertSame('69279037', $tfa->getCode($secret, 2000000000));
173+
$this->assertSame('65353130', $tfa->getCode($secret, 20000000000));
174174
}
175175

176176
public function testKnownTestVectors_sha256(): void
177177
{
178178
//Known test vectors for SHA256: https://tools.ietf.org/html/rfc6238#page-15
179179
$secret = 'GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZA'; //== base32encode('12345678901234567890123456789012')
180180
$tfa = new TwoFactorAuth('Test', 8, 30, Algorithm::Sha256);
181-
$this->assertEquals('46119246', $tfa->getCode($secret, 59));
182-
$this->assertEquals('68084774', $tfa->getCode($secret, 1111111109));
183-
$this->assertEquals('67062674', $tfa->getCode($secret, 1111111111));
184-
$this->assertEquals('91819424', $tfa->getCode($secret, 1234567890));
185-
$this->assertEquals('90698825', $tfa->getCode($secret, 2000000000));
186-
$this->assertEquals('77737706', $tfa->getCode($secret, 20000000000));
181+
$this->assertSame('46119246', $tfa->getCode($secret, 59));
182+
$this->assertSame('68084774', $tfa->getCode($secret, 1111111109));
183+
$this->assertSame('67062674', $tfa->getCode($secret, 1111111111));
184+
$this->assertSame('91819424', $tfa->getCode($secret, 1234567890));
185+
$this->assertSame('90698825', $tfa->getCode($secret, 2000000000));
186+
$this->assertSame('77737706', $tfa->getCode($secret, 20000000000));
187187
}
188188

189189
public function testKnownTestVectors_sha512(): void
190190
{
191191
//Known test vectors for SHA512: https://tools.ietf.org/html/rfc6238#page-15
192192
$secret = 'GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNA'; //== base32encode('1234567890123456789012345678901234567890123456789012345678901234')
193193
$tfa = new TwoFactorAuth('Test', 8, 30, Algorithm::Sha512);
194-
$this->assertEquals('90693936', $tfa->getCode($secret, 59));
195-
$this->assertEquals('25091201', $tfa->getCode($secret, 1111111109));
196-
$this->assertEquals('99943326', $tfa->getCode($secret, 1111111111));
197-
$this->assertEquals('93441116', $tfa->getCode($secret, 1234567890));
198-
$this->assertEquals('38618901', $tfa->getCode($secret, 2000000000));
199-
$this->assertEquals('47863826', $tfa->getCode($secret, 20000000000));
194+
$this->assertSame('90693936', $tfa->getCode($secret, 59));
195+
$this->assertSame('25091201', $tfa->getCode($secret, 1111111109));
196+
$this->assertSame('99943326', $tfa->getCode($secret, 1111111111));
197+
$this->assertSame('93441116', $tfa->getCode($secret, 1234567890));
198+
$this->assertSame('38618901', $tfa->getCode($secret, 2000000000));
199+
$this->assertSame('47863826', $tfa->getCode($secret, 20000000000));
200200
}
201201
}

0 commit comments

Comments
 (0)