func TestWritev(t *testing.T) { f, err := os.Create("testwritev") if err != nil { panic(err) } defer f.Close() data := [][]byte{[]byte("foo"), []byte("bar"), []byte("baz")} nw, err := vectorio.Writev(f, data) f.Seek(0, 0) if err != nil { t.Errorf("WritevRaw threw error: %s", err) } if nw != 9 { t.Errorf("Length %d of input does not match %d written bytes", len(data), nw) } fromdisk, err := ioutil.ReadAll(f) if err != nil { t.Errorf("can't read file back, %s", err) } should := []byte("foobarbaz") if bytes.Compare(fromdisk, should) != 0 { t.Errorf("contents of file don't match input, %s != %s", fromdisk, should) } os.Remove("testwritev") }
func TestWritevSocket(t *testing.T) { go func() { ln, err := net.Listen("tcp", "127.0.0.1:9999") if err != nil { t.Errorf("could not listen on 127.0.0.1:9999: %s", err) } conn, err := ln.Accept() if err != nil { t.Errorf("could not accept conn: %s", err) } defer conn.Close() buf := make([]byte, 9) nr, err := conn.Read(buf) if nr != len(buf) { t.Errorf("read was wrong length: %d != %d", nr, len(buf)) } good := []byte("foobarbaz") if bytes.Compare(buf, good) != 0 { t.Errorf("read got wrong data: %s != %s", buf, good) } else { t.Logf("read got correct data: %s == %s", buf, good) } }() time.Sleep(1 * time.Second) addr, _ := net.ResolveTCPAddr("tcp4", "127.0.0.1:9999") conn, err := net.DialTCP("tcp4", nil, addr) if err != nil { t.Errorf("error connecting to 127.0.0.1:9999: %s", err) } else { t.Logf("connected to server") } defer conn.Close() data := [][]byte{[]byte("foo"), []byte("bar"), []byte("baz")} f, err := conn.File() if err != nil { t.Errorf("could not get file handle for TCP client: %s", err) } defer f.Close() nw, err := vectorio.Writev(f, data) f.Seek(0, 0) if err != nil { t.Errorf("WritevRaw threw error: %s", err) } if nw != 9 { t.Errorf("Length %d of input does not match %d written bytes", len(data), nw) } else { t.Logf("right number of bytes written") } time.Sleep(1 * time.Second) }
// Write sends a log with prio, tag and message to Android's kernel logger. // // Returns an error if writing to the kernel logger fails. func (self *LoggerWriter) Write(prio Priority, tag Tag, message string) error { iov := [][]byte{ []byte{byte(prio)}, []byte(tag), []byte(message), } // Both the tag and the message need to be null-terminated iov[1] = append(iov[1], '\x00') iov[2] = append(iov[2], '\x00') _, err := vectorio.Writev(self.f, iov) return err }