marshal_test.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. // +build all unit
  2. package gocql
  3. import (
  4. "bytes"
  5. "math"
  6. "math/big"
  7. "net"
  8. "reflect"
  9. "strings"
  10. "testing"
  11. "time"
  12. "gopkg.in/inf.v0"
  13. )
  14. type AliasInt int
  15. var marshalTests = []struct {
  16. Info TypeInfo
  17. Data []byte
  18. Value interface{}
  19. }{
  20. {
  21. NativeType{proto: 2, typ: TypeVarchar},
  22. []byte("hello world"),
  23. []byte("hello world"),
  24. },
  25. {
  26. NativeType{proto: 2, typ: TypeVarchar},
  27. []byte("hello world"),
  28. "hello world",
  29. },
  30. {
  31. NativeType{proto: 2, typ: TypeVarchar},
  32. []byte(nil),
  33. []byte(nil),
  34. },
  35. {
  36. NativeType{proto: 2, typ: TypeVarchar},
  37. []byte("hello world"),
  38. MyString("hello world"),
  39. },
  40. {
  41. NativeType{proto: 2, typ: TypeVarchar},
  42. []byte("HELLO WORLD"),
  43. CustomString("hello world"),
  44. },
  45. {
  46. NativeType{proto: 2, typ: TypeBlob},
  47. []byte("hello\x00"),
  48. []byte("hello\x00"),
  49. },
  50. {
  51. NativeType{proto: 2, typ: TypeBlob},
  52. []byte(nil),
  53. []byte(nil),
  54. },
  55. {
  56. NativeType{proto: 2, typ: TypeTimeUUID},
  57. []byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
  58. func() UUID {
  59. x, _ := UUIDFromBytes([]byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0})
  60. return x
  61. }(),
  62. },
  63. {
  64. NativeType{proto: 2, typ: TypeInt},
  65. []byte("\x00\x00\x00\x00"),
  66. 0,
  67. },
  68. {
  69. NativeType{proto: 2, typ: TypeInt},
  70. []byte("\x01\x02\x03\x04"),
  71. int(16909060),
  72. },
  73. {
  74. NativeType{proto: 2, typ: TypeInt},
  75. []byte("\x01\x02\x03\x04"),
  76. AliasInt(16909060),
  77. },
  78. {
  79. NativeType{proto: 2, typ: TypeInt},
  80. []byte("\x80\x00\x00\x00"),
  81. int32(math.MinInt32),
  82. },
  83. {
  84. NativeType{proto: 2, typ: TypeInt},
  85. []byte("\x7f\xff\xff\xff"),
  86. int32(math.MaxInt32),
  87. },
  88. {
  89. NativeType{proto: 2, typ: TypeInt},
  90. []byte("\x00\x00\x00\x00"),
  91. "0",
  92. },
  93. {
  94. NativeType{proto: 2, typ: TypeInt},
  95. []byte("\x01\x02\x03\x04"),
  96. "16909060",
  97. },
  98. {
  99. NativeType{proto: 2, typ: TypeInt},
  100. []byte("\x80\x00\x00\x00"),
  101. "-2147483648", // math.MinInt32
  102. },
  103. {
  104. NativeType{proto: 2, typ: TypeInt},
  105. []byte("\x7f\xff\xff\xff"),
  106. "2147483647", // math.MaxInt32
  107. },
  108. {
  109. NativeType{proto: 2, typ: TypeBigInt},
  110. []byte("\x00\x00\x00\x00\x00\x00\x00\x00"),
  111. 0,
  112. },
  113. {
  114. NativeType{proto: 2, typ: TypeBigInt},
  115. []byte("\x01\x02\x03\x04\x05\x06\x07\x08"),
  116. 72623859790382856,
  117. },
  118. {
  119. NativeType{proto: 2, typ: TypeBigInt},
  120. []byte("\x80\x00\x00\x00\x00\x00\x00\x00"),
  121. int64(math.MinInt64),
  122. },
  123. {
  124. NativeType{proto: 2, typ: TypeBigInt},
  125. []byte("\x7f\xff\xff\xff\xff\xff\xff\xff"),
  126. int64(math.MaxInt64),
  127. },
  128. {
  129. NativeType{proto: 2, typ: TypeBigInt},
  130. []byte("\x00\x00\x00\x00\x00\x00\x00\x00"),
  131. "0",
  132. },
  133. {
  134. NativeType{proto: 2, typ: TypeBigInt},
  135. []byte("\x01\x02\x03\x04\x05\x06\x07\x08"),
  136. "72623859790382856",
  137. },
  138. {
  139. NativeType{proto: 2, typ: TypeBigInt},
  140. []byte("\x80\x00\x00\x00\x00\x00\x00\x00"),
  141. "-9223372036854775808", // math.MinInt64
  142. },
  143. {
  144. NativeType{proto: 2, typ: TypeBigInt},
  145. []byte("\x7f\xff\xff\xff\xff\xff\xff\xff"),
  146. "9223372036854775807", // math.MaxInt64
  147. },
  148. {
  149. NativeType{proto: 2, typ: TypeBoolean},
  150. []byte("\x00"),
  151. false,
  152. },
  153. {
  154. NativeType{proto: 2, typ: TypeBoolean},
  155. []byte("\x01"),
  156. true,
  157. },
  158. {
  159. NativeType{proto: 2, typ: TypeFloat},
  160. []byte("\x40\x49\x0f\xdb"),
  161. float32(3.14159265),
  162. },
  163. {
  164. NativeType{proto: 2, typ: TypeDouble},
  165. []byte("\x40\x09\x21\xfb\x53\xc8\xd4\xf1"),
  166. float64(3.14159265),
  167. },
  168. {
  169. NativeType{proto: 2, typ: TypeDecimal},
  170. []byte("\x00\x00\x00\x00\x00"),
  171. inf.NewDec(0, 0),
  172. },
  173. {
  174. NativeType{proto: 2, typ: TypeDecimal},
  175. []byte("\x00\x00\x00\x00\x64"),
  176. inf.NewDec(100, 0),
  177. },
  178. {
  179. NativeType{proto: 2, typ: TypeDecimal},
  180. []byte("\x00\x00\x00\x02\x19"),
  181. decimalize("0.25"),
  182. },
  183. {
  184. NativeType{proto: 2, typ: TypeDecimal},
  185. []byte("\x00\x00\x00\x13\xD5\a;\x20\x14\xA2\x91"),
  186. decimalize("-0.0012095473475870063"), // From the iconara/cql-rb test suite
  187. },
  188. {
  189. NativeType{proto: 2, typ: TypeDecimal},
  190. []byte("\x00\x00\x00\x13*\xF8\xC4\xDF\xEB]o"),
  191. decimalize("0.0012095473475870063"), // From the iconara/cql-rb test suite
  192. },
  193. {
  194. NativeType{proto: 2, typ: TypeDecimal},
  195. []byte("\x00\x00\x00\x12\xF2\xD8\x02\xB6R\x7F\x99\xEE\x98#\x99\xA9V"),
  196. decimalize("-1042342234234.123423435647768234"), // From the iconara/cql-rb test suite
  197. },
  198. {
  199. NativeType{proto: 2, typ: TypeDecimal},
  200. []byte("\x00\x00\x00\r\nJ\x04\"^\x91\x04\x8a\xb1\x18\xfe"),
  201. decimalize("1243878957943.1234124191998"), // From the datastax/python-driver test suite
  202. },
  203. {
  204. NativeType{proto: 2, typ: TypeDecimal},
  205. []byte("\x00\x00\x00\x06\xe5\xde]\x98Y"),
  206. decimalize("-112233.441191"), // From the datastax/python-driver test suite
  207. },
  208. {
  209. NativeType{proto: 2, typ: TypeDecimal},
  210. []byte("\x00\x00\x00\x14\x00\xfa\xce"),
  211. decimalize("0.00000000000000064206"), // From the datastax/python-driver test suite
  212. },
  213. {
  214. NativeType{proto: 2, typ: TypeDecimal},
  215. []byte("\x00\x00\x00\x14\xff\x052"),
  216. decimalize("-0.00000000000000064206"), // From the datastax/python-driver test suite
  217. },
  218. {
  219. NativeType{proto: 2, typ: TypeDecimal},
  220. []byte("\xff\xff\xff\x9c\x00\xfa\xce"),
  221. inf.NewDec(64206, -100), // From the datastax/python-driver test suite
  222. },
  223. {
  224. NativeType{proto: 2, typ: TypeTimestamp},
  225. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  226. time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC),
  227. },
  228. {
  229. NativeType{proto: 2, typ: TypeTimestamp},
  230. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  231. int64(1376387523000),
  232. },
  233. {
  234. CollectionType{
  235. NativeType: NativeType{proto: 2, typ: TypeList},
  236. Elem: NativeType{proto: 2, typ: TypeInt},
  237. },
  238. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  239. []int{1, 2},
  240. },
  241. {
  242. CollectionType{
  243. NativeType: NativeType{proto: 2, typ: TypeList},
  244. Elem: NativeType{proto: 2, typ: TypeInt},
  245. },
  246. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  247. [2]int{1, 2},
  248. },
  249. {
  250. CollectionType{
  251. NativeType: NativeType{proto: 2, typ: TypeSet},
  252. Elem: NativeType{proto: 2, typ: TypeInt},
  253. },
  254. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  255. []int{1, 2},
  256. },
  257. {
  258. CollectionType{
  259. NativeType: NativeType{proto: 2, typ: TypeSet},
  260. Elem: NativeType{proto: 2, typ: TypeInt},
  261. },
  262. []byte{0, 0}, // encoding of a list should always include the size of the collection
  263. []int{},
  264. },
  265. {
  266. CollectionType{
  267. NativeType: NativeType{proto: 2, typ: TypeMap},
  268. Key: NativeType{proto: 2, typ: TypeVarchar},
  269. Elem: NativeType{proto: 2, typ: TypeInt},
  270. },
  271. []byte("\x00\x01\x00\x03foo\x00\x04\x00\x00\x00\x01"),
  272. map[string]int{"foo": 1},
  273. },
  274. {
  275. CollectionType{
  276. NativeType: NativeType{proto: 2, typ: TypeMap},
  277. Key: NativeType{proto: 2, typ: TypeVarchar},
  278. Elem: NativeType{proto: 2, typ: TypeInt},
  279. },
  280. []byte{0, 0},
  281. map[string]int{},
  282. },
  283. {
  284. CollectionType{
  285. NativeType: NativeType{proto: 2, typ: TypeList},
  286. Elem: NativeType{proto: 2, typ: TypeVarchar},
  287. },
  288. bytes.Join([][]byte{
  289. []byte("\x00\x01\xFF\xFF"),
  290. bytes.Repeat([]byte("X"), 65535)}, []byte("")),
  291. []string{strings.Repeat("X", 65535)},
  292. },
  293. {
  294. CollectionType{
  295. NativeType: NativeType{proto: 2, typ: TypeMap},
  296. Key: NativeType{proto: 2, typ: TypeVarchar},
  297. Elem: NativeType{proto: 2, typ: TypeVarchar},
  298. },
  299. bytes.Join([][]byte{
  300. []byte("\x00\x01\xFF\xFF"),
  301. bytes.Repeat([]byte("X"), 65535),
  302. []byte("\xFF\xFF"),
  303. bytes.Repeat([]byte("Y"), 65535)}, []byte("")),
  304. map[string]string{
  305. strings.Repeat("X", 65535): strings.Repeat("Y", 65535),
  306. },
  307. },
  308. {
  309. NativeType{proto: 2, typ: TypeVarint},
  310. []byte("\x00"),
  311. 0,
  312. },
  313. {
  314. NativeType{proto: 2, typ: TypeVarint},
  315. []byte("\x37\xE2\x3C\xEC"),
  316. int32(937573612),
  317. },
  318. {
  319. NativeType{proto: 2, typ: TypeVarint},
  320. []byte("\x37\xE2\x3C\xEC"),
  321. big.NewInt(937573612),
  322. },
  323. {
  324. NativeType{proto: 2, typ: TypeVarint},
  325. []byte("\x03\x9EV \x15\f\x03\x9DK\x18\xCDI\\$?\a["),
  326. bigintize("1231312312331283012830129382342342412123"), // From the iconara/cql-rb test suite
  327. },
  328. {
  329. NativeType{proto: 2, typ: TypeVarint},
  330. []byte("\xC9v\x8D:\x86"),
  331. big.NewInt(-234234234234), // From the iconara/cql-rb test suite
  332. },
  333. {
  334. NativeType{proto: 2, typ: TypeVarint},
  335. []byte("f\x1e\xfd\xf2\xe3\xb1\x9f|\x04_\x15"),
  336. bigintize("123456789123456789123456789"), // From the datastax/python-driver test suite
  337. },
  338. {
  339. NativeType{proto: 2, typ: TypeInet},
  340. []byte("\x7F\x00\x00\x01"),
  341. net.ParseIP("127.0.0.1").To4(),
  342. },
  343. {
  344. NativeType{proto: 2, typ: TypeInet},
  345. []byte("\xFF\xFF\xFF\xFF"),
  346. net.ParseIP("255.255.255.255").To4(),
  347. },
  348. {
  349. NativeType{proto: 2, typ: TypeInet},
  350. []byte("\x7F\x00\x00\x01"),
  351. "127.0.0.1",
  352. },
  353. {
  354. NativeType{proto: 2, typ: TypeInet},
  355. []byte("\xFF\xFF\xFF\xFF"),
  356. "255.255.255.255",
  357. },
  358. {
  359. NativeType{proto: 2, typ: TypeInet},
  360. []byte("\x21\xDA\x00\xd3\x00\x00\x2f\x3b\x02\xaa\x00\xff\xfe\x28\x9c\x5a"),
  361. "21da:d3:0:2f3b:2aa:ff:fe28:9c5a",
  362. },
  363. {
  364. NativeType{proto: 2, typ: TypeInet},
  365. []byte("\xfe\x80\x00\x00\x00\x00\x00\x00\x02\x02\xb3\xff\xfe\x1e\x83\x29"),
  366. "fe80::202:b3ff:fe1e:8329",
  367. },
  368. {
  369. NativeType{proto: 2, typ: TypeInet},
  370. []byte("\x21\xDA\x00\xd3\x00\x00\x2f\x3b\x02\xaa\x00\xff\xfe\x28\x9c\x5a"),
  371. net.ParseIP("21da:d3:0:2f3b:2aa:ff:fe28:9c5a"),
  372. },
  373. {
  374. NativeType{proto: 2, typ: TypeInet},
  375. []byte("\xfe\x80\x00\x00\x00\x00\x00\x00\x02\x02\xb3\xff\xfe\x1e\x83\x29"),
  376. net.ParseIP("fe80::202:b3ff:fe1e:8329"),
  377. },
  378. {
  379. NativeType{proto: 2, typ: TypeInt},
  380. []byte(nil),
  381. nil,
  382. },
  383. {
  384. NativeType{proto: 2, typ: TypeVarchar},
  385. []byte("nullable string"),
  386. func() *string {
  387. value := "nullable string"
  388. return &value
  389. }(),
  390. },
  391. {
  392. NativeType{proto: 2, typ: TypeVarchar},
  393. []byte{},
  394. (*string)(nil),
  395. },
  396. {
  397. NativeType{proto: 2, typ: TypeInt},
  398. []byte("\x7f\xff\xff\xff"),
  399. func() *int {
  400. var value int = math.MaxInt32
  401. return &value
  402. }(),
  403. },
  404. {
  405. NativeType{proto: 2, typ: TypeInt},
  406. []byte(nil),
  407. (*int)(nil),
  408. },
  409. {
  410. NativeType{proto: 2, typ: TypeTimeUUID},
  411. []byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
  412. &UUID{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
  413. },
  414. {
  415. NativeType{proto: 2, typ: TypeTimeUUID},
  416. []byte{},
  417. (*UUID)(nil),
  418. },
  419. {
  420. NativeType{proto: 2, typ: TypeTimestamp},
  421. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  422. func() *time.Time {
  423. t := time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC)
  424. return &t
  425. }(),
  426. },
  427. {
  428. NativeType{proto: 2, typ: TypeTimestamp},
  429. []byte(nil),
  430. (*time.Time)(nil),
  431. },
  432. {
  433. NativeType{proto: 2, typ: TypeBoolean},
  434. []byte("\x00"),
  435. func() *bool {
  436. b := false
  437. return &b
  438. }(),
  439. },
  440. {
  441. NativeType{proto: 2, typ: TypeBoolean},
  442. []byte("\x01"),
  443. func() *bool {
  444. b := true
  445. return &b
  446. }(),
  447. },
  448. {
  449. NativeType{proto: 2, typ: TypeBoolean},
  450. []byte(nil),
  451. (*bool)(nil),
  452. },
  453. {
  454. NativeType{proto: 2, typ: TypeFloat},
  455. []byte("\x40\x49\x0f\xdb"),
  456. func() *float32 {
  457. f := float32(3.14159265)
  458. return &f
  459. }(),
  460. },
  461. {
  462. NativeType{proto: 2, typ: TypeFloat},
  463. []byte(nil),
  464. (*float32)(nil),
  465. },
  466. {
  467. NativeType{proto: 2, typ: TypeDouble},
  468. []byte("\x40\x09\x21\xfb\x53\xc8\xd4\xf1"),
  469. func() *float64 {
  470. d := float64(3.14159265)
  471. return &d
  472. }(),
  473. },
  474. {
  475. NativeType{proto: 2, typ: TypeDouble},
  476. []byte(nil),
  477. (*float64)(nil),
  478. },
  479. {
  480. NativeType{proto: 2, typ: TypeInet},
  481. []byte("\x7F\x00\x00\x01"),
  482. func() *net.IP {
  483. ip := net.ParseIP("127.0.0.1").To4()
  484. return &ip
  485. }(),
  486. },
  487. {
  488. NativeType{proto: 2, typ: TypeInet},
  489. []byte(nil),
  490. (*net.IP)(nil),
  491. },
  492. {
  493. CollectionType{
  494. NativeType: NativeType{proto: 2, typ: TypeList},
  495. Elem: NativeType{proto: 2, typ: TypeInt},
  496. },
  497. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  498. func() *[]int {
  499. l := []int{1, 2}
  500. return &l
  501. }(),
  502. },
  503. {
  504. CollectionType{
  505. NativeType: NativeType{proto: 2, typ: TypeList},
  506. Elem: NativeType{proto: 2, typ: TypeInt},
  507. },
  508. []byte(nil),
  509. (*[]int)(nil),
  510. },
  511. {
  512. CollectionType{
  513. NativeType: NativeType{proto: 2, typ: TypeMap},
  514. Key: NativeType{proto: 2, typ: TypeVarchar},
  515. Elem: NativeType{proto: 2, typ: TypeInt},
  516. },
  517. []byte("\x00\x01\x00\x03foo\x00\x04\x00\x00\x00\x01"),
  518. func() *map[string]int {
  519. m := map[string]int{"foo": 1}
  520. return &m
  521. }(),
  522. },
  523. {
  524. CollectionType{
  525. NativeType: NativeType{proto: 2, typ: TypeMap},
  526. Key: NativeType{proto: 2, typ: TypeVarchar},
  527. Elem: NativeType{proto: 2, typ: TypeInt},
  528. },
  529. []byte(nil),
  530. (*map[string]int)(nil),
  531. },
  532. {
  533. NativeType{proto: 2, typ: TypeVarchar},
  534. []byte("HELLO WORLD"),
  535. func() *CustomString {
  536. customString := CustomString("hello world")
  537. return &customString
  538. }(),
  539. },
  540. {
  541. NativeType{proto: 2, typ: TypeVarchar},
  542. []byte(nil),
  543. (*CustomString)(nil),
  544. },
  545. }
  546. func decimalize(s string) *inf.Dec {
  547. i, _ := new(inf.Dec).SetString(s)
  548. return i
  549. }
  550. func bigintize(s string) *big.Int {
  551. i, _ := new(big.Int).SetString(s, 10)
  552. return i
  553. }
  554. func TestMarshal(t *testing.T) {
  555. for i, test := range marshalTests {
  556. data, err := Marshal(test.Info, test.Value)
  557. if err != nil {
  558. t.Errorf("marshalTest[%d]: %v", i, err)
  559. continue
  560. }
  561. if !bytes.Equal(data, test.Data) {
  562. t.Errorf("marshalTest[%d]: expected %q, got %q (%#v)", i, test.Data, data, test.Value)
  563. }
  564. }
  565. }
  566. func TestUnmarshal(t *testing.T) {
  567. for i, test := range marshalTests {
  568. if test.Value != nil {
  569. v := reflect.New(reflect.TypeOf(test.Value))
  570. err := Unmarshal(test.Info, test.Data, v.Interface())
  571. if err != nil {
  572. t.Errorf("unmarshalTest[%d]: %v", i, err)
  573. continue
  574. }
  575. if !reflect.DeepEqual(v.Elem().Interface(), test.Value) {
  576. t.Errorf("unmarshalTest[%d]: expected %#v, got %#v.", i, test.Value, v.Elem().Interface())
  577. }
  578. } else {
  579. if err := Unmarshal(test.Info, test.Data, test.Value); nil == err {
  580. t.Errorf("unmarshalTest[%d]: %#v not return error.", i, test.Value)
  581. }
  582. }
  583. }
  584. }
  585. func TestMarshalVarint(t *testing.T) {
  586. varintTests := []struct {
  587. Value interface{}
  588. Marshaled []byte
  589. Unmarshaled *big.Int
  590. }{
  591. {
  592. Value: int8(0),
  593. Marshaled: []byte("\x00"),
  594. Unmarshaled: big.NewInt(0),
  595. },
  596. {
  597. Value: uint8(255),
  598. Marshaled: []byte("\x00\xFF"),
  599. Unmarshaled: big.NewInt(255),
  600. },
  601. {
  602. Value: int8(-1),
  603. Marshaled: []byte("\xFF"),
  604. Unmarshaled: big.NewInt(-1),
  605. },
  606. {
  607. Value: big.NewInt(math.MaxInt32),
  608. Marshaled: []byte("\x7F\xFF\xFF\xFF"),
  609. Unmarshaled: big.NewInt(math.MaxInt32),
  610. },
  611. {
  612. Value: big.NewInt(int64(math.MaxInt32) + 1),
  613. Marshaled: []byte("\x00\x80\x00\x00\x00"),
  614. Unmarshaled: big.NewInt(int64(math.MaxInt32) + 1),
  615. },
  616. {
  617. Value: big.NewInt(math.MinInt32),
  618. Marshaled: []byte("\x80\x00\x00\x00"),
  619. Unmarshaled: big.NewInt(math.MinInt32),
  620. },
  621. {
  622. Value: big.NewInt(int64(math.MinInt32) - 1),
  623. Marshaled: []byte("\xFF\x7F\xFF\xFF\xFF"),
  624. Unmarshaled: big.NewInt(int64(math.MinInt32) - 1),
  625. },
  626. {
  627. Value: math.MinInt64,
  628. Marshaled: []byte("\x80\x00\x00\x00\x00\x00\x00\x00"),
  629. Unmarshaled: big.NewInt(math.MinInt64),
  630. },
  631. {
  632. Value: uint64(math.MaxInt64) + 1,
  633. Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00"),
  634. Unmarshaled: bigintize("9223372036854775808"),
  635. },
  636. {
  637. Value: bigintize("2361183241434822606848"), // 2**71
  638. Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00"),
  639. Unmarshaled: bigintize("2361183241434822606848"),
  640. },
  641. {
  642. Value: bigintize("-9223372036854775809"), // -2**63 - 1
  643. Marshaled: []byte("\xFF\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF"),
  644. Unmarshaled: bigintize("-9223372036854775809"),
  645. },
  646. }
  647. for i, test := range varintTests {
  648. data, err := Marshal(NativeType{proto: 2, typ: TypeVarint}, test.Value)
  649. if err != nil {
  650. t.Errorf("error marshaling varint: %v (test #%d)", err, i)
  651. }
  652. if !bytes.Equal(test.Marshaled, data) {
  653. t.Errorf("marshaled varint mismatch: expected %v, got %v (test #%d)", test.Marshaled, data, i)
  654. }
  655. binder := new(big.Int)
  656. err = Unmarshal(NativeType{proto: 2, typ: TypeVarint}, test.Marshaled, binder)
  657. if err != nil {
  658. t.Errorf("error unmarshaling varint: %v (test #%d)", err, i)
  659. }
  660. if test.Unmarshaled.Cmp(binder) != 0 {
  661. t.Errorf("unmarshaled varint mismatch: expected %v, got %v (test #%d)", test.Unmarshaled, binder, i)
  662. }
  663. }
  664. }
  665. func equalStringSlice(leftList, rightList []string) bool {
  666. if len(leftList) != len(rightList) {
  667. return false
  668. }
  669. for index := range leftList {
  670. if rightList[index] != leftList[index] {
  671. return false
  672. }
  673. }
  674. return true
  675. }
  676. func TestMarshalList(t *testing.T) {
  677. typeInfo := CollectionType{
  678. NativeType: NativeType{proto: 2, typ: TypeList},
  679. Elem: NativeType{proto: 2, typ: TypeVarchar},
  680. }
  681. sourceLists := [][]string{
  682. []string{"valueA"},
  683. []string{"valueA", "valueB"},
  684. []string{"valueB"},
  685. }
  686. listDatas := [][]byte{}
  687. for _, list := range sourceLists {
  688. listData, marshalErr := Marshal(typeInfo, list)
  689. if nil != marshalErr {
  690. t.Errorf("Error marshal %+v of type %+v: %s", list, typeInfo, marshalErr)
  691. }
  692. listDatas = append(listDatas, listData)
  693. }
  694. outputLists := [][]string{}
  695. var outputList []string
  696. for _, listData := range listDatas {
  697. if unmarshalErr := Unmarshal(typeInfo, listData, &outputList); nil != unmarshalErr {
  698. t.Error(unmarshalErr)
  699. }
  700. outputLists = append(outputLists, outputList)
  701. }
  702. for index, sourceList := range sourceLists {
  703. outputList := outputLists[index]
  704. if !equalStringSlice(sourceList, outputList) {
  705. t.Errorf("Lists %+v not equal to lists %+v, but should", sourceList, outputList)
  706. }
  707. }
  708. }
  709. type CustomString string
  710. func (c CustomString) MarshalCQL(info TypeInfo) ([]byte, error) {
  711. return []byte(strings.ToUpper(string(c))), nil
  712. }
  713. func (c *CustomString) UnmarshalCQL(info TypeInfo, data []byte) error {
  714. *c = CustomString(strings.ToLower(string(data)))
  715. return nil
  716. }
  717. type MyString string
  718. type MyInt int
  719. var typeLookupTest = []struct {
  720. TypeName string
  721. ExpectedType Type
  722. }{
  723. {"AsciiType", TypeAscii},
  724. {"LongType", TypeBigInt},
  725. {"BytesType", TypeBlob},
  726. {"BooleanType", TypeBoolean},
  727. {"CounterColumnType", TypeCounter},
  728. {"DecimalType", TypeDecimal},
  729. {"DoubleType", TypeDouble},
  730. {"FloatType", TypeFloat},
  731. {"Int32Type", TypeInt},
  732. {"DateType", TypeTimestamp},
  733. {"TimestampType", TypeTimestamp},
  734. {"UUIDType", TypeUUID},
  735. {"UTF8Type", TypeVarchar},
  736. {"IntegerType", TypeVarint},
  737. {"TimeUUIDType", TypeTimeUUID},
  738. {"InetAddressType", TypeInet},
  739. {"MapType", TypeMap},
  740. {"ListType", TypeList},
  741. {"SetType", TypeSet},
  742. {"unknown", TypeCustom},
  743. }
  744. func testType(t *testing.T, cassType string, expectedType Type) {
  745. if computedType := getApacheCassandraType(apacheCassandraTypePrefix + cassType); computedType != expectedType {
  746. t.Errorf("Cassandra custom type lookup for %s failed. Expected %s, got %s.", cassType, expectedType.String(), computedType.String())
  747. }
  748. }
  749. func TestLookupCassType(t *testing.T) {
  750. for _, lookupTest := range typeLookupTest {
  751. testType(t, lookupTest.TypeName, lookupTest.ExpectedType)
  752. }
  753. }
  754. type MyPointerMarshaler struct{}
  755. func (m *MyPointerMarshaler) MarshalCQL(_ TypeInfo) ([]byte, error) {
  756. return []byte{42}, nil
  757. }
  758. func TestMarshalPointer(t *testing.T) {
  759. m := &MyPointerMarshaler{}
  760. typ := NativeType{proto: 2, typ: TypeInt}
  761. data, err := Marshal(typ, m)
  762. if err != nil {
  763. t.Errorf("Pointer marshaling failed. Error: %s", err)
  764. }
  765. if len(data) != 1 || data[0] != 42 {
  766. t.Errorf("Pointer marshaling failed. Expected %+v, got %+v", []byte{42}, data)
  767. }
  768. }
  769. func TestMarshalTimestamp(t *testing.T) {
  770. var marshalTimestampTests = []struct {
  771. Info TypeInfo
  772. Data []byte
  773. Value interface{}
  774. }{
  775. {
  776. NativeType{proto: 2, typ: TypeTimestamp},
  777. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  778. time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC),
  779. },
  780. {
  781. NativeType{proto: 2, typ: TypeTimestamp},
  782. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  783. int64(1376387523000),
  784. },
  785. {
  786. // 9223372036854 is the maximum time representable in ms since the epoch
  787. // with int64 if using UnixNano to convert
  788. NativeType{proto: 2, typ: TypeTimestamp},
  789. []byte("\x00\x00\x08\x63\x7b\xd0\x5a\xf6"),
  790. time.Date(2262, time.April, 11, 23, 47, 16, 854775807, time.UTC),
  791. },
  792. {
  793. // One nanosecond after causes overflow when using UnixNano
  794. // Instead it should resolve to the same time in ms
  795. NativeType{proto: 2, typ: TypeTimestamp},
  796. []byte("\x00\x00\x08\x63\x7b\xd0\x5a\xf6"),
  797. time.Date(2262, time.April, 11, 23, 47, 16, 854775808, time.UTC),
  798. },
  799. {
  800. // -9223372036855 is the minimum time representable in ms since the epoch
  801. // with int64 if using UnixNano to convert
  802. NativeType{proto: 2, typ: TypeTimestamp},
  803. []byte("\xff\xff\xf7\x9c\x84\x2f\xa5\x09"),
  804. time.Date(1677, time.September, 21, 00, 12, 43, 145224192, time.UTC),
  805. },
  806. {
  807. // One nanosecond earlier causes overflow when using UnixNano
  808. // it should resolve to the same time in ms
  809. NativeType{proto: 2, typ: TypeTimestamp},
  810. []byte("\xff\xff\xf7\x9c\x84\x2f\xa5\x09"),
  811. time.Date(1677, time.September, 21, 00, 12, 43, 145224191, time.UTC),
  812. },
  813. {
  814. // Store the zero time as a blank slice
  815. NativeType{proto: 2, typ: TypeTimestamp},
  816. []byte{},
  817. time.Time{},
  818. },
  819. }
  820. for i, test := range marshalTimestampTests {
  821. t.Log(i, test)
  822. data, err := Marshal(test.Info, test.Value)
  823. if err != nil {
  824. t.Errorf("marshalTest[%d]: %v", i, err)
  825. continue
  826. }
  827. if !bytes.Equal(data, test.Data) {
  828. t.Errorf("marshalTest[%d]: expected %x (%v), got %x (%v) for time %s", i,
  829. test.Data, decBigInt(test.Data), data, decBigInt(data), test.Value)
  830. }
  831. }
  832. }
  833. func TestMarshalTuple(t *testing.T) {
  834. info := TupleTypeInfo{
  835. NativeType: NativeType{proto: 3, typ: TypeTuple},
  836. Elems: []TypeInfo{
  837. NativeType{proto: 3, typ: TypeVarchar},
  838. NativeType{proto: 3, typ: TypeVarchar},
  839. },
  840. }
  841. expectedData := []byte("\x00\x00\x00\x03foo\x00\x00\x00\x03bar")
  842. value := []interface{}{"foo", "bar"}
  843. data, err := Marshal(info, value)
  844. if err != nil {
  845. t.Errorf("marshalTest: %v", err)
  846. return
  847. }
  848. if !bytes.Equal(data, expectedData) {
  849. t.Errorf("marshalTest: expected %x (%v), got %x (%v)",
  850. expectedData, decBigInt(expectedData), data, decBigInt(data))
  851. return
  852. }
  853. var s1, s2 string
  854. val := []interface{}{&s1, &s2}
  855. err = Unmarshal(info, expectedData, val)
  856. if err != nil {
  857. t.Errorf("unmarshalTest: %v", err)
  858. return
  859. }
  860. if s1 != "foo" || s2 != "bar" {
  861. t.Errorf("unmarshalTest: expected [foo, bar], got [%s, %s]", s1, s2)
  862. }
  863. }
  864. func TestMarshalNil(t *testing.T) {
  865. types := []Type{
  866. TypeAscii,
  867. TypeBlob,
  868. TypeBoolean,
  869. TypeBigInt,
  870. TypeCounter,
  871. TypeDecimal,
  872. TypeDouble,
  873. TypeFloat,
  874. TypeInt,
  875. TypeTimestamp,
  876. TypeUUID,
  877. TypeVarchar,
  878. TypeVarint,
  879. TypeTimeUUID,
  880. TypeInet,
  881. }
  882. for _, typ := range types {
  883. data, err := Marshal(NativeType{proto: 3, typ: typ}, nil)
  884. if err != nil {
  885. t.Errorf("unable to marshal nil %v: %v\n", typ, err)
  886. } else if data != nil {
  887. t.Errorf("expected to get nil byte for nil %v got % X", typ, data)
  888. }
  889. }
  890. }
  891. func TestUnmarshalInetCopyBytes(t *testing.T) {
  892. data := []byte{127, 0, 0, 1}
  893. var ip net.IP
  894. if err := unmarshalInet(NativeType{proto: 2, typ: TypeInet}, data, &ip); err != nil {
  895. t.Fatal(err)
  896. }
  897. copy(data, []byte{0xFF, 0xFF, 0xFF, 0xFF})
  898. ip2 := net.IP(data)
  899. if !ip.Equal(net.IPv4(127, 0, 0, 1)) {
  900. t.Fatalf("IP memory shared with data: ip=%v ip2=%v", ip, ip2)
  901. }
  902. }