Skip to content

Commit

Permalink
tests: stop using deprecated assert fns
Browse files Browse the repository at this point in the history
  • Loading branch information
joonas-fi committed Apr 8, 2023
1 parent 1332822 commit 6f1204d
Show file tree
Hide file tree
Showing 18 changed files with 81 additions and 81 deletions.
2 changes: 1 addition & 1 deletion app/backoff/backoff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestBackoff(t *testing.T) {
for idx, expectation := range expectations {
expectation := expectation // pin
t.Run(fmt.Sprintf("Iteration %d", idx), func(t *testing.T) {
assert.Assert(t, backoffDuration() == expectation)
assert.Equal(t, backoffDuration(), expectation)
})
}
}
12 changes: 6 additions & 6 deletions app/retry/retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestSucceedsRightAway(t *testing.T) {
}, DefaultBackoff(), ifFailed)

assert.Ok(t, err)
assert.Assert(t, attempts == 1)
assert.Equal(t, attempts, 1)
}

func TestSucceedsOnThirdTry(t *testing.T) {
Expand All @@ -49,8 +49,8 @@ func TestSucceedsOnThirdTry(t *testing.T) {
}, DefaultBackoff(), ifFailed)

assert.Ok(t, err)
assert.Assert(t, attempts == 3)
assert.Assert(t, len(receivedErrors) == 2)
assert.Equal(t, attempts, 3)
assert.Equal(t, len(receivedErrors), 2)
// use regex to work around variable timing ("failed in 270ns")
assert.Matches(t, receivedErrors[0].Error(), "attempt 1 failed in .+: fails on first try")
assert.Matches(t, receivedErrors[1].Error(), "attempt 2 failed in .+: fails on second as well")
Expand Down Expand Up @@ -80,8 +80,8 @@ func TestTakesTooLong(t *testing.T) {
}
}, DefaultBackoff(), ifFailed)

assert.Assert(t, attempts == 1)
assert.Assert(t, len(receivedErrors) == 1)
assert.Assert(t, regexp.MustCompile(`GIVING UP \(context timeout\): attempt 1 failed in .+: encountered timeout`).MatchString(receivedErrors[0].Error()))
assert.Equal(t, attempts, 1)
assert.Equal(t, len(receivedErrors), 1)
assert.Equal(t, regexp.MustCompile(`GIVING UP \(context timeout\): attempt 1 failed in .+: encountered timeout`).MatchString(receivedErrors[0].Error()), true)
assert.Equal(t, err.Error(), receivedErrors[0].Error())
}
6 changes: 3 additions & 3 deletions app/throttle/throttle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestBurstThrottler(t *testing.T) {

elapsed := time.Since(started)

assert.Assert(t, timesCalled == 6)
assert.Assert(t, elapsed > 1*interval)
assert.Assert(t, elapsed < 2*interval)
assert.Equal(t, timesCalled, 6)
assert.Equal(t, elapsed > 1*interval, true)
assert.Equal(t, elapsed < 2*interval, true)
}
14 changes: 7 additions & 7 deletions crypto/cryptoutil/cryptorandombytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ import (
)

func TestRandHex(t *testing.T) {
assert.Assert(t, len(RandHex(2)) == 4)
assert.Assert(t, len(RandHex(4)) == 8)
assert.Assert(t, len(RandHex(8)) == 16)
assert.Equal(t, len(RandHex(2)), 4)
assert.Equal(t, len(RandHex(4)), 8)
assert.Equal(t, len(RandHex(8)), 16)
}

func TestBase64Url(t *testing.T) {
// base64 has 33.33 % wasted space
assert.Assert(t, len(RandBase64Url(3)) == 4)
assert.Assert(t, len(RandBase64Url(6)) == 8)
assert.Assert(t, len(RandBase64Url(7)) == 10)
assert.Equal(t, len(RandBase64Url(3)), 4)
assert.Equal(t, len(RandBase64Url(6)), 8)
assert.Equal(t, len(RandBase64Url(7)), 10)
}

func TestRandBase64UrlWithoutLeadingDash(t *testing.T) {
assert.Assert(t, len(RandBase64UrlWithoutLeadingDash(3)) == 4)
assert.Equal(t, len(RandBase64UrlWithoutLeadingDash(3)), 4)
}
4 changes: 2 additions & 2 deletions crypto/cryptoutil/publickeycrypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ aGVsbG8=
func TestParsePemPkcs1EncodedRsaPrivateKey(t *testing.T) {
privKey, err := ParsePemPkcs1EncodedRsaPrivateKey([]byte(testValidRsaPrivateKey))
assert.Ok(t, err)
assert.Assert(t, privKey.E == 65537)
assert.Equal(t, privKey.E, 65537)
}

func TestParsePemPkcs1EncodedRsaPublicKey(t *testing.T) {
pubKey, err := ParsePemPkcs1EncodedRsaPublicKey([]byte(testValidRsaPublicKey))
assert.Ok(t, err)
assert.Assert(t, pubKey.E == 65537)
assert.Equal(t, pubKey.E, 65537)

// invalid PEM
_, err = ParsePemPkcs1EncodedRsaPublicKey([]byte("-----BEGIN RSA PUBLIC KEY-----\nMIIB@\n-----END RSA PUBLIC KEY-----"))
Expand Down
4 changes: 2 additions & 2 deletions crypto/cryptoutil/x509x25519_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ func TestPrivateAndPublicKeyParsing(t *testing.T) {
bobPublic, err := ParsePemPKCS8X25519PublicKey([]byte(bobPublicPemFromOpenssl))
assert.Ok(t, err)

assert.Assert(t, bytes.Equal(bobPublic.Bytes(), bobPrivate.Public().Bytes()))
assert.Equal(t, bytes.Equal(bobPublic.Bytes(), bobPrivate.Public().Bytes()), true)

aliceShared, err := alicePrivate.ECDH(*bobPublic)
assert.Ok(t, err)

bobShared, err := bobPrivate.ECDH(alicePublic)
assert.Ok(t, err)

assert.Assert(t, bytes.Equal(aliceShared, bobShared))
assert.Equal(t, bytes.Equal(aliceShared, bobShared), true)
assert.Equal(t, fmt.Sprintf("%x", aliceShared), "1e9be8d5c01de22df9ccd852c17db8ca9367f72140cc1bbed9bac9e0ee7c1e53")

/*
Expand Down
2 changes: 1 addition & 1 deletion crypto/envelopeenc/rsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func TestEncryptAndDecrypt(t *testing.T) {

nonceLen := 24

assert.Assert(t, len(pwdEnvelope.EncryptedContent)-nonceLen == len("hunter2")+secretbox.Overhead)
assert.Equal(t, len(pwdEnvelope.EncryptedContent)-nonceLen, len("hunter2")+secretbox.Overhead)

decrypted, err := pwdEnvelope.Decrypt(kek1Private)
assert.Ok(t, err)
Expand Down
2 changes: 1 addition & 1 deletion crypto/mac/mac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestAuthenticate(t *testing.T) {
assert.Equal(t, signature, "bo4dqebLiFXPTpqv")

assert.Ok(t, New(key1, msg).Authenticate(signature))
assert.Assert(t, New(key1, msg).Authenticate("wrong signature") == ErrMacValidationFailed)
assert.Equal(t, New(key1, msg).Authenticate("wrong signature"), ErrMacValidationFailed)
}

func TestDifferentMessagesProduceDifferentSignatures(t *testing.T) {
Expand Down
14 changes: 7 additions & 7 deletions crypto/storedpassword/storeandverify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func TestStoreAndVerify(t *testing.T) {
stored, err := Store("hunter2", CurrentBestDerivationStrategy)
assert.Ok(t, err)
assert.Assert(t, len(stored) == 107)
assert.Equal(t, len(stored), 107)

strategyId, _, _, err := deserialize(stored)
assert.Ok(t, err)
Expand All @@ -18,21 +18,21 @@ func TestStoreAndVerify(t *testing.T) {
// pretend above strategy is not found
upgrade, err := Verify(stored, "hunter2", alwaysFailingResolver)
assert.Equal(t, err.Error(), "unknown strategy")
assert.Assert(t, upgrade == "")
assert.Equal(t, upgrade, "")

// strategy should now be found
upgrade, err = Verify(stored, "hunter2", BuiltinStrategies)
assert.Ok(t, err)
assert.Assert(t, upgrade == "")
assert.Equal(t, upgrade, "")

upgrade, err = Verify(stored, "hunter INCORRECT", BuiltinStrategies)
assert.Assert(t, err == ErrIncorrectPassword)
assert.Assert(t, upgrade == "")
assert.Equal(t, err, ErrIncorrectPassword)
assert.Equal(t, upgrade, "")

// Verify() should now suggest upgrade with this resolver
upgrade, err = Verify(stored, "hunter2", downgradingResolver)
assert.Ok(t, err)
assert.Assert(t, upgrade != "")
assert.Equal(t, upgrade != "", true)

// upgraded password should now use the ridiculously insecure strategy
strategyId, _, _, err = deserialize(upgrade)
Expand All @@ -42,7 +42,7 @@ func TestStoreAndVerify(t *testing.T) {
// verify upgraded password
upgrade, err = Verify(upgrade, "hunter2", downgradingResolver)
assert.Ok(t, err)
assert.Assert(t, upgrade == "")
assert.Equal(t, upgrade, "")
}

func alwaysFailingResolver(strategyId string) (DerivationStrategy, DerivationStrategy) {
Expand Down
8 changes: 4 additions & 4 deletions mime/mime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func TestExtensionByType(t *testing.T) {
}

func TestIs(t *testing.T) {
assert.Assert(t, Is("image/jpeg", TypeImage))
assert.Assert(t, Is("image/", TypeImage))
assert.Assert(t, !Is("image", TypeImage))
assert.Assert(t, !Is("text/plain", TypeImage))
assert.Equal(t, Is("image/jpeg", TypeImage), true)
assert.Equal(t, Is("image/", TypeImage), true)
assert.Equal(t, Is("image", TypeImage), false)
assert.Equal(t, Is("text/plain", TypeImage), false)
}
6 changes: 3 additions & 3 deletions net/http/ezhttp/ezhttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,9 @@ func TestErrorIs(t *testing.T) {

_, err := Get(context.TODO(), ts.URL)

assert.Assert(t, ErrorIs(err, http.StatusInternalServerError))
assert.Assert(t, !ErrorIs(err, http.StatusNotModified))
assert.Assert(t, !ErrorIs(nil, http.StatusNotModified))
assert.Equal(t, ErrorIs(err, http.StatusInternalServerError), true)
assert.Equal(t, ErrorIs(err, http.StatusNotModified), false)
assert.Equal(t, ErrorIs(nil, http.StatusNotModified), false)
}

func TestRequestBodyForNonBodyMethods(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions net/http/httpauth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ func TestSignAndAuthenticate(t *testing.T) {
token := signer.Sign(UserDetails{Id: "123"}, time.Now())

bothCookies := ToCookiesWithCsrfProtection(token)
assert.Assert(t, len(bothCookies) == 2)
assert.Equal(t, len(bothCookies), 2)

assert.Equal(t, bothCookies[0].Name, "auth")
assert.Equal(t, bothCookies[0].Value, token)
assert.Assert(t, bothCookies[0].HttpOnly)
assert.Equal(t, bothCookies[0].HttpOnly, true)
assert.Equal(t, bothCookies[1].Name, "csrf_token")
assert.Assert(t, len(bothCookies[1].Value) == 22)
assert.Assert(t, !bothCookies[1].HttpOnly)
assert.Equal(t, len(bothCookies[1].Value), 22)
assert.Equal(t, bothCookies[1].HttpOnly, false)

authenticator, _ := NewEcJwtAuthenticator([]byte(testPublicKey))

Expand Down
2 changes: 1 addition & 1 deletion os/osutil/envvar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestEnvThatExists(t *testing.T) {
val, err := GetenvRequired("PATH")

assert.Ok(t, err)
assert.Assert(t, len(val) > 1)
assert.Equal(t, len(val) > 1, true)
}

func TestGetenvRequiredFromBase64(t *testing.T) {
Expand Down
40 changes: 20 additions & 20 deletions os/osutil/filemode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@ import (
func TestFileMode(t *testing.T) {
// combos

assert.Assert(t, FileMode(OwnerR, GroupRW, OtherRWX) == 0467)
assert.Assert(t, FileMode(OwnerRWX, GroupRWX, OtherRWX) == 0777)
assert.Assert(t, FileMode(OwnerNone, GroupNone, OtherRWX) == 0007)
assert.Equal(t, FileMode(OwnerR, GroupRW, OtherRWX), 0467)
assert.Equal(t, FileMode(OwnerRWX, GroupRWX, OtherRWX), 0777)
assert.Equal(t, FileMode(OwnerNone, GroupNone, OtherRWX), 0007)

// each individually

assert.Assert(t, FileMode(OwnerNone, GroupNone, OtherNone) == 0000)
assert.Assert(t, FileMode(OwnerR, GroupNone, OtherNone) == 0400)
assert.Assert(t, FileMode(OwnerRX, GroupNone, OtherNone) == 0500)
assert.Assert(t, FileMode(OwnerRW, GroupNone, OtherNone) == 0600)
assert.Assert(t, FileMode(OwnerRWX, GroupNone, OtherNone) == 0700)

assert.Assert(t, FileMode(OwnerNone, GroupNone, OtherNone) == 0000)
assert.Assert(t, FileMode(OwnerNone, GroupR, OtherNone) == 0040)
assert.Assert(t, FileMode(OwnerNone, GroupRX, OtherNone) == 0050)
assert.Assert(t, FileMode(OwnerNone, GroupRW, OtherNone) == 0060)
assert.Assert(t, FileMode(OwnerNone, GroupRWX, OtherNone) == 0070)

assert.Assert(t, FileMode(OwnerNone, GroupNone, OtherNone) == 0000)
assert.Assert(t, FileMode(OwnerNone, GroupNone, OtherR) == 0004)
assert.Assert(t, FileMode(OwnerNone, GroupNone, OtherRX) == 0005)
assert.Assert(t, FileMode(OwnerNone, GroupNone, OtherRW) == 0006)
assert.Assert(t, FileMode(OwnerNone, GroupNone, OtherRWX) == 0007)
assert.Equal(t, FileMode(OwnerNone, GroupNone, OtherNone), 0000)
assert.Equal(t, FileMode(OwnerR, GroupNone, OtherNone), 0400)
assert.Equal(t, FileMode(OwnerRX, GroupNone, OtherNone), 0500)
assert.Equal(t, FileMode(OwnerRW, GroupNone, OtherNone), 0600)
assert.Equal(t, FileMode(OwnerRWX, GroupNone, OtherNone), 0700)

assert.Equal(t, FileMode(OwnerNone, GroupNone, OtherNone), 0000)
assert.Equal(t, FileMode(OwnerNone, GroupR, OtherNone), 0040)
assert.Equal(t, FileMode(OwnerNone, GroupRX, OtherNone), 0050)
assert.Equal(t, FileMode(OwnerNone, GroupRW, OtherNone), 0060)
assert.Equal(t, FileMode(OwnerNone, GroupRWX, OtherNone), 0070)

assert.Equal(t, FileMode(OwnerNone, GroupNone, OtherNone), 0000)
assert.Equal(t, FileMode(OwnerNone, GroupNone, OtherR), 0004)
assert.Equal(t, FileMode(OwnerNone, GroupNone, OtherRX), 0005)
assert.Equal(t, FileMode(OwnerNone, GroupNone, OtherRW), 0006)
assert.Equal(t, FileMode(OwnerNone, GroupNone, OtherRWX), 0007)
}
22 changes: 11 additions & 11 deletions sliceutil/sliceutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,32 @@ import (
)

func TestContainsString(t *testing.T) {
assert.Assert(t, ContainsString([]string{"foo", "bar"}, "bar"))
assert.Assert(t, !ContainsString([]string{"foo", "bar"}, "ufo"))
assert.Equal(t, ContainsString([]string{"foo", "bar"}, "bar"), true)
assert.Equal(t, ContainsString([]string{"foo", "bar"}, "ufo"), false)
}

func TestFilterString(t *testing.T) {
result := FilterString([]string{"one", "two", "three", "four", "five"}, func(item string) bool {
return item != "three" && item != "four"
})

assert.Assert(t, len(result) == 3)
assert.Assert(t, result[0] == "one")
assert.Assert(t, result[1] == "two")
assert.Assert(t, result[2] == "five")
assert.Equal(t, len(result), 3)
assert.Equal(t, result[0], "one")
assert.Equal(t, result[1], "two")
assert.Equal(t, result[2], "five")
}

func TestContainsInt(t *testing.T) {
assert.Assert(t, ContainsInt([]int{1, 2}, 2))
assert.Assert(t, !ContainsInt([]int{1, 2}, 3))
assert.Equal(t, ContainsInt([]int{1, 2}, 2), true)
assert.Equal(t, ContainsInt([]int{1, 2}, 3), false)
}

func TestFilterInt(t *testing.T) {
result := FilterInt([]int{1, 2, 3, 4, 5}, func(item int) bool {
return item >= 4
})

assert.Assert(t, len(result) == 2)
assert.Assert(t, result[0] == 4)
assert.Assert(t, result[1] == 5)
assert.Equal(t, len(result), 2)
assert.Equal(t, result[0], 4)
assert.Equal(t, result[1], 5)
}
2 changes: 1 addition & 1 deletion sync/syncutil/concurrently_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ func TestConcurrently(t *testing.T) {
})

assert.Ok(t, err)
assert.Assert(t, sum == 45) // 0+1+2+3+4+5+6+7+8+9
assert.Equal(t, sum, 45) // 0+1+2+3+4+5+6+7+8+9
}
8 changes: 4 additions & 4 deletions sync/syncutil/mutexmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ func TestMutexMap(t *testing.T) {
unlockFoo()

unlockFoo, fooOk := mm.TryLock("foo")
assert.Assert(t, fooOk)
assert.Equal(t, fooOk, true)

_, fooConcurrentOk := mm.TryLock("foo")
assert.Assert(t, !fooConcurrentOk)
assert.Equal(t, fooConcurrentOk, false)

unlockFoo()

unlockFoo, fooOk = mm.TryLock("foo")
assert.Assert(t, fooOk)
assert.Equal(t, fooOk, true)

lockAcquireDuration := make(chan time.Duration)

Expand All @@ -39,5 +39,5 @@ func TestMutexMap(t *testing.T) {

unlockFoo()

assert.Assert(t, <-lockAcquireDuration > 10*time.Millisecond)
assert.Equal(t, <-lockAcquireDuration > 10*time.Millisecond, true)
}
6 changes: 3 additions & 3 deletions sync/taskrunner/taskrunner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestWaitThreeTasks(t *testing.T) {
cancel()

assert.Ok(t, runner.Wait())
assert.Assert(t, time.Since(taskStarted) > 69*time.Millisecond)
assert.Equal(t, time.Since(taskStarted) > 69*time.Millisecond, true)
}

func TestCancellationStopsTask(t *testing.T) {
Expand All @@ -62,7 +62,7 @@ func TestCancellationStopsTask(t *testing.T) {

assert.Ok(t, <-runner.Done())

assert.Assert(t, time.Since(taskStarted) > 59*time.Millisecond)
assert.Equal(t, time.Since(taskStarted) > 59*time.Millisecond, true)
}

func TestStoppingFails(t *testing.T) {
Expand Down Expand Up @@ -96,5 +96,5 @@ func TestTaskErrorShouldStopSiblings(t *testing.T) {
})

assert.Equal(t, runner.Wait().Error(), "unexpected exit of fails: i fail immediately")
assert.Assert(t, correctlyWorkingSiblingAlsoStopped)
assert.Equal(t, correctlyWorkingSiblingAlsoStopped, true)
}

0 comments on commit 6f1204d

Please sign in to comment.