github.com/shyftnetwork/go-empyrean@v1.8.3-0.20191127201940-fbfca9338f04/swarm/network/kademlia/address_test.go (about) 1 // Copyright 2016 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package kademlia 18 19 import ( 20 "math/rand" 21 "reflect" 22 "testing" 23 24 "github.com/ShyftNetwork/go-empyrean/common" 25 ) 26 27 func (Address) Generate(rand *rand.Rand, size int) reflect.Value { 28 var id Address 29 for i := 0; i < len(id); i++ { 30 id[i] = byte(uint8(rand.Intn(255))) 31 } 32 return reflect.ValueOf(id) 33 } 34 35 func TestCommonBitsAddrF(t *testing.T) { 36 a := Address(common.HexToHash("0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")) 37 b := Address(common.HexToHash("0x8123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")) 38 c := Address(common.HexToHash("0x4123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")) 39 d := Address(common.HexToHash("0x0023456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")) 40 e := Address(common.HexToHash("0x01A3456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")) 41 ab := CommonBitsAddrF(a, b, func() byte { return byte(0x00) }, 10) 42 expab := Address(common.HexToHash("0x8000000000000000000000000000000000000000000000000000000000000000")) 43 44 if ab != expab { 45 t.Fatalf("%v != %v", ab, expab) 46 } 47 ac := CommonBitsAddrF(a, c, func() byte { return byte(0x00) }, 10) 48 expac := Address(common.HexToHash("0x4000000000000000000000000000000000000000000000000000000000000000")) 49 50 if ac != expac { 51 t.Fatalf("%v != %v", ac, expac) 52 } 53 ad := CommonBitsAddrF(a, d, func() byte { return byte(0x00) }, 10) 54 expad := Address(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000")) 55 56 if ad != expad { 57 t.Fatalf("%v != %v", ad, expad) 58 } 59 ae := CommonBitsAddrF(a, e, func() byte { return byte(0x00) }, 10) 60 expae := Address(common.HexToHash("0x0180000000000000000000000000000000000000000000000000000000000000")) 61 62 if ae != expae { 63 t.Fatalf("%v != %v", ae, expae) 64 } 65 acf := CommonBitsAddrF(a, c, func() byte { return byte(0xff) }, 10) 66 expacf := Address(common.HexToHash("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")) 67 68 if acf != expacf { 69 t.Fatalf("%v != %v", acf, expacf) 70 } 71 aeo := CommonBitsAddrF(a, e, func() byte { return byte(0x00) }, 2) 72 expaeo := Address(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000")) 73 74 if aeo != expaeo { 75 t.Fatalf("%v != %v", aeo, expaeo) 76 } 77 aep := CommonBitsAddrF(a, e, func() byte { return byte(0xff) }, 2) 78 expaep := Address(common.HexToHash("0x3fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")) 79 80 if aep != expaep { 81 t.Fatalf("%v != %v", aep, expaep) 82 } 83 84 } 85 86 func TestRandomAddressAt(t *testing.T) { 87 var a Address 88 for i := 0; i < 100; i++ { 89 a = RandomAddress() 90 prox := rand.Intn(255) 91 b := RandomAddressAt(a, prox) 92 if proximity(a, b) != prox { 93 t.Fatalf("incorrect address prox(%v, %v) == %v (expected %v)", a, b, proximity(a, b), prox) 94 } 95 } 96 }