marshal_test.go 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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. varintUint64Tests := []struct {
  665. Value interface{}
  666. Marshaled []byte
  667. Unmarshaled uint64
  668. }{
  669. {
  670. Value: int8(0),
  671. Marshaled: []byte("\x00"),
  672. Unmarshaled: 0,
  673. },
  674. {
  675. Value: uint8(255),
  676. Marshaled: []byte("\x00\xFF"),
  677. Unmarshaled: 255,
  678. },
  679. {
  680. Value: big.NewInt(math.MaxInt32),
  681. Marshaled: []byte("\x7F\xFF\xFF\xFF"),
  682. Unmarshaled: uint64(math.MaxInt32),
  683. },
  684. {
  685. Value: big.NewInt(int64(math.MaxInt32) + 1),
  686. Marshaled: []byte("\x00\x80\x00\x00\x00"),
  687. Unmarshaled: uint64(int64(math.MaxInt32) + 1),
  688. },
  689. {
  690. Value: uint64(math.MaxInt64) + 1,
  691. Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00"),
  692. Unmarshaled: 9223372036854775808,
  693. },
  694. {
  695. Value: uint64(math.MaxUint64),
  696. Marshaled: []byte("\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"),
  697. Unmarshaled: uint64(math.MaxUint64),
  698. },
  699. }
  700. for i, test := range varintUint64Tests {
  701. data, err := Marshal(NativeType{proto: 2, typ: TypeVarint}, test.Value)
  702. if err != nil {
  703. t.Errorf("error marshaling varint: %v (test #%d)", err, i)
  704. }
  705. if !bytes.Equal(test.Marshaled, data) {
  706. t.Errorf("marshaled varint mismatch: expected %v, got %v (test #%d)", test.Marshaled, data, i)
  707. }
  708. var binder uint64
  709. err = Unmarshal(NativeType{proto: 2, typ: TypeVarint}, test.Marshaled, &binder)
  710. if err != nil {
  711. t.Errorf("error unmarshaling varint to uint64: %v (test #%d)", err, i)
  712. }
  713. if test.Unmarshaled != binder {
  714. t.Errorf("unmarshaled varint mismatch: expected %v, got %v (test #%d)", test.Unmarshaled, binder, i)
  715. }
  716. }
  717. }
  718. func equalStringSlice(leftList, rightList []string) bool {
  719. if len(leftList) != len(rightList) {
  720. return false
  721. }
  722. for index := range leftList {
  723. if rightList[index] != leftList[index] {
  724. return false
  725. }
  726. }
  727. return true
  728. }
  729. func TestMarshalList(t *testing.T) {
  730. typeInfo := CollectionType{
  731. NativeType: NativeType{proto: 2, typ: TypeList},
  732. Elem: NativeType{proto: 2, typ: TypeVarchar},
  733. }
  734. sourceLists := [][]string{
  735. {"valueA"},
  736. {"valueA", "valueB"},
  737. {"valueB"},
  738. }
  739. listDatas := [][]byte{}
  740. for _, list := range sourceLists {
  741. listData, marshalErr := Marshal(typeInfo, list)
  742. if nil != marshalErr {
  743. t.Errorf("Error marshal %+v of type %+v: %s", list, typeInfo, marshalErr)
  744. }
  745. listDatas = append(listDatas, listData)
  746. }
  747. outputLists := [][]string{}
  748. var outputList []string
  749. for _, listData := range listDatas {
  750. if unmarshalErr := Unmarshal(typeInfo, listData, &outputList); nil != unmarshalErr {
  751. t.Error(unmarshalErr)
  752. }
  753. outputLists = append(outputLists, outputList)
  754. }
  755. for index, sourceList := range sourceLists {
  756. outputList := outputLists[index]
  757. if !equalStringSlice(sourceList, outputList) {
  758. t.Errorf("Lists %+v not equal to lists %+v, but should", sourceList, outputList)
  759. }
  760. }
  761. }
  762. type CustomString string
  763. func (c CustomString) MarshalCQL(info TypeInfo) ([]byte, error) {
  764. return []byte(strings.ToUpper(string(c))), nil
  765. }
  766. func (c *CustomString) UnmarshalCQL(info TypeInfo, data []byte) error {
  767. *c = CustomString(strings.ToLower(string(data)))
  768. return nil
  769. }
  770. type MyString string
  771. type MyInt int
  772. var typeLookupTest = []struct {
  773. TypeName string
  774. ExpectedType Type
  775. }{
  776. {"AsciiType", TypeAscii},
  777. {"LongType", TypeBigInt},
  778. {"BytesType", TypeBlob},
  779. {"BooleanType", TypeBoolean},
  780. {"CounterColumnType", TypeCounter},
  781. {"DecimalType", TypeDecimal},
  782. {"DoubleType", TypeDouble},
  783. {"FloatType", TypeFloat},
  784. {"Int32Type", TypeInt},
  785. {"DateType", TypeTimestamp},
  786. {"TimestampType", TypeTimestamp},
  787. {"UUIDType", TypeUUID},
  788. {"UTF8Type", TypeVarchar},
  789. {"IntegerType", TypeVarint},
  790. {"TimeUUIDType", TypeTimeUUID},
  791. {"InetAddressType", TypeInet},
  792. {"MapType", TypeMap},
  793. {"ListType", TypeList},
  794. {"SetType", TypeSet},
  795. {"unknown", TypeCustom},
  796. }
  797. func testType(t *testing.T, cassType string, expectedType Type) {
  798. if computedType := getApacheCassandraType(apacheCassandraTypePrefix + cassType); computedType != expectedType {
  799. t.Errorf("Cassandra custom type lookup for %s failed. Expected %s, got %s.", cassType, expectedType.String(), computedType.String())
  800. }
  801. }
  802. func TestLookupCassType(t *testing.T) {
  803. for _, lookupTest := range typeLookupTest {
  804. testType(t, lookupTest.TypeName, lookupTest.ExpectedType)
  805. }
  806. }
  807. type MyPointerMarshaler struct{}
  808. func (m *MyPointerMarshaler) MarshalCQL(_ TypeInfo) ([]byte, error) {
  809. return []byte{42}, nil
  810. }
  811. func TestMarshalPointer(t *testing.T) {
  812. m := &MyPointerMarshaler{}
  813. typ := NativeType{proto: 2, typ: TypeInt}
  814. data, err := Marshal(typ, m)
  815. if err != nil {
  816. t.Errorf("Pointer marshaling failed. Error: %s", err)
  817. }
  818. if len(data) != 1 || data[0] != 42 {
  819. t.Errorf("Pointer marshaling failed. Expected %+v, got %+v", []byte{42}, data)
  820. }
  821. }
  822. func TestMarshalTimestamp(t *testing.T) {
  823. var marshalTimestampTests = []struct {
  824. Info TypeInfo
  825. Data []byte
  826. Value interface{}
  827. }{
  828. {
  829. NativeType{proto: 2, typ: TypeTimestamp},
  830. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  831. time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC),
  832. },
  833. {
  834. NativeType{proto: 2, typ: TypeTimestamp},
  835. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  836. int64(1376387523000),
  837. },
  838. {
  839. // 9223372036854 is the maximum time representable in ms since the epoch
  840. // with int64 if using UnixNano to convert
  841. NativeType{proto: 2, typ: TypeTimestamp},
  842. []byte("\x00\x00\x08\x63\x7b\xd0\x5a\xf6"),
  843. time.Date(2262, time.April, 11, 23, 47, 16, 854775807, time.UTC),
  844. },
  845. {
  846. // One nanosecond after causes overflow when using UnixNano
  847. // Instead it should resolve to the same time in ms
  848. NativeType{proto: 2, typ: TypeTimestamp},
  849. []byte("\x00\x00\x08\x63\x7b\xd0\x5a\xf6"),
  850. time.Date(2262, time.April, 11, 23, 47, 16, 854775808, time.UTC),
  851. },
  852. {
  853. // -9223372036855 is the minimum time representable in ms since the epoch
  854. // with int64 if using UnixNano to convert
  855. NativeType{proto: 2, typ: TypeTimestamp},
  856. []byte("\xff\xff\xf7\x9c\x84\x2f\xa5\x09"),
  857. time.Date(1677, time.September, 21, 00, 12, 43, 145224192, time.UTC),
  858. },
  859. {
  860. // One nanosecond earlier causes overflow when using UnixNano
  861. // it should resolve to the same time in ms
  862. NativeType{proto: 2, typ: TypeTimestamp},
  863. []byte("\xff\xff\xf7\x9c\x84\x2f\xa5\x09"),
  864. time.Date(1677, time.September, 21, 00, 12, 43, 145224191, time.UTC),
  865. },
  866. {
  867. // Store the zero time as a blank slice
  868. NativeType{proto: 2, typ: TypeTimestamp},
  869. []byte{},
  870. time.Time{},
  871. },
  872. }
  873. for i, test := range marshalTimestampTests {
  874. t.Log(i, test)
  875. data, err := Marshal(test.Info, test.Value)
  876. if err != nil {
  877. t.Errorf("marshalTest[%d]: %v", i, err)
  878. continue
  879. }
  880. if !bytes.Equal(data, test.Data) {
  881. t.Errorf("marshalTest[%d]: expected %x (%v), got %x (%v) for time %s", i,
  882. test.Data, decBigInt(test.Data), data, decBigInt(data), test.Value)
  883. }
  884. }
  885. }
  886. func TestMarshalTuple(t *testing.T) {
  887. info := TupleTypeInfo{
  888. NativeType: NativeType{proto: 3, typ: TypeTuple},
  889. Elems: []TypeInfo{
  890. NativeType{proto: 3, typ: TypeVarchar},
  891. NativeType{proto: 3, typ: TypeVarchar},
  892. },
  893. }
  894. expectedData := []byte("\x00\x00\x00\x03foo\x00\x00\x00\x03bar")
  895. value := []interface{}{"foo", "bar"}
  896. data, err := Marshal(info, value)
  897. if err != nil {
  898. t.Errorf("marshalTest: %v", err)
  899. return
  900. }
  901. if !bytes.Equal(data, expectedData) {
  902. t.Errorf("marshalTest: expected %x (%v), got %x (%v)",
  903. expectedData, decBigInt(expectedData), data, decBigInt(data))
  904. return
  905. }
  906. var s1, s2 string
  907. val := []interface{}{&s1, &s2}
  908. err = Unmarshal(info, expectedData, val)
  909. if err != nil {
  910. t.Errorf("unmarshalTest: %v", err)
  911. return
  912. }
  913. if s1 != "foo" || s2 != "bar" {
  914. t.Errorf("unmarshalTest: expected [foo, bar], got [%s, %s]", s1, s2)
  915. }
  916. }
  917. func TestMarshalNil(t *testing.T) {
  918. types := []Type{
  919. TypeAscii,
  920. TypeBlob,
  921. TypeBoolean,
  922. TypeBigInt,
  923. TypeCounter,
  924. TypeDecimal,
  925. TypeDouble,
  926. TypeFloat,
  927. TypeInt,
  928. TypeTimestamp,
  929. TypeUUID,
  930. TypeVarchar,
  931. TypeVarint,
  932. TypeTimeUUID,
  933. TypeInet,
  934. }
  935. for _, typ := range types {
  936. data, err := Marshal(NativeType{proto: 3, typ: typ}, nil)
  937. if err != nil {
  938. t.Errorf("unable to marshal nil %v: %v\n", typ, err)
  939. } else if data != nil {
  940. t.Errorf("expected to get nil byte for nil %v got % X", typ, data)
  941. }
  942. }
  943. }
  944. func TestUnmarshalInetCopyBytes(t *testing.T) {
  945. data := []byte{127, 0, 0, 1}
  946. var ip net.IP
  947. if err := unmarshalInet(NativeType{proto: 2, typ: TypeInet}, data, &ip); err != nil {
  948. t.Fatal(err)
  949. }
  950. copy(data, []byte{0xFF, 0xFF, 0xFF, 0xFF})
  951. ip2 := net.IP(data)
  952. if !ip.Equal(net.IPv4(127, 0, 0, 1)) {
  953. t.Fatalf("IP memory shared with data: ip=%v ip2=%v", ip, ip2)
  954. }
  955. }