Exemplo n.º 1
2
// bigIntToNetIPv6 is a helper function that correctly returns a net.IP with the
// correctly padded values.
func bigIntToNetIPv6(bi *big.Int) *net.IP {
	x := make(net.IP, IPv6len)
	ipv6Bytes := bi.Bytes()

	// It's possibe for ipv6Bytes to be less than IPv6len bytes in size.  If
	// they are different sizes we to pad the size of response.
	if len(ipv6Bytes) < IPv6len {
		buf := new(bytes.Buffer)
		buf.Grow(IPv6len)

		for i := len(ipv6Bytes); i < IPv6len; i++ {
			if err := binary.Write(buf, binary.BigEndian, byte(0)); err != nil {
				panic(fmt.Sprintf("Unable to pad byte %d of input %v: %v", i, bi, err))
			}
		}

		for _, b := range ipv6Bytes {
			if err := binary.Write(buf, binary.BigEndian, b); err != nil {
				panic(fmt.Sprintf("Unable to preserve endianness of input %v: %v", bi, err))
			}
		}

		ipv6Bytes = buf.Bytes()
	}
	i := copy(x, ipv6Bytes)
	if i != IPv6len {
		panic("IPv6 wrong size")
	}
	return &x
}
Exemplo n.º 2
2
func csv2String() string {
	buf := new(bytes.Buffer)
	for _, pub := range csv2.pubkeys {
		buf.WriteString(fmt.Sprintf("%s,\n", pub))
	}
	return string(buf.Bytes())
}
Exemplo n.º 3
1
Arquivo: etcd.go Projeto: leobcn/gru
// Processes new tasks
func (m *etcdMinion) processTask(t *task.Task) error {
	var buf bytes.Buffer

	// Update state of task to indicate that we are now processing it
	t.State = task.TaskStateProcessing
	m.SaveTaskResult(t)

	cmd := exec.Command(t.Command, t.Args...)
	cmd.Stdout = &buf
	cmd.Stderr = &buf

	log.Printf("Processing task %s\n", t.TaskID)

	cmdError := cmd.Run()
	t.TimeProcessed = time.Now().Unix()
	t.Result = buf.String()

	if cmdError != nil {
		log.Printf("Failed to process task %s\n", t.TaskID)
		t.Error = cmdError.Error()
		t.State = task.TaskStateFailed
	} else {
		log.Printf("Finished processing task %s\n", t.TaskID)
		t.State = task.TaskStateSuccess
	}

	m.SaveTaskResult(t)

	return cmdError
}
Exemplo n.º 4
1
func TestDumper(t *testing.T) {
	var in [40]byte
	for i := range in {
		in[i] = byte(i + 30)
	}

	for stride := 1; stride < len(in); stride++ {
		var out bytes.Buffer
		dumper := Dumper(&out)
		done := 0
		for done < len(in) {
			todo := done + stride
			if todo > len(in) {
				todo = len(in)
			}
			dumper.Write(in[done:todo])
			done = todo
		}

		dumper.Close()
		if !bytes.Equal(out.Bytes(), expectedHexDump) {
			t.Errorf("stride: %d failed. got:\n%s\nwant:\n%s", stride, out.Bytes(), expectedHexDump)
		}
	}
}
Exemplo n.º 5
1
// Format implements the NodeFormatter interface.
func (node IndexElem) Format(buf *bytes.Buffer, f FmtFlags) {
	FormatNode(buf, f, node.Column)
	if node.Direction != DefaultDirection {
		buf.WriteByte(' ')
		buf.WriteString(node.Direction.String())
	}
}
Exemplo n.º 6
1
func TestMd5sum(t *testing.T) {
	r := NewRun(t)
	defer r.Finalise()
	file1 := r.WriteBoth("potato2", "------------------------------------------------------------", t1)
	file2 := r.WriteBoth("empty space", "", t2)

	fstest.CheckItems(t, r.fremote, file1, file2)

	var buf bytes.Buffer
	err := fs.Md5sum(r.fremote, &buf)
	if err != nil {
		t.Fatalf("List failed: %v", err)
	}
	res := buf.String()
	if !strings.Contains(res, "d41d8cd98f00b204e9800998ecf8427e  empty space\n") &&
		!strings.Contains(res, "                     UNSUPPORTED  empty space\n") &&
		!strings.Contains(res, "                                  empty space\n") {
		t.Errorf("empty space missing: %q", res)
	}
	if !strings.Contains(res, "6548b156ea68a4e003e786df99eee76  potato2\n") &&
		!strings.Contains(res, "                     UNSUPPORTED  potato2\n") &&
		!strings.Contains(res, "                                  potato2\n") {
		t.Errorf("potato2 missing: %q", res)
	}
}
Exemplo n.º 7
1
Arquivo: task.go Projeto: psev/atom
func (self *Task) Render() {
	for _, config := range self.Config.Templates {
		log.Printf("Render: %s", config.Dst)
		f, err := ioutil.ReadFile(self.Controller.Atom.Config.TmplDir + "/" + config.Src)
		if err != nil {
			log.Printf("Error:: Read - %s", err)
			continue
		}
		var output *bytes.Buffer
		if self.Controller.Atom.Config.Verbose {
			output = bytes.NewBuffer(nil)
		}
		src := bytes.NewBuffer(f)
		tmpl, err := template.New("").Funcs(self.Config.Store.FuncMap).Parse(src.String())
		if err != nil {
			log.Printf("Error:: Render - %s", err)
			continue
		}
		vars := map[string]interface{}{"cli": self.Controller.Atom.CLI}
		if self.Controller.Atom.Config.Verbose {
			if err := tmpl.Execute(output, vars); err != nil {
				log.Printf("Error:: Write - %s", err)
			}
			fmt.Print(output.String())
		}
		dst, err := os.Create(config.Dst)
		if err != nil {
			log.Printf("Error:: Open - %s", err)
			continue
		}
		if err := tmpl.Execute(dst, vars); err != nil {
			log.Printf("Error:: Write - %s", err)
		}
	}
}
Exemplo n.º 8
0
func TestProgressWriterIgnoreTotal(t *testing.T) {
	filename := "progress_test.go"
	f, err := os.Open(filename)
	defer f.Close()
	if err != nil {
		log.Fatalln(err)
	}
	fs, err := os.Stat(filename)
	if err != nil {
		log.Fatalln(err)
	}

	p := New()
	p.IgnoreTotal = true
	p.Progress = func(current, total, expected int64) {
		log.Println("Ignore total writing", current, total, expected)
		assert.Equal(t, true, current >= total)
	}

	b := new(bytes.Buffer)
	w := io.MultiWriter(p, b)
	_, err = io.Copy(w, f)
	if err != nil {
		log.Fatalln(err)
	}
	assert.Equal(t, fs.Size(), int64(b.Len()))
}
Exemplo n.º 9
0
// loads notification config if any for a given bucket, returns
// structured notification config.
func loadNotificationConfig(bucket string, objAPI ObjectLayer) (*notificationConfig, error) {
	// Construct the notification config path.
	ncPath := path.Join(bucketConfigPrefix, bucket, bucketNotificationConfig)

	// Acquire a write lock on notification config before modifying.
	objLock := globalNSMutex.NewNSLock(minioMetaBucket, ncPath)
	objLock.RLock()
	defer objLock.RUnlock()

	var buffer bytes.Buffer
	err := objAPI.GetObject(minioMetaBucket, ncPath, 0, -1, &buffer) // Read everything.
	if err != nil {
		// 'notification.xml' not found return
		// 'errNoSuchNotifications'.  This is default when no
		// bucket notifications are found on the bucket.
		if isErrObjectNotFound(err) || isErrIncompleteBody(err) {
			return nil, errNoSuchNotifications
		}
		errorIf(err, "Unable to load bucket-notification for bucket %s", bucket)
		// Returns error for other errors.
		return nil, err
	}

	// Unmarshal notification bytes.
	notificationConfigBytes := buffer.Bytes()
	notificationCfg := &notificationConfig{}
	if err = xml.Unmarshal(notificationConfigBytes, &notificationCfg); err != nil {
		return nil, err
	}

	// Return success.
	return notificationCfg, nil
}
Exemplo n.º 10
0
func TestDecompressor(t *testing.T) {
	b := new(bytes.Buffer)
	for _, tt := range zlibTests {
		in := bytes.NewBuffer(tt.compressed)
		zlib, err := NewReaderDict(in, tt.dict)
		if err != nil {
			if err != tt.err {
				t.Errorf("%s: NewReader: %s", tt.desc, err)
			}
			continue
		}
		defer zlib.Close()
		b.Reset()
		n, err := io.Copy(b, zlib)
		if err != nil {
			if err != tt.err {
				t.Errorf("%s: io.Copy: %v want %v", tt.desc, err, tt.err)
			}
			continue
		}
		s := b.String()
		if s != tt.raw {
			t.Errorf("%s: got %d-byte %q want %d-byte %q", tt.desc, n, s, len(tt.raw), tt.raw)
		}
	}
}
Exemplo n.º 11
0
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
// This is part of the Message interface implementation.
func (msg *MsgAlert) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
	var err error
	var serializedpayload []byte
	if msg.Payload != nil {
		// try to Serialize Payload if possible
		r := new(bytes.Buffer)
		err = msg.Payload.Serialize(r, pver)
		if err != nil {
			// Serialize failed - ignore & fallback
			// to SerializedPayload
			serializedpayload = msg.SerializedPayload
		} else {
			serializedpayload = r.Bytes()
		}
	} else {
		serializedpayload = msg.SerializedPayload
	}
	slen := uint64(len(serializedpayload))
	if slen == 0 {
		return messageError("MsgAlert.BtcEncode", "empty serialized payload")
	}
	err = WriteVarBytes(w, pver, serializedpayload)
	if err != nil {
		return err
	}
	return WriteVarBytes(w, pver, msg.Signature)
}
Exemplo n.º 12
0
func main() {
	var (
		config Config
	)

	configFileName := flag.String("config", "", "Config file")
	headerReport := flag.Bool("headerReport", false, "Produce a report of header mappings")
	flag.Parse()

	configFile, err := os.Open(*configFileName)
	if err != nil {
		fmt.Println("Error opening config:", err)
		return
	}
	defer configFile.Close()
	configBuf := new(bytes.Buffer)
	configBuf.ReadFrom(configFile)
	xml.Unmarshal(configBuf.Bytes(), &config)

	// Parse templates
	textTemplates, err := makeTemplates(&config.TemplateConfig)
	if err != nil {
		panic(err)
	}

	// Process each input file config
	for _, fileConfig := range config.FileConfig {
		if *headerReport {
			processHeader(fileConfig, textTemplates)
			continue
		}
		processFile(fileConfig, textTemplates)
	}
}
Exemplo n.º 13
0
// Method Close encrypts and writes the encrypted data to the key file
func (k *KeyStore) Close(secret []byte) {
	// Encode a gob of the keystore
	var buff bytes.Buffer
	enc := gob.NewEncoder(&buff)
	enc.Encode(k)
	buffString := buff.String()

	// Encrypt the data
	block, err := aes.NewCipher(secret)
	if err != nil {
		panic(err)
	}
	ciphertext := make([]byte, aes.BlockSize+len(buffString))
	iv := ciphertext[:aes.BlockSize]
	if _, err := io.ReadFull(rand.Reader, iv); err != nil {
		panic(err)
	}
	stream := cipher.NewCFBEncrypter(block, iv)
	stream.XORKeyStream(ciphertext[aes.BlockSize:], []byte(buffString))

	// Write the encrypted data to the file
	kFile, err := os.OpenFile(kf, os.O_WRONLY, 0644)
	if err != nil {
		panic(err)
	}
	bytesWritten, err := kFile.Write(ciphertext)
	if err != nil || bytesWritten == 0 {
		panic(err)
	}
}
Exemplo n.º 14
0
func TestMultiWriter(t *testing.T) {
	sha1 := sha1.New()
	sink := new(bytes.Buffer)
	mw := MultiWriter(sha1, sink)

	sourceString := "My input text."
	source := strings.NewReader(sourceString)
	written, err := Copy(mw, source)

	if written != int64(len(sourceString)) {
		t.Errorf("short write of %d, not %d", written, len(sourceString))
	}

	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	sha1hex := fmt.Sprintf("%x", sha1.Sum())
	if sha1hex != "01cb303fa8c30a64123067c5aa6284ba7ec2d31b" {
		t.Error("incorrect sha1 value")
	}

	if sink.String() != sourceString {
		t.Errorf("expected %q; got %q", sourceString, sink.String())
	}
}
Exemplo n.º 15
0
// SaveGameState saves the current gamestate for a user. Does not delete old gamestates.
func (db *GameStateDB) SaveGameState(
	tx *sqlx.Tx, userRow UserRow, gameState libgame.GameState) error {
	var binarizedState bytes.Buffer
	encoder := gob.NewEncoder(&binarizedState)
	encoder.Encode(gameState)
	dataStruct := GameStateRow{}
	dataStruct.UserID = userRow.ID
	dataStruct.BinarizedState = binarizedState.Bytes()

	dataMap := make(map[string]interface{})
	dataMap["user_id"] = dataStruct.UserID
	dataMap["binarized_state"] = dataStruct.BinarizedState
	insertResult, err := db.InsertIntoTable(tx, dataMap)
	if err != nil {
		logrus.Warning("error saving game state:", err)
		return err
	}
	rowsAffected, err := insertResult.RowsAffected()
	if err != nil || rowsAffected != 1 {
		return errors.New(
			fmt.Sprintf("expected to change 1 row, changed %d", insertResult.RowsAffected))
	}

	id, err := insertResult.LastInsertId()
	logrus.Infof("Saved new gamestate (id %d) to db", id)
	return nil
}
Exemplo n.º 16
0
func dl_balance(w http.ResponseWriter, r *http.Request) {
	if !ipchecker(r) {
		return
	}

	wallet.UpdateBalanceFolder()
	buf := new(bytes.Buffer)
	zi := zip.NewWriter(buf)
	filepath.Walk("balance/", func(path string, fi os.FileInfo, err error) error {
		if !fi.IsDir() {
			f, _ := zi.Create(path)
			if f != nil {
				da, _ := ioutil.ReadFile(path)
				f.Write(da)
			}
		}
		return nil
	})
	if zi.Close() == nil {
		w.Header()["Content-Type"] = []string{"application/zip"}
		w.Write(buf.Bytes())
	} else {
		w.Write([]byte("Error"))
	}
}
Exemplo n.º 17
0
func (ss *StoreServer) uploadHandler(w http.ResponseWriter, r *http.Request) {
	defer r.Body.Close()
	vars := mux.Vars(r)
	fileIDStr := vars["fileID"]
	volID, needleID, cookie, err := newFileID(fileIDStr)
	if err != nil {
		helper.WriteJson(w, result{Error: err.Error()}, http.StatusInternalServerError)
		return
	}
	if ss.volumeMap[volID] == nil {
		helper.WriteJson(w, result{Error: fmt.Sprintf("no volume %d", volID)}, http.StatusInternalServerError)
		return
	}
	data, name, err := parseUpload(r)
	if err != nil {
		helper.WriteJson(w, result{Error: err.Error()}, http.StatusInternalServerError)
		return
	}
	n := storage.NewNeedle(cookie, needleID, data, name)
	if err = ss.volumeMap[volID].AppendNeedle(n); err != nil {
		helper.WriteJson(w, result{Error: err.Error()}, http.StatusInternalServerError)
		return
	}

	fi, _ := ss.volumeMap[volID].StoreFile.Stat()
	vi := volumeInfo{
		ID:   volID,
		Size: fi.Size(),
	}
	viBytes, _ := json.Marshal(vi)
	for i := range ss.conf.Directories { // send volume information to directory server
		var b bytes.Buffer
		b.Write(viBytes)
		_, err := postAndError("http://"+ss.conf.Directories[i]+"/vol/info", "application/json", &b)
		if err == nil {
			break
		} else {
			log4go.Warn("send volumeInfo to directory get err: %s", err.Error())
		}
	}
	for _, localVolIDIP := range ss.localVolIDIPs {
		if localVolIDIP.ID == volID {
			for _, ip := range localVolIDIP.IP {
				if ip != ss.Addr {
					if err = replicateUpload(fmt.Sprintf("http://%s/replicate/%s", ip, fileIDStr), string(name), data); err != nil {
						helper.WriteJson(w, result{Error: err.Error()}, http.StatusInternalServerError)
						return
					}
				}
			}
			break
		}
	}
	res := result{
		Name: string(name),
		Size: len(data),
	}
	helper.WriteJson(w, res, http.StatusOK)

}
Exemplo n.º 18
0
func (s *charmsSuite) TestGetUsesCache(c *gc.C) {
	// Add a fake charm archive in the cache directory.
	cacheDir := filepath.Join(s.DataDir(), "charm-get-cache")
	err := os.MkdirAll(cacheDir, 0755)
	c.Assert(err, jc.ErrorIsNil)

	// Create and save a bundle in it.
	charmDir := testcharms.Repo.ClonedDir(c.MkDir(), "dummy")
	testPath := filepath.Join(charmDir.Path, "utils.js")
	contents := "// blah blah"
	err = ioutil.WriteFile(testPath, []byte(contents), 0755)
	c.Assert(err, jc.ErrorIsNil)
	var buffer bytes.Buffer
	err = charmDir.ArchiveTo(&buffer)
	c.Assert(err, jc.ErrorIsNil)
	charmArchivePath := filepath.Join(
		cacheDir, charm.Quote("local:trusty/django-42")+".zip")
	err = ioutil.WriteFile(charmArchivePath, buffer.Bytes(), 0644)
	c.Assert(err, jc.ErrorIsNil)

	// Ensure the cached contents are properly retrieved.
	uri := s.charmsURI(c, "?url=local:trusty/django-42&file=utils.js")
	resp, err := s.authRequest(c, "GET", uri, "", nil)
	c.Assert(err, jc.ErrorIsNil)
	s.assertGetFileResponse(c, resp, contents, "application/javascript")
}
Exemplo n.º 19
0
func TestSha1sum(t *testing.T) {
	r := NewRun(t)
	defer r.Finalise()
	file1 := r.WriteBoth("potato2", "------------------------------------------------------------", t1)
	file2 := r.WriteBoth("empty space", "", t2)

	fstest.CheckItems(t, r.fremote, file1, file2)

	var buf bytes.Buffer
	err := fs.Sha1sum(r.fremote, &buf)
	if err != nil {
		t.Fatalf("List failed: %v", err)
	}
	res := buf.String()
	if !strings.Contains(res, "da39a3ee5e6b4b0d3255bfef95601890afd80709  empty space\n") &&
		!strings.Contains(res, "                             UNSUPPORTED  empty space\n") &&
		!strings.Contains(res, "                                          empty space\n") {
		t.Errorf("empty space missing: %q", res)
	}
	if !strings.Contains(res, "9dc7f7d3279715991a22853f5981df582b7f9f6d  potato2\n") &&
		!strings.Contains(res, "                             UNSUPPORTED  potato2\n") &&
		!strings.Contains(res, "                                          potato2\n") {
		t.Errorf("potato2 missing: %q", res)
	}
}
Exemplo n.º 20
0
// InitComplexMessage initializes a complex structure of the
// type CustomComplexMessage which includes a xml, struct of type PubnubDemoMessage,
// strings, float and integer.
func InitComplexMessage() CustomComplexMessage {
	pubnubDemoMessage := PubnubDemoMessage{
		DefaultMessage: "~!@#$%^&*()_+ `1234567890-= qwertyuiop[]\\ {}| asdfghjkl;' :\" zxcvbnm,./ <>? ",
	}

	xmlDoc := &Data{Name: "Doe", Age: 42}

	//_, err := xml.MarshalIndent(xmlDoc, "  ", "    ")
	//output, err := xml.MarshalIndent(xmlDoc, "  ", "    ")
	output := new(bytes.Buffer)
	enc := xml.NewEncoder(output)

	err := enc.Encode(xmlDoc)
	if err != nil {
		fmt.Printf("error: %v\n", err)
		return CustomComplexMessage{}
	}
	//fmt.Printf("xmlDoc: %v\n", xmlDoc)
	customComplexMessage := CustomComplexMessage{
		VersionID:     3.4,
		TimeToken:     13601488652764619,
		OperationName: "Publish",
		Channels:      []string{"ch1", "ch 2"},
		DemoMessage:   pubnubDemoMessage,
		//SampleXml        : xmlDoc,
		SampleXML: output.String(),
	}
	return customComplexMessage
}
Exemplo n.º 21
0
func TestProgressReader(t *testing.T) {
	filename := "progress_test.go"
	f, err := os.Open(filename)
	defer f.Close()
	if err != nil {
		log.Fatalln(err)
	}
	fs, err := os.Stat(filename)
	if err != nil {
		log.Fatalln(err)
	}

	p := New()
	p.Total = fs.Size()
	p.Progress = func(current, total, expected int64) {
		log.Println("Reading", current, total, expected)
		assert.Equal(t, true, current <= total)
	}

	b := new(bytes.Buffer)
	r := syncreader.New(f, p)
	_, err = b.ReadFrom(r)
	if err != nil {
		log.Fatalln(err)
	}
	assert.Equal(t, fs.Size(), int64(b.Len()))
}
Exemplo n.º 22
0
func TestParseCommand_History(t *testing.T) {
	t.Parallel()
	c := cli.CommandLine{Line: liner.NewLiner()}
	defer c.Line.Close()

	// append one entry to history
	c.Line.AppendHistory("abc")

	tests := []struct {
		cmd string
	}{
		{cmd: "history"},
		{cmd: " history"},
		{cmd: "history "},
		{cmd: "History "},
	}

	for _, test := range tests {
		if err := c.ParseCommand(test.cmd); err != nil {
			t.Fatalf(`Got error %v for command %q, expected nil.`, err, test.cmd)
		}
	}

	// buf size should be at least 1
	var buf bytes.Buffer
	c.Line.WriteHistory(&buf)
	if buf.Len() < 1 {
		t.Fatal("History is borked")
	}
}
Exemplo n.º 23
0
// Format implements the NodeFormatter interface.
func (node *CreateDatabase) Format(buf *bytes.Buffer, f FmtFlags) {
	buf.WriteString("CREATE DATABASE ")
	if node.IfNotExists {
		buf.WriteString("IF NOT EXISTS ")
	}
	FormatNode(buf, f, node.Name)
}
Exemplo n.º 24
0
func TestPixels(t *testing.T) {
	rgba := decodeFile(t, "rgba.png")
	allExpected := []*Pixel{
		{Red: 255},
		{Green: 255},
		{Blue: 255},
		{Opacity: 127},
	}
	leftExpected := []*Pixel{
		allExpected[0],
		allExpected[2],
	}
	rightExpected := []*Pixel{
		allExpected[1],
		allExpected[3],
	}
	runPixelTests := func() {
		px1, err := rgba.Pixels(rgba.Rect())
		if err != nil {
			t.Fatal(err)
		}
		if !reflect.DeepEqual(px1, allExpected) {
			t.Errorf("expecting pixels %v, got %v instead", allExpected, px1)
		}
		px2, err := rgba.Pixels(Rect{Width: 1, Height: 2})
		if err != nil {
			t.Fatal(err)
		}
		if !reflect.DeepEqual(px2, leftExpected) {
			t.Errorf("expecting left pixels %v, got %v instead", leftExpected, px2)
		}
		px3, err := rgba.Pixels(Rect{X: 1, Width: 1, Height: 2})
		if err != nil {
			t.Fatal(err)
		}
		if !reflect.DeepEqual(px3, rightExpected) {
			t.Errorf("expecting right pixels %v, got %v instead", rightExpected, px3)
		}
	}
	// First test with the image as it's loaded
	runPixelTests()
	// Change the green pixel to blue and test again
	if err := rgba.SetPixel(1, 0, &Pixel{Blue: 255}); err != nil {
		t.Fatal(err)
	}
	allExpected[1].Green = 0
	allExpected[1].Blue = 255
	runPixelTests()
	// Encode the image, decode it and check again
	var buf bytes.Buffer
	if err := rgba.Encode(&buf, nil); err != nil {
		t.Fatal(err)
	}
	var err error
	rgba, err = DecodeData(buf.Bytes())
	if err != nil {
		t.Fatal(err)
	}
	runPixelTests()
}
Exemplo n.º 25
0
func (options *Html) BlockCode(out *bytes.Buffer, text []byte, lang string) {
	doubleSpace(out)

	// parse out the language names/classes
	count := 0
	for _, elt := range strings.Fields(lang) {
		if elt[0] == '.' {
			elt = elt[1:]
		}
		if len(elt) == 0 {
			continue
		}
		if count == 0 {
			out.WriteString("<pre><code class=\"language-")
		} else {
			out.WriteByte(' ')
		}
		attrEscape(out, []byte(elt))
		count++
	}

	if count == 0 {
		out.WriteString("<pre><code>")
	} else {
		out.WriteString("\">")
	}

	attrEscape(out, text)
	out.WriteString("</code></pre>\n")
}
Exemplo n.º 26
0
// Helper function to test that a value is marshalled to JSON as expected.
func testJSONMarshal(t *testing.T, v interface{}, want string) {
	j, err := json.Marshal(v)
	if err != nil {
		t.Errorf("Unable to marshal JSON for %v", v)
	}

	w := new(bytes.Buffer)
	err = json.Compact(w, []byte(want))
	if err != nil {
		t.Errorf("String is not valid json: %s", want)
	}

	if w.String() != string(j) {
		t.Errorf("json.Marshal(%q) returned %s, want %s", v, j, w)
	}

	// now go the other direction and make sure things unmarshal as expected
	u := reflect.ValueOf(v).Interface()
	if err := json.Unmarshal([]byte(want), u); err != nil {
		t.Errorf("Unable to unmarshal JSON for %v", want)
	}

	if !reflect.DeepEqual(v, u) {
		t.Errorf("json.Unmarshal(%q) returned %s, want %s", want, u, v)
	}
}
Exemplo n.º 27
0
func csv1String() string {
	buf := new(bytes.Buffer)
	for i, pub := range csv1.pubkeys {
		buf.WriteString(fmt.Sprintf("%s,%d,%s,%d,%d\n", pub, csv1.amts[i], csv1.names[i], csv1.perms[i], csv1.setbits[i]))
	}
	return string(buf.Bytes())
}
Exemplo n.º 28
0
// TODO Make this private, since we should be able to test against BuildStatement()
func (c *Context) RenderCQL() (string, error) {

	var buf bytes.Buffer

	// TODO This should be a switch
	switch c.Operation {
	case ReadOperation:
		{
			renderSelect(c, &buf)
		}
	case WriteOperation:
		{
			if c.hasConditions() {
				renderUpdate(c, &buf, false)
			} else {
				renderInsert(c, &buf)
			}

			renderCAS(c, &buf)
		}
	case CounterOperation:
		{
			renderUpdate(c, &buf, true)
		}
	case DeleteOperation:
		{
			renderDelete(c, &buf)
		}
	default:
		return "", fmt.Errorf("Unknown operation type: %s", c.Operation)
	}

	return buf.String(), nil
}
Exemplo n.º 29
0
func (g *GpgCLI) Sign(fp PGPFingerprint, payload []byte) (string, error) {
	g.outputVersion()
	arg := RunGpg2Arg{
		Arguments: []string{"--armor", "--sign", "-u", fp.String()},
		Stdout:    true,
		Stdin:     true,
	}

	res := g.Run2(arg)
	if res.Err != nil {
		return "", res.Err
	}

	res.Stdin.Write(payload)
	res.Stdin.Close()

	buf := new(bytes.Buffer)
	buf.ReadFrom(res.Stdout)
	armored := buf.String()

	// Convert to posix style on windows
	armored = PosixLineEndings(armored)

	if err := res.Wait(); err != nil {
		return "", err
	}

	return armored, nil
}
Exemplo n.º 30
0
func TestEncoding(t *testing.T) {
	msgs := []raftpb.Message{
		{To: 1, From: 1},
		{To: 2, From: 2, Entries: []raftpb.Entry{{Term: 2, Data: []byte{2}}}},
		{To: 3, From: 3},
		{To: 4, From: 4, Entries: []raftpb.Entry{{Term: 2, Data: []byte{4, 4}}}},
	}
	s := 0
	var buf bytes.Buffer
	enc := NewEncoder(&buf)
	for _, m := range msgs {
		s += m.Size()
		if err := enc.Encode(m); err != nil {
			t.Fatalf("cannot encode message: %v", err)
		}
	}
	b := buf.Bytes()
	if len(b) != s+len(msgs)*4 {
		t.Errorf("invalid number of bytes: actual=%d want=%d", len(b),
			s+len(msgs)*4)
	}
	dec := NewDecoder(&buf)
	dmsgs := make([]raftpb.Message, len(msgs))
	for i := range dmsgs {
		if err := dec.Decode(&dmsgs[i]); err != nil {
			t.Fatalf("cannot decode: %v", err)
			continue
		}
		if !reflect.DeepEqual(msgs[i], dmsgs[i]) {
			t.Errorf("invalid decoded message: actual=%#v want=%#v", msgs[i],
				dmsgs[i])
		}
	}
}