github.com/xfers/quorum@v21.1.0+incompatible/permission/core/permissions_test.go (about) 1 package core 2 3 import ( 4 "encoding/json" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "testing" 9 10 "github.com/ethereum/go-ethereum/p2p/enode" 11 "github.com/ethereum/go-ethereum/params" 12 ) 13 14 const ( 15 node1 = "enode://ac6b1096ca56b9f6d004b779ae3728bf83f8e22453404cc3cef16a3d9b96608bc67c4b30db88e0a5a6c6390213f7acbe1153ff6d23ce57380104288ae19373ef@127.0.0.1:21000?discport=0&raftport=50401" 16 node2 = "enode://0ba6b9f606a43a95edc6247cdb1c1e105145817be7bcafd6b2c0ba15d58145f0dc1a194f70ba73cd6f4cdd6864edc7687f311254c7555cc32e4d45aeb1b80416@127.0.0.1:21001?discport=0&raftport=50402" 17 node3 = "enode://579f786d4e2830bbcc02815a27e8a9bacccc9605df4dc6f20bcc1a6eb391e7225fff7cb83e5b4ecd1f3a94d8b733803f2f66b7e871961e7b029e22c155c3a778@127.0.0.1:21002?discport=0&raftport=50403" 18 ) 19 20 func TestIsNodePermissioned(t *testing.T) { 21 type args struct { 22 nodename string 23 currentNode string 24 datadir string 25 direction string 26 } 27 d, _ := ioutil.TempDir("", "qdata") 28 defer os.RemoveAll(d) 29 writeNodeToFile(d, params.PERMISSIONED_CONFIG, node1) 30 writeNodeToFile(d, params.PERMISSIONED_CONFIG, node3) 31 writeNodeToFile(d, params.BLACKLIST_CONFIG, node3) 32 n1, _ := enode.ParseV4(node1) 33 n2, _ := enode.ParseV4(node2) 34 n3, _ := enode.ParseV4(node3) 35 36 tests := []struct { 37 name string 38 args args 39 want bool 40 }{ 41 { 42 name: "node present", 43 args: args{n1.ID().String(), n2.EnodeID(), d, "INWARD"}, 44 want: true, 45 }, 46 { 47 name: "node not present", 48 args: args{n2.ID().String(), n1.EnodeID(), d, "OUTWARD"}, 49 want: false, 50 }, 51 { 52 name: "blacklisted node", 53 args: args{n3.ID().String(), n1.EnodeID(), d, "INWARD"}, 54 want: false, 55 }, 56 } 57 58 for _, tt := range tests { 59 t.Run(tt.name, func(t *testing.T) { 60 if got := IsNodePermissioned(tt.args.nodename, tt.args.currentNode, tt.args.datadir, tt.args.direction); got != tt.want { 61 t.Errorf("IsNodePermissioned() = %v, want %v", got, tt.want) 62 } 63 }) 64 } 65 66 } 67 68 func Test_isNodeBlackListed(t *testing.T) { 69 type args struct { 70 nodeName string 71 dataDir string 72 } 73 74 d, _ := ioutil.TempDir("", "qdata") 75 defer os.RemoveAll(d) 76 writeNodeToFile(d, params.BLACKLIST_CONFIG, node1) 77 n1, _ := enode.ParseV4(node1) 78 n2, _ := enode.ParseV4(node2) 79 80 tests := []struct { 81 name string 82 args args 83 want bool 84 }{ 85 { 86 name: "blacklisted node", 87 args: args{n1.ID().String(), d}, 88 want: true, 89 }, 90 { 91 name: "blacklisted node", 92 args: args{n2.ID().String(), d}, 93 want: false, 94 }, 95 } 96 97 for _, tt := range tests { 98 t.Run(tt.name, func(t *testing.T) { 99 if got := isNodeBlackListed(tt.args.nodeName, tt.args.dataDir); got != tt.want { 100 t.Errorf("isNodeBlackListed() = %v, want %v", got, tt.want) 101 } 102 }) 103 } 104 } 105 106 func writeNodeToFile(dataDir, fileName, url string) { 107 fileExists := true 108 path := filepath.Join(dataDir, fileName) 109 110 // Check if the file is existing. If the file is not existing create the file 111 if _, err := os.Stat(path); err != nil { 112 if _, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0644); err != nil { 113 return 114 } 115 fileExists = false 116 } 117 118 var nodeList []string 119 var blob []byte 120 if fileExists { 121 blob, err := ioutil.ReadFile(path) 122 if err == nil { 123 if err := json.Unmarshal(blob, &nodeList); err != nil { 124 return 125 } 126 } 127 } 128 nodeList = append(nodeList, url) 129 blob, _ = json.Marshal(nodeList) 130 _ = ioutil.WriteFile(path, blob, 0644) 131 132 }