// Unit tests for our FileCopy function. // @param *testing.T t - The wrapper for the test func TestFileCopy(t *testing.T) { fmt.Println("\n----------------TestFileCopy----------------") result := lynxutil.FileCopy("test.txt", "test2.txt") if result != nil { t.Error("Test failed, expected no errors. Got ", result) } else { fmt.Println("Successfully Copied File") successful++ } // Tests that overwriting a file is fine result = lynxutil.FileCopy("test.txt", "test2.txt") if result != nil { t.Error("Test failed, expected no errors. Got ", result) } else { fmt.Println("Successfully Overwrote File") successful++ } result = lynxutil.FileCopy("fake.txt", "test2.txt") if result == nil { t.Error("Test failed, expected failure due to non-existent file fake.txt. Got ", result) } else { fmt.Println("Successfully Produced Non-Existent File Error") successful++ } result = lynxutil.FileCopy("nopermission.txt", "test2.txt") if result == nil { t.Error("Test failed, expected failure due to permissions on nopermission.txt. Got ", result) } else { fmt.Println("Successfully Produced File Permission Error") successful++ } }
// CreateSwarm - Creates a new swarm.info upon clicking of create button in gui // @param string name - the name of the lynk func CreateSwarm(name string) { p1 := lynxutil.Peer{IP: "", Port: "8080"} currentuser, err := user.Current() trackerDir := currentuser.HomeDir + "/Lynx/" + name + "/" + name + "_Tracker" os.Mkdir(trackerDir, 0755) _, err = os.Create(trackerDir + "/swarm.info") if err != nil { fmt.Println(err) } p1.IP = lynxutil.GetIP() addToSwarminfo(p1, trackerDir+"/swarm.info") lynxutil.FileCopy(currentuser.HomeDir+"/Lynx/"+name+"/meta.info", trackerDir+"/meta.info") }
// Function which creates the directory for a newly joined lynk. // @params name string - the name of the new lynk // @params oldMetaPath string - the name of the metaPath we are using to create our new metaPath func createJoin(name, oldMetaPath string) error { tDir, err := os.Stat(lynxutil.HomePath + name) // Checks to see if the directory exists so we don't overwrite if err == nil && tDir.IsDir() { fmt.Println("ERROR!" + tDir.Name() + " Already Exists") return errors.New("Directory " + name + " Already Exists") } newLynkDir := lynxutil.HomePath + name os.Mkdir(newLynkDir, 0755) err = lynxutil.FileCopy(oldMetaPath, newLynkDir+"/meta.info") if err != nil { fmt.Println(err) return err } return nil // Everything was fine if we reached this point }