<commit_before>package server

import (
	"context"
	"log"
	"net"
	"net/http"
	"os"
	"os/signal"
	"syscall"
	"time"

	"github.com/go-chi/chi"
	homedir "github.com/mitchellh/go-homedir"
	"github.com/zerowidth/gh-shorthand/internal/pkg/config"
)

// Run runs the gh-shorthand RPC server on the configured unix socket path
func Run() {
	path, _ := homedir.Expand("~/.gh-shorthand.yml")
	cfg, err := config.LoadFromFile(path)
	if err != nil {
		log.Fatal("couldn't load config", err)
	}

	if len(cfg.SocketPath) == 0 {
		log.Fatal("no socket_path configured in ~/.gh-shorthand.yml")
	}

	r := chi.NewRouter()
	r.Get("/", func(w http.ResponseWriter, r *http.Request) {
		_, _ = w.Write([]byte("hello world\n"))
	})

	server := &http.Server{
		Handler:      r,
		ReadTimeout:  time.Second,
		WriteTimeout: time.Second,
	}

	sigs := make(chan os.Signal, 1)
	done := make(chan interface{})
	signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

	go func() {
		<-sigs
		close(done)
	}()

	sock, err := net.Listen("unix", cfg.SocketPath)
	if err != nil {
		log.Fatal(err)
	}
	defer func() {
		os.Remove(cfg.SocketPath)
	}()

	go func() {
		log.Printf("server started on %s\n", cfg.SocketPath)
		if err := server.Serve(sock); err != nil {
			log.Fatal("server error", err)
		}
	}()

	<-done

	log.Printf("shutting down server...")
	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
	defer cancel()

	if err := server.Shutdown(ctx); err != nil {
		log.Fatal("server shutdown error", err)
	}
}
<commit_msg>Simplify signal handler<commit_after>package server

import (
	"context"
	"log"
	"net"
	"net/http"
	"os"
	"os/signal"
	"syscall"
	"time"

	"github.com/go-chi/chi"
	homedir "github.com/mitchellh/go-homedir"
	"github.com/zerowidth/gh-shorthand/internal/pkg/config"
)

// Run runs the gh-shorthand RPC server on the configured unix socket path
func Run() {
	path, _ := homedir.Expand("~/.gh-shorthand.yml")
	cfg, err := config.LoadFromFile(path)
	if err != nil {
		log.Fatal("couldn't load config", err)
	}

	if len(cfg.SocketPath) == 0 {
		log.Fatal("no socket_path configured in ~/.gh-shorthand.yml")
	}

	r := chi.NewRouter()
	r.Get("/", func(w http.ResponseWriter, r *http.Request) {
		_, _ = w.Write([]byte("hello world\n"))
	})

	server := &http.Server{
		Handler:      r,
		ReadTimeout:  time.Second,
		WriteTimeout: time.Second,
	}

	sig := make(chan os.Signal, 1)
	signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)

	sock, err := net.Listen("unix", cfg.SocketPath)
	if err != nil {
		log.Fatal(err)
	}
	defer func() {
		os.Remove(cfg.SocketPath)
	}()

	go func() {
		log.Printf("server started on %s\n", cfg.SocketPath)
		if err := server.Serve(sock); err != nil {
			log.Fatal("server error", err)
		}
	}()

	<-sig

	log.Printf("shutting down server")
	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
	defer cancel()

	if err := server.Shutdown(ctx); err != nil {
		log.Fatal("server shutdown error", err)
	}
}
<|endoftext|>


<commit_before>package restorer

import (
	"context"
	"os"
	"path/filepath"

	"github.com/restic/restic/internal/crypto"
	"github.com/restic/restic/internal/errors"

	"github.com/restic/restic/internal/debug"
	"github.com/restic/restic/internal/fs"
	"github.com/restic/restic/internal/restic"
)

// Restorer is used to restore a snapshot to a directory.
type Restorer struct {
	repo restic.Repository
	sn   *restic.Snapshot

	Error        func(location string, err error) error
	SelectFilter func(item string, dstpath string, node *restic.Node) (selectedForRestore bool, childMayBeSelected bool)
}

var restorerAbortOnAllErrors = func(location string, err error) error { return err }

// NewRestorer creates a restorer preloaded with the content from the snapshot id.
func NewRestorer(repo restic.Repository, id restic.ID) (*Restorer, error) {
	r := &Restorer{
		repo:         repo,
		Error:        restorerAbortOnAllErrors,
		SelectFilter: func(string, string, *restic.Node) (bool, bool) { return true, true },
	}

	var err error

	r.sn, err = restic.LoadSnapshot(context.TODO(), repo, id)
	if err != nil {
		return nil, err
	}

	return r, nil
}

type treeVisitor struct {
	enterDir  func(node *restic.Node, target, location string) error
	visitNode func(node *restic.Node, target, location string) error
	leaveDir  func(node *restic.Node, target, location string) error
}

// traverseTree traverses a tree from the repo and calls treeVisitor.
// target is the path in the file system, location within the snapshot.
func (res *Restorer) traverseTree(ctx context.Context, target, location string, treeID restic.ID, visitor treeVisitor) error {
	debug.Log("%v %v %v", target, location, treeID)
	tree, err := res.repo.LoadTree(ctx, treeID)
	if err != nil {
		debug.Log("error loading tree %v: %v", treeID, err)
		return res.Error(location, err)
	}

	for _, node := range tree.Nodes {

		// ensure that the node name does not contain anything that refers to a
		// top-level directory.
		nodeName := filepath.Base(filepath.Join(string(filepath.Separator), node.Name))
		if nodeName != node.Name {
			debug.Log("node %q has invalid name %q", node.Name, nodeName)
			err := res.Error(location, errors.Errorf("invalid child node name %s", node.Name))
			if err != nil {
				return err
			}
			continue
		}

		nodeTarget := filepath.Join(target, nodeName)
		nodeLocation := filepath.Join(location, nodeName)

		if target == nodeTarget || !fs.HasPathPrefix(target, nodeTarget) {
			debug.Log("target: %v %v", target, nodeTarget)
			debug.Log("node %q has invalid target path %q", node.Name, nodeTarget)
			err := res.Error(nodeLocation, errors.New("node has invalid path"))
			if err != nil {
				return err
			}
			continue
		}

		// sockets cannot be restored
		if node.Type == "socket" {
			continue
		}

		selectedForRestore, childMayBeSelected := res.SelectFilter(nodeLocation, nodeTarget, node)
		debug.Log("SelectFilter returned %v %v", selectedForRestore, childMayBeSelected)

		sanitizeError := func(err error) error {
			if err != nil {
				err = res.Error(nodeLocation, err)
			}
			return err
		}

		if node.Type == "dir" {
			if node.Subtree == nil {
				return errors.Errorf("Dir without subtree in tree %v", treeID.Str())
			}

			if selectedForRestore {
				err = sanitizeError(visitor.enterDir(node, nodeTarget, nodeLocation))
				if err != nil {
					return err
				}
			}

			if childMayBeSelected {
				err = sanitizeError(res.traverseTree(ctx, nodeTarget, nodeLocation, *node.Subtree, visitor))
				if err != nil {
					return err
				}
			}

			if selectedForRestore {
				err = sanitizeError(visitor.leaveDir(node, nodeTarget, nodeLocation))
				if err != nil {
					return err
				}
			}

			continue
		}

		if selectedForRestore {
			err = sanitizeError(visitor.visitNode(node, nodeTarget, nodeLocation))
			if err != nil {
				return err
			}
		}
	}

	return nil
}

func (res *Restorer) restoreNodeTo(ctx context.Context, node *restic.Node, target, location string) error {
	debug.Log("restoreNode %v %v %v", node.Name, target, location)

	err := node.CreateAt(ctx, target, res.repo)
	if err != nil {
		debug.Log("node.CreateAt(%s) error %v", target, err)
	}
	if err == nil {
		err = res.restoreNodeMetadataTo(node, target, location)
	}

	return err
}

func (res *Restorer) restoreNodeMetadataTo(node *restic.Node, target, location string) error {
	debug.Log("restoreNodeMetadata %v %v %v", node.Name, target, location)
	err := node.RestoreMetadata(target)
	if err != nil {
		debug.Log("node.RestoreMetadata(%s) error %v", target, err)
	}
	return err
}

func (res *Restorer) restoreHardlinkAt(node *restic.Node, target, path, location string) error {
	if err := fs.Remove(path); !os.IsNotExist(err) {
		return errors.Wrap(err, "RemoveCreateHardlink")
	}
	err := fs.Link(target, path)
	if err != nil {
		return errors.Wrap(err, "CreateHardlink")
	}
	return res.restoreNodeMetadataTo(node, target, location)
}

// RestoreTo creates the directories and files in the snapshot below dst.
// Before an item is created, res.Filter is called.
func (res *Restorer) RestoreTo(ctx context.Context, dst string) error {
	var err error
	if !filepath.IsAbs(dst) {
		dst, err = filepath.Abs(dst)
		if err != nil {
			return errors.Wrap(err, "Abs")
		}
	}

	restoreNodeMetadata := func(node *restic.Node, target, location string) error {
		return res.restoreNodeMetadataTo(node, target, location)
	}
	noop := func(node *restic.Node, target, location string) error { return nil }

	idx := restic.NewHardlinkIndex()

	filerestorer := newFileRestorer(dst, res.repo.Backend().Load, res.repo.Key(), filePackTraverser{lookup: res.repo.Index().Lookup})

	// first tree pass: create directories and collect all files to restore
	err = res.traverseTree(ctx, dst, string(filepath.Separator), *res.sn.Tree, treeVisitor{
		enterDir: func(node *restic.Node, target, location string) error {
			// create dir with default permissions
			// #leaveDir restores dir metadata after visiting all children
			return fs.MkdirAll(target, 0700)
		},

		visitNode: func(node *restic.Node, target, location string) error {
			// create parent dir with default permissions
			// second pass #leaveDir restores dir metadata after visiting/restoring all children
			err := fs.MkdirAll(filepath.Dir(target), 0700)
			if err != nil {
				return err
			}

			if node.Type != "file" {
				return nil
			}

			if node.Links > 1 {
				if idx.Has(node.Inode, node.DeviceID) {
					return nil
				}
				idx.Add(node.Inode, node.DeviceID, location)
			}

			filerestorer.addFile(location, node.Content)

			return nil
		},
		leaveDir: noop,
	})
	if err != nil {
		return err
	}

	err = filerestorer.restoreFiles(ctx, func(location string, err error) { res.Error(location, err) })
	if err != nil {
		return err
	}

	// second tree pass: restore special files and filesystem metadata
	return res.traverseTree(ctx, dst, string(filepath.Separator), *res.sn.Tree, treeVisitor{
		enterDir: noop,
		visitNode: func(node *restic.Node, target, location string) error {
			if idx.Has(node.Inode, node.DeviceID) && idx.GetFilename(node.Inode, node.DeviceID) != location {
				return res.restoreHardlinkAt(node, filerestorer.targetPath(idx.GetFilename(node.Inode, node.DeviceID)), target, location)
			}

			if node.Type != "file" {
				return res.restoreNodeTo(ctx, node, target, location)
			}

			return res.restoreNodeMetadataTo(node, target, location)
		},
		leaveDir: restoreNodeMetadata,
	})
}

// Snapshot returns the snapshot this restorer is configured to use.
func (res *Restorer) Snapshot() *restic.Snapshot {
	return res.sn
}

// VerifyFiles reads all snapshot files and verifies their contents
func (res *Restorer) VerifyFiles(ctx context.Context, dst string) (int, error) {
	// TODO multithreaded?

	count := 0
	err := res.traverseTree(ctx, dst, string(filepath.Separator), *res.sn.Tree, treeVisitor{
		enterDir: func(node *restic.Node, target, location string) error { return nil },
		visitNode: func(node *restic.Node, target, location string) error {
			if node.Type != "file" {
				return nil
			}

			count++
			stat, err := os.Stat(target)
			if err != nil {
				return err
			}
			if int64(node.Size) != stat.Size() {
				return errors.Errorf("Invalid file size: expected %d got %d", node.Size, stat.Size())
			}

			offset := int64(0)
			for _, blobID := range node.Content {
				rd, err := os.Open(target)
				if err != nil {
					return err
				}
				blobs, _ := res.repo.Index().Lookup(blobID, restic.DataBlob)
				length := blobs[0].Length - uint(crypto.Extension)
				buf := make([]byte, length) // TODO do I want to reuse the buffer somehow?
				_, err = rd.ReadAt(buf, offset)
				if err != nil {
					return err
				}
				if !blobID.Equal(restic.Hash(buf)) {
					return errors.Errorf("Unexpected contents starting at offset %d", offset)
				}
				offset += int64(length)
			}

			return nil
		},
		leaveDir: func(node *restic.Node, target, location string) error { return nil },
	})

	return count, err
}
<commit_msg>restorer: Add a note on hardlink metadata<commit_after>package restorer

import (
	"context"
	"os"
	"path/filepath"

	"github.com/restic/restic/internal/crypto"
	"github.com/restic/restic/internal/errors"

	"github.com/restic/restic/internal/debug"
	"github.com/restic/restic/internal/fs"
	"github.com/restic/restic/internal/restic"
)

// Restorer is used to restore a snapshot to a directory.
type Restorer struct {
	repo restic.Repository
	sn   *restic.Snapshot

	Error        func(location string, err error) error
	SelectFilter func(item string, dstpath string, node *restic.Node) (selectedForRestore bool, childMayBeSelected bool)
}

var restorerAbortOnAllErrors = func(location string, err error) error { return err }

// NewRestorer creates a restorer preloaded with the content from the snapshot id.
func NewRestorer(repo restic.Repository, id restic.ID) (*Restorer, error) {
	r := &Restorer{
		repo:         repo,
		Error:        restorerAbortOnAllErrors,
		SelectFilter: func(string, string, *restic.Node) (bool, bool) { return true, true },
	}

	var err error

	r.sn, err = restic.LoadSnapshot(context.TODO(), repo, id)
	if err != nil {
		return nil, err
	}

	return r, nil
}

type treeVisitor struct {
	enterDir  func(node *restic.Node, target, location string) error
	visitNode func(node *restic.Node, target, location string) error
	leaveDir  func(node *restic.Node, target, location string) error
}

// traverseTree traverses a tree from the repo and calls treeVisitor.
// target is the path in the file system, location within the snapshot.
func (res *Restorer) traverseTree(ctx context.Context, target, location string, treeID restic.ID, visitor treeVisitor) error {
	debug.Log("%v %v %v", target, location, treeID)
	tree, err := res.repo.LoadTree(ctx, treeID)
	if err != nil {
		debug.Log("error loading tree %v: %v", treeID, err)
		return res.Error(location, err)
	}

	for _, node := range tree.Nodes {

		// ensure that the node name does not contain anything that refers to a
		// top-level directory.
		nodeName := filepath.Base(filepath.Join(string(filepath.Separator), node.Name))
		if nodeName != node.Name {
			debug.Log("node %q has invalid name %q", node.Name, nodeName)
			err := res.Error(location, errors.Errorf("invalid child node name %s", node.Name))
			if err != nil {
				return err
			}
			continue
		}

		nodeTarget := filepath.Join(target, nodeName)
		nodeLocation := filepath.Join(location, nodeName)

		if target == nodeTarget || !fs.HasPathPrefix(target, nodeTarget) {
			debug.Log("target: %v %v", target, nodeTarget)
			debug.Log("node %q has invalid target path %q", node.Name, nodeTarget)
			err := res.Error(nodeLocation, errors.New("node has invalid path"))
			if err != nil {
				return err
			}
			continue
		}

		// sockets cannot be restored
		if node.Type == "socket" {
			continue
		}

		selectedForRestore, childMayBeSelected := res.SelectFilter(nodeLocation, nodeTarget, node)
		debug.Log("SelectFilter returned %v %v", selectedForRestore, childMayBeSelected)

		sanitizeError := func(err error) error {
			if err != nil {
				err = res.Error(nodeLocation, err)
			}
			return err
		}

		if node.Type == "dir" {
			if node.Subtree == nil {
				return errors.Errorf("Dir without subtree in tree %v", treeID.Str())
			}

			if selectedForRestore {
				err = sanitizeError(visitor.enterDir(node, nodeTarget, nodeLocation))
				if err != nil {
					return err
				}
			}

			if childMayBeSelected {
				err = sanitizeError(res.traverseTree(ctx, nodeTarget, nodeLocation, *node.Subtree, visitor))
				if err != nil {
					return err
				}
			}

			if selectedForRestore {
				err = sanitizeError(visitor.leaveDir(node, nodeTarget, nodeLocation))
				if err != nil {
					return err
				}
			}

			continue
		}

		if selectedForRestore {
			err = sanitizeError(visitor.visitNode(node, nodeTarget, nodeLocation))
			if err != nil {
				return err
			}
		}
	}

	return nil
}

func (res *Restorer) restoreNodeTo(ctx context.Context, node *restic.Node, target, location string) error {
	debug.Log("restoreNode %v %v %v", node.Name, target, location)

	err := node.CreateAt(ctx, target, res.repo)
	if err != nil {
		debug.Log("node.CreateAt(%s) error %v", target, err)
	}
	if err == nil {
		err = res.restoreNodeMetadataTo(node, target, location)
	}

	return err
}

func (res *Restorer) restoreNodeMetadataTo(node *restic.Node, target, location string) error {
	debug.Log("restoreNodeMetadata %v %v %v", node.Name, target, location)
	err := node.RestoreMetadata(target)
	if err != nil {
		debug.Log("node.RestoreMetadata(%s) error %v", target, err)
	}
	return err
}

func (res *Restorer) restoreHardlinkAt(node *restic.Node, target, path, location string) error {
	if err := fs.Remove(path); !os.IsNotExist(err) {
		return errors.Wrap(err, "RemoveCreateHardlink")
	}
	err := fs.Link(target, path)
	if err != nil {
		return errors.Wrap(err, "CreateHardlink")
	}
	// TODO investigate if hardlinks have separate metadata on any supported system
	return res.restoreNodeMetadataTo(node, path, location)
}

// RestoreTo creates the directories and files in the snapshot below dst.
// Before an item is created, res.Filter is called.
func (res *Restorer) RestoreTo(ctx context.Context, dst string) error {
	var err error
	if !filepath.IsAbs(dst) {
		dst, err = filepath.Abs(dst)
		if err != nil {
			return errors.Wrap(err, "Abs")
		}
	}

	restoreNodeMetadata := func(node *restic.Node, target, location string) error {
		return res.restoreNodeMetadataTo(node, target, location)
	}
	noop := func(node *restic.Node, target, location string) error { return nil }

	idx := restic.NewHardlinkIndex()

	filerestorer := newFileRestorer(dst, res.repo.Backend().Load, res.repo.Key(), filePackTraverser{lookup: res.repo.Index().Lookup})

	// first tree pass: create directories and collect all files to restore
	err = res.traverseTree(ctx, dst, string(filepath.Separator), *res.sn.Tree, treeVisitor{
		enterDir: func(node *restic.Node, target, location string) error {
			// create dir with default permissions
			// #leaveDir restores dir metadata after visiting all children
			return fs.MkdirAll(target, 0700)
		},

		visitNode: func(node *restic.Node, target, location string) error {
			// create parent dir with default permissions
			// second pass #leaveDir restores dir metadata after visiting/restoring all children
			err := fs.MkdirAll(filepath.Dir(target), 0700)
			if err != nil {
				return err
			}

			if node.Type != "file" {
				return nil
			}

			if node.Links > 1 {
				if idx.Has(node.Inode, node.DeviceID) {
					return nil
				}
				idx.Add(node.Inode, node.DeviceID, location)
			}

			filerestorer.addFile(location, node.Content)

			return nil
		},
		leaveDir: noop,
	})
	if err != nil {
		return err
	}

	err = filerestorer.restoreFiles(ctx, func(location string, err error) { res.Error(location, err) })
	if err != nil {
		return err
	}

	// second tree pass: restore special files and filesystem metadata
	return res.traverseTree(ctx, dst, string(filepath.Separator), *res.sn.Tree, treeVisitor{
		enterDir: noop,
		visitNode: func(node *restic.Node, target, location string) error {
			if idx.Has(node.Inode, node.DeviceID) && idx.GetFilename(node.Inode, node.DeviceID) != location {
				return res.restoreHardlinkAt(node, filerestorer.targetPath(idx.GetFilename(node.Inode, node.DeviceID)), target, location)
			}

			if node.Type != "file" {
				return res.restoreNodeTo(ctx, node, target, location)
			}

			return res.restoreNodeMetadataTo(node, target, location)
		},
		leaveDir: restoreNodeMetadata,
	})
}

// Snapshot returns the snapshot this restorer is configured to use.
func (res *Restorer) Snapshot() *restic.Snapshot {
	return res.sn
}

// VerifyFiles reads all snapshot files and verifies their contents
func (res *Restorer) VerifyFiles(ctx context.Context, dst string) (int, error) {
	// TODO multithreaded?

	count := 0
	err := res.traverseTree(ctx, dst, string(filepath.Separator), *res.sn.Tree, treeVisitor{
		enterDir: func(node *restic.Node, target, location string) error { return nil },
		visitNode: func(node *restic.Node, target, location string) error {
			if node.Type != "file" {
				return nil
			}

			count++
			stat, err := os.Stat(target)
			if err != nil {
				return err
			}
			if int64(node.Size) != stat.Size() {
				return errors.Errorf("Invalid file size: expected %d got %d", node.Size, stat.Size())
			}

			offset := int64(0)
			for _, blobID := range node.Content {
				rd, err := os.Open(target)
				if err != nil {
					return err
				}
				blobs, _ := res.repo.Index().Lookup(blobID, restic.DataBlob)
				length := blobs[0].Length - uint(crypto.Extension)
				buf := make([]byte, length) // TODO do I want to reuse the buffer somehow?
				_, err = rd.ReadAt(buf, offset)
				if err != nil {
					return err
				}
				if !blobID.Equal(restic.Hash(buf)) {
					return errors.Errorf("Unexpected contents starting at offset %d", offset)
				}
				offset += int64(length)
			}

			return nil
		},
		leaveDir: func(node *restic.Node, target, location string) error { return nil },
	})

	return count, err
}
<|endoftext|>


<commit_before>package elements

import (
	"testing"

	"github.com/spf13/afero"
	"github.com/stretchr/testify/assert"
)

func TestDefaultComponentsList(t *testing.T) {
	cl := NewComponentsList("/foo")

	mfs := afero.NewMemMapFs()
	mfs.MkdirAll("/foo", 0655)

	cl.fs = mfs
	cl.Parse()

	assert.True(t, cl.Exists("node"))
	assert.True(t, cl.Exists("actor"))
	assert.True(t, cl.Exists("cloud"))
	assert.False(t, cl.Exists("foo"))
}

func TestComponentsList(t *testing.T) {
	cl := NewComponentsList("/foo")

	mfs := afero.NewMemMapFs()
	mfs.MkdirAll("/foo", 0655)
	mfs.Create("/foo/foo.svg")
	mfs.Create("/foo/bar.svg")
	mfs.Create("/foo/blubb.svg")

	cl.fs = mfs
	cl.Parse()

	assert.True(t, cl.Exists("node"))
	assert.True(t, cl.Exists("actor"))
	assert.True(t, cl.Exists("cloud"))
	assert.True(t, cl.Exists("foo"))
	assert.True(t, cl.Exists("bar"))
	assert.True(t, cl.Exists("blubb"))
	assert.False(t, cl.Exists("baz"))
}

func TestThemedComponentsList(t *testing.T) {
	cl := NewComponentsList("/foo")

	mfs := afero.NewMemMapFs()
	mfs.MkdirAll("/foo", 0655)
	mfs.Create("/foo/foo.svg")
	mfs.Create("/foo/bar.svg")
	mfs.Create("/foo/blubb.svg")
	mfs.Create("/foo/aws/lambda.svg")
	mfs.Create("/foo/aws/ec2_instance.svg")
	mfs.Create("/foo/aws/sns.svg")

	cl.fs = mfs
	cl.Parse()

	assert.True(t, cl.Exists("node"))
	assert.True(t, cl.Exists("actor"))
	assert.True(t, cl.Exists("cloud"))
	assert.True(t, cl.Exists("foo"))
	assert.True(t, cl.Exists("bar"))
	assert.True(t, cl.Exists("blubb"))
	assert.True(t, cl.Exists("aws_lambda"))
	assert.True(t, cl.Exists("aws_ec2_instance"))
	assert.True(t, cl.Exists("aws_sns"))
	assert.False(t, cl.Exists("baz"))
	assert.False(t, cl.Exists("aws_foo"))
}

func TestErrorsComponentsList(t *testing.T) {
	cl := NewComponentsList("/foo")

	mfs := afero.NewMemMapFs()
	cl.fs = mfs
	err := cl.Parse()

	assert.EqualError(t, err, "open /foo: file does not exist")

	cl1 := NewComponentsList("/foo")

	mfs1 := afero.NewMemMapFs()
	mfs1.Mkdir("/foo", 0644)
	mfs1.Mkdir("/foo/bar", 0000)
	cl1.fs = mfs1
	err1 := cl1.Parse()

	assert.Nil(t, err1)
}

func TestGetFilenameByType(t *testing.T) {
	cl := NewComponentsList("/foo")

	mfs := afero.NewMemMapFs()
	mfs.MkdirAll("/foo", 0655)
	mfs.Create("/foo/foo.svg")
	mfs.Create("/foo/bar.svg")
	mfs.Create("/foo/blubb.svg")
	mfs.Create("/foo/aws/lambda.svg")
	mfs.Create("/foo/aws/ec2_instance.svg")
	mfs.Create("/foo/aws/sns.svg")

	cl.fs = mfs
	cl.Parse()
	c := NewComponent("foo", "bar", "blubb")
	assert.Equal(t, "/foo/foo.svg", cl.GetFilenameByType(c))

	c1 := NewComponent("blubb", "bar", "blubb")
	assert.Equal(t, "/foo/blubb.svg", cl.GetFilenameByType(c1))

	c2 := NewComponent("aws_ec2_instance", "bar", "blubb")
	assert.Equal(t, "/foo/aws/ec2_instance.svg", cl.GetFilenameByType(c2))
}
<commit_msg>Added root element test<commit_after>package elements

import (
	"testing"

	"github.com/spf13/afero"
	"github.com/stretchr/testify/assert"
)

func TestDefaultComponentsList(t *testing.T) {
	cl := NewComponentsList("/foo")

	mfs := afero.NewMemMapFs()
	mfs.MkdirAll("/foo", 0655)

	cl.fs = mfs
	cl.Parse()

	assert.True(t, cl.Exists("node"))
	assert.True(t, cl.Exists("actor"))
	assert.True(t, cl.Exists("cloud"))
	assert.False(t, cl.Exists("foo"))
}

func TestComponentsList(t *testing.T) {
	cl := NewComponentsList("/foo")

	mfs := afero.NewMemMapFs()
	mfs.MkdirAll("/foo", 0655)
	mfs.Create("/foo/foo.svg")
	mfs.Create("/foo/bar.svg")
	mfs.Create("/foo/blubb.svg")

	cl.fs = mfs
	cl.Parse()

	assert.True(t, cl.Exists("node"))
	assert.True(t, cl.Exists("actor"))
	assert.True(t, cl.Exists("cloud"))
	assert.True(t, cl.Exists("foo"))
	assert.True(t, cl.Exists("bar"))
	assert.True(t, cl.Exists("blubb"))
	assert.False(t, cl.Exists("baz"))
}

func TestThemedComponentsList(t *testing.T) {
	cl := NewComponentsList("/foo")

	mfs := afero.NewMemMapFs()
	mfs.MkdirAll("/foo", 0655)
	mfs.Create("/foo/foo.svg")
	mfs.Create("/foo/bar.svg")
	mfs.Create("/foo/blubb.svg")
	mfs.Create("/foo/aws/lambda.svg")
	mfs.Create("/foo/aws/ec2_instance.svg")
	mfs.Create("/foo/aws/sns.svg")

	cl.fs = mfs
	cl.Parse()

	assert.True(t, cl.Exists("node"))
	assert.True(t, cl.Exists("actor"))
	assert.True(t, cl.Exists("cloud"))
	assert.True(t, cl.Exists("foo"))
	assert.True(t, cl.Exists("bar"))
	assert.True(t, cl.Exists("blubb"))
	assert.True(t, cl.Exists("aws_lambda"))
	assert.True(t, cl.Exists("aws_ec2_instance"))
	assert.True(t, cl.Exists("aws_sns"))
	assert.False(t, cl.Exists("baz"))
	assert.False(t, cl.Exists("aws_foo"))
}

func TestErrorsComponentsList(t *testing.T) {
	cl := NewComponentsList("/foo")

	mfs := afero.NewMemMapFs()
	cl.fs = mfs
	err := cl.Parse()

	assert.EqualError(t, err, "open /foo: file does not exist")

	cl1 := NewComponentsList("/foo")

	mfs1 := afero.NewMemMapFs()
	mfs1.Mkdir("/foo", 0644)
	mfs1.Mkdir("/foo/bar", 0000)
	cl1.fs = mfs1
	err1 := cl1.Parse()

	assert.Nil(t, err1)
}

func TestGetFilenameByType(t *testing.T) {
	cl := NewComponentsList("/foo")

	mfs := afero.NewMemMapFs()
	mfs.MkdirAll("/foo", 0655)
	mfs.Create("/foo/foo.svg")
	mfs.Create("/foo/bar.svg")
	mfs.Create("/foo/blubb.svg")
	mfs.Create("/foo/aws/lambda.svg")
	mfs.Create("/foo/aws/ec2_instance.svg")
	mfs.Create("/foo/aws/sns.svg")

	cl.fs = mfs
	cl.Parse()
	c := NewComponent("foo", "bar", "blubb")
	assert.Equal(t, "/foo/foo.svg", cl.GetFilenameByType(c))

	c1 := NewComponent("blubb", "bar", "blubb")
	assert.Equal(t, "/foo/blubb.svg", cl.GetFilenameByType(c1))

	c2 := NewComponent("aws_ec2_instance", "bar", "blubb")
	assert.Equal(t, "/foo/aws/ec2_instance.svg", cl.GetFilenameByType(c2))

	c3 := NewComponent("", "bar", "blubb")
	assert.Equal(t, "", cl.GetFilenameByType(c3))
}
<|endoftext|>


<commit_before>package template

var (
	Makefile = `
GOPATH:=$(shell go env GOPATH)

{{if ne .Type "web"}}
.PHONY: proto
proto:
	protoc --proto_path=${GOPATH}/src:. --micro_out=. --go_out=. proto/{{.Alias}}/{{.Alias}}.proto

.PHONY: build
build: proto
{{else}}
.PHONY: build
build:
{{end}}
	go build -o {{.Alias}}-{{.Type}} *.go

.PHONY: test
test:
	go test -v ./... -cover

.PHONY: docker
docker:
	docker build . -t {{.Alias}}-{{.Type}}:latest
`

	GenerateFile = `package main
//go:generate make proto
`
)
<commit_msg>feat: remove GOPATH on --gopath=false<commit_after>package template

var (
	Makefile = `
GOPATH:=$(shell go env GOPATH)

{{if ne .Type "web"}}
.PHONY: proto
proto:
    {{if ne .useGoPath false}}
	protoc --proto_path=${GOPATH}/src:. --micro_out=. --go_out=. proto/{{.Alias}}/{{.Alias}}.proto
    {{else}}
	protoc --proto_path=. --micro_out=. --go_out=. proto/{{.Alias}}/{{.Alias}}.proto

.PHONY: build
build: proto
{{else}}
.PHONY: build
build:
{{end}}
	go build -o {{.Alias}}-{{.Type}} *.go

.PHONY: test
test:
	go test -v ./... -cover

.PHONY: docker
docker:
	docker build . -t {{.Alias}}-{{.Type}}:latest
`

	GenerateFile = `package main
//go:generate make proto
`
)
<|endoftext|>


<commit_before>package capnproto2

import (
	"errors"
	"io"

	"zombiezen.com/go/capnproto2"

	"github.com/goadesign/goa"
)

// Enforce that codec.Decoder satisfies goa.Decoder at compile time
var (
	_ goa.Decoder = (*ProtoDecoder)(nil)
	_ goa.Encoder = (*ProtoEncoder)(nil)
)

type (
	// ProtoDecoder stores state between Reset and Decode
	ProtoDecoder struct {
		dec *capnp.Decoder
	}

	// ProtoEncoder stores state between Reset and Encode
	ProtoEncoder struct {
		enc *capnp.Encoder
	}
)

// NewDecoder returns a new capnp.Decoder that satisfies goa.Decoder
func NewDecoder(r io.Reader) goa.Decoder {
	return &ProtoDecoder{
		dec: capnp.NewDecoder(r),
	}
}

// NewPackedDecoder returns a new packed capnp.Decoder that satisfies goa.Decoder
func NewPackedDecoder(r io.Reader) goa.Decoder {
	return &ProtoDecoder{
		dec: capnp.NewPackedDecoder(r),
	}
}

// Decode unmarshals an io.Reader into *capnp.Message v
func (dec *ProtoDecoder) Decode(v interface{}) error {
	msg, ok := v.(*capnp.Message)
	if !ok {
		return errors.New("Cannot decode into struct that doesn't implement *capnp.Message")
	}

	newMsg, err := dec.dec.Decode()
	if err != nil {
		return err
	}

	if newMsg == nil {
		msg = nil
		return nil
	}

	*msg = *newMsg
	return nil
}

// NewEncoder returns a new capnp.Encoder that satisfies goa.Encoder
func NewEncoder(w io.Writer) goa.Encoder {
	return &ProtoEncoder{
		enc: capnp.NewEncoder(w),
	}
}

// NewPackedEncoder returns a new packed capnp.Encoder that satisfies goa.Encoder
func NewPackedEncoder(w io.Writer) goa.Encoder {
	return &ProtoEncoder{
		enc: capnp.NewPackedEncoder(w),
	}
}

// Encode marshals a *capnp.Message and writes it to an io.Writer
func (enc *ProtoEncoder) Encode(v interface{}) error {
	msg, ok := v.(*capnp.Message)
	if !ok {
		return errors.New("Cannot encode struct that doesn't implement *capnp.Message")
	}

	var err error

	if err = enc.enc.Encode(msg); err != nil {
		return err
	}

	return nil
}
<commit_msg>Attempt to fix golint issue<commit_after>package capnproto2

import (
	"errors"
	"io"

	"github.com/goadesign/goa"
	"zombiezen.com/go/capnproto2"
)

// Enforce that codec.Decoder satisfies goa.Decoder at compile time
var (
	_ goa.Decoder = (*ProtoDecoder)(nil)
	_ goa.Encoder = (*ProtoEncoder)(nil)
)

type (
	// ProtoDecoder stores state between Reset and Decode
	ProtoDecoder struct {
		dec *capnp.Decoder
	}

	// ProtoEncoder stores state between Reset and Encode
	ProtoEncoder struct {
		enc *capnp.Encoder
	}
)

// NewDecoder returns a new capnp.Decoder that satisfies goa.Decoder
func NewDecoder(r io.Reader) goa.Decoder {
	return &ProtoDecoder{
		dec: capnp.NewDecoder(r),
	}
}

// NewPackedDecoder returns a new packed capnp.Decoder that satisfies goa.Decoder
func NewPackedDecoder(r io.Reader) goa.Decoder {
	return &ProtoDecoder{
		dec: capnp.NewPackedDecoder(r),
	}
}

// Decode unmarshals an io.Reader into *capnp.Message v
func (dec *ProtoDecoder) Decode(v interface{}) error {
	msg, ok := v.(*capnp.Message)
	if !ok {
		return errors.New("Cannot decode into struct that doesn't implement *capnp.Message")
	}

	newMsg, err := dec.dec.Decode()
	if err != nil {
		return err
	}

	if newMsg == nil {
		msg = nil
		return nil
	}

	*msg = *newMsg
	return nil
}

// NewEncoder returns a new capnp.Encoder that satisfies goa.Encoder
func NewEncoder(w io.Writer) goa.Encoder {
	return &ProtoEncoder{
		enc: capnp.NewEncoder(w),
	}
}

// NewPackedEncoder returns a new packed capnp.Encoder that satisfies goa.Encoder
func NewPackedEncoder(w io.Writer) goa.Encoder {
	return &ProtoEncoder{
		enc: capnp.NewPackedEncoder(w),
	}
}

// Encode marshals a *capnp.Message and writes it to an io.Writer
func (enc *ProtoEncoder) Encode(v interface{}) error {
	msg, ok := v.(*capnp.Message)
	if !ok {
		return errors.New("Cannot encode struct that doesn't implement *capnp.Message")
	}

	var err error

	if err = enc.enc.Encode(msg); err != nil {
		return err
	}

	return nil
}
<|endoftext|>


<commit_before>// Copyright 2016 Politecnico di Torino
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package router

import (
	"bytes"
	"errors"
	"fmt"
	"net"
	"strconv"

	"github.com/mvbpolito/gosexy/to"

	"github.com/netgroup-polito/iovisor-ovn/hoverctl"
	l "github.com/op/go-logging"
)

var log = l.MustGetLogger("iomodules-router")

type RouterModule struct {
	ModuleId     string
	PortsCount   int //number of allocated ports
	RoutingTable [10]RoutingTableEntry
	Interfaces   map[string]*RouterModuleInterface

	deployed  bool
	dataplane *hoverctl.Dataplane // used to send commands to hover
}

type RouterModuleInterface struct {
	IfaceIdRedirectHover int    // Iface id inside hover
	LinkIdHover          string // iomodules Link Id
	IfaceName            string
	IP                   string
	Netmask              string
	MAC                  string
}

type RoutingTableEntry struct {
	network string
	netmask string
	outputIface string
	nexthop string
}

func Create(dp *hoverctl.Dataplane) *RouterModule {

	if dp == nil {
		log.Errorf("Dataplane is not valid")
		return nil
	}

	r := new(RouterModule)
	r.Interfaces = make(map[string]*RouterModuleInterface)
	r.dataplane = dp
	r.deployed = false
	return r
}

func (r *RouterModule) GetModuleId() string {
	return r.ModuleId
}

func (r *RouterModule) Deploy() (err error) {

	if r.deployed {
		return nil
	}

	routerError, routerHover := hoverctl.ModulePOST(r.dataplane, "bpf",
		"Router", RouterCode)
	if routerError != nil {
		log.Errorf("Error in POST Router IOModule: %s\n", routerError)
		return routerError
	}

	log.Noticef("POST Router IOModule %s\n", routerHover.Id)
	r.ModuleId = routerHover.Id
	r.deployed = true

	return nil
}

func (r *RouterModule) Destroy() (err error) {

	if !r.deployed {
		return nil
	}

	// TODO:
	// All interfaces must be detached before destroying the module.
	// Should it be done automatically here, or should be the application responsible for that?

	moduleDeleteError, _ := hoverctl.ModuleDELETE(r.dataplane, r.ModuleId)
	if moduleDeleteError != nil {
		log.Errorf("Error in destrying Router IOModule: %s\n", moduleDeleteError)
		return moduleDeleteError
	}

	r.ModuleId = ""
	r.deployed = false

	return nil
}

func (r *RouterModule) AttachExternalInterface(ifaceName string) (err error) {

	if !r.deployed {
		errString := "Trying to attach port in undeployed router"
		log.Errorf(errString)
		return errors.New(errString)
	}

	if r.PortsCount == 10 {
		errString := "There are not free ports in the router\n"
		log.Errorf(errString)
		return errors.New(errString)
	}

	linkError, linkHover := hoverctl.LinkPOST(r.dataplane, "i:"+ifaceName, r.ModuleId)
	if linkError != nil {
		log.Errorf("Error in POSTing the Link: %s\n", linkError)
		return linkError
	}

	r.PortsCount++

	// Saving IfaceIdRedirectHover for this port. The number will be used by security policies
	ifacenumber := -1
	if linkHover.From[0:2] == "m:" {
		ifacenumber = linkHover.FromId
	}
	if linkHover.To[0:2] == "m:" {
		ifacenumber = linkHover.ToId
	}
	if ifacenumber == -1 {
		log.Warningf("IfaceIdRedirectHover == -1 something wrong happened...\n")
	}

	iface := new(RouterModuleInterface)

	iface.IfaceIdRedirectHover = ifacenumber
	iface.LinkIdHover = linkHover.Id
	iface.IfaceName = ifaceName

	r.Interfaces[ifaceName] = iface

	return nil
}

func (r *RouterModule) DetachExternalInterface(ifaceName string) (err error) {

	if !r.deployed {
		errString := "Trying to detach port in undeployed router"
		log.Errorf(errString)
		return errors.New(errString)
	}

	iface, ok := r.Interfaces[ifaceName]

	if !ok {
		errString := fmt.Sprintf("Iface '%s' is not present in router '%s'\n",
			ifaceName, r.ModuleId)
		log.Warningf(errString)
		return errors.New(errString)
	}

	linkDeleteError, _ := hoverctl.LinkDELETE(r.dataplane, iface.LinkIdHover)

	if linkDeleteError != nil {
		log.Warningf("Problem removing iface '%s' from router '%s'\n",
			ifaceName, r.ModuleId)
		return linkDeleteError
	}

	// remove port from list of ports
	ifaceIdString := strconv.Itoa(iface.IfaceIdRedirectHover)
	hoverctl.TableEntryDELETE(r.dataplane, r.ModuleId, "router_port", ifaceIdString)

	// TODO: remove static route entry
	delete(r.Interfaces, ifaceName)

	return nil
}

func (r *RouterModule) AttachToIoModule(ifaceId int, ifaceName string) (err error) {
	if !r.deployed {
		errString := "Trying to attach port in undeployed router"
		log.Errorf(errString)
		return errors.New(errString)
	}

	if r.PortsCount == 10 {
		errString := "There are not free ports in the router"
		log.Errorf(errString)
		return errors.New(errString)
	}

	iface := new(RouterModuleInterface)

	iface.IfaceIdRedirectHover = ifaceId
	iface.LinkIdHover = ""
	iface.IfaceName = ifaceName

	r.Interfaces[ifaceName] = iface

	return nil
}

func (r *RouterModule) DetachFromIoModule(ifaceName string) (err error) {
	return errors.New("Not implemented")
}

// After a interface has been added, it is necessary to configure it before
// it can be used to route packets
//TODO I think we have to add next hop parameter here!
func (r *RouterModule) ConfigureInterface(ifaceName string, ip string,
	netmask string, mac string) (err error) {
	if !r.deployed {
		errString := "Trying to configure an interface in undeployed router"
		log.Errorf(errString)
		return errors.New(errString)
	}

	iface, ok := r.Interfaces[ifaceName]

	if !ok {
		errString := fmt.Sprintf("Iface '%s' is not present in router '%s'\n",
			ifaceName, r.ModuleId)
		log.Warningf(errString)
		return errors.New(errString)
	}

	// TODO: check ip, netmask and mac

	iface.IP = ip
	iface.Netmask = netmask
	iface.MAC = mac

	// configure port entry
	ifaceIdString := strconv.Itoa(iface.IfaceIdRedirectHover)
	ipString := ipToHexadecimalString(ip)
	netmaskString := ipToHexadecimalString(netmask)
	macString := macToHexadecimalString(mac)

	toSend := ipString + " " + netmaskString + " " + macString

	hoverctl.TableEntryPOST(r.dataplane, r.ModuleId, "router_port",
		ifaceIdString, toSend)

	ip_ := net.ParseIP(ip)
	netmask_ := ParseIPv4Mask(netmask)
	network_ := ip_.Mask(netmask_)

	// add route for that port
	if r.AddRoutingTableEntryLocal(network_.String(), netmask, ifaceName) != nil {
		errString := fmt.Sprintf("Error adding static route for port '%s' in router '%s'\n",
			ifaceName, r.ModuleId)
		log.Warningf(errString)
		return errors.New(errString)
	}

	return nil
}

// A local entry of the routing table indicates that the network interface is
// directly attached, so there is no need of the next hop address.
// This function force the routing table entry to be local, pushing 0 as nexthop
func (r *RouterModule) AddRoutingTableEntryLocal(network string, netmask string,
	outputIface string) (err error) {
	return r.AddRoutingTableEntry(network, netmask, outputIface, "0.0.0.0")
}

// With the current implementation of the eBPF router this functions is a kind
// of complicated.  This is because it has to add the routes in a sorte way in the
// table, routes with the longest prefix should appear first.
// However current implementation, it is the very first one, only adds the routes
// one after the other, without performing this sorting
// next hop is a string indicating the ip address of the nexthop, 0 if local iface
func (r *RouterModule) AddRoutingTableEntry(network string, netmask string,
	outputIface string, nexthop string) (err error) {

	// look for a free entry in the routing table
	index := -1
	for i := 0; i < 10; i++ {
		if r.RoutingTable[i].network == "" {
			index = i
			break
		}
	}

	if index == -1 {
		return errors.New("Routing table is full")
	}

	iface, ok := r.Interfaces[outputIface]

	if !ok {
		errString := fmt.Sprintf("Iface '%s' is not present in router '%s'\n",
			outputIface, r.ModuleId)
		log.Warningf(errString)
		return errors.New(errString)
	}

	stringIndex := strconv.Itoa(index)
	toSend := "{" + ipToHexadecimalString(network) + " " +
		ipToHexadecimalString(netmask) + " " +
		strconv.Itoa(iface.IfaceIdRedirectHover) + " " +
		ipToHexadecimalString(nexthop) + "}"

	hoverctl.TableEntryPUT(r.dataplane, r.ModuleId, "routing_table",
		stringIndex, toSend)

	r.RoutingTable[index].network = network
	r.RoutingTable[index].netmask = netmask
	r.RoutingTable[index].outputIface = outputIface
	r.RoutingTable[index].nexthop = nexthop

	return nil
}

// TODO: Implement this function
//func (r *RouterModule) RemoveRoutingTableEntry(network string) {
//
//}

func (r *RouterModule) Configure(conf interface{}) (err error) {
	// The interface is a map that contains:
	// interfaces: A list of maps for the interfaces present on the router, each
	// of this has to have:
	//		name, ip, netmask, mac
	// static_routes: A list of map containing the static routes to be configured
	// 		network: network address
	//		netmask: netmask for network
	//		interface: output interface where destination can be reached
	//		next_hop: [optional] ip address of next hop

	log.Infof("Configuring Router")
	confMap := to.Map(conf)

	// configure interfaces
	if interfaces, ok := confMap["interfaces"]; ok {
		for _, entry := range to.List(interfaces) {
			entryMap := to.Map(entry)

			name, ok1 := entryMap["name"]
			ip, ok2 := entryMap["ip"]
			netmask, ok3 := entryMap["netmask"]
			mac, ok4 := entryMap["mac"]

			if !ok1 || !ok2 || !ok3 || !ok4 {
				log.Errorf("Skipping non valid interface")
				continue
			}

			log.Infof("Configuring Interface '%s', '%s', '%s', '%s'",
				name.(string), ip.(string), netmask.(string), mac.(string))

			err := r.ConfigureInterface(name.(string), ip.(string),
				netmask.(string), mac.(string))
			if err != nil {
				return err
			}
		}
	}

	// configure static routes
	if static_routes, ok := confMap["static_routes"]; ok {
		for _, entry := range to.List(static_routes) {
			entryMap := to.Map(entry)

			network, ok1 := entryMap["network"]
			netmask, ok2 := entryMap["netmask"]
			interface_, ok3 := entryMap["interface"]
			next_hop, ok4 := entryMap["next_hop"]

			if !ok1 || !ok2|| !ok3 {
				log.Errorf("Skipping non valid static route")
				continue
			}

			if !ok4 {
				next_hop = "0"
			}

			log.Infof("Adding Static Route: '%s', '%s', '%s', '%s'",
				network.(string), netmask.(string), interface_.(string), next_hop.(string))

			err := r.AddRoutingTableEntry(network.(string), netmask.(string),
				interface_.(string), next_hop.(string))
			if err != nil {
				return err
			}
		}
	}

	return nil
}

// TODO: this function should be smarter
func macToHexadecimalString(s string) string {
	var buffer bytes.Buffer

	buffer.WriteString("0x")
	buffer.WriteString(s[0:2])
	buffer.WriteString(s[3:5])
	buffer.WriteString(s[6:8])
	buffer.WriteString(s[9:11])
	buffer.WriteString(s[12:14])
	buffer.WriteString(s[15:17])

	return buffer.String()
}

func ipToHexadecimalString(ip string) string {

	trial := net.ParseIP(ip)
	if trial.To4() != nil {
		ba := []byte(trial.To4())
		ipv4HexStr := fmt.Sprintf("0x%02x%02x%02x%02x", ba[0], ba[1], ba[2], ba[3])
		return ipv4HexStr
	}

	return ""
}

func ParseIPv4Mask(s string) net.IPMask {
	mask := net.ParseIP(s)
	if mask == nil {
		return nil
	}
	return net.IPv4Mask(mask[12], mask[13], mask[14], mask[15])
}
<commit_msg>iomodules/router: sort routing tables by longest prefix<commit_after>// Copyright 2016 Politecnico di Torino
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package router

import (
	"bytes"
	"errors"
	"fmt"
	"net"
	"sort"
	"strconv"

	"github.com/mvbpolito/gosexy/to"

	"github.com/netgroup-polito/iovisor-ovn/hoverctl"
	l "github.com/op/go-logging"
)

var log = l.MustGetLogger("iomodules-router")

type RouterModule struct {
	ModuleId     string
	PortsCount   int //number of allocated ports
	RoutingTable []RoutingTableEntry
	routingTableCount int // number of elements in the routing table
	Interfaces   map[string]*RouterModuleInterface

	deployed  bool
	dataplane *hoverctl.Dataplane // used to send commands to hover
}

type RouterModuleInterface struct {
	IfaceIdRedirectHover int    // Iface id inside hover
	LinkIdHover          string // iomodules Link Id
	IfaceName            string
	IP                   string
	Netmask              string
	MAC                  string
}

type RoutingTableEntry struct {
	network string
	netmask string
	outputIface *RouterModuleInterface
	nexthop string
}

func Create(dp *hoverctl.Dataplane) *RouterModule {

	if dp == nil {
		log.Errorf("Dataplane is not valid")
		return nil
	}

	r := new(RouterModule)
	r.Interfaces = make(map[string]*RouterModuleInterface)
	r.RoutingTable = make([]RoutingTableEntry, 10)
	r.dataplane = dp
	r.deployed = false
	return r
}

func (r *RouterModule) GetModuleId() string {
	return r.ModuleId
}

func (r *RouterModule) Deploy() (err error) {

	if r.deployed {
		return nil
	}

	routerError, routerHover := hoverctl.ModulePOST(r.dataplane, "bpf",
		"Router", RouterCode)
	if routerError != nil {
		log.Errorf("Error in POST Router IOModule: %s\n", routerError)
		return routerError
	}

	log.Noticef("POST Router IOModule %s\n", routerHover.Id)
	r.ModuleId = routerHover.Id
	r.deployed = true

	return nil
}

func (r *RouterModule) Destroy() (err error) {

	if !r.deployed {
		return nil
	}

	// TODO:
	// All interfaces must be detached before destroying the module.
	// Should it be done automatically here, or should be the application responsible for that?

	moduleDeleteError, _ := hoverctl.ModuleDELETE(r.dataplane, r.ModuleId)
	if moduleDeleteError != nil {
		log.Errorf("Error in destrying Router IOModule: %s\n", moduleDeleteError)
		return moduleDeleteError
	}

	r.ModuleId = ""
	r.deployed = false

	return nil
}

func (r *RouterModule) AttachExternalInterface(ifaceName string) (err error) {

	if !r.deployed {
		errString := "Trying to attach port in undeployed router"
		log.Errorf(errString)
		return errors.New(errString)
	}

	if r.PortsCount == 10 {
		errString := "There are not free ports in the router\n"
		log.Errorf(errString)
		return errors.New(errString)
	}

	linkError, linkHover := hoverctl.LinkPOST(r.dataplane, "i:"+ifaceName, r.ModuleId)
	if linkError != nil {
		log.Errorf("Error in POSTing the Link: %s\n", linkError)
		return linkError
	}

	r.PortsCount++

	// Saving IfaceIdRedirectHover for this port. The number will be used by security policies
	ifacenumber := -1
	if linkHover.From[0:2] == "m:" {
		ifacenumber = linkHover.FromId
	}
	if linkHover.To[0:2] == "m:" {
		ifacenumber = linkHover.ToId
	}
	if ifacenumber == -1 {
		log.Warningf("IfaceIdRedirectHover == -1 something wrong happened...\n")
	}

	iface := new(RouterModuleInterface)

	iface.IfaceIdRedirectHover = ifacenumber
	iface.LinkIdHover = linkHover.Id
	iface.IfaceName = ifaceName

	r.Interfaces[ifaceName] = iface

	return nil
}

func (r *RouterModule) DetachExternalInterface(ifaceName string) (err error) {

	if !r.deployed {
		errString := "Trying to detach port in undeployed router"
		log.Errorf(errString)
		return errors.New(errString)
	}

	iface, ok := r.Interfaces[ifaceName]

	if !ok {
		errString := fmt.Sprintf("Iface '%s' is not present in router '%s'\n",
			ifaceName, r.ModuleId)
		log.Warningf(errString)
		return errors.New(errString)
	}

	linkDeleteError, _ := hoverctl.LinkDELETE(r.dataplane, iface.LinkIdHover)

	if linkDeleteError != nil {
		log.Warningf("Problem removing iface '%s' from router '%s'\n",
			ifaceName, r.ModuleId)
		return linkDeleteError
	}

	// remove port from list of ports
	ifaceIdString := strconv.Itoa(iface.IfaceIdRedirectHover)
	hoverctl.TableEntryDELETE(r.dataplane, r.ModuleId, "router_port", ifaceIdString)

	// TODO: remove static route entry
	delete(r.Interfaces, ifaceName)

	return nil
}

func (r *RouterModule) AttachToIoModule(ifaceId int, ifaceName string) (err error) {
	if !r.deployed {
		errString := "Trying to attach port in undeployed router"
		log.Errorf(errString)
		return errors.New(errString)
	}

	if r.PortsCount == 10 {
		errString := "There are not free ports in the router"
		log.Errorf(errString)
		return errors.New(errString)
	}

	iface := new(RouterModuleInterface)

	iface.IfaceIdRedirectHover = ifaceId
	iface.LinkIdHover = ""
	iface.IfaceName = ifaceName

	r.Interfaces[ifaceName] = iface

	return nil
}

func (r *RouterModule) DetachFromIoModule(ifaceName string) (err error) {
	return errors.New("Not implemented")
}

// After a interface has been added, it is necessary to configure it before
// it can be used to route packets
//TODO I think we have to add next hop parameter here!
func (r *RouterModule) ConfigureInterface(ifaceName string, ip string,
	netmask string, mac string) (err error) {
	if !r.deployed {
		errString := "Trying to configure an interface in undeployed router"
		log.Errorf(errString)
		return errors.New(errString)
	}

	iface, ok := r.Interfaces[ifaceName]

	if !ok {
		errString := fmt.Sprintf("Iface '%s' is not present in router '%s'\n",
			ifaceName, r.ModuleId)
		log.Warningf(errString)
		return errors.New(errString)
	}

	// TODO: check ip, netmask and mac

	iface.IP = ip
	iface.Netmask = netmask
	iface.MAC = mac

	// configure port entry
	ifaceIdString := strconv.Itoa(iface.IfaceIdRedirectHover)
	ipString := ipToHexadecimalString(ip)
	netmaskString := ipToHexadecimalString(netmask)
	macString := macToHexadecimalString(mac)

	toSend := ipString + " " + netmaskString + " " + macString

	hoverctl.TableEntryPOST(r.dataplane, r.ModuleId, "router_port",
		ifaceIdString, toSend)

	ip_ := net.ParseIP(ip)
	netmask_ := ParseIPv4Mask(netmask)
	network_ := ip_.Mask(netmask_)

	// add route for that port
	if r.AddRoutingTableEntryLocal(network_.String(), netmask, ifaceName) != nil {
		errString := fmt.Sprintf("Error adding static route for port '%s' in router '%s'\n",
			ifaceName, r.ModuleId)
		log.Warningf(errString)
		return errors.New(errString)
	}

	return nil
}

// A local entry of the routing table indicates that the network interface is
// directly attached, so there is no need of the next hop address.
// This function force the routing table entry to be local, pushing 0 as nexthop
func (r *RouterModule) AddRoutingTableEntryLocal(network string, netmask string,
	outputIface string) (err error) {
	return r.AddRoutingTableEntry(network, netmask, outputIface, "0.0.0.0")
}

// Routes in the routing table have to be ordered according to the netmask length.
// This is because of a limitation in the eBPF datapath (no way to perform LPM there)
// next hop is a string indicating the ip address of the nexthop, 0.0.0.0 indicates that
// the network is directly attached to the router.
func (r *RouterModule) AddRoutingTableEntry(network string, netmask string,
	outputIface string, nexthop string) (err error) {

	if r.routingTableCount == 10 {
		return errors.New("Routing table is full")
	}

	index := r.routingTableCount

	iface, ok := r.Interfaces[outputIface]

	if !ok {
		errString := fmt.Sprintf("Iface '%s' is not present in router '%s'\n",
			outputIface, r.ModuleId)
		log.Warningf(errString)
		return errors.New(errString)
	}

	r.RoutingTable[index].network = network
	r.RoutingTable[index].netmask = netmask
	r.RoutingTable[index].outputIface = iface
	r.RoutingTable[index].nexthop = nexthop

	r.routingTableCount++

	r.sortRoutingTable()
	r.sendRoutingTable()

	return nil
}

// implement sort.Interface in order to use the sort function

type ByMaskLen []RoutingTableEntry

func (s ByMaskLen) Len() int {
	return len(s)
}

func (s ByMaskLen) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}

func (s ByMaskLen) Less(i, j int) bool {
	neti := ParseIPv4Mask(s[i].netmask)
	netj := ParseIPv4Mask(s[j].netmask)

	si, _ := neti.Size()
	sj, _ := netj.Size()

	return si > sj
}

// sort routing table entries according to the length of the netmas, the longest
// ones are first
func (r *RouterModule) sortRoutingTable() () {
	sort.Sort(ByMaskLen(r.RoutingTable))
}

func (r *RouterModule) sendRoutingTable() (err error) {

	index := 0
	for _, i := range r.RoutingTable {
		if i.network == "" {
			break
		}

		stringIndex := strconv.Itoa(index)

		toSend := "{" + ipToHexadecimalString(i.network) + " " +
			ipToHexadecimalString(i.netmask) + " " +
			strconv.Itoa(i.outputIface.IfaceIdRedirectHover) + " " +
			ipToHexadecimalString(i.nexthop) + "}"

		hoverctl.TableEntryPUT(r.dataplane, r.ModuleId, "routing_table",
			stringIndex, toSend)

		index++
	}

	return nil;
}

// TODO: Implement this function
//func (r *RouterModule) RemoveRoutingTableEntry(network string) {
//
//}

func (r *RouterModule) Configure(conf interface{}) (err error) {
	// The interface is a map that contains:
	// interfaces: A list of maps for the interfaces present on the router, each
	// of this has to have:
	//		name, ip, netmask, mac
	// static_routes: A list of map containing the static routes to be configured
	// 		network: network address
	//		netmask: netmask for network
	//		interface: output interface where destination can be reached
	//		next_hop: [optional] ip address of next hop

	log.Infof("Configuring Router")
	confMap := to.Map(conf)

	// configure interfaces
	if interfaces, ok := confMap["interfaces"]; ok {
		for _, entry := range to.List(interfaces) {
			entryMap := to.Map(entry)

			name, ok1 := entryMap["name"]
			ip, ok2 := entryMap["ip"]
			netmask, ok3 := entryMap["netmask"]
			mac, ok4 := entryMap["mac"]

			if !ok1 || !ok2 || !ok3 || !ok4 {
				log.Errorf("Skipping non valid interface")
				continue
			}

			log.Infof("Configuring Interface '%s', '%s', '%s', '%s'",
				name.(string), ip.(string), netmask.(string), mac.(string))

			err := r.ConfigureInterface(name.(string), ip.(string),
				netmask.(string), mac.(string))
			if err != nil {
				return err
			}
		}
	}

	// configure static routes
	if static_routes, ok := confMap["static_routes"]; ok {
		for _, entry := range to.List(static_routes) {
			entryMap := to.Map(entry)

			network, ok1 := entryMap["network"]
			netmask, ok2 := entryMap["netmask"]
			interface_, ok3 := entryMap["interface"]
			next_hop, ok4 := entryMap["next_hop"]

			if !ok1 || !ok2|| !ok3 {
				log.Errorf("Skipping non valid static route")
				continue
			}

			if !ok4 {
				next_hop = "0"
			}

			log.Infof("Adding Static Route: '%s', '%s', '%s', '%s'",
				network.(string), netmask.(string), interface_.(string), next_hop.(string))

			err := r.AddRoutingTableEntry(network.(string), netmask.(string),
				interface_.(string), next_hop.(string))
			if err != nil {
				return err
			}
		}
	}

	return nil
}

// TODO: this function should be smarter
func macToHexadecimalString(s string) string {
	var buffer bytes.Buffer

	buffer.WriteString("0x")
	buffer.WriteString(s[0:2])
	buffer.WriteString(s[3:5])
	buffer.WriteString(s[6:8])
	buffer.WriteString(s[9:11])
	buffer.WriteString(s[12:14])
	buffer.WriteString(s[15:17])

	return buffer.String()
}

func ipToHexadecimalString(ip string) string {

	trial := net.ParseIP(ip)
	if trial.To4() != nil {
		ba := []byte(trial.To4())
		ipv4HexStr := fmt.Sprintf("0x%02x%02x%02x%02x", ba[0], ba[1], ba[2], ba[3])
		return ipv4HexStr
	}

	return ""
}

func ParseIPv4Mask(s string) net.IPMask {
	mask := net.ParseIP(s)
	if mask == nil {
		return nil
	}
	return net.IPv4Mask(mask[12], mask[13], mask[14], mask[15])
}
<|endoftext|>


<commit_before>package elasticService

import (
	"net/http"
	"fmt"
	"errors"
	"bytes"
	"encoding/json"
)

const (
	SEARCH = "/_search"
	JSON = "application/json"
)

type ShardInfo struct {
	Total int		`json:"total"`
	Success int		`json:"successful"`
	Failed int		`json:"failed"`
}

type DocInfo struct {
	Index string				`json:"_index"`
	Type string					`json:"_type"`
	Id string					`json:"_id"`
	Score float32				`json:"_score"`
	Source UpdateESDocType		`json:"_source"`
	Sort []string				`json:"sort"`
}

type HitsInfo struct {
	Total int			`json:"total"`
	MaxScore float32	`json:"max_score"`
	Hits []DocInfo		`json:"hits"`
}

type SearchResponse struct {
	Took int			`json:"took"`
	TimeOut bool		`json:"timed_out"`
	Shards ShardInfo	`json:"_shards"`
	Hits HitsInfo		`json:"hits"`
}

type MatchUID struct {
	Uid string `json:"_uid"`
}

type MatchDeviceID struct {
	ID int	`json:"device_id"`
}

type MustGeoMatch struct {
	MatchAll interface{} 	`json:"match_all"`
}

type TermMatch struct {
	Status int 		`json:"status"`
}

type MustGeoStatusMatch struct {
	Term TermMatch		`json:"term"`
}

type Bound struct {
	Lat float32		`json:"lat"`
	Lng float32		`json:"lon"`
}

type BoxBounds struct {
	UpperLeft	Bound	`json:"top_left"`
	BottomRight	Bound	`json:"bottom_right"`
}

type GeoBox struct {
	Bounds BoxBounds 	`json:"geo.location"`
}

type FilterGeo struct {
	BoundingBox GeoBox	`json:"geo_bounding_box"`
}

type BoolGeo struct {
	M MustGeoMatch		`json:"must"`
	F FilterGeo			`json:"filter"`
}

type BoolGeoStatus struct {
	M MustGeoStatusMatch	`json:"must"`
	F FilterGeo				`json:"filter"`
}

type QueryDeviceId struct {
	Match MatchDeviceID		`json:"match"`
}

type QueryGeoShit struct {
	B BoolGeo	`json:"bool"`
}

type QueryGeoStatusShit struct {
	B BoolGeoStatus `json:"bool"`
}

type SearchDeviceIDRequest struct {
	Query QueryDeviceId		`json:"query"`
	Sort []MatchUID		`json:"sort"`
}

type SearchGeoRequest struct {
	Query QueryGeoShit		`json:"query"`
	Sort []MatchUID			`json:"sort"`
}

type SearchGeoStatusRequest struct {
	Query QueryGeoStatusShit	`json:"query"`
	Sort []MatchUID				`json:"sort"`
}

func (db *ElasticSearchDB) SearchDeviceId(deviceId int) (SearchResponse, error) {
	var searchResponse SearchResponse

	// Construct search request
	searchRequest := SearchDeviceIDRequest{QueryDeviceId{MatchDeviceID{deviceId}}, nil}

	// Send request
	resp, err := sendJson(db, &searchRequest)
	defer resp.Body.Close()

	// If it failed give up
	if err != nil {
		if db.errLogger != nil {
			db.errLogger.Error(err, "Failed doing sumthing in search device id")
		}
		return searchResponse, err
	}

	// If it didn't fail get result
	decoder := json.NewDecoder(resp.Body)
	return searchResponse, decoder.Decode(&searchResponse)
}

func (db *ElasticSearchDB) SearchGeo(upperLeft, bottomRight Bound) (SearchResponse, error) {
	var searchResponse SearchResponse

	// Construct search request
	searchRequest := SearchGeoRequest{}

	return searchResponse, nil
}

func (db *ElasticSearchDB) SearchGeoStatus(upperLeft, bottomRight Bound, stat int) (SearchResponse,  error) {
	var searchResponse SearchResponse

	return searchResponse, nil
}

func sendJson(db *ElasticSearchDB, searchRequest interface{}) (*http.Response, error) {
	// Get client from fishie pool
	client, ok := db.clientPool.GetFishie().(http.Client)
	if !ok {
		if db.errLogger != nil {
			db.errLogger.Warn(1, "No free fishie")
		}
		fmt.Println("No free fishie")
		return &http.Response{}, errors.New("no free fishie")
	}

	defer db.clientPool.PutFishieBack(client)

	// Now shove that json request into a buffer
	var b bytes.Buffer
	encoder := json.NewEncoder(&b)

	err := encoder.Encode(searchRequest)
	if err != nil {
		if db.errLogger != nil {
			db.errLogger.Error(err, "Failed enccoding json into buffer")
		}
		return &http.Response{}, err
	}

	// Send request
	return client.Post(db.dbIp + UPDATE + SEARCH, JSON, &b)
}<commit_msg>Well fuck I think shit might be done<commit_after>package elasticService

import (
	"net/http"
	"fmt"
	"errors"
	"bytes"
	"encoding/json"
)

const (
	SEARCH = "/_search"
	JSON = "application/json"
)

type ShardInfo struct {
	Total int		`json:"total"`
	Success int		`json:"successful"`
	Failed int		`json:"failed"`
}

type DocInfo struct {
	Index string				`json:"_index"`
	Type string					`json:"_type"`
	Id string					`json:"_id"`
	Score float32				`json:"_score"`
	Source UpdateESDocType		`json:"_source"`
	Sort []string				`json:"sort"`
}

type HitsInfo struct {
	Total int			`json:"total"`
	MaxScore float32	`json:"max_score"`
	Hits []DocInfo		`json:"hits"`
}

type SearchResponse struct {
	Took int			`json:"took"`
	TimeOut bool		`json:"timed_out"`
	Shards ShardInfo	`json:"_shards"`
	Hits HitsInfo		`json:"hits"`
}

type MatchUID struct {
	Uid string `json:"_uid"`
}

type MatchDeviceID struct {
	ID int	`json:"device_id"`
}

type MustGeoMatch struct {
	MatchAll interface{} 	`json:"match_all"`
}

type TermMatch struct {
	Status int 		`json:"status"`
}

type MustGeoStatusMatch struct {
	Term TermMatch		`json:"term"`
}

type Bound struct {
	Lat float32		`json:"lat"`
	Lng float32		`json:"lon"`
}

type BoxBounds struct {
	UpperLeft	Bound	`json:"top_left"`
	BottomRight	Bound	`json:"bottom_right"`
}

type GeoBox struct {
	Bounds BoxBounds 	`json:"geo.location"`
}

type FilterGeo struct {
	BoundingBox GeoBox	`json:"geo_bounding_box"`
}

type BoolGeo struct {
	M MustGeoMatch		`json:"must"`
	F FilterGeo			`json:"filter"`
}

type BoolGeoStatus struct {
	M MustGeoStatusMatch	`json:"must"`
	F FilterGeo				`json:"filter"`
}

type QueryDeviceId struct {
	Match MatchDeviceID		`json:"match"`
}

type QueryGeoShit struct {
	B BoolGeo	`json:"bool"`
}

type QueryGeoStatusShit struct {
	B BoolGeoStatus `json:"bool"`
}

type SearchDeviceIDRequest struct {
	Query QueryDeviceId		`json:"query"`
	Sort []MatchUID		`json:"sort"`
}

type SearchGeoRequest struct {
	Query QueryGeoShit		`json:"query"`
	Sort []MatchUID			`json:"sort"`
}

type SearchGeoStatusRequest struct {
	Query QueryGeoStatusShit	`json:"query"`
	Sort []MatchUID				`json:"sort"`
}

func (db *ElasticSearchDB) SearchDeviceId(deviceId int) (SearchResponse, error) {
	var searchResponse SearchResponse

	// Construct search request
	searchRequest := SearchDeviceIDRequest{QueryDeviceId{MatchDeviceID{deviceId}}, nil}

	// Send request
	resp, err := sendJson(db, &searchRequest)
	defer resp.Body.Close()

	// If it failed give up
	if err != nil {
		if db.errLogger != nil {
			db.errLogger.Error(err, "Failed doing sumthing in search device id")
		}
		return searchResponse, err
	}

	// If it didn't fail get result
	decoder := json.NewDecoder(resp.Body)
	return searchResponse, decoder.Decode(&searchResponse)
}

func (db *ElasticSearchDB) SearchGeo(upperLeft, bottomRight Bound) (SearchResponse, error) {
	var searchResponse SearchResponse

	// Construct search request
	searchRequest := SearchGeoRequest{
		QueryGeoShit{
			BoolGeo{
				MustGeoMatch{
					nil,
				},
				FilterGeo{
					GeoBox{
						BoxBounds{
							upperLeft,
							bottomRight,
						},
					},
				},
			},
		},
		nil,
	}

	// Send request
	resp, err := sendJson(db, &searchRequest)
	defer resp.Body.Close()

	// If it failed give up
	if err != nil {
		if db.errLogger != nil {
			db.errLogger.Error(err, "Failed doing sumthing in search device id")
		}
		return searchResponse, err
	}

	// If it didn't fail get result
	decoder := json.NewDecoder(resp.Body)
	return searchResponse, decoder.Decode(&searchResponse)
}

func (db *ElasticSearchDB) SearchGeoStatus(upperLeft, bottomRight Bound, stat int) (SearchResponse,  error) {
	var searchResponse SearchResponse

	// Construct search request
	searchRequest := SearchGeoStatusRequest{
		QueryGeoStatusShit{
			BoolGeoStatus{
				MustGeoStatusMatch{
					TermMatch{
						stat,
					},
				},
				FilterGeo{
					GeoBox{
						BoxBounds{
							upperLeft,
							bottomRight,
						},
					},
				},
			},
		},
		nil,
	}

	// Send request
	resp, err := sendJson(db, &searchRequest)
	defer resp.Body.Close()

	// If it failed give up
	if err != nil {
		if db.errLogger != nil {
			db.errLogger.Error(err, "Failed doing sumthing in search device id")
		}
		return searchResponse, err
	}

	// If it didn't fail get result
	decoder := json.NewDecoder(resp.Body)
	return searchResponse, decoder.Decode(&searchResponse)
}

func sendJson(db *ElasticSearchDB, searchRequest interface{}) (*http.Response, error) {
	// Get client from fishie pool
	client, ok := db.clientPool.GetFishie().(http.Client)
	if !ok {
		if db.errLogger != nil {
			db.errLogger.Warn(1, "No free fishie")
		}
		fmt.Println("No free fishie")
		return &http.Response{}, errors.New("no free fishie")
	}

	defer db.clientPool.PutFishieBack(client)

	// Now shove that json request into a buffer
	var b bytes.Buffer
	encoder := json.NewEncoder(&b)

	err := encoder.Encode(searchRequest)
	if err != nil {
		if db.errLogger != nil {
			db.errLogger.Error(err, "Failed enccoding json into buffer")
		}
		return &http.Response{}, err
	}

	// Send request
	return client.Post(db.dbIp + UPDATE + SEARCH, JSON, &b)
}<|endoftext|>


<commit_before>package test_helpers

import (
	"bytes"
	"crypto/md5"
	"encoding/hex"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"syscall"
	"testing"

	"github.com/rfjakob/gocryptfs/ctlsock"
	"github.com/rfjakob/gocryptfs/internal/nametransform"
	"github.com/rfjakob/gocryptfs/internal/syscallcompat"
)

// TmpDir will be created inside this directory, set in init() to
// $TMPDIR/gocryptfs-test-parent .
var testParentDir = ""

// GocryptfsBinary is the assumed path to the gocryptfs build.
const GocryptfsBinary = "../../gocryptfs"

// UnmountScript is the fusermount/umount compatibility wrapper script
const UnmountScript = "../../tests/fuse-unmount.bash"

// X255 contains 255 uppercase "X". This can be used as a maximum-length filename.
var X255 string

// TmpDir is a unique temporary directory. "go test" runs package tests in parallel. We create a
// unique TmpDir in init() so the tests do not interfere.
var TmpDir string

// DefaultPlainDir is TmpDir + "/default-plain"
var DefaultPlainDir string

// DefaultCipherDir is TmpDir + "/default-cipher"
var DefaultCipherDir string

func init() {
	doInit()
}

func doInit() {
	X255 = string(bytes.Repeat([]byte("X"), 255))
	MountInfo = make(map[string]mountInfo)
	// Something like /tmp/gocryptfs-test-parent-1234
	testParentDir := fmt.Sprintf("%s/gocryptfs-test-parent-%d", os.TempDir(), os.Getuid())
	os.MkdirAll(testParentDir, 0755)
	if !isExt4(testParentDir) {
		fmt.Printf("test_helpers: warning: testParentDir %q does not reside on ext4, we will miss failures caused by ino reuse\n", testParentDir)
	}
	var err error
	TmpDir, err = ioutil.TempDir(testParentDir, "")
	if err != nil {
		panic(err)
	}
	// Open permissions for the allow_other tests
	os.Chmod(TmpDir, 0755)
	DefaultPlainDir = TmpDir + "/default-plain"
	DefaultCipherDir = TmpDir + "/default-cipher"
}

// ResetTmpDir deletes TmpDir, create new dir tree:
//
// TmpDir
// |-- DefaultPlainDir
// *-- DefaultCipherDir
//     *-- gocryptfs.diriv
func ResetTmpDir(createDirIV bool) {
	// Try to unmount and delete everything
	entries, err := ioutil.ReadDir(TmpDir)
	if err == nil {
		for _, e := range entries {
			d := filepath.Join(TmpDir, e.Name())
			err = os.Remove(d)
			if err != nil {
				pe := err.(*os.PathError)
				if pe.Err == syscall.EBUSY {
					if testing.Verbose() {
						fmt.Printf("Remove failed: %v. Maybe still mounted?\n", pe)
					}
					err = UnmountErr(d)
					if err != nil {
						panic(err)
					}
				} else if pe.Err != syscall.ENOTEMPTY {
					panic("Unhandled error: " + pe.Err.Error())
				}
				err = os.RemoveAll(d)
				if err != nil {
					panic(err)
				}
			}
		}
	}
	err = os.Mkdir(DefaultPlainDir, 0755)
	if err != nil {
		panic(err)
	}
	err = os.Mkdir(DefaultCipherDir, 0755)
	if err != nil {
		panic(err)
	}
	if createDirIV {
		// Open cipherdir (following symlinks)
		dirfd, err := syscall.Open(DefaultCipherDir, syscall.O_DIRECTORY|syscallcompat.O_PATH, 0)
		if err == nil {
			err = nametransform.WriteDirIVAt(dirfd)
			syscall.Close(dirfd)
		}
		if err != nil {
			panic(err)
		}
	}
}

// isExt4 finds out if `path` resides on an ext4 filesystem, as reported by
// statfs.
func isExt4(path string) bool {
	// From man statfs
	const EXT4_SUPER_MAGIC = 0xef53

	var fs syscall.Statfs_t
	err := syscall.Statfs(path, &fs)
	if err != nil {
		return false
	}
	if fs.Type == EXT4_SUPER_MAGIC {
		return true
	}
	return false
}

// InitFS creates a new empty cipherdir and calls
//
//     gocryptfs -q -init -extpass "echo test" -scryptn=10 $extraArgs $cipherdir
//
// It returns cipherdir without a trailing slash.
//
// If t is set, t.Fatal() is called on error, log.Panic() otherwise.
func InitFS(t *testing.T, extraArgs ...string) string {
	prefix := "x."
	if t != nil {
		prefix = t.Name() + "."
	}
	dir, err := ioutil.TempDir(TmpDir, prefix)
	if err != nil {
		if t != nil {
			t.Fatal(err)
		} else {
			log.Panic(err)
		}
	}
	args := []string{"-q", "-init", "-extpass", "echo test", "-scryptn=10"}
	args = append(args, extraArgs...)
	args = append(args, dir)

	cmd := exec.Command(GocryptfsBinary, args...)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr

	err = cmd.Run()
	if err != nil {
		if t != nil {
			t.Fatalf("InitFS with args %q failed: %v", args, err)
		} else {
			log.Panic(err)
		}
	}

	return dir
}

// Md5fn returns an md5 string for file "filename"
func Md5fn(filename string) string {
	buf, err := ioutil.ReadFile(filename)
	if err != nil {
		fmt.Printf("ReadFile: %v\n", err)
		return ""
	}
	return Md5hex(buf)
}

// Md5hex returns an md5 string for "buf"
func Md5hex(buf []byte) string {
	rawHash := md5.Sum(buf)
	hash := hex.EncodeToString(rawHash[:])
	return hash
}

// VerifySize checks that the file size equals "want". This checks:
// 1) Number of bytes returned when reading the whole file
// 2) Size reported by Stat()
// 3) Size reported by Fstat()
func VerifySize(t *testing.T, path string, want int) {
	// Read whole file
	buf, err := ioutil.ReadFile(path)
	if err != nil {
		t.Errorf("ReadFile failed: %v", err)
	} else if len(buf) != want {
		t.Errorf("wrong read size: got=%d want=%d", len(buf), want)
	}
	// Stat()
	var st syscall.Stat_t
	err = syscall.Stat(path, &st)
	if err != nil {
		t.Errorf("Stat failed: %v", err)
	} else if st.Size != int64(want) {
		t.Errorf("wrong stat file size, got=%d want=%d", st.Size, want)
	}
	// Fstat()
	fd, err := os.Open(path)
	if err != nil {
		t.Fatal(err)
	}
	defer fd.Close()
	var st2 syscall.Stat_t
	err = syscall.Fstat(int(fd.Fd()), &st2)
	if err != nil {
		t.Fatal(err)
	}
	if st2.Size != int64(want) {
		t.Errorf("wrong fstat file size, got=%d want=%d", st2.Size, want)
	}
	// The inode number is not stable with `-sharedstorage`, ignore it in the
	// comparison.
	st.Ino = 0
	st2.Ino = 0
	if st != st2 {
		t.Logf("Stat vs Fstat mismatch:\nst= %#v\nst2=%#v", st, st2)
	}
}

// TestMkdirRmdir creates and deletes a directory
func TestMkdirRmdir(t *testing.T, plainDir string) {
	dir := plainDir + "/dir1"
	err := os.Mkdir(dir, 0777)
	if err != nil {
		t.Error(err)
		return
	}
	err = syscall.Rmdir(dir)
	if err != nil {
		t.Error(err)
		return
	}
	// Create a directory and put a file in it
	// Trying to rmdir it should fail with ENOTEMPTY
	err = os.Mkdir(dir, 0777)
	if err != nil {
		t.Error(err)
		return
	}
	f, err := os.Create(dir + "/file")
	if err != nil {
		t.Error(err)
		return
	}
	f.Close()
	err = syscall.Rmdir(dir)
	errno := err.(syscall.Errno)
	if errno != syscall.ENOTEMPTY {
		t.Errorf("Should have gotten ENOTEMPTY, got %v", errno)
	}
	err = syscall.Unlink(dir + "/file")
	if err != nil {
		var st syscall.Stat_t
		syscall.Stat(dir, &st)
		t.Errorf("err=%v mode=%0o", err, st.Mode)
		return
	}
	err = syscall.Rmdir(dir)
	if err != nil {
		t.Error(err)
		return
	}
	// We should also be able to remove a directory we do not have permissions to
	// read or write
	err = os.Mkdir(dir, 0000)
	if err != nil {
		t.Error(err)
		return
	}
	err = syscall.Rmdir(dir)
	if err != nil {
		// Make sure the directory can cleaned up by the next test run
		os.Chmod(dir, 0700)
		t.Error(err)
		return
	}
}

// TestRename creates and renames a file
func TestRename(t *testing.T, plainDir string) {
	file1 := plainDir + "/rename1"
	file2 := plainDir + "/rename2"
	err := ioutil.WriteFile(file1, []byte("content"), 0777)
	if err != nil {
		t.Error(err)
		return
	}
	err = syscall.Rename(file1, file2)
	if err != nil {
		t.Errorf("Rename: %v", err)
		return
	}
	syscall.Unlink(file2)
}

// VerifyExistence checks in 3 ways that "path" exists:
// stat, open, readdir. Returns true if the path exists, false otherwise.
// Panics if the result is inconsistent.
func VerifyExistence(t *testing.T, path string) bool {
	t.Helper()
	// Check if file can be stat()ed
	stat := true
	fi, err := os.Stat(path)
	if err != nil {
		stat = false
	}
	// Check if file can be opened
	open := true
	fd, err := os.Open(path)
	if err != nil {
		open = false
	}
	fd.Close()
	// Check if file shows up in directory listing
	readdir := false
	dir := filepath.Dir(path)
	name := filepath.Base(path)
	d, err := os.Open(dir)
	if err != nil && open {
		t.Errorf("VerifyExistence: we can open the file but not the parent dir!? err=%v", err)
	} else if err == nil {
		defer d.Close()
		listing, err := d.Readdirnames(0)
		if stat && fi.IsDir() && err != nil {
			t.Errorf("VerifyExistence: It's a directory, but readdirnames failed: %v", err)
		}
		for _, entry := range listing {
			if entry == name {
				readdir = true
			}
		}
	}
	// If the result is consistent, return it.
	if stat == open && open == readdir {
		return stat
	}
	t.Errorf("VerifyExistence: inconsistent result on %q: stat=%v open=%v readdir=%v, path=%q", name, stat, open, readdir, path)
	return false
}

// Du returns the disk usage of the file "fd" points to, in bytes.
// Same as "du --block-size=1".
func Du(t *testing.T, fd int) (nBytes int64) {
	var st syscall.Stat_t
	err := syscall.Fstat(fd, &st)
	if err != nil {
		t.Fatal(err)
	}
	// st.Blocks = number of 512-byte blocks
	return st.Blocks * 512
}

// QueryCtlSock sends a request to the control socket at "socketPath" and
// returns the response.
func QueryCtlSock(t *testing.T, socketPath string, req ctlsock.RequestStruct) ctlsock.ResponseStruct {
	c, err := ctlsock.New(socketPath)
	if err != nil {
		// Connecting to the socket failed already. This is fatal.
		t.Fatal(err)
	}
	defer c.Close()
	resp, err := c.Query(&req)
	if err != nil {
		// If we got a response, try to extract it. This is not fatal here
		// as the tests may expect error responses.
		if resp2, ok := err.(*ctlsock.ResponseStruct); ok {
			return *resp2
		}
		// Another error means that we did not even get a response. This is fatal.
		t.Fatal(err)
	}
	return *resp
}

// ExtractCmdExitCode extracts the exit code from an error value that was
// returned from exec / cmd.Run()
func ExtractCmdExitCode(err error) int {
	if err == nil {
		return 0
	}
	// OMG this is convoluted
	if err2, ok := err.(*exec.ExitError); ok {
		return err2.Sys().(syscall.WaitStatus).ExitStatus()
	}
	if err2, ok := err.(*os.PathError); ok {
		return int(err2.Err.(syscall.Errno))
	}
	log.Panicf("could not decode error %#v", err)
	return 0
}
<commit_msg>test_helpers: actually use global testParentDir variable<commit_after>package test_helpers

import (
	"bytes"
	"crypto/md5"
	"encoding/hex"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"syscall"
	"testing"

	"github.com/rfjakob/gocryptfs/ctlsock"
	"github.com/rfjakob/gocryptfs/internal/nametransform"
	"github.com/rfjakob/gocryptfs/internal/syscallcompat"
)

// TmpDir will be created inside this directory, set in init() to
// $TMPDIR/gocryptfs-test-parent .
var testParentDir = ""

// GocryptfsBinary is the assumed path to the gocryptfs build.
const GocryptfsBinary = "../../gocryptfs"

// UnmountScript is the fusermount/umount compatibility wrapper script
const UnmountScript = "../../tests/fuse-unmount.bash"

// X255 contains 255 uppercase "X". This can be used as a maximum-length filename.
var X255 string

// TmpDir is a unique temporary directory. "go test" runs package tests in parallel. We create a
// unique TmpDir in init() so the tests do not interfere.
var TmpDir string

// DefaultPlainDir is TmpDir + "/default-plain"
var DefaultPlainDir string

// DefaultCipherDir is TmpDir + "/default-cipher"
var DefaultCipherDir string

func init() {
	doInit()
}

func doInit() {
	X255 = string(bytes.Repeat([]byte("X"), 255))
	MountInfo = make(map[string]mountInfo)
	// Something like /tmp/gocryptfs-test-parent-1234
	testParentDir = fmt.Sprintf("%s/gocryptfs-test-parent-%d", os.TempDir(), os.Getuid())
	os.MkdirAll(testParentDir, 0755)
	if !isExt4(testParentDir) {
		fmt.Printf("test_helpers: warning: testParentDir %q does not reside on ext4, we will miss failures caused by ino reuse\n", testParentDir)
	}
	var err error
	TmpDir, err = ioutil.TempDir(testParentDir, "")
	if err != nil {
		panic(err)
	}
	// Open permissions for the allow_other tests
	os.Chmod(TmpDir, 0755)
	DefaultPlainDir = TmpDir + "/default-plain"
	DefaultCipherDir = TmpDir + "/default-cipher"
}

// ResetTmpDir deletes TmpDir, create new dir tree:
//
// TmpDir
// |-- DefaultPlainDir
// *-- DefaultCipherDir
//     *-- gocryptfs.diriv
func ResetTmpDir(createDirIV bool) {
	// Try to unmount and delete everything
	entries, err := ioutil.ReadDir(TmpDir)
	if err == nil {
		for _, e := range entries {
			d := filepath.Join(TmpDir, e.Name())
			err = os.Remove(d)
			if err != nil {
				pe := err.(*os.PathError)
				if pe.Err == syscall.EBUSY {
					if testing.Verbose() {
						fmt.Printf("Remove failed: %v. Maybe still mounted?\n", pe)
					}
					err = UnmountErr(d)
					if err != nil {
						panic(err)
					}
				} else if pe.Err != syscall.ENOTEMPTY {
					panic("Unhandled error: " + pe.Err.Error())
				}
				err = os.RemoveAll(d)
				if err != nil {
					panic(err)
				}
			}
		}
	}
	err = os.Mkdir(DefaultPlainDir, 0755)
	if err != nil {
		panic(err)
	}
	err = os.Mkdir(DefaultCipherDir, 0755)
	if err != nil {
		panic(err)
	}
	if createDirIV {
		// Open cipherdir (following symlinks)
		dirfd, err := syscall.Open(DefaultCipherDir, syscall.O_DIRECTORY|syscallcompat.O_PATH, 0)
		if err == nil {
			err = nametransform.WriteDirIVAt(dirfd)
			syscall.Close(dirfd)
		}
		if err != nil {
			panic(err)
		}
	}
}

// isExt4 finds out if `path` resides on an ext4 filesystem, as reported by
// statfs.
func isExt4(path string) bool {
	// From man statfs
	const EXT4_SUPER_MAGIC = 0xef53

	var fs syscall.Statfs_t
	err := syscall.Statfs(path, &fs)
	if err != nil {
		return false
	}
	if fs.Type == EXT4_SUPER_MAGIC {
		return true
	}
	return false
}

// InitFS creates a new empty cipherdir and calls
//
//     gocryptfs -q -init -extpass "echo test" -scryptn=10 $extraArgs $cipherdir
//
// It returns cipherdir without a trailing slash.
//
// If t is set, t.Fatal() is called on error, log.Panic() otherwise.
func InitFS(t *testing.T, extraArgs ...string) string {
	prefix := "x."
	if t != nil {
		prefix = t.Name() + "."
	}
	dir, err := ioutil.TempDir(TmpDir, prefix)
	if err != nil {
		if t != nil {
			t.Fatal(err)
		} else {
			log.Panic(err)
		}
	}
	args := []string{"-q", "-init", "-extpass", "echo test", "-scryptn=10"}
	args = append(args, extraArgs...)
	args = append(args, dir)

	cmd := exec.Command(GocryptfsBinary, args...)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr

	err = cmd.Run()
	if err != nil {
		if t != nil {
			t.Fatalf("InitFS with args %q failed: %v", args, err)
		} else {
			log.Panic(err)
		}
	}

	return dir
}

// Md5fn returns an md5 string for file "filename"
func Md5fn(filename string) string {
	buf, err := ioutil.ReadFile(filename)
	if err != nil {
		fmt.Printf("ReadFile: %v\n", err)
		return ""
	}
	return Md5hex(buf)
}

// Md5hex returns an md5 string for "buf"
func Md5hex(buf []byte) string {
	rawHash := md5.Sum(buf)
	hash := hex.EncodeToString(rawHash[:])
	return hash
}

// VerifySize checks that the file size equals "want". This checks:
// 1) Number of bytes returned when reading the whole file
// 2) Size reported by Stat()
// 3) Size reported by Fstat()
func VerifySize(t *testing.T, path string, want int) {
	// Read whole file
	buf, err := ioutil.ReadFile(path)
	if err != nil {
		t.Errorf("ReadFile failed: %v", err)
	} else if len(buf) != want {
		t.Errorf("wrong read size: got=%d want=%d", len(buf), want)
	}
	// Stat()
	var st syscall.Stat_t
	err = syscall.Stat(path, &st)
	if err != nil {
		t.Errorf("Stat failed: %v", err)
	} else if st.Size != int64(want) {
		t.Errorf("wrong stat file size, got=%d want=%d", st.Size, want)
	}
	// Fstat()
	fd, err := os.Open(path)
	if err != nil {
		t.Fatal(err)
	}
	defer fd.Close()
	var st2 syscall.Stat_t
	err = syscall.Fstat(int(fd.Fd()), &st2)
	if err != nil {
		t.Fatal(err)
	}
	if st2.Size != int64(want) {
		t.Errorf("wrong fstat file size, got=%d want=%d", st2.Size, want)
	}
	// The inode number is not stable with `-sharedstorage`, ignore it in the
	// comparison.
	st.Ino = 0
	st2.Ino = 0
	if st != st2 {
		t.Logf("Stat vs Fstat mismatch:\nst= %#v\nst2=%#v", st, st2)
	}
}

// TestMkdirRmdir creates and deletes a directory
func TestMkdirRmdir(t *testing.T, plainDir string) {
	dir := plainDir + "/dir1"
	err := os.Mkdir(dir, 0777)
	if err != nil {
		t.Error(err)
		return
	}
	err = syscall.Rmdir(dir)
	if err != nil {
		t.Error(err)
		return
	}
	// Create a directory and put a file in it
	// Trying to rmdir it should fail with ENOTEMPTY
	err = os.Mkdir(dir, 0777)
	if err != nil {
		t.Error(err)
		return
	}
	f, err := os.Create(dir + "/file")
	if err != nil {
		t.Error(err)
		return
	}
	f.Close()
	err = syscall.Rmdir(dir)
	errno := err.(syscall.Errno)
	if errno != syscall.ENOTEMPTY {
		t.Errorf("Should have gotten ENOTEMPTY, got %v", errno)
	}
	err = syscall.Unlink(dir + "/file")
	if err != nil {
		var st syscall.Stat_t
		syscall.Stat(dir, &st)
		t.Errorf("err=%v mode=%0o", err, st.Mode)
		return
	}
	err = syscall.Rmdir(dir)
	if err != nil {
		t.Error(err)
		return
	}
	// We should also be able to remove a directory we do not have permissions to
	// read or write
	err = os.Mkdir(dir, 0000)
	if err != nil {
		t.Error(err)
		return
	}
	err = syscall.Rmdir(dir)
	if err != nil {
		// Make sure the directory can cleaned up by the next test run
		os.Chmod(dir, 0700)
		t.Error(err)
		return
	}
}

// TestRename creates and renames a file
func TestRename(t *testing.T, plainDir string) {
	file1 := plainDir + "/rename1"
	file2 := plainDir + "/rename2"
	err := ioutil.WriteFile(file1, []byte("content"), 0777)
	if err != nil {
		t.Error(err)
		return
	}
	err = syscall.Rename(file1, file2)
	if err != nil {
		t.Errorf("Rename: %v", err)
		return
	}
	syscall.Unlink(file2)
}

// VerifyExistence checks in 3 ways that "path" exists:
// stat, open, readdir. Returns true if the path exists, false otherwise.
// Panics if the result is inconsistent.
func VerifyExistence(t *testing.T, path string) bool {
	t.Helper()
	// Check if file can be stat()ed
	stat := true
	fi, err := os.Stat(path)
	if err != nil {
		stat = false
	}
	// Check if file can be opened
	open := true
	fd, err := os.Open(path)
	if err != nil {
		open = false
	}
	fd.Close()
	// Check if file shows up in directory listing
	readdir := false
	dir := filepath.Dir(path)
	name := filepath.Base(path)
	d, err := os.Open(dir)
	if err != nil && open {
		t.Errorf("VerifyExistence: we can open the file but not the parent dir!? err=%v", err)
	} else if err == nil {
		defer d.Close()
		listing, err := d.Readdirnames(0)
		if stat && fi.IsDir() && err != nil {
			t.Errorf("VerifyExistence: It's a directory, but readdirnames failed: %v", err)
		}
		for _, entry := range listing {
			if entry == name {
				readdir = true
			}
		}
	}
	// If the result is consistent, return it.
	if stat == open && open == readdir {
		return stat
	}
	t.Errorf("VerifyExistence: inconsistent result on %q: stat=%v open=%v readdir=%v, path=%q", name, stat, open, readdir, path)
	return false
}

// Du returns the disk usage of the file "fd" points to, in bytes.
// Same as "du --block-size=1".
func Du(t *testing.T, fd int) (nBytes int64) {
	var st syscall.Stat_t
	err := syscall.Fstat(fd, &st)
	if err != nil {
		t.Fatal(err)
	}
	// st.Blocks = number of 512-byte blocks
	return st.Blocks * 512
}

// QueryCtlSock sends a request to the control socket at "socketPath" and
// returns the response.
func QueryCtlSock(t *testing.T, socketPath string, req ctlsock.RequestStruct) ctlsock.ResponseStruct {
	c, err := ctlsock.New(socketPath)
	if err != nil {
		// Connecting to the socket failed already. This is fatal.
		t.Fatal(err)
	}
	defer c.Close()
	resp, err := c.Query(&req)
	if err != nil {
		// If we got a response, try to extract it. This is not fatal here
		// as the tests may expect error responses.
		if resp2, ok := err.(*ctlsock.ResponseStruct); ok {
			return *resp2
		}
		// Another error means that we did not even get a response. This is fatal.
		t.Fatal(err)
	}
	return *resp
}

// ExtractCmdExitCode extracts the exit code from an error value that was
// returned from exec / cmd.Run()
func ExtractCmdExitCode(err error) int {
	if err == nil {
		return 0
	}
	// OMG this is convoluted
	if err2, ok := err.(*exec.ExitError); ok {
		return err2.Sys().(syscall.WaitStatus).ExitStatus()
	}
	if err2, ok := err.(*os.PathError); ok {
		return int(err2.Err.(syscall.Errno))
	}
	log.Panicf("could not decode error %#v", err)
	return 0
}
<|endoftext|>