|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + |
| 8 | + logger "github.com/number571/go-peer/internal/logger/std" |
| 9 | + "github.com/number571/go-peer/pkg/crypto/asymmetric" |
| 10 | + "github.com/number571/go-peer/pkg/crypto/random" |
| 11 | + "github.com/number571/go-peer/pkg/stringtools" |
| 12 | + testutils "github.com/number571/go-peer/test/_data" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + tcNewNetworkKey = "abc_network_key" |
| 17 | +) |
| 18 | + |
| 19 | +var ( |
| 20 | + tgNewConnections = []string{"a", "b", "c", "b"} |
| 21 | + tgNewBackupConnections = []string{"x", "y", "z", "y"} |
| 22 | + tgNewFriends = map[string]asymmetric.IPubKey{ |
| 23 | + "a": asymmetric.LoadRSAPubKey(testutils.TgPubKeys[2]), |
| 24 | + "b": asymmetric.LoadRSAPubKey(testutils.TgPubKeys[3]), |
| 25 | + } |
| 26 | + |
| 27 | + // diff size of keys: 1024, 4096 bits |
| 28 | + tgNewIncorrect1Friends = map[string]asymmetric.IPubKey{ |
| 29 | + "a": asymmetric.LoadRSAPubKey(testutils.TgPubKeys[2]), |
| 30 | + "b": asymmetric.LoadRSAPubKey(`PubKey(go-peer/rsa){3082020A0282020100C62F3CFA3D9809EE6DD77EBBFD38BC6796ABA76B795B3C76D3449F0AC808E01EDA8B2B08C58E508C306B2D842A2D317FF6B6D4A13EB76C7BBD5B157B663C3390B227476F4985EF649510D8CCA38FAB9FFCD67916FE73DB77595AB64FBE66D85892708A2DBCA94447A628F183FA6328136FCF158688CB6664EBA91F4C41621741786D50E3286AF9CAB81C101BDB19ACF42E10041CFDA5C6F30ACBBC4251E3D13C0E0781CBDC622E4ED490DD76BBA04D0A9C0012EBDAA77BD9F23183205A9D533C95A6C1FAAD8AB7C3B21FA4C76F7A3FB8EAEB231083ED925C1F71D23671E8C90E460C673A0DCD82ECFA956DF315200554571A99D79EB1E744681B9652389DBA6B9937CE476EBCAC34D02AEACF381DA40469B2F23E4F3DBFD5D8E04031708E46C31E3DC94342298E6F83CF7869C1209ACE2EA04FDB011D0FE265C8D51CF7D90C947160415B3415DFF9D1B16D5A9961F896109223B1408E740C421C6F413FA7B3D7094144DE4A0211DCAF043BC1A9FDE120251CBD654E705795D692A912F0543FF2F13EC733BD1E3AB83B915F95D3540EAA809C1E6E8C248A1EA1AE1D3B29C804F855167F64DA0AB06E5D89080D77D95A6E7199B079925922EA8735DF7654A01B350D67472F25B79DE5FF65B7E9156AEFC8818A1D9216BC4BE527DDC7D88F249B8745CF7DF1610A8237EB4BC1325C64FF47BD34B32CFE59720EC7FB52608D9009C70203010001}`), |
| 31 | + } |
| 32 | + |
| 33 | + // duplicated public keys |
| 34 | + tgNewIncorrect2Friends = map[string]asymmetric.IPubKey{ |
| 35 | + "a": asymmetric.LoadRSAPubKey(testutils.TgPubKeys[2]), |
| 36 | + "b": asymmetric.LoadRSAPubKey(testutils.TgPubKeys[2]), |
| 37 | + } |
| 38 | +) |
| 39 | + |
| 40 | +type tsConfig struct{} |
| 41 | + |
| 42 | +var ( |
| 43 | + _ IConfig = &tsConfig{} |
| 44 | +) |
| 45 | + |
| 46 | +func (p *tsConfig) GetSettings() IConfigSettings { return nil } |
| 47 | +func (p *tsConfig) GetLogging() logger.ILogging { return nil } |
| 48 | +func (p *tsConfig) GetAddress() IAddress { return nil } |
| 49 | +func (p *tsConfig) GetNetworkKey() string { return "" } |
| 50 | +func (p *tsConfig) GetConnections() []string { return nil } |
| 51 | +func (p *tsConfig) GetBackupConnections() []string { return nil } |
| 52 | +func (p *tsConfig) GetFriends() map[string]asymmetric.IPubKey { return nil } |
| 53 | +func (p *tsConfig) GetService(_ string) (string, bool) { return "", false } |
| 54 | + |
| 55 | +func TestPanicEditor(t *testing.T) { |
| 56 | + t.Parallel() |
| 57 | + |
| 58 | + for i := 0; i < 2; i++ { |
| 59 | + testPanicEditor(t, i) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func testPanicEditor(t *testing.T, n int) { |
| 64 | + defer func() { |
| 65 | + if r := recover(); r == nil { |
| 66 | + t.Error("nothing panics") |
| 67 | + return |
| 68 | + } |
| 69 | + }() |
| 70 | + switch n { |
| 71 | + case 0: |
| 72 | + _ = newEditor(nil) |
| 73 | + case 1: |
| 74 | + _ = newEditor(&tsConfig{}) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func TestEditor(t *testing.T) { |
| 79 | + t.Parallel() |
| 80 | + |
| 81 | + configFile := fmt.Sprintf(tcConfigFileTemplate, 4) |
| 82 | + defer os.Remove(configFile) |
| 83 | + |
| 84 | + testConfigDefaultInit(configFile) |
| 85 | + cfg, err := LoadConfig(configFile) |
| 86 | + if err != nil { |
| 87 | + t.Error(err) |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + wrapper := NewWrapper(cfg) |
| 92 | + |
| 93 | + config := wrapper.GetConfig() |
| 94 | + editor := wrapper.GetEditor() |
| 95 | + |
| 96 | + beforeNetworkKey := config.GetNetworkKey() |
| 97 | + beforeConnections := config.GetConnections() |
| 98 | + beforeBackupConnections := config.GetBackupConnections() |
| 99 | + beforeFriends := config.GetFriends() |
| 100 | + |
| 101 | + if err := editor.UpdateNetworkKey(tcNewNetworkKey); err != nil { |
| 102 | + t.Error(err) |
| 103 | + return |
| 104 | + } |
| 105 | + afterNetworkKey := config.GetNetworkKey() |
| 106 | + if beforeNetworkKey == afterNetworkKey { |
| 107 | + t.Error("beforeNetworkKey == afterNetworkKey") |
| 108 | + return |
| 109 | + } |
| 110 | + if afterNetworkKey != tcNewNetworkKey { |
| 111 | + t.Error("afterNetworkKey != tcNewNetworkKey") |
| 112 | + return |
| 113 | + } |
| 114 | + |
| 115 | + if err := editor.UpdateConnections(tgNewConnections); err != nil { |
| 116 | + t.Error(err) |
| 117 | + return |
| 118 | + } |
| 119 | + afterConnections := config.GetConnections() |
| 120 | + if len(afterConnections) != 3 { |
| 121 | + t.Error("failed deduplicate strings (connections)") |
| 122 | + return |
| 123 | + } |
| 124 | + hasNewConn := false |
| 125 | + for _, ac := range afterConnections { |
| 126 | + if !stringtools.HasInSlice(beforeConnections, ac) { |
| 127 | + hasNewConn = true |
| 128 | + break |
| 129 | + } |
| 130 | + } |
| 131 | + if !hasNewConn { |
| 132 | + t.Error("beforeConnections == afterConnections") |
| 133 | + return |
| 134 | + } |
| 135 | + for _, nc := range tgNewConnections { |
| 136 | + if !stringtools.HasInSlice(afterConnections, nc) { |
| 137 | + t.Error("afterConnections != tgNewConnections") |
| 138 | + return |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + if err := editor.UpdateBackupConnections(tgNewBackupConnections); err != nil { |
| 143 | + t.Error(err) |
| 144 | + return |
| 145 | + } |
| 146 | + afterBackupConnections := config.GetBackupConnections() |
| 147 | + if len(afterBackupConnections) != 3 { |
| 148 | + t.Error("failed deduplicate strings (backup connections)") |
| 149 | + return |
| 150 | + } |
| 151 | + hasNewBackupConn := false |
| 152 | + for _, ac := range afterBackupConnections { |
| 153 | + if !stringtools.HasInSlice(beforeBackupConnections, ac) { |
| 154 | + hasNewBackupConn = true |
| 155 | + break |
| 156 | + } |
| 157 | + } |
| 158 | + if !hasNewBackupConn { |
| 159 | + t.Error("beforeBackupConnections == afterBackupConnections") |
| 160 | + return |
| 161 | + } |
| 162 | + for _, nc := range tgNewBackupConnections { |
| 163 | + if !stringtools.HasInSlice(afterBackupConnections, nc) { |
| 164 | + t.Error("afterBackupConnections != tgNewBackupConnections") |
| 165 | + return |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + if err := editor.UpdateFriends(tgNewFriends); err != nil { |
| 170 | + t.Error(err) |
| 171 | + return |
| 172 | + } |
| 173 | + afterFriends := config.GetFriends() |
| 174 | + if len(afterFriends) != 2 { |
| 175 | + t.Error("failed deduplicate public keys (friends)") |
| 176 | + return |
| 177 | + } |
| 178 | + for af := range afterFriends { |
| 179 | + if _, ok := beforeFriends[af]; ok { |
| 180 | + t.Error("beforeFriends == afterFriends") |
| 181 | + return |
| 182 | + } |
| 183 | + } |
| 184 | + for nf := range tgNewFriends { |
| 185 | + if _, ok := afterFriends[nf]; !ok { |
| 186 | + t.Error("afterFriends != tgNewFriends") |
| 187 | + return |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + if err := editor.UpdateFriends(tgNewIncorrect2Friends); err == nil { |
| 192 | + t.Error("success update friends with duplicates") |
| 193 | + return |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +func TestIncorrectFilepathEditor(t *testing.T) { |
| 198 | + t.Parallel() |
| 199 | + |
| 200 | + configFile := fmt.Sprintf(tcConfigFileTemplate, 5) |
| 201 | + defer os.Remove(configFile) |
| 202 | + |
| 203 | + testConfigDefaultInit(configFile) |
| 204 | + cfg, err := LoadConfig(configFile) |
| 205 | + if err != nil { |
| 206 | + t.Error(err) |
| 207 | + return |
| 208 | + } |
| 209 | + |
| 210 | + wrapper := NewWrapper(cfg) |
| 211 | + |
| 212 | + config := wrapper.GetConfig().(*SConfig) |
| 213 | + editor := wrapper.GetEditor() |
| 214 | + |
| 215 | + config.fFilepath = random.NewStdPRNG().GetString(32) |
| 216 | + |
| 217 | + if err := editor.UpdateNetworkKey(tcNewNetworkKey); err == nil { |
| 218 | + t.Error("success update network key with incorrect filepath") |
| 219 | + return |
| 220 | + } |
| 221 | + |
| 222 | + if err := editor.UpdateConnections(tgNewConnections); err == nil { |
| 223 | + t.Error("success update connections with incorrect filepath") |
| 224 | + return |
| 225 | + } |
| 226 | + |
| 227 | + if err := editor.UpdateBackupConnections(tgNewBackupConnections); err == nil { |
| 228 | + t.Error("success update backup connections with incorrect filepath") |
| 229 | + return |
| 230 | + } |
| 231 | + |
| 232 | + if err := editor.UpdateFriends(tgNewIncorrect1Friends); err == nil { |
| 233 | + t.Error("success update friends with incorrect key sizes") |
| 234 | + return |
| 235 | + } |
| 236 | + |
| 237 | + if err := editor.UpdateFriends(tgNewFriends); err == nil { |
| 238 | + t.Error("success update friends with incorrect filepath") |
| 239 | + return |
| 240 | + } |
| 241 | +} |
0 commit comments