Skip to content

Commit

Permalink
fix algorithm not being used in CLI
Browse files Browse the repository at this point in the history
add tests
  • Loading branch information
Thumbscrew committed Jan 25, 2025
1 parent 976bfc9 commit c10a5d2
Show file tree
Hide file tree
Showing 2 changed files with 263 additions and 2 deletions.
22 changes: 20 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ func main() {
},
Action: func(ctx *cli.Context) error {
var (
path = ctx.Path("path")
path = ctx.Path("path")
algorithm = strings.ToLower(ctx.String("algorithm"))
)

fi, err := os.Stat(path)
Expand All @@ -73,10 +74,27 @@ func main() {
Progress: bar,
}

h, err := c.Sha256(f)
var h string
switch algorithm {
case "md5":
h, err = c.Md5(f)
case "sha1":
h, err = c.Sha1(f)
case "sha224":
h, err = c.Sha224(f)
case "sha256":
h, err = c.Sha256(f)
case "sha384":
h, err = c.Sha384(f)
case "sha512":
h, err = c.Sha512(f)
default:
return fmt.Errorf("invalid hashing algorithm: %s", algorithm)
}
if err != nil {
return err
}

_ = bar.Close()

fmt.Println(h)
Expand Down
243 changes: 243 additions & 0 deletions pkg/hasher/hasher_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
package hasher

import (
"io"
"strings"
"testing"
)

var testString = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."

func TestClient_Md5(t *testing.T) {
type fields struct {
Progress io.Writer
}
type args struct {
r io.Reader
}
tests := []struct {
name string
fields fields
args args
want string
wantErr bool
}{
{
name: "test correct MD5 checksum is calculated",
args: args{
r: strings.NewReader(testString),
},
want: "818c6e601a24f72750da0f6c9b8ebe28",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Client{
Progress: tt.fields.Progress,
}
got, err := c.Md5(tt.args.r)
if (err != nil) != tt.wantErr {
t.Errorf("Md5() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("Md5() got = %v, want %v", got, tt.want)
}
})
}
}

func TestClient_Sha1(t *testing.T) {
type fields struct {
Progress io.Writer
}
type args struct {
r io.Reader
}
tests := []struct {
name string
fields fields
args args
want string
wantErr bool
}{
{
name: "test correct SHA1 checksum is calculated",
args: args{
r: strings.NewReader(testString),
},
want: "cca0871ecbe200379f0a1e4b46de177e2d62e655",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Client{
Progress: tt.fields.Progress,
}
got, err := c.Sha1(tt.args.r)
if (err != nil) != tt.wantErr {
t.Errorf("Sha1() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("Sha1() got = %v, want %v", got, tt.want)
}
})
}
}

func TestClient_Sha224(t *testing.T) {
type fields struct {
Progress io.Writer
}
type args struct {
r io.Reader
}
tests := []struct {
name string
fields fields
args args
want string
wantErr bool
}{
{
name: "test correct SHA224 checksum is calculated",
args: args{
r: strings.NewReader(testString),
},
want: "730fbab30ac9c9fce3b7e3af8e0f637ad9705191b1424e0ab798896d",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Client{
Progress: tt.fields.Progress,
}
got, err := c.Sha224(tt.args.r)
if (err != nil) != tt.wantErr {
t.Errorf("Sha224() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("Sha224() got = %v, want %v", got, tt.want)
}
})
}
}

func TestClient_Sha256(t *testing.T) {
type fields struct {
Progress io.Writer
}
type args struct {
r io.Reader
}
tests := []struct {
name string
fields fields
args args
want string
wantErr bool
}{
{
name: "test correct SHA256 checksum is calculated",
args: args{
r: strings.NewReader(testString),
},
want: "973153f86ec2da1748e63f0cf85b89835b42f8ee8018c549868a1308a19f6ca3",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Client{
Progress: tt.fields.Progress,
}
got, err := c.Sha256(tt.args.r)
if (err != nil) != tt.wantErr {
t.Errorf("Sha256() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("Sha256() got = %v, want %v", got, tt.want)
}
})
}
}

func TestClient_Sha384(t *testing.T) {
type fields struct {
Progress io.Writer
}
type args struct {
r io.Reader
}
tests := []struct {
name string
fields fields
args args
want string
wantErr bool
}{
{
name: "test correct SHA384 checksum is calculated",
args: args{
r: strings.NewReader(testString),
},
want: "8ac70afc3c5cb8daf3c332fbe88b7b9c2a5104217693b065a42db8de1bf40aca0bdaad7dbc6c405e97cf0809d1afcf52",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Client{
Progress: tt.fields.Progress,
}
got, err := c.Sha384(tt.args.r)
if (err != nil) != tt.wantErr {
t.Errorf("Sha384() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("Sha384() got = %v, want %v", got, tt.want)
}
})
}
}

func TestClient_Sha512(t *testing.T) {
type fields struct {
Progress io.Writer
}
type args struct {
r io.Reader
}
tests := []struct {
name string
fields fields
args args
want string
wantErr bool
}{
{
name: "test correct SHA512 checksum is calculated",
args: args{
r: strings.NewReader(testString),
},
want: "83cd8866be238eda447cb0ee94a6bfa6248109346b1ce3c75f8a67d35f3d8ab1697b46703065c094fcc7d3a61acc1e8ee85a4f306f13cc1a7aea7651781199b3",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Client{
Progress: tt.fields.Progress,
}
got, err := c.Sha512(tt.args.r)
if (err != nil) != tt.wantErr {
t.Errorf("Sha512() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("Sha512() got = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit c10a5d2

Please sign in to comment.