<commit_before>package wav

import (
	"encoding/binary"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"time"

	"github.com/go-audio/audio"
	"github.com/mattetti/audio/riff"
)

// Decoder handles the decoding of wav files.
type Decoder struct {
	r      io.ReadSeeker
	parser *riff.Parser

	NumChans   uint16
	BitDepth   uint16
	SampleRate uint32

	AvgBytesPerSec uint32
	WavAudioFormat uint16

	err             error
	PCMSize         int
	pcmDataAccessed bool
	// pcmChunk is available so we can use the LimitReader
	PCMChunk *riff.Chunk
}

// NewDecoder creates a decoder for the passed wav reader.
// Note that the reader doesn't get rewinded as the container is processed.
func NewDecoder(r io.ReadSeeker) *Decoder {
	return &Decoder{
		r:      r,
		parser: riff.New(r),
	}
}

// SampleBitDepth returns the bit depth encoding of each sample.
func (d *Decoder) SampleBitDepth() int32 {
	if d == nil {
		return 0
	}
	return int32(d.BitDepth)
}

// PCMLen returns the total number of bytes in the PCM data chunk
func (d *Decoder) PCMLen() int64 {
	if d == nil {
		return 0
	}
	return int64(d.PCMSize)
}

// Err returns the first non-EOF error that was encountered by the Decoder.
func (d *Decoder) Err() error {
	if d.err == io.EOF {
		return nil
	}
	return d.err
}

// EOF returns positively if the underlying reader reached the end of file.
func (d *Decoder) EOF() bool {
	if d == nil || d.err == io.EOF {
		return true
	}
	return false
}

// IsValidFile verifies that the file is valid/readable.
func (d *Decoder) IsValidFile() bool {
	d.err = d.readHeaders()
	if d.err != nil {
		return false
	}
	if d.NumChans < 1 {
		return false
	}
	if d.BitDepth < 8 {
		return false
	}
	if d, err := d.Duration(); err != nil || d <= 0 {
		return false
	}

	return true
}

// ReadInfo reads the underlying reader until the comm header is parsed.
// This method is safe to call multiple times.
func (d *Decoder) ReadInfo() {
	d.err = d.readHeaders()
}

// Reset resets the decoder (and rewind the underlying reader)
func (d *Decoder) Reset() {
	d.err = nil
	d.pcmDataAccessed = false
	d.NumChans = 0
	d.BitDepth = 0
	d.SampleRate = 0
	d.AvgBytesPerSec = 0
	d.WavAudioFormat = 0
	d.PCMSize = 0
	d.r.Seek(0, 0)
	d.PCMChunk = nil
	d.parser = riff.New(d.r)
}

// FwdToPCM forwards the underlying reader until the start of the PCM chunk.
// If the PCM chunk was already read, no data will be found (you need to rewind).
func (d *Decoder) FwdToPCM() error {
	if d == nil {
		return fmt.Errorf("PCM data not found")
	}
	d.err = d.readHeaders()
	if d.err != nil {
		return nil
	}

	var chunk *riff.Chunk
	for d.err == nil {
		chunk, d.err = d.NextChunk()
		if d.err != nil {
			return d.err
		}
		if chunk.ID == riff.DataFormatID {
			d.PCMSize = chunk.Size
			d.PCMChunk = chunk
			break
		}
		chunk.Drain()
	}
	if chunk == nil {
		return fmt.Errorf("PCM data not found")
	}
	d.pcmDataAccessed = true

	return nil
}

// WasPCMAccessed returns positively if the PCM data was previously accessed.
func (d *Decoder) WasPCMAccessed() bool {
	if d == nil {
		return false
	}
	return d.pcmDataAccessed
}

// FullPCMBuffer is an inneficient way to access all the PCM data contained in the
// audio container. The entire PCM data is held in memory.
// Consider using Buffer() instead.
func (d *Decoder) FullPCMBuffer() (*audio.IntBuffer, error) {
	if !d.WasPCMAccessed() {
		err := d.FwdToPCM()
		if err != nil {
			return nil, d.err
		}
	}
	if d.PCMChunk == nil {
		return nil, errors.New("PCM chunk not found")
	}
	format := &audio.Format{
		NumChannels: int(d.NumChans),
		SampleRate:  int(d.SampleRate),
	}

	buf := &audio.IntBuffer{Data: make([]int, 4096), Format: format, SourceBitDepth: int(d.BitDepth)}
	bytesPerSample := (d.BitDepth-1)/8 + 1
	sampleBufData := make([]byte, bytesPerSample)
	decodeF, err := sampleDecodeFunc(int(d.BitDepth))
	if err != nil {
		return nil, fmt.Errorf("could not get sample decode func %v", err)
	}

	i := 0
	for err == nil {
		_, err = d.PCMChunk.Read(sampleBufData)
		if err != nil {
			break
		}
		buf.Data[i] = decodeF(sampleBufData)
		i++
		// grow the underlying slice if needed
		if i == len(buf.Data) {
			buf.Data = append(buf.Data, make([]int, 4096)...)
		}
	}
	buf.Data = buf.Data[:i]

	if err == io.EOF {
		err = nil
	}

	return buf, err
}

// PCMBuffer populates the passed PCM buffer
func (d *Decoder) PCMBuffer(buf *audio.IntBuffer) (n int, err error) {
	if buf == nil {
		return 0, nil
	}

	if !d.pcmDataAccessed {
		err := d.FwdToPCM()
		if err != nil {
			return 0, d.err
		}
	}

	bytesPerSample := (d.BitDepth-1)/8 + 1
	sampleBufData := make([]byte, bytesPerSample)
	decodeF, err := sampleDecodeFunc(int(d.BitDepth))
	if err != nil {
		return 0, fmt.Errorf("could not get sample decode func %v", err)
	}

	// Note that we populate the buffer even if the
	// size of the buffer doesn't fit an even number of frames.
	for n = 0; n < len(buf.Data); n++ {
		_, err = d.PCMChunk.Read(sampleBufData)
		if err != nil {
			break
		}
		buf.Data[n] = decodeF(sampleBufData)
	}
	if err == io.EOF {
		err = nil
	}
	buf.Format = d.Format()
	buf.SourceBitDepth = int(d.BitDepth)

	return n, err
}

// Format returns the audio format of the decoded content.
func (d *Decoder) Format() *audio.Format {
	if d == nil {
		return nil
	}
	return &audio.Format{
		NumChannels: int(d.NumChans),
		SampleRate:  int(d.SampleRate),
	}
}

// NextChunk returns the next available chunk
func (d *Decoder) NextChunk() (*riff.Chunk, error) {
	if d.err = d.readHeaders(); d.err != nil {
		d.err = fmt.Errorf("failed to read header - %v", d.err)
		return nil, d.err
	}

	var (
		id   [4]byte
		size uint32
	)

	id, size, d.err = d.parser.IDnSize()
	if d.err != nil {
		d.err = fmt.Errorf("error reading chunk header - %v", d.err)
		return nil, d.err
	}

	c := &riff.Chunk{
		ID:   id,
		Size: int(size),
		R:    io.LimitReader(d.r, int64(size)),
	}
	return c, d.err
}

// Duration returns the time duration for the current audio container
func (d *Decoder) Duration() (time.Duration, error) {
	if d == nil || d.parser == nil {
		return 0, errors.New("can't calculate the duration of a nil pointer")
	}
	return d.parser.Duration()
}

// String implements the Stringer interface.
func (d *Decoder) String() string {
	return d.parser.String()
}

// readHeaders is safe to call multiple times
func (d *Decoder) readHeaders() error {
	if d == nil || d.NumChans > 0 {
		return nil
	}

	id, size, err := d.parser.IDnSize()
	if err != nil {
		return err
	}
	d.parser.ID = id
	if d.parser.ID != riff.RiffID {
		return fmt.Errorf("%s - %s", d.parser.ID, riff.ErrFmtNotSupported)
	}
	d.parser.Size = size
	if err := binary.Read(d.r, binary.BigEndian, &d.parser.Format); err != nil {
		return err
	}

	var chunk *riff.Chunk
	var rewindBytes int64

	for err == nil {
		chunk, err = d.parser.NextChunk()
		if err != nil {
			break
		}

		if chunk.ID == riff.FmtID {
			chunk.DecodeWavHeader(d.parser)
			d.NumChans = d.parser.NumChannels
			d.BitDepth = d.parser.BitsPerSample
			d.SampleRate = d.parser.SampleRate
			d.WavAudioFormat = d.parser.WavAudioFormat
			d.AvgBytesPerSec = d.parser.AvgBytesPerSec

			if rewindBytes > 0 {
				d.r.Seek(-(rewindBytes + int64(chunk.Size) + 8), 1)
			}
			break
		} else {
			// unexpected chunk order, might be a bext chunk
			rewindBytes += int64(chunk.Size) + 8
			// drain the chunk
			io.CopyN(ioutil.Discard, d.r, int64(chunk.Size))
		}

	}

	return d.err
}

// sampleDecodeFunc returns a function that can be used to convert
// a byte range into an int value based on the amount of bits used per sample.
// Note that 8bit samples are unsigned, all other values are signed.
func sampleDecodeFunc(bitsPerSample int) (func([]byte) int, error) {
	// NOTE: WAV PCM data is stored using little-endian
	switch bitsPerSample {
	case 8:
		// 8bit values are unsigned
		return func(s []byte) int {
			return int(uint8(s[0]))
		}, nil
	case 16:
		// -32,768	(0x7FFF) to	32,767	(0x8000)
		return func(s []byte) int {
			return int(int16(binary.LittleEndian.Uint16(s)))
		}, nil
	case 24:
		// -34,359,738,367 (0x7FFFFF) to 34,359,738,368	(0x800000)
		return func(s []byte) int {
			return int(audio.Int24LETo32(s))
		}, nil
	case 32:
		return func(s []byte) int {
			return int(s[0]) + int(s[1])<<8 + int(s[2])<<16 + int(s[3])<<24
		}, nil
	default:
		return nil, fmt.Errorf("unhandled byte depth:%d", bitsPerSample)
	}
}

// sampleDecodeFloat64Func returns a function that can be used to convert
// a byte range into a float64 value based on the amount of bits used per sample.
func sampleFloat64DecodeFunc(bitsPerSample int) (func([]byte) float64, error) {
	bytesPerSample := bitsPerSample / 8
	switch bytesPerSample {
	case 1:
		// 8bit values are unsigned
		return func(s []byte) float64 {
			return float64(uint8(s[0]))
		}, nil
	case 2:
		return func(s []byte) float64 {
			return float64(int(s[0]) + int(s[1])<<8)
		}, nil
	case 3:
		return func(s []byte) float64 {
			var output int32
			output |= int32(s[2]) << 0
			output |= int32(s[1]) << 8
			output |= int32(s[0]) << 16
			return float64(output)
		}, nil
	case 4:
		// TODO: fix the float64 conversion (current int implementation)
		return func(s []byte) float64 {
			return float64(int(s[0]) + int(s[1])<<8 + int(s[2])<<16 + int(s[3])<<24)
		}, nil
	default:
		return nil, fmt.Errorf("unhandled byte depth:%d", bitsPerSample)
	}
}
<commit_msg>around 20x performance improvement to read PCM buffers (dependending on the size)<commit_after>package wav

import (
	"bytes"
	"encoding/binary"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"time"

	"github.com/go-audio/audio"
	"github.com/mattetti/audio/riff"
)

// Decoder handles the decoding of wav files.
type Decoder struct {
	r      io.ReadSeeker
	parser *riff.Parser

	NumChans   uint16
	BitDepth   uint16
	SampleRate uint32

	AvgBytesPerSec uint32
	WavAudioFormat uint16

	err             error
	PCMSize         int
	pcmDataAccessed bool
	// pcmChunk is available so we can use the LimitReader
	PCMChunk *riff.Chunk
}

// NewDecoder creates a decoder for the passed wav reader.
// Note that the reader doesn't get rewinded as the container is processed.
func NewDecoder(r io.ReadSeeker) *Decoder {
	return &Decoder{
		r:      r,
		parser: riff.New(r),
	}
}

// SampleBitDepth returns the bit depth encoding of each sample.
func (d *Decoder) SampleBitDepth() int32 {
	if d == nil {
		return 0
	}
	return int32(d.BitDepth)
}

// PCMLen returns the total number of bytes in the PCM data chunk
func (d *Decoder) PCMLen() int64 {
	if d == nil {
		return 0
	}
	return int64(d.PCMSize)
}

// Err returns the first non-EOF error that was encountered by the Decoder.
func (d *Decoder) Err() error {
	if d.err == io.EOF {
		return nil
	}
	return d.err
}

// EOF returns positively if the underlying reader reached the end of file.
func (d *Decoder) EOF() bool {
	if d == nil || d.err == io.EOF {
		return true
	}
	return false
}

// IsValidFile verifies that the file is valid/readable.
func (d *Decoder) IsValidFile() bool {
	d.err = d.readHeaders()
	if d.err != nil {
		return false
	}
	if d.NumChans < 1 {
		return false
	}
	if d.BitDepth < 8 {
		return false
	}
	if d, err := d.Duration(); err != nil || d <= 0 {
		return false
	}

	return true
}

// ReadInfo reads the underlying reader until the comm header is parsed.
// This method is safe to call multiple times.
func (d *Decoder) ReadInfo() {
	d.err = d.readHeaders()
}

// Reset resets the decoder (and rewind the underlying reader)
func (d *Decoder) Reset() {
	d.err = nil
	d.pcmDataAccessed = false
	d.NumChans = 0
	d.BitDepth = 0
	d.SampleRate = 0
	d.AvgBytesPerSec = 0
	d.WavAudioFormat = 0
	d.PCMSize = 0
	d.r.Seek(0, 0)
	d.PCMChunk = nil
	d.parser = riff.New(d.r)
}

// FwdToPCM forwards the underlying reader until the start of the PCM chunk.
// If the PCM chunk was already read, no data will be found (you need to rewind).
func (d *Decoder) FwdToPCM() error {
	if d == nil {
		return fmt.Errorf("PCM data not found")
	}
	d.err = d.readHeaders()
	if d.err != nil {
		return nil
	}

	var chunk *riff.Chunk
	for d.err == nil {
		chunk, d.err = d.NextChunk()
		if d.err != nil {
			return d.err
		}
		if chunk.ID == riff.DataFormatID {
			d.PCMSize = chunk.Size
			d.PCMChunk = chunk
			break
		}
		chunk.Drain()
	}
	if chunk == nil {
		return fmt.Errorf("PCM data not found")
	}
	d.pcmDataAccessed = true

	return nil
}

// WasPCMAccessed returns positively if the PCM data was previously accessed.
func (d *Decoder) WasPCMAccessed() bool {
	if d == nil {
		return false
	}
	return d.pcmDataAccessed
}

// FullPCMBuffer is an inneficient way to access all the PCM data contained in the
// audio container. The entire PCM data is held in memory.
// Consider using Buffer() instead.
func (d *Decoder) FullPCMBuffer() (*audio.IntBuffer, error) {
	if !d.WasPCMAccessed() {
		err := d.FwdToPCM()
		if err != nil {
			return nil, d.err
		}
	}
	if d.PCMChunk == nil {
		return nil, errors.New("PCM chunk not found")
	}
	format := &audio.Format{
		NumChannels: int(d.NumChans),
		SampleRate:  int(d.SampleRate),
	}

	buf := &audio.IntBuffer{Data: make([]int, 4096), Format: format, SourceBitDepth: int(d.BitDepth)}
	bytesPerSample := (d.BitDepth-1)/8 + 1
	sampleBufData := make([]byte, bytesPerSample)
	decodeF, err := sampleDecodeFunc(int(d.BitDepth))
	if err != nil {
		return nil, fmt.Errorf("could not get sample decode func %v", err)
	}

	i := 0
	for err == nil {
		buf.Data[i], err = decodeF(d.PCMChunk, sampleBufData)
		if err != nil {
			break
		}
		i++
		// grow the underlying slice if needed
		if i == len(buf.Data) {
			buf.Data = append(buf.Data, make([]int, 4096)...)
		}
	}
	buf.Data = buf.Data[:i]

	if err == io.EOF {
		err = nil
	}

	return buf, err
}

// PCMBuffer populates the passed PCM buffer
func (d *Decoder) PCMBuffer(buf *audio.IntBuffer) (n int, err error) {
	if buf == nil {
		return 0, nil
	}

	if !d.pcmDataAccessed {
		err := d.FwdToPCM()
		if err != nil {
			return 0, d.err
		}
	}

	format := &audio.Format{
		NumChannels: int(d.NumChans),
		SampleRate:  int(d.SampleRate),
	}

	buf.SourceBitDepth = int(d.BitDepth)
	decodeF, err := sampleDecodeFunc(int(d.BitDepth))
	if err != nil {
		return 0, fmt.Errorf("could not get sample decode func %v", err)
	}

	// populate a file buffer to avoid multiple very small reads
	// we need to cap the buffer size to not be bigger than the pcm chunk.
	size := len(buf.Data) * (int(d.BitDepth) / 8)
	tmpBuf := make([]byte, size)
	var m int
	m, err = d.PCMChunk.R.Read(tmpBuf)
	if err != nil {
		if err == io.EOF {
			return m, nil
		}
		return m, err
	}
	if m == 0 {
		return m, nil
	}
	bufR := bytes.NewReader(tmpBuf[:m])
	sampleBuf := make([]byte, 4, 4)

	// Note that we populate the buffer even if the
	// size of the buffer doesn't fit an even number of frames.
	for n = 0; n < len(buf.Data); n++ {
		buf.Data[n], err = decodeF(bufR, sampleBuf)
		if err != nil {
			break
		}
	}
	buf.Format = format
	if err == io.EOF {
		err = nil
	}

	return n, err
}

// Format returns the audio format of the decoded content.
func (d *Decoder) Format() *audio.Format {
	if d == nil {
		return nil
	}
	return &audio.Format{
		NumChannels: int(d.NumChans),
		SampleRate:  int(d.SampleRate),
	}
}

// NextChunk returns the next available chunk
func (d *Decoder) NextChunk() (*riff.Chunk, error) {
	if d.err = d.readHeaders(); d.err != nil {
		d.err = fmt.Errorf("failed to read header - %v", d.err)
		return nil, d.err
	}

	var (
		id   [4]byte
		size uint32
	)

	id, size, d.err = d.parser.IDnSize()
	if d.err != nil {
		d.err = fmt.Errorf("error reading chunk header - %v", d.err)
		return nil, d.err
	}

	c := &riff.Chunk{
		ID:   id,
		Size: int(size),
		R:    io.LimitReader(d.r, int64(size)),
	}
	return c, d.err
}

// Duration returns the time duration for the current audio container
func (d *Decoder) Duration() (time.Duration, error) {
	if d == nil || d.parser == nil {
		return 0, errors.New("can't calculate the duration of a nil pointer")
	}
	return d.parser.Duration()
}

// String implements the Stringer interface.
func (d *Decoder) String() string {
	return d.parser.String()
}

// readHeaders is safe to call multiple times
func (d *Decoder) readHeaders() error {
	if d == nil || d.NumChans > 0 {
		return nil
	}

	id, size, err := d.parser.IDnSize()
	if err != nil {
		return err
	}
	d.parser.ID = id
	if d.parser.ID != riff.RiffID {
		return fmt.Errorf("%s - %s", d.parser.ID, riff.ErrFmtNotSupported)
	}
	d.parser.Size = size
	if err := binary.Read(d.r, binary.BigEndian, &d.parser.Format); err != nil {
		return err
	}

	var chunk *riff.Chunk
	var rewindBytes int64

	for err == nil {
		chunk, err = d.parser.NextChunk()
		if err != nil {
			break
		}

		if chunk.ID == riff.FmtID {
			chunk.DecodeWavHeader(d.parser)
			d.NumChans = d.parser.NumChannels
			d.BitDepth = d.parser.BitsPerSample
			d.SampleRate = d.parser.SampleRate
			d.WavAudioFormat = d.parser.WavAudioFormat
			d.AvgBytesPerSec = d.parser.AvgBytesPerSec

			if rewindBytes > 0 {
				d.r.Seek(-(rewindBytes + int64(chunk.Size) + 8), 1)
			}
			break
		} else {
			// unexpected chunk order, might be a bext chunk
			rewindBytes += int64(chunk.Size) + 8
			// drain the chunk
			io.CopyN(ioutil.Discard, d.r, int64(chunk.Size))
		}

	}

	return d.err
}

// sampleDecodeFunc returns a function that can be used to convert
// a byte range into an int value based on the amount of bits used per sample.
// Note that 8bit samples are unsigned, all other values are signed.
func sampleDecodeFunc(bitsPerSample int) (func(io.Reader, []byte) (int, error), error) {
	// NOTE: WAV PCM data is stored using little-endian
	switch bitsPerSample {
	case 8:
		// 8bit values are unsigned
		return func(r io.Reader, buf []byte) (int, error) {
			_, err := r.Read(buf[:1])
			return int(buf[0]), err
		}, nil
	case 16:
		return func(r io.Reader, buf []byte) (int, error) {
			_, err := r.Read(buf[:2])
			return int(int16(binary.LittleEndian.Uint16(buf[:2]))), err
		}, nil
	case 24:
		// -34,359,738,367 (0x7FFFFF) to 34,359,738,368	(0x800000)
		return func(r io.Reader, buf []byte) (int, error) {
			_, err := r.Read(buf[:3])
			if err != nil {
				return 0, err
			}
			return int(audio.Int24LETo32(buf[:3])), nil
		}, nil
	case 32:
		return func(r io.Reader, buf []byte) (int, error) {
			_, err := r.Read(buf[:4])
			return int(int32(binary.LittleEndian.Uint32(buf[:4]))), err
		}, nil
	default:
		return nil, fmt.Errorf("unhandled byte depth:%d", bitsPerSample)
	}
}

// sampleDecodeFloat64Func returns a function that can be used to convert
// a byte range into a float64 value based on the amount of bits used per sample.
func sampleFloat64DecodeFunc(bitsPerSample int) (func([]byte) float64, error) {
	bytesPerSample := bitsPerSample / 8
	switch bytesPerSample {
	case 1:
		// 8bit values are unsigned
		return func(s []byte) float64 {
			return float64(uint8(s[0]))
		}, nil
	case 2:
		return func(s []byte) float64 {
			return float64(int(s[0]) + int(s[1])<<8)
		}, nil
	case 3:
		return func(s []byte) float64 {
			var output int32
			output |= int32(s[2]) << 0
			output |= int32(s[1]) << 8
			output |= int32(s[0]) << 16
			return float64(output)
		}, nil
	case 4:
		// TODO: fix the float64 conversion (current int implementation)
		return func(s []byte) float64 {
			return float64(int(s[0]) + int(s[1])<<8 + int(s[2])<<16 + int(s[3])<<24)
		}, nil
	default:
		return nil, fmt.Errorf("unhandled byte depth:%d", bitsPerSample)
	}
}
<|endoftext|>


<commit_before>package spherical

import (
	"github.com/twpayne/gogeom/geom"
	"math"
)

func CosineDist(p1, p2 Point) float64 {
	d := math.Sin(p1.Y)*math.Sin(p2.Y) + math.Cos(p1.Y)*math.Cos(p2.Y)*math.Cos(p1.Y-p2.Y)
	if d < 1 {
		return math.Acos(d)
	} else {
		return 0
	}
}

func HaversineDist(p1, p2 Point) float64 {
	halfDeltaX := (p1.X - p2.X) / 2
	halfDeltaY := (p1.Y - p2.Y) / 2
	a := math.Sin(halfDeltaY)*math.Sin(halfDeltaY) + math.Sin(halfDeltaX)*math.Sin(halfDeltaX)*math.Cos(p1.Y)*math.Cos(p2.Y)
	return 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
}
<commit_msg>Remove spherical<commit_after><|endoftext|>


<commit_before>package azureclusterconditions

import (
	"context"

	"github.com/giantswarm/microerror"

	"github.com/giantswarm/azure-operator/v5/service/controller/key"
)

func (r *Resource) EnsureCreated(ctx context.Context, cr interface{}) error {
	var err error
	azureCluster, err := key.ToAzureCluster(cr)
	if err != nil {
		return microerror.Mask(err)
	}

	// ensure Ready condition
	err = r.ensureReadyCondition(ctx, &azureCluster)
	if err != nil {
		return microerror.Mask(err)
	}

	err = r.ctrlClient.Status().Update(ctx, &azureCluster)
	if err != nil {
		return microerror.Mask(err)
	}

	return nil
}
<commit_msg>Don't error when k8s API returns conflict while saving CR status (#1184)<commit_after>package azureclusterconditions

import (
	"context"

	"github.com/giantswarm/microerror"
	apierrors "k8s.io/apimachinery/pkg/api/errors"

	"github.com/giantswarm/azure-operator/v5/service/controller/key"
)

func (r *Resource) EnsureCreated(ctx context.Context, cr interface{}) error {
	var err error
	azureCluster, err := key.ToAzureCluster(cr)
	if err != nil {
		return microerror.Mask(err)
	}

	// ensure Ready condition
	err = r.ensureReadyCondition(ctx, &azureCluster)
	if err != nil {
		return microerror.Mask(err)
	}

	err = r.ctrlClient.Status().Update(ctx, &azureCluster)
	if apierrors.IsConflict(err) {
		r.logger.LogCtx(ctx, "level", "debug", "message", "conflict trying to save object in k8s API concurrently", "stack", microerror.JSON(microerror.Mask(err)))
		r.logger.LogCtx(ctx, "level", "debug", "message", "canceling resource")
		return nil
	} else if err != nil {
		return microerror.Mask(err)
	}

	return nil
}
<|endoftext|>


<commit_before>package logs

import (
	"net/http"
	"strconv"
	"time"

	"github.com/gorilla/websocket"
	"github.com/pivotal-golang/lager"

	"github.com/concourse/atc/auth"
	"github.com/concourse/atc/config"
	"github.com/concourse/atc/db"
	"github.com/concourse/atc/logfanout"
)

var upgrader = websocket.Upgrader{
	CheckOrigin: func(*http.Request) bool {
		return true
	},
}

func NewHandler(
	logger lager.Logger,
	validator auth.Validator,
	jobs config.Jobs,
	tracker *logfanout.Tracker,
	db db.DB,
) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		buildIDStr := r.FormValue(":build_id")

		log := logger.Session("logs-out", lager.Data{
			"build_id": buildIDStr,
		})

		buildID, err := strconv.Atoi(buildIDStr)
		if err != nil {
			log.Error("invalid-build-id", err)
			return
		}

		if !validator.IsAuthenticated(r) {
			build, err := db.GetBuild(buildID)
			if err != nil {
				log.Error("invalid-build-id", err)
				w.WriteHeader(http.StatusNotFound)
				return
			}

			job, found := jobs.Lookup(build.JobName)
			if !found || !job.Public {
				w.WriteHeader(http.StatusNotFound)
				return
			}
		}

		conn, err := upgrader.Upgrade(w, r, nil)
		if err != nil {
			log.Error("upgrade-failed", err)
			return
		}

		defer conn.Close()

		logFanout := tracker.Register(buildID, conn)
		defer tracker.Unregister(buildID, conn)

		err = logFanout.Attach(conn)
		if err != nil {
			log.Error("attach-failed", err)
			conn.Close()
			return
		}

		for {
			time.Sleep(5 * time.Second)

			err := conn.WriteControl(websocket.PingMessage, []byte("ping"), time.Time{})
			if err != nil {
				log.Error("ping-failed", err)
				break
			}
		}
	})
}
<commit_msg>censor often-senstive info out of v1.0 event streams<commit_after>package logs

import (
	"net/http"
	"strconv"
	"time"

	"github.com/gorilla/websocket"
	"github.com/pivotal-golang/lager"

	"github.com/concourse/atc/auth"
	"github.com/concourse/atc/config"
	"github.com/concourse/atc/db"
	"github.com/concourse/atc/logfanout"
)

var upgrader = websocket.Upgrader{
	CheckOrigin: func(*http.Request) bool {
		return true
	},
}

func NewHandler(
	logger lager.Logger,
	validator auth.Validator,
	jobs config.Jobs,
	tracker *logfanout.Tracker,
	db db.DB,
) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		buildIDStr := r.FormValue(":build_id")

		log := logger.Session("logs-out", lager.Data{
			"build_id": buildIDStr,
		})

		buildID, err := strconv.Atoi(buildIDStr)
		if err != nil {
			log.Error("invalid-build-id", err)
			return
		}

		authenticated := validator.IsAuthenticated(r)

		if !authenticated {
			build, err := db.GetBuild(buildID)
			if err != nil {
				log.Error("invalid-build-id", err)
				w.WriteHeader(http.StatusNotFound)
				return
			}

			job, found := jobs.Lookup(build.JobName)
			if !found || !job.Public {
				w.WriteHeader(http.StatusNotFound)
				return
			}
		}

		conn, err := upgrader.Upgrade(w, r, nil)
		if err != nil {
			log.Error("upgrade-failed", err)
			return
		}

		defer conn.Close()

		logFanout := tracker.Register(buildID, conn)
		defer tracker.Unregister(buildID, conn)

		var sink logfanout.Sink
		if authenticated {
			sink = logfanout.NewRawSink(conn)
		} else {
			sink = logfanout.NewCensoredSink(conn)
		}

		err = logFanout.Attach(sink)
		if err != nil {
			log.Error("attach-failed", err)
			conn.Close()
			return
		}

		for {
			time.Sleep(5 * time.Second)

			err := conn.WriteControl(websocket.PingMessage, []byte("ping"), time.Time{})
			if err != nil {
				log.Error("ping-failed", err)
				break
			}
		}
	})
}
<|endoftext|>


<commit_before>package util

import (
	"crypto/tls"
	"crypto/x509"
	"fmt"
	"io/ioutil"
	"net"
	"syscall"
	"time"

	"golang.org/x/sys/unix"

	"github.com/lxc/lxd/shared"
	"github.com/lxc/lxd/shared/logger"
)

// InMemoryNetwork creates a fully in-memory listener and dial function.
//
// Each time the dial function is invoked a new pair of net.Conn objects will
// be created using net.Pipe: the listener's Accept method will unblock and
// return one end of the pipe and the other end will be returned by the dial
// function.
func InMemoryNetwork() (net.Listener, func() net.Conn) {
	listener := &inMemoryListener{
		conns:  make(chan net.Conn, 16),
		closed: make(chan struct{}),
	}
	dialer := func() net.Conn {
		server, client := net.Pipe()
		listener.conns <- server
		return client
	}
	return listener, dialer
}

type inMemoryListener struct {
	conns  chan net.Conn
	closed chan struct{}
}

// Accept waits for and returns the next connection to the listener.
func (l *inMemoryListener) Accept() (net.Conn, error) {
	select {
	case conn := <-l.conns:
		return conn, nil
	case <-l.closed:
		return nil, fmt.Errorf("closed")
	}
}

// Close closes the listener.
// Any blocked Accept operations will be unblocked and return errors.
func (l *inMemoryListener) Close() error {
	close(l.closed)
	return nil
}

// Addr returns the listener's network address.
func (l *inMemoryListener) Addr() net.Addr {
	return &inMemoryAddr{}
}

type inMemoryAddr struct {
}

func (a *inMemoryAddr) Network() string {
	return "memory"
}

func (a *inMemoryAddr) String() string {
	return ""
}

// CanonicalNetworkAddress parses the given network address and returns a string of the form "host:port",
// possibly filling it with the default port if it's missing. It will also wrap a bare IPv6 address with square
// brackets if needed.
func CanonicalNetworkAddress(address string, defaultPort int) string {
	_, _, err := net.SplitHostPort(address)
	if err != nil {
		ip := net.ParseIP(address)
		if ip != nil {
			// If the input address is a bare IP address, then convert it to a proper listen address
			// using the canonical IP with default port and wrap IPv6 addresses in square brackets.
			address = net.JoinHostPort(ip.String(), fmt.Sprintf("%d", defaultPort))
		} else {
			// Otherwise assume this is either a host name or a partial address (e.g `[::]`) without
			// a port number, so append the default port.
			address = fmt.Sprintf("%s:%d", address, defaultPort)
		}
	}

	return address
}

// CanonicalNetworkAddressFromAddressAndPort returns a network address from separate address and port values.
// The address accepts values such as "[::]", "::" and "localhost".
func CanonicalNetworkAddressFromAddressAndPort(address string, port int, defaultPort int) string {
	// Because we accept just the host part of an IPv6 listen address (e.g. `[::]`) don't use net.JoinHostPort.
	// If a bare IP address is supplied then CanonicalNetworkAddress will use net.JoinHostPort if needed.
	return CanonicalNetworkAddress(fmt.Sprintf("%s:%d", address, port), defaultPort)
}

// ServerTLSConfig returns a new server-side tls.Config generated from the give
// certificate info.
func ServerTLSConfig(cert *shared.CertInfo) *tls.Config {
	config := shared.InitTLSConfig()
	config.ClientAuth = tls.RequestClientCert
	config.Certificates = []tls.Certificate{cert.KeyPair()}
	config.NextProtos = []string{"h2"} // Required by gRPC

	if cert.CA() != nil {
		pool := x509.NewCertPool()
		pool.AddCert(cert.CA())
		config.RootCAs = pool
		config.ClientCAs = pool

		logger.Infof("LXD is in CA mode, only CA-signed certificates will be allowed")
	}

	config.BuildNameToCertificate()
	return config
}

// NetworkInterfaceAddress returns the first global unicast address of any of the system network interfaces.
// Return the empty string if none is found.
func NetworkInterfaceAddress() string {
	ifaces, err := net.Interfaces()
	if err != nil {
		return ""
	}

	for _, iface := range ifaces {
		addrs, err := iface.Addrs()
		if err != nil {
			continue
		}

		if len(addrs) == 0 {
			continue
		}

		for _, addr := range addrs {
			ipNet, ok := addr.(*net.IPNet)
			if !ok {
				continue
			}

			if !ipNet.IP.IsGlobalUnicast() {
				continue
			}

			return ipNet.IP.String()
		}
	}

	return ""
}

// IsAddressCovered detects if network address1 is actually covered by
// address2, in the sense that they are either the same address or address2 is
// specified using a wildcard with the same port of address1.
func IsAddressCovered(address1, address2 string) bool {
	address1 = CanonicalNetworkAddress(address1, shared.HTTPSDefaultPort)
	address2 = CanonicalNetworkAddress(address2, shared.HTTPSDefaultPort)

	if address1 == address2 {
		return true
	}

	host1, port1, err := net.SplitHostPort(address1)
	if err != nil {
		return false
	}

	host2, port2, err := net.SplitHostPort(address2)
	if err != nil {
		return false
	}

	// If the ports are different, then address1 is clearly not covered by
	// address2.
	if port2 != port1 {
		return false
	}

	// If address1 contains a host name, let's try to resolve it, in order
	// to compare the actual IPs.
	var addresses1 []net.IP
	if host1 != "" {
		ip := net.ParseIP(host1)
		if ip != nil {
			addresses1 = append(addresses1, ip)
		} else {
			ips, err := net.LookupHost(host1)
			if err == nil && len(ips) > 0 {
				for _, ipStr := range ips {
					ip := net.ParseIP(ipStr)
					if ip != nil {
						addresses1 = append(addresses1, ip)
					}
				}
			}
		}
	}

	// If address2 contains a host name, let's try to resolve it, in order
	// to compare the actual IPs.
	var addresses2 []net.IP
	if host2 != "" {
		ip := net.ParseIP(host2)
		if ip != nil {
			addresses2 = append(addresses2, ip)
		} else {
			ips, err := net.LookupHost(host2)
			if err == nil && len(ips) > 0 {
				for _, ipStr := range ips {
					ip := net.ParseIP(ipStr)
					if ip != nil {
						addresses2 = append(addresses2, ip)
					}
				}
			}
		}
	}

	for _, a1 := range addresses1 {
		for _, a2 := range addresses2 {
			if a1.Equal(a2) {
				return true
			}
		}
	}

	// If address2 is using an IPv4 wildcard for the host, then address2 is
	// only covered if it's an IPv4 address.
	if host2 == "0.0.0.0" {
		ip1 := net.ParseIP(host1)
		if ip1 != nil && ip1.To4() != nil {
			return true
		}
		return false
	}

	// If address2 is using an IPv6 wildcard for the host, then address2 is
	// always covered.
	if host2 == "::" || host2 == "" {
		return true
	}

	return false
}

// IsWildCardAddress returns whether the given address is a wildcard.
func IsWildCardAddress(address string) bool {
	address = CanonicalNetworkAddress(address, shared.HTTPSDefaultPort)

	host, _, err := net.SplitHostPort(address)
	if err != nil {
		return false
	}

	if host == "0.0.0.0" || host == "::" || host == "" {
		return true
	}

	return false
}

// SysctlGet retrieves the value of a sysctl file in /proc/sys.
func SysctlGet(path string) (string, error) {
	// Read the current content
	content, err := ioutil.ReadFile(fmt.Sprintf("/proc/sys/%s", path))
	if err != nil {
		return "", err
	}

	return string(content), nil
}

// SysctlSet writes a value to a sysctl file in /proc/sys.
// Requires an even number of arguments as key/value pairs. E.g. SysctlSet("path1", "value1", "path2", "value2")
func SysctlSet(parts ...string) error {
	partsLen := len(parts)
	if partsLen%2 != 0 {
		return fmt.Errorf("Requires even number of arguments")
	}

	for i := 0; i < partsLen; i = i + 2 {
		path := parts[i]
		newValue := parts[i+1]

		// Get current value.
		currentValue, err := SysctlGet(path)
		if err == nil && currentValue == newValue {
			// Nothing to update.
			return nil
		}

		err = ioutil.WriteFile(fmt.Sprintf("/proc/sys/%s", path), []byte(newValue), 0)
		if err != nil {
			return err
		}
	}

	return nil
}

// SetTCPUserTimeout sets the TCP user timeout on a connection's socket
func SetTCPUserTimeout(conn *net.TCPConn, timeout time.Duration) error {
	rawConn, err := conn.SyscallConn()
	if err != nil {
		return fmt.Errorf("Error getting raw connection: %w", err)
	}

	err = rawConn.Control(func(fd uintptr) {
		err = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, unix.TCP_USER_TIMEOUT, int(timeout/time.Millisecond))
	})
	if err != nil {
		return fmt.Errorf("Error setting option on socket: %w", err)
	}

	return nil
}
<commit_msg>lxd/util/net: Adds SetTCPTimeouts and ExtractTCPConn functions<commit_after>package util

import (
	"crypto/tls"
	"crypto/x509"
	"fmt"
	"io/ioutil"
	"net"
	"reflect"
	"syscall"
	"time"
	"unsafe"

	"golang.org/x/sys/unix"

	"github.com/lxc/lxd/shared"
	"github.com/lxc/lxd/shared/logger"
)

// InMemoryNetwork creates a fully in-memory listener and dial function.
//
// Each time the dial function is invoked a new pair of net.Conn objects will
// be created using net.Pipe: the listener's Accept method will unblock and
// return one end of the pipe and the other end will be returned by the dial
// function.
func InMemoryNetwork() (net.Listener, func() net.Conn) {
	listener := &inMemoryListener{
		conns:  make(chan net.Conn, 16),
		closed: make(chan struct{}),
	}
	dialer := func() net.Conn {
		server, client := net.Pipe()
		listener.conns <- server
		return client
	}
	return listener, dialer
}

type inMemoryListener struct {
	conns  chan net.Conn
	closed chan struct{}
}

// Accept waits for and returns the next connection to the listener.
func (l *inMemoryListener) Accept() (net.Conn, error) {
	select {
	case conn := <-l.conns:
		return conn, nil
	case <-l.closed:
		return nil, fmt.Errorf("closed")
	}
}

// Close closes the listener.
// Any blocked Accept operations will be unblocked and return errors.
func (l *inMemoryListener) Close() error {
	close(l.closed)
	return nil
}

// Addr returns the listener's network address.
func (l *inMemoryListener) Addr() net.Addr {
	return &inMemoryAddr{}
}

type inMemoryAddr struct {
}

func (a *inMemoryAddr) Network() string {
	return "memory"
}

func (a *inMemoryAddr) String() string {
	return ""
}

// CanonicalNetworkAddress parses the given network address and returns a string of the form "host:port",
// possibly filling it with the default port if it's missing. It will also wrap a bare IPv6 address with square
// brackets if needed.
func CanonicalNetworkAddress(address string, defaultPort int) string {
	_, _, err := net.SplitHostPort(address)
	if err != nil {
		ip := net.ParseIP(address)
		if ip != nil {
			// If the input address is a bare IP address, then convert it to a proper listen address
			// using the canonical IP with default port and wrap IPv6 addresses in square brackets.
			address = net.JoinHostPort(ip.String(), fmt.Sprintf("%d", defaultPort))
		} else {
			// Otherwise assume this is either a host name or a partial address (e.g `[::]`) without
			// a port number, so append the default port.
			address = fmt.Sprintf("%s:%d", address, defaultPort)
		}
	}

	return address
}

// CanonicalNetworkAddressFromAddressAndPort returns a network address from separate address and port values.
// The address accepts values such as "[::]", "::" and "localhost".
func CanonicalNetworkAddressFromAddressAndPort(address string, port int, defaultPort int) string {
	// Because we accept just the host part of an IPv6 listen address (e.g. `[::]`) don't use net.JoinHostPort.
	// If a bare IP address is supplied then CanonicalNetworkAddress will use net.JoinHostPort if needed.
	return CanonicalNetworkAddress(fmt.Sprintf("%s:%d", address, port), defaultPort)
}

// ServerTLSConfig returns a new server-side tls.Config generated from the give
// certificate info.
func ServerTLSConfig(cert *shared.CertInfo) *tls.Config {
	config := shared.InitTLSConfig()
	config.ClientAuth = tls.RequestClientCert
	config.Certificates = []tls.Certificate{cert.KeyPair()}
	config.NextProtos = []string{"h2"} // Required by gRPC

	if cert.CA() != nil {
		pool := x509.NewCertPool()
		pool.AddCert(cert.CA())
		config.RootCAs = pool
		config.ClientCAs = pool

		logger.Infof("LXD is in CA mode, only CA-signed certificates will be allowed")
	}

	config.BuildNameToCertificate()
	return config
}

// NetworkInterfaceAddress returns the first global unicast address of any of the system network interfaces.
// Return the empty string if none is found.
func NetworkInterfaceAddress() string {
	ifaces, err := net.Interfaces()
	if err != nil {
		return ""
	}

	for _, iface := range ifaces {
		addrs, err := iface.Addrs()
		if err != nil {
			continue
		}

		if len(addrs) == 0 {
			continue
		}

		for _, addr := range addrs {
			ipNet, ok := addr.(*net.IPNet)
			if !ok {
				continue
			}

			if !ipNet.IP.IsGlobalUnicast() {
				continue
			}

			return ipNet.IP.String()
		}
	}

	return ""
}

// IsAddressCovered detects if network address1 is actually covered by
// address2, in the sense that they are either the same address or address2 is
// specified using a wildcard with the same port of address1.
func IsAddressCovered(address1, address2 string) bool {
	address1 = CanonicalNetworkAddress(address1, shared.HTTPSDefaultPort)
	address2 = CanonicalNetworkAddress(address2, shared.HTTPSDefaultPort)

	if address1 == address2 {
		return true
	}

	host1, port1, err := net.SplitHostPort(address1)
	if err != nil {
		return false
	}

	host2, port2, err := net.SplitHostPort(address2)
	if err != nil {
		return false
	}

	// If the ports are different, then address1 is clearly not covered by
	// address2.
	if port2 != port1 {
		return false
	}

	// If address1 contains a host name, let's try to resolve it, in order
	// to compare the actual IPs.
	var addresses1 []net.IP
	if host1 != "" {
		ip := net.ParseIP(host1)
		if ip != nil {
			addresses1 = append(addresses1, ip)
		} else {
			ips, err := net.LookupHost(host1)
			if err == nil && len(ips) > 0 {
				for _, ipStr := range ips {
					ip := net.ParseIP(ipStr)
					if ip != nil {
						addresses1 = append(addresses1, ip)
					}
				}
			}
		}
	}

	// If address2 contains a host name, let's try to resolve it, in order
	// to compare the actual IPs.
	var addresses2 []net.IP
	if host2 != "" {
		ip := net.ParseIP(host2)
		if ip != nil {
			addresses2 = append(addresses2, ip)
		} else {
			ips, err := net.LookupHost(host2)
			if err == nil && len(ips) > 0 {
				for _, ipStr := range ips {
					ip := net.ParseIP(ipStr)
					if ip != nil {
						addresses2 = append(addresses2, ip)
					}
				}
			}
		}
	}

	for _, a1 := range addresses1 {
		for _, a2 := range addresses2 {
			if a1.Equal(a2) {
				return true
			}
		}
	}

	// If address2 is using an IPv4 wildcard for the host, then address2 is
	// only covered if it's an IPv4 address.
	if host2 == "0.0.0.0" {
		ip1 := net.ParseIP(host1)
		if ip1 != nil && ip1.To4() != nil {
			return true
		}
		return false
	}

	// If address2 is using an IPv6 wildcard for the host, then address2 is
	// always covered.
	if host2 == "::" || host2 == "" {
		return true
	}

	return false
}

// IsWildCardAddress returns whether the given address is a wildcard.
func IsWildCardAddress(address string) bool {
	address = CanonicalNetworkAddress(address, shared.HTTPSDefaultPort)

	host, _, err := net.SplitHostPort(address)
	if err != nil {
		return false
	}

	if host == "0.0.0.0" || host == "::" || host == "" {
		return true
	}

	return false
}

// SysctlGet retrieves the value of a sysctl file in /proc/sys.
func SysctlGet(path string) (string, error) {
	// Read the current content
	content, err := ioutil.ReadFile(fmt.Sprintf("/proc/sys/%s", path))
	if err != nil {
		return "", err
	}

	return string(content), nil
}

// SysctlSet writes a value to a sysctl file in /proc/sys.
// Requires an even number of arguments as key/value pairs. E.g. SysctlSet("path1", "value1", "path2", "value2")
func SysctlSet(parts ...string) error {
	partsLen := len(parts)
	if partsLen%2 != 0 {
		return fmt.Errorf("Requires even number of arguments")
	}

	for i := 0; i < partsLen; i = i + 2 {
		path := parts[i]
		newValue := parts[i+1]

		// Get current value.
		currentValue, err := SysctlGet(path)
		if err == nil && currentValue == newValue {
			// Nothing to update.
			return nil
		}

		err = ioutil.WriteFile(fmt.Sprintf("/proc/sys/%s", path), []byte(newValue), 0)
		if err != nil {
			return err
		}
	}

	return nil
}

// ExtractTCPConn tries to extract the underlying net.TCPConn from a tls.Conn.
func ExtractTCPConn(conn net.Conn) (*net.TCPConn, error) {
	// Go doesn't currently expose the underlying TCP connection of a TLS connection, but we need it in order
	// to set timeout properties on the connection. We use some reflect/unsafe magic to extract the private
	// remote.conn field, which is indeed the underlying TCP connection.
	tlsConn, ok := conn.(*tls.Conn)
	if !ok {
		return nil, fmt.Errorf("Connection is not a tls.Conn")
	}

	field := reflect.ValueOf(tlsConn).Elem().FieldByName("conn")
	field = reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).Elem()
	c := field.Interface()

	tcpConn, ok := c.(*net.TCPConn)
	if !ok {
		return tcpConn, fmt.Errorf("Connection is not a net.TCPConn")
	}

	return tcpConn, nil
}

// SetTCPTimeouts sets TCP_USER_TIMEOUT and TCP keep alive timeouts on a connection.
func SetTCPTimeouts(conn *net.TCPConn) error {
	// Set TCP_USER_TIMEOUT option to limit the maximum amount of time in ms that transmitted data may remain
	// unacknowledged before TCP will forcefully close the corresponding connection and return ETIMEDOUT to the
	// application. This combined with the TCP keepalive options on the socket will ensure that should the
	// remote side of the connection disappear abruptly that LXD will detect this and close the socket quickly.
	// Decreasing the user timeouts allows applications to "fail fast" if so desired. Otherwise it may take
	// up to 20 minutes with the current system defaults in a normal WAN environment if there are packets in
	// the send queue that will prevent the keepalive timer from working as the retransmission timers kick in.
	// See https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=dca43c75e7e545694a9dd6288553f55c53e2a3a3
	err := SetTCPUserTimeout(conn, time.Second*30)
	if err != nil {
		return err
	}

	err = conn.SetKeepAlive(true)
	if err != nil {
		return err
	}

	err = conn.SetKeepAlivePeriod(3 * time.Second)
	if err != nil {
		return err
	}

	return nil
}

// SetTCPUserTimeout sets the TCP user timeout on a connection's socket
func SetTCPUserTimeout(conn *net.TCPConn, timeout time.Duration) error {
	rawConn, err := conn.SyscallConn()
	if err != nil {
		return fmt.Errorf("Error getting raw connection: %w", err)
	}

	err = rawConn.Control(func(fd uintptr) {
		err = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, unix.TCP_USER_TIMEOUT, int(timeout/time.Millisecond))
	})
	if err != nil {
		return fmt.Errorf("Error setting option on socket: %w", err)
	}

	return nil
}
<|endoftext|>


<commit_before>package quotes

import (
	"strings"

	"github.com/graffic/wanon/bot"
	"github.com/graffic/wanon/telegram"
	"github.com/op/go-logging"
)

type addQuote struct{}

var l = logging.MustGetLogger("wanon.messages.quotes")

func (handler *addQuote) Check(message *telegram.Message, context *bot.Context) int {
	isAddQuote := strings.Index(message.Text, "/addquote") == 0
	isReply := message.ReplyToMessage != nil

	if !isAddQuote {
		return bot.RouteNothing
	}

	if !isReply {
		answer := telegram.AnswerBack{API: context.API, Message: message}
		answer.Reply("To add a quote use /addquote in a reply")

		return bot.RouteNothing
	}

	l.Info("Adding quote")
	return bot.RouteAccept
}

func (handler *addQuote) Handle(message *telegram.Message, context *bot.Context) {
	quotes := quoteStorage{context.Storage}
	quote := Quote{
		AddedBy: message.From.Username,
		SaidBy:  message.ReplyToMessage.From.Username,
		When:    message.Date,
		What:    message.ReplyToMessage.Text,
	}

	err := quotes.AddQuote(message.Chat.ID, &quote)
	if err != nil {
		l.Fatal(err)
	}
	l.Info("Quote Added: <%s> %s", quote.SaidBy, quote.What)
}

// CreateAddQuote does nothing
func CreateAddQuote() bot.Handler {
	return new(addQuote)
}
<commit_msg>Dummy message when a quote is added<commit_after>package quotes

import (
	"strings"

	"github.com/graffic/wanon/bot"
	"github.com/graffic/wanon/telegram"
	"github.com/op/go-logging"
)

type addQuote struct{}

var l = logging.MustGetLogger("wanon.messages.quotes")

func (handler *addQuote) Check(message *telegram.Message, context *bot.Context) int {
	isAddQuote := strings.Index(message.Text, "/addquote") == 0
	isReply := message.ReplyToMessage != nil

	if !isAddQuote {
		return bot.RouteNothing
	}

	if !isReply {
		answer := telegram.AnswerBack{API: context.API, Message: message}
		answer.Reply("To add a quote use /addquote in a reply")

		return bot.RouteNothing
	}

	l.Info("Adding quote")
	return bot.RouteAccept
}

func (handler *addQuote) Handle(message *telegram.Message, context *bot.Context) {
	quotes := quoteStorage{context.Storage}
	quote := Quote{
		AddedBy: message.From.Username,
		SaidBy:  message.ReplyToMessage.From.Username,
		When:    message.Date,
		What:    message.ReplyToMessage.Text,
	}

	err := quotes.AddQuote(message.Chat.ID, &quote)
	if err != nil {
		l.Fatal(err)
	}
	l.Info("Quote Added: <%s> %s", quote.SaidBy, quote.What)
	answer := telegram.AnswerBack{API: context.API, Message: message}
	answer.Reply("procesado correctamente, siguienteeeeeee!!!!")
}

// CreateAddQuote does nothing
func CreateAddQuote() bot.Handler {
	return new(addQuote)
}
<|endoftext|>


<commit_before>package flagmqtt

import (
	"crypto/tls"
	"crypto/x509"
	"errors"
	"fmt"
	mq "github.com/eclipse/paho.mqtt.golang"
	"github.com/satori/go.uuid"
	"io/ioutil"
	"os"
	"strings"
	"time"
)

type ClientConfig struct {
	WillTopic               string
	WillPayload             string
	WillQoS                 int
	WillRetain              bool
	ClientID                string
	OnConnectHandler        func(mq.Client)
	OnConnectionLostHandler func(mq.Client, error)
}

func NewPersistentMqtt(config ClientConfig) (mqttClient mq.Client, err error) {
	useTls := false

	if val, ok := os.LookupEnv("MQTT_TLS"); ok {
		useTls = val != "0" && val != "" && strings.ToLower(val) != "false"
	}
	if *MqttTLSFlag {
		useTls = true
	}

	caPath := envOrFlagStr(*MqttCAFlag, "MQTT_CA_PATH", "")
	certPath := envOrFlagStr(*MqttCertFlag, "MQTT_CERT_PATH", "")
	keyPath := envOrFlagStr(*MqttKeyFlag, "MQTT_KEY_PATH", "")
	address := envOrFlagStr(*MqttAddressFlag, "MQTT_ADDRESS", "localhost:1883")
	username := envOrFlagStr(*MqttUsernameFlag, "MQTT_USERNAME", "")
	password := envOrFlagStr(*MqttPasswordFlag, "MQTT_PASSWORD", "")
	connectionTimeout := envOrFlagInt(*MqttConnectionTimeout, "MQTT_CONNECTION_TIMEOUT", 10)
	keepAlive := envOrFlagInt(*MqttKeepAlive, "MQTT_KEEPALIVE", 5)
	maxReconnectInterval := envOrFlagInt(*MqttMaxReconnectInterval, "MQTT_MAX_RECONNECT_INTERVAL", 2)
	pingTimeout := envOrFlagInt(*MqttPingTimeout, "MQTT_PING_TIMEOUT", 10)
	writeTimeout := envOrFlagInt(*MqttWriteTimeout, "MQTT_WRITE_TIMEOUT", 5)

	var tlsCfg *tls.Config
	if useTls {
		tlsCfg, err = setupTLS(caPath, certPath, keyPath)
		if err != nil {
			return
		}
	}

	clientId := config.ClientID

	if clientId == "" {
		var rndUuid uuid.UUID
		rndUuid = uuid.NewV4()
		clientId = rndUuid.String()
	}

	opts := mq.NewClientOptions().
		AddBroker(fmt.Sprintf("tcp://%s", address)).
		SetClientID(clientId).
		SetConnectTimeout(time.Duration(connectionTimeout) * time.Second).
		SetKeepAlive(time.Duration(keepAlive) * time.Second).
		SetMaxReconnectInterval(time.Duration(maxReconnectInterval) * time.Minute).
		SetMessageChannelDepth(100).
		SetPingTimeout(time.Duration(pingTimeout) * time.Second).
		SetProtocolVersion(4).
		SetWriteTimeout(time.Duration(writeTimeout) * time.Second)

	if config.OnConnectHandler != nil {
		opts.SetOnConnectHandler(config.OnConnectHandler)
	}

	if config.OnConnectionLostHandler != nil {
		opts.SetConnectionLostHandler(config.OnConnectionLostHandler)
	}

	if config.WillTopic != "" {
		opts.SetWill(
			config.WillTopic,
			config.WillPayload,
			byte(config.WillQoS&0xff),
			config.WillRetain,
		)
	}

	if username != "" {
		opts.SetUsername(username)
	}
	if password != "" {
		opts.SetPassword(password)
	}
	if useTls {
		opts.SetTLSConfig(tlsCfg)
	}

	return mq.NewClient(opts), nil
}

// NewUniqueIdentifier returns a unique identifier that the client can use.
// This identifier is what should be set for the lastWillID for anything
// that is bridging more than one device
func NewUniqueIdentifier() string {
	return uuid.NewV4().String()
}

func setupTLS(caPath, certPath, keyPath string) (*tls.Config, error) {
	tlsCfg := &tls.Config{}
	if caPath != "" {
		caPem, err := ioutil.ReadFile(caPath)
		if err != nil {
			return nil, err
		}
		tlsCfg.RootCAs = x509.NewCertPool()
		tlsCfg.RootCAs.AppendCertsFromPEM(caPem)
	}

	if certPath != "" && keyPath != "" {
		if keyPath == "" {
			return nil, errors.New("Certificate path specified, but key path missing")
		}
		cert, err := tls.LoadX509KeyPair(certPath, keyPath)
		if err != nil {
			return nil, err
		}
		tlsCfg.Certificates = []tls.Certificate{cert}
		tlsCfg.BuildNameToCertificate()
	}

	return tlsCfg, nil
}
<commit_msg>:ambulance: Don't care about the callback order<commit_after>package flagmqtt

import (
	"crypto/tls"
	"crypto/x509"
	"errors"
	"fmt"
	mq "github.com/eclipse/paho.mqtt.golang"
	"github.com/satori/go.uuid"
	"io/ioutil"
	"os"
	"strings"
	"time"
)

type ClientConfig struct {
	WillTopic               string
	WillPayload             string
	WillQoS                 int
	WillRetain              bool
	ClientID                string
	OnConnectHandler        func(mq.Client)
	OnConnectionLostHandler func(mq.Client, error)
}

func NewPersistentMqtt(config ClientConfig) (mqttClient mq.Client, err error) {
	useTls := false

	if val, ok := os.LookupEnv("MQTT_TLS"); ok {
		useTls = val != "0" && val != "" && strings.ToLower(val) != "false"
	}
	if *MqttTLSFlag {
		useTls = true
	}

	caPath := envOrFlagStr(*MqttCAFlag, "MQTT_CA_PATH", "")
	certPath := envOrFlagStr(*MqttCertFlag, "MQTT_CERT_PATH", "")
	keyPath := envOrFlagStr(*MqttKeyFlag, "MQTT_KEY_PATH", "")
	address := envOrFlagStr(*MqttAddressFlag, "MQTT_ADDRESS", "localhost:1883")
	username := envOrFlagStr(*MqttUsernameFlag, "MQTT_USERNAME", "")
	password := envOrFlagStr(*MqttPasswordFlag, "MQTT_PASSWORD", "")
	connectionTimeout := envOrFlagInt(*MqttConnectionTimeout, "MQTT_CONNECTION_TIMEOUT", 10)
	keepAlive := envOrFlagInt(*MqttKeepAlive, "MQTT_KEEPALIVE", 5)
	maxReconnectInterval := envOrFlagInt(*MqttMaxReconnectInterval, "MQTT_MAX_RECONNECT_INTERVAL", 2)
	pingTimeout := envOrFlagInt(*MqttPingTimeout, "MQTT_PING_TIMEOUT", 10)
	writeTimeout := envOrFlagInt(*MqttWriteTimeout, "MQTT_WRITE_TIMEOUT", 5)

	var tlsCfg *tls.Config
	if useTls {
		tlsCfg, err = setupTLS(caPath, certPath, keyPath)
		if err != nil {
			return
		}
	}

	clientId := config.ClientID

	if clientId == "" {
		var rndUuid uuid.UUID
		rndUuid = uuid.NewV4()
		clientId = rndUuid.String()
	}

	opts := mq.NewClientOptions().
		AddBroker(fmt.Sprintf("tcp://%s", address)).
		SetClientID(clientId).
		SetConnectTimeout(time.Duration(connectionTimeout) * time.Second).
		SetKeepAlive(time.Duration(keepAlive) * time.Second).
		SetMaxReconnectInterval(time.Duration(maxReconnectInterval) * time.Minute).
		SetMessageChannelDepth(100).
		SetPingTimeout(time.Duration(pingTimeout) * time.Second).
		SetProtocolVersion(4).
		SetOrderMatters(false).
		SetWriteTimeout(time.Duration(writeTimeout) * time.Second)

	if config.OnConnectHandler != nil {
		opts.SetOnConnectHandler(config.OnConnectHandler)
	}

	if config.OnConnectionLostHandler != nil {
		opts.SetConnectionLostHandler(config.OnConnectionLostHandler)
	}

	if config.WillTopic != "" {
		opts.SetWill(
			config.WillTopic,
			config.WillPayload,
			byte(config.WillQoS&0xff),
			config.WillRetain,
		)
	}

	if username != "" {
		opts.SetUsername(username)
	}
	if password != "" {
		opts.SetPassword(password)
	}
	if useTls {
		opts.SetTLSConfig(tlsCfg)
	}

	return mq.NewClient(opts), nil
}

// NewUniqueIdentifier returns a unique identifier that the client can use.
// This identifier is what should be set for the lastWillID for anything
// that is bridging more than one device
func NewUniqueIdentifier() string {
	return uuid.NewV4().String()
}

func setupTLS(caPath, certPath, keyPath string) (*tls.Config, error) {
	tlsCfg := &tls.Config{}
	if caPath != "" {
		caPem, err := ioutil.ReadFile(caPath)
		if err != nil {
			return nil, err
		}
		tlsCfg.RootCAs = x509.NewCertPool()
		tlsCfg.RootCAs.AppendCertsFromPEM(caPem)
	}

	if certPath != "" && keyPath != "" {
		if keyPath == "" {
			return nil, errors.New("Certificate path specified, but key path missing")
		}
		cert, err := tls.LoadX509KeyPair(certPath, keyPath)
		if err != nil {
			return nil, err
		}
		tlsCfg.Certificates = []tls.Certificate{cert}
		tlsCfg.BuildNameToCertificate()
	}

	return tlsCfg, nil
}
<|endoftext|>


<commit_before>package lzma

import (
	"bufio"
	"errors"
	"io"
)

// states defines the overall state count
const states = 12

// bufferLen is the value used for the bufferLen used by the decoder.
var bufferLen = 64 * (1 << 10)

// Decoder is able to read a LZMA byte stream and to read the plain text.
type Decoder struct {
	properties         Properties
	packedLen          uint64
	unpackedLen        uint64
	unpackedLenDefined bool
	dict               *decoderDict
	state              uint32
	posBitMask         uint32
	rd                 *rangeDecoder
	isMatch            [states << maxPosBits]prob
	isRep              [states]prob
	isRepG0            [states]prob
	isRepG1            [states]prob
	isRepG2            [states]prob
	isRepG0Long        [states << maxPosBits]prob
	rep                [4]uint32
	litDecoder         *literalCodec
	lengthDecoder      *lengthCodec
	distDecoder        *distCodec
}

// NewDecoder creates an LZMA decoder. It reads the classic, original LZMA
// format. Note that LZMA2 uses a different header format.
func NewDecoder(r io.Reader) (d *Decoder, err error) {
	f := bufio.NewReader(r)
	properties, err := readProperties(f)
	if err != nil {
		return nil, err
	}
	historyLen := int(properties.DictLen)
	if historyLen < 0 {
		return nil, errors.New(
			"LZMA property DictLen exceeds maximum int value")
	}
	d = &Decoder{
		properties: *properties,
	}
	if d.packedLen, err = readUint64LE(f); err != nil {
		return nil, err
	}
	if d.dict, err = newDecoderDict(bufferLen, historyLen); err != nil {
		return nil, err
	}
	d.posBitMask = (uint32(1) << uint(d.properties.PB)) - 1
	if d.rd, err = newRangeDecoder(f); err != nil {
		return nil, err
	}
	initProbSlice(d.isMatch[:])
	initProbSlice(d.isRep[:])
	initProbSlice(d.isRepG0[:])
	initProbSlice(d.isRepG1[:])
	initProbSlice(d.isRepG2[:])
	initProbSlice(d.isRepG0Long[:])
	d.litDecoder = newLiteralCodec(d.properties.LC, d.properties.LP)
	d.lengthDecoder = newLengthCodec()
	d.distDecoder = newDistCodec()
	return d, nil
}

// Properties returns a set of properties.
func (d *Decoder) Properties() Properties {
	return d.properties
}

// getUint64LE converts the uint64 value stored as little endian to an uint64
// value.
func getUint64LE(b []byte) uint64 {
	x := uint64(b[7]) << 56
	x |= uint64(b[6]) << 48
	x |= uint64(b[5]) << 40
	x |= uint64(b[4]) << 32
	x |= uint64(b[3]) << 24
	x |= uint64(b[2]) << 16
	x |= uint64(b[1]) << 8
	x |= uint64(b[0])
	return x
}

// readUint64LE reads a uint64 little-endian integer from reader.
func readUint64LE(r io.Reader) (x uint64, err error) {
	b := make([]byte, 8)
	if _, err = io.ReadFull(r, b); err != nil {
		return 0, err
	}
	x = getUint64LE(b)
	return x, nil
}

// initProbSlice initializes a slice of probabilities.
func initProbSlice(p []prob) {
	for i := range p {
		p[i] = probInit
	}
}

// Reads reads data from the decoder stream.
//
// The function fill put as much data in the buffer as it is available. The
// function might block and is not reentrant.
//
// The end of the LZMA stream is indicated by EOF. There might be other errors
// returned. The decoder will not be able to recover from an error returned.
func (d *Decoder) Read(p []byte) (n int, err error) {
	for n < len(p) {
		var k int
		k, err = d.dict.Read(p)
		if err != nil {
			return
		}
		n += k
		if n == len(p) {
			return
		}
		if err = d.fill(len(p) - n); err != nil {
			return
		}
	}
	return
}

// fill puts at lest the requested number of bytes into the decoder dictionary.
func (d *Decoder) fill(n int) error {
	panic("TODO")
}

// updateStateLiteral updates the state for a literal.
func (d *Decoder) updateStateLiteral() {
	switch {
	case d.state < 4:
		d.state = 0
		return
	case d.state < 10:
		d.state -= 3
		return
	}
	d.state -= 6
	return
}

// updateStateMatch updates the state for a match.
func (d *Decoder) updateStateMatch() {
	if d.state < 7 {
		d.state = 7
		return
	}
	d.state = 10
	return
}

// updateStateRep updates the state for a repetition.
func (d *Decoder) updateStateRep() {
	if d.state < 7 {
		d.state = 8
	}
	d.state = 11
}

// updateStateShortRep updates the state for a short repetition.
func (d *Decoder) updateStateShortRep() {
	if d.state < 7 {
		d.state = 9
	}
	d.state = 11
}

// decodeLiteral decodes a literal.
func (d *Decoder) decodeLiteral() (op operation, err error) {
	prevByte := d.dict.getByte(1)
	lp, lc := uint(d.properties.LP), uint(d.properties.LC)
	litState := ((uint32(d.dict.total) & ((1 << lp) - 1)) << lc) |
		(uint32(prevByte) >> (8 - lc))
	match := d.dict.getByte(int(d.rep[0]) + 1)
	s, err := d.litDecoder.Decode(d.rd, d.state, match, litState)
	if err != nil {
		return nil, err
	}
	return lit{s}, nil
}

// errWrongTermination indicates that a termination symbol has been received,
// but the range decoder could still produces more data
var errWrongTermination = errors.New(
	"range decoder doesn't support termination")

// decodeOp decodes an operation. The function returns io.EOF if the stream is
// terminated.
func (d *Decoder) decodeOp() (op operation, err error) {
	posState := uint32(d.dict.total) & d.posBitMask
	state2 := (d.state << maxPosBits) | posState

	b, err := d.isMatch[state2].Decode(d.rd)
	if err != nil {
		return nil, err
	}
	if b == 0 {
		// literal
		op, err := d.decodeLiteral()
		if err != nil {
			return nil, err
		}
		d.updateStateLiteral()
		return op, nil
	}
	b, err = d.isRep[d.state].Decode(d.rd)
	if err != nil {
		return nil, err
	}
	if b == 0 {
		// simple match
		d.rep[3], d.rep[2], d.rep[1] = d.rep[2], d.rep[1], d.rep[0]
		d.updateStateMatch()
		// TODO: check base for output of length decoder
		n, err := d.lengthDecoder.Decode(d.rd, posState)
		if err != nil {
			return nil, err
		}
		// TODO: check that distDecoder is using the same base as the
		// output of the lengthDecoder
		// TODO: check base of the repetition
		d.rep[0], err = d.distDecoder.Decode(n, d.rd)
		if d.rep[0] == 0xffffffff {
			if !d.rd.possiblyAtEnd() {
				return nil, errWrongTermination
			}
			return nil, io.EOF
		}
		// TODO: Create translator in rep operation
		op := rep{length: int(n), distance: int(d.rep[0])}
		return op, nil
	}
	b, err = d.isRepG0[d.state].Decode(d.rd)
	if b == 0 {
		// rep0
		panic("TODO")
	}
	b, err = d.isRepG1[d.state].Decode(d.rd)
	if b == 0 {
		// rep match 1
		panic("TODO")
	}
	b, err = d.isRepG2[d.state].Decode(d.rd)
	if b == 0 {
		// rep match 2
		panic("TODO")
	}
	// rep match 3
	panic("TODO")
}
<commit_msg>lzma: more work on the simple match case in the op decoder<commit_after>package lzma

import (
	"bufio"
	"errors"
	"io"
)

// states defines the overall state count
const states = 12

// bufferLen is the value used for the bufferLen used by the decoder.
var bufferLen = 64 * (1 << 10)

// Decoder is able to read a LZMA byte stream and to read the plain text.
type Decoder struct {
	properties         Properties
	packedLen          uint64
	unpackedLen        uint64
	unpackedLenDefined bool
	dict               *decoderDict
	state              uint32
	posBitMask         uint32
	rd                 *rangeDecoder
	isMatch            [states << maxPosBits]prob
	isRep              [states]prob
	isRepG0            [states]prob
	isRepG1            [states]prob
	isRepG2            [states]prob
	isRepG0Long        [states << maxPosBits]prob
	rep                [4]uint32
	litDecoder         *literalCodec
	lengthDecoder      *lengthCodec
	distDecoder        *distCodec
}

// NewDecoder creates an LZMA decoder. It reads the classic, original LZMA
// format. Note that LZMA2 uses a different header format.
func NewDecoder(r io.Reader) (d *Decoder, err error) {
	f := bufio.NewReader(r)
	properties, err := readProperties(f)
	if err != nil {
		return nil, err
	}
	historyLen := int(properties.DictLen)
	if historyLen < 0 {
		return nil, errors.New(
			"LZMA property DictLen exceeds maximum int value")
	}
	d = &Decoder{
		properties: *properties,
	}
	if d.packedLen, err = readUint64LE(f); err != nil {
		return nil, err
	}
	if d.dict, err = newDecoderDict(bufferLen, historyLen); err != nil {
		return nil, err
	}
	d.posBitMask = (uint32(1) << uint(d.properties.PB)) - 1
	if d.rd, err = newRangeDecoder(f); err != nil {
		return nil, err
	}
	initProbSlice(d.isMatch[:])
	initProbSlice(d.isRep[:])
	initProbSlice(d.isRepG0[:])
	initProbSlice(d.isRepG1[:])
	initProbSlice(d.isRepG2[:])
	initProbSlice(d.isRepG0Long[:])
	d.litDecoder = newLiteralCodec(d.properties.LC, d.properties.LP)
	d.lengthDecoder = newLengthCodec()
	d.distDecoder = newDistCodec()
	return d, nil
}

// Properties returns a set of properties.
func (d *Decoder) Properties() Properties {
	return d.properties
}

// getUint64LE converts the uint64 value stored as little endian to an uint64
// value.
func getUint64LE(b []byte) uint64 {
	x := uint64(b[7]) << 56
	x |= uint64(b[6]) << 48
	x |= uint64(b[5]) << 40
	x |= uint64(b[4]) << 32
	x |= uint64(b[3]) << 24
	x |= uint64(b[2]) << 16
	x |= uint64(b[1]) << 8
	x |= uint64(b[0])
	return x
}

// readUint64LE reads a uint64 little-endian integer from reader.
func readUint64LE(r io.Reader) (x uint64, err error) {
	b := make([]byte, 8)
	if _, err = io.ReadFull(r, b); err != nil {
		return 0, err
	}
	x = getUint64LE(b)
	return x, nil
}

// initProbSlice initializes a slice of probabilities.
func initProbSlice(p []prob) {
	for i := range p {
		p[i] = probInit
	}
}

// Reads reads data from the decoder stream.
//
// The function fill put as much data in the buffer as it is available. The
// function might block and is not reentrant.
//
// The end of the LZMA stream is indicated by EOF. There might be other errors
// returned. The decoder will not be able to recover from an error returned.
func (d *Decoder) Read(p []byte) (n int, err error) {
	for n < len(p) {
		var k int
		k, err = d.dict.Read(p)
		if err != nil {
			return
		}
		n += k
		if n == len(p) {
			return
		}
		if err = d.fill(len(p) - n); err != nil {
			return
		}
	}
	return
}

// fill puts at lest the requested number of bytes into the decoder dictionary.
func (d *Decoder) fill(n int) error {
	panic("TODO")
}

// updateStateLiteral updates the state for a literal.
func (d *Decoder) updateStateLiteral() {
	switch {
	case d.state < 4:
		d.state = 0
		return
	case d.state < 10:
		d.state -= 3
		return
	}
	d.state -= 6
	return
}

// updateStateMatch updates the state for a match.
func (d *Decoder) updateStateMatch() {
	if d.state < 7 {
		d.state = 7
		return
	}
	d.state = 10
	return
}

// updateStateRep updates the state for a repetition.
func (d *Decoder) updateStateRep() {
	if d.state < 7 {
		d.state = 8
	}
	d.state = 11
}

// updateStateShortRep updates the state for a short repetition.
func (d *Decoder) updateStateShortRep() {
	if d.state < 7 {
		d.state = 9
	}
	d.state = 11
}

// decodeLiteral decodes a literal.
func (d *Decoder) decodeLiteral() (op operation, err error) {
	prevByte := d.dict.getByte(1)
	lp, lc := uint(d.properties.LP), uint(d.properties.LC)
	litState := ((uint32(d.dict.total) & ((1 << lp) - 1)) << lc) |
		(uint32(prevByte) >> (8 - lc))
	match := d.dict.getByte(int(d.rep[0]) + 1)
	s, err := d.litDecoder.Decode(d.rd, d.state, match, litState)
	if err != nil {
		return nil, err
	}
	return lit{s}, nil
}

// errWrongTermination indicates that a termination symbol has been received,
// but the range decoder could still produces more data
var errWrongTermination = errors.New(
	"range decoder doesn't support termination")

// decodeOp decodes an operation. The function returns io.EOF if the stream is
// terminated.
func (d *Decoder) decodeOp() (op operation, err error) {
	posState := uint32(d.dict.total) & d.posBitMask
	state2 := (d.state << maxPosBits) | posState

	b, err := d.isMatch[state2].Decode(d.rd)
	if err != nil {
		return nil, err
	}
	if b == 0 {
		// literal
		op, err := d.decodeLiteral()
		if err != nil {
			return nil, err
		}
		d.updateStateLiteral()
		return op, nil
	}
	b, err = d.isRep[d.state].Decode(d.rd)
	if err != nil {
		return nil, err
	}
	if b == 0 {
		// simple match
		d.rep[3], d.rep[2], d.rep[1] = d.rep[2], d.rep[1], d.rep[0]
		d.updateStateMatch()
		// The length decoder returns the length offset.
		l, err := d.lengthDecoder.Decode(d.rd, posState)
		if err != nil {
			return nil, err
		}
		// TODO: check that distDecoder is using the same base as the
		// output of the lengthDecoder
		// TODO: check base of the repetition
		d.rep[0], err = d.distDecoder.Decode(l, d.rd)
		if err != nil {
			return nil, err
		}
		if d.rep[0] == 0xffffffff {
			if !d.rd.possiblyAtEnd() {
				return nil, errWrongTermination
			}
			return nil, io.EOF
		}
		op := rep{length: int(l + minLength), distance: int(d.rep[0])}
		return op, nil
	}
	b, err = d.isRepG0[d.state].Decode(d.rd)
	if b == 0 {
		// rep0
		panic("TODO")
	}
	b, err = d.isRepG1[d.state].Decode(d.rd)
	if b == 0 {
		// rep match 1
		panic("TODO")
	}
	b, err = d.isRepG2[d.state].Decode(d.rd)
	if b == 0 {
		// rep match 2
		panic("TODO")
	}
	// rep match 3
	panic("TODO")
}
<|endoftext|>


<commit_before>package collectors

import (
	"fmt"

	"github.com/prometheus/client_golang/prometheus"

	"github.com/cloudfoundry-community/firehose_exporter/metrics"
	"github.com/cloudfoundry-community/firehose_exporter/utils"
)

type valueMetricsCollector struct {
	namespace                 string
	metricsStore              *metrics.Store
	valueMetricsCollectorDesc *prometheus.Desc
}

func NewValueMetricsCollector(
	namespace string,
	metricsStore *metrics.Store,
) *valueMetricsCollector {
	valueMetricsCollectorDesc := prometheus.NewDesc(
		prometheus.BuildFQName(namespace, value_metrics_subsystem, "collector"),
		"Cloud Foundry Firehose value metrics collector.",
		nil,
		nil,
	)

	collector := &valueMetricsCollector{
		namespace:                 namespace,
		metricsStore:              metricsStore,
		valueMetricsCollectorDesc: valueMetricsCollectorDesc,
	}
	return collector
}

func (c valueMetricsCollector) Collect(ch chan<- prometheus.Metric) {
	for _, valueMetric := range c.metricsStore.GetValueMetrics() {
		ch <- prometheus.MustNewConstMetric(
			prometheus.NewDesc(
				prometheus.BuildFQName(c.namespace, value_metrics_subsystem, utils.NormalizeName(valueMetric.Name)),
				fmt.Sprintf("Cloud Foundry firehose '%s' value metric.", valueMetric.Name),
				[]string{"origin", "deployment", "job", "index", "ip", "unit"},
				nil,
			),
			prometheus.GaugeValue,
			float64(valueMetric.Value),
			valueMetric.Origin,
			valueMetric.Deployment,
			valueMetric.Job,
			valueMetric.Index,
			valueMetric.IP,
			valueMetric.Unit,
		)
	}
}

func (c valueMetricsCollector) Describe(ch chan<- *prometheus.Desc) {
	ch <- c.valueMetricsCollectorDesc
}
<commit_msg>Append origin to value metrics<commit_after>package collectors

import (
	"fmt"

	"github.com/prometheus/client_golang/prometheus"

	"github.com/cloudfoundry-community/firehose_exporter/metrics"
	"github.com/cloudfoundry-community/firehose_exporter/utils"
)

type valueMetricsCollector struct {
	namespace                 string
	metricsStore              *metrics.Store
	valueMetricsCollectorDesc *prometheus.Desc
}

func NewValueMetricsCollector(
	namespace string,
	metricsStore *metrics.Store,
) *valueMetricsCollector {
	valueMetricsCollectorDesc := prometheus.NewDesc(
		prometheus.BuildFQName(namespace, value_metrics_subsystem, "collector"),
		"Cloud Foundry Firehose value metrics collector.",
		nil,
		nil,
	)

	collector := &valueMetricsCollector{
		namespace:                 namespace,
		metricsStore:              metricsStore,
		valueMetricsCollectorDesc: valueMetricsCollectorDesc,
	}
	return collector
}

func (c valueMetricsCollector) Collect(ch chan<- prometheus.Metric) {
	for _, valueMetric := range c.metricsStore.GetValueMetrics() {
		metricName := utils.NormalizeName(valueMetric.Origin) + "_" + utils.NormalizeName(valueMetric.Name)
		ch <- prometheus.MustNewConstMetric(
			prometheus.NewDesc(
				prometheus.BuildFQName(c.namespace, value_metrics_subsystem, metricName),
				fmt.Sprintf("Cloud Foundry Firehose '%s' value metric.", valueMetric.Name),
				[]string{"origin", "deployment", "job", "index", "ip", "unit"},
				nil,
			),
			prometheus.GaugeValue,
			float64(valueMetric.Value),
			valueMetric.Origin,
			valueMetric.Deployment,
			valueMetric.Job,
			valueMetric.Index,
			valueMetric.IP,
			valueMetric.Unit,
		)
	}
}

func (c valueMetricsCollector) Describe(ch chan<- *prometheus.Desc) {
	ch <- c.valueMetricsCollectorDesc
}
<|endoftext|>


<commit_before>package chat

import (
	"crypto/rand"
	"errors"
	"io"

	"github.com/agl/ed25519"
	"github.com/keybase/client/go/chat/signencrypt"
	"github.com/keybase/client/go/libkb"
)

type Encrypter interface {
	// EncryptedLen returns the number of bytes that the ciphertext of
	// size plaintext bytes will be.
	EncryptedLen(size int) int

	// Encrypt takes a plaintext reader and returns a ciphertext reader.
	// It generates new keys every time it is called.
	Encrypt(plaintext io.Reader) (ciphertext io.Reader, err error)

	// EncryptKey returns the ephemeral key that was used during Encrypt.
	EncryptKey() []byte

	// VerifyKey returns the public portion of the signing key that
	// can be used for signature verification.
	VerifyKey() []byte
}

type Decrypter interface {
	// Decrypt takes a ciphertext reader, encryption and verify keys.
	// It returns a plaintext reader.
	Decrypt(ciphertext io.Reader, encKey, verifyKey []byte) (plaintext io.Reader)
}

var nonce signencrypt.Nonce

func init() {
	var n [signencrypt.NonceSize]byte
	copy(n[:], "kbchatattachment")
	nonce = &n
}

type SignEncrypter struct {
	encKey    signencrypt.SecretboxKey
	signKey   signencrypt.SignKey
	verifyKey signencrypt.VerifyKey
}

func NewSignEncrypter() *SignEncrypter {
	return &SignEncrypter{}
}

func (s *SignEncrypter) EncryptedLen(size int) int {
	return signencrypt.GetSealedSize(size)
}

func (s *SignEncrypter) Encrypt(r io.Reader) (io.Reader, error) {
	if err := s.makeKeys(); err != nil {
		return nil, err
	}
	return signencrypt.NewEncodingReader(s.encKey, s.signKey, nonce, r), nil
}

func (s *SignEncrypter) EncryptKey() []byte {
	return []byte((*s.encKey)[:])
}

func (s *SignEncrypter) VerifyKey() []byte {
	return []byte((*s.verifyKey)[:])
}

func (s *SignEncrypter) makeKeys() error {
	var encKey [signencrypt.SecretboxKeySize]byte
	n, err := rand.Read(encKey[:])
	if err != nil {
		return err
	}
	if n != signencrypt.SecretboxKeySize {
		return errors.New("failed to rand.Read the correct number of bytes")
	}

	sign, err := libkb.GenerateNaclSigningKeyPair()
	if err != nil {
		return err
	}

	var signKey [ed25519.PrivateKeySize]byte
	copy(signKey[:], (*sign.Private)[:])
	var verifyKey [ed25519.PublicKeySize]byte
	copy(verifyKey[:], sign.Public[:])

	s.encKey = &encKey
	s.signKey = &signKey
	s.verifyKey = &verifyKey

	return nil
}

type SignDecrypter struct{}

func NewSignDecrypter() *SignDecrypter {
	return &SignDecrypter{}
}

func (s *SignDecrypter) Decrypt(r io.Reader, encKey, verifyKey []byte) io.Reader {
	var xencKey [signencrypt.SecretboxKeySize]byte
	copy(xencKey[:], encKey)
	var xverifyKey [ed25519.PublicKeySize]byte
	copy(xverifyKey[:], verifyKey)
	return signencrypt.NewDecodingReader(&xencKey, &xverifyKey, nonce, r)
}
<commit_msg>PR feedback<commit_after>package chat

import (
	"crypto/rand"
	"errors"
	"io"

	"github.com/agl/ed25519"
	"github.com/keybase/client/go/chat/signencrypt"
	"github.com/keybase/client/go/libkb"
)

type Encrypter interface {
	// EncryptedLen returns the number of bytes that the ciphertext of
	// size plaintext bytes will be.
	EncryptedLen(size int) int

	// Encrypt takes a plaintext reader and returns a ciphertext reader.
	// It generates new keys every time it is called.
	Encrypt(plaintext io.Reader) (ciphertext io.Reader, err error)

	// EncryptKey returns the ephemeral key that was used during the
	// last invocation of Encrypt.
	EncryptKey() []byte

	// VerifyKey returns the public portion of the signing key used during
	// the last invocation of Encrypt.  It can be used for signature
	// verification.
	VerifyKey() []byte
}

type Decrypter interface {
	// Decrypt takes a ciphertext reader, encryption and verify keys.
	// It returns a plaintext reader.
	Decrypt(ciphertext io.Reader, encKey, verifyKey []byte) (plaintext io.Reader)
}

// The ASCII bytes "kbchatattachment".
var nonce signencrypt.Nonce = &[16]byte{0x6b, 0x62, 0x63, 0x68, 0x61, 0x74, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74}

type SignEncrypter struct {
	encKey    signencrypt.SecretboxKey
	signKey   signencrypt.SignKey
	verifyKey signencrypt.VerifyKey
}

func NewSignEncrypter() *SignEncrypter {
	return &SignEncrypter{}
}

func (s *SignEncrypter) EncryptedLen(size int) int {
	return signencrypt.GetSealedSize(size)
}

func (s *SignEncrypter) Encrypt(r io.Reader) (io.Reader, error) {
	// It is *very* important that Encrypt calls makeKeys() to make
	// new keys every time it runs.  The keys it uses cannot be reused
	// since we are using a constant nonce.
	if err := s.makeKeys(); err != nil {
		return nil, err
	}
	return signencrypt.NewEncodingReader(s.encKey, s.signKey, nonce, r), nil
}

func (s *SignEncrypter) EncryptKey() []byte {
	return []byte((*s.encKey)[:])
}

func (s *SignEncrypter) VerifyKey() []byte {
	return []byte((*s.verifyKey)[:])
}

func (s *SignEncrypter) makeKeys() error {
	var encKey [signencrypt.SecretboxKeySize]byte
	n, err := rand.Read(encKey[:])
	if err != nil {
		return err
	}
	if n != signencrypt.SecretboxKeySize {
		return errors.New("failed to rand.Read the correct number of bytes")
	}

	sign, err := libkb.GenerateNaclSigningKeyPair()
	if err != nil {
		return err
	}

	var signKey [ed25519.PrivateKeySize]byte
	copy(signKey[:], (*sign.Private)[:])
	var verifyKey [ed25519.PublicKeySize]byte
	copy(verifyKey[:], sign.Public[:])

	s.encKey = &encKey
	s.signKey = &signKey
	s.verifyKey = &verifyKey

	return nil
}

type SignDecrypter struct{}

func NewSignDecrypter() *SignDecrypter {
	return &SignDecrypter{}
}

func (s *SignDecrypter) Decrypt(r io.Reader, encKey, verifyKey []byte) io.Reader {
	var xencKey [signencrypt.SecretboxKeySize]byte
	copy(xencKey[:], encKey)
	var xverifyKey [ed25519.PublicKeySize]byte
	copy(xverifyKey[:], verifyKey)
	return signencrypt.NewDecodingReader(&xencKey, &xverifyKey, nonce, r)
}
<|endoftext|>


<commit_before>package systests

import (
	"context"
	"io/ioutil"
	"net/http"
	"testing"

	"github.com/davecgh/go-spew/spew"
	"github.com/keybase/client/go/engine"
	"github.com/keybase/client/go/kbtest"
	"github.com/keybase/client/go/libkb"
	keybase1 "github.com/keybase/client/go/protocol/keybase1"
	"github.com/stretchr/testify/require"
)

func TestProofSuggestions(t *testing.T) {
	t.Skip()
	tt := newTeamTester(t)
	defer tt.cleanup()

	alice := tt.addUser("abc")

	res, err := alice.userClient.ProofSuggestions(context.Background(), 0)
	require.NoError(t, err)
	t.Logf("suggestions: %v", spew.Sdump(res))
	expected := keybase1.ProofSuggestionsRes{
		ShowMore: true,
		Suggestions: []keybase1.ProofSuggestion{{
			Key:           "twitter",
			ProfileText:   "Prove your Twitter",
			PickerText:    "Twitter",
			PickerSubtext: "twitter.com",
		}, {
			Key:           "github",
			ProfileText:   "Prove your GitHub",
			PickerText:    "GitHub",
			PickerSubtext: "github.com",
		}, {
			Key:           "reddit",
			ProfileText:   "Prove your Reddit",
			PickerText:    "Reddit",
			PickerSubtext: "reddit.com",
		}, {
			Key:           "hackernews",
			ProfileText:   "Prove your Hacker News",
			PickerText:    "Hacker News",
			PickerSubtext: "news.ycombinator.com",
		}, {
			Key:           "rooter",
			ProfileText:   "Prove your Rooter",
			PickerText:    "Rooter",
			PickerSubtext: "",
		}, {
			Key:           "mastodon.social",
			ProfileText:   "Prove your Local Mastodon Social",
			PickerText:    "Local Mastodon Social",
			PickerSubtext: "mastodon.social",
		}, {
			Key:           "gubble.social",
			ProfileText:   "Prove your Gubble.social",
			PickerText:    "Gubble.social",
			PickerSubtext: "Gubble instance",
		}, {
			Key:           "web",
			ProfileText:   "Prove your website",
			PickerText:    "Your own website",
			PickerSubtext: "",
		}, {
			Key:           "pgp",
			ProfileText:   "Add a PGP key",
			PickerText:    "PGP key",
			PickerSubtext: "",
		}, {
			Key:           "btc",
			ProfileText:   "Set a Bitcoin address",
			PickerText:    "Bitcoin address",
			PickerSubtext: "",
		}, {
			Key:           "zcash",
			ProfileText:   "Set a Zcash address",
			PickerText:    "Zcash address",
			PickerSubtext: "",
		}, {
			Key:           "gubble.cloud",
			BelowFold:     true,
			ProfileText:   "Prove your Gubble.cloud",
			PickerText:    "Gubble.cloud",
			PickerSubtext: "Gubble instance",
		}, {
			Key:           "theqrl.org",
			BelowFold:     true,
			ProfileText:   "Prove your theqrl.org",
			PickerText:    "theqrl.org",
			PickerSubtext: "theqrl.org",
		}}}
	require.Equal(t, expected.ShowMore, res.ShowMore)
	require.True(t, len(res.Suggestions) >= len(expected.Suggestions), "should be at least as many results as expected")
	for _, b := range res.Suggestions {
		if b.Key == "theqrl.org" {
			// Skip checking for logos for this one.
			continue
		}
		require.Len(t, b.ProfileIcon, 2)
		for _, icon := range b.ProfileIcon {
			checkIcon(t, icon)
		}
		for _, icon := range b.PickerIcon {
			checkIcon(t, icon)
		}

	}
	var found int
	for i, b := range res.Suggestions {
		if found >= len(expected.Suggestions) {
			t.Logf("done")
			break
		}
		t.Logf("row %v %v", i, b.Key)
		a := expected.Suggestions[found]
		if a.Key != b.Key {
			t.Logf("skipping %v (mismatch)", a.Key)
			continue
		}
		found++
		require.Equal(t, a.Key, b.Key)
		require.Equal(t, a.BelowFold, b.BelowFold)
		require.Equal(t, a.ProfileText, b.ProfileText)
		require.Equal(t, a.PickerText, b.PickerText)
		require.Equal(t, a.PickerSubtext, b.PickerSubtext)

	}
	require.Len(t, expected.Suggestions, found)
}

func checkIcon(t testing.TB, icon keybase1.SizedImage) {
	if icon.Width < 2 {
		t.Fatalf("unreasonable icon size")
	}
	if kbtest.SkipIconRemoteTest() {
		t.Logf("Skipping icon remote test")
		require.True(t, len(icon.Path) > 8)
	} else {
		resp, err := http.Get(icon.Path)
		require.Equal(t, 200, resp.StatusCode, "icon file should be reachable")
		require.NoError(t, err)
		body, err := ioutil.ReadAll(resp.Body)
		require.NoError(t, err)
		if len(body) < 150 {
			t.Fatalf("unreasonable icon payload size")
		}
	}
}

func TestProofSuggestionsOmitProven(t *testing.T) {
	tt := newTeamTester(t)
	defer tt.cleanup()
	alice := tt.addUser("abc")

	assertOmitted := func(service string) {
		res, err := alice.userClient.ProofSuggestions(context.Background(), 0)
		require.NoError(t, err)
		for _, suggestion := range res.Suggestions {
			require.NotEqual(t, service, suggestion.Key)
		}
	}

	alice.proveRooter()
	t.Logf("alice proved rooter, so rooter is no longer suggested")
	assertOmitted("rooter")

	eng := engine.NewCryptocurrencyEngine(alice.MetaContext().G(), keybase1.RegisterAddressArg{
		Address: "zcCk6rKzynC4tT1Rmg325A5Xw81Ck3S6nD6mtPWCXaMtyFczkyU4kYjEhrcz2QKfF5T2siWGyJNxWo43XWT3qk5YpPhFGj2",
	})
	err := engine.RunEngine2(alice.MetaContext().WithUIs(libkb.UIs{
		LogUI:    alice.MetaContext().G().Log,
		SecretUI: alice.newSecretUI(),
	}), eng)
	require.NoError(t, err)
	t.Logf("alice added a zcash address, so zcash is no longer suggested")
	assertOmitted("zcash")
}
<commit_msg>re-enable TestProofSuggestions<commit_after>package systests

import (
	"context"
	"io/ioutil"
	"net/http"
	"testing"

	"github.com/davecgh/go-spew/spew"
	"github.com/keybase/client/go/engine"
	"github.com/keybase/client/go/kbtest"
	"github.com/keybase/client/go/libkb"
	keybase1 "github.com/keybase/client/go/protocol/keybase1"
	"github.com/stretchr/testify/require"
)

func TestProofSuggestions(t *testing.T) {
	tt := newTeamTester(t)
	defer tt.cleanup()

	alice := tt.addUser("abc")

	res, err := alice.userClient.ProofSuggestions(context.Background(), 0)
	require.NoError(t, err)
	t.Logf("suggestions: %v", spew.Sdump(res))
	expected := keybase1.ProofSuggestionsRes{
		ShowMore: true,
		Suggestions: []keybase1.ProofSuggestion{{
			Key:           "twitter",
			ProfileText:   "Prove your Twitter",
			PickerText:    "Twitter",
			PickerSubtext: "twitter.com",
		}, {
			Key:           "github",
			ProfileText:   "Prove your GitHub",
			PickerText:    "GitHub",
			PickerSubtext: "github.com",
		}, {
			Key:           "reddit",
			ProfileText:   "Prove your Reddit",
			PickerText:    "Reddit",
			PickerSubtext: "reddit.com",
		}, {
			Key:           "hackernews",
			ProfileText:   "Prove your Hacker News",
			PickerText:    "Hacker News",
			PickerSubtext: "news.ycombinator.com",
		}, {
			Key:           "rooter",
			ProfileText:   "Prove your Rooter",
			PickerText:    "Rooter",
			PickerSubtext: "",
		}, {
			Key:           "gubble.social",
			ProfileText:   "Prove your Gubble.social",
			PickerText:    "Gubble.social",
			PickerSubtext: "Gubble instance",
		}, {
			Key:           "web",
			ProfileText:   "Prove your website",
			PickerText:    "Your own website",
			PickerSubtext: "",
		}, {
			Key:           "pgp",
			ProfileText:   "Add a PGP key",
			PickerText:    "PGP key",
			PickerSubtext: "",
		}, {
			Key:           "btc",
			ProfileText:   "Set a Bitcoin address",
			PickerText:    "Bitcoin address",
			PickerSubtext: "",
		}, {
			Key:           "zcash",
			ProfileText:   "Set a Zcash address",
			PickerText:    "Zcash address",
			PickerSubtext: "",
		}, {
			Key:           "gubble.cloud",
			BelowFold:     true,
			ProfileText:   "Prove your Gubble.cloud",
			PickerText:    "Gubble.cloud",
			PickerSubtext: "Gubble instance",
		}, {
			Key:           "theqrl.org",
			BelowFold:     true,
			ProfileText:   "Prove your theqrl.org",
			PickerText:    "theqrl.org",
			PickerSubtext: "theqrl.org",
		}}}
	require.Equal(t, expected.ShowMore, res.ShowMore)
	require.True(t, len(res.Suggestions) >= len(expected.Suggestions), "should be at least as many results as expected")
	for _, b := range res.Suggestions {
		if b.Key == "theqrl.org" {
			// Skip checking for logos for this one.
			continue
		}
		require.Len(t, b.ProfileIcon, 2)
		for _, icon := range b.ProfileIcon {
			checkIcon(t, icon)
		}
		for _, icon := range b.PickerIcon {
			checkIcon(t, icon)
		}

	}
	var found int
	for i, b := range res.Suggestions {
		if found >= len(expected.Suggestions) {
			t.Logf("done")
			break
		}
		t.Logf("row %v %v", i, b.Key)
		a := expected.Suggestions[found]
		if a.Key != b.Key {
			t.Logf("skipping %v (mismatch)", a.Key)
			continue
		}
		found++
		require.Equal(t, a.Key, b.Key)
		require.Equal(t, a.BelowFold, b.BelowFold)
		require.Equal(t, a.ProfileText, b.ProfileText)
		require.Equal(t, a.PickerText, b.PickerText)
		require.Equal(t, a.PickerSubtext, b.PickerSubtext)

	}
	require.Len(t, expected.Suggestions, found)
}

func checkIcon(t testing.TB, icon keybase1.SizedImage) {
	if icon.Width < 2 {
		t.Fatalf("unreasonable icon size")
	}
	if kbtest.SkipIconRemoteTest() {
		t.Logf("Skipping icon remote test")
		require.True(t, len(icon.Path) > 8)
	} else {
		resp, err := http.Get(icon.Path)
		require.Equal(t, 200, resp.StatusCode, "icon file should be reachable")
		require.NoError(t, err)
		body, err := ioutil.ReadAll(resp.Body)
		require.NoError(t, err)
		if len(body) < 150 {
			t.Fatalf("unreasonable icon payload size")
		}
	}
}

func TestProofSuggestionsOmitProven(t *testing.T) {
	tt := newTeamTester(t)
	defer tt.cleanup()
	alice := tt.addUser("abc")

	assertOmitted := func(service string) {
		res, err := alice.userClient.ProofSuggestions(context.Background(), 0)
		require.NoError(t, err)
		for _, suggestion := range res.Suggestions {
			require.NotEqual(t, service, suggestion.Key)
		}
	}

	alice.proveRooter()
	t.Logf("alice proved rooter, so rooter is no longer suggested")
	assertOmitted("rooter")

	eng := engine.NewCryptocurrencyEngine(alice.MetaContext().G(), keybase1.RegisterAddressArg{
		Address: "zcCk6rKzynC4tT1Rmg325A5Xw81Ck3S6nD6mtPWCXaMtyFczkyU4kYjEhrcz2QKfF5T2siWGyJNxWo43XWT3qk5YpPhFGj2",
	})
	err := engine.RunEngine2(alice.MetaContext().WithUIs(libkb.UIs{
		LogUI:    alice.MetaContext().G().Log,
		SecretUI: alice.newSecretUI(),
	}), eng)
	require.NoError(t, err)
	t.Logf("alice added a zcash address, so zcash is no longer suggested")
	assertOmitted("zcash")
}
<|endoftext|>


<commit_before>package callerid

import (
	"reflect"
	"testing"

	"golang.org/x/net/context"

	qrpb "github.com/youtube/vitess/go/vt/proto/query"
	vtpb "github.com/youtube/vitess/go/vt/proto/vtrpc"
)

const (
	// FakePrincipal is the principal of testing EffectiveCallerID
	FakePrincipal = "TestPrincipal"
	// FakeComponent is the component of testing EffectiveCallerID
	FakeComponent = "TestComponent"
	// FakeSubcomponent is the subcomponent of testing EffectiveCallerID
	FakeSubcomponent = "TestSubcomponent"
	// FakeUsername is the username of testing ImmediateCallerID
	FakeUsername = "TestUsername"
)

// Tests performs the necessary testsuite for CallerID operations
func Tests(t *testing.T, im *qrpb.VTGateCallerID, ef *vtpb.CallerID) {
	ctx := context.TODO()
	ctxim := ImmediateCallerIDFromContext(ctx)
	// For Contexts without ImmediateCallerID, ImmediateCallerIDFromContext should fail
	if ctxim != nil {
		t.Errorf("Expect nil from ImmediateCallerIDFromContext, but got %v", ctxim)
	}
	// For Contexts without EffectiveCallerID, EffectiveCallerIDFromContext should fail
	ctxef := EffectiveCallerIDFromContext(ctx)
	if ctxef != nil {
		t.Errorf("Expect nil from EffectiveCallerIDFromContext, but got %v", ctxef)
	}

	ctx = NewContext(ctx, nil, nil)
	ctxim = ImmediateCallerIDFromContext(ctx)
	// For Contexts with nil ImmediateCallerID, ImmediateCallerIDFromContext should fail
	if ctxim != nil {
		t.Errorf("Expect nil from ImmediateCallerIDFromContext, but got %v", ctxim)
	}
	// For Contexts with nil EffectiveCallerID, EffectiveCallerIDFromContext should fail
	ctxef = EffectiveCallerIDFromContext(ctx)
	if ctxef != nil {
		t.Errorf("Expect nil from EffectiveCallerIDFromContext, but got %v", ctxef)
	}

	// Test GetXxx on nil receivers, should get all empty strings
	if u := GetUsername(ctxim); u != "" {
		t.Errorf("Expect empty string from (nil).GetUsername(), but got %v", u)
	}
	if p := GetPrincipal(ctxef); p != "" {
		t.Errorf("Expect empty string from (nil).GetPrincipal(), but got %v", p)
	}
	if c := GetComponent(ctxef); c != "" {
		t.Errorf("Expect empty string from (nil).GetComponent(), but got %v", c)
	}
	if s := GetSubcomponent(ctxef); s != "" {
		t.Errorf("Expect empty string from (nil).GetSubcomponent(), but got %v", s)
	}

	ctx = NewContext(ctx, ef, im)
	ctxim = ImmediateCallerIDFromContext(ctx)
	// retrieved ImmediateCallerID should be equal to the one we put into Context
	if !reflect.DeepEqual(ctxim, im) {
		t.Errorf("Expect %v from ImmediateCallerIDFromContext, but got %v", im, ctxim)
	}
	if u := GetUsername(im); u != FakeUsername {
		t.Errorf("Expect %v from im.Username(), but got %v", FakeUsername, u)
	}

	ctxef = EffectiveCallerIDFromContext(ctx)
	// retrieved EffectiveCallerID should be equal to the one we put into Context
	if !reflect.DeepEqual(ctxef, ef) {
		t.Errorf("Expect %v from EffectiveCallerIDFromContext, but got %v", ef, ctxef)
	}
	if p := GetPrincipal(ef); p != FakePrincipal {
		t.Errorf("Expect %v from ef.Principal(), but got %v", FakePrincipal, p)
	}
	if c := GetComponent(ef); c != FakeComponent {
		t.Errorf("Expect %v from ef.Component(), but got %v", FakeComponent, c)
	}
	if s := GetSubcomponent(ef); s != FakeSubcomponent {
		t.Errorf("Expect %v from ef.Subcomponent(), but got %v", FakeSubcomponent, s)
	}
}
<commit_msg>Fix test printings<commit_after>package callerid

import (
	"reflect"
	"testing"

	"golang.org/x/net/context"

	qrpb "github.com/youtube/vitess/go/vt/proto/query"
	vtpb "github.com/youtube/vitess/go/vt/proto/vtrpc"
)

const (
	// FakePrincipal is the principal of testing EffectiveCallerID
	FakePrincipal = "TestPrincipal"
	// FakeComponent is the component of testing EffectiveCallerID
	FakeComponent = "TestComponent"
	// FakeSubcomponent is the subcomponent of testing EffectiveCallerID
	FakeSubcomponent = "TestSubcomponent"
	// FakeUsername is the username of testing ImmediateCallerID
	FakeUsername = "TestUsername"
)

// Tests performs the necessary testsuite for CallerID operations
func Tests(t *testing.T, im *qrpb.VTGateCallerID, ef *vtpb.CallerID) {
	ctx := context.TODO()
	ctxim := ImmediateCallerIDFromContext(ctx)
	// For Contexts without ImmediateCallerID, ImmediateCallerIDFromContext should fail
	if ctxim != nil {
		t.Errorf("Expect nil from ImmediateCallerIDFromContext, but got %v", ctxim)
	}
	// For Contexts without EffectiveCallerID, EffectiveCallerIDFromContext should fail
	ctxef := EffectiveCallerIDFromContext(ctx)
	if ctxef != nil {
		t.Errorf("Expect nil from EffectiveCallerIDFromContext, but got %v", ctxef)
	}

	ctx = NewContext(ctx, nil, nil)
	ctxim = ImmediateCallerIDFromContext(ctx)
	// For Contexts with nil ImmediateCallerID, ImmediateCallerIDFromContext should fail
	if ctxim != nil {
		t.Errorf("Expect nil from ImmediateCallerIDFromContext, but got %v", ctxim)
	}
	// For Contexts with nil EffectiveCallerID, EffectiveCallerIDFromContext should fail
	ctxef = EffectiveCallerIDFromContext(ctx)
	if ctxef != nil {
		t.Errorf("Expect nil from EffectiveCallerIDFromContext, but got %v", ctxef)
	}

	// Test GetXxx on nil receivers, should get all empty strings
	if u := GetUsername(ctxim); u != "" {
		t.Errorf("Expect empty string from GetUsername(nil), but got %v", u)
	}
	if p := GetPrincipal(ctxef); p != "" {
		t.Errorf("Expect empty string from GetPrincipal(nil), but got %v", p)
	}
	if c := GetComponent(ctxef); c != "" {
		t.Errorf("Expect empty string from GetComponent(nil), but got %v", c)
	}
	if s := GetSubcomponent(ctxef); s != "" {
		t.Errorf("Expect empty string from GetSubcomponent(nil), but got %v", s)
	}

	ctx = NewContext(ctx, ef, im)
	ctxim = ImmediateCallerIDFromContext(ctx)
	// retrieved ImmediateCallerID should be equal to the one we put into Context
	if !reflect.DeepEqual(ctxim, im) {
		t.Errorf("Expect %v from ImmediateCallerIDFromContext, but got %v", im, ctxim)
	}
	if u := GetUsername(im); u != FakeUsername {
		t.Errorf("Expect %v from GetUsername(im), but got %v", FakeUsername, u)
	}

	ctxef = EffectiveCallerIDFromContext(ctx)
	// retrieved EffectiveCallerID should be equal to the one we put into Context
	if !reflect.DeepEqual(ctxef, ef) {
		t.Errorf("Expect %v from EffectiveCallerIDFromContext, but got %v", ef, ctxef)
	}
	if p := GetPrincipal(ef); p != FakePrincipal {
		t.Errorf("Expect %v from GetPrincipal(ef), but got %v", FakePrincipal, p)
	}
	if c := GetComponent(ef); c != FakeComponent {
		t.Errorf("Expect %v from GetComponent(ef), but got %v", FakeComponent, c)
	}
	if s := GetSubcomponent(ef); s != FakeSubcomponent {
		t.Errorf("Expect %v from GetSubcomponent(ef), but got %v", FakeSubcomponent, s)
	}
}
<|endoftext|>


<commit_before>package goltime

import (
  "strconv"
  "time"
  "net/http"
)

type Timestamp struct{
  Year, Month, Day, Hour, Min, Sec int
}

func CreateTimestamp(time_point []string) Timestamp{
  year, _   := strconv.Atoi(time_point[0])
  month, _  := strconv.Atoi(time_point[1])
  day, _    := strconv.Atoi(time_point[2])
  hour, _   := strconv.Atoi(time_point[3])
  min, _    := strconv.Atoi(time_point[4])
  sec, _    := strconv.Atoi(time_point[5])

  return Timestamp{
    Year: year,
    Month: month,
    Day: day,
    Hour: hour,
    Min: min,
    Sec: sec,
  }
}

func TimestampFromHTTPRequest(req *http.Request) Timestamp{
  return CreateTimestamp([]string {
    req.Form["year"][0], req.Form["month"][0], req.Form["day"][0],
    req.Form["hour"][0], req.Form["min"][0], req.Form["sec"][0],
  })
}

func (timestamp *Timestamp) Time() time.Time{
  return time.Date(timestamp.Year, time.Month(timestamp.Month), timestamp.Day,
                   timestamp.Hour, timestamp.Min, timestamp.Sec, 0, time.UTC)
}
<commit_msg>goltime go-fmt-ized<commit_after>package goltime

import (
	"net/http"
	"strconv"
	"time"
)

type Timestamp struct {
	Year, Month, Day, Hour, Min, Sec int
}

func CreateTimestamp(time_point []string) Timestamp {
	year, _ := strconv.Atoi(time_point[0])
	month, _ := strconv.Atoi(time_point[1])
	day, _ := strconv.Atoi(time_point[2])
	hour, _ := strconv.Atoi(time_point[3])
	min, _ := strconv.Atoi(time_point[4])
	sec, _ := strconv.Atoi(time_point[5])

	return Timestamp{
		Year:  year,
		Month: month,
		Day:   day,
		Hour:  hour,
		Min:   min,
		Sec:   sec,
	}
}

func TimestampFromHTTPRequest(req *http.Request) Timestamp {
	return CreateTimestamp([]string{
		req.Form["year"][0], req.Form["month"][0], req.Form["day"][0],
		req.Form["hour"][0], req.Form["min"][0], req.Form["sec"][0],
	})
}

func (timestamp *Timestamp) Time() time.Time {
	return time.Date(timestamp.Year, time.Month(timestamp.Month), timestamp.Day,
		timestamp.Hour, timestamp.Min, timestamp.Sec, 0, time.UTC)
}
<|endoftext|>