Skip to content

Commit 4130d88

Browse files
committed
Merge remote-tracking branch 'zeripath/full-gc-lfs' into full-gc-lfs
2 parents cc31e60 + 3a3ac24 commit 4130d88

File tree

23 files changed

+277
-168
lines changed

23 files changed

+277
-168
lines changed

custom/conf/app.example.ini

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2378,33 +2378,33 @@ ROUTER = console
23782378
;; Path for chunked uploads. Defaults to APP_DATA_PATH + `tmp/package-upload`
23792379
;CHUNKED_UPLOAD_PATH = tmp/package-upload
23802380
;;
2381-
;; Maxmimum count of package versions a single owner can have (`-1` means no limits)
2381+
;; Maximum count of package versions a single owner can have (`-1` means no limits)
23822382
;LIMIT_TOTAL_OWNER_COUNT = -1
2383-
;; Maxmimum size of packages a single owner can use (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
2383+
;; Maximum size of packages a single owner can use (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
23842384
;LIMIT_TOTAL_OWNER_SIZE = -1
2385-
;; Maxmimum size of a Composer upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
2385+
;; Maximum size of a Composer upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
23862386
;LIMIT_SIZE_COMPOSER = -1
2387-
;; Maxmimum size of a Conan upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
2387+
;; Maximum size of a Conan upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
23882388
;LIMIT_SIZE_CONAN = -1
2389-
;; Maxmimum size of a Container upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
2389+
;; Maximum size of a Container upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
23902390
;LIMIT_SIZE_CONTAINER = -1
2391-
;; Maxmimum size of a Generic upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
2391+
;; Maximum size of a Generic upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
23922392
;LIMIT_SIZE_GENERIC = -1
2393-
;; Maxmimum size of a Helm upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
2393+
;; Maximum size of a Helm upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
23942394
;LIMIT_SIZE_HELM = -1
2395-
;; Maxmimum size of a Maven upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
2395+
;; Maximum size of a Maven upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
23962396
;LIMIT_SIZE_MAVEN = -1
2397-
;; Maxmimum size of a npm upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
2397+
;; Maximum size of a npm upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
23982398
;LIMIT_SIZE_NPM = -1
2399-
;; Maxmimum size of a NuGet upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
2399+
;; Maximum size of a NuGet upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
24002400
;LIMIT_SIZE_NUGET = -1
2401-
;; Maxmimum size of a Pub upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
2401+
;; Maximum size of a Pub upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
24022402
;LIMIT_SIZE_PUB = -1
2403-
;; Maxmimum size of a PyPI upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
2403+
;; Maximum size of a PyPI upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
24042404
;LIMIT_SIZE_PYPI = -1
2405-
;; Maxmimum size of a RubyGems upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
2405+
;; Maximum size of a RubyGems upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
24062406
;LIMIT_SIZE_RUBYGEMS = -1
2407-
;; Maxmimum size of a Vagrant upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
2407+
;; Maximum size of a Vagrant upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
24082408
;LIMIT_SIZE_VAGRANT = -1
24092409

24102410
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

docs/content/doc/advanced/config-cheat-sheet.en-us.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,20 +1179,20 @@ Task queue configuration has been moved to `queue.task`. However, the below conf
11791179

11801180
- `ENABLED`: **true**: Enable/Disable package registry capabilities
11811181
- `CHUNKED_UPLOAD_PATH`: **tmp/package-upload**: Path for chunked uploads. Defaults to `APP_DATA_PATH` + `tmp/package-upload`
1182-
- `LIMIT_TOTAL_OWNER_COUNT`: **-1**: Maxmimum count of package versions a single owner can have (`-1` means no limits)
1183-
- `LIMIT_TOTAL_OWNER_SIZE`: **-1**: Maxmimum size of packages a single owner can use (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
1184-
- `LIMIT_SIZE_COMPOSER`: **-1**: Maxmimum size of a Composer upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
1185-
- `LIMIT_SIZE_CONAN`: **-1**: Maxmimum size of a Conan upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
1186-
- `LIMIT_SIZE_CONTAINER`: **-1**: Maxmimum size of a Container upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
1187-
- `LIMIT_SIZE_GENERIC`: **-1**: Maxmimum size of a Generic upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
1188-
- `LIMIT_SIZE_HELM`: **-1**: Maxmimum size of a Helm upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
1189-
- `LIMIT_SIZE_MAVEN`: **-1**: Maxmimum size of a Maven upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
1190-
- `LIMIT_SIZE_NPM`: **-1**: Maxmimum size of a npm upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
1191-
- `LIMIT_SIZE_NUGET`: **-1**: Maxmimum size of a NuGet upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
1192-
- `LIMIT_SIZE_PUB`: **-1**: Maxmimum size of a Pub upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
1193-
- `LIMIT_SIZE_PYPI`: **-1**: Maxmimum size of a PyPI upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
1194-
- `LIMIT_SIZE_RUBYGEMS`: **-1**: Maxmimum size of a RubyGems upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
1195-
- `LIMIT_SIZE_VAGRANT`: **-1**: Maxmimum size of a Vagrant upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
1182+
- `LIMIT_TOTAL_OWNER_COUNT`: **-1**: Maximum count of package versions a single owner can have (`-1` means no limits)
1183+
- `LIMIT_TOTAL_OWNER_SIZE`: **-1**: Maximum size of packages a single owner can use (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
1184+
- `LIMIT_SIZE_COMPOSER`: **-1**: Maximum size of a Composer upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
1185+
- `LIMIT_SIZE_CONAN`: **-1**: Maximum size of a Conan upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
1186+
- `LIMIT_SIZE_CONTAINER`: **-1**: Maximum size of a Container upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
1187+
- `LIMIT_SIZE_GENERIC`: **-1**: Maximum size of a Generic upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
1188+
- `LIMIT_SIZE_HELM`: **-1**: Maximum size of a Helm upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
1189+
- `LIMIT_SIZE_MAVEN`: **-1**: Maximum size of a Maven upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
1190+
- `LIMIT_SIZE_NPM`: **-1**: Maximum size of a npm upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
1191+
- `LIMIT_SIZE_NUGET`: **-1**: Maximum size of a NuGet upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
1192+
- `LIMIT_SIZE_PUB`: **-1**: Maximum size of a Pub upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
1193+
- `LIMIT_SIZE_PYPI`: **-1**: Maximum size of a PyPI upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
1194+
- `LIMIT_SIZE_RUBYGEMS`: **-1**: Maximum size of a RubyGems upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
1195+
- `LIMIT_SIZE_VAGRANT`: **-1**: Maximum size of a Vagrant upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
11961196

11971197
## Mirror (`mirror`)
11981198

models/auth/token.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@ package auth
66

77
import (
88
"crypto/subtle"
9+
"encoding/hex"
910
"fmt"
1011
"time"
1112

1213
"code.gitea.io/gitea/models/db"
13-
"code.gitea.io/gitea/modules/base"
1414
"code.gitea.io/gitea/modules/setting"
1515
"code.gitea.io/gitea/modules/timeutil"
1616
"code.gitea.io/gitea/modules/util"
1717

18-
gouuid "github.com/google/uuid"
1918
lru "github.com/hashicorp/golang-lru"
2019
)
2120

@@ -100,8 +99,12 @@ func NewAccessToken(t *AccessToken) error {
10099
if err != nil {
101100
return err
102101
}
102+
token, err := util.CryptoRandomBytes(20)
103+
if err != nil {
104+
return err
105+
}
103106
t.TokenSalt = salt
104-
t.Token = base.EncodeSha1(gouuid.New().String())
107+
t.Token = hex.EncodeToString(token)
105108
t.TokenHash = HashToken(t.Token, t.TokenSalt)
106109
t.TokenLastEight = t.Token[len(t.Token)-8:]
107110
_, err = db.GetEngine(db.DefaultContext).Insert(t)

models/auth/twofactor.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"crypto/subtle"
1010
"encoding/base32"
1111
"encoding/base64"
12+
"encoding/hex"
1213
"fmt"
1314

1415
"code.gitea.io/gitea/models/db"
@@ -78,7 +79,7 @@ func (t *TwoFactor) GenerateScratchToken() (string, error) {
7879
// HashToken return the hashable salt
7980
func HashToken(token, salt string) string {
8081
tempHash := pbkdf2.Key([]byte(token), []byte(salt), 10000, 50, sha256.New)
81-
return fmt.Sprintf("%x", tempHash)
82+
return hex.EncodeToString(tempHash)
8283
}
8384

8485
// VerifyScratchToken verifies if the specified scratch token is valid.

models/db/index.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ var (
2323
ErrGetResourceIndexFailed = errors.New("get resource index failed")
2424
)
2525

26-
const (
27-
// MaxDupIndexAttempts max retry times to create index
28-
MaxDupIndexAttempts = 3
29-
)
30-
3126
// SyncMaxResourceIndex sync the max index with the resource
3227
func SyncMaxResourceIndex(ctx context.Context, tableName string, groupID, maxIndex int64) (err error) {
3328
e := GetEngine(ctx)

models/git/commit_status.go

Lines changed: 36 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package git
66
import (
77
"context"
88
"crypto/sha1"
9+
"errors"
910
"fmt"
1011
"net/url"
1112
"strings"
@@ -48,79 +49,49 @@ func init() {
4849
db.RegisterModel(new(CommitStatusIndex))
4950
}
5051

51-
// upsertCommitStatusIndex the function will not return until it acquires the lock or receives an error.
52-
func upsertCommitStatusIndex(ctx context.Context, repoID int64, sha string) (err error) {
53-
// An atomic UPSERT operation (INSERT/UPDATE) is the only operation
54-
// that ensures that the key is actually locked.
55-
switch {
56-
case setting.Database.UseSQLite3 || setting.Database.UsePostgreSQL:
57-
_, err = db.Exec(ctx, "INSERT INTO `commit_status_index` (repo_id, sha, max_index) "+
58-
"VALUES (?,?,1) ON CONFLICT (repo_id,sha) DO UPDATE SET max_index = `commit_status_index`.max_index+1",
59-
repoID, sha)
60-
case setting.Database.UseMySQL:
61-
_, err = db.Exec(ctx, "INSERT INTO `commit_status_index` (repo_id, sha, max_index) "+
62-
"VALUES (?,?,1) ON DUPLICATE KEY UPDATE max_index = max_index+1",
63-
repoID, sha)
64-
case setting.Database.UseMSSQL:
65-
// https://weblogs.sqlteam.com/dang/2009/01/31/upsert-race-condition-with-merge/
66-
_, err = db.Exec(ctx, "MERGE `commit_status_index` WITH (HOLDLOCK) as target "+
67-
"USING (SELECT ? AS repo_id, ? AS sha) AS src "+
68-
"ON src.repo_id = target.repo_id AND src.sha = target.sha "+
69-
"WHEN MATCHED THEN UPDATE SET target.max_index = target.max_index+1 "+
70-
"WHEN NOT MATCHED THEN INSERT (repo_id, sha, max_index) "+
71-
"VALUES (src.repo_id, src.sha, 1);",
72-
repoID, sha)
73-
default:
74-
return fmt.Errorf("database type not supported")
75-
}
76-
return err
77-
}
78-
7952
// GetNextCommitStatusIndex retried 3 times to generate a resource index
80-
func GetNextCommitStatusIndex(repoID int64, sha string) (int64, error) {
81-
for i := 0; i < db.MaxDupIndexAttempts; i++ {
82-
idx, err := getNextCommitStatusIndex(repoID, sha)
83-
if err == db.ErrResouceOutdated {
84-
continue
85-
}
86-
if err != nil {
87-
return 0, err
88-
}
89-
return idx, nil
90-
}
91-
return 0, db.ErrGetResourceIndexFailed
92-
}
53+
func GetNextCommitStatusIndex(ctx context.Context, repoID int64, sha string) (int64, error) {
54+
e := db.GetEngine(ctx)
9355

94-
// getNextCommitStatusIndex return the next index
95-
func getNextCommitStatusIndex(repoID int64, sha string) (int64, error) {
96-
ctx, commiter, err := db.TxContext(db.DefaultContext)
56+
// try to update the max_index to next value, and acquire the write-lock for the record
57+
res, err := e.Exec("UPDATE `commit_status_index` SET max_index=max_index+1 WHERE repo_id=? AND sha=?", repoID, sha)
9758
if err != nil {
9859
return 0, err
9960
}
100-
defer commiter.Close()
101-
102-
var preIdx int64
103-
_, err = db.GetEngine(ctx).SQL("SELECT max_index FROM `commit_status_index` WHERE repo_id = ? AND sha = ?", repoID, sha).Get(&preIdx)
61+
affected, err := res.RowsAffected()
10462
if err != nil {
10563
return 0, err
10664
}
107-
108-
if err := upsertCommitStatusIndex(ctx, repoID, sha); err != nil {
109-
return 0, err
65+
if affected == 0 {
66+
// this slow path is only for the first time of creating a resource index
67+
_, errIns := e.Exec("INSERT INTO `commit_status_index` (repo_id, sha, max_index) VALUES (?, ?, 0)", repoID, sha)
68+
res, err = e.Exec("UPDATE `commit_status_index` SET max_index=max_index+1 WHERE repo_id=? AND sha=?", repoID, sha)
69+
if err != nil {
70+
return 0, err
71+
}
72+
affected, err = res.RowsAffected()
73+
if err != nil {
74+
return 0, err
75+
}
76+
// if the update still can not update any records, the record must not exist and there must be some errors (insert error)
77+
if affected == 0 {
78+
if errIns == nil {
79+
return 0, errors.New("impossible error when GetNextCommitStatusIndex, insert and update both succeeded but no record is updated")
80+
}
81+
return 0, errIns
82+
}
11083
}
11184

112-
var curIdx int64
113-
has, err := db.GetEngine(ctx).SQL("SELECT max_index FROM `commit_status_index` WHERE repo_id = ? AND sha = ? AND max_index=?", repoID, sha, preIdx+1).Get(&curIdx)
85+
// now, the new index is in database (protected by the transaction and write-lock)
86+
var newIdx int64
87+
has, err := e.SQL("SELECT max_index FROM `commit_status_index` WHERE repo_id=? AND sha=?", repoID, sha).Get(&newIdx)
11488
if err != nil {
11589
return 0, err
11690
}
11791
if !has {
118-
return 0, db.ErrResouceOutdated
92+
return 0, errors.New("impossible error when GetNextCommitStatusIndex, upsert succeeded but no record can be selected")
11993
}
120-
if err := commiter.Commit(); err != nil {
121-
return 0, err
122-
}
123-
return curIdx, nil
94+
return newIdx, nil
12495
}
12596

12697
func (status *CommitStatus) loadAttributes(ctx context.Context) (err error) {
@@ -290,18 +261,18 @@ func NewCommitStatus(opts NewCommitStatusOptions) error {
290261
return fmt.Errorf("NewCommitStatus[%s, %s]: no user specified", repoPath, opts.SHA)
291262
}
292263

293-
// Get the next Status Index
294-
idx, err := GetNextCommitStatusIndex(opts.Repo.ID, opts.SHA)
295-
if err != nil {
296-
return fmt.Errorf("generate commit status index failed: %w", err)
297-
}
298-
299264
ctx, committer, err := db.TxContext(db.DefaultContext)
300265
if err != nil {
301266
return fmt.Errorf("NewCommitStatus[repo_id: %d, user_id: %d, sha: %s]: %w", opts.Repo.ID, opts.Creator.ID, opts.SHA, err)
302267
}
303268
defer committer.Close()
304269

270+
// Get the next Status Index
271+
idx, err := GetNextCommitStatusIndex(ctx, opts.Repo.ID, opts.SHA)
272+
if err != nil {
273+
return fmt.Errorf("generate commit status index failed: %w", err)
274+
}
275+
305276
opts.CommitStatus.Description = strings.TrimSpace(opts.CommitStatus.Description)
306277
opts.CommitStatus.Context = strings.TrimSpace(opts.CommitStatus.Context)
307278
opts.CommitStatus.TargetURL = strings.TrimSpace(opts.CommitStatus.TargetURL)
@@ -315,7 +286,7 @@ func NewCommitStatus(opts NewCommitStatusOptions) error {
315286

316287
// Insert new CommitStatus
317288
if _, err = db.GetEngine(ctx).Insert(opts.CommitStatus); err != nil {
318-
return fmt.Errorf("Insert CommitStatus[%s, %s]: %w", repoPath, opts.SHA, err)
289+
return fmt.Errorf("insert CommitStatus[%s, %s]: %w", repoPath, opts.SHA, err)
319290
}
320291

321292
return committer.Commit()

models/migrations/base/hash.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ package base
55

66
import (
77
"crypto/sha256"
8-
"fmt"
8+
"encoding/hex"
99

1010
"golang.org/x/crypto/pbkdf2"
1111
)
1212

1313
func HashToken(token, salt string) string {
1414
tempHash := pbkdf2.Key([]byte(token), []byte(salt), 10000, 50, sha256.New)
15-
return fmt.Sprintf("%x", tempHash)
15+
return hex.EncodeToString(tempHash)
1616
}

models/migrations/v1_14/v166.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package v1_14 //nolint
55

66
import (
77
"crypto/sha256"
8-
"fmt"
8+
"encoding/hex"
99

1010
"golang.org/x/crypto/argon2"
1111
"golang.org/x/crypto/bcrypt"
@@ -53,7 +53,7 @@ func RecalculateUserEmptyPWD(x *xorm.Engine) (err error) {
5353
tempPasswd = pbkdf2.Key([]byte(passwd), []byte(salt), 10000, 50, sha256.New)
5454
}
5555

56-
return fmt.Sprintf("%x", tempPasswd)
56+
return hex.EncodeToString(tempPasswd)
5757
}
5858

5959
// ValidatePassword checks if given password matches the one belongs to the user.

models/user/user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ func hashPassword(passwd, salt, algo string) (string, error) {
401401
tempPasswd = pbkdf2.Key([]byte(passwd), saltBytes, 10000, 50, sha256.New)
402402
}
403403

404-
return fmt.Sprintf("%x", tempPasswd), nil
404+
return hex.EncodeToString(tempPasswd), nil
405405
}
406406

407407
// SetPassword hashes a password using the algorithm defined in the config value of PASSWORD_HASH_ALGO

modules/packages/hashed_buffer_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
package packages
55

66
import (
7-
"fmt"
7+
"encoding/hex"
88
"io"
99
"strings"
1010
"testing"
@@ -36,10 +36,10 @@ func TestHashedBuffer(t *testing.T) {
3636
assert.Equal(t, c.Data, string(data))
3737

3838
hashMD5, hashSHA1, hashSHA256, hashSHA512 := buf.Sums()
39-
assert.Equal(t, c.HashMD5, fmt.Sprintf("%x", hashMD5))
40-
assert.Equal(t, c.HashSHA1, fmt.Sprintf("%x", hashSHA1))
41-
assert.Equal(t, c.HashSHA256, fmt.Sprintf("%x", hashSHA256))
42-
assert.Equal(t, c.HashSHA512, fmt.Sprintf("%x", hashSHA512))
39+
assert.Equal(t, c.HashMD5, hex.EncodeToString(hashMD5))
40+
assert.Equal(t, c.HashSHA1, hex.EncodeToString(hashSHA1))
41+
assert.Equal(t, c.HashSHA256, hex.EncodeToString(hashSHA256))
42+
assert.Equal(t, c.HashSHA512, hex.EncodeToString(hashSHA512))
4343

4444
assert.NoError(t, buf.Close())
4545
}

modules/packages/multi_hasher_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
package packages
55

66
import (
7-
"fmt"
7+
"encoding/hex"
88
"testing"
99

1010
"github.com/stretchr/testify/assert"
@@ -24,10 +24,10 @@ func TestMultiHasherSums(t *testing.T) {
2424

2525
hashMD5, hashSHA1, hashSHA256, hashSHA512 := h.Sums()
2626

27-
assert.Equal(t, expectedMD5, fmt.Sprintf("%x", hashMD5))
28-
assert.Equal(t, expectedSHA1, fmt.Sprintf("%x", hashSHA1))
29-
assert.Equal(t, expectedSHA256, fmt.Sprintf("%x", hashSHA256))
30-
assert.Equal(t, expectedSHA512, fmt.Sprintf("%x", hashSHA512))
27+
assert.Equal(t, expectedMD5, hex.EncodeToString(hashMD5))
28+
assert.Equal(t, expectedSHA1, hex.EncodeToString(hashSHA1))
29+
assert.Equal(t, expectedSHA256, hex.EncodeToString(hashSHA256))
30+
assert.Equal(t, expectedSHA512, hex.EncodeToString(hashSHA512))
3131
})
3232

3333
t.Run("State", func(t *testing.T) {
@@ -45,9 +45,9 @@ func TestMultiHasherSums(t *testing.T) {
4545

4646
hashMD5, hashSHA1, hashSHA256, hashSHA512 := h2.Sums()
4747

48-
assert.Equal(t, expectedMD5, fmt.Sprintf("%x", hashMD5))
49-
assert.Equal(t, expectedSHA1, fmt.Sprintf("%x", hashSHA1))
50-
assert.Equal(t, expectedSHA256, fmt.Sprintf("%x", hashSHA256))
51-
assert.Equal(t, expectedSHA512, fmt.Sprintf("%x", hashSHA512))
48+
assert.Equal(t, expectedMD5, hex.EncodeToString(hashMD5))
49+
assert.Equal(t, expectedSHA1, hex.EncodeToString(hashSHA1))
50+
assert.Equal(t, expectedSHA256, hex.EncodeToString(hashSHA256))
51+
assert.Equal(t, expectedSHA512, hex.EncodeToString(hashSHA512))
5252
})
5353
}

0 commit comments

Comments
 (0)