github.com/Synthesix/Sia@v1.3.3-0.20180413141344-f863baeed3ca/cmd/siad/daemon_test.go (about) 1 package main 2 3 import ( 4 "testing" 5 ) 6 7 // TestUnitProcessNetAddr probes the 'processNetAddr' function. 8 func TestUnitProcessNetAddr(t *testing.T) { 9 testVals := struct { 10 inputs []string 11 expectedOutputs []string 12 }{ 13 inputs: []string{"9980", ":9980", "localhost:9980", "test.com:9980", "192.168.14.92:9980"}, 14 expectedOutputs: []string{":9980", ":9980", "localhost:9980", "test.com:9980", "192.168.14.92:9980"}, 15 } 16 for i, input := range testVals.inputs { 17 output := processNetAddr(input) 18 if output != testVals.expectedOutputs[i] { 19 t.Error("unexpected result", i) 20 } 21 } 22 } 23 24 // TestUnitProcessModules tests that processModules correctly processes modules 25 // passed to the -M / --modules flag. 26 func TestUnitProcessModules(t *testing.T) { 27 // Test valid modules. 28 testVals := []struct { 29 in string 30 out string 31 }{ 32 {"cghmrtwe", "cghmrtwe"}, 33 {"CGHMRTWE", "cghmrtwe"}, 34 {"c", "c"}, 35 {"g", "g"}, 36 {"h", "h"}, 37 {"m", "m"}, 38 {"r", "r"}, 39 {"t", "t"}, 40 {"w", "w"}, 41 {"e", "e"}, 42 {"C", "c"}, 43 {"G", "g"}, 44 {"H", "h"}, 45 {"M", "m"}, 46 {"R", "r"}, 47 {"T", "t"}, 48 {"W", "w"}, 49 {"E", "e"}, 50 } 51 for _, testVal := range testVals { 52 out, err := processModules(testVal.in) 53 if err != nil { 54 t.Error("processModules failed with error:", err) 55 } 56 if out != testVal.out { 57 t.Errorf("processModules returned incorrect modules: expected %s, got %s\n", testVal.out, out) 58 } 59 } 60 61 // Test invalid modules. 62 invalidModules := []string{"abdfijklnopqsuvxyz", "cghmrtwez", "cz", "z", "cc", "ccz", "ccm", "cmm", "ccmm"} 63 for _, invalidModule := range invalidModules { 64 _, err := processModules(invalidModule) 65 if err == nil { 66 t.Error("processModules didn't error on invalid module:", invalidModule) 67 } 68 } 69 } 70 71 // TestUnitProcessProfile tests that processProfileFlags correctly processes profiles 72 // passed to the --profile flag. 73 func TestUnitProcessProfile(t *testing.T) { 74 // Test valid profiles. 75 testVals := []struct { 76 in string 77 out string 78 }{ 79 {"cmt", "cmt"}, 80 {"CMT", "cmt"}, 81 {"c", "c"}, 82 {"m", "m"}, 83 {"t", "t"}, 84 {"C", "c"}, 85 {"M", "m"}, 86 {"T", "t"}, 87 } 88 for _, testVal := range testVals { 89 out, err := processProfileFlags(testVal.in) 90 if err != nil { 91 t.Error("processProfileFlags failed with error:", err) 92 } 93 if out != testVal.out { 94 t.Errorf("processProfileFlags returned incorrect modules: expected %s, got %s\n", testVal.out, out) 95 } 96 } 97 98 // Test invalid modules. 99 invalidProfiles := []string{"abdfijklnopqsuvxyz", "cghmrtwez", "cz", "z", "cc", "ccz", "ccm", "cmm", "ccmm", "g", "h", "cghmrtwe", "CGHMRTWE", "mts"} 100 for _, invalidProfiles := range invalidProfiles { 101 _, err := processProfileFlags(invalidProfiles) 102 if err == nil { 103 t.Error("processProfileFlags didn't error on invalid profile:", invalidProfiles) 104 } 105 } 106 } 107 108 // TestUnitProcessConfig probes the 'processConfig' function. 109 func TestUnitProcessConfig(t *testing.T) { 110 // Test valid configs. 111 testVals := struct { 112 inputs [][]string 113 expectedOutputs [][]string 114 }{ 115 inputs: [][]string{ 116 {"localhost:9980", "localhost:9981", "localhost:9982", "cghmrtwe"}, 117 {"localhost:9980", "localhost:9981", "localhost:9982", "CGHMRTWE"}, 118 }, 119 expectedOutputs: [][]string{ 120 {"localhost:9980", "localhost:9981", "localhost:9982", "cghmrtwe"}, 121 {"localhost:9980", "localhost:9981", "localhost:9982", "cghmrtwe"}, 122 }, 123 } 124 var config Config 125 for i := range testVals.inputs { 126 config.Siad.APIaddr = testVals.inputs[i][0] 127 config.Siad.RPCaddr = testVals.inputs[i][1] 128 config.Siad.HostAddr = testVals.inputs[i][2] 129 config, err := processConfig(config) 130 if err != nil { 131 t.Error("processConfig failed with error:", err) 132 } 133 if config.Siad.APIaddr != testVals.expectedOutputs[i][0] { 134 t.Error("processing failure at check", i, 0) 135 } 136 if config.Siad.RPCaddr != testVals.expectedOutputs[i][1] { 137 t.Error("processing failure at check", i, 1) 138 } 139 if config.Siad.HostAddr != testVals.expectedOutputs[i][2] { 140 t.Error("processing failure at check", i, 2) 141 } 142 } 143 144 // Test invalid configs. 145 invalidModule := "z" 146 config.Siad.Modules = invalidModule 147 _, err := processConfig(config) 148 if err == nil { 149 t.Error("processModules didn't error on invalid module:", invalidModule) 150 } 151 } 152 153 // TestVerifyAPISecurity checks that the verifyAPISecurity function is 154 // correctly banning the use of a non-loopback address without the 155 // --disable-security flag, and that the --disable-security flag cannot be used 156 // without an api password. 157 func TestVerifyAPISecurity(t *testing.T) { 158 // Check that the loopback address is accepted when security is enabled. 159 var securityOnLoopback Config 160 securityOnLoopback.Siad.APIaddr = "127.0.0.1:9980" 161 err := verifyAPISecurity(securityOnLoopback) 162 if err != nil { 163 t.Error("loopback + securityOn was rejected") 164 } 165 166 // Check that the blank address is rejected when security is enabled. 167 var securityOnBlank Config 168 securityOnBlank.Siad.APIaddr = ":9980" 169 err = verifyAPISecurity(securityOnBlank) 170 if err == nil { 171 t.Error("blank + securityOn was accepted") 172 } 173 174 // Check that a public hostname is rejected when security is enabled. 175 var securityOnPublic Config 176 securityOnPublic.Siad.APIaddr = "sia.tech:9980" 177 err = verifyAPISecurity(securityOnPublic) 178 if err == nil { 179 t.Error("public + securityOn was accepted") 180 } 181 182 // Check that a public hostname is rejected when security is disabled and 183 // there is no api password. 184 var securityOffPublic Config 185 securityOffPublic.Siad.APIaddr = "sia.tech:9980" 186 securityOffPublic.Siad.AllowAPIBind = true 187 err = verifyAPISecurity(securityOffPublic) 188 if err == nil { 189 t.Error("public + securityOff was accepted without authentication") 190 } 191 192 // Check that a public hostname is accepted when security is disabled and 193 // there is an api password. 194 var securityOffPublicAuthenticated Config 195 securityOffPublicAuthenticated.Siad.APIaddr = "sia.tech:9980" 196 securityOffPublicAuthenticated.Siad.AllowAPIBind = true 197 securityOffPublicAuthenticated.Siad.AuthenticateAPI = true 198 err = verifyAPISecurity(securityOffPublicAuthenticated) 199 if err != nil { 200 t.Error("public + securityOff with authentication was rejected:", err) 201 } 202 }