Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
itpey committed Jun 3, 2024
1 parent abc68c3 commit 8731cd4
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,4 @@ motp is open-source software released under the MIT License. You can find a copy

# Author

JetTest was created by [itpey](https://github.com/itpey)
motp was created by [itpey](https://github.com/itpey)
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/itpey/motp

go 1.22.0
go 1.21.10

require github.com/urfave/cli/v2 v2.27.2

Expand Down
132 changes: 132 additions & 0 deletions motp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package motp

import (
"testing"
)

func TestNewMOTPDefaultValues(t *testing.T) {
secret := "testsecret"
pin := "1234"

motp, err := New(secret, pin)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

if motp.secret != secret {
t.Errorf("Expected secret: %s, got: %s", secret, motp.secret)
}

if motp.pin != pin {
t.Errorf("Expected pin: %s, got: %s", pin, motp.pin)
}

if motp.period != 10 {
t.Errorf("Expected period: %d, got: %d", 10, motp.period)
}

if motp.digits != 6 {
t.Errorf("Expected digits: %d, got: %d", 6, motp.digits)
}
}

func TestNewMOTPWithCustomValues(t *testing.T) {
secret := "testsecret"
pin := "1234"
period := uint(30)
digits := uint(8)

motp, err := New(secret, pin, WithPeriod(period), WithDigits(digits))
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

if motp.period != period {
t.Errorf("Expected period: %d, got: %d", period, motp.period)
}

if motp.digits != digits {
t.Errorf("Expected digits: %d, got: %d", digits, motp.digits)
}
}

func TestNewMOTPWithInvalidPeriod(t *testing.T) {
secret := "testsecret"
pin := "1234"
period := uint(0)

_, err := New(secret, pin, WithPeriod(period))
if err == nil {
t.Fatalf("Expected error for invalid period, got none")
}
}

func TestNewMOTPWithInvalidDigits(t *testing.T) {
secret := "testsecret"
pin := "1234"
digits := uint(0)

_, err := New(secret, pin, WithDigits(digits))
if err == nil {
t.Fatalf("Expected error for invalid digits, got none")
}
}

func TestGenerateOTP(t *testing.T) {
secret := "testsecret"
pin := "1234"
period := uint(10)
digits := uint(6)
unixSeconds := int64(1625097600)

motp, err := New(secret, pin, WithPeriod(period), WithDigits(digits))
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

otp, err := motp.Generate(unixSeconds)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

expectedLength := int(digits)
if len(otp) != expectedLength {
t.Errorf("Expected OTP length: %d, got: %d", expectedLength, len(otp))
}
}

func TestGenerateCurrentOTP(t *testing.T) {
secret := "testsecret"
pin := "1234"

motp, err := New(secret, pin)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

otp, err := motp.GenerateCurrent()
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

expectedLength := int(motp.digits)
if len(otp) != expectedLength {
t.Errorf("Expected OTP length: %d, got: %d", expectedLength, len(otp))
}
}

func TestGenerateOTPWithNegativeUnixSeconds(t *testing.T) {
secret := "testsecret"
pin := "1234"
unixSeconds := int64(-1)

motp, err := New(secret, pin)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

_, err = motp.Generate(unixSeconds)
if err == nil {
t.Fatalf("Expected error for negative unixSeconds, got none")
}
}

0 comments on commit 8731cd4

Please sign in to comment.