testutil_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Public Domain Software
  3. *
  4. * I (Matthias Ladkau) am the author of the source code in this file.
  5. * I have placed the source code in this file in the public domain.
  6. *
  7. * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. package testutil
  10. import (
  11. "bytes"
  12. "encoding/gob"
  13. "strings"
  14. "testing"
  15. "time"
  16. )
  17. func TestGetCaller(t *testing.T) {
  18. var name, loc string
  19. foo := func() {
  20. // Foo asks who called me?
  21. name, loc = GetCaller(0)
  22. }
  23. foo()
  24. // Answer should be TestGetCaller in file testutil_test.go line 30
  25. if !strings.Contains(name, "devt.de/krotik/common/testutil.TestGetCaller") {
  26. t.Error("Unexpected result:", name)
  27. }
  28. if !strings.Contains(loc, "testutil_test.go:30") {
  29. t.Error("Unexpected result:", loc)
  30. }
  31. }
  32. func TestErrorTestingConnection(t *testing.T) {
  33. c := &ErrorTestingConnection{}
  34. // Check methods which do nothing
  35. c.Close()
  36. c.LocalAddr()
  37. c.RemoteAddr()
  38. c.SetDeadline(time.Now())
  39. c.SetReadDeadline(time.Now())
  40. c.SetWriteDeadline(time.Now())
  41. c.In.WriteString("This is a test")
  42. c.InErr = 4
  43. tb := make([]byte, 4, 4)
  44. // First read of 4 bytes should be fine
  45. n, err := c.Read(tb)
  46. if err != nil || n != 4 {
  47. t.Error(n, err)
  48. return
  49. }
  50. // Then we should get an error
  51. n, err = c.Read(tb)
  52. if err.Error() != "Test reading error" || n != 0 {
  53. t.Error(n, err)
  54. return
  55. }
  56. c.OutErr = 4
  57. // First write of 4 bytes should be fine
  58. n, err = c.Write([]byte("test"))
  59. if err != nil || n != 4 {
  60. t.Error(n, err)
  61. return
  62. }
  63. // Then we should get an error
  64. n, err = c.Write([]byte("test"))
  65. if err.Error() != "Test writing error" || n != 0 {
  66. t.Error(n, err)
  67. return
  68. }
  69. c.OutErr = 0
  70. c.OutClose = true
  71. n, err = c.Write([]byte("test"))
  72. if err != nil || n != 0 {
  73. t.Error(n, err)
  74. return
  75. }
  76. }
  77. func TestErrorTestingBuffer(t *testing.T) {
  78. buf := &ErrorTestingBuffer{5, 0}
  79. n, err := buf.Write(make([]byte, 5))
  80. if n != 5 {
  81. t.Error("Unexpected number of bytes written:", n)
  82. return
  83. }
  84. if err != nil {
  85. t.Error(err)
  86. return
  87. }
  88. n, err = buf.Write(make([]byte, 6))
  89. if n != 0 {
  90. t.Error("Unexpected number of bytes written:", n)
  91. return
  92. }
  93. if err == nil {
  94. t.Error("Writing too much too the buffer didn't return an error")
  95. return
  96. }
  97. etb, ok := err.(ErrorTestingBuffer)
  98. if !ok {
  99. t.Error("Unexpected error type was returned")
  100. return
  101. }
  102. if etb.WrittenSize != 5 || etb.RemainingSize != 0 {
  103. t.Error("Unexpected error state")
  104. return
  105. }
  106. if etb.Error() != "Buffer is full at: 5" {
  107. t.Error("Unexpected error message:", err)
  108. return
  109. }
  110. }
  111. func TestErrorTestingFile(t *testing.T) {
  112. f := NewTestingFile(5)
  113. n, err := f.Write(make([]byte, 5))
  114. if n != 5 {
  115. t.Error("Unexpected number of bytes written:", n)
  116. return
  117. }
  118. if err != nil {
  119. t.Error(err)
  120. return
  121. }
  122. n, err = f.Write(make([]byte, 6))
  123. if n != 0 {
  124. t.Error("Unexpected number of bytes written:", n)
  125. return
  126. }
  127. if err == nil {
  128. t.Error("Writing too much too the buffer didn't return an error")
  129. return
  130. }
  131. // Methods do nothing
  132. f.Sync()
  133. f.Close()
  134. }
  135. func TestGobTestObject(t *testing.T) {
  136. bb := &bytes.Buffer{}
  137. var ret GobTestObject
  138. gobtest := &GobTestObject{"test", true, false}
  139. if err := gob.NewEncoder(bb).Encode(gobtest); err == nil || err.Error() != "Encode error" {
  140. t.Error("Unexpected result:", err)
  141. return
  142. }
  143. ret = GobTestObject{"test", false, true}
  144. bb = &bytes.Buffer{}
  145. if err := gob.NewEncoder(bb).Encode(&GobTestObject{"test", false, false}); err != nil {
  146. t.Error(err)
  147. return
  148. }
  149. if err := gob.NewDecoder(bb).Decode(&ret); err == nil || err.Error() != "Decode error" {
  150. t.Error("Unexpected result:", err)
  151. return
  152. }
  153. bb = &bytes.Buffer{}
  154. ret = GobTestObject{"", false, false}
  155. if err := gob.NewEncoder(bb).Encode(&GobTestObject{"test", false, false}); err != nil {
  156. t.Error(err)
  157. return
  158. }
  159. if err := gob.NewDecoder(bb).Decode(&ret); err != nil {
  160. t.Error(err)
  161. return
  162. }
  163. if ret.Name != "test" {
  164. t.Error("Unexpected decoding result:", ret)
  165. return
  166. }
  167. }