Skip to content

Commit

Permalink
Refactor using testify and upgrade go v1.22.7 (pavlo-v-chernykh#53)
Browse files Browse the repository at this point in the history
Switch to Go v1.22.7

Refactor using github.com/stretchr/testify

Upgrade linter in GH action

Fix linters warnings

Incorporate suggestions and feedback
  • Loading branch information
nitram509 authored Oct 1, 2024
1 parent 294a547 commit 6ecc0bc
Show file tree
Hide file tree
Showing 16 changed files with 153 additions and 262 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: '1.17.10'
go-version: '1.22.7'
- name: Fmt
run: go fmt github.com/pavlo-v-chernykh/keystore-go/v4/...
lint:
Expand All @@ -20,10 +20,10 @@ jobs:
- name: Clone repository
uses: actions/checkout@v2
- name: Lint
uses: golangci/golangci-lint-action@v2.5.2
uses: golangci/golangci-lint-action@v6
with:
args: --timeout=5m0s -c .golangci.yaml
version: v1.54.2
version: v1.61.0
test:
name: Test
runs-on: ubuntu-latest
Expand All @@ -33,6 +33,6 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: '1.17.10'
go-version: '1.22.7'
- name: Test
run: go test -cover -count=1 -v github.com/pavlo-v-chernykh/keystore-go/v4/...
16 changes: 6 additions & 10 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,24 @@ linters:
disable:
- gochecknoglobals
- funlen
- goerr113
- err113
- gofumpt
- exhaustivestruct
- gomoddirectives
- scopelint
- makezero
- golint
- interfacer
- maligned
- varnamelen
- exhaustruct
- gomnd # because WARN The linter 'gomnd' is deprecated (since v1.58.0) due to: The linter has been renamed. Replaced by mnd.
- exportloopref # because WARN The linter 'exportloopref' is deprecated (since v1.60.2) due to: Since Go1.22 (loopvar) this linter is no longer relevant. Replaced by copyloopvar.
- execinquery # because WARN The linter 'execinquery' is deprecated (since v1.58.0) due to: The repository of the linter has been archived by the owner.

linters-settings:
gomnd:
settings:
mnd:
checks: [case, condition, return]
cyclop:
max-complexity: 15


issues:
exclude:
- import '.*' is not allowed from list 'Main'
exclude-rules:
- path: _test\.go
linters:
Expand Down
2 changes: 1 addition & 1 deletion common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var byteOrder = binary.BigEndian
var whitenerMessage = []byte("Mighty Aphrodite")

func passwordBytes(password []byte) []byte {
result := make([]byte, 0, len(password)*2)
result := make([]byte, 0, len(password)*2) //nolint:gomnd,mnd
for _, b := range password {
result = append(result, 0, b)
}
Expand Down
38 changes: 19 additions & 19 deletions common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,30 @@ package keystore

import (
"crypto/rand"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestZeroing(t *testing.T) {
var table [][]byte
const tableLength = 20

var table = make([][]byte, tableLength)

for i := 0; i < 20; i++ {
for i := range tableLength {
buf := make([]byte, 4096)
if _, err := rand.Read(buf); err != nil {
t.Errorf("read random bytes: %v", err)
}
_, err := rand.Read(buf)
require.NoError(t, err)

table = append(table, buf)
table[i] = buf
}

for _, tt := range table {
zeroing(tt)

for i := range tt {
if tt[i] != 0 {
t.Errorf("fill input with zeros '%v'", tt)
}
assert.Equalf(t, uint8(0), tt[i], "fill input with zeros '%v'", tt)
}
}
}
Expand All @@ -35,13 +36,14 @@ func TestPasswordBytes(t *testing.T) {
output []byte
}

var table []item
const tableLength = 20

for i := 0; i < 20; i++ {
var table = make([]item, tableLength)

for i := range tableLength {
input := make([]byte, 1024)
if _, err := rand.Read(input); err != nil {
t.Errorf("read random bytes: %v", err)
}
_, err := rand.Read(input)
require.NoError(t, err)

output := make([]byte, len(input)*2)

Expand All @@ -50,13 +52,11 @@ func TestPasswordBytes(t *testing.T) {
output[j+1] = input[k]
}

table = append(table, item{input: input, output: output})
table[i] = item{input: input, output: output}
}

for _, tt := range table {
output := passwordBytes(tt.input)
if !reflect.DeepEqual(output, tt.output) {
t.Errorf("convert password bytes '%v', '%v'", output, tt.output)
}
assert.Equal(t, tt.output, output, "convert password bytes")
}
}
12 changes: 6 additions & 6 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ type decoder struct {
}

func (d decoder) readUint16() (uint16, error) {
b, err := d.readBytes(2)
b, err := d.readBytes(2) //nolint:gomnd,mnd

return byteOrder.Uint16(b), err
}

func (d decoder) readUint32() (uint32, error) {
b, err := d.readBytes(4)
b, err := d.readBytes(4) //nolint:gomnd,mnd

return byteOrder.Uint32(b), err
}

func (d decoder) readUint64() (uint64, error) {
b, err := d.readBytes(8)
b, err := d.readBytes(8) //nolint:gomnd,mnd

return byteOrder.Uint64(b), err
}
Expand Down Expand Up @@ -119,7 +119,7 @@ func (d decoder) readPrivateKeyEntry(version uint32) (PrivateKeyEntry, error) {

chain := make([]Certificate, 0, certNum)

for i := uint32(0); i < certNum; i++ {
for i := range certNum {
cert, err := d.readCertificate(version)
if err != nil {
return PrivateKeyEntry{}, fmt.Errorf("read %d certificate: %w", i, err)
Expand All @@ -128,7 +128,7 @@ func (d decoder) readPrivateKeyEntry(version uint32) (PrivateKeyEntry, error) {
chain = append(chain, cert)
}

creationDateTime := time.UnixMilli(int64(creationTimeStamp))
creationDateTime := time.UnixMilli(int64(creationTimeStamp)) //nolint:gosec
privateKeyEntry := PrivateKeyEntry{
PrivateKey: encryptedPrivateKey,
CreationTime: creationDateTime,
Expand All @@ -149,7 +149,7 @@ func (d decoder) readTrustedCertificateEntry(version uint32) (TrustedCertificate
return TrustedCertificateEntry{}, fmt.Errorf("read certificate: %w", err)
}

creationDateTime := time.UnixMilli(int64(creationTimeStamp))
creationDateTime := time.UnixMilli(int64(creationTimeStamp)) //nolint:gosec
trustedCertificateEntry := TrustedCertificateEntry{
CreationTime: creationDateTime,
Certificate: certificate,
Expand Down
85 changes: 24 additions & 61 deletions decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"errors"
"fmt"
"io"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestReadUint16(t *testing.T) {
Expand Down Expand Up @@ -69,20 +71,14 @@ func TestReadUint16(t *testing.T) {
}

number, err := d.readUint16()
if !reflect.DeepEqual(err, tt.err) {
t.Errorf("invalid error '%v' '%v'", err, tt.err)
}
assert.Equal(t, tt.err, err)

if err == nil {
if number != tt.number {
t.Errorf("invalid number '%v' '%v'", number, tt.number)
}
assert.Equal(t, tt.number, number)
}

hash := d.h.Sum(nil)
if !reflect.DeepEqual(hash, tt.hash[:]) {
t.Errorf("invalid hash '%v' '%v'", hash, tt.hash)
}
assert.Equal(t, tt.hash[:], hash)
}
}

Expand Down Expand Up @@ -143,20 +139,14 @@ func TestReadUint32(t *testing.T) {
}

number, err := d.readUint32()
if !reflect.DeepEqual(err, tt.err) {
t.Errorf("invalid error '%v' '%v'", err, tt.err)
}
assert.Equal(t, tt.err, err)

if err == nil {
if number != tt.number {
t.Errorf("invalid uint32 '%v' '%v'", number, tt.number)
}
assert.Equal(t, tt.number, number)
}

hash := d.h.Sum(nil)
if !reflect.DeepEqual(hash, tt.hash[:]) {
t.Errorf("invalid hash '%v' '%v'", hash, tt.hash)
}
assert.Equal(t, tt.hash[:], hash)
}
}

Expand Down Expand Up @@ -221,20 +211,14 @@ func TestReadUint64(t *testing.T) {
}

number, err := d.readUint64()
if !reflect.DeepEqual(err, tt.err) {
t.Errorf("invalid error '%v' '%v'", err, tt.err)
}
assert.Equal(t, tt.err, err)

if err == nil {
if number != tt.number {
t.Errorf("invalid uint64 '%v' '%v'", number, tt.number)
}
assert.Equal(t, tt.number, number)
}

hash := d.h.Sum(nil)
if !reflect.DeepEqual(hash, tt.hash[:]) {
t.Errorf("invalid hash '%v' '%v'", hash, tt.hash)
}
assert.Equal(t, tt.hash[:], hash)
}
}

Expand Down Expand Up @@ -268,9 +252,8 @@ func TestReadBytes(t *testing.T) {
})
buf := func() []byte {
buf := make([]byte, 10*1024)
if _, err := rand.Read(buf); err != nil {
t.Errorf("read random bytes: %v", err)
}
_, err := rand.Read(buf)
require.NoError(t, err)

return buf
}()
Expand All @@ -292,18 +275,12 @@ func TestReadBytes(t *testing.T) {
}

bts, err := d.readBytes(tt.readLen)
if err != nil {
t.Errorf("got error '%v'", err)
}
require.NoError(t, err)

if !reflect.DeepEqual(bts, tt.bytes) {
t.Errorf("invalid bytes '%v' '%v'", bts, tt.bytes)
}
assert.Equal(t, tt.bytes, bts)

hash := d.h.Sum(nil)
if !reflect.DeepEqual(hash, tt.hash[:]) {
t.Errorf("invalid hash '%v' '%v'", hash, tt.hash)
}
assert.Equal(t, tt.hash[:], hash)
}
}

Expand Down Expand Up @@ -343,7 +320,7 @@ func TestReadString(t *testing.T) {
})
str := "some string to read"
buf := make([]byte, 2)
binary.BigEndian.PutUint16(buf, uint16(len(str)))
binary.BigEndian.PutUint16(buf, uint16(len(str))) //nolint:gosec
buf = append(buf, []byte(str)...)
table = append(table, item{
input: buf,
Expand All @@ -362,18 +339,11 @@ func TestReadString(t *testing.T) {
}

str, err := d.readString()
if !reflect.DeepEqual(err, tt.err) {
t.Errorf("invalid error '%v' '%v'", err, tt.err)
}

if str != tt.string {
t.Errorf("invalid string '%v' '%v'", str, tt.string)
}
assert.Equal(t, tt.err, err)
assert.Equal(t, tt.string, str)

hash := d.h.Sum(nil)
if !reflect.DeepEqual(hash, tt.hash[:]) {
t.Errorf("invalid hash '%v' '%v'", hash, tt.hash)
}
assert.Equal(t, tt.hash[:], hash)
}
}

Expand Down Expand Up @@ -468,17 +438,10 @@ func TestReadCertificate(t *testing.T) {
}

cert, err := d.readCertificate(tt.version)
if !reflect.DeepEqual(err, tt.err) {
t.Errorf("invalid error '%v' '%v'", err, tt.err)
}

if !reflect.DeepEqual(cert, tt.cert) {
t.Errorf("invalid certificate '%v' '%v'", cert, tt.cert)
}
assert.Equal(t, tt.err, err)
assert.Equal(t, tt.cert, cert)

hash := d.h.Sum(nil)
if !reflect.DeepEqual(hash, tt.hash[:]) {
t.Errorf("invalid hash '%v' '%v'", hash, tt.hash)
}
assert.Equal(t, tt.hash[:], hash)
}
}
4 changes: 2 additions & 2 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (e encoder) writePrivateKeyEntry(alias string, pke PrivateKeyEntry) error {
return fmt.Errorf("write alias: %w", err)
}

if err := e.writeUint64(uint64(pke.CreationTime.UnixMilli())); err != nil {
if err := e.writeUint64(uint64(pke.CreationTime.UnixMilli())); err != nil { //nolint:gosec
return fmt.Errorf("write creation timestamp: %w", err)
}

Expand Down Expand Up @@ -140,7 +140,7 @@ func (e encoder) writeTrustedCertificateEntry(alias string, tce TrustedCertifica
return fmt.Errorf("write alias: %w", err)
}

if err := e.writeUint64(uint64(tce.CreationTime.UnixMilli())); err != nil {
if err := e.writeUint64(uint64(tce.CreationTime.UnixMilli())); err != nil { //nolint:gosec
return fmt.Errorf("write creation timestamp: %w", err)
}

Expand Down
2 changes: 1 addition & 1 deletion examples/compare/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/pavlo-v-chernykh/keystore-go/v4/examples/compare

go 1.17
go 1.22.7

require github.com/pavlo-v-chernykh/keystore-go/v4 v4.0.0

Expand Down
Loading

0 comments on commit 6ecc0bc

Please sign in to comment.