<commit_before>package securitygroup

import (
	"github.com/cloudfoundry/cli/cf/api/security_groups"
	"github.com/cloudfoundry/cli/cf/api/security_groups/defaults/running"
	"github.com/cloudfoundry/cli/cf/command"
	"github.com/cloudfoundry/cli/cf/command_metadata"
	"github.com/cloudfoundry/cli/cf/configuration"
	"github.com/cloudfoundry/cli/cf/errors"
	"github.com/cloudfoundry/cli/cf/requirements"
	"github.com/cloudfoundry/cli/cf/terminal"
	"github.com/codegangsta/cli"
)

type removeFromRunningGroup struct {
	ui                terminal.UI
	configRepo        configuration.Reader
	securityGroupRepo security_groups.SecurityGroupRepo
	runningGroupRepo  running.RunningSecurityGroupsRepo
}

func NewRemoveFromRunningGroup(ui terminal.UI, configRepo configuration.Reader, securityGroupRepo security_groups.SecurityGroupRepo, runningGroupRepo running.RunningSecurityGroupsRepo) command.Command {
	return &removeFromRunningGroup{
		ui:                ui,
		configRepo:        configRepo,
		securityGroupRepo: securityGroupRepo,
		runningGroupRepo:  runningGroupRepo,
	}
}

func (cmd *removeFromRunningGroup) Metadata() command_metadata.CommandMetadata {
	return command_metadata.CommandMetadata{
		Name:        "remove-running-security-group",
		Description: "Remove a security group from the set of security groups for running applications",
		Usage:       "CF_NAME remove-Running-security-group NAME",
	}
}

func (cmd *removeFromRunningGroup) GetRequirements(requirementsFactory requirements.Factory, context *cli.Context) ([]requirements.Requirement, error) {
	if len(context.Args()) != 1 {
		cmd.ui.FailWithUsage(context)
	}

	return []requirements.Requirement{
		requirementsFactory.NewLoginRequirement(),
	}, nil
}

func (cmd *removeFromRunningGroup) Run(context *cli.Context) {
	name := context.Args()[0]

	securityGroup, err := cmd.securityGroupRepo.Read(name)
	switch (err).(type) {
	case nil:
	case *errors.ModelNotFoundError:
		cmd.ui.Ok()
		cmd.ui.Warn("Security group %s %s",
			terminal.EntityNameColor(name),
			terminal.WarningColor("does not exist."))
		return
	default:
		cmd.ui.Failed(err.Error())
	}

	cmd.ui.Say("Removing security group %s from defaults for running as %s",
		terminal.EntityNameColor(securityGroup.Name),
		terminal.EntityNameColor(cmd.configRepo.Username()))
	err = cmd.runningGroupRepo.RemoveFromRunningSet(securityGroup.Guid)
	if err != nil {
		cmd.ui.Failed(err.Error())
	}

	cmd.ui.Ok()
}
<commit_msg>Fixin' this usage.<commit_after>package securitygroup

import (
	"github.com/cloudfoundry/cli/cf/api/security_groups"
	"github.com/cloudfoundry/cli/cf/api/security_groups/defaults/running"
	"github.com/cloudfoundry/cli/cf/command"
	"github.com/cloudfoundry/cli/cf/command_metadata"
	"github.com/cloudfoundry/cli/cf/configuration"
	"github.com/cloudfoundry/cli/cf/errors"
	"github.com/cloudfoundry/cli/cf/requirements"
	"github.com/cloudfoundry/cli/cf/terminal"
	"github.com/codegangsta/cli"
)

type removeFromRunningGroup struct {
	ui                terminal.UI
	configRepo        configuration.Reader
	securityGroupRepo security_groups.SecurityGroupRepo
	runningGroupRepo  running.RunningSecurityGroupsRepo
}

func NewRemoveFromRunningGroup(ui terminal.UI, configRepo configuration.Reader, securityGroupRepo security_groups.SecurityGroupRepo, runningGroupRepo running.RunningSecurityGroupsRepo) command.Command {
	return &removeFromRunningGroup{
		ui:                ui,
		configRepo:        configRepo,
		securityGroupRepo: securityGroupRepo,
		runningGroupRepo:  runningGroupRepo,
	}
}

func (cmd *removeFromRunningGroup) Metadata() command_metadata.CommandMetadata {
	return command_metadata.CommandMetadata{
		Name:        "remove-running-security-group",
		Description: "Remove a security group from the set of security groups for running applications",
		Usage:       "CF_NAME remove-running-security-group NAME",
	}
}

func (cmd *removeFromRunningGroup) GetRequirements(requirementsFactory requirements.Factory, context *cli.Context) ([]requirements.Requirement, error) {
	if len(context.Args()) != 1 {
		cmd.ui.FailWithUsage(context)
	}

	return []requirements.Requirement{
		requirementsFactory.NewLoginRequirement(),
	}, nil
}

func (cmd *removeFromRunningGroup) Run(context *cli.Context) {
	name := context.Args()[0]

	securityGroup, err := cmd.securityGroupRepo.Read(name)
	switch (err).(type) {
	case nil:
	case *errors.ModelNotFoundError:
		cmd.ui.Ok()
		cmd.ui.Warn("Security group %s %s",
			terminal.EntityNameColor(name),
			terminal.WarningColor("does not exist."))
		return
	default:
		cmd.ui.Failed(err.Error())
	}

	cmd.ui.Say("Removing security group %s from defaults for running as %s",
		terminal.EntityNameColor(securityGroup.Name),
		terminal.EntityNameColor(cmd.configRepo.Username()))
	err = cmd.runningGroupRepo.RemoveFromRunningSet(securityGroup.Guid)
	if err != nil {
		cmd.ui.Failed(err.Error())
	}

	cmd.ui.Ok()
}
<|endoftext|>


<commit_before>// Package websocketproxy is a reverse proxy for WebSocket connections.
package websocketproxy

import (
	"io"
	"log"
	"net"
	"net/http"
	"net/url"
	"strings"

	"github.com/gorilla/websocket"
)

var (
	// DefaultUpgrader specifies the paramaters for upgrading an HTTP
	// connection to a WebSocket connection.
	DefaultUpgrader = &websocket.Upgrader{
		ReadBufferSize:  1024,
		WriteBufferSize: 1024,
	}

	// DefaultDialer is a dialer with all fields set to the default zero values.
	DefaultDialer = websocket.DefaultDialer
)

// WebsocketProxy is an HTTP Handler that takes an incoming websocket
// connection and proxies it to another server.
type WebsocketProxy struct {
	// Backend returns the backend URL which the proxy uses to reverse proxy
	// the incoming websocket connection. Request is the initial incoming and
	// unmodified request.
	Backend func(*http.Request) *url.URL

	// Upgrader specifies the paramaters for upgrading an HTTP connection to a
	// WebSocket connection. If nil, DefaultUpgrader is used.
	Upgrader *websocket.Upgrader

	//  Dialer contains options for connecting to WebSocket server. If nil,
	//  DefaultDialer is used.
	Dialer *websocket.Dialer
}

// ProxyHandler returns a new http.Handler interface that reverse proxies the
// request to the given target.
func ProxyHandler(target *url.URL) http.Handler {
	return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
		NewProxy(target).ServeHTTP(rw, req)
	})
}

// NewProxy returns a new Websocket reverse proxy that rewrites the
// URL's to the scheme, host and base path provider in target.
func NewProxy(target *url.URL) *WebsocketProxy {
	backend := func(r *http.Request) *url.URL { return target }
	return &WebsocketProxy{Backend: backend}
}

// ServeHTTP implements the http.Handler that proxies WebSocket connections.
func (w *WebsocketProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
	backendURL := w.Backend(req)

	dialer := w.Dialer
	if w.Dialer == nil {
		dialer = DefaultDialer
	}

	// Pass headers from the incoming request to the dialer to forward them to
	// the final destinations.
	h := http.Header{}
	h.Add("Origin", req.Header.Get("Origin"))
	protocols := req.Header["Sec-WebSocket-Protocol"]
	for _, prot := range protocols {
		h.Add("Sec-WebSocket-Protocol", prot)
	}
	cookies := req.Header["Cookie"]
	for _, cookie := range cookies {
		h.Add("Cookie", cookie)
	}

	// Pass X-Forwarded-For headers too, code below is a part of
	// httputil.ReverseProxy. See http://en.wikipedia.org/wiki/X-Forwarded-For
	// for more information
	if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
		// If we aren't the first proxy retain prior
		// X-Forwarded-For information as a comma+space
		// separated list and fold multiple headers into one.
		if prior, ok := req.Header["X-Forwarded-For"]; ok {
			clientIP = strings.Join(prior, ", ") + ", " + clientIP
		}
		h.Set("X-Forwarded-For", clientIP)
	}

	// Set the originating protol of the incoming HTTP request. The SSL might
	// be terminated on our site and because we doing proxy adding this would
	// be helpful for applications on the backedn.
	h.Set("X-Forwarded-Proto", "http")
	if req.TLS != nil {
		h.Set("X-Forwarded-Proto", "https")
	}

	// Connect to the backend url, also pass the headers we prepared above.
	connBackend, resp, err := dialer.Dial(backendURL.String(), h)
	if err != nil {
		log.Printf("websocketproxy: couldn't dial to remote backend url %s\n", err)
		return
	}
	defer connBackend.Close()

	upgrader := w.Upgrader
	if w.Upgrader == nil {
		upgrader = DefaultUpgrader
	}

	// Only pass those headers to the upgrader.
	upgradeHeader := http.Header{}
	upgradeHeader.Add("Sec-WebSocket-Protocol", resp.Header.Get("Sec-WebSocket-Protocol"))
	upgradeHeader.Add("Set-Cookie", resp.Header.Get("Set-Cookie"))

	// Now upgrade the existing incoming request to a WebSocket connection.
	// Also pass the header that we gathered from the Dial handshake.
	connPub, err := upgrader.Upgrade(rw, req, upgradeHeader)
	if err != nil {
		log.Printf("websocketproxy: couldn't upgrade %s\n", err)
		return
	}
	defer connPub.Close()

	errc := make(chan error, 2)
	cp := func(dst io.Writer, src io.Reader) {
		_, err := io.Copy(dst, src)
		errc <- err
	}

	// Start our proxy now, everything is ready...
	go cp(connBackend.UnderlyingConn(), connPub.UnderlyingConn())
	go cp(connPub.UnderlyingConn(), connBackend.UnderlyingConn())
	<-errc
}
<commit_msg>websocketproxy: add some note<commit_after>// Package websocketproxy is a reverse proxy for WebSocket connections.
package websocketproxy

import (
	"io"
	"log"
	"net"
	"net/http"
	"net/url"
	"strings"

	"github.com/gorilla/websocket"
)

var (
	// DefaultUpgrader specifies the paramaters for upgrading an HTTP
	// connection to a WebSocket connection.
	DefaultUpgrader = &websocket.Upgrader{
		ReadBufferSize:  1024,
		WriteBufferSize: 1024,
	}

	// DefaultDialer is a dialer with all fields set to the default zero values.
	DefaultDialer = websocket.DefaultDialer
)

// WebsocketProxy is an HTTP Handler that takes an incoming websocket
// connection and proxies it to another server.
type WebsocketProxy struct {
	// Backend returns the backend URL which the proxy uses to reverse proxy
	// the incoming websocket connection. Request is the initial incoming and
	// unmodified request.
	Backend func(*http.Request) *url.URL

	// Upgrader specifies the paramaters for upgrading an HTTP connection to a
	// WebSocket connection. If nil, DefaultUpgrader is used.
	Upgrader *websocket.Upgrader

	//  Dialer contains options for connecting to WebSocket server. If nil,
	//  DefaultDialer is used.
	Dialer *websocket.Dialer
}

// ProxyHandler returns a new http.Handler interface that reverse proxies the
// request to the given target.
func ProxyHandler(target *url.URL) http.Handler {
	return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
		NewProxy(target).ServeHTTP(rw, req)
	})
}

// NewProxy returns a new Websocket reverse proxy that rewrites the
// URL's to the scheme, host and base path provider in target.
func NewProxy(target *url.URL) *WebsocketProxy {
	backend := func(r *http.Request) *url.URL { return target }
	return &WebsocketProxy{Backend: backend}
}

// ServeHTTP implements the http.Handler that proxies WebSocket connections.
func (w *WebsocketProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
	backendURL := w.Backend(req)

	dialer := w.Dialer
	if w.Dialer == nil {
		dialer = DefaultDialer
	}

	// Pass headers from the incoming request to the dialer to forward them to
	// the final destinations.
	h := http.Header{}
	h.Add("Origin", req.Header.Get("Origin"))
	protocols := req.Header["Sec-WebSocket-Protocol"]
	for _, prot := range protocols {
		h.Add("Sec-WebSocket-Protocol", prot)
	}
	cookies := req.Header["Cookie"]
	for _, cookie := range cookies {
		h.Add("Cookie", cookie)
	}

	// Pass X-Forwarded-For headers too, code below is a part of
	// httputil.ReverseProxy. See http://en.wikipedia.org/wiki/X-Forwarded-For
	// for more information
	if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
		// If we aren't the first proxy retain prior
		// X-Forwarded-For information as a comma+space
		// separated list and fold multiple headers into one.
		if prior, ok := req.Header["X-Forwarded-For"]; ok {
			clientIP = strings.Join(prior, ", ") + ", " + clientIP
		}
		h.Set("X-Forwarded-For", clientIP)
	}

	// Set the originating protol of the incoming HTTP request. The SSL might
	// be terminated on our site and because we doing proxy adding this would
	// be helpful for applications on the backedn.
	h.Set("X-Forwarded-Proto", "http")
	if req.TLS != nil {
		h.Set("X-Forwarded-Proto", "https")
	}

	// Connect to the backend url, also pass the headers we prepared above.
	// TODO: support multiplexing on the same backend connection instead of
	// opening a new TCP connection time for each request.
	connBackend, resp, err := dialer.Dial(backendURL.String(), h)
	if err != nil {
		log.Printf("websocketproxy: couldn't dial to remote backend url %s\n", err)
		return
	}
	defer connBackend.Close()

	upgrader := w.Upgrader
	if w.Upgrader == nil {
		upgrader = DefaultUpgrader
	}

	// Only pass those headers to the upgrader.
	upgradeHeader := http.Header{}
	upgradeHeader.Add("Sec-WebSocket-Protocol", resp.Header.Get("Sec-WebSocket-Protocol"))
	upgradeHeader.Add("Set-Cookie", resp.Header.Get("Set-Cookie"))

	// Now upgrade the existing incoming request to a WebSocket connection.
	// Also pass the header that we gathered from the Dial handshake.
	connPub, err := upgrader.Upgrade(rw, req, upgradeHeader)
	if err != nil {
		log.Printf("websocketproxy: couldn't upgrade %s\n", err)
		return
	}
	defer connPub.Close()

	errc := make(chan error, 2)
	cp := func(dst io.Writer, src io.Reader) {
		_, err := io.Copy(dst, src)
		errc <- err
	}

	// Start our proxy now, everything is ready...
	go cp(connBackend.UnderlyingConn(), connPub.UnderlyingConn())
	go cp(connPub.UnderlyingConn(), connBackend.UnderlyingConn())
	<-errc
}
<|endoftext|>


<commit_before>/* This file will contain functionalities to host the main service
in which the client will make requests to. Use net/http to create
API endpoints to access the following functionalities:
	- UpdateBloomFilter
	- RepopulateBloomFilter
	- GetArrayOfUnsubscribedEmails
	- etc. (Steph
		add more functionalities
		here and implement this
		after you've completed your
		current tasks)
*/
package main

import (
	"Inf191BloomFilter/bloomManager"
	"encoding/json"
	"fmt"
	"net/http"
)

//struct that takes an int and a list of emails
type Payload struct {
	UserId int
	Emails []string
}

type Result struct {
	Trues []string
}

//handleUpdate will update the respective bloomFilter
func handleUpdate(r http.ResponseWriter, req *http.Request) {
	//	Use UpdateBloomFilter() here
}

func checkErr(err error) {
	if err != nil {
		panic(err.Error())
	}
}

func handleFilterUnsubscribed(w http.ResponseWriter, r *http.Request) {
	fmt.Printf("Received request: %v %v %v\n", r.Method, r.URL, r.Proto)
	var buff []byte
	var payload Payload

	err := json.NewDecoder(r.Body).Decode(&buff)
	if err != nil {
		http.Error(w, err.Error(), 400)
		return
	}
	err = json.Unmarshal(buff, &payload)
	checkErr(err)

	bf := bloomManager.New()
	emails := bf.GetArrayOfUnsubscribedEmails(payload.Emails)
	filteredEmails := Result{emails}

	js, err := json.Marshal(filteredEmails)
	checkErr(err)

	w.Write(js)
}

func main() {
	http.HandleFunc("/filterUnsubscribed", handleFilterUnsubscribed)
	http.ListenAndServe(":9090", nil)
}
<commit_msg>Resolved merge conflict<commit_after>/* This file will contain functionalities to host the main service
in which the client will make requests to. Use net/http to create
API endpoints to access the following functionalities:
	- UpdateBloomFilter
	- RepopulateBloomFilter
	- GetArrayOfUnsubscribedEmails
	- etc. (Steph
		add more functionalities
		here and implement this
		after you've completed your
		current tasks)
*/
package main

import (
	"Inf191BloomFilter/bloomManager"
	"encoding/json"
	"fmt"
	"net/http"
)
type Payload struct {
	UserId int
	Emails []string
}

//Global variable
//The bloom filter for this server
var bf = bloomManager.New()

//handleUpdate will update the respective bloomFilter
func handleUpdate(r http.ResponseWriter, req *http.Request) {
	bf.UpdateBloomFilter()

}

//checkErr checks fro errors in the decoded text and encoded text...
func checkErr(err error) {
	if err != nil {
		panic(err.Error())
	}
}

func handleFilterUnsubscribed(w http.ResponseWriter, r *http.Request) {
	fmt.Printf("Received request: %v %v %v\n", r.Method, r.URL, r.Proto)
	var buff []byte
	var payload Payload
	//Result struct made to carry the result of unsuscribed emails
	type Result struct {
	Trues []string
	}

	err := json.NewDecoder(r.Body).Decode(&buff)
	if err != nil {
		http.Error(w, err.Error(), 400)
		return
	}
	err = json.Unmarshal(buff, &payload)
	checkErr(err)

	bf := bloomManager.New()
	emails := bf.GetArrayOfUnsubscribedEmails(payload.Emails)
	filteredEmails := Result{emails}

	js, err := json.Marshal(filteredEmails)
	checkErr(err)

	w.Write(js)
}

func main() {
	http.HandleFunc("/filterUnsubscribed", handleFilterUnsubscribed)
	http.HandleFunc("/update", handleUpdate)

	http.ListenAndServe(":9090", nil)
}
<|endoftext|>


<commit_before>// +build go1.7

package errors

import (
	"fmt"
	"testing"

	stderrors "errors"
)

func noErrors(at, depth int) error {
	if at >= depth {
		return stderrors.New("no error")
	}
	return noErrors(at+1, depth)
}

func yesErrors(at, depth int) error {
	if at >= depth {
		return New("ye error")
	}
	return yesErrors(at+1, depth)
}

func BenchmarkErrors(b *testing.B) {
	var toperr error
	type run struct {
		stack int
		std   bool
	}
	runs := []run{
		{10, false},
		{10, true},
		{100, false},
		{100, true},
		{1000, false},
		{1000, true},
	}
	for _, r := range runs {
		part := "pkg/errors"
		if r.std {
			part = "errors"
		}
		name := fmt.Sprintf("%s-stack-%d", part, r.stack)
		b.Run(name, func(b *testing.B) {
			var err error
			f := yesErrors
			if r.std {
				f = noErrors
			}
			b.ReportAllocs()
			for i := 0; i < b.N; i++ {
				err = f(0, r.stack)
			}
			b.StopTimer()
			toperr = err
		})
	}
}
<commit_msg>Move benchmark assigned err to global exported variable (#106)<commit_after>// +build go1.7

package errors

import (
	"fmt"
	"testing"

	stderrors "errors"
)

func noErrors(at, depth int) error {
	if at >= depth {
		return stderrors.New("no error")
	}
	return noErrors(at+1, depth)
}

func yesErrors(at, depth int) error {
	if at >= depth {
		return New("ye error")
	}
	return yesErrors(at+1, depth)
}

// GlobalE is an exported global to store the result of benchmark results,
// preventing the compiler from optimising the benchmark functions away.
var GlobalE error

func BenchmarkErrors(b *testing.B) {
	type run struct {
		stack int
		std   bool
	}
	runs := []run{
		{10, false},
		{10, true},
		{100, false},
		{100, true},
		{1000, false},
		{1000, true},
	}
	for _, r := range runs {
		part := "pkg/errors"
		if r.std {
			part = "errors"
		}
		name := fmt.Sprintf("%s-stack-%d", part, r.stack)
		b.Run(name, func(b *testing.B) {
			var err error
			f := yesErrors
			if r.std {
				f = noErrors
			}
			b.ReportAllocs()
			for i := 0; i < b.N; i++ {
				err = f(0, r.stack)
			}
			b.StopTimer()
			GlobalE = err
		})
	}
}
<|endoftext|>


<commit_before>package controllers

import (
	"encoding/json"
	"net/http"

	"github.com/learnin/go-multilog"
	"github.com/zenazn/goji/web"

	"github.com/learnin/batch-rest-controller/helpers"
)

type JobsController struct {
	DS     *helpers.DataSource
	Logger *multilog.MultiLogger
}

type ReqForm struct {
	Async         bool
	RequireResult bool
	Command       string
	Args          string
}

func (controller *JobsController) Show(c web.C, w http.ResponseWriter, r *http.Request) {
	// jobId := c.URLParams["jobId"]
	encoder := json.NewEncoder(w)
	encoder.Encode(response{Error: false, Messages: []string{}})
}

func (controller *JobsController) Run(c web.C, w http.ResponseWriter, r *http.Request) {
	form := ReqForm{}
	if err := json.NewDecoder(r.Body).Decode(&form); err != nil {
		http.Error(w, "", http.StatusBadRequest)
		return
	}
	encoder := json.NewEncoder(w)
	encoder.Encode(response{Error: false, Messages: []string{}})
}
<commit_msg>Implement feature<commit_after>package controllers

import (
	"encoding/json"
	"net/http"

	"github.com/learnin/go-multilog"
	"github.com/zenazn/goji/web"

	"github.com/learnin/batch-rest-controller/helpers"
)

type JobsController struct {
	DS     *helpers.DataSource
	Logger *multilog.MultiLogger
}

type Request struct {
	Async         bool
	RequireResult bool
	Command       string
	Args          string
}

func (controller *JobsController) Show(c web.C, w http.ResponseWriter, r *http.Request) {
	// jobId := c.URLParams["jobId"]
	encoder := json.NewEncoder(w)
	encoder.Encode(response{Error: false, Messages: []string{}})
}

func (controller *JobsController) Run(c web.C, w http.ResponseWriter, r *http.Request) {
	req := Request{}
	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
		controller.Logger.Error(err)
		http.Error(w, "", http.StatusBadRequest)
		return
	}
	if req.Async && req.RequireResult {
		db := controller.DS.GetDB()
		if d := db.Exec("insert into jobs(async, require_result, command, args) VALUES(?, ?, ?, ?)", req.Async, req.RequireResult, req.Command, req.Args); d.Error != nil {
			controller.Logger.Errorf("jobs テーブル登録時にエラーが発生しました。error=%v", d.Error)
			sendEroorResponse(w, d.Error, "")
			return
		}
	}
	encoder := json.NewEncoder(w)
	encoder.Encode(response{Error: false, Messages: []string{}})
}
<|endoftext|>


<commit_before>package controllers

import (
	"bitbucket.org/liamstask/goose/lib/goose"
	"database/sql"
	"github.com/coopernurse/gorp"
	"github.com/jpibarra1130/simple-api-go/models"
	_ "github.com/ziutek/mymysql/godrv"
	"log"
)

func GetPosts() []models.Post {
	conf, err := goose.NewDBConf(*flagPath, *flagEnv)

	if err != nil {
		log.Fatal(err)
		return nil
	}

	log.Printf("Connecting to %v, %v", conf.Driver.Name, conf.Driver.OpenStr)

	db, err := sql.Open(conf.Driver.Name, conf.Driver.OpenStr)

	dbmap := &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{"InnoDB", "UTF8"}}
	defer dbmap.Db.Close()

	var posts []models.Post
	_, err = dbmap.Select(&posts, "select id, title, body from posts order by id")

	if err != nil {
		log.Printf("Error: ", err)
		return nil
	}

	return posts
}
<commit_msg>Used initDb() to get the dbMap<commit_after>package controllers

import (
	"github.com/jpibarra1130/simple-api-go/models"
	"log"
)

func GetPosts() []models.Post {
	dbmap := initDb()
	defer dbmap.Db.Close()

	var posts []models.Post
	_, err := dbmap.Select(&posts, "select id, title, body from posts order by id")

	if err != nil {
		log.Printf("Error: ", err)
		return nil
	}

	return posts
}
<|endoftext|>


<commit_before>package projects

import (
	"fmt"
	"time"

	"github.com/ion-channel/ionic/aliases"
	"github.com/ion-channel/ionic/tags"
)

var (
	// ErrInvalidProject is returned when a given project does not pass the
	// standards for a project
	ErrInvalidProject = fmt.Errorf("project has invalid fields")
)

//Project is a representation of a project within the Ion Channel system
type Project struct {
	ID             *string         `json:"id,omitempty"`
	TeamID         *string         `json:"team_id,omitempty"`
	RulesetID      *string         `json:"ruleset_id,omitempty"`
	Name           *string         `json:"name,omitempty"`
	Type           *string         `json:"type,omitempty"`
	Source         *string         `json:"source,omitempty"`
	Branch         *string         `json:"branch,omitempty"`
	Description    *string         `json:"description,omitempty"`
	Active         bool            `json:"active"`
	ChatChannel    string          `json:"chat_channel"`
	CreatedAt      time.Time       `json:"created_at"`
	UpdatedAt      time.Time       `json:"updated_at"`
	DeployKey      string          `json:"deploy_key"`
	Monitor        bool            `json:"should_monitor"`
	POCName        string          `json:"poc_name"`
	POCEmail       string          `json:"poc_email"`
	Username       string          `json:"username"`
	Password       string          `json:"password"`
	KeyFingerprint string          `json:"key_fingerprint"`
	Aliases        []aliases.Alias `json:"aliases"`
	Tags           []tags.Tag      `json:"tags"`
}

// Validate returns a slice of fields as a string and an error. The fields will
// be a list of fields that did not pass the validation. An error will only be
// returned if any of the fields fail their validation.
func (p *Project) Validate() ([]string, error) {
	var fields []string
	var err error

	if p.ID == nil {
		fields = append(fields, "id")
		err = ErrInvalidProject
	}

	if p.TeamID == nil {
		fields = append(fields, "team_id")
		err = ErrInvalidProject
	}

	if p.RulesetID == nil {
		fields = append(fields, "ruleset_id")
		err = ErrInvalidProject
	}

	if p.Name == nil {
		fields = append(fields, "name")
		err = ErrInvalidProject
	}

	if p.Type == nil {
		fields = append(fields, "type")
		err = ErrInvalidProject
	}

	if p.Source == nil {
		fields = append(fields, "source")
		err = ErrInvalidProject
	}

	if p.Branch == nil {
		fields = append(fields, "branch")
		err = ErrInvalidProject
	}

	if p.Description == nil {
		fields = append(fields, "description")
		err = ErrInvalidProject
	}

	return fields, err
}
<commit_msg>updated per PR request<commit_after>package projects

import (
	"fmt"
	"time"

	"github.com/ion-channel/ionic/aliases"
	"github.com/ion-channel/ionic/tags"
)

var (
	// ErrInvalidProject is returned when a given project does not pass the
	// standards for a project
	ErrInvalidProject = fmt.Errorf("project has invalid fields")
)

//Project is a representation of a project within the Ion Channel system
type Project struct {
	ID             *string         `json:"id,omitempty"`
	TeamID         *string         `json:"team_id,omitempty"`
	RulesetID      *string         `json:"ruleset_id,omitempty"`
	Name           *string         `json:"name,omitempty"`
	Type           *string         `json:"type,omitempty"`
	Source         *string         `json:"source,omitempty"`
	Branch         *string         `json:"branch,omitempty"`
	Description    *string         `json:"description,omitempty"`
	Active         bool            `json:"active"`
	ChatChannel    string          `json:"chat_channel"`
	CreatedAt      time.Time       `json:"created_at"`
	UpdatedAt      time.Time       `json:"updated_at"`
	DeployKey      string          `json:"deploy_key"`
	Monitor        bool            `json:"should_monitor"`
	POCName        string          `json:"poc_name"`
	POCEmail       string          `json:"poc_email"`
	Username       string          `json:"username"`
	Password       string          `json:"password"`
	KeyFingerprint string          `json:"key_fingerprint"`
	Aliases        []aliases.Alias `json:"aliases"`
	Tags           []tags.Tag      `json:"tags"`
}

// Validate returns a slice of fields as a string and an error. The fields will
// be a list of fields that did not pass the validation. An error will only be
// returned if any of the fields fail their validation.
func (p *Project) Validate() ([]string, error) {
	var invalidFields []string
	var err error

	if p.ID == nil {
		invalidFields = append(invalidFields, "id")
		err = ErrInvalidProject
	}

	if p.TeamID == nil {
		invalidFields = append(invalidFields, "team_id")
		err = ErrInvalidProject
	}

	if p.RulesetID == nil {
		invalidFields = append(invalidFields, "ruleset_id")
		err = ErrInvalidProject
	}

	if p.Name == nil {
		invalidFields = append(invalidFields, "name")
		err = ErrInvalidProject
	}

	if p.Type == nil {
		invalidFields = append(invalidFields, "type")
		err = ErrInvalidProject
	}

	if p.Source == nil {
		invalidFields = append(invalidFields, "source")
		err = ErrInvalidProject
	}

	if p.Branch == nil {
		invalidFields = append(invalidFields, "branch")
		err = ErrInvalidProject
	}

	if p.Description == nil {
		invalidFields = append(invalidFields, "description")
		err = ErrInvalidProject
	}

	return invalidFields, err
}
<|endoftext|>


<commit_before>package bus

func <commit_msg>Added stub for mesh.go<commit_after>package bus

/*
StartMesh ...
This is intended to start the mesh networking piece.
Ideally this will enable the CM system to be self-healing and
provide resiliency against host and network failure.
*/
func StartMesh() {
    //TODO: Write the code to actually start and build the mesh network
}<|endoftext|>


<commit_before>package workqueue

// Copyright 2017 Nick Saika
//
// 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.

import (
	"fmt"
	"math/rand"
	"sync"
	"testing"
	"time"
)

func init() {
	rand.Seed(time.Now().UnixNano())
}

func TestWorkQueue(t *testing.T) {
	var wg sync.WaitGroup
	for i := 10; i < 1000000; i *= 10 {
		wg.Add(i)
		q := New(i)
		for j := 0; j < i; j++ {
			go func(w int) {
				q <- func() {
					dur := time.Duration(rand.Intn(10))
					time.Sleep(dur * time.Millisecond)
					t.Log(w)
					wg.Done()
				}
			}(j)
		}
		wg.Wait()
		close(q)
	}
}

func ExampleNew() {
	// Create a new WorkQueue.
	wq := New(1024)

	// This sync.WaitGroup is to make sure we wait until all of our work
	// is done.
	var wg sync.WaitGroup

	// Do some work.
	for i := 0; i < 2048; i++ {
		wg.Add(1)
		go func(v int) {
			wq <- func() {
				defer wg.Done()

				time.Sleep(time.Duration(rand.Intn(10)) * time.Millisecond)
				fmt.Println(v)
			}
		}(i)
	}

	// Wait for all of the work to finish, then close the WorkQueue.
	wg.Wait()
	close(wq)
}
<commit_msg>move the example to NewN<commit_after>package workqueue

// Copyright 2017 Nick Saika
//
// 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.

import (
	"fmt"
	"math/rand"
	"sync"
	"testing"
	"time"
)

func init() {
	rand.Seed(time.Now().UnixNano())
}

func TestWorkQueue(t *testing.T) {
	var wg sync.WaitGroup
	for i := 10; i < 1000000; i *= 10 {
		wg.Add(i)
		q := New(i)
		for j := 0; j < i; j++ {
			go func(w int) {
				q <- func() {
					dur := time.Duration(rand.Intn(10))
					time.Sleep(dur * time.Millisecond)
					t.Log(w)
					wg.Done()
				}
			}(j)
		}
		wg.Wait()
		close(q)
	}
}

func ExampleNewN() {
	// Create a new WorkQueue.
	wq := New(1024)

	// This sync.WaitGroup is to make sure we wait until all of our work
	// is done.
	var wg sync.WaitGroup

	// Do some work.
	for i := 0; i < 2048; i++ {
		wg.Add(1)
		go func(v int) {
			wq <- func() {
				defer wg.Done()

				time.Sleep(time.Duration(rand.Intn(10)) * time.Millisecond)
				fmt.Println(v)
			}
		}(i)
	}

	// Wait for all of the work to finish, then close the WorkQueue.
	wg.Wait()
	close(wq)
}
<|endoftext|>


<commit_before>// Author: Simon Labrecque <simon@wegel.ca>

package main

import (
	"flag"
	"fmt"
	"io"
	"log"
	"net"
	"net/url"
	"os"
	"os/signal"
	"time"
	"unicode/utf8"

	"github.com/gorilla/websocket"
)

type TCPConnWithStatus struct {
	conn *net.TCPConn
	up   bool
}

var addr = flag.String("addr", "localhost:8080", "the middle websocket connector")
var channelId = flag.String("channel", "", "the channel ID (guid)")
var remote = flag.String("remote", "localhost:22", "remote host:port to proxy to")

func main() {
	flag.Parse()

	if *channelId == "" {
		log.Println("The channel ID is mandatory. Please set it (-channel={id})")
		return
	}

	interrupt := make(chan os.Signal, 1)
	signal.Notify(interrupt, os.Interrupt)

	done := make(chan struct{})
	toWS := make(chan []byte)
	fromWS := make(chan []byte)
	controlChan := make(chan string)

	tcpAddr, err := net.ResolveTCPAddr("tcp", *remote)
	if err != nil {
		println("ResolveTCPAddr failed:", err.Error())
		os.Exit(1)
	}

	go func() {
		ts := TCPConnWithStatus{conn: nil, up: false}
		for {
			select {

			case message := <-fromWS:
				if !ts.up {
					log.Println("Connecting to", tcpAddr.String())
					ts.conn, err = net.DialTCP("tcp", nil, tcpAddr)
					if err != nil {
						println("Dial failed:", err.Error())
						os.Exit(1)
					}
					ts.up = true
					go handleTCP(&ts, toWS, controlChan)
				}
				if n, err := ts.conn.Write(message); err != nil || n < len(message) {
					if err != nil {
						log.Fatalln("Error while writing to TCP", err)
					}
					if n < len(message) {
						log.Fatalf("Could't write all message; wrote %v / %v\n", n, len(message))
					}
				}
			}
		}
	}()

	u := url.URL{Scheme: "ws", Host: *addr, Path: "/ws/proxy/" + *channelId}
	log.Printf("connecting to %s", u.String())

	ws, resp, err := websocket.DefaultDialer.Dial(u.String(), nil)
	if err != nil {
		log.Fatal("handshake failed with status ", resp.StatusCode)
	}
	defer ws.Close()

	go func() {
		defer close(done)
		for {
			msgType, message, err := ws.ReadMessage()
			if err != nil {
				log.Println("read:", err)
				return
			} else {
				switch msgType {
				case websocket.BinaryMessage:
					fromWS <- message
				case websocket.TextMessage:
					controlChan <- string(message)
				}

			}
		}
		log.Println("Terminating websocket read pump")
	}()

	for {
		select {
		case message := <-toWS:
			if err := ws.WriteMessage(websocket.BinaryMessage, message); err != nil {
				log.Fatalln("Error while sending message to ws:", err)
			}
		case <-interrupt:
			log.Println("interrupt")
			// To cleanly close a connection, a client should send a close
			// frame and wait for the server to close the connection.
			err := ws.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
			if err != nil {
				log.Println("write close:", err)
				return
			}
			select {
			case <-done:
			case <-time.After(time.Second):
			}
			ws.Close()
			return
		}
	}
	log.Println("Terminating websocket read pump")
}

func handleTCP(ts *TCPConnWithStatus, fromTCP chan<- []byte, controlChan <-chan string) {
	defer func() {
		if r := recover(); r != nil {
			fmt.Println("Recovered in f", r)
		}

		fmt.Println("Done handleRequest")
		if ts.conn != nil {
			ts.conn.Close()
			ts.up = false
		}
	}()

	go func() {
		for {
			buf := make([]byte, 1024*1024*1)
			n, err := ts.conn.Read(buf)

			switch err {
			case io.EOF:
				fmt.Println("EOF")
				return

			case nil:
				message := buf[:n]
				fromTCP <- message

			default:
				log.Printf("Receive data failed:%s\n", err)
				return
			}
		}
	}()

	for {
		select {
		case controlMessage := <-controlChan:
			log.Println("Got control message:", controlMessage)
			return
		}
	}
}

func truncateString(s string, n int) string {
	if len(s) <= n {
		return s
	}
	for !utf8.ValidString(s[:n]) {
		n--
	}
	return s[:n]
}
<commit_msg>wwsproxy: extract read() and write() functions<commit_after>// Author: Simon Labrecque <simon@wegel.ca>

package main

import (
	"flag"
	"fmt"
	"io"
	"log"
	"net"
	"net/url"
	"os"
	"os/signal"
	"time"
	"unicode/utf8"

	"github.com/gorilla/websocket"
)

type TCPConnWithStatus struct {
	conn *net.TCPConn
	up   bool
}

var addr = flag.String("addr", "localhost:8080", "the middle websocket connector")
var channelId = flag.String("channel", "", "the channel ID (guid)")
var remote = flag.String("remote", "localhost:22", "remote host:port to proxy to")

func main() {
	flag.Parse()

	if *channelId == "" {
		log.Println("The channel ID is mandatory. Please set it (-channel={id})")
		return
	}

	interrupt := make(chan os.Signal, 1)
	signal.Notify(interrupt, os.Interrupt)

	done := make(chan struct{})
	toWS := make(chan []byte)
	fromWS := make(chan []byte)
	controlChan := make(chan string)

	tcpAddr, err := net.ResolveTCPAddr("tcp", *remote)
	if err != nil {
		println("ResolveTCPAddr failed:", err.Error())
		os.Exit(1)
	}

	go func() {
		ts := TCPConnWithStatus{conn: nil, up: false}
		for {
			select {

			case message := <-fromWS:
				if !ts.up {
					log.Println("Connecting to", tcpAddr.String())
					ts.conn, err = net.DialTCP("tcp", nil, tcpAddr)
					if err != nil {
						println("Dial failed:", err.Error())
						os.Exit(1)
					}
					ts.up = true
					go handleTCP(&ts, toWS, controlChan)
				}
				if n, err := ts.conn.Write(message); err != nil || n < len(message) {
					if err != nil {
						log.Fatalln("Error while writing to TCP", err)
					}
					if n < len(message) {
						log.Fatalf("Could't write all message; wrote %v / %v\n", n, len(message))
					}
				}
			}
		}
	}()

	u := url.URL{Scheme: "ws", Host: *addr, Path: "/ws/proxy/" + *channelId}
	log.Printf("connecting to %s", u.String())

	ws, resp, err := websocket.DefaultDialer.Dial(u.String(), nil)
	if err != nil {
		log.Fatal("handshake failed with status ", resp.StatusCode)
	}
	defer ws.Close()

	go read(ws, fromWS, controlChan, done)
	write(ws, toWS, done, interrupt)

	log.Println("Terminating websocket read pump")
}

func read(ws *websocket.Conn, fromWS chan<- []byte, controlChan chan<- string, done chan struct{}) {
	defer close(done)
	for {
		msgType, message, err := ws.ReadMessage()
		if err != nil {
			log.Println("read:", err)
			return
		}

		switch msgType {
		case websocket.BinaryMessage:
			fromWS <- message
		case websocket.TextMessage:
			controlChan <- string(message)
		}
	}
	log.Println("Terminating websocket read pump")

	return
}

func write(ws *websocket.Conn, toWS <-chan []byte, done chan struct{}, interrupt chan os.Signal) {
	for {
		select {
		case message := <-toWS:
			if err := ws.WriteMessage(websocket.BinaryMessage, message); err != nil {
				log.Fatalln("Error while sending message to ws:", err)
			}
		case <-interrupt:
			log.Println("interrupt")
			// To cleanly close a connection, a client should send a close
			// frame and wait for the server to close the connection.
			err := ws.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
			if err != nil {
				log.Println("write close:", err)
				return
			}
			select {
			case <-done:
			case <-time.After(time.Second):
			}
			ws.Close()
			return
		}
	}
}

func handleTCP(ts *TCPConnWithStatus, fromTCP chan<- []byte, controlChan <-chan string) {
	defer func() {
		if r := recover(); r != nil {
			fmt.Println("Recovered in f", r)
		}

		fmt.Println("Done handleRequest")
		if ts.conn != nil {
			ts.conn.Close()
			ts.up = false
		}
	}()

	go func() {
		for {
			buf := make([]byte, 1024*1024*1)
			n, err := ts.conn.Read(buf)

			switch err {
			case io.EOF:
				fmt.Println("EOF")
				return

			case nil:
				message := buf[:n]
				fromTCP <- message

			default:
				log.Printf("Receive data failed:%s\n", err)
				return
			}
		}
	}()

	for {
		select {
		case controlMessage := <-controlChan:
			log.Println("Got control message:", controlMessage)
			return
		}
	}
}
<|endoftext|>


<commit_before>//
// Copyright (c) 2016 Intel Corporation
//
// 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 virtcontainers

import (
	"net"
	"os"
	"reflect"
	"testing"
)

func testNetworkModelSet(t *testing.T, value string, expected NetworkModel) {
	var netModel NetworkModel

	err := netModel.Set(value)
	if err != nil {
		t.Fatal(err)
	}

	if netModel != expected {
		t.Fatal()
	}
}

func TestNoopNetworkModelSet(t *testing.T) {
	testNetworkModelSet(t, "noop", NoopNetworkModel)
}

func TestCNINetworkModelSet(t *testing.T) {
	testNetworkModelSet(t, "CNI", CNINetworkModel)
}

func TestCNMNetworkModelSet(t *testing.T) {
	testNetworkModelSet(t, "CNM", CNMNetworkModel)
}

func TestNetworkModelSetFailure(t *testing.T) {
	var netModel NetworkModel

	err := netModel.Set("wrong-value")
	if err == nil {
		t.Fatal(err)
	}
}

func testNetworkModelString(t *testing.T, netModel *NetworkModel, expected string) {
	result := netModel.String()

	if result != expected {
		t.Fatal()
	}
}

func TestNoopNetworkModelString(t *testing.T) {
	netModel := NoopNetworkModel
	testNetworkModelString(t, &netModel, string(NoopNetworkModel))
}

func TestCNINetworkModelString(t *testing.T) {
	netModel := CNINetworkModel
	testNetworkModelString(t, &netModel, string(CNINetworkModel))
}

func TestCNMNetworkModelString(t *testing.T) {
	netModel := CNMNetworkModel
	testNetworkModelString(t, &netModel, string(CNMNetworkModel))
}

func TestWrongNetworkModelString(t *testing.T) {
	var netModel NetworkModel
	testNetworkModelString(t, &netModel, "")
}

func testNewNetworkFromNetworkModel(t *testing.T, netModel NetworkModel, expected interface{}) {
	result := newNetwork(netModel)

	if reflect.DeepEqual(result, expected) == false {
		t.Fatal()
	}
}

func TestNewNoopNetworkFromNetworkModel(t *testing.T) {
	testNewNetworkFromNetworkModel(t, NoopNetworkModel, &noopNetwork{})
}

func TestNewCNINetworkFromNetworkModel(t *testing.T) {
	testNewNetworkFromNetworkModel(t, CNINetworkModel, &cni{})
}

func TestNewCNMNetworkFromNetworkModel(t *testing.T) {
	testNewNetworkFromNetworkModel(t, CNMNetworkModel, &cnm{})
}

func TestNewUnknownNetworkFromNetworkModel(t *testing.T) {
	var netModel NetworkModel
	testNewNetworkFromNetworkModel(t, netModel, &noopNetwork{})
}

func TestCreateDeleteNetNS(t *testing.T) {
	if os.Geteuid() != 0 {
		t.Skip(testDisabledAsNonRoot)
	}

	netNSPath, err := createNetNS()
	if err != nil {
		t.Fatal(err)
	}

	if netNSPath == "" {
		t.Fatal()
	}

	_, err = os.Stat(netNSPath)
	if err != nil {
		t.Fatal(err)
	}

	err = deleteNetNS(netNSPath, true)
	if err != nil {
		t.Fatal(err)
	}
}

func TestCreateNetworkEndpoint(t *testing.T) {
	macAddr := net.HardwareAddr{0x02, 0x00, 0xCA, 0xFE, 0x00, 0x04}

	expected := Endpoint{
		NetPair: NetworkInterfacePair{
			ID:   "uniqueTestID-4",
			Name: "br4",
			VirtIface: NetworkInterface{
				Name:     "eth4",
				HardAddr: macAddr.String(),
			},
			TAPIface: NetworkInterface{
				Name: "tap4",
			},
		},
	}

	result, err := createNetworkEndpoint(4, "uniqueTestID", "")
	if err != nil {
		t.Fatal(err)
	}

	if reflect.DeepEqual(result, expected) == false {
		t.Fatal()
	}
}

func TestCreateNetworkEndpointChooseIfaceName(t *testing.T) {
	macAddr := net.HardwareAddr{0x02, 0x00, 0xCA, 0xFE, 0x00, 0x04}

	expected := Endpoint{
		NetPair: NetworkInterfacePair{
			ID:   "uniqueTestID-4",
			Name: "br4",
			VirtIface: NetworkInterface{
				Name:     "eth1",
				HardAddr: macAddr.String(),
			},
			TAPIface: NetworkInterface{
				Name: "tap4",
			},
		},
	}

	result, err := createNetworkEndpoint(4, "uniqueTestID", "eth1")
	if err != nil {
		t.Fatal(err)
	}

	if reflect.DeepEqual(result, expected) == false {
		t.Fatal()
	}
}

func TestCreateNetworkEndpointInvalidArgs(t *testing.T) {
	type endpointValues struct {
		idx      int
		uniqueID string
		ifName   string
	}

	// all elements are expected to result in failure
	failingValues := []endpointValues{
		{-1, "foo", "bar"},
		{-1, "foo", ""},
		{-3, "foo", "bar"},
		{-3, "foo", ""},
		{0, "", "bar"},
		{0, "", ""},
		{1, "", "bar"},
		{1, "", ""},
	}

	for _, d := range failingValues {
		result, err := createNetworkEndpoint(d.idx, d.uniqueID, d.ifName)
		if err == nil {
			t.Fatalf("expected invalid endpoint for %v, got %v", d, result)
		}
	}
}

func TestCreateNetworkEndpoints(t *testing.T) {
	numOfEndpoints := 3

	endpoints, err := createNetworkEndpoints(numOfEndpoints)
	if err != nil {
		t.Fatal(err)
	}

	if len(endpoints) != numOfEndpoints {
		t.Fatal()
	}
}

func TestCreateNetworkEndpointsFailure(t *testing.T) {
	_, err := createNetworkEndpoints(0)
	if err == nil {
		t.Fatal(err)
	}
}
<commit_msg>network: Add new unit tests<commit_after>//
// Copyright (c) 2016 Intel Corporation
//
// 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 virtcontainers

import (
	"net"
	"os"
	"reflect"
	"testing"

	"github.com/containernetworking/cni/pkg/ns"
	"github.com/vishvananda/netlink"
	"github.com/vishvananda/netns"
)

func testNetworkModelSet(t *testing.T, value string, expected NetworkModel) {
	var netModel NetworkModel

	err := netModel.Set(value)
	if err != nil {
		t.Fatal(err)
	}

	if netModel != expected {
		t.Fatal()
	}
}

func TestNoopNetworkModelSet(t *testing.T) {
	testNetworkModelSet(t, "noop", NoopNetworkModel)
}

func TestCNINetworkModelSet(t *testing.T) {
	testNetworkModelSet(t, "CNI", CNINetworkModel)
}

func TestCNMNetworkModelSet(t *testing.T) {
	testNetworkModelSet(t, "CNM", CNMNetworkModel)
}

func TestNetworkModelSetFailure(t *testing.T) {
	var netModel NetworkModel

	err := netModel.Set("wrong-value")
	if err == nil {
		t.Fatal(err)
	}
}

func testNetworkModelString(t *testing.T, netModel *NetworkModel, expected string) {
	result := netModel.String()

	if result != expected {
		t.Fatal()
	}
}

func TestNoopNetworkModelString(t *testing.T) {
	netModel := NoopNetworkModel
	testNetworkModelString(t, &netModel, string(NoopNetworkModel))
}

func TestCNINetworkModelString(t *testing.T) {
	netModel := CNINetworkModel
	testNetworkModelString(t, &netModel, string(CNINetworkModel))
}

func TestCNMNetworkModelString(t *testing.T) {
	netModel := CNMNetworkModel
	testNetworkModelString(t, &netModel, string(CNMNetworkModel))
}

func TestWrongNetworkModelString(t *testing.T) {
	var netModel NetworkModel
	testNetworkModelString(t, &netModel, "")
}

func testNewNetworkFromNetworkModel(t *testing.T, netModel NetworkModel, expected interface{}) {
	result := newNetwork(netModel)

	if reflect.DeepEqual(result, expected) == false {
		t.Fatal()
	}
}

func TestNewNoopNetworkFromNetworkModel(t *testing.T) {
	testNewNetworkFromNetworkModel(t, NoopNetworkModel, &noopNetwork{})
}

func TestNewCNINetworkFromNetworkModel(t *testing.T) {
	testNewNetworkFromNetworkModel(t, CNINetworkModel, &cni{})
}

func TestNewCNMNetworkFromNetworkModel(t *testing.T) {
	testNewNetworkFromNetworkModel(t, CNMNetworkModel, &cnm{})
}

func TestNewUnknownNetworkFromNetworkModel(t *testing.T) {
	var netModel NetworkModel
	testNewNetworkFromNetworkModel(t, netModel, &noopNetwork{})
}

func TestCreateDeleteNetNS(t *testing.T) {
	if os.Geteuid() != 0 {
		t.Skip(testDisabledAsNonRoot)
	}

	netNSPath, err := createNetNS()
	if err != nil {
		t.Fatal(err)
	}

	if netNSPath == "" {
		t.Fatal()
	}

	_, err = os.Stat(netNSPath)
	if err != nil {
		t.Fatal(err)
	}

	err = deleteNetNS(netNSPath, true)
	if err != nil {
		t.Fatal(err)
	}
}

func TestCreateNetworkEndpoint(t *testing.T) {
	macAddr := net.HardwareAddr{0x02, 0x00, 0xCA, 0xFE, 0x00, 0x04}

	expected := Endpoint{
		NetPair: NetworkInterfacePair{
			ID:   "uniqueTestID-4",
			Name: "br4",
			VirtIface: NetworkInterface{
				Name:     "eth4",
				HardAddr: macAddr.String(),
			},
			TAPIface: NetworkInterface{
				Name: "tap4",
			},
		},
	}

	result, err := createNetworkEndpoint(4, "uniqueTestID", "")
	if err != nil {
		t.Fatal(err)
	}

	if reflect.DeepEqual(result, expected) == false {
		t.Fatal()
	}
}

func TestCreateNetworkEndpointChooseIfaceName(t *testing.T) {
	macAddr := net.HardwareAddr{0x02, 0x00, 0xCA, 0xFE, 0x00, 0x04}

	expected := Endpoint{
		NetPair: NetworkInterfacePair{
			ID:   "uniqueTestID-4",
			Name: "br4",
			VirtIface: NetworkInterface{
				Name:     "eth1",
				HardAddr: macAddr.String(),
			},
			TAPIface: NetworkInterface{
				Name: "tap4",
			},
		},
	}

	result, err := createNetworkEndpoint(4, "uniqueTestID", "eth1")
	if err != nil {
		t.Fatal(err)
	}

	if reflect.DeepEqual(result, expected) == false {
		t.Fatal()
	}
}

func TestCreateNetworkEndpointInvalidArgs(t *testing.T) {
	type endpointValues struct {
		idx      int
		uniqueID string
		ifName   string
	}

	// all elements are expected to result in failure
	failingValues := []endpointValues{
		{-1, "foo", "bar"},
		{-1, "foo", ""},
		{-3, "foo", "bar"},
		{-3, "foo", ""},
		{0, "", "bar"},
		{0, "", ""},
		{1, "", "bar"},
		{1, "", ""},
	}

	for _, d := range failingValues {
		result, err := createNetworkEndpoint(d.idx, d.uniqueID, d.ifName)
		if err == nil {
			t.Fatalf("expected invalid endpoint for %v, got %v", d, result)
		}
	}
}

func TestCreateNetworkEndpoints(t *testing.T) {
	numOfEndpoints := 3

	endpoints, err := createNetworkEndpoints(numOfEndpoints)
	if err != nil {
		t.Fatal(err)
	}

	if len(endpoints) != numOfEndpoints {
		t.Fatal()
	}
}

func TestCreateNetworkEndpointsFailure(t *testing.T) {
	numOfEndpoints := 0

	_, err := createNetworkEndpoints(numOfEndpoints)
	if err == nil {
		t.Fatalf("Should fail because %d endpoints is invalid",
			numOfEndpoints)
	}
}

func TestGetIfacesFromNetNsFailureEmptyNetNsPath(t *testing.T) {
	if _, err := getIfacesFromNetNs(""); err == nil {
		t.Fatal("Should fail because network namespace is empty")
	}
}

func testGetIfacesFromNetNsSuccessful(t *testing.T, link netlink.Link, expected []netIfaceAddrs) {
	lAttrs := link.Attrs()

	n, err := ns.NewNS()
	if err != nil {
		t.Fatal(err)
	}
	defer n.Close()

	netnsHandle, err := netns.GetFromPath(n.Path())
	if err != nil {
		t.Fatal(err)
	}
	defer netnsHandle.Close()

	netlinkHandle, err := netlink.NewHandleAt(netnsHandle)
	if err != nil {
		t.Fatal(err)
	}
	defer netlinkHandle.Delete()

	if err := netlinkHandle.LinkAdd(link); err != nil {
		t.Fatal(err)
	}

	if err := netlinkHandle.LinkSetHardwareAddr(link, lAttrs.HardwareAddr); err != nil {
		t.Fatal(err)
	}

	result, err := getIfacesFromNetNs(n.Path())
	if err != nil {
		t.Fatal(err)
	}

	if reflect.DeepEqual(result, expected) == false {
		t.Fatalf("Got %+v\nExpecting %+v", result, expected)
	}
}

func TestGetIfacesFromNetNsSuccessfulBridge(t *testing.T) {
	testNetIface := "testIface0"
	testMTU := 1500
	testMACAddr := "00:00:00:00:00:01"

	hwAddr, err := net.ParseMAC(testMACAddr)
	if err != nil {
		t.Fatal(err)
	}

	link := &netlink.Bridge{
		LinkAttrs: netlink.LinkAttrs{
			Name:         testNetIface,
			MTU:          testMTU,
			HardwareAddr: hwAddr,
			TxQLen:       -1,
		},
	}

	expected := []netIfaceAddrs{
		{
			iface: net.Interface{
				Index: 1,
				MTU:   65536,
				Name:  "lo",
				Flags: net.FlagLoopback,
			},
		},
		{
			iface: net.Interface{
				Index:        2,
				MTU:          testMTU,
				Name:         testNetIface,
				HardwareAddr: hwAddr,
				Flags:        net.FlagBroadcast | net.FlagMulticast,
			},
		},
	}

	testGetIfacesFromNetNsSuccessful(t, link, expected)
}

func TestGetNetIfaceByNameFailureEmptyList(t *testing.T) {
	if _, err := getNetIfaceByName("testIface", []netIfaceAddrs{}); err == nil {
		t.Fatal("Should fail because network interface list is empty")
	}
}

func TestGetNetIfaceByNameSuccessful(t *testing.T) {
	testIfaceName := "testIfaceName"

	expected := net.Interface{
		Name: testIfaceName,
	}

	netIfaces := []netIfaceAddrs{
		{
			iface: net.Interface{
				Name: testIfaceName,
			},
		},
	}

	result, err := getNetIfaceByName(testIfaceName, netIfaces)
	if err != nil {
		t.Fatal(err)
	}

	if reflect.DeepEqual(result, expected) == false {
		t.Fatalf("Got %+v\nExpecting %+v", result, expected)
	}
}
<|endoftext|>


<commit_before>package mutation

import (
	"database/sql"
	"fmt"
	"reflect"
	"testing"

	"github.com/google/keytransparency/core/transaction"

	"golang.org/x/net/context"
	"google.golang.org/grpc"

	tpb "github.com/google/keytransparency/core/proto/keytransparency_v1_types"
	"github.com/google/trillian"
)

const (
	logID = 0
	mapID = 0
)

func signedKV(t *testing.T, start, end int) []*tpb.SignedKV {
	if start > end {
		t.Fatalf("start=%v > end=%v", start, end)
	}
	kvs := make([]*tpb.SignedKV, 0, end-start)
	for i := start; i <= end; i++ {
		kvs = append(kvs, &tpb.SignedKV{
			KeyValue: &tpb.KeyValue{
				Key:   []byte(fmt.Sprintf("key_%v", i)),
				Value: []byte(fmt.Sprintf("value_%v", i)),
			}})
	}
	return kvs
}

func prepare(t *testing.T, mutations *fakeMutation, fakeMap *fakeTrillianMapClient) {
	createEpoch(t, mutations, fakeMap, 1, 1, 6)
	createEpoch(t, mutations, fakeMap, 2, 7, 10)
}

func createEpoch(t *testing.T, mutations *fakeMutation, fakeMap *fakeTrillianMapClient, epoch int64, start, end int) {
	kvs := signedKV(t, start, end)
	for _, kv := range kvs {
		if _, err := mutations.Write(nil, kv); err != nil {
			t.Fatalf("mutations.Write failed: %v", err)
		}
	}
	fakeMap.tmap[epoch] = &trillian.SignedMapRoot{
		Metadata: &trillian.MapperMetadata{
			HighestFullyCompletedSeq: int64(end),
		},
		MapRevision: epoch,
	}
}

func TestGetMutations(t *testing.T) {
	ctx := context.Background()
	fakeMutations := &fakeMutation{}
	fakeMap := newFakeTrillianMapClient()
	prepare(t, fakeMutations, fakeMap)

	for _, tc := range []struct {
		description string
		epoch       int64
		token       string
		pageSize    int32
		mutations   []*tpb.SignedKV
		nextToken   string
		success     bool
	}{
		{"working case complete epoch 1", 1, "", 6, signedKV(t, 1, 6), "", true},
		{"working case complete epoch 2", 2, "", 4, signedKV(t, 7, 10), "", true},
		{"working case partial epoch 1", 1, "", 4, signedKV(t, 1, 4), "4", true},
		{"working case partial epoch 2", 2, "", 3, signedKV(t, 7, 9), "9", true},
		{"working case larger page size", 1, "", 10, signedKV(t, 1, 6), "", true},
		{"working case with page token", 1, "4", 2, signedKV(t, 5, 6), "", true},
		{"working case with page token and small page size", 1, "2", 2, signedKV(t, 3, 4), "4", true},
		{"invalid page token", 1, "some_token", 0, nil, "", false},
	} {
		srv := New(logID, mapID, &fakeTrillianLogClient{}, fakeMap, fakeMutations, &fakeFactory{})
		resp, err := srv.GetMutations(ctx, &tpb.GetMutationsRequest{
			Epoch:     tc.epoch,
			PageToken: tc.token,
			PageSize:  tc.pageSize,
		})
		if got, want := err == nil, tc.success; got != want {
			t.Fatalf("%v: GetMutations: err=%v, want %v", tc.description, got, want)
		}
		if err != nil {
			continue
		}
		if got, want := resp.Epoch, tc.epoch; got != want {
			t.Errorf("%v: resp.Epoch=%v, want %v", tc.description, got, want)
		}
		if got, want := len(resp.Mutations), len(tc.mutations); got != want {
			t.Errorf("%v: len(resp.Mutations)=%v, want %v", tc.description, got, want)
		}
		for i := 0; i < len(resp.Mutations); i++ {
			if got, want := resp.Mutations[i].Update, tc.mutations[i]; !reflect.DeepEqual(got, want) {
				t.Errorf("%v: resp.Mutations[i].Update=%v, want %v", tc.description, got, want)
			}
		}
		if got, want := resp.NextPageToken, tc.nextToken; got != want {
			t.Errorf("%v: resp.NextPageToken=%v, %v", tc.description, got, want)
		}
	}
}

func TestLowestSequenceNumber(t *testing.T) {
	ctx := context.Background()
	fakeMutations := &fakeMutation{}
	fakeMap := newFakeTrillianMapClient()
	prepare(t, fakeMutations, fakeMap)

	for _, tc := range []struct {
		token     string
		epoch     int64
		lowestSeq uint64
		success   bool
	}{
		{"", 0, 0, true},
		{"4", 0, 4, true},
		{"4", 1, 4, true},
		{"some_token", 0, 0, false},
		{"", 1, 6, true},
	} {
		srv := New(logID, mapID, &fakeTrillianLogClient{}, fakeMap, fakeMutations, &fakeFactory{})
		seq, err := srv.lowestSequenceNumber(ctx, tc.token, tc.epoch)
		if got, want := err == nil, tc.success; got != want {
			t.Errorf("lowestSequenceNumber(%v, %v): err=%v, want %v", tc.token, tc.epoch, got, want)
		}
		if got, want := seq, tc.lowestSeq; got != want {
			t.Errorf("lowestSequenceNumber(%v, %v)=%v, want %v", tc.token, tc.epoch, got, want)
		}
	}
}

// trillian.TrillianMapClient fake.
type fakeTrillianMapClient struct {
	tmap map[int64]*trillian.SignedMapRoot
}

func newFakeTrillianMapClient() *fakeTrillianMapClient {
	return &fakeTrillianMapClient{
		tmap: make(map[int64]*trillian.SignedMapRoot),
	}
}

func (*fakeTrillianMapClient) GetLeaves(ctx context.Context, in *trillian.GetMapLeavesRequest, opts ...grpc.CallOption) (*trillian.GetMapLeavesResponse, error) {
	leaves := make([]*trillian.MapLeafInclusion, 0, len(in.Index))
	for _, index := range in.Index {
		leaves = append(leaves, &trillian.MapLeafInclusion{
			Leaf: &trillian.MapLeaf{
				Index: index,
			},
		})
	}
	return &trillian.GetMapLeavesResponse{
		MapLeafInclusion: leaves,
	}, nil
}

func (*fakeTrillianMapClient) SetLeaves(ctx context.Context, in *trillian.SetMapLeavesRequest, opts ...grpc.CallOption) (*trillian.SetMapLeavesResponse, error) {
	return nil, nil
}

func (*fakeTrillianMapClient) GetSignedMapRoot(ctx context.Context, in *trillian.GetSignedMapRootRequest, opts ...grpc.CallOption) (*trillian.GetSignedMapRootResponse, error) {
	return nil, nil
}

// GetSignedMapRootByRevision echos in.MapId in HighestFullyCompletedSeq.
func (m *fakeTrillianMapClient) GetSignedMapRootByRevision(ctx context.Context, in *trillian.GetSignedMapRootByRevisionRequest, opts ...grpc.CallOption) (*trillian.GetSignedMapRootResponse, error) {
	return &trillian.GetSignedMapRootResponse{
		MapRoot: m.tmap[in.Revision],
	}, nil
}

// transaction.Txn fake.
type fakeTxn struct{}

func (*fakeTxn) Prepare(query string) (*sql.Stmt, error) { return nil, nil }
func (*fakeTxn) Commit() error                           { return nil }
func (*fakeTxn) Rollback() error                         { return nil }

// transaction.Factory fake.
type fakeFactory struct{}

func (fakeFactory) NewTxn(ctx context.Context) (transaction.Txn, error) {
	return &fakeTxn{}, nil
}

// mutator.Mutation fake.
type fakeMutation struct {
	mtns []*tpb.SignedKV
}

// sequence numbers are 1-based.
func (m *fakeMutation) ReadRange(txn transaction.Txn, startSequence, endSequence uint64, count int32) (uint64, []*tpb.SignedKV, error) {
	if startSequence > uint64(len(m.mtns)) {
		panic("startSequence > len(m.mtns)")
	}
	// Adjust endSequence.
	if endSequence-startSequence > uint64(count) {
		endSequence = startSequence + uint64(count)
	}
	if endSequence > uint64(len(m.mtns)) {
		endSequence = uint64(len(m.mtns))
	}
	return endSequence, m.mtns[startSequence:endSequence], nil
}

func (m *fakeMutation) ReadAll(txn transaction.Txn, startSequence uint64) (uint64, []*tpb.SignedKV, error) {
	return 0, nil, nil
}

func (m *fakeMutation) Write(txn transaction.Txn, mutation *tpb.SignedKV) (uint64, error) {
	m.mtns = append(m.mtns, mutation)
	return uint64(len(m.mtns)), nil
}

// trillian.TrillianLogClient fake.
type fakeTrillianLogClient struct{}

func (*fakeTrillianLogClient) QueueLeaf(ctx context.Context, in *trillian.QueueLeafRequest, opts ...grpc.CallOption) (*trillian.QueueLeafResponse, error) {
	return nil, nil
}

func (*fakeTrillianLogClient) QueueLeaves(ctx context.Context, in *trillian.QueueLeavesRequest, opts ...grpc.CallOption) (*trillian.QueueLeavesResponse, error) {
	return nil, nil
}

func (*fakeTrillianLogClient) GetInclusionProof(ctx context.Context, in *trillian.GetInclusionProofRequest, opts ...grpc.CallOption) (*trillian.GetInclusionProofResponse, error) {
	return &trillian.GetInclusionProofResponse{}, nil
}

func (*fakeTrillianLogClient) GetInclusionProofByHash(ctx context.Context, in *trillian.GetInclusionProofByHashRequest, opts ...grpc.CallOption) (*trillian.GetInclusionProofByHashResponse, error) {
	return nil, nil
}

func (*fakeTrillianLogClient) GetConsistencyProof(ctx context.Context, in *trillian.GetConsistencyProofRequest, opts ...grpc.CallOption) (*trillian.GetConsistencyProofResponse, error) {
	return &trillian.GetConsistencyProofResponse{}, nil
}

func (*fakeTrillianLogClient) GetLatestSignedLogRoot(ctx context.Context, in *trillian.GetLatestSignedLogRootRequest, opts ...grpc.CallOption) (*trillian.GetLatestSignedLogRootResponse, error) {
	return &trillian.GetLatestSignedLogRootResponse{}, nil
}

func (*fakeTrillianLogClient) GetSequencedLeafCount(ctx context.Context, in *trillian.GetSequencedLeafCountRequest, opts ...grpc.CallOption) (*trillian.GetSequencedLeafCountResponse, error) {
	return nil, nil
}

func (*fakeTrillianLogClient) GetLeavesByIndex(ctx context.Context, in *trillian.GetLeavesByIndexRequest, opts ...grpc.CallOption) (*trillian.GetLeavesByIndexResponse, error) {
	return nil, nil
}

func (*fakeTrillianLogClient) GetLeavesByHash(ctx context.Context, in *trillian.GetLeavesByHashRequest, opts ...grpc.CallOption) (*trillian.GetLeavesByHashResponse, error) {
	return nil, nil
}

func (*fakeTrillianLogClient) GetEntryAndProof(ctx context.Context, in *trillian.GetEntryAndProofRequest, opts ...grpc.CallOption) (*trillian.GetEntryAndProofResponse, error) {
	return nil, nil
}
<commit_msg>Use the FakeTrillianLogClient object in core/fake<commit_after>package mutation

import (
	"database/sql"
	"fmt"
	"reflect"
	"testing"

	"github.com/google/keytransparency/core/fake"
	"github.com/google/keytransparency/core/transaction"

	"golang.org/x/net/context"
	"google.golang.org/grpc"

	tpb "github.com/google/keytransparency/core/proto/keytransparency_v1_types"
	"github.com/google/trillian"
)

const (
	logID = 0
	mapID = 0
)

func signedKV(t *testing.T, start, end int) []*tpb.SignedKV {
	if start > end {
		t.Fatalf("start=%v > end=%v", start, end)
	}
	kvs := make([]*tpb.SignedKV, 0, end-start)
	for i := start; i <= end; i++ {
		kvs = append(kvs, &tpb.SignedKV{
			KeyValue: &tpb.KeyValue{
				Key:   []byte(fmt.Sprintf("key_%v", i)),
				Value: []byte(fmt.Sprintf("value_%v", i)),
			}})
	}
	return kvs
}

func prepare(t *testing.T, mutations *fakeMutation, fakeMap *fakeTrillianMapClient) {
	createEpoch(t, mutations, fakeMap, 1, 1, 6)
	createEpoch(t, mutations, fakeMap, 2, 7, 10)
}

func createEpoch(t *testing.T, mutations *fakeMutation, fakeMap *fakeTrillianMapClient, epoch int64, start, end int) {
	kvs := signedKV(t, start, end)
	for _, kv := range kvs {
		if _, err := mutations.Write(nil, kv); err != nil {
			t.Fatalf("mutations.Write failed: %v", err)
		}
	}
	fakeMap.tmap[epoch] = &trillian.SignedMapRoot{
		Metadata: &trillian.MapperMetadata{
			HighestFullyCompletedSeq: int64(end),
		},
		MapRevision: epoch,
	}
}

func TestGetMutations(t *testing.T) {
	ctx := context.Background()
	fakeMutations := &fakeMutation{}
	fakeMap := newFakeTrillianMapClient()
	prepare(t, fakeMutations, fakeMap)

	for _, tc := range []struct {
		description string
		epoch       int64
		token       string
		pageSize    int32
		mutations   []*tpb.SignedKV
		nextToken   string
		success     bool
	}{
		{"working case complete epoch 1", 1, "", 6, signedKV(t, 1, 6), "", true},
		{"working case complete epoch 2", 2, "", 4, signedKV(t, 7, 10), "", true},
		{"working case partial epoch 1", 1, "", 4, signedKV(t, 1, 4), "4", true},
		{"working case partial epoch 2", 2, "", 3, signedKV(t, 7, 9), "9", true},
		{"working case larger page size", 1, "", 10, signedKV(t, 1, 6), "", true},
		{"working case with page token", 1, "4", 2, signedKV(t, 5, 6), "", true},
		{"working case with page token and small page size", 1, "2", 2, signedKV(t, 3, 4), "4", true},
		{"invalid page token", 1, "some_token", 0, nil, "", false},
	} {
		srv := New(logID, mapID, fake.NewFakeTrillianLogClient(), fakeMap, fakeMutations, &fakeFactory{})
		resp, err := srv.GetMutations(ctx, &tpb.GetMutationsRequest{
			Epoch:     tc.epoch,
			PageToken: tc.token,
			PageSize:  tc.pageSize,
		})
		if got, want := err == nil, tc.success; got != want {
			t.Fatalf("%v: GetMutations: err=%v, want %v", tc.description, got, want)
		}
		if err != nil {
			continue
		}
		if got, want := resp.Epoch, tc.epoch; got != want {
			t.Errorf("%v: resp.Epoch=%v, want %v", tc.description, got, want)
		}
		if got, want := len(resp.Mutations), len(tc.mutations); got != want {
			t.Errorf("%v: len(resp.Mutations)=%v, want %v", tc.description, got, want)
		}
		for i := 0; i < len(resp.Mutations); i++ {
			if got, want := resp.Mutations[i].Update, tc.mutations[i]; !reflect.DeepEqual(got, want) {
				t.Errorf("%v: resp.Mutations[i].Update=%v, want %v", tc.description, got, want)
			}
		}
		if got, want := resp.NextPageToken, tc.nextToken; got != want {
			t.Errorf("%v: resp.NextPageToken=%v, %v", tc.description, got, want)
		}
	}
}

func TestLowestSequenceNumber(t *testing.T) {
	ctx := context.Background()
	fakeMutations := &fakeMutation{}
	fakeMap := newFakeTrillianMapClient()
	prepare(t, fakeMutations, fakeMap)

	for _, tc := range []struct {
		token     string
		epoch     int64
		lowestSeq uint64
		success   bool
	}{
		{"", 0, 0, true},
		{"4", 0, 4, true},
		{"4", 1, 4, true},
		{"some_token", 0, 0, false},
		{"", 1, 6, true},
	} {
		srv := New(logID, mapID, fake.NewFakeTrillianLogClient(), fakeMap, fakeMutations, &fakeFactory{})
		seq, err := srv.lowestSequenceNumber(ctx, tc.token, tc.epoch)
		if got, want := err == nil, tc.success; got != want {
			t.Errorf("lowestSequenceNumber(%v, %v): err=%v, want %v", tc.token, tc.epoch, got, want)
		}
		if got, want := seq, tc.lowestSeq; got != want {
			t.Errorf("lowestSequenceNumber(%v, %v)=%v, want %v", tc.token, tc.epoch, got, want)
		}
	}
}

// trillian.TrillianMapClient fake.
type fakeTrillianMapClient struct {
	tmap map[int64]*trillian.SignedMapRoot
}

func newFakeTrillianMapClient() *fakeTrillianMapClient {
	return &fakeTrillianMapClient{
		tmap: make(map[int64]*trillian.SignedMapRoot),
	}
}

func (*fakeTrillianMapClient) GetLeaves(ctx context.Context, in *trillian.GetMapLeavesRequest, opts ...grpc.CallOption) (*trillian.GetMapLeavesResponse, error) {
	leaves := make([]*trillian.MapLeafInclusion, 0, len(in.Index))
	for _, index := range in.Index {
		leaves = append(leaves, &trillian.MapLeafInclusion{
			Leaf: &trillian.MapLeaf{
				Index: index,
			},
		})
	}
	return &trillian.GetMapLeavesResponse{
		MapLeafInclusion: leaves,
	}, nil
}

func (*fakeTrillianMapClient) SetLeaves(ctx context.Context, in *trillian.SetMapLeavesRequest, opts ...grpc.CallOption) (*trillian.SetMapLeavesResponse, error) {
	return nil, nil
}

func (*fakeTrillianMapClient) GetSignedMapRoot(ctx context.Context, in *trillian.GetSignedMapRootRequest, opts ...grpc.CallOption) (*trillian.GetSignedMapRootResponse, error) {
	return nil, nil
}

// GetSignedMapRootByRevision echos in.MapId in HighestFullyCompletedSeq.
func (m *fakeTrillianMapClient) GetSignedMapRootByRevision(ctx context.Context, in *trillian.GetSignedMapRootByRevisionRequest, opts ...grpc.CallOption) (*trillian.GetSignedMapRootResponse, error) {
	return &trillian.GetSignedMapRootResponse{
		MapRoot: m.tmap[in.Revision],
	}, nil
}

// transaction.Txn fake.
type fakeTxn struct{}

func (*fakeTxn) Prepare(query string) (*sql.Stmt, error) { return nil, nil }
func (*fakeTxn) Commit() error                           { return nil }
func (*fakeTxn) Rollback() error                         { return nil }

// transaction.Factory fake.
type fakeFactory struct{}

func (fakeFactory) NewTxn(ctx context.Context) (transaction.Txn, error) {
	return &fakeTxn{}, nil
}

// mutator.Mutation fake.
type fakeMutation struct {
	mtns []*tpb.SignedKV
}

// sequence numbers are 1-based.
func (m *fakeMutation) ReadRange(txn transaction.Txn, startSequence, endSequence uint64, count int32) (uint64, []*tpb.SignedKV, error) {
	if startSequence > uint64(len(m.mtns)) {
		panic("startSequence > len(m.mtns)")
	}
	// Adjust endSequence.
	if endSequence-startSequence > uint64(count) {
		endSequence = startSequence + uint64(count)
	}
	if endSequence > uint64(len(m.mtns)) {
		endSequence = uint64(len(m.mtns))
	}
	return endSequence, m.mtns[startSequence:endSequence], nil
}

func (m *fakeMutation) ReadAll(txn transaction.Txn, startSequence uint64) (uint64, []*tpb.SignedKV, error) {
	return 0, nil, nil
}

func (m *fakeMutation) Write(txn transaction.Txn, mutation *tpb.SignedKV) (uint64, error) {
	m.mtns = append(m.mtns, mutation)
	return uint64(len(m.mtns)), nil
}
<|endoftext|>


<commit_before>// Package golang includes The runtime, which is just a stub that connects the cocoon code to the connector's RPC
// server. The runtime provides access to APIs for various operations.
package golang

import (
	"fmt"
	"net"
	"os"
	"strconv"
	"time"

	"strings"

	"github.com/ellcrys/util"
	"github.com/ncodes/cocoon/core/common"
	"github.com/ncodes/cocoon/core/runtime/golang/config"
	"github.com/ncodes/cocoon/core/runtime/golang/proto"
	"github.com/ncodes/cocoon/core/types"
	"github.com/op/go-logging"
	cmap "github.com/orcaman/concurrent-map"
	"google.golang.org/grpc"
)

var (

	// serverAddr to bind to
	serverAddr = util.Env("COCOON_ADDR", ":8000")

	// stub logger
	log *logging.Logger

	// default running server
	defaultServer *stubServer

	// stop channel to stop the server/cocoon code
	serverDone chan bool

	// Default runtime link
	defaultLink = NewNativeLink(GetID())

	// txChannels holds the channels to send transaction responses to
	txRespChannels = cmap.New()

	// ErrAlreadyExist represents an error about an already existing resource
	ErrAlreadyExist = fmt.Errorf("already exists")

	// ErrNotConnected represents an error about the cocoon code not
	// having an active connection with the connector.
	ErrNotConnected = fmt.Errorf("not connected to the connector")

	// Flag to help tell whether cocoon code is running
	running = false

	// Number of transactions per block
	txPerBlock = util.Env("TX_PER_BLOCK", "100")

	// Time between block creation (seconds)
	blockCreationInterval = util.Env("BLOCK_CREATION_INT", "5")

	// blockMaker creates a collection of blockchain transactions at interval
	blockMaker *BlockMaker

	// The cocoon code currently running
	ccode CocoonCode
)

// GetLogger returns the stubs logger.
func GetLogger() *logging.Logger {
	return log
}

// SetDebugLevel sets the default logger debug level
func SetDebugLevel(level logging.Level) {
	logging.SetLevel(level, log.Module)
}

func init() {
	defaultServer = new(stubServer)
	config.ConfigureLogger()
	log = logging.MustGetLogger("ccode.runtime")
}

// GetID returns the cocoon id. However, it will return the
// natively linked cocoon id if this cocoon is linked to another
// cocoon.
func GetID() string {
	return util.Env("COCOON_LINK", os.Getenv("COCOON_ID"))
}

// GetCocoonID returns the unique cocoon id
func GetCocoonID() string {
	return os.Getenv("COCOON_ID")
}

// Run starts the stub server, takes a cocoon code and attempts to initialize it..
func Run(cc CocoonCode) {

	if running {
		log.Info("cocoon code is already running")
		return
	}

	go func() {
		for {
			log.Info("Alive!!")
			time.Sleep(10 * time.Second)
		}
	}()

	serverDone = make(chan bool, 1)

	lis, err := net.Listen("tcp", fmt.Sprintf("%s", serverAddr))
	if err != nil {
		log.Fatalf("failed to listen on port=%s", strings.Split(serverAddr, ":")[1])
	}

	log.Infof("Started stub service at port=%s", strings.Split(serverAddr, ":")[1])
	server := grpc.NewServer()
	proto.RegisterStubServer(server, defaultServer)
	go server.Serve(lis)

	intTxPerBlock, _ := strconv.Atoi(txPerBlock)
	intBlkCreationInt, _ := strconv.Atoi(blockCreationInterval)
	blockMaker = NewBlockMaker(intTxPerBlock, time.Duration(intBlkCreationInt)*time.Second)
	go blockMaker.Begin(blockCommitter)

	defaultLink.SetDefaultLedger(types.GetGlobalLedgerName())

	ccode = cc

	// run Init() after 1 second to give time for connector to connect
	time.AfterFunc(1*time.Second, func() {
		if err = cc.OnInit(defaultLink); err != nil {
			log.Errorf("cocoode OnInit() returned error: %s", err)
			Stop(1)
		} else {
			running = true
		}
	})

	<-serverDone
	log.Info("Cocoon code stopped")
	os.Exit(0)
}

// blockCommit creates a PUT operation which adds one or many
// transactions to the store and blockchain and returns the block if
// if succeed or error if otherwise.
func blockCommitter(entries []*Entry) interface{} {

	var block types.Block

	if len(entries) == 0 {
		return block
	}

	txs := make([]*types.Transaction, len(entries))
	for i, e := range entries {
		txs[i] = e.Tx
	}

	ledgerName := entries[0].Tx.Ledger
	txsJSON, _ := util.ToJSON(txs)

	var respCh = make(chan *proto.Tx)

	txID := util.UUID4()
	err := sendTx(&proto.Tx{
		Id:     txID,
		Invoke: true,
		Name:   types.TxPut,
		LinkTo: entries[0].To,
		Params: []string{ledgerName},
		Body:   txsJSON,
	}, respCh)
	if err != nil {
		return fmt.Errorf("failed to put block transaction. %s", err)
	}

	resp, err := common.AwaitTxChan(respCh)
	if err != nil {
		return err
	}

	if resp.Status != 200 {
		return fmt.Errorf("%s", common.GetRPCErrDesc(fmt.Errorf("%s", resp.Body)))
	}

	if err = util.FromJSON(resp.Body, &block); err != nil {
		return fmt.Errorf("failed to unmarshall response data")
	}

	return &block
}

// sendTx sends a transaction to the cocoon code
// and saves the response channel. The response channel will
// be passed a response when it is available in the Transact loop.
func sendTx(tx *proto.Tx, respCh chan *proto.Tx) error {
	txRespChannels.Set(tx.GetId(), respCh)
	if err := defaultServer.stream.Send(tx); err != nil {
		txRespChannels.Remove(tx.GetId())
		log.Errorf("failed to send transaction [%s] to connector. %s", tx.GetId(), err)
		return err
	}
	log.Debugf("Successfully sent transaction [%s] to connector", tx.GetId())
	return nil
}

// Stop stub and cocoon code
func Stop(exitCode int) {
	if blockMaker != nil {
		blockMaker.Stop()
	}
	defaultServer.stream = nil
	serverDone <- true
	log.Info("Cocoon code exiting with exit code %d", exitCode)
	os.Exit(exitCode)
}

// isConnected checks if connection with the connector
// is active.
func isConnected() bool {
	return defaultServer.stream != nil
}

// GetGlobalLedger returns the name of the global ledger.
func GetGlobalLedger() string {
	return types.GetGlobalLedgerName()
}
<commit_msg>Restart server when stopped unexpectedly.<commit_after>// Package golang includes The runtime, which is just a stub that connects the cocoon code to the connector's RPC
// server. The runtime provides access to APIs for various operations.
package golang

import (
	"fmt"
	"net"
	"os"
	"strconv"
	"time"

	"strings"

	"github.com/ellcrys/util"
	"github.com/ncodes/cocoon/core/common"
	"github.com/ncodes/cocoon/core/runtime/golang/config"
	"github.com/ncodes/cocoon/core/runtime/golang/proto"
	"github.com/ncodes/cocoon/core/types"
	"github.com/op/go-logging"
	cmap "github.com/orcaman/concurrent-map"
	"google.golang.org/grpc"
)

var (

	// serverAddr to bind to
	serverAddr = util.Env("COCOON_ADDR", ":8000")

	// stub logger
	log *logging.Logger

	// default running server
	defaultServer *stubServer

	// stop channel to stop the server/cocoon code
	serverDone chan bool

	// Default runtime link
	defaultLink = NewNativeLink(GetID())

	// txChannels holds the channels to send transaction responses to
	txRespChannels = cmap.New()

	// ErrAlreadyExist represents an error about an already existing resource
	ErrAlreadyExist = fmt.Errorf("already exists")

	// ErrNotConnected represents an error about the cocoon code not
	// having an active connection with the connector.
	ErrNotConnected = fmt.Errorf("not connected to the connector")

	// Flag to help tell whether cocoon code is running
	running = false

	// Number of transactions per block
	txPerBlock = util.Env("TX_PER_BLOCK", "100")

	// Time between block creation (seconds)
	blockCreationInterval = util.Env("BLOCK_CREATION_INT", "5")

	// blockMaker creates a collection of blockchain transactions at interval
	blockMaker *BlockMaker

	// The cocoon code currently running
	ccode CocoonCode
)

// GetLogger returns the stubs logger.
func GetLogger() *logging.Logger {
	return log
}

// SetDebugLevel sets the default logger debug level
func SetDebugLevel(level logging.Level) {
	logging.SetLevel(level, log.Module)
}

func init() {
	defaultServer = new(stubServer)
	config.ConfigureLogger()
	log = logging.MustGetLogger("ccode.runtime")
}

// GetID returns the cocoon id. However, it will return the
// natively linked cocoon id if this cocoon is linked to another
// cocoon.
func GetID() string {
	return util.Env("COCOON_LINK", os.Getenv("COCOON_ID"))
}

// GetCocoonID returns the unique cocoon id
func GetCocoonID() string {
	return os.Getenv("COCOON_ID")
}

// Run starts the stub server, takes a cocoon code and attempts to initialize it..
func Run(cc CocoonCode) {

	if running {
		log.Info("cocoon code is already running")
		return
	}

	go func() {
		for {
			log.Info("Alive!!")
			time.Sleep(10 * time.Second)
		}
	}()

	serverDone = make(chan bool, 1)

	lis, err := net.Listen("tcp", fmt.Sprintf("%s", serverAddr))
	if err != nil {
		log.Fatalf("failed to listen on port=%s", strings.Split(serverAddr, ":")[1])
	}

	log.Infof("Started stub service at port=%s", strings.Split(serverAddr, ":")[1])
	server := grpc.NewServer()
	proto.RegisterStubServer(server, defaultServer)

	// start server with the ability to restart
	// when it unexpectedly stops.
	go func() {
		maxRetries := 5
		retries := 1
		for {
			err := server.Serve(lis)
			log.Errorf("Server stopped: %s", err)
			retries++
			if retries == maxRetries {
				log.Errorf("RPC server stopped: %s", err)
				Stop(1)
				break
			}
			log.Info("Restarting server")
		}
	}()

	intTxPerBlock, _ := strconv.Atoi(txPerBlock)
	intBlkCreationInt, _ := strconv.Atoi(blockCreationInterval)
	blockMaker = NewBlockMaker(intTxPerBlock, time.Duration(intBlkCreationInt)*time.Second)
	go blockMaker.Begin(blockCommitter)

	defaultLink.SetDefaultLedger(types.GetGlobalLedgerName())

	ccode = cc

	// run Init() after 1 second to give time for connector to connect
	time.AfterFunc(1*time.Second, func() {
		if err = cc.OnInit(defaultLink); err != nil {
			log.Errorf("cocoode OnInit() returned error: %s", err)
			Stop(1)
		} else {
			running = true
		}
	})

	<-serverDone
	log.Info("Cocoon code stopped")
	os.Exit(0)
}

// blockCommit creates a PUT operation which adds one or many
// transactions to the store and blockchain and returns the block if
// if succeed or error if otherwise.
func blockCommitter(entries []*Entry) interface{} {

	var block types.Block

	if len(entries) == 0 {
		return block
	}

	txs := make([]*types.Transaction, len(entries))
	for i, e := range entries {
		txs[i] = e.Tx
	}

	ledgerName := entries[0].Tx.Ledger
	txsJSON, _ := util.ToJSON(txs)

	var respCh = make(chan *proto.Tx)

	txID := util.UUID4()
	err := sendTx(&proto.Tx{
		Id:     txID,
		Invoke: true,
		Name:   types.TxPut,
		LinkTo: entries[0].To,
		Params: []string{ledgerName},
		Body:   txsJSON,
	}, respCh)
	if err != nil {
		return fmt.Errorf("failed to put block transaction. %s", err)
	}

	resp, err := common.AwaitTxChan(respCh)
	if err != nil {
		return err
	}

	if resp.Status != 200 {
		return fmt.Errorf("%s", common.GetRPCErrDesc(fmt.Errorf("%s", resp.Body)))
	}

	if err = util.FromJSON(resp.Body, &block); err != nil {
		return fmt.Errorf("failed to unmarshall response data")
	}

	return &block
}

// sendTx sends a transaction to the cocoon code
// and saves the response channel. The response channel will
// be passed a response when it is available in the Transact loop.
func sendTx(tx *proto.Tx, respCh chan *proto.Tx) error {
	txRespChannels.Set(tx.GetId(), respCh)
	if err := defaultServer.stream.Send(tx); err != nil {
		txRespChannels.Remove(tx.GetId())
		log.Errorf("failed to send transaction [%s] to connector. %s", tx.GetId(), err)
		return err
	}
	log.Debugf("Successfully sent transaction [%s] to connector", tx.GetId())
	return nil
}

// Stop stub and cocoon code
func Stop(exitCode int) {
	running = false
	if blockMaker != nil {
		blockMaker.Stop()
	}
	defaultServer.stream = nil
	serverDone <- true
	log.Info("Cocoon code exiting with exit code %d", exitCode)
	os.Exit(exitCode)
}

// isConnected checks if connection with the connector
// is active.
func isConnected() bool {
	return defaultServer.stream != nil
}

// GetGlobalLedger returns the name of the global ledger.
func GetGlobalLedger() string {
	return types.GetGlobalLedgerName()
}
<|endoftext|>


<commit_before>package router

import (
	"fmt"
	"io/ioutil"
	"os"

	kv "gopkg.in/Clever/kayvee-go.v5"
)

var (
	appName  string
	teamName string
)

func init() {
	appName = os.Getenv("_APP_NAME")
	if appName == "" {
		appName = "UNSET"
	}
	teamName = os.Getenv("_TEAM_OWNER")
	if teamName == "" {
		teamName = "UNSET"
	}
}

// Route returns routing metadata for the log line `msg`. The outputs (with
// variable substitutions performed) for each rule matched are placed under the
// "routes" key.
func (r *RuleRouter) Route(msg map[string]interface{}) map[string]interface{} {
	outputs := []map[string]interface{}{}
	for _, rule := range r.rules {
		if rule.Matches(msg) {
			outputs = append(outputs, rule.OutputFor(msg))
		}
	}
	return map[string]interface{}{
		"app":         appName,
		"team":        teamName,
		"kv_version":  kv.Version,
		"kv_language": "go",
		"routes":      outputs,
	}
}

// NewFromConfig constructs a Router using the configuration specified as yaml
// in `filename`. The routing rules should be placed under the "routes" key on
// the root-level map in the file. Validation is performed as described in
// parse.go.
func NewFromConfig(filename string) (Router, error) {
	file, err := os.Open(filename)
	if err != nil {
		return nil, err
	}
	defer file.Close()

	fileBytes, err := ioutil.ReadAll(file)
	if err != nil {
		return nil, err
	}

	router, err := newFromConfigBytes(fileBytes)
	if err != nil {
		return nil, fmt.Errorf(
			"Error initializing kayvee log router from file '%s':\n%s",
			filename, err.Error(),
		)
	}
	return router, nil
}

func newFromConfigBytes(fileBytes []byte) (Router, error) {
	routes, err := parse(fileBytes)
	if err != nil {
		return &RuleRouter{}, err
	}

	return NewFromRoutes(routes)
}

// NewFromRoutes constructs a RuleRouter using the provided map of route names
// to Rules.
func NewFromRoutes(routes map[string]Rule) (Router, error) {
	router := &RuleRouter{}
	for name, rule := range routes {
		output, err := substituteEnvVars(rule.Output)
		if err != nil {
			return router, err
		}

		rule.Name = name
		rule.Output = output
		router.rules = append(router.rules, rule)
	}

	return router, nil
}
<commit_msg>Removed appname from kayvee metadata<commit_after>package router

import (
	"fmt"
	"io/ioutil"
	"os"

	kv "gopkg.in/Clever/kayvee-go.v5"
)

var teamName string

func init() {
	teamName = os.Getenv("_TEAM_OWNER")
	if teamName == "" {
		teamName = "UNSET"
	}
}

// Route returns routing metadata for the log line `msg`. The outputs (with
// variable substitutions performed) for each rule matched are placed under the
// "routes" key.
func (r *RuleRouter) Route(msg map[string]interface{}) map[string]interface{} {
	outputs := []map[string]interface{}{}
	for _, rule := range r.rules {
		if rule.Matches(msg) {
			outputs = append(outputs, rule.OutputFor(msg))
		}
	}
	return map[string]interface{}{
		"team":        teamName,
		"kv_version":  kv.Version,
		"kv_language": "go",
		"routes":      outputs,
	}
}

// NewFromConfig constructs a Router using the configuration specified as yaml
// in `filename`. The routing rules should be placed under the "routes" key on
// the root-level map in the file. Validation is performed as described in
// parse.go.
func NewFromConfig(filename string) (Router, error) {
	file, err := os.Open(filename)
	if err != nil {
		return nil, err
	}
	defer file.Close()

	fileBytes, err := ioutil.ReadAll(file)
	if err != nil {
		return nil, err
	}

	router, err := newFromConfigBytes(fileBytes)
	if err != nil {
		return nil, fmt.Errorf(
			"Error initializing kayvee log router from file '%s':\n%s",
			filename, err.Error(),
		)
	}
	return router, nil
}

func newFromConfigBytes(fileBytes []byte) (Router, error) {
	routes, err := parse(fileBytes)
	if err != nil {
		return &RuleRouter{}, err
	}

	return NewFromRoutes(routes)
}

// NewFromRoutes constructs a RuleRouter using the provided map of route names
// to Rules.
func NewFromRoutes(routes map[string]Rule) (Router, error) {
	router := &RuleRouter{}
	for name, rule := range routes {
		output, err := substituteEnvVars(rule.Output)
		if err != nil {
			return router, err
		}

		rule.Name = name
		rule.Output = output
		router.rules = append(router.rules, rule)
	}

	return router, nil
}
<|endoftext|>


<commit_before>// Copyright 2012 Aaron Jacobs. All Rights Reserved.
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
//
// 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 blob

import (
	"crypto/sha1"
	"fmt"
)

// A Score is the identifier for a blob previously stored by a blob store. It
// consists of a 20-byte SHA-1 hash of the blob's contents, so that with high
// probability two blobs have the same contents if and only if they have the
// same score.
type Score []byte

// Compute the score for the supplied blob. This is primarily intended for use
// by blob store implementations; users should obtain only scores through calls
// to a store's Store method.
func ComputeScore(b []byte) Score {
	h := sha1.New()
	h.Write(b)
	return &score{hash: h.Sum(nil)}
}

// Return a fixed-width hex version of the score's hash, suitable for using
// e.g. as a filename.
func HexScore(score Score) string {
	return fmt.Sprintf("%x", score.Sha1Hash())
}
<commit_msg>Made ./blob build again.<commit_after>// Copyright 2012 Aaron Jacobs. All Rights Reserved.
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
//
// 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 blob

import (
	"crypto/sha1"
	"fmt"
)

// A Score is the identifier for a blob previously stored by a blob store. It
// consists of a 20-byte SHA-1 hash of the blob's contents, so that with high
// probability two blobs have the same contents if and only if they have the
// same score.
type Score []byte

// Compute the score for the supplied blob. This is primarily intended for use
// by blob store implementations; most users should obtain scores through calls
// to a blob store's Store method.
func ComputeScore(b []byte) Score {
	h := sha1.New()
	h.Write(b)
	return Score(h.Sum(nil))
}

// Return a fixed-width hex version of the score's hash, suitable for using
// e.g. as a filename.
func (s Score) Hex() string {
	return fmt.Sprintf("%x", s)
}
<|endoftext|>


<commit_before>package rpc

import (
	"bufio"
	"io"
	"net"
	"sync"

	"github.com/keybase/go-codec/codec"
)

type WrapErrorFunc func(error) interface{}

type Transporter interface {
	// IsConnected returns false when incoming packets have
	// finished processing.
	//
	// TODO: Use a better name.
	IsConnected() bool

	registerProtocol(p Protocol) error

	getDispatcher() (dispatcher, error)
	getReceiver() (receiver, error)

	// receiveFrames starts processing incoming frames in a
	// background goroutine, if it's not already happening.
	// Returns the result of done(), for convenience.
	receiveFrames() <-chan struct{}

	// Returns a channel that's closed when incoming frames have
	// finished processing, either due to an error or the
	// underlying connection being closed. Successive calls to
	// done() return the same value.
	done() <-chan struct{}

	// err returns a non-nil error value after done() is closed.
	// After done() is closed, successive calls to err return the
	// same value.
	err() error

	// Close closes the transport and releases resources.
	Close()
}

type connDecoder struct {
	decoder
	net.Conn
	Reader *bufio.Reader
}

func newConnDecoder(c net.Conn) *connDecoder {
	br := bufio.NewReader(c)
	mh := &codec.MsgpackHandle{WriteExt: true}

	return &connDecoder{
		Conn:    c,
		Reader:  br,
		decoder: codec.NewDecoder(br, mh),
	}
}

var _ Transporter = (*transport)(nil)

type transport struct {
	cdec       *connDecoder
	enc        *framedMsgpackEncoder
	dispatcher dispatcher
	receiver   receiver
	packetizer packetizer
	protocols  *protocolHandler
	calls      *callContainer
	log        LogInterface
	closeOnce  *sync.Once
	startOnce  *sync.Once
	stopCh     chan struct{}

	// Filled in right before stopCh is closed.
	stopErr error
}

func NewTransport(c net.Conn, l LogFactory, wef WrapErrorFunc) Transporter {
	cdec := newConnDecoder(c)
	if l == nil {
		l = NewSimpleLogFactory(nil, nil)
	}
	log := l.NewLog(cdec.RemoteAddr())

	ret := &transport{
		cdec:      cdec,
		log:       log,
		closeOnce: &sync.Once{},
		startOnce: &sync.Once{},
		stopCh:    make(chan struct{}),
		protocols: newProtocolHandler(wef),
		calls:     newCallContainer(),
	}
	enc := newFramedMsgpackEncoder(ret.cdec)
	ret.enc = enc
	ret.dispatcher = newDispatch(enc, ret.calls, log)
	ret.receiver = newReceiveHandler(enc, ret.protocols, log)
	ret.packetizer = newPacketHandler(cdec.Reader, ret.protocols, ret.calls)
	return ret
}

func (t *transport) Close() {
	t.closeOnce.Do(func() {
		// Since the receiver might require the transport, we have to
		// close it before terminating our loops
		close(t.stopCh)
		t.dispatcher.Close()
		<-t.receiver.Close()

		// First inform the encoder that it should close
		encoderClosed := t.enc.Close()
		// Unblock any remaining writes
		t.cdec.Close()
		// Wait for the encoder to finish handling the now unblocked writes
		<-encoderClosed
	})
}

func (t *transport) IsConnected() bool {
	select {
	case <-t.stopCh:
		return false
	default:
		return true
	}
}

func (t *transport) receiveFrames() <-chan struct{} {
	t.startOnce.Do(func() {
		go t.receiveFramesLoop()
	})
	return t.stopCh
}

func (t *transport) done() <-chan struct{} {
	return t.stopCh
}

func (t *transport) err() error {
	select {
	case <-t.stopCh:
		return t.stopErr
	default:
		return nil
	}
}

func (t *transport) receiveFramesLoop() {
	// Packetize: do work
	var err error
	for shouldContinue(err) {
		var rpc rpcMessage
		if rpc, err = t.packetizer.NextFrame(); shouldReceive(rpc) {
			t.receiver.Receive(rpc)
		}
	}

	// Log packetizer completion
	t.log.TransportError(err)

	// This must happen before stopCh is closed to have a correct
	// ordering.
	t.stopErr = err

	t.Close()
}

func (t *transport) getDispatcher() (dispatcher, error) {
	if !t.IsConnected() {
		return nil, io.EOF
	}
	return t.dispatcher, nil
}

func (t *transport) getReceiver() (receiver, error) {
	if !t.IsConnected() {
		return nil, io.EOF
	}
	return t.receiver, nil
}

func (t *transport) registerProtocol(p Protocol) error {
	return t.protocols.registerProtocol(p)
}

func shouldContinue(err error) bool {
	err = unboxRPCError(err)
	switch err.(type) {
	case nil:
		return true
	case CallNotFoundError:
		return true
	case MethodNotFoundError:
		return true
	case ProtocolNotFoundError:
		return true
	default:
		return false
	}
}

func shouldReceive(rpc rpcMessage) bool {
	if rpc == nil {
		return false
	}
	switch rpc.Err().(type) {
	case nil:
		return true
	case MethodNotFoundError:
		return true
	case ProtocolNotFoundError:
		return true
	default:
		return false
	}
}
<commit_msg>review feedback from jzila<commit_after>package rpc

import (
	"bufio"
	"io"
	"net"
	"sync"

	"github.com/keybase/go-codec/codec"
)

type WrapErrorFunc func(error) interface{}

type Transporter interface {
	// IsConnected returns false when incoming packets have
	// finished processing.
	//
	// TODO: Use a better name.
	IsConnected() bool

	registerProtocol(p Protocol) error

	getDispatcher() (dispatcher, error)
	getReceiver() (receiver, error)

	// receiveFrames starts processing incoming frames in a
	// background goroutine, if it's not already happening.
	// Returns the result of done(), for convenience.
	receiveFrames() <-chan struct{}

	// Returns a channel that's closed when incoming frames have
	// finished processing, either due to an error or the
	// underlying connection being closed. Successive calls to
	// done() return the same value.
	done() <-chan struct{}

	// err returns a non-nil error value after done() is closed.
	// After done() is closed, successive calls to err return the
	// same value.
	err() error

	// Close closes the transport and releases resources.
	Close()
}

type connDecoder struct {
	decoder
	net.Conn
	Reader *bufio.Reader
}

func newConnDecoder(c net.Conn) *connDecoder {
	br := bufio.NewReader(c)
	mh := &codec.MsgpackHandle{WriteExt: true}

	return &connDecoder{
		Conn:    c,
		Reader:  br,
		decoder: codec.NewDecoder(br, mh),
	}
}

var _ Transporter = (*transport)(nil)

type transport struct {
	cdec       *connDecoder
	enc        *framedMsgpackEncoder
	dispatcher dispatcher
	receiver   receiver
	packetizer packetizer
	protocols  *protocolHandler
	calls      *callContainer
	log        LogInterface
	closeOnce  sync.Once
	startOnce  sync.Once
	stopCh     chan struct{}

	// Filled in right before stopCh is closed.
	stopErr error
}

func NewTransport(c net.Conn, l LogFactory, wef WrapErrorFunc) Transporter {
	cdec := newConnDecoder(c)
	if l == nil {
		l = NewSimpleLogFactory(nil, nil)
	}
	log := l.NewLog(cdec.RemoteAddr())

	ret := &transport{
		cdec:      cdec,
		log:       log,
		stopCh:    make(chan struct{}),
		protocols: newProtocolHandler(wef),
		calls:     newCallContainer(),
	}
	enc := newFramedMsgpackEncoder(ret.cdec)
	ret.enc = enc
	ret.dispatcher = newDispatch(enc, ret.calls, log)
	ret.receiver = newReceiveHandler(enc, ret.protocols, log)
	ret.packetizer = newPacketHandler(cdec.Reader, ret.protocols, ret.calls)
	return ret
}

func (t *transport) Close() {
	t.closeOnce.Do(func() {
		// Since the receiver might require the transport, we have to
		// close it before terminating our loops
		close(t.stopCh)
		t.dispatcher.Close()
		<-t.receiver.Close()

		// First inform the encoder that it should close
		encoderClosed := t.enc.Close()
		// Unblock any remaining writes
		t.cdec.Close()
		// Wait for the encoder to finish handling the now unblocked writes
		<-encoderClosed
	})
}

func (t *transport) IsConnected() bool {
	select {
	case <-t.stopCh:
		return false
	default:
		return true
	}
}

func (t *transport) receiveFrames() <-chan struct{} {
	t.startOnce.Do(func() {
		go t.receiveFramesLoop()
	})
	return t.stopCh
}

func (t *transport) done() <-chan struct{} {
	return t.stopCh
}

func (t *transport) err() error {
	select {
	case <-t.stopCh:
		return t.stopErr
	default:
		return nil
	}
}

func (t *transport) receiveFramesLoop() {
	// Packetize: do work
	var err error
	for shouldContinue(err) {
		var rpc rpcMessage
		if rpc, err = t.packetizer.NextFrame(); shouldReceive(rpc) {
			t.receiver.Receive(rpc)
		}
	}

	// Log packetizer completion
	t.log.TransportError(err)

	// This must happen before stopCh is closed to have a correct
	// ordering.
	t.stopErr = err

	t.Close()
}

func (t *transport) getDispatcher() (dispatcher, error) {
	if !t.IsConnected() {
		return nil, io.EOF
	}
	return t.dispatcher, nil
}

func (t *transport) getReceiver() (receiver, error) {
	if !t.IsConnected() {
		return nil, io.EOF
	}
	return t.receiver, nil
}

func (t *transport) registerProtocol(p Protocol) error {
	return t.protocols.registerProtocol(p)
}

func shouldContinue(err error) bool {
	err = unboxRPCError(err)
	switch err.(type) {
	case nil:
		return true
	case CallNotFoundError:
		return true
	case MethodNotFoundError:
		return true
	case ProtocolNotFoundError:
		return true
	default:
		return false
	}
}

func shouldReceive(rpc rpcMessage) bool {
	if rpc == nil {
		return false
	}
	switch rpc.Err().(type) {
	case nil:
		return true
	case MethodNotFoundError:
		return true
	case ProtocolNotFoundError:
		return true
	default:
		return false
	}
}
<|endoftext|>