Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
number571 committed Oct 23, 2023
1 parent 51b917b commit d35b65b
Show file tree
Hide file tree
Showing 8 changed files with 566 additions and 498 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
- Update `cmd/hidden_lake/service`: update _math directory
- Update `pkg/client`: append payload check decode
- Update `pkg/client/message`: append check on unknown type
- Update `cmd/hidden_lake/service`: fix check duplicate public key in config actions

## v1.5.19

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

The `go-peer` library contains a large number of functions necessary to ensure the security of transmitted or stored information, as well as for the anonymity of nodes in the decentralized form. The library can be divided into several main modules:

1. The `crypto` module represents cryptographic primitives: 1) asymmetric encryption, decryption; 2) asymmetric signing and signature verification; 3) symmetric encryption and decryption; 4) hashing; 5) entropy enhancement; 6) computational problems (puzzles); 7) cryptographically stable pseudorandom number generator.
1. The `crypto` module represents cryptographic primitives: 1) asymmetric encryption, decryption; 2) asymmetric signing and signature verification; 3) symmetric encryption and decryption; 4) hashing; 5) key building; 6) computational problems (puzzles); 7) cryptographically stable pseudorandom number generator.
2. The `client` module for encrypting and decrypting information with the attached data integrity (hash), authentication (signature) and confirmation (work). It is a basic part of the `anonymity` module.
3. The `client/queue` module represents the generation, storage and issuance of encrypted messages every time the period specified by the application is reached. Uses the `client` module.
4. The `network` module is a decentralized communication between network nodes. It does not represent any protection of information and anonymity of participants.
Expand Down
14 changes: 9 additions & 5 deletions cmd/hidden_lake/service/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,15 @@ func BuildConfig(pFilepath string, pCfg *SConfig) (IConfig, error) {
return nil, errors.NewError(fmt.Sprintf("config file '%s' already exist", pFilepath))
}

if err := configFile.Write(encoding.Serialize(pCfg, true)); err != nil {
return nil, errors.WrapError(err, "write config")
}

pCfg.fFilepath = pFilepath
if err := pCfg.initConfig(); err != nil {
return nil, errors.WrapError(err, "init config")
}

if err := configFile.Write(encoding.Serialize(pCfg, true)); err != nil {
return nil, errors.WrapError(err, "write config")
}

return pCfg, nil
}

Expand Down Expand Up @@ -177,15 +178,18 @@ func (p *SConfig) loadLogging() error {
func (p *SConfig) loadPubKeys() error {
p.fFriends = make(map[string]asymmetric.IPubKey)

mapping := make(map[string]interface{})
mapping := make(map[string]struct{})
for name, val := range p.FFriends {
if _, ok := mapping[val]; ok {
return fmt.Errorf("found public key duplicate '%s'", val)
}
mapping[val] = struct{}{}

pubKey := asymmetric.LoadRSAPubKey(val)
if pubKey == nil {
return errors.NewError(fmt.Sprintf("public key is nil for '%s'", name))
}

p.fFriends[name] = pubKey
if pubKey.GetSize() != p.FSettings.FKeySizeBits {
return errors.NewError(fmt.Sprintf("not supported key size for '%s'", name))
Expand Down
130 changes: 127 additions & 3 deletions cmd/hidden_lake/service/internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package config
import (
"fmt"
"os"
"strings"
"testing"

"github.com/number571/go-peer/pkg/crypto/asymmetric"
"github.com/number571/go-peer/pkg/errors"
"github.com/number571/go-peer/pkg/filesystem"
)

Expand Down Expand Up @@ -38,6 +40,10 @@ var (
"test_connect1",
"test_connect2",
}
tgBackupConnects = []string{
"test_backup_connect1",
"test_backup_connect2",
}
tgPubKeys = map[string]string{
tcPubKeyAlias1: `PubKey(go-peer/rsa){3082020A0282020100C62F3CFA3D9809EE6DD77EBBFD38BC6796ABA76B795B3C76D3449F0AC808E01EDA8B2B08C58E508C306B2D842A2D317FF6B6D4A13EB76C7BBD5B157B663C3390B227476F4985EF649510D8CCA38FAB9FFCD67916FE73DB77595AB64FBE66D85892708A2DBCA94447A628F183FA6328136FCF158688CB6664EBA91F4C41621741786D50E3286AF9CAB81C101BDB19ACF42E10041CFDA5C6F30ACBBC4251E3D13C0E0781CBDC622E4ED490DD76BBA04D0A9C0012EBDAA77BD9F23183205A9D533C95A6C1FAAD8AB7C3B21FA4C76F7A3FB8EAEB231083ED925C1F71D23671E8C90E460C673A0DCD82ECFA956DF315200554571A99D79EB1E744681B9652389DBA6B9937CE476EBCAC34D02AEACF381DA40469B2F23E4F3DBFD5D8E04031708E46C31E3DC94342298E6F83CF7869C1209ACE2EA04FDB011D0FE265C8D51CF7D90C947160415B3415DFF9D1B16D5A9961F896109223B1408E740C421C6F413FA7B3D7094144DE4A0211DCAF043BC1A9FDE120251CBD654E705795D692A912F0543FF2F13EC733BD1E3AB83B915F95D3540EAA809C1E6E8C248A1EA1AE1D3B29C804F855167F64DA0AB06E5D89080D77D95A6E7199B079925922EA8735DF7654A01B350D67472F25B79DE5FF65B7E9156AEFC8818A1D9216BC4BE527DDC7D88F249B8745CF7DF1610A8237EB4BC1325C64FF47BD34B32CFE59720EC7FB52608D9009C70203010001}`,
tcPubKeyAlias2: `PubKey(go-peer/rsa){3082020A0282020100C17B6FA53983050B0339A0AB60D20A8A5FF5F8210564464C45CD2FAC2F266E8DDBA3B36C6F356AE57D1A71EED7B612C4CBC808557E4FCBAF6EDCFCECE37494144F09D65C7533109CE2F9B9B31D754453CA636A4463594F2C38303AE1B7BFFE738AC57805C782193B4854FF3F3FACA2C6BF9F75428DF6C583FBC29614C0B3329DF50F7B6399E1CC1F12BED77F29F885D7137ADFADE74A43451BB97A32F2301BE8EA866AFF34D6C7ED7FF1FAEA11FFB5B1034602B67E7918E42CA3D20E3E68AA700BE1B55A78C73A1D60D0A3DED3A6E5778C0BA68BAB9C345462131B9DC554D1A189066D649D7E167621815AB5B93905582BF19C28BCA6018E0CD205702968885E92A3B1E3DB37A25AC26FA4D2A47FF024ECD401F79FA353FEF2E4C2183C44D1D44B44938D32D8DBEDDAF5C87D042E4E9DAD671BE9C10DD8B3FE0A7C29AFE20843FE268C6A8F14949A04FF25A3EEE1EBE0027A99CE1C4DC561697297EA9FD9E23CF2E190B58CA385B66A235290A23CBB3856108EFFDD775601B3DE92C06C9EA2695C2D25D7897FD9D43C1AE10016E51C46C67F19AC84CD25F47DE2962A48030BCD8A0F14FFE4135A2893F62AC3E15CC61EC2E4ACADE0736C9A8DBC17D439248C42C5C0C6E08612414170FBE5AA6B52AE64E4CCDAE6FD3066BED5C200E07DBB0167D74A9FAD263AF253DFA870F44407F8EF3D9F12B8D910C4D803AD82ABA136F93F0203010001}`,
Expand Down Expand Up @@ -69,6 +75,10 @@ const (
"%s",
"%s"
],
"backup_connections": [
"%s",
"%s"
],
"friends": {
"%s": "%s",
"%s": "%s"
Expand All @@ -95,6 +105,8 @@ func testNewConfigString() string {
tcNetwork,
tgConnects[0],
tgConnects[1],
tgBackupConnects[0],
tgBackupConnects[1],
tcPubKeyAlias1,
tgPubKeys[tcPubKeyAlias1],
tcPubKeyAlias2,
Expand All @@ -110,14 +122,115 @@ func testConfigDefaultInit(configPath string) {
filesystem.OpenFile(configPath).Write([]byte(testNewConfigString()))
}

func TestConfig(t *testing.T) {
func TestBuildConfig(t *testing.T) {
t.Parallel()

configFile := fmt.Sprintf(tcConfigFileTemplate, 0)
config1File := fmt.Sprintf(tcConfigFileTemplate, 2)
config2File := fmt.Sprintf(tcConfigFileTemplate, 3)

testConfigDefaultInit(configFile)
testConfigDefaultInit(config1File)
defer os.Remove(config1File)

cfg, err := LoadConfig(config1File)
if err != nil {
t.Error(err)
return
}

if _, err := BuildConfig(config2File, &SConfig{}); err == nil {
t.Error("success build config with void structure")
return
}

if _, err := BuildConfig(config2File, cfg.(*SConfig)); err != nil {
t.Error(err)
return
}
defer os.Remove(config2File)

if _, err := BuildConfig(config2File, cfg.(*SConfig)); err == nil {
t.Error("success build already exist config")
return
}
}

func testIncorrectConfig(configFile string) error {
if _, err := LoadConfig(configFile); err == nil {
return errors.NewError("success load config on non exist file")
}

if err := filesystem.OpenFile(configFile).Write([]byte("abc")); err != nil {
return err
}

if _, err := LoadConfig(configFile); err == nil {
return errors.NewError("success load config with invalid structure")
}

cfg1Bytes := []byte(strings.ReplaceAll(testNewConfigString(), "settings", "settings_v2"))
if err := filesystem.OpenFile(configFile).Write(cfg1Bytes); err != nil {
return err
}

if _, err := LoadConfig(configFile); err == nil {
return errors.NewError("success load config with required fields (settings)")
}

cfg2Bytes := []byte(strings.ReplaceAll(testNewConfigString(), "PubKey", "PubKey_v2"))
if err := filesystem.OpenFile(configFile).Write(cfg2Bytes); err != nil {
return err
}

if _, err := LoadConfig(configFile); err == nil {
return errors.NewError("success load config with invalid fields (friends)")
}

cfg3Bytes := []byte(strings.ReplaceAll(testNewConfigString(), "erro", "erro_v2"))
if err := filesystem.OpenFile(configFile).Write(cfg3Bytes); err != nil {
return err
}

if _, err := LoadConfig(configFile); err == nil {
return errors.NewError("success load config with invalid fields (logging)")
}

pubKey1 := tgPubKeys[tcPubKeyAlias1]
pubKey2 := tgPubKeys[tcPubKeyAlias2]

cfg4Bytes := []byte(strings.ReplaceAll(testNewConfigString(), pubKey1, pubKey2))
if err := filesystem.OpenFile(configFile).Write(cfg4Bytes); err != nil {
return err
}

if _, err := LoadConfig(configFile); err == nil {
return errors.NewError("success load config with invalid fields (duplicate publc keys)")
}

newPubKey := asymmetric.NewRSAPrivKey(512).GetPubKey().ToString()
cfg5Bytes := []byte(strings.ReplaceAll(testNewConfigString(), pubKey1, newPubKey))
if err := filesystem.OpenFile(configFile).Write(cfg5Bytes); err != nil {
return err
}

if _, err := LoadConfig(configFile); err == nil {
return errors.NewError("success load config with invalid fields (diff key sizes)")
}

return nil
}

func TestComplexConfig(t *testing.T) {
t.Parallel()

configFile := fmt.Sprintf(tcConfigFileTemplate, 0)
defer os.Remove(configFile)

if err := testIncorrectConfig(configFile); err != nil {
t.Error(err)
return
}

testConfigDefaultInit(configFile)
cfg, err := LoadConfig(configFile)
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -200,6 +313,17 @@ func TestConfig(t *testing.T) {
}
}

if len(cfg.GetBackupConnections()) != 2 {
t.Error("len backup connections != 2")
return
}
for i, v := range cfg.GetBackupConnections() {
if v != tgBackupConnects[i] {
t.Errorf("backup connection '%d' is invalid", i)
return
}
}

for k, v := range tgServices {
v1, ok := cfg.GetService(k)
if !ok {
Expand Down
16 changes: 16 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,19 @@ func (p *sObject) DoSomething() {
_ = p.GetValue() // correct
}
```

### 6. Args/Returns interfaces

It is not allowed to use global structures in function arguments or when returning. Interfaces should be used instead of structures.

The following is allowed.
```go
func doObject(_ IObject) {}
func newObject() IObject {}
```

The following is not allowed.
```go
func doObject(_ *SObject) {}
func newObject() *SObject {}
```
2 changes: 1 addition & 1 deletion test/result/badge.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit d35b65b

Please sign in to comment.