Example #1
0
func doInit(out io.Writer, repoRoot string, force bool, empty bool, nBitsForKeypair int) error {
	log.Println("initializing ipfs node at ", repoRoot)

	if err := checkWriteable(repoRoot); err != nil {
		return err
	}

	if fsrepo.IsInitialized(repoRoot) && !force {
		log.Println("repo already exists")
		return nil
	}

	conf, err := config.Init(out, nBitsForKeypair)
	if err != nil {
		return err
	}

	if fsrepo.IsInitialized(repoRoot) {
		if err := fsrepo.Remove(repoRoot); err != nil {
			return err
		}
	}

	if err := fsrepo.Init(repoRoot, conf); err != nil {
		return err
	}

	if !empty {
		if err := addDefaultAssets(out, repoRoot); err != nil {
			return err
		}
	}

	return initializeIpnsKeyspace(repoRoot)
}
Example #2
0
func doInit(out io.Writer, repoRoot string, force bool, nBitsForKeypair int) error {
	if _, err := fmt.Fprintf(out, "initializing ipfs node at %s\n", repoRoot); err != nil {
		return err
	}

	if err := checkWriteable(repoRoot); err != nil {
		return err
	}

	if fsrepo.IsInitialized(repoRoot) && !force {
		return errRepoExists
	}

	conf, err := config.Init(out, nBitsForKeypair)
	if err != nil {
		return err
	}

	if fsrepo.IsInitialized(repoRoot) {
		if err := fsrepo.Remove(repoRoot); err != nil {
			return err
		}
	}

	if err := fsrepo.Init(repoRoot, conf); err != nil {
		return err
	}

	if err := addDefaultAssets(out, repoRoot); err != nil {
		return err
	}

	return initializeIpnsKeyspace(repoRoot)
}
Example #3
0
func TestSelf(t *testing.T) {
	const (
		rootPath = "test"
	)
	jsonstr := `
{
"max_connection":12,
"file_dir":".",
"run_dir":".",
"option":"option"
} 
`
	if err := fsrepo.Remove("./.ipfs/"); err != nil {
		log.Println(err)
	}
	if err := ioutil.WriteFile("test.json", []byte(jsonstr), 0644); err != nil {
		t.Error(err)
	}
	var cfg config.Config
	config.Load(rootPath, ".", &cfg)
	cfg.SetDefaults()

	self := NewSelf(&cfg, rootPath)
	log.Println("node name", self.NodeName().Pretty())
	if self.NodeName() == "" {
		t.Fatal("nodename is empty")
	}
	n := &merkledag.Node{}
	n.Data = []byte("root")
	n1 := &merkledag.Node{}
	n1.Data = []byte("leaf")
	if err := n.AddNodeLink("leaf", n1); err != nil {
		t.Fatal(err)
	}
	k, err := self.AddDAGNode(n, false)
	if err != nil {
		t.Fatal(err)
	}
	nn, err := self.GetDAGNode(k)
	if err != nil {
		t.Fatal(err)
	}
	log.Println(string(nn.Data))
	if string(n.Data) != string(nn.Data) {
		t.Fatal("not match merkle data")
	}
	d := []byte("test")
	pub := self.Pubkey()
	s, err := self.Sign(d)
	if err != nil {
		t.Fatal(err)
	}
	ok, err := pub.Verify(d, s)
	if err != nil {
		t.Fatal(err)
	}
	if !ok {
		t.Fatal("cannot verify")
	}
	ok, err = pub.Verify(d, []byte("aa"))
	if err == nil && ok {
		t.Fatal("invalid verify")
	}
	skeys := []string{
		"QmZQJubLBGn1JUv8zx24TasR57LnyK1KjUiHWncVqrPxy7",
		"QmU3eUhmUnZxtUJzMtq8YNVz2Q1U75fn2DEgoMXWcCNf6v",
		"QmdrPVCaguLLJM2QnK5qdBDqYwQWQKuq4yHK8E9zX1Et8X",
		"QmPhL4RCK1h241eyMBbYxPBv4iBuguPbRB2gGbGjsuXLQv",
	}
	keys := slice.FromB58Slice(skeys)
	peers := FromStringSlice(skeys, self)
	st, err := self.StorageUsage()
	if err != nil {
		t.Fatal(err)
	}
	log.Println("storage usage", st, "bytes")
	if st <= 0 {
		t.Fatal("illegal storage usage")
	}

	peers1 := FromStringSlice(skeys[1:], self)
	peers2 := FromStringSlice(skeys[:2], self)
	wpd, err := self.WritePeersToDAG(peers, key.Key(""))
	if err != nil {
		t.Fatal(err)
	}
	wpd, err = self.WritePeersToDAG(peers1, wpd)
	if err != nil {
		t.Fatal(err)
	}
	wpd, err = self.WritePeersToDAG(peers2, wpd)
	if err != nil {
		t.Fatal(err)
	}
	p0, k0, err := self.ReadPeersFromDAG(wpd)
	if err != nil {
		t.Fatal(err)
	}
	if !peers2.Equals(p0) {
		t.Fatal("cannot write/read peer list", len(p0))
	}
	p1, k1, err := self.ReadPeersFromDAG(k0)
	if err != nil {
		t.Fatal(err)
	}
	if !peers1.Equals(p1) {
		t.Fatal("cannot write/read peer list")
	}
	p2, k2, err := self.ReadPeersFromDAG(k1)
	if err != nil {
		t.Fatal(err)
	}
	if !peers.Equals(p2) {
		t.Fatal("cannot write/read peer list")
	}
	if k2 != "" {
		t.Fatal("illegal link")
	}
	ts := make(slice.String)
	ts.Add("a")
	ts.Add("b")
	ts.Add("c")
	self.WriteThread(ts)
	if err := self.WriteSpams(keys); err != nil {
		t.Fatal(err)
	}
	if err := self.WritePeers(peers); err != nil {
		t.Fatal(err)
	}
	if err := self.WriteRecent(key.B58KeyDecode(skeys[0])); err != nil {
		t.Fatal(err)
	}
	tag1 := tag.New("test1")
	tagstr := []string{
		"rec1",
		"rec3",
		"rec4",
	}
	tag1.AddSlice(tagstr)
	tag2 := tag.New("test1")
	if !tag2.Add("rec1") {
		t.Errorf("illegal adds")
	}
	tag3 := tag.New("test2")
	if !tag3.Add("rec1") {
		t.Errorf("illegal adds")
	}
	tags := make(tag.Tags)
	tags.Add(tag1)
	tags.Add(tag2)
	tags.Add(tag3)
	if err := self.WriteTag(tags); err != nil {
		t.Fatal(err)
	}

	log.Println("waiting for namesys cache will be outdated.")
	time.Sleep(time.Minute)
	myp := self.ToPeer()
	nnn, err := myp.GetDAGNode("")
	if err != nil {
		t.Fatal(err)
	}
	knnn, err := nnn.Key()
	if err != nil {
		t.Fatal(err)
	}
	if knnn != self.myIpns {
		t.Fatal("illegal ipns key")
	}
	thh := self.ReadThreads()
	if !thh.Equals(ts) {
		t.Error("failed to read threads", len(thh))
	}
	spam, err := myp.ReadSpams()
	if err != nil {
		t.Fatal(err)
	}
	if !spam.Equals(keys) {
		t.Error("failed to read spam", len(spam))
	}
	r, err := myp.ReadRecent()
	if err != nil {
		t.Fatal(err)
	}
	if r != key.B58KeyDecode(skeys[0]) {
		t.Error("cannot read recent")
	}
	rpeers, err := myp.ReadPeers()
	if err != nil {
		t.Fatal(err)
	}
	if !rpeers.Equals(peers) {
		t.Error("failed to read peers")
	}
	rtag, err := myp.ReadTags()
	if err != nil {
		t.Fatal(err)
	}
	if !rtag.Equals(tags) {
		t.Error("failed to read peers")
	}

}
Example #4
0
func TestManager(t *testing.T) {
	const (
		rootPath = "test"
	)
	jsonstr := `
{
"ipfs_repo":"./test1/.ipfs",
"file_dir":"./test1",
"run_dir":"./test1"
} 
`
	jsonstr2 := `
{
"ipfs_repo":"./test2/.ipfs",
"file_dir":"./test2",
"run_dir":"./test2"
} 
`

	if err := os.RemoveAll("./test1"); err != nil {
		t.Log(err)
	}
	if err := os.RemoveAll("./test2"); err != nil {
		t.Log(err)
	}
	if err := os.Mkdir("./test1", 0755); err != nil {
		t.Fatal(err)
	}
	if err := os.Mkdir("./test2", 0755); err != nil {
		t.Fatal(err)
	}

	if err := fsrepo.Remove("./.ipfs"); err != nil {
		log.Println(err)
	}
	if err := ioutil.WriteFile("test.json", []byte(jsonstr), 0644); err != nil {
		t.Error(err)
	}
	var cfg config.Config
	config.Load(rootPath, ".", &cfg)
	cfg.SetDefaults()
	mng, err := New(&cfg, rootPath)
	if err != nil {
		t.Fatal(err)
	}

	if err := ioutil.WriteFile("./test2/init_peers.txt",
		[]byte(mng.NodeName().Pretty()), 0644); err != nil {
		t.Error(err)
	}
	if err := fsrepo.Remove("./.ipfs2"); err != nil {
		log.Println(err)
	}
	if err := ioutil.WriteFile("test.json", []byte(jsonstr2), 0644); err != nil {
		t.Error(err)
	}
	var cfg2 config.Config
	config.Load(rootPath, ".", &cfg2)
	cfg2.SetDefaults()
	mng2, err := New(&cfg2, rootPath)
	if err != nil {
		t.Fatal(err)
	}
	if len(mng2.peers) != 1 {
		t.Fatal("illegal peers")
	}
	cont1 := map[string][]byte{
		"test1": []byte("data1"),
		"test2": []byte("data2"),
	}
	cont2 := map[string][]byte{
		"test3": []byte("data1"),
		"test4": []byte("data2"),
	}
	updated, err := mng2.AddRecord("testA", time.Now(), cont1, true)
	if err != nil {
		t.Fatal(err)
	}
	if !updated {
		t.Fatal("illegal update flag")
	}
	updated, err = mng2.AddRecord("testA", time.Now(), cont2, true)
	if err != nil {
		t.Fatal(err)
	}
	if !updated {
		t.Fatal("illegal update flag")
	}
	if err := mng2.Broadcast(); err != nil {
		t.Fatal(err)
	}
	time.Sleep(time.Minute)
	log.Println(mng, mng2)
}