You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

465 lines
11 KiB

  1. package binary
  2. import (
  3. "bytes"
  4. "fmt"
  5. "reflect"
  6. "testing"
  7. "time"
  8. . "github.com/tendermint/tendermint/common"
  9. )
  10. type SimpleStruct struct {
  11. String string
  12. Bytes []byte
  13. Time time.Time
  14. }
  15. //-------------------------------------
  16. type Animal interface{}
  17. const (
  18. AnimalTypeCat = byte(0x01)
  19. AnimalTypeDog = byte(0x02)
  20. AnimalTypeSnake = byte(0x03)
  21. AnimalTypeViper = byte(0x04)
  22. )
  23. // Implements Animal
  24. type Cat struct {
  25. SimpleStruct
  26. }
  27. // Implements Animal
  28. type Dog struct {
  29. SimpleStruct
  30. }
  31. // Implements Animal
  32. type Snake []byte
  33. // Implements Animal
  34. type Viper struct {
  35. Bytes []byte
  36. }
  37. var _ = RegisterInterface(
  38. struct{ Animal }{},
  39. ConcreteType{Cat{}, AnimalTypeCat},
  40. ConcreteType{Dog{}, AnimalTypeDog},
  41. ConcreteType{Snake{}, AnimalTypeSnake},
  42. ConcreteType{&Viper{}, AnimalTypeViper},
  43. )
  44. func TestAnimalInterface(t *testing.T) {
  45. var foo Animal
  46. // Type of pointer to Animal
  47. rt := reflect.TypeOf(&foo)
  48. fmt.Printf("rt: %v\n", rt)
  49. // Type of Animal itself.
  50. // NOTE: normally this is acquired through other means
  51. // like introspecting on method signatures, or struct fields.
  52. rte := rt.Elem()
  53. fmt.Printf("rte: %v\n", rte)
  54. // Get a new pointer to the interface
  55. // NOTE: calling .Interface() is to get the actual value,
  56. // instead of reflection values.
  57. ptr := reflect.New(rte).Interface()
  58. fmt.Printf("ptr: %v", ptr)
  59. // Make a binary byteslice that represents a snake.
  60. snakeBytes := BinaryBytes(Snake([]byte("snake")))
  61. snakeReader := bytes.NewReader(snakeBytes)
  62. // Now you can read it.
  63. n, err := new(int64), new(error)
  64. it := *ReadBinary(ptr, snakeReader, n, err).(*Animal)
  65. fmt.Println(it, reflect.TypeOf(it))
  66. }
  67. //-------------------------------------
  68. type Constructor func() interface{}
  69. type Instantiator func() (o interface{}, ptr interface{})
  70. type Validator func(o interface{}, t *testing.T)
  71. type TestCase struct {
  72. Constructor
  73. Instantiator
  74. Validator
  75. }
  76. //-------------------------------------
  77. func constructBasic() interface{} {
  78. cat := Cat{
  79. SimpleStruct{
  80. String: "String",
  81. Bytes: []byte("Bytes"),
  82. Time: time.Unix(123, 0),
  83. },
  84. }
  85. return cat
  86. }
  87. func instantiateBasic() (interface{}, interface{}) {
  88. return Cat{}, &Cat{}
  89. }
  90. func validateBasic(o interface{}, t *testing.T) {
  91. cat := o.(Cat)
  92. if cat.String != "String" {
  93. t.Errorf("Expected cat.String == 'String', got %v", cat.String)
  94. }
  95. if string(cat.Bytes) != "Bytes" {
  96. t.Errorf("Expected cat.Bytes == 'Bytes', got %X", cat.Bytes)
  97. }
  98. if cat.Time.Unix() != 123 {
  99. t.Errorf("Expected cat.Time == 'Unix(123)', got %v", cat.Time)
  100. }
  101. }
  102. //-------------------------------------
  103. type NilTestStruct struct {
  104. IntPtr *int
  105. CatPtr *Cat
  106. Animal Animal
  107. }
  108. func constructNilTestStruct() interface{} {
  109. return NilTestStruct{}
  110. }
  111. func instantiateNilTestStruct() (interface{}, interface{}) {
  112. return NilTestStruct{}, &NilTestStruct{}
  113. }
  114. func validateNilTestStruct(o interface{}, t *testing.T) {
  115. nts := o.(NilTestStruct)
  116. if nts.IntPtr != nil {
  117. t.Errorf("Expected nts.IntPtr to be nil, got %v", nts.IntPtr)
  118. }
  119. if nts.CatPtr != nil {
  120. t.Errorf("Expected nts.CatPtr to be nil, got %v", nts.CatPtr)
  121. }
  122. if nts.Animal != nil {
  123. t.Errorf("Expected nts.Animal to be nil, got %v", nts.Animal)
  124. }
  125. }
  126. //-------------------------------------
  127. type ComplexStruct struct {
  128. Name string
  129. Animal Animal
  130. }
  131. func constructComplex() interface{} {
  132. c := ComplexStruct{
  133. Name: "Complex",
  134. Animal: constructBasic(),
  135. }
  136. return c
  137. }
  138. func instantiateComplex() (interface{}, interface{}) {
  139. return ComplexStruct{}, &ComplexStruct{}
  140. }
  141. func validateComplex(o interface{}, t *testing.T) {
  142. c2 := o.(ComplexStruct)
  143. if cat, ok := c2.Animal.(Cat); ok {
  144. validateBasic(cat, t)
  145. } else {
  146. t.Errorf("Expected c2.Animal to be of type cat, got %v", reflect.ValueOf(c2.Animal).Elem().Type())
  147. }
  148. }
  149. //-------------------------------------
  150. type ComplexStruct2 struct {
  151. Cat Cat
  152. Dog *Dog
  153. Snake Snake
  154. Snake2 *Snake
  155. Viper Viper
  156. Viper2 *Viper
  157. }
  158. func constructComplex2() interface{} {
  159. snake_ := Snake([]byte("hiss"))
  160. snakePtr_ := &snake_
  161. c := ComplexStruct2{
  162. Cat: Cat{
  163. SimpleStruct{
  164. String: "String",
  165. Bytes: []byte("Bytes"),
  166. },
  167. },
  168. Dog: &Dog{
  169. SimpleStruct{
  170. String: "Woof",
  171. Bytes: []byte("Bark"),
  172. },
  173. },
  174. Snake: Snake([]byte("hiss")),
  175. Snake2: snakePtr_,
  176. Viper: Viper{Bytes: []byte("hizz")},
  177. Viper2: &Viper{Bytes: []byte("hizz")},
  178. }
  179. return c
  180. }
  181. func instantiateComplex2() (interface{}, interface{}) {
  182. return ComplexStruct2{}, &ComplexStruct2{}
  183. }
  184. func validateComplex2(o interface{}, t *testing.T) {
  185. c2 := o.(ComplexStruct2)
  186. cat := c2.Cat
  187. if cat.String != "String" {
  188. t.Errorf("Expected cat.String == 'String', got %v", cat.String)
  189. }
  190. if string(cat.Bytes) != "Bytes" {
  191. t.Errorf("Expected cat.Bytes == 'Bytes', got %X", cat.Bytes)
  192. }
  193. dog := c2.Dog
  194. if dog.String != "Woof" {
  195. t.Errorf("Expected dog.String == 'Woof', got %v", dog.String)
  196. }
  197. if string(dog.Bytes) != "Bark" {
  198. t.Errorf("Expected dog.Bytes == 'Bark', got %X", dog.Bytes)
  199. }
  200. snake := c2.Snake
  201. if string(snake) != "hiss" {
  202. t.Errorf("Expected string(snake) == 'hiss', got %v", string(snake))
  203. }
  204. snake2 := c2.Snake2
  205. if string(*snake2) != "hiss" {
  206. t.Errorf("Expected string(snake2) == 'hiss', got %v", string(*snake2))
  207. }
  208. viper := c2.Viper
  209. if string(viper.Bytes) != "hizz" {
  210. t.Errorf("Expected string(viper.Bytes) == 'hizz', got %v", string(viper.Bytes))
  211. }
  212. viper2 := c2.Viper2
  213. if string(viper2.Bytes) != "hizz" {
  214. t.Errorf("Expected string(viper2.Bytes) == 'hizz', got %v", string(viper2.Bytes))
  215. }
  216. }
  217. //-------------------------------------
  218. type ComplexStructArray struct {
  219. Animals []Animal
  220. }
  221. func constructComplexArray() interface{} {
  222. c := ComplexStructArray{
  223. Animals: []Animal{
  224. Cat{
  225. SimpleStruct{
  226. String: "String",
  227. Bytes: []byte("Bytes"),
  228. },
  229. },
  230. Dog{
  231. SimpleStruct{
  232. String: "Woof",
  233. Bytes: []byte("Bark"),
  234. },
  235. },
  236. Snake([]byte("hiss")),
  237. &Viper{
  238. Bytes: []byte("hizz"),
  239. },
  240. },
  241. }
  242. return c
  243. }
  244. func instantiateComplexArray() (interface{}, interface{}) {
  245. return ComplexStructArray{}, &ComplexStructArray{}
  246. }
  247. func validateComplexArray(o interface{}, t *testing.T) {
  248. c2 := o.(ComplexStructArray)
  249. if cat, ok := c2.Animals[0].(Cat); ok {
  250. if cat.String != "String" {
  251. t.Errorf("Expected cat.String == 'String', got %v", cat.String)
  252. }
  253. if string(cat.Bytes) != "Bytes" {
  254. t.Errorf("Expected cat.Bytes == 'Bytes', got %X", cat.Bytes)
  255. }
  256. } else {
  257. t.Errorf("Expected c2.Animals[0] to be of type cat, got %v", reflect.ValueOf(c2.Animals[0]).Elem().Type())
  258. }
  259. if dog, ok := c2.Animals[1].(Dog); ok {
  260. if dog.String != "Woof" {
  261. t.Errorf("Expected dog.String == 'Woof', got %v", dog.String)
  262. }
  263. if string(dog.Bytes) != "Bark" {
  264. t.Errorf("Expected dog.Bytes == 'Bark', got %X", dog.Bytes)
  265. }
  266. } else {
  267. t.Errorf("Expected c2.Animals[1] to be of type dog, got %v", reflect.ValueOf(c2.Animals[1]).Elem().Type())
  268. }
  269. if snake, ok := c2.Animals[2].(Snake); ok {
  270. if string(snake) != "hiss" {
  271. t.Errorf("Expected string(snake) == 'hiss', got %v", string(snake))
  272. }
  273. } else {
  274. t.Errorf("Expected c2.Animals[2] to be of type Snake, got %v", reflect.ValueOf(c2.Animals[2]).Elem().Type())
  275. }
  276. if viper, ok := c2.Animals[3].(*Viper); ok {
  277. if string(viper.Bytes) != "hizz" {
  278. t.Errorf("Expected string(viper.Bytes) == 'hizz', got %v", string(viper.Bytes))
  279. }
  280. } else {
  281. t.Errorf("Expected c2.Animals[3] to be of type *Viper, got %v", reflect.ValueOf(c2.Animals[3]).Elem().Type())
  282. }
  283. }
  284. //-----------------------------------------------------------------------------
  285. var testCases = []TestCase{}
  286. func init() {
  287. testCases = append(testCases, TestCase{constructBasic, instantiateBasic, validateBasic})
  288. testCases = append(testCases, TestCase{constructComplex, instantiateComplex, validateComplex})
  289. testCases = append(testCases, TestCase{constructComplex2, instantiateComplex2, validateComplex2})
  290. testCases = append(testCases, TestCase{constructComplexArray, instantiateComplexArray, validateComplexArray})
  291. testCases = append(testCases, TestCase{constructNilTestStruct, instantiateNilTestStruct, validateNilTestStruct})
  292. }
  293. func TestBinary(t *testing.T) {
  294. for i, testCase := range testCases {
  295. log.Info(fmt.Sprintf("Running test case %v", i))
  296. // Construct an object
  297. o := testCase.Constructor()
  298. // Write the object
  299. data := BinaryBytes(o)
  300. t.Logf("Binary: %X", data)
  301. instance, instancePtr := testCase.Instantiator()
  302. // Read onto a struct
  303. n, err := new(int64), new(error)
  304. res := ReadBinary(instance, bytes.NewReader(data), n, err)
  305. if *err != nil {
  306. t.Fatalf("Failed to read into instance: %v", *err)
  307. }
  308. // Validate object
  309. testCase.Validator(res, t)
  310. // Read onto a pointer
  311. n, err = new(int64), new(error)
  312. res = ReadBinary(instancePtr, bytes.NewReader(data), n, err)
  313. if *err != nil {
  314. t.Fatalf("Failed to read into instance: %v", *err)
  315. }
  316. if res != instancePtr {
  317. t.Errorf("Expected pointer to pass through")
  318. }
  319. // Validate object
  320. testCase.Validator(reflect.ValueOf(res).Elem().Interface(), t)
  321. }
  322. }
  323. func TestJSON(t *testing.T) {
  324. for i, testCase := range testCases {
  325. log.Info(fmt.Sprintf("Running test case %v", i))
  326. // Construct an object
  327. o := testCase.Constructor()
  328. // Write the object
  329. data := JSONBytes(o)
  330. t.Logf("JSON: %v", string(data))
  331. instance, instancePtr := testCase.Instantiator()
  332. // Read onto a struct
  333. err := new(error)
  334. res := ReadJSON(instance, data, err)
  335. if *err != nil {
  336. t.Fatalf("Failed to read cat: %v", *err)
  337. }
  338. // Validate object
  339. testCase.Validator(res, t)
  340. // Read onto a pointer
  341. res = ReadJSON(instancePtr, data, err)
  342. if *err != nil {
  343. t.Fatalf("Failed to read cat: %v", *err)
  344. }
  345. if res != instancePtr {
  346. t.Errorf("Expected pointer to pass through")
  347. }
  348. // Validate object
  349. testCase.Validator(reflect.ValueOf(res).Elem().Interface(), t)
  350. }
  351. }
  352. //------------------------------------------------------------------------------
  353. type Foo struct {
  354. FieldA string `json:"fieldA"` // json field name is "fieldA"
  355. FieldB string // json field name is "FieldB"
  356. fieldC string // not exported, not serialized.
  357. }
  358. func TestJSONFieldNames(t *testing.T) {
  359. for i := 0; i < 20; i++ { // Try to ensure deterministic success.
  360. foo := Foo{"a", "b", "c"}
  361. stringified := string(JSONBytes(foo))
  362. expected := `{"fieldA":"a","FieldB":"b"}`
  363. if stringified != expected {
  364. t.Fatalf("JSONFieldNames error: expected %v, got %v",
  365. expected, stringified)
  366. }
  367. }
  368. }
  369. //------------------------------------------------------------------------------
  370. func TestBadAlloc(t *testing.T) {
  371. n, err := new(int64), new(error)
  372. instance := new([]byte)
  373. data := RandBytes(ByteSliceChunk * 100)
  374. b := new(bytes.Buffer)
  375. // this slice of data claims to be much bigger than it really is
  376. WriteUvarint(uint(10000000000000000), b, n, err)
  377. b.Write(data)
  378. res := ReadBinary(instance, b, n, err)
  379. fmt.Println(res, *err)
  380. }