Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
number571 committed Oct 13, 2023
1 parent 8014c52 commit c2c49cf
Show file tree
Hide file tree
Showing 12 changed files with 1,042 additions and 750 deletions.
5 changes: 0 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,9 @@ test-run:
echo "Build took $$(($$(date +%s)-d)) seconds";

test-coverage:
go vet ./...;
$(CHECK_ERROR);

go test -coverprofile=$(TEST_PATH)/coverage.out `go list ./...`
$(CHECK_ERROR);

go test -race -cover -count=1 `go list ./...` | tee $(TEST_PATH)/result.out;

test-coverage-view:
go tool cover -html=$(TEST_PATH)/coverage.out

Expand Down
3 changes: 3 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@
7. Fix docker build for arm macOS
8. HLS: append proxy connections
9. HLS: write a threat model (in "Hidden Lake" article)
10. HLF: Hidden Lake Forum
11. HLM: grouped chats
12. HLC: Hidden Lake Connector
35 changes: 35 additions & 0 deletions pkg/network/anonymity/anonymity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,41 @@ const (
tcIter = 10
)

func TestSettings(t *testing.T) {
for i := 0; i < 3; i++ {
testSettings(t, i)
}
}

func testSettings(t *testing.T, n int) {
defer func() {
if r := recover(); r == nil {
t.Error("nothing panics")
return
}
}()
switch n {
case 0:
_ = NewSettings(&SSettings{
FRetryEnqueue: 0,
FNetworkMask: 1,
FFetchTimeWait: time.Second,
})
case 1:
_ = NewSettings(&SSettings{
FServiceName: "TEST",
FRetryEnqueue: 0,
FFetchTimeWait: time.Second,
})
case 2:
_ = NewSettings(&SSettings{
FServiceName: "TEST",
FRetryEnqueue: 0,
FNetworkMask: 1,
})
}
}

func TestComplex(t *testing.T) {
for i := 0; i < 5; i++ {
os.RemoveAll(fmt.Sprintf(tcPathDBTemplate, i, 0))
Expand Down
25 changes: 25 additions & 0 deletions pkg/network/conn_keeper/conn_keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,31 @@ import (
testutils "github.com/number571/go-peer/test/_data"
)

func TestSettings(t *testing.T) {
for i := 0; i < 2; i++ {
testSettings(t, i)
}
}

func testSettings(t *testing.T, n int) {
defer func() {
if r := recover(); r == nil {
t.Error("nothing panics")
return
}
}()
switch n {
case 0:
_ = NewSettings(&SSettings{
FDuration: 500 * time.Millisecond,
})
case 1:
_ = NewSettings(&SSettings{
FConnections: func() []string { return []string{testutils.TgAddrs[18]} },
})
}
}

func TestConnKeeper(t *testing.T) {
listener := testNewService(t)
defer testFreeService(listener)
Expand Down
77 changes: 77 additions & 0 deletions pkg/network/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,83 @@ const (
tcTimeWait = time.Minute
)

func TestSettings(t *testing.T) {
for i := 0; i < 5; i++ {
testSettings(t, i)
}
}

func testSettings(t *testing.T, n int) {
defer func() {
if r := recover(); r == nil {
t.Error("nothing panics")
return
}
}()
switch n {
case 0:
_ = NewSettings(&SSettings{
FAddress: "test",
FMaxConnects: testutils.TCMaxConnects,
FReadTimeout: tcTimeWait,
FWriteTimeout: tcTimeWait,
FConnSettings: conn.NewSettings(&conn.SSettings{
FMessageSizeBytes: testutils.TCMessageSize,
FWaitReadDeadline: time.Hour,
FReadDeadline: time.Minute,
FWriteDeadline: time.Minute,
}),
})
case 1:
_ = NewSettings(&SSettings{
FAddress: "test",
FCapacity: testutils.TCCapacity,
FReadTimeout: tcTimeWait,
FWriteTimeout: tcTimeWait,
FConnSettings: conn.NewSettings(&conn.SSettings{
FMessageSizeBytes: testutils.TCMessageSize,
FWaitReadDeadline: time.Hour,
FReadDeadline: time.Minute,
FWriteDeadline: time.Minute,
}),
})
case 2:
_ = NewSettings(&SSettings{
FAddress: "test",
FCapacity: testutils.TCCapacity,
FMaxConnects: testutils.TCMaxConnects,
FWriteTimeout: tcTimeWait,
FConnSettings: conn.NewSettings(&conn.SSettings{
FMessageSizeBytes: testutils.TCMessageSize,
FWaitReadDeadline: time.Hour,
FReadDeadline: time.Minute,
FWriteDeadline: time.Minute,
}),
})
case 3:
_ = NewSettings(&SSettings{
FAddress: "test",
FCapacity: testutils.TCCapacity,
FMaxConnects: testutils.TCMaxConnects,
FReadTimeout: tcTimeWait,
FConnSettings: conn.NewSettings(&conn.SSettings{
FMessageSizeBytes: testutils.TCMessageSize,
FWaitReadDeadline: time.Hour,
FReadDeadline: time.Minute,
FWriteDeadline: time.Minute,
}),
})
case 4:
_ = NewSettings(&SSettings{
FAddress: "test",
FCapacity: testutils.TCCapacity,
FMaxConnects: testutils.TCMaxConnects,
FReadTimeout: tcTimeWait,
FWriteTimeout: tcTimeWait,
})
}
}

func TestBroadcast(t *testing.T) {
nodes, mapp := testNodes()
defer testFreeNodes(nodes[:])
Expand Down
6 changes: 6 additions & 0 deletions pkg/payload/payload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ func TestPayload(t *testing.T) {
t.Error("title not equal with decoded version of payload")
return
}

invalidPld := LoadPayload([]byte{1})
if invalidPld != nil {
t.Error("invalid payload success decoded")
return
}
}
4 changes: 0 additions & 4 deletions pkg/storage/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,6 @@ func (p *sKeyValueDB) Get(pKey []byte) ([]byte, error) {
return nil, errors.WrapError(err, "read value by key")
}

if encValue == nil {
return nil, errors.NewError("undefined value")
}

return tryDecrypt(
p.fCipher,
p.fAuthKey,
Expand Down
55 changes: 39 additions & 16 deletions pkg/storage/database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,54 @@ import (
"testing"

"github.com/number571/go-peer/pkg/crypto/asymmetric"
"github.com/number571/go-peer/pkg/crypto/hashing"
"github.com/number571/go-peer/pkg/crypto/symmetric"
"github.com/number571/go-peer/pkg/storage"

testutils "github.com/number571/go-peer/test/_data"
)

type (
tiDBConsctruct func(storage.ISettings) (IKVDatabase, error)
)

const (
tcPathDBTemplate = "database_test_%d.db"
)

func TestTryDecrypt(t *testing.T) {
cipher := symmetric.NewAESCipher([]byte("abcdefghijklmnopqrstuvwxyz123456"))
if _, err := tryDecrypt(cipher, []byte{1}, []byte{2}); err == nil {
t.Error("invalid size of encrypt data")
return
}

authKey := []byte("auth-key")
encData := cipher.EncryptBytes([]byte{})
resData := bytes.Join(
[][]byte{
hashing.NewHMACSHA256Hasher(
authKey,
encData,
).ToBytes(),
encData,
},
[]byte{},
)

if _, err := tryDecrypt(cipher, authKey, resData); err != nil {
t.Error("got error with encrypted empty slice: []")
return
}
}

func TestAllDBs(t *testing.T) {
testCreate(t, NewKeyValueDB)
testCipherKey(t, NewKeyValueDB)
testBasic(t, NewKeyValueDB)
testCreate(t)
testCipherKey(t)
testBasic(t)
}

func testCreate(t *testing.T, dbConstruct tiDBConsctruct) {
func testCreate(t *testing.T) {
dbPath := fmt.Sprintf(tcPathDBTemplate, 3)
defer os.RemoveAll(dbPath)

store, err := dbConstruct(storage.NewSettings(&storage.SSettings{
store, err := NewKeyValueDB(storage.NewSettings(&storage.SSettings{
FPath: dbPath,
FWorkSize: testutils.TCWorkSize,
FPassword: "CIPHER",
Expand Down Expand Up @@ -62,11 +86,11 @@ func testCreate(t *testing.T, dbConstruct tiDBConsctruct) {
}
}

func testCipherKey(t *testing.T, dbConstruct tiDBConsctruct) {
func testCipherKey(t *testing.T) {
dbPath := fmt.Sprintf(tcPathDBTemplate, 2)
defer os.RemoveAll(dbPath)

store, err := dbConstruct(storage.NewSettings(&storage.SSettings{
store, err := NewKeyValueDB(storage.NewSettings(&storage.SSettings{
FPath: dbPath,
FWorkSize: testutils.TCWorkSize,
FPassword: "CIPHER1",
Expand All @@ -80,7 +104,7 @@ func testCipherKey(t *testing.T, dbConstruct tiDBConsctruct) {
store.Set([]byte("KEY"), []byte("VALUE"))

store.Close() // open this database with another key
store, err = dbConstruct(storage.NewSettings(&storage.SSettings{
store, err = NewKeyValueDB(storage.NewSettings(&storage.SSettings{
FPath: dbPath,
FWorkSize: testutils.TCWorkSize,
FPassword: "CIPHER2",
Expand All @@ -97,11 +121,11 @@ func testCipherKey(t *testing.T, dbConstruct tiDBConsctruct) {
}
}

func testBasic(t *testing.T, dbConstruct tiDBConsctruct) {
func testBasic(t *testing.T) {
dbPath := fmt.Sprintf(tcPathDBTemplate, 1)
defer os.RemoveAll(dbPath)

store, err := dbConstruct(storage.NewSettings(&storage.SSettings{
store, err := NewKeyValueDB(storage.NewSettings(&storage.SSettings{
FPath: dbPath,
FWorkSize: testutils.TCWorkSize,
FPassword: "CIPHER",
Expand All @@ -126,8 +150,7 @@ func testBasic(t *testing.T, dbConstruct tiDBConsctruct) {
return
}

err = store.Del([]byte("KEY"))
if err != nil {
if err := store.Del([]byte("KEY")); err != nil {
t.Error("[testBasic]", err)
return
}
Expand Down
7 changes: 1 addition & 6 deletions pkg/storage/database/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,5 @@ func tryDecrypt(pCipher symmetric.ICipher, pAuthKey []byte, pEncBytes []byte) ([
return nil, errors.NewError("incorrect hash of decrypted data")
}

decBytes := pCipher.DecryptBytes(encDataBytes)
if decBytes == nil {
return nil, errors.NewError("failed decrypt message")
}

return decBytes, nil
return pCipher.DecryptBytes(encDataBytes), nil
}
37 changes: 37 additions & 0 deletions pkg/storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,38 @@ func TestCryptoStorage(t *testing.T) {
testTempCryptoStorage(t, tcTmpStorageName)
}

func TestSettings(t *testing.T) {
for i := 0; i < 3; i++ {
testSettings(t, i)
}
}

func testSettings(t *testing.T, n int) {
defer func() {
if r := recover(); r == nil {
t.Error("nothing panics")
return
}
}()
switch n {
case 0:
_ = NewSettings(&SSettings{
FWorkSize: testutils.TCWorkSize,
FPassword: "CIPHER",
})
case 1:
_ = NewSettings(&SSettings{
FPath: "path",
FPassword: "CIPHER",
})
case 2:
_ = NewSettings(&SSettings{
FPath: "path",
FWorkSize: testutils.TCWorkSize,
})
}
}

func testCryptoStorage(t *testing.T, path string) {
sett := NewSettings(&SSettings{
FPath: path,
Expand All @@ -35,6 +67,11 @@ func testCryptoStorage(t *testing.T, path string) {
return
}

if store.GetSettings().GetPath() != path {
t.Error("incorrect value from settings")
return
}

gotSecret, err := store.Get([]byte("KEY"))
if err != nil {
t.Error(err)
Expand Down
Loading

0 comments on commit c2c49cf

Please sign in to comment.