func TestNoResize(t *testing.T) { buffer := buffer.NewWithSize(4, true) n, _ := buffer.Write([]byte{1, 2, 3, 4, 5, 6}) if n != 4 { t.Errorf("Should have wrote 4, but wrote %d", n) } if buffer.Pointer != 4 { t.Errorf("Pointer should be at 4, but got %d", buffer.Pointer) } if buffer.Size != 4 { t.Errorf("Shouldn't have resized, but resized to %d", buffer.Size) } buffer.Seek(0, 0) buf := make([]byte, 10) n, _ = buffer.Read(buf) if n != 4 { t.Errorf("Should have read 4, but got %d", n) } if string(buf) != string([]byte{1, 2, 3, 4, 0, 0, 0, 0, 0, 0}) { t.Errorf("Didn't read what was written...") } }
func TestUnicode(t *testing.T) { SetupCluster() log.Info("Testing TestUnicode...") // Read write test path := fs.NewPath("/tests/io/writeunicode") byts := []byte("Some é unicode à data 世界") buf := bytes.NewBuffer(byts) err := tc.nodes[0].Fss.Write(path, int64(len(byts)), "", buf, nil) if err != nil { t.Errorf("1) Write returned an an error for: %s\n", err) } // Check if read data is the same as written bufwriter := bytes.NewBuffer(make([]byte, 0)) n, err := tc.nodes[2].Fss.Read(path, 0, -1, 0, io.Writer(bufwriter), nil) if n != int64(len(byts)) || bytes.Compare(bufwriter.Bytes(), byts) != 0 { t.Errorf("2) Didn't read written data correctly: %s!=%s\n", byts, bufwriter) } // Test unicode path path = fs.NewPath("/école èàaû/世界.txt") wrtnData := buffer.NewFromString("some data") err = tc.nodes[0].Fss.Write(path, wrtnData.Size, "", wrtnData, nil) if err != nil { t.Errorf("3) Error while writing: %s\n", err) } // Reading written data rdData := buffer.NewWithSize(0, false) n, err = tc.nodes[1].Fss.Read(path, 0, -1, 0, rdData, nil) if n != wrtnData.Size || bytes.Compare(rdData.Bytes(), wrtnData.Bytes()) != 0 { t.Errorf("4) Didn't read what had ben written: %s!=%s", rdData.Bytes(), wrtnData.Bytes()) } }
func (r *Message) readMessage(reader io.Reader) (err os.Error) { treader := typedio.NewReader(reader) r.Id, err = treader.ReadUint16() // message id if err != nil { return } flags, err := treader.ReadUint8() // flags if err != nil { return } hasInitId := false if flags&prm_has_init_msg_id == prm_has_init_msg_id { hasInitId = true } if flags&prm_is_data == prm_is_data { r.Type = T_DATA } else { r.Type = T_MSG } if flags&prm_src_node_adhoc == prm_src_node_adhoc { r.srcNodeAdhoc = true } if flags&prm_has_middle_node == prm_has_middle_node { r.middleNodePresent = true } if flags&prm_middle_node_adhoc == prm_middle_node_adhoc { r.middleNodeAdhoc = true } if hasInitId { r.InitId, err = treader.ReadUint16() // initial message id if err != nil { return } } r.ServiceId, err = treader.ReadUint8() // service id if err != nil { return } msgSize, err := treader.ReadUint16() // message size if err != nil { return } if r.Type == T_DATA { r.DataSize, err = treader.ReadInt64() // data size if err != nil { return } } if r.srcNodeAdhoc { // source node information adr, err := treader.ReadString() // addr if err != nil { return } r.srcNodeAdr = net.ParseIP(adr) r.srcNodeTcpPort, err = treader.ReadUint16() // tcp port if err != nil { return } r.srcNodeUdpPort, err = treader.ReadUint16() // udp port if err != nil { return } } else { r.srcNodeId, err = treader.ReadUint16() // node id if err != nil { return } } // TODO: Adhoc if r.middleNodePresent { if r.middleNodeAdhoc { adr, err := treader.ReadString() // addr if err != nil { return } r.middleNodeAdr = net.ParseIP(adr) r.middleNodeTcpPort, err = treader.ReadUint16() // tcp port if err != nil { return } r.middleNodeUdpPort, err = treader.ReadUint16() // udp port if err != nil { return } } else { r.middleNodeId, err = treader.ReadUint16() // node id if err != nil { return } } } r.FunctionId, err = treader.ReadUint8() // function id if err != nil { return } // Load message r.Message = buffer.NewWithSize(int64(msgSize), false) // message n, err := io.Copyn(r.Message, reader, int64(msgSize)) r.Message.Seek(0, 0) if err != nil { log.Error("COMM: Got an error reading message from message: %s", err) return err } if n != int64(msgSize) { log.Error("COMM: Couldn't read the whole message. Read %d out of %d", n, msgSize) return os.NewError("Message truncated") } // release the connection if its a message if r.Type == T_MSG { r.Release() } else { r.Data = reader } return nil }