func TestConnect(t *testing.T) { conf, err := cjdngo.ReadConf("/etc/cjdroute.conf") if err != nil || len(conf.Admin.Password) == 0 { // This is not related to the test. t.Log(err) t.Fatal("Could not read the config file. This is not related to cjdngo/admin.") } cjd, err := Connect("127.0.0.1", "11234", conf.Admin.Password) if err != nil { t.Fatal(err) } pingOK, authOK := cjd.Ping() if !authOK { t.Fatal("Server authentication failed.") } else if !pingOK { t.Fatal("Server did not respond to ping.") } cjdns = cjd }
func main() { // Define the flags, and parse them. flag.Parse() cmd = strings.ToLower(flag.Arg(0)) argument = flag.Arg(1) var jsonArg []byte if UseJSON { var jsonTmp string for i := 2; i < flag.NArg(); i++ { // Put all of the remaining arguments into the string json. jsonTmp += flag.Arg(i) } jsonArg = []byte(jsonTmp) } Conf, err := cjdngo.ReadConf(File) if err != nil { log.Fatal(err) } var iface *cjdngo.InterfaceBlock if UseETH { if len(Conf.Interfaces.ETHInterface) > 0 { iface = &Conf.Interfaces.ETHInterface[0] } else { log.Fatal("Interface not present in config.") } } else { if len(Conf.Interfaces.UDPInterface) > 0 { iface = &Conf.Interfaces.UDPInterface[0] } else { log.Fatal("Interface not present in config.") } } // log.SetOutput(ioutil.Discard) // This will be used to determine whether the configuration should // be rewritten afterward. willWrite := false // Perform an appropriate action, based on the subcommand. switch cmd { case authCmd: willWrite = true var details string if UseETH { details = Conf.EthConn } else { details = Conf.TunConn } index, err := strconv.Atoi(argument) if err != nil { // If we can't parse the argument for whatever reason, // assume that it's an append. index = -1 if UseJSON { // If we couldn't parse the argument, then it might've // been JSON, so treat it as such. Authorize(Conf, details, index, append([]byte(argument), jsonArg...)) break } } Authorize(Conf, details, index, jsonArg) case connCmd: willWrite = true if !UseJSON { Connect(Conf, iface, argument, nil) } else { Connect(Conf, iface, "", append([]byte(argument), jsonArg...)) } case lsAuthCmd: ListAuthorization(Conf, argument) case lsConnCmd: ListConnection(iface, argument) case rmCmd: willWrite = true Remove(Conf, iface, argument) default: usage() } if willWrite { err = cjdngo.WriteConf(File, *Conf) if err != nil { log.Fatal(err) } } }