netascii_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package netascii
  2. import (
  3. "bytes"
  4. "io/ioutil"
  5. "strings"
  6. "testing"
  7. "testing/iotest"
  8. )
  9. var basic = map[string]string{
  10. "\r": "\r\x00",
  11. "\n": "\r\n",
  12. "la\nbu": "la\r\nbu",
  13. "la\rbu": "la\r\x00bu",
  14. "\r\r\r": "\r\x00\r\x00\r\x00",
  15. "\n\n\n": "\r\n\r\n\r\n",
  16. }
  17. func TestTo(t *testing.T) {
  18. for text, netascii := range basic {
  19. to := ToReader(strings.NewReader(text))
  20. n, _ := ioutil.ReadAll(to)
  21. if bytes.Compare(n, []byte(netascii)) != 0 {
  22. t.Errorf("%q to netascii: %q != %q", text, n, netascii)
  23. }
  24. }
  25. }
  26. func TestFrom(t *testing.T) {
  27. for text, netascii := range basic {
  28. r := bytes.NewReader([]byte(netascii))
  29. b := &bytes.Buffer{}
  30. from := FromWriter(b)
  31. r.WriteTo(from)
  32. n, _ := ioutil.ReadAll(b)
  33. if string(n) != text {
  34. t.Errorf("%q from netascii: %q != %q", netascii, n, text)
  35. }
  36. }
  37. }
  38. const text = `
  39. Therefore, the sequence "CR LF" must be treated as a single "new
  40. line" character and used whenever their combined action is
  41. intended; the sequence "CR NUL" must be used where a carriage
  42. return alone is actually desired; and the CR character must be
  43. avoided in other contexts. This rule gives assurance to systems
  44. which must decide whether to perform a "new line" function or a
  45. multiple-backspace that the TELNET stream contains a character
  46. following a CR that will allow a rational decision.
  47. (in the default ASCII mode), to preserve the symmetry of the
  48. NVT model. Even though it may be known in some situations
  49. (e.g., with remote echo and suppress go ahead options in
  50. effect) that characters are not being sent to an actual
  51. printer, nonetheless, for the sake of consistency, the protocol
  52. requires that a NUL be inserted following a CR not followed by
  53. a LF in the data stream. The converse of this is that a NUL
  54. received in the data stream after a CR (in the absence of
  55. options negotiations which explicitly specify otherwise) should
  56. be stripped out prior to applying the NVT to local character
  57. set mapping.
  58. `
  59. func TestWriteRead(t *testing.T) {
  60. var one bytes.Buffer
  61. to := ToReader(strings.NewReader(text))
  62. one.ReadFrom(to)
  63. two := &bytes.Buffer{}
  64. from := FromWriter(two)
  65. one.WriteTo(from)
  66. text2, _ := ioutil.ReadAll(two)
  67. if text != string(text2) {
  68. t.Errorf("text mismatch \n%x \n%x", text, text2)
  69. }
  70. }
  71. func TestOneByte(t *testing.T) {
  72. var one bytes.Buffer
  73. to := iotest.OneByteReader(ToReader(strings.NewReader(text)))
  74. one.ReadFrom(to)
  75. two := &bytes.Buffer{}
  76. from := FromWriter(two)
  77. one.WriteTo(from)
  78. text2, _ := ioutil.ReadAll(two)
  79. if text != string(text2) {
  80. t.Errorf("text mismatch \n%x \n%x", text, text2)
  81. }
  82. }