作者:dbabit
项目:mint-clien
func csv2String() string {
buf := new(bytes.Buffer)
for _, pub := range csv2.pubkeys {
buf.WriteString(fmt.Sprintf("%s,\n", pub))
}
return string(buf.Bytes())
}
作者:luizbafilh
项目:fusi
// 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
}
作者:leobc
项目:gr
// 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
}
作者:anuvazhayi
项目:HelloWorld_32bitO
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)
}
}
}
作者:GitGoldi
项目:cockroac
// 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())
}
}
作者:vanphong378
项目:rclon
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)
}
}
作者:pse
项目:ato
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)
}
}
}
作者:nkwangleiGI
项目:dockerboar
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()))
}
作者:balamurugan
项目:mini
// 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 := ¬ificationConfig{}
if err = xml.Unmarshal(notificationConfigBytes, ¬ificationCfg); err != nil {
return nil, err
}
// Return success.
return notificationCfg, nil
}
作者:kostyl
项目:gccp
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)
}
}
}
作者:Roasbee
项目:btc
// 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)
}
作者:japettyjoh
项目:layoutcs
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)
}
}
作者:JonPulfe
项目:pma
// 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)
}
}
作者:IntegerCompan
项目:linaro-android-gc
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())
}
}
作者:topher20
项目:forty-thieve
// 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
}
作者:vipwz
项目:gocoi
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"))
}
}
作者:se77e
项目:rabbitf
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)
}
作者:kakamessi9
项目:juj
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")
}
作者:vanphong378
项目:rclon
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)
}
}
作者:Gleason
项目:g
// 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: "[email protected]#$%^&*()_+ `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
}