Skip to content

Commit

Permalink
adding more unit tests (#30)
Browse files Browse the repository at this point in the history
* test h2c

* more tests

* more tests

* fix test name

* logs

* adapt to github action

* fix wrong order

* fix indent

* fix test

* ByAmount test

* rename

* tests

* add cashu test

* more

* more
  • Loading branch information
gohumble authored Oct 15, 2022
1 parent 2ab9711 commit af7262b
Show file tree
Hide file tree
Showing 13 changed files with 577 additions and 30 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ jobs:
uses: actions/setup-go@v3
with:
go-version: 1.19
- name: Build
run: go build -v -o cashu-feni cmd/cashu/mint.go
- name: Run Cashu Feni
run: nohup go run cmd/cashu/mint.go &
- name: Golang run tests
run: go test -coverprofile=coverage.txt -covermode=atomic -v ./...
- uses: codecov/codecov-action@v3
with:
verbose: true # optional (default = false)
- name: Build
run: go build -v -o cashu-feni cmd/cashu/mint.go
- name: Run Cashu Feni
run: nohup go run cmd/cashu/mint.go &
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
Expand All @@ -40,7 +40,7 @@ jobs:
run: |
cd cashu
poetry install --with dev
- name: Python run Poetry tests
- name: Python run acceptance tests
env:
LIGHTNING: False
MINT_HOST: localhost
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[![GoReportCard example](https://goreportcard.com/badge/github.com/gohumble/cashu-feni)](https://goreportcard.com/report/github.com/gohumble/cashu-feni)
[![Docker](https://badgen.net/badge/icon/docker?icon=docker&label)](https://https://docker.com/)
[![Github tag](https://badgen.net/github/tag/gohumble/cashu-feni)](https://github.com/gohumble/cashu-feni/tags/)
[![codecov](https://codecov.io/gh/gohumble/cashu-feni/branch/master/graph/badge.svg)](https://codecov.io/gh/gohumble/cashu-feni)

<html>
<simple-boost amount="2100" address="hello@getalby.com"></simple-boost>
Expand Down
2 changes: 1 addition & 1 deletion api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func New() *Api {
Mint: mint.New(Config.Mint.PrivateKey,
mint.WithClient(lnBitsClient),
mint.WithStorage(sqlStorage),
mint.WithInitialKeySet(Config.Mint.PrivateKey, Config.Mint.DerivationPath),
mint.WithInitialKeySet(Config.Mint.DerivationPath),
),
}

Expand Down
172 changes: 172 additions & 0 deletions cashu/cashu_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package cashu

import (
"encoding/hex"
"fmt"
"github.com/gohumble/cashu-feni/lightning"
"github.com/gohumble/cashu-feni/lightning/lnbits"
"reflect"
"testing"
"time"
)

func TestToJson(t *testing.T) {
Expand Down Expand Up @@ -34,3 +40,169 @@ func TestToJson(t *testing.T) {
})
}
}

func TestWithCode(t *testing.T) {
type args struct {
code int
}
tests := []struct {
name string
args args
want ErrorResponse
}{
{name: "withCode", want: ErrorResponse{Code: 1, Err: "test"}, args: args{code: 1}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := NewErrorResponse(fmt.Errorf("test"), WithCode(tt.args.code))
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("WithCode() = %v, want %v", got, tt.want)
}
})
}
}

func TestNewErrorResponse(t *testing.T) {
type args struct {
err error
options []ErrorOptions
}
tests := []struct {
name string
args args
want ErrorResponse
}{
{name: "withCode", want: ErrorResponse{Code: 1, Err: "test"},
args: args{err: fmt.Errorf("test"),
options: []ErrorOptions{WithCode(1)}}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewErrorResponse(tt.args.err, tt.args.options...); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewErrorResponse() = %v, want %v", got, tt.want)
}
})
}
}

func TestErrorResponse_String(t *testing.T) {
type fields struct {
Err string
Code int
}
tests := []struct {
name string
fields fields
want string
}{
{name: "string", fields: fields{Err: "test", Code: 1}, want: "{\"error\":\"test\",\"code\":1}"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := ErrorResponse{
Err: tt.fields.Err,
Code: tt.fields.Code,
}
if got := e.String(); got != tt.want {
t.Errorf("String() = %v, want %v", got, tt.want)
}
})
}
}

func TestErrorResponse_Error(t *testing.T) {
type fields struct {
Err string
Code int
}
tests := []struct {
name string
fields fields
want string
}{
{name: "err", want: "test", fields: fields{Err: "test"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := ErrorResponse{
Err: tt.fields.Err,
Code: tt.fields.Code,
}
if got := e.Error(); got != tt.want {
t.Errorf("Error() = %v, want %v", got, tt.want)
}
})
}
}

func TestCreateInvoice(t *testing.T) {
tests := []struct {
name string
want lightning.Invoice
}{
{name: "createNoInvoice", want: nil},
{name: "createInvoice", want: &lnbits.Invoice{}},
{name: "lightningOnly", want: nil},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
switch tt.name {
case "createInvoice":
lightning.Config.Lightning.Lnbits = &lightning.LnbitsConfig{}
lightning.Config.Lightning.Enabled = true
case "lightningOnly":
lightning.Config.Lightning.Enabled = true
lightning.Config.Lightning.Lnbits = nil
}
if got := CreateInvoice(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("%s = %v, want %v", tt.name, got, tt.want)
}
})
}
}

func TestProof_Decode(t *testing.T) {
type fields struct {
Id string
Amount int64
Secret string
C string
reserved bool
Script *P2SHScript
sendId string
timeCreated time.Time
timeReserved time.Time
}
msg := hex.EncodeToString([]byte("hello"))
tests := []struct {
name string
fields fields
want []byte
wantErr bool
}{
{name: "decode", want: []byte("hello"), fields: fields{C: msg}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := Proof{
Id: tt.fields.Id,
Amount: tt.fields.Amount,
Secret: tt.fields.Secret,
C: tt.fields.C,
reserved: tt.fields.reserved,
Script: tt.fields.Script,
sendId: tt.fields.sendId,
timeCreated: tt.fields.timeCreated,
timeReserved: tt.fields.timeReserved,
}
got, err := p.Decode()
if (err != nil) != tt.wantErr {
t.Errorf("Decode() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Decode() got = %v, want %v", string(got), tt.want)
}
})
}
}
8 changes: 2 additions & 6 deletions crypto/b_dhke.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,8 @@ func HashToCurve(secretMessage []byte) *secp256k1.PublicKey {
}

// FirstStepAlice creates blinded secrets and produces outputs
func FirstStepAlice(secretMessage string) (*secp256k1.PublicKey, *secp256k1.PrivateKey) {
func FirstStepAlice(secretMessage string, r *secp256k1.PrivateKey) (*secp256k1.PublicKey, *secp256k1.PrivateKey) {
Y := HashToCurve([]byte(secretMessage))
r, err := secp256k1.GeneratePrivateKey()
if err != nil {
panic(err)
}
var pointr, pointy, result secp256k1.JacobianPoint

r.PubKey().AsJacobian(&pointr)
Expand All @@ -65,7 +61,7 @@ func FirstStepAlice(secretMessage string) (*secp256k1.PublicKey, *secp256k1.Priv
return B_, r
}

// SecondStepBob signes blinded secrets and produces promises
// SecondStepBob signs blinded secrets and produces promises
func SecondStepBob(B_ secp256k1.PublicKey, a secp256k1.PrivateKey) *secp256k1.PublicKey {
var pointB_, Cp_ secp256k1.JacobianPoint
B_.AsJacobian(&pointB_)
Expand Down
116 changes: 116 additions & 0 deletions crypto/b_dhke_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package crypto

import (
"encoding/hex"
"fmt"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
"reflect"
"testing"
)

func TestHashToCurve(t *testing.T) {
type args struct {
secretMessage []byte
}
pk, err := hex.DecodeString("049595c9df90075148eb06860365df33584b75bff782a510c6cd4883a419833d50bbf2e883bdb76cdbb58e57fc0a2df3bcadf9413358a603d7485d572589df9676")
if err != nil {
panic(err)
}
key, err := secp256k1.ParsePubKey(pk)
if err != nil {
panic(err)
}
tests := []struct {
name string
args args
want *secp256k1.PublicKey
}{
{name: "h2c", args: args{secretMessage: []byte("hello")}, want: key},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := HashToCurve(tt.args.secretMessage); !reflect.DeepEqual(got, tt.want) {
fmt.Printf("%x\n", got.SerializeUncompressed())
t.Errorf("HashToCurve() = %v, want %v", got, tt.want)
}
})
}
}

func TestFirstStepAlice(t *testing.T) {
type args struct {
secretMessage string
}
PK, err := hex.DecodeString("0249eb5dbb4fac2750991cf18083388c6ef76cde9537a6ac6f3e6679d35cdf4b0c")
if err != nil {
panic(err)
}
publicKey, err := secp256k1.ParsePubKey(PK)
if err != nil {
panic(err)
}
pk, err := hex.DecodeString("6d7e0abffc83267de28ed8ecc8760f17697e51252e13333ba69b4ddad1f95d05")
if err != nil {
panic(err)
}
privateKey := secp256k1.PrivKeyFromBytes(pk)
if err != nil {
panic(err)
}
tests := []struct {
name string
args args
want *secp256k1.PublicKey
want1 *secp256k1.PrivateKey
}{
{name: "firstStepAlice", args: args{secretMessage: "hello"}, want: publicKey, want1: privateKey},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1 := FirstStepAlice(tt.args.secretMessage, privateKey)
fmt.Printf("%x\n", got.SerializeUncompressed())
fmt.Printf("%x\n", got1.Serialize())
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("FirstStepAlice() got = %x, want %x", got.SerializeUncompressed(), tt.want.SerializeUncompressed())
}
if !reflect.DeepEqual(got1, tt.want1) {
t.Errorf("FirstStepAlice() got1 = %x, want %x", got1.Serialize(), tt.want1.Serialize())
}
})
}
}

func TestSecondStepBob(t *testing.T) {
type args struct {
B_ secp256k1.PublicKey
a secp256k1.PrivateKey
}
r, err := secp256k1.GeneratePrivateKey()
if err != nil {
panic(err)
}

publicKey, privateKey := FirstStepAlice("hello", r)

tests := []struct {
name string
args args
want *secp256k1.PublicKey
}{
{name: "SecondStepBob", args: args{B_: *publicKey, a: *privateKey}, want: publicKey},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := SecondStepBob(tt.args.B_, tt.args.a)
if !got.IsOnCurve() || !publicKey.IsOnCurve() {
t.Errorf("SecondStepBob() not on curve")
}
alice := ThirdStepAlice(*got, *privateKey, *r.PubKey())
if !Verify(*privateKey, *alice, "hello", HashToCurve) {
t.Errorf("verify(a, C, secret_msg) == %v \n", false)
return
}
})
}
}
2 changes: 1 addition & 1 deletion crypto/keyset.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type KeySet struct {
ValidFrom time.Time
ValidTo time.Time
FirstSeen time.Time
Active time.Time
Active bool
}

func NewKeySet(masterKey, derivationPath string) *KeySet {
Expand Down
Loading

0 comments on commit af7262b

Please sign in to comment.