From 03415d3169f5603bee18eeb0baaaa410ab102f4f Mon Sep 17 00:00:00 2001 From: Mikhail Savochkin Date: Mon, 5 Nov 2018 15:22:19 +0600 Subject: [PATCH 1/5] New tests for endpoint --- CHANGELOG | 6 +++--- lib/endpoint.go | 18 +++++++++++++++++- lib/endpoint_test.go | 9 ++++++++- lib/errors_test.go | 16 ++++++++++++++++ 4 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 lib/errors_test.go diff --git a/CHANGELOG b/CHANGELOG index 895705e5..5ed966a4 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -5,10 +5,10 @@ * Implemented latency measurement for peers (#445) * Implemented latency measurement for proxies (#907) * Added reconnect timeout for failing DHT connections (#1114) -* Refactored save/restore sybsysten (#1106) -* Save dump now uses YAML (#1105) +* Refactored save/restore sybsystem (#1106) +* Dump file now uses YAML (#1105) * Output to stderr instead of stdout on non-zero exit code (#1102) -* Fixed non-zero exit code on failed start attempt (#1132) +* Fixed non-zero exit code on failed start attempts (#1132) ## [8.0.1] 10/09/2018 diff --git a/lib/endpoint.go b/lib/endpoint.go index b1a8d101..c1897d85 100644 --- a/lib/endpoint.go +++ b/lib/endpoint.go @@ -24,6 +24,14 @@ func (e *Endpoint) Measure(n *Network, id string) { return } + if e.Addr == nil { + return + } + + if n == nil { + return + } + if time.Since(e.LastLatencyQuery) < EndpointLatencyRequestInterval { return } @@ -31,10 +39,15 @@ func (e *Endpoint) Measure(n *Network, id string) { e.LastLatencyQuery = time.Now() ts, _ := time.Now().MarshalBinary() + ba := e.addrToBytes() + + if ba == nil { + return + } payload := []byte{} payload = append(payload, LatencyRequestHeader...) - payload = append(payload, e.addrToBytes()...) + payload = append(payload, ba...) payload = append(payload, []byte(id)...) payload = append(payload, ts...) @@ -57,6 +70,9 @@ func (e *Endpoint) addrToBytes() []byte { ipfield := make([]byte, 6) ip4 := e.Addr.IP.To4() + if len(ip4) != 4 { + return nil + } port := e.Addr.Port copy(ipfield[0:4], ip4[:4]) diff --git a/lib/endpoint_test.go b/lib/endpoint_test.go index 0614d54f..aadc1c82 100644 --- a/lib/endpoint_test.go +++ b/lib/endpoint_test.go @@ -39,6 +39,7 @@ func TestEndpoint_addrToBytes(t *testing.T) { fields fields want []byte }{ + {"nil addr", fields{}, nil}, {"Testing 127.0.0.1:1111", fields{Addr: a1}, r1}, {"Testing 0.0.0.0:0000", fields{Addr: a2}, r2}, {"Testing 255.255.255.255:65535", fields{Addr: a3}, r3}, @@ -74,12 +75,18 @@ func TestEndpoint_Measure(t *testing.T) { n *Network id string } + addr, _ := net.ResolveUDPAddr("udp4", "127.0.0.1:1234") tests := []struct { name string fields fields args args }{ - // TODO: Add test cases. + {"Broken EP", fields{broken: true}, args{}}, + {"nil addr", fields{}, args{}}, + {"nil network", fields{Addr: new(net.UDPAddr)}, args{}}, + {"time not passed", fields{Addr: new(net.UDPAddr), LastLatencyQuery: time.Now()}, args{new(Network), ""}}, + {"address bytes is nil", fields{Addr: new(net.UDPAddr), LastLatencyQuery: time.Unix(0, 0)}, args{new(Network), ""}}, + {"proper addr", fields{Addr: addr, LastLatencyQuery: time.Unix(0, 0)}, args{new(Network), ""}}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/lib/errors_test.go b/lib/errors_test.go new file mode 100644 index 00000000..3bc4b9cc --- /dev/null +++ b/lib/errors_test.go @@ -0,0 +1,16 @@ +package ptp + +import "testing" + +func TestInitErrors(t *testing.T) { + tests := []struct { + name string + }{ + {"init"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + InitErrors() + }) + } +} From 4f6fdb57805c2d1b57b66e1748ab7acbaf92d8ce Mon Sep 17 00:00:00 2001 From: Mikhail Savochkin Date: Mon, 5 Nov 2018 16:38:16 +0600 Subject: [PATCH 2/5] Covered DHT callbacks --- lib/dht_callbacks.go | 30 +- lib/dht_callbacks_test.go | 1221 ++++++++++++++++++++++++++++++++++++- lib/peerlist.go | 17 +- 3 files changed, 1233 insertions(+), 35 deletions(-) diff --git a/lib/dht_callbacks.go b/lib/dht_callbacks.go index 41f2c462..3f3a780b 100644 --- a/lib/dht_callbacks.go +++ b/lib/dht_callbacks.go @@ -12,6 +12,10 @@ import ( type dhtCallback func(*protocol.DHTPacket) error func (p *PeerToPeer) setupTCPCallbacks() { + if p.Dht == nil { + Log(Error, "Can't setup TCP callbacks: DHT is nil") + return + } p.Dht.TCPCallbacks = make(map[protocol.DHTPacketType]dhtCallback) p.Dht.TCPCallbacks[protocol.DHTPacketType_BadProxy] = p.packetBadProxy p.Dht.TCPCallbacks[protocol.DHTPacketType_Connect] = p.packetConnect @@ -39,6 +43,12 @@ func (p *PeerToPeer) packetBadProxy(packet *protocol.DHTPacket) error { // Handshake response should be handled here. func (p *PeerToPeer) packetConnect(packet *protocol.DHTPacket) error { + if p.Dht == nil { + return fmt.Errorf("nil dht") + } + if packet == nil { + return fmt.Errorf("nil packet") + } if len(packet.Id) != 36 { return fmt.Errorf("Received malformed ID") } @@ -49,6 +59,12 @@ func (p *PeerToPeer) packetConnect(packet *protocol.DHTPacket) error { } func (p *PeerToPeer) packetDHCP(packet *protocol.DHTPacket) error { + if p.Dht == nil { + return fmt.Errorf("nil dht") + } + if packet == nil { + return fmt.Errorf("nil packet") + } if packet.Data != "" && packet.Extra != "" { ip, network, err := net.ParseCIDR(fmt.Sprintf("%s/%s", packet.Data, packet.Extra)) if err != nil { @@ -63,6 +79,9 @@ func (p *PeerToPeer) packetDHCP(packet *protocol.DHTPacket) error { } func (p *PeerToPeer) packetError(packet *protocol.DHTPacket) error { + if packet == nil { + return fmt.Errorf("nil packet") + } lvl := LogLevel(Trace) if packet.Data == "" { lvl = Error @@ -76,14 +95,23 @@ func (p *PeerToPeer) packetError(packet *protocol.DHTPacket) error { } func (p *PeerToPeer) packetFind(packet *protocol.DHTPacket) error { + if packet == nil { + return fmt.Errorf("nil packet") + } + if p.Dht == nil { + return fmt.Errorf("nil dht") + } if len(packet.Arguments) == 0 { Log(Warning, "Received empty peer list") return nil } if packet.Data == p.Dht.ID { - Log(Debug, "Skipping self") + Log(Debug, "Skipping self [%s = %s]", packet.Data, p.Dht.ID) return nil } + if p.Peers == nil { + return fmt.Errorf("nil peer list") + } Log(Debug, "Received `find`: %+v", packet) peer := p.Peers.GetPeer(packet.Data) diff --git a/lib/dht_callbacks_test.go b/lib/dht_callbacks_test.go index 873fbe59..51a1fd1a 100644 --- a/lib/dht_callbacks_test.go +++ b/lib/dht_callbacks_test.go @@ -1,41 +1,1200 @@ package ptp import ( + "net" + "sync" "testing" "github.com/subutai-io/p2p/protocol" ) -func TestPacketConnect(t *testing.T) { - ptp := new(PeerToPeer) - pct := new(protocol.DHTPacket) - id := "12345" - pct.Id = id - err := ptp.packetConnect(pct) - if err == nil { - t.Error("Wrong value of identificator") - } -} - -func TestPacketError(t *testing.T) { - ptp := new(PeerToPeer) - pct := new(protocol.DHTPacket) - data1 := "" - pct.Data = data1 - err := ptp.packetError(pct) - if err != nil { - t.Error("err") - } - data2 := "Warning" - pct.Data = data2 - err2 := ptp.packetError(pct) - if err2 != nil { - t.Error("Error") - } - data := "Error" - pct.Data = data - err3 := ptp.packetError(pct) - if err3 != nil { - t.Error("Error") +func TestPeerToPeer_setupTCPCallbacks(t *testing.T) { + type fields struct { + Config Configuration + UDPSocket *Network + LocalIPs []net.IP + Dht *DHTClient + Crypter Crypto + Shutdown bool + ForwardMode bool + ReadyToStop bool + MessageHandlers map[uint16]MessageHandler + PacketHandlers map[PacketType]PacketHandlerCallback + PeersLock sync.Mutex + Hash string + Interface TAP + Peers *PeerList + HolePunching sync.Mutex + ProxyManager *ProxyManager + outboundIP net.IP + UsePMTU bool + } + tests := []struct { + name string + fields fields + }{ + {"nil dht", fields{}}, + {"setup", fields{Dht: new(DHTClient)}}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &PeerToPeer{ + Config: tt.fields.Config, + UDPSocket: tt.fields.UDPSocket, + LocalIPs: tt.fields.LocalIPs, + Dht: tt.fields.Dht, + Crypter: tt.fields.Crypter, + Shutdown: tt.fields.Shutdown, + ForwardMode: tt.fields.ForwardMode, + ReadyToStop: tt.fields.ReadyToStop, + MessageHandlers: tt.fields.MessageHandlers, + PacketHandlers: tt.fields.PacketHandlers, + PeersLock: tt.fields.PeersLock, + Hash: tt.fields.Hash, + Interface: tt.fields.Interface, + Peers: tt.fields.Peers, + HolePunching: tt.fields.HolePunching, + ProxyManager: tt.fields.ProxyManager, + outboundIP: tt.fields.outboundIP, + UsePMTU: tt.fields.UsePMTU, + } + p.setupTCPCallbacks() + }) + } +} + +func TestPeerToPeer_packetBadProxy(t *testing.T) { + type fields struct { + Config Configuration + UDPSocket *Network + LocalIPs []net.IP + Dht *DHTClient + Crypter Crypto + Shutdown bool + ForwardMode bool + ReadyToStop bool + MessageHandlers map[uint16]MessageHandler + PacketHandlers map[PacketType]PacketHandlerCallback + PeersLock sync.Mutex + Hash string + Interface TAP + Peers *PeerList + HolePunching sync.Mutex + ProxyManager *ProxyManager + outboundIP net.IP + UsePMTU bool + } + type args struct { + packet *protocol.DHTPacket + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + {"bad proxy", fields{}, args{}, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &PeerToPeer{ + Config: tt.fields.Config, + UDPSocket: tt.fields.UDPSocket, + LocalIPs: tt.fields.LocalIPs, + Dht: tt.fields.Dht, + Crypter: tt.fields.Crypter, + Shutdown: tt.fields.Shutdown, + ForwardMode: tt.fields.ForwardMode, + ReadyToStop: tt.fields.ReadyToStop, + MessageHandlers: tt.fields.MessageHandlers, + PacketHandlers: tt.fields.PacketHandlers, + PeersLock: tt.fields.PeersLock, + Hash: tt.fields.Hash, + Interface: tt.fields.Interface, + Peers: tt.fields.Peers, + HolePunching: tt.fields.HolePunching, + ProxyManager: tt.fields.ProxyManager, + outboundIP: tt.fields.outboundIP, + UsePMTU: tt.fields.UsePMTU, + } + if err := p.packetBadProxy(tt.args.packet); (err != nil) != tt.wantErr { + t.Errorf("PeerToPeer.packetBadProxy() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestPeerToPeer_packetConnect(t *testing.T) { + type fields struct { + Config Configuration + UDPSocket *Network + LocalIPs []net.IP + Dht *DHTClient + Crypter Crypto + Shutdown bool + ForwardMode bool + ReadyToStop bool + MessageHandlers map[uint16]MessageHandler + PacketHandlers map[PacketType]PacketHandlerCallback + PeersLock sync.Mutex + Hash string + Interface TAP + Peers *PeerList + HolePunching sync.Mutex + ProxyManager *ProxyManager + outboundIP net.IP + UsePMTU bool + } + type args struct { + packet *protocol.DHTPacket + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + {"nil dht", fields{}, args{}, true}, + {"nil packet", fields{Dht: new(DHTClient)}, args{}, true}, + {"small id", fields{Dht: new(DHTClient)}, args{&protocol.DHTPacket{Id: "123"}}, true}, + {"normal id", fields{Dht: new(DHTClient)}, args{&protocol.DHTPacket{Id: "123e4567-e89b-12d3-a456-426655440000"}}, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &PeerToPeer{ + Config: tt.fields.Config, + UDPSocket: tt.fields.UDPSocket, + LocalIPs: tt.fields.LocalIPs, + Dht: tt.fields.Dht, + Crypter: tt.fields.Crypter, + Shutdown: tt.fields.Shutdown, + ForwardMode: tt.fields.ForwardMode, + ReadyToStop: tt.fields.ReadyToStop, + MessageHandlers: tt.fields.MessageHandlers, + PacketHandlers: tt.fields.PacketHandlers, + PeersLock: tt.fields.PeersLock, + Hash: tt.fields.Hash, + Interface: tt.fields.Interface, + Peers: tt.fields.Peers, + HolePunching: tt.fields.HolePunching, + ProxyManager: tt.fields.ProxyManager, + outboundIP: tt.fields.outboundIP, + UsePMTU: tt.fields.UsePMTU, + } + if err := p.packetConnect(tt.args.packet); (err != nil) != tt.wantErr { + t.Errorf("PeerToPeer.packetConnect() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestPeerToPeer_packetDHCP(t *testing.T) { + type fields struct { + Config Configuration + UDPSocket *Network + LocalIPs []net.IP + Dht *DHTClient + Crypter Crypto + Shutdown bool + ForwardMode bool + ReadyToStop bool + MessageHandlers map[uint16]MessageHandler + PacketHandlers map[PacketType]PacketHandlerCallback + PeersLock sync.Mutex + Hash string + Interface TAP + Peers *PeerList + HolePunching sync.Mutex + ProxyManager *ProxyManager + outboundIP net.IP + UsePMTU bool + } + type args struct { + packet *protocol.DHTPacket + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + {"nil dht", fields{}, args{}, true}, + {"nil packet", fields{Dht: new(DHTClient)}, args{}, true}, + {"broken cidr", fields{Dht: new(DHTClient)}, args{&protocol.DHTPacket{Data: "d", Extra: "e"}}, true}, + {"normal packet", fields{Dht: new(DHTClient)}, args{&protocol.DHTPacket{Data: "192.168.0.1", Extra: "32"}}, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &PeerToPeer{ + Config: tt.fields.Config, + UDPSocket: tt.fields.UDPSocket, + LocalIPs: tt.fields.LocalIPs, + Dht: tt.fields.Dht, + Crypter: tt.fields.Crypter, + Shutdown: tt.fields.Shutdown, + ForwardMode: tt.fields.ForwardMode, + ReadyToStop: tt.fields.ReadyToStop, + MessageHandlers: tt.fields.MessageHandlers, + PacketHandlers: tt.fields.PacketHandlers, + PeersLock: tt.fields.PeersLock, + Hash: tt.fields.Hash, + Interface: tt.fields.Interface, + Peers: tt.fields.Peers, + HolePunching: tt.fields.HolePunching, + ProxyManager: tt.fields.ProxyManager, + outboundIP: tt.fields.outboundIP, + UsePMTU: tt.fields.UsePMTU, + } + if err := p.packetDHCP(tt.args.packet); (err != nil) != tt.wantErr { + t.Errorf("PeerToPeer.packetDHCP() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestPeerToPeer_packetError(t *testing.T) { + type fields struct { + Config Configuration + UDPSocket *Network + LocalIPs []net.IP + Dht *DHTClient + Crypter Crypto + Shutdown bool + ForwardMode bool + ReadyToStop bool + MessageHandlers map[uint16]MessageHandler + PacketHandlers map[PacketType]PacketHandlerCallback + PeersLock sync.Mutex + Hash string + Interface TAP + Peers *PeerList + HolePunching sync.Mutex + ProxyManager *ProxyManager + outboundIP net.IP + UsePMTU bool + } + type args struct { + packet *protocol.DHTPacket + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + {"nil packet", fields{}, args{}, true}, + {"empty level", fields{}, args{&protocol.DHTPacket{}}, false}, + {"warning level", fields{}, args{&protocol.DHTPacket{Data: "Warning"}}, false}, + {"error level", fields{}, args{&protocol.DHTPacket{Data: "Error"}}, false}, + {"unknown level", fields{}, args{&protocol.DHTPacket{Data: "unknown"}}, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &PeerToPeer{ + Config: tt.fields.Config, + UDPSocket: tt.fields.UDPSocket, + LocalIPs: tt.fields.LocalIPs, + Dht: tt.fields.Dht, + Crypter: tt.fields.Crypter, + Shutdown: tt.fields.Shutdown, + ForwardMode: tt.fields.ForwardMode, + ReadyToStop: tt.fields.ReadyToStop, + MessageHandlers: tt.fields.MessageHandlers, + PacketHandlers: tt.fields.PacketHandlers, + PeersLock: tt.fields.PeersLock, + Hash: tt.fields.Hash, + Interface: tt.fields.Interface, + Peers: tt.fields.Peers, + HolePunching: tt.fields.HolePunching, + ProxyManager: tt.fields.ProxyManager, + outboundIP: tt.fields.outboundIP, + UsePMTU: tt.fields.UsePMTU, + } + if err := p.packetError(tt.args.packet); (err != nil) != tt.wantErr { + t.Errorf("PeerToPeer.packetError() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestPeerToPeer_packetFind(t *testing.T) { + type fields struct { + Config Configuration + UDPSocket *Network + LocalIPs []net.IP + Dht *DHTClient + Crypter Crypto + Shutdown bool + ForwardMode bool + ReadyToStop bool + MessageHandlers map[uint16]MessageHandler + PacketHandlers map[PacketType]PacketHandlerCallback + PeersLock sync.Mutex + Hash string + Interface TAP + Peers *PeerList + HolePunching sync.Mutex + ProxyManager *ProxyManager + outboundIP net.IP + UsePMTU bool + } + + SetMinLogLevel(Debug) + + tdht := new(DHTClient) + tdht.ID = "testid" + + pl0 := new(PeerList) + pl0.Init() + + knownIP, _ := net.ResolveUDPAddr("udp4", "192.168.1.2:3456") + + pl1 := new(PeerList) + pl1.Init() + pl1.Update("testid", new(NetworkPeer)) + pl1.Update("testid2", &NetworkPeer{ + KnownIPs: []*net.UDPAddr{knownIP}, + }) + + lip0 := []net.IP{} + lip0 = append(lip0, net.ParseIP("192.168.1.2")) + + type args struct { + packet *protocol.DHTPacket + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + {"nil packet", fields{}, args{}, true}, + {"nil dht", fields{}, args{new(protocol.DHTPacket)}, true}, + {"empty peer list", fields{Dht: tdht}, args{new(protocol.DHTPacket)}, false}, + {"skip self", fields{Dht: tdht}, args{&protocol.DHTPacket{Data: tdht.ID, Arguments: []string{"arg1"}}}, false}, + {"nil peer list", fields{Dht: tdht}, args{&protocol.DHTPacket{Data: "ttt", Arguments: []string{"arg1"}}}, true}, + {"new peer>bad ip", fields{Dht: tdht, Peers: pl0}, args{&protocol.DHTPacket{Data: "ttt", Arguments: []string{"arg1"}}}, false}, + {"new peer>known ip", fields{Dht: tdht, Peers: pl1}, args{&protocol.DHTPacket{Data: "ttt", Arguments: []string{knownIP.String(), knownIP.String()}}}, false}, + {"new peer>local ip", fields{Dht: tdht, Peers: pl1, LocalIPs: lip0}, args{&protocol.DHTPacket{Data: "ttt", Arguments: []string{knownIP.String()}}}, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &PeerToPeer{ + Config: tt.fields.Config, + UDPSocket: tt.fields.UDPSocket, + LocalIPs: tt.fields.LocalIPs, + Dht: tt.fields.Dht, + Crypter: tt.fields.Crypter, + Shutdown: tt.fields.Shutdown, + ForwardMode: tt.fields.ForwardMode, + ReadyToStop: tt.fields.ReadyToStop, + MessageHandlers: tt.fields.MessageHandlers, + PacketHandlers: tt.fields.PacketHandlers, + PeersLock: tt.fields.PeersLock, + Hash: tt.fields.Hash, + Interface: tt.fields.Interface, + Peers: tt.fields.Peers, + HolePunching: tt.fields.HolePunching, + ProxyManager: tt.fields.ProxyManager, + outboundIP: tt.fields.outboundIP, + UsePMTU: tt.fields.UsePMTU, + } + if err := p.packetFind(tt.args.packet); (err != nil) != tt.wantErr { + t.Errorf("PeerToPeer.packetFind() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestPeerToPeer_packetForward(t *testing.T) { + type fields struct { + Config Configuration + UDPSocket *Network + LocalIPs []net.IP + Dht *DHTClient + Crypter Crypto + Shutdown bool + ForwardMode bool + ReadyToStop bool + MessageHandlers map[uint16]MessageHandler + PacketHandlers map[PacketType]PacketHandlerCallback + PeersLock sync.Mutex + Hash string + Interface TAP + Peers *PeerList + HolePunching sync.Mutex + ProxyManager *ProxyManager + outboundIP net.IP + UsePMTU bool + } + type args struct { + packet *protocol.DHTPacket + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &PeerToPeer{ + Config: tt.fields.Config, + UDPSocket: tt.fields.UDPSocket, + LocalIPs: tt.fields.LocalIPs, + Dht: tt.fields.Dht, + Crypter: tt.fields.Crypter, + Shutdown: tt.fields.Shutdown, + ForwardMode: tt.fields.ForwardMode, + ReadyToStop: tt.fields.ReadyToStop, + MessageHandlers: tt.fields.MessageHandlers, + PacketHandlers: tt.fields.PacketHandlers, + PeersLock: tt.fields.PeersLock, + Hash: tt.fields.Hash, + Interface: tt.fields.Interface, + Peers: tt.fields.Peers, + HolePunching: tt.fields.HolePunching, + ProxyManager: tt.fields.ProxyManager, + outboundIP: tt.fields.outboundIP, + UsePMTU: tt.fields.UsePMTU, + } + if err := p.packetForward(tt.args.packet); (err != nil) != tt.wantErr { + t.Errorf("PeerToPeer.packetForward() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestPeerToPeer_packetNode(t *testing.T) { + type fields struct { + Config Configuration + UDPSocket *Network + LocalIPs []net.IP + Dht *DHTClient + Crypter Crypto + Shutdown bool + ForwardMode bool + ReadyToStop bool + MessageHandlers map[uint16]MessageHandler + PacketHandlers map[PacketType]PacketHandlerCallback + PeersLock sync.Mutex + Hash string + Interface TAP + Peers *PeerList + HolePunching sync.Mutex + ProxyManager *ProxyManager + outboundIP net.IP + UsePMTU bool + } + type args struct { + packet *protocol.DHTPacket + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &PeerToPeer{ + Config: tt.fields.Config, + UDPSocket: tt.fields.UDPSocket, + LocalIPs: tt.fields.LocalIPs, + Dht: tt.fields.Dht, + Crypter: tt.fields.Crypter, + Shutdown: tt.fields.Shutdown, + ForwardMode: tt.fields.ForwardMode, + ReadyToStop: tt.fields.ReadyToStop, + MessageHandlers: tt.fields.MessageHandlers, + PacketHandlers: tt.fields.PacketHandlers, + PeersLock: tt.fields.PeersLock, + Hash: tt.fields.Hash, + Interface: tt.fields.Interface, + Peers: tt.fields.Peers, + HolePunching: tt.fields.HolePunching, + ProxyManager: tt.fields.ProxyManager, + outboundIP: tt.fields.outboundIP, + UsePMTU: tt.fields.UsePMTU, + } + if err := p.packetNode(tt.args.packet); (err != nil) != tt.wantErr { + t.Errorf("PeerToPeer.packetNode() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestPeerToPeer_packetNotify(t *testing.T) { + type fields struct { + Config Configuration + UDPSocket *Network + LocalIPs []net.IP + Dht *DHTClient + Crypter Crypto + Shutdown bool + ForwardMode bool + ReadyToStop bool + MessageHandlers map[uint16]MessageHandler + PacketHandlers map[PacketType]PacketHandlerCallback + PeersLock sync.Mutex + Hash string + Interface TAP + Peers *PeerList + HolePunching sync.Mutex + ProxyManager *ProxyManager + outboundIP net.IP + UsePMTU bool + } + type args struct { + packet *protocol.DHTPacket + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &PeerToPeer{ + Config: tt.fields.Config, + UDPSocket: tt.fields.UDPSocket, + LocalIPs: tt.fields.LocalIPs, + Dht: tt.fields.Dht, + Crypter: tt.fields.Crypter, + Shutdown: tt.fields.Shutdown, + ForwardMode: tt.fields.ForwardMode, + ReadyToStop: tt.fields.ReadyToStop, + MessageHandlers: tt.fields.MessageHandlers, + PacketHandlers: tt.fields.PacketHandlers, + PeersLock: tt.fields.PeersLock, + Hash: tt.fields.Hash, + Interface: tt.fields.Interface, + Peers: tt.fields.Peers, + HolePunching: tt.fields.HolePunching, + ProxyManager: tt.fields.ProxyManager, + outboundIP: tt.fields.outboundIP, + UsePMTU: tt.fields.UsePMTU, + } + if err := p.packetNotify(tt.args.packet); (err != nil) != tt.wantErr { + t.Errorf("PeerToPeer.packetNotify() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestPeerToPeer_packetPing(t *testing.T) { + type fields struct { + Config Configuration + UDPSocket *Network + LocalIPs []net.IP + Dht *DHTClient + Crypter Crypto + Shutdown bool + ForwardMode bool + ReadyToStop bool + MessageHandlers map[uint16]MessageHandler + PacketHandlers map[PacketType]PacketHandlerCallback + PeersLock sync.Mutex + Hash string + Interface TAP + Peers *PeerList + HolePunching sync.Mutex + ProxyManager *ProxyManager + outboundIP net.IP + UsePMTU bool + } + type args struct { + packet *protocol.DHTPacket + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &PeerToPeer{ + Config: tt.fields.Config, + UDPSocket: tt.fields.UDPSocket, + LocalIPs: tt.fields.LocalIPs, + Dht: tt.fields.Dht, + Crypter: tt.fields.Crypter, + Shutdown: tt.fields.Shutdown, + ForwardMode: tt.fields.ForwardMode, + ReadyToStop: tt.fields.ReadyToStop, + MessageHandlers: tt.fields.MessageHandlers, + PacketHandlers: tt.fields.PacketHandlers, + PeersLock: tt.fields.PeersLock, + Hash: tt.fields.Hash, + Interface: tt.fields.Interface, + Peers: tt.fields.Peers, + HolePunching: tt.fields.HolePunching, + ProxyManager: tt.fields.ProxyManager, + outboundIP: tt.fields.outboundIP, + UsePMTU: tt.fields.UsePMTU, + } + if err := p.packetPing(tt.args.packet); (err != nil) != tt.wantErr { + t.Errorf("PeerToPeer.packetPing() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestPeerToPeer_packetProxy(t *testing.T) { + type fields struct { + Config Configuration + UDPSocket *Network + LocalIPs []net.IP + Dht *DHTClient + Crypter Crypto + Shutdown bool + ForwardMode bool + ReadyToStop bool + MessageHandlers map[uint16]MessageHandler + PacketHandlers map[PacketType]PacketHandlerCallback + PeersLock sync.Mutex + Hash string + Interface TAP + Peers *PeerList + HolePunching sync.Mutex + ProxyManager *ProxyManager + outboundIP net.IP + UsePMTU bool + } + type args struct { + packet *protocol.DHTPacket + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &PeerToPeer{ + Config: tt.fields.Config, + UDPSocket: tt.fields.UDPSocket, + LocalIPs: tt.fields.LocalIPs, + Dht: tt.fields.Dht, + Crypter: tt.fields.Crypter, + Shutdown: tt.fields.Shutdown, + ForwardMode: tt.fields.ForwardMode, + ReadyToStop: tt.fields.ReadyToStop, + MessageHandlers: tt.fields.MessageHandlers, + PacketHandlers: tt.fields.PacketHandlers, + PeersLock: tt.fields.PeersLock, + Hash: tt.fields.Hash, + Interface: tt.fields.Interface, + Peers: tt.fields.Peers, + HolePunching: tt.fields.HolePunching, + ProxyManager: tt.fields.ProxyManager, + outboundIP: tt.fields.outboundIP, + UsePMTU: tt.fields.UsePMTU, + } + if err := p.packetProxy(tt.args.packet); (err != nil) != tt.wantErr { + t.Errorf("PeerToPeer.packetProxy() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestPeerToPeer_packetRequestProxy(t *testing.T) { + type fields struct { + Config Configuration + UDPSocket *Network + LocalIPs []net.IP + Dht *DHTClient + Crypter Crypto + Shutdown bool + ForwardMode bool + ReadyToStop bool + MessageHandlers map[uint16]MessageHandler + PacketHandlers map[PacketType]PacketHandlerCallback + PeersLock sync.Mutex + Hash string + Interface TAP + Peers *PeerList + HolePunching sync.Mutex + ProxyManager *ProxyManager + outboundIP net.IP + UsePMTU bool + } + type args struct { + packet *protocol.DHTPacket + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &PeerToPeer{ + Config: tt.fields.Config, + UDPSocket: tt.fields.UDPSocket, + LocalIPs: tt.fields.LocalIPs, + Dht: tt.fields.Dht, + Crypter: tt.fields.Crypter, + Shutdown: tt.fields.Shutdown, + ForwardMode: tt.fields.ForwardMode, + ReadyToStop: tt.fields.ReadyToStop, + MessageHandlers: tt.fields.MessageHandlers, + PacketHandlers: tt.fields.PacketHandlers, + PeersLock: tt.fields.PeersLock, + Hash: tt.fields.Hash, + Interface: tt.fields.Interface, + Peers: tt.fields.Peers, + HolePunching: tt.fields.HolePunching, + ProxyManager: tt.fields.ProxyManager, + outboundIP: tt.fields.outboundIP, + UsePMTU: tt.fields.UsePMTU, + } + if err := p.packetRequestProxy(tt.args.packet); (err != nil) != tt.wantErr { + t.Errorf("PeerToPeer.packetRequestProxy() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestPeerToPeer_packetReportProxy(t *testing.T) { + type fields struct { + Config Configuration + UDPSocket *Network + LocalIPs []net.IP + Dht *DHTClient + Crypter Crypto + Shutdown bool + ForwardMode bool + ReadyToStop bool + MessageHandlers map[uint16]MessageHandler + PacketHandlers map[PacketType]PacketHandlerCallback + PeersLock sync.Mutex + Hash string + Interface TAP + Peers *PeerList + HolePunching sync.Mutex + ProxyManager *ProxyManager + outboundIP net.IP + UsePMTU bool + } + type args struct { + packet *protocol.DHTPacket + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &PeerToPeer{ + Config: tt.fields.Config, + UDPSocket: tt.fields.UDPSocket, + LocalIPs: tt.fields.LocalIPs, + Dht: tt.fields.Dht, + Crypter: tt.fields.Crypter, + Shutdown: tt.fields.Shutdown, + ForwardMode: tt.fields.ForwardMode, + ReadyToStop: tt.fields.ReadyToStop, + MessageHandlers: tt.fields.MessageHandlers, + PacketHandlers: tt.fields.PacketHandlers, + PeersLock: tt.fields.PeersLock, + Hash: tt.fields.Hash, + Interface: tt.fields.Interface, + Peers: tt.fields.Peers, + HolePunching: tt.fields.HolePunching, + ProxyManager: tt.fields.ProxyManager, + outboundIP: tt.fields.outboundIP, + UsePMTU: tt.fields.UsePMTU, + } + if err := p.packetReportProxy(tt.args.packet); (err != nil) != tt.wantErr { + t.Errorf("PeerToPeer.packetReportProxy() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestPeerToPeer_packetRegisterProxy(t *testing.T) { + type fields struct { + Config Configuration + UDPSocket *Network + LocalIPs []net.IP + Dht *DHTClient + Crypter Crypto + Shutdown bool + ForwardMode bool + ReadyToStop bool + MessageHandlers map[uint16]MessageHandler + PacketHandlers map[PacketType]PacketHandlerCallback + PeersLock sync.Mutex + Hash string + Interface TAP + Peers *PeerList + HolePunching sync.Mutex + ProxyManager *ProxyManager + outboundIP net.IP + UsePMTU bool + } + type args struct { + packet *protocol.DHTPacket + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &PeerToPeer{ + Config: tt.fields.Config, + UDPSocket: tt.fields.UDPSocket, + LocalIPs: tt.fields.LocalIPs, + Dht: tt.fields.Dht, + Crypter: tt.fields.Crypter, + Shutdown: tt.fields.Shutdown, + ForwardMode: tt.fields.ForwardMode, + ReadyToStop: tt.fields.ReadyToStop, + MessageHandlers: tt.fields.MessageHandlers, + PacketHandlers: tt.fields.PacketHandlers, + PeersLock: tt.fields.PeersLock, + Hash: tt.fields.Hash, + Interface: tt.fields.Interface, + Peers: tt.fields.Peers, + HolePunching: tt.fields.HolePunching, + ProxyManager: tt.fields.ProxyManager, + outboundIP: tt.fields.outboundIP, + UsePMTU: tt.fields.UsePMTU, + } + if err := p.packetRegisterProxy(tt.args.packet); (err != nil) != tt.wantErr { + t.Errorf("PeerToPeer.packetRegisterProxy() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestPeerToPeer_packetReportLoad(t *testing.T) { + type fields struct { + Config Configuration + UDPSocket *Network + LocalIPs []net.IP + Dht *DHTClient + Crypter Crypto + Shutdown bool + ForwardMode bool + ReadyToStop bool + MessageHandlers map[uint16]MessageHandler + PacketHandlers map[PacketType]PacketHandlerCallback + PeersLock sync.Mutex + Hash string + Interface TAP + Peers *PeerList + HolePunching sync.Mutex + ProxyManager *ProxyManager + outboundIP net.IP + UsePMTU bool + } + type args struct { + packet *protocol.DHTPacket + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &PeerToPeer{ + Config: tt.fields.Config, + UDPSocket: tt.fields.UDPSocket, + LocalIPs: tt.fields.LocalIPs, + Dht: tt.fields.Dht, + Crypter: tt.fields.Crypter, + Shutdown: tt.fields.Shutdown, + ForwardMode: tt.fields.ForwardMode, + ReadyToStop: tt.fields.ReadyToStop, + MessageHandlers: tt.fields.MessageHandlers, + PacketHandlers: tt.fields.PacketHandlers, + PeersLock: tt.fields.PeersLock, + Hash: tt.fields.Hash, + Interface: tt.fields.Interface, + Peers: tt.fields.Peers, + HolePunching: tt.fields.HolePunching, + ProxyManager: tt.fields.ProxyManager, + outboundIP: tt.fields.outboundIP, + UsePMTU: tt.fields.UsePMTU, + } + if err := p.packetReportLoad(tt.args.packet); (err != nil) != tt.wantErr { + t.Errorf("PeerToPeer.packetReportLoad() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestPeerToPeer_packetState(t *testing.T) { + type fields struct { + Config Configuration + UDPSocket *Network + LocalIPs []net.IP + Dht *DHTClient + Crypter Crypto + Shutdown bool + ForwardMode bool + ReadyToStop bool + MessageHandlers map[uint16]MessageHandler + PacketHandlers map[PacketType]PacketHandlerCallback + PeersLock sync.Mutex + Hash string + Interface TAP + Peers *PeerList + HolePunching sync.Mutex + ProxyManager *ProxyManager + outboundIP net.IP + UsePMTU bool + } + type args struct { + packet *protocol.DHTPacket + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &PeerToPeer{ + Config: tt.fields.Config, + UDPSocket: tt.fields.UDPSocket, + LocalIPs: tt.fields.LocalIPs, + Dht: tt.fields.Dht, + Crypter: tt.fields.Crypter, + Shutdown: tt.fields.Shutdown, + ForwardMode: tt.fields.ForwardMode, + ReadyToStop: tt.fields.ReadyToStop, + MessageHandlers: tt.fields.MessageHandlers, + PacketHandlers: tt.fields.PacketHandlers, + PeersLock: tt.fields.PeersLock, + Hash: tt.fields.Hash, + Interface: tt.fields.Interface, + Peers: tt.fields.Peers, + HolePunching: tt.fields.HolePunching, + ProxyManager: tt.fields.ProxyManager, + outboundIP: tt.fields.outboundIP, + UsePMTU: tt.fields.UsePMTU, + } + if err := p.packetState(tt.args.packet); (err != nil) != tt.wantErr { + t.Errorf("PeerToPeer.packetState() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestPeerToPeer_packetStop(t *testing.T) { + type fields struct { + Config Configuration + UDPSocket *Network + LocalIPs []net.IP + Dht *DHTClient + Crypter Crypto + Shutdown bool + ForwardMode bool + ReadyToStop bool + MessageHandlers map[uint16]MessageHandler + PacketHandlers map[PacketType]PacketHandlerCallback + PeersLock sync.Mutex + Hash string + Interface TAP + Peers *PeerList + HolePunching sync.Mutex + ProxyManager *ProxyManager + outboundIP net.IP + UsePMTU bool + } + type args struct { + packet *protocol.DHTPacket + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &PeerToPeer{ + Config: tt.fields.Config, + UDPSocket: tt.fields.UDPSocket, + LocalIPs: tt.fields.LocalIPs, + Dht: tt.fields.Dht, + Crypter: tt.fields.Crypter, + Shutdown: tt.fields.Shutdown, + ForwardMode: tt.fields.ForwardMode, + ReadyToStop: tt.fields.ReadyToStop, + MessageHandlers: tt.fields.MessageHandlers, + PacketHandlers: tt.fields.PacketHandlers, + PeersLock: tt.fields.PeersLock, + Hash: tt.fields.Hash, + Interface: tt.fields.Interface, + Peers: tt.fields.Peers, + HolePunching: tt.fields.HolePunching, + ProxyManager: tt.fields.ProxyManager, + outboundIP: tt.fields.outboundIP, + UsePMTU: tt.fields.UsePMTU, + } + if err := p.packetStop(tt.args.packet); (err != nil) != tt.wantErr { + t.Errorf("PeerToPeer.packetStop() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestPeerToPeer_packetUnknown(t *testing.T) { + type fields struct { + Config Configuration + UDPSocket *Network + LocalIPs []net.IP + Dht *DHTClient + Crypter Crypto + Shutdown bool + ForwardMode bool + ReadyToStop bool + MessageHandlers map[uint16]MessageHandler + PacketHandlers map[PacketType]PacketHandlerCallback + PeersLock sync.Mutex + Hash string + Interface TAP + Peers *PeerList + HolePunching sync.Mutex + ProxyManager *ProxyManager + outboundIP net.IP + UsePMTU bool + } + type args struct { + packet *protocol.DHTPacket + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &PeerToPeer{ + Config: tt.fields.Config, + UDPSocket: tt.fields.UDPSocket, + LocalIPs: tt.fields.LocalIPs, + Dht: tt.fields.Dht, + Crypter: tt.fields.Crypter, + Shutdown: tt.fields.Shutdown, + ForwardMode: tt.fields.ForwardMode, + ReadyToStop: tt.fields.ReadyToStop, + MessageHandlers: tt.fields.MessageHandlers, + PacketHandlers: tt.fields.PacketHandlers, + PeersLock: tt.fields.PeersLock, + Hash: tt.fields.Hash, + Interface: tt.fields.Interface, + Peers: tt.fields.Peers, + HolePunching: tt.fields.HolePunching, + ProxyManager: tt.fields.ProxyManager, + outboundIP: tt.fields.outboundIP, + UsePMTU: tt.fields.UsePMTU, + } + if err := p.packetUnknown(tt.args.packet); (err != nil) != tt.wantErr { + t.Errorf("PeerToPeer.packetUnknown() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestPeerToPeer_packetUnsupported(t *testing.T) { + type fields struct { + Config Configuration + UDPSocket *Network + LocalIPs []net.IP + Dht *DHTClient + Crypter Crypto + Shutdown bool + ForwardMode bool + ReadyToStop bool + MessageHandlers map[uint16]MessageHandler + PacketHandlers map[PacketType]PacketHandlerCallback + PeersLock sync.Mutex + Hash string + Interface TAP + Peers *PeerList + HolePunching sync.Mutex + ProxyManager *ProxyManager + outboundIP net.IP + UsePMTU bool + } + type args struct { + packet *protocol.DHTPacket + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &PeerToPeer{ + Config: tt.fields.Config, + UDPSocket: tt.fields.UDPSocket, + LocalIPs: tt.fields.LocalIPs, + Dht: tt.fields.Dht, + Crypter: tt.fields.Crypter, + Shutdown: tt.fields.Shutdown, + ForwardMode: tt.fields.ForwardMode, + ReadyToStop: tt.fields.ReadyToStop, + MessageHandlers: tt.fields.MessageHandlers, + PacketHandlers: tt.fields.PacketHandlers, + PeersLock: tt.fields.PeersLock, + Hash: tt.fields.Hash, + Interface: tt.fields.Interface, + Peers: tt.fields.Peers, + HolePunching: tt.fields.HolePunching, + ProxyManager: tt.fields.ProxyManager, + outboundIP: tt.fields.outboundIP, + UsePMTU: tt.fields.UsePMTU, + } + if err := p.packetUnsupported(tt.args.packet); (err != nil) != tt.wantErr { + t.Errorf("PeerToPeer.packetUnsupported() error = %v, wantErr %v", err, tt.wantErr) + } + }) } } diff --git a/lib/peerlist.go b/lib/peerlist.go index 45489709..0313bb03 100644 --- a/lib/peerlist.go +++ b/lib/peerlist.go @@ -30,7 +30,16 @@ func (l *PeerList) Init() { l.tableMacID = make(map[string]string) } -func (l *PeerList) operate(action ListOperation, id string, peer *NetworkPeer) { +func (l *PeerList) operate(action ListOperation, id string, peer *NetworkPeer) error { + if l.peers == nil { + return fmt.Errorf("peers is nil - not initialized") + } + if l.tableIPID == nil { + return fmt.Errorf("IP-ID table is nil - not initialized") + } + if l.tableMacID == nil { + return fmt.Errorf("Mac-ID table is nil - not initialized") + } l.lock.Lock() defer l.lock.Unlock() if action == OperateUpdate { @@ -44,15 +53,17 @@ func (l *PeerList) operate(action ListOperation, id string, peer *NetworkPeer) { mac = peer.PeerHW.String() } l.updateTables(id, ip, mac) + return nil } else if action == OperateDelete { peer, exists := l.peers[id] if !exists { - return + return fmt.Errorf("can't delete peer: entry doesn't exists") } l.deleteTables(peer.PeerLocalIP.String(), peer.PeerHW.String()) delete(l.peers, id) - return + return nil } + return nil } func (l *PeerList) updateTables(id, ip, mac string) { From 0d82a18776c9d3f06f35af71267f744c500b4c23 Mon Sep 17 00:00:00 2001 From: Mikhail Savochkin Date: Mon, 5 Nov 2018 16:42:55 +0600 Subject: [PATCH 3/5] Forced restored envs to be alive --- restore.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/restore.go b/restore.go index 75d3097b..4658e545 100644 --- a/restore.go +++ b/restore.go @@ -250,6 +250,11 @@ func (r *Restore) decodeInstances(data []byte) error { item.Keyfile = string(blocksOfArguments[5]) item.Key = string(blocksOfArguments[6]) item.TTL = string(blocksOfArguments[7]) + + // Force this env to be nor marked as failed + item.Enabled = true + ls, _ := time.Now().MarshalText() + item.LastSuccess = string(ls) //item.Fwd = false // if string(blocksOfArguments[8]) == "1" { // item.Fwd = true From ff1d82d665e62b7fa616cc924733c02ae4770bd8 Mon Sep 17 00:00:00 2001 From: Mikhail Savochkin Date: Mon, 5 Nov 2018 17:58:10 +0600 Subject: [PATCH 4/5] Covering DHT callbacks --- lib/dht_callbacks.go | 30 ++++++++++-- lib/dht_callbacks_test.go | 96 ++++++++++++++++++++++++++++++++------- 2 files changed, 106 insertions(+), 20 deletions(-) diff --git a/lib/dht_callbacks.go b/lib/dht_callbacks.go index 3f3a780b..1c76fd02 100644 --- a/lib/dht_callbacks.go +++ b/lib/dht_callbacks.go @@ -112,6 +112,9 @@ func (p *PeerToPeer) packetFind(packet *protocol.DHTPacket) error { if p.Peers == nil { return fmt.Errorf("nil peer list") } + if p.ProxyManager == nil { + return fmt.Errorf("nil proxy manager") + } Log(Debug, "Received `find`: %+v", packet) peer := p.Peers.GetPeer(packet.Data) @@ -168,11 +171,14 @@ func (p *PeerToPeer) packetFind(packet *protocol.DHTPacket) error { Log(Debug, "Adding proxy: %s", addr.String()) } } - peer.SetState(PeerStateInit, p) - peer.LastFind = time.Now() - p.Peers.Update(peer.ID, peer) - p.Peers.RunPeer(peer.ID, p) + if packet.GetExtra() != "skip" { + peer.SetState(PeerStateInit, p) + peer.LastFind = time.Now() + p.Peers.Update(peer.ID, peer) + p.Peers.RunPeer(peer.ID, p) + } } else { + // This is an existing peer peer.LastFind = time.Now() ips := []*net.UDPAddr{} @@ -228,6 +234,13 @@ func (p *PeerToPeer) packetForward(packet *protocol.DHTPacket) error { } func (p *PeerToPeer) packetNode(packet *protocol.DHTPacket) error { + if packet == nil { + return fmt.Errorf("nil packet") + } + + if p.Peers == nil { + return fmt.Errorf("nil peer list") + } if len(packet.Arguments) == 0 { return fmt.Errorf("Empty IP's list") @@ -266,6 +279,15 @@ func (p *PeerToPeer) packetPing(packet *protocol.DHTPacket) error { } func (p *PeerToPeer) packetProxy(packet *protocol.DHTPacket) error { + if packet == nil { + return fmt.Errorf("nil packet") + } + if p.UDPSocket == nil { + return fmt.Errorf("nil socket") + } + if p.ProxyManager == nil { + return fmt.Errorf("nil packet manager") + } Log(Debug, "Received list of proxies") for _, proxy := range packet.Proxies { proxyAddr, err := net.ResolveUDPAddr("udp4", proxy) diff --git a/lib/dht_callbacks_test.go b/lib/dht_callbacks_test.go index 51a1fd1a..568cc51d 100644 --- a/lib/dht_callbacks_test.go +++ b/lib/dht_callbacks_test.go @@ -351,14 +351,37 @@ func TestPeerToPeer_packetFind(t *testing.T) { pl1 := new(PeerList) pl1.Init() - pl1.Update("testid", new(NetworkPeer)) - pl1.Update("testid2", &NetworkPeer{ + pl1.peers["testid"] = &NetworkPeer{ + Proxies: []*net.UDPAddr{knownIP, knownIP}, + } + pl1.peers["testid2"] = &NetworkPeer{ KnownIPs: []*net.UDPAddr{knownIP}, - }) + Proxies: []*net.UDPAddr{knownIP}, + } lip0 := []net.IP{} lip0 = append(lip0, net.ParseIP("192.168.1.2")) + proxy1 := []string{"b:p"} + proxy2 := []string{"192.168.0.1:1234", "192.168.0.1:1234"} + proxy3 := []string{"192.168.1.2:3456"} + + proxyServer := new(proxyServer) + proxyServer.Endpoint = knownIP + proxyServer.Addr = knownIP + + pm1 := new(ProxyManager) + pm2 := new(ProxyManager) + pm2.init() + pm2.proxies[knownIP.String()] = proxyServer + + f1 := fields{} + f2 := fields{Dht: tdht} + f3 := fields{Dht: tdht, Peers: pl0} + f4 := fields{Dht: tdht, Peers: pl0, ProxyManager: pm1} + f5 := fields{Dht: tdht, Peers: pl1, LocalIPs: lip0, ProxyManager: pm1} + f6 := fields{Dht: tdht, Peers: pl1, LocalIPs: lip0, ProxyManager: pm2} + type args struct { packet *protocol.DHTPacket } @@ -368,14 +391,26 @@ func TestPeerToPeer_packetFind(t *testing.T) { args args wantErr bool }{ - {"nil packet", fields{}, args{}, true}, - {"nil dht", fields{}, args{new(protocol.DHTPacket)}, true}, - {"empty peer list", fields{Dht: tdht}, args{new(protocol.DHTPacket)}, false}, - {"skip self", fields{Dht: tdht}, args{&protocol.DHTPacket{Data: tdht.ID, Arguments: []string{"arg1"}}}, false}, - {"nil peer list", fields{Dht: tdht}, args{&protocol.DHTPacket{Data: "ttt", Arguments: []string{"arg1"}}}, true}, - {"new peer>bad ip", fields{Dht: tdht, Peers: pl0}, args{&protocol.DHTPacket{Data: "ttt", Arguments: []string{"arg1"}}}, false}, - {"new peer>known ip", fields{Dht: tdht, Peers: pl1}, args{&protocol.DHTPacket{Data: "ttt", Arguments: []string{knownIP.String(), knownIP.String()}}}, false}, - {"new peer>local ip", fields{Dht: tdht, Peers: pl1, LocalIPs: lip0}, args{&protocol.DHTPacket{Data: "ttt", Arguments: []string{knownIP.String()}}}, false}, + {"nil packet", f1, args{}, true}, + {"nil dht", f1, args{new(protocol.DHTPacket)}, true}, + {"empty peer list", f2, args{new(protocol.DHTPacket)}, false}, + {"skip self", f2, args{&protocol.DHTPacket{Data: tdht.ID, Arguments: []string{"arg1"}, Extra: "skip"}}, false}, + {"nil peer list", f2, args{&protocol.DHTPacket{Data: "ttt", Arguments: []string{"arg1"}, Extra: "skip"}}, true}, + {"nil proxy manager", f3, args{&protocol.DHTPacket{Data: "ttt", Arguments: []string{"arg1"}, Extra: "skip"}}, true}, + {"new peer>bad ip", f4, args{&protocol.DHTPacket{Data: "ttt", Arguments: []string{"arg1"}, Extra: "skip"}}, false}, + {"new peer>known ip", f4, args{&protocol.DHTPacket{Data: "ttt", Arguments: []string{knownIP.String(), knownIP.String()}, Extra: "skip"}}, false}, + {"new peer>local ip", f5, args{&protocol.DHTPacket{Data: "ttt", Arguments: []string{knownIP.String()}, Extra: "skip"}}, false}, + {"new peer>bad proxy", f5, args{&protocol.DHTPacket{Data: "ttt", Arguments: []string{knownIP.String()}, Extra: "skip", Proxies: proxy1}}, false}, + {"new peer>existing proxy", f5, args{&protocol.DHTPacket{Data: "ttt", Arguments: []string{knownIP.String()}, Extra: "skip", Proxies: proxy2}}, false}, + {"new peer>own proxy", f6, args{&protocol.DHTPacket{Data: "ttt", Arguments: []string{knownIP.String()}, Extra: "skip", Proxies: proxy3}}, false}, + {"existing peer>empty ip", f5, args{&protocol.DHTPacket{Data: "testid2", Arguments: []string{""}, Extra: "skip"}}, false}, + {"existing peer>bad ip", f5, args{&protocol.DHTPacket{Data: "testid2", Arguments: []string{"arg1"}, Extra: "skip"}}, false}, + {"existing peer>known ip", f5, args{&protocol.DHTPacket{Data: "testid2", Arguments: []string{knownIP.String(), knownIP.String()}, Extra: "skip"}}, false}, + {"existing peer>local ip", f5, args{&protocol.DHTPacket{Data: "testid2", Arguments: []string{knownIP.String()}, Extra: "skip"}}, false}, + {"existing peer>empty proxy", f5, args{&protocol.DHTPacket{Data: "testid2", Arguments: []string{knownIP.String()}, Extra: "skip", Proxies: []string{""}}}, false}, + {"existing peer>bad proxy", f5, args{&protocol.DHTPacket{Data: "testid2", Arguments: []string{knownIP.String()}, Extra: "skip", Proxies: proxy1}}, false}, + {"existing peer>existing proxy", f5, args{&protocol.DHTPacket{Data: "testid2", Arguments: []string{knownIP.String()}, Extra: "skip", Proxies: proxy2}}, false}, + {"existing peer>own proxy", f6, args{&protocol.DHTPacket{Data: "testid2", Arguments: []string{knownIP.String()}, Extra: "skip", Proxies: proxy3}}, false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -436,7 +471,7 @@ func TestPeerToPeer_packetForward(t *testing.T) { args args wantErr bool }{ - // TODO: Add test cases. + {"empty test", fields{}, args{}, false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -491,13 +526,42 @@ func TestPeerToPeer_packetNode(t *testing.T) { type args struct { packet *protocol.DHTPacket } + + p1 := &protocol.DHTPacket{} + p2 := &protocol.DHTPacket{ + Data: "unknown-id", + Arguments: []string{""}, + } + p3 := &protocol.DHTPacket{ + Data: "test-id-1", + Arguments: []string{""}, + } + p4 := &protocol.DHTPacket{ + Data: "test-id-1", + Arguments: []string{"b/p"}, + } + p5 := &protocol.DHTPacket{ + Data: "test-id-1", + Arguments: []string{"192.168.0.1:1234"}, + } + + pl1 := new(PeerList) + pl1.Init() + pl1.peers["test-id-1"] = new(NetworkPeer) + tests := []struct { name string fields fields args args wantErr bool }{ - // TODO: Add test cases. + {"nil packet", fields{}, args{}, true}, + {"nil peer list", fields{}, args{p1}, true}, + {"empty arguments", fields{Peers: new(PeerList)}, args{p1}, true}, + {"unknown peer", fields{Peers: pl1}, args{p2}, true}, + {"empty addr", fields{Peers: pl1}, args{p3}, false}, + {"bad addr", fields{Peers: pl1}, args{p4}, false}, + {"passing test", fields{Peers: pl1}, args{p5}, false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -558,7 +622,7 @@ func TestPeerToPeer_packetNotify(t *testing.T) { args args wantErr bool }{ - // TODO: Add test cases. + {"empty test", fields{}, args{}, false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -619,7 +683,7 @@ func TestPeerToPeer_packetPing(t *testing.T) { args args wantErr bool }{ - // TODO: Add test cases. + {"empty test", fields{}, args{}, false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -680,7 +744,7 @@ func TestPeerToPeer_packetProxy(t *testing.T) { args args wantErr bool }{ - // TODO: Add test cases. + {"nil packet", fields{}, args{}, true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { From 2adbf129ab04d3dbbd0152c767fe32bab658a639 Mon Sep 17 00:00:00 2001 From: Mikhail Savochkin Date: Mon, 5 Nov 2018 22:40:03 +0600 Subject: [PATCH 5/5] Finished coverage of dht_callbacks --- lib/dht_callbacks.go | 45 +++++++++--- lib/dht_callbacks_test.go | 144 +++++++++++++++++++++++++++++++++++--- lib/p2p.go | 4 ++ 3 files changed, 174 insertions(+), 19 deletions(-) diff --git a/lib/dht_callbacks.go b/lib/dht_callbacks.go index 1c76fd02..b0685ffb 100644 --- a/lib/dht_callbacks.go +++ b/lib/dht_callbacks.go @@ -286,7 +286,10 @@ func (p *PeerToPeer) packetProxy(packet *protocol.DHTPacket) error { return fmt.Errorf("nil socket") } if p.ProxyManager == nil { - return fmt.Errorf("nil packet manager") + return fmt.Errorf("nil proxy manager") + } + if p.Dht == nil { + return fmt.Errorf("nil dht") } Log(Debug, "Received list of proxies") for _, proxy := range packet.Proxies { @@ -308,6 +311,9 @@ func (p *PeerToPeer) packetProxy(packet *protocol.DHTPacket) error { // packetRequestProxy received when we was requesting proxy to connect to some peer func (p *PeerToPeer) packetRequestProxy(packet *protocol.DHTPacket) error { + if p.Peers == nil { + return fmt.Errorf("nil peer list") + } list := []*net.UDPAddr{} for _, proxy := range packet.Proxies { addr, err := net.ResolveUDPAddr("udp4", proxy) @@ -318,16 +324,6 @@ func (p *PeerToPeer) packetRequestProxy(packet *protocol.DHTPacket) error { list = append(list, addr) } - // peers := p.Peers.Get() - // for _, proxy := range list { - // for _, existingPeer := range peers { - // if existingPeer.Endpoint.String() == proxy.String() && existingPeer.ID != packet.Data { - // existingPeer.SetState(PeerStateDisconnect, p) - // Log(Info, "Peer %s was associated with address %s. Disconnecting", existingPeer.ID, proxy.String()) - // } - // } - // } - peer := p.Peers.GetPeer(packet.Data) if peer != nil { peer.Proxies = list @@ -341,6 +337,9 @@ func (p *PeerToPeer) packetReportProxy(packet *protocol.DHTPacket) error { } func (p *PeerToPeer) packetRegisterProxy(packet *protocol.DHTPacket) error { + if packet == nil { + return fmt.Errorf("nil packet") + } if packet.Data == "OK" { Log(Info, "Proxy registration confirmed") } @@ -352,6 +351,12 @@ func (p *PeerToPeer) packetReportLoad(packet *protocol.DHTPacket) error { } func (p *PeerToPeer) packetState(packet *protocol.DHTPacket) error { + if packet == nil { + return fmt.Errorf("nil packet") + } + if p.Peers == nil { + return fmt.Errorf("nil peer list") + } if len(packet.Data) != 36 { return fmt.Errorf("Receied state packet for unknown/broken ID") } @@ -380,6 +385,18 @@ func (p *PeerToPeer) packetStop(packet *protocol.DHTPacket) error { } func (p *PeerToPeer) packetUnknown(packet *protocol.DHTPacket) error { + if packet == nil { + return fmt.Errorf("nil packet") + } + if p.Dht == nil { + return fmt.Errorf("nil dht") + } + if p.ProxyManager == nil { + return fmt.Errorf("nil proxy manager") + } + if p.Interface == nil { + return fmt.Errorf("nil interface") + } Log(Debug, "Received unknown packet") p.FindNetworkAddresses() if len(packet.Data) > 0 && packet.Data == "DHCP" { @@ -392,6 +409,12 @@ func (p *PeerToPeer) packetUnknown(packet *protocol.DHTPacket) error { } func (p *PeerToPeer) packetUnsupported(packet *protocol.DHTPacket) error { + if packet == nil { + return fmt.Errorf("nil packet") + } + if p.Dht == nil { + return fmt.Errorf("nil dht") + } Log(Error, "Bootstap node doesn't support our version. Shutting down") return p.Dht.Close() } diff --git a/lib/dht_callbacks_test.go b/lib/dht_callbacks_test.go index 568cc51d..1306a261 100644 --- a/lib/dht_callbacks_test.go +++ b/lib/dht_callbacks_test.go @@ -738,6 +738,36 @@ func TestPeerToPeer_packetProxy(t *testing.T) { type args struct { packet *protocol.DHTPacket } + + n1 := new(Network) + n1.Init("127.0.0.1", 1234) + + pm1 := new(ProxyManager) + pm1.init() + + f1 := fields{ + UDPSocket: n1, + ProxyManager: pm1, + } + + f2 := fields{ + UDPSocket: n1, + ProxyManager: pm1, + Dht: &DHTClient{}, + } + + p1 := &protocol.DHTPacket{ + Proxies: []string{"s:b"}, + } + + p2 := &protocol.DHTPacket{ + Proxies: []string{"127.0.0.1:1111", "127.0.0.1:1111"}, + } + + p3 := &protocol.DHTPacket{ + Proxies: []string{"127.0.0.1:1111"}, + } + tests := []struct { name string fields fields @@ -745,6 +775,12 @@ func TestPeerToPeer_packetProxy(t *testing.T) { wantErr bool }{ {"nil packet", fields{}, args{}, true}, + {"nil socket", fields{}, args{new(protocol.DHTPacket)}, true}, + {"nil proxy manager", fields{UDPSocket: new(Network)}, args{new(protocol.DHTPacket)}, true}, + {"nil dht", f1, args{new(protocol.DHTPacket)}, true}, + {"bad proxy addr", f2, args{p1}, false}, + {"two same proxies", f2, args{p2}, false}, + {"new proxy", f2, args{p3}, false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -799,13 +835,38 @@ func TestPeerToPeer_packetRequestProxy(t *testing.T) { type args struct { packet *protocol.DHTPacket } + + p1 := &protocol.DHTPacket{ + Proxies: []string{"b:a"}, + } + + p2 := &protocol.DHTPacket{ + Proxies: []string{"127.0.0.1:1234"}, + } + + p3 := &protocol.DHTPacket{ + Proxies: []string{"127.0.0.1:1234"}, + Data: "test-peer", + } + + pl1 := new(PeerList) + pl1.Init() + pl1.peers["test-peer"] = &NetworkPeer{} + + f1 := fields{ + Peers: pl1, + } + tests := []struct { name string fields fields args args wantErr bool }{ - // TODO: Add test cases. + {"nil peer list", fields{}, args{}, true}, + {"bad proxy addr", fields{Peers: new(PeerList)}, args{p1}, false}, + {"non existing peer", f1, args{p2}, false}, + {"existing peer", f1, args{p3}, false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -866,7 +927,7 @@ func TestPeerToPeer_packetReportProxy(t *testing.T) { args args wantErr bool }{ - // TODO: Add test cases. + {"empty test", fields{}, args{}, false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -927,7 +988,9 @@ func TestPeerToPeer_packetRegisterProxy(t *testing.T) { args args wantErr bool }{ - // TODO: Add test cases. + {"nil packet", fields{}, args{}, true}, + {"ok", fields{}, args{&protocol.DHTPacket{Data: "OK"}}, false}, + {"not ok", fields{}, args{&protocol.DHTPacket{Data: "!ok"}}, false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -988,7 +1051,7 @@ func TestPeerToPeer_packetReportLoad(t *testing.T) { args args wantErr bool }{ - // TODO: Add test cases. + {"empty test", fields{}, args{}, false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -1043,13 +1106,54 @@ func TestPeerToPeer_packetState(t *testing.T) { type args struct { packet *protocol.DHTPacket } + + pl1 := new(PeerList) + pl1.Init() + + pl2 := new(PeerList) + pl2.Init() + pl2.peers["123e4567-e89b-12d3-a456-426655440000"] = &NetworkPeer{} + + f1 := fields{ + Peers: pl1, + } + + f2 := fields{ + Peers: pl2, + } + + p1 := &protocol.DHTPacket{ + Data: "short", + } + + p2 := &protocol.DHTPacket{ + Data: "123e4567-e89b-12d3-a456-426655440000", + Extra: "", + } + + p3 := &protocol.DHTPacket{ + Data: "123e4567-e89b-12d3-a456-426655440000", + Extra: "error", + } + + p4 := &protocol.DHTPacket{ + Data: "123e4567-e89b-12d3-a456-426655440000", + Extra: "4", + } + tests := []struct { name string fields fields args args wantErr bool }{ - // TODO: Add test cases. + {"nil packet", fields{}, args{}, true}, + {"nil peer list", fields{}, args{new(protocol.DHTPacket)}, true}, + {"short data", f1, args{p1}, true}, + {"empty extra", f1, args{p2}, true}, + {"broken extra", f1, args{p3}, true}, + {"peer not exists", f1, args{p4}, false}, + {"peer exists", f2, args{p4}, false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -1110,7 +1214,7 @@ func TestPeerToPeer_packetStop(t *testing.T) { args args wantErr bool }{ - // TODO: Add test cases. + {"empty test", fields{}, args{}, false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -1165,13 +1269,35 @@ func TestPeerToPeer_packetUnknown(t *testing.T) { type args struct { packet *protocol.DHTPacket } + + p1 := &protocol.DHTPacket{ + Data: "DHCP", + } + + p2 := &protocol.DHTPacket{ + Data: "Anything, but DHCP", + } + + pm1 := new(ProxyManager) + pm1.init() + + dht := new(DHTClient) + dht.Init("") + + inf, _ := newTAP("", "192.168.0.1", "00:11:22:33:44:55", "", 1500, false) + tests := []struct { name string fields fields args args wantErr bool }{ - // TODO: Add test cases. + {"nil packet", fields{}, args{}, true}, + {"nil dht", fields{}, args{p1}, true}, + {"nil proxy manager", fields{Dht: dht}, args{p1}, true}, + {"nil interface", fields{Dht: dht, ProxyManager: pm1}, args{p1}, true}, + {"normal data", fields{Dht: dht, ProxyManager: pm1, Interface: inf}, args{p1}, false}, + {"refuse data", fields{Dht: dht, ProxyManager: pm1, Interface: inf}, args{p2}, true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -1232,7 +1358,9 @@ func TestPeerToPeer_packetUnsupported(t *testing.T) { args args wantErr bool }{ - // TODO: Add test cases. + {"nil packet", fields{}, args{}, true}, + {"nil dht", fields{}, args{new(protocol.DHTPacket)}, true}, + {"passing", fields{Dht: new(DHTClient)}, args{new(protocol.DHTPacket)}, false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/lib/p2p.go b/lib/p2p.go index de25bd7e..27e1f526 100644 --- a/lib/p2p.go +++ b/lib/p2p.go @@ -411,6 +411,10 @@ func (p *PeerToPeer) RequestIP(mac, device string) (net.IP, net.IPMask, error) { // ReportIP will send IP specified at service start to DHCP-like service func (p *PeerToPeer) ReportIP(ipAddress, mac, device string) (net.IP, net.IPMask, error) { + if p.Dht == nil { + return nil, nil, fmt.Errorf("nil dht") + } + Log(Debug, "Reporting IP to bootstranp node: %s", ipAddress) ip, ipnet, err := net.ParseCIDR(ipAddress) if err != nil {