marshal_test.go 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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: TypeVarint},
  340. []byte(nil),
  341. nil,
  342. },
  343. {
  344. NativeType{proto: 2, typ: TypeInet},
  345. []byte("\x7F\x00\x00\x01"),
  346. net.ParseIP("127.0.0.1").To4(),
  347. },
  348. {
  349. NativeType{proto: 2, typ: TypeInet},
  350. []byte("\xFF\xFF\xFF\xFF"),
  351. net.ParseIP("255.255.255.255").To4(),
  352. },
  353. {
  354. NativeType{proto: 2, typ: TypeInet},
  355. []byte("\x7F\x00\x00\x01"),
  356. "127.0.0.1",
  357. },
  358. {
  359. NativeType{proto: 2, typ: TypeInet},
  360. []byte("\xFF\xFF\xFF\xFF"),
  361. "255.255.255.255",
  362. },
  363. {
  364. NativeType{proto: 2, typ: TypeInet},
  365. []byte("\x21\xDA\x00\xd3\x00\x00\x2f\x3b\x02\xaa\x00\xff\xfe\x28\x9c\x5a"),
  366. "21da:d3:0:2f3b:2aa:ff:fe28:9c5a",
  367. },
  368. {
  369. NativeType{proto: 2, typ: TypeInet},
  370. []byte("\xfe\x80\x00\x00\x00\x00\x00\x00\x02\x02\xb3\xff\xfe\x1e\x83\x29"),
  371. "fe80::202:b3ff:fe1e:8329",
  372. },
  373. {
  374. NativeType{proto: 2, typ: TypeInet},
  375. []byte("\x21\xDA\x00\xd3\x00\x00\x2f\x3b\x02\xaa\x00\xff\xfe\x28\x9c\x5a"),
  376. net.ParseIP("21da:d3:0:2f3b:2aa:ff:fe28:9c5a"),
  377. },
  378. {
  379. NativeType{proto: 2, typ: TypeInet},
  380. []byte("\xfe\x80\x00\x00\x00\x00\x00\x00\x02\x02\xb3\xff\xfe\x1e\x83\x29"),
  381. net.ParseIP("fe80::202:b3ff:fe1e:8329"),
  382. },
  383. {
  384. NativeType{proto: 2, typ: TypeInt},
  385. []byte(nil),
  386. nil,
  387. },
  388. {
  389. NativeType{proto: 2, typ: TypeVarchar},
  390. []byte("nullable string"),
  391. func() *string {
  392. value := "nullable string"
  393. return &value
  394. }(),
  395. },
  396. {
  397. NativeType{proto: 2, typ: TypeVarchar},
  398. []byte{},
  399. (*string)(nil),
  400. },
  401. {
  402. NativeType{proto: 2, typ: TypeInt},
  403. []byte("\x7f\xff\xff\xff"),
  404. func() *int {
  405. var value int = math.MaxInt32
  406. return &value
  407. }(),
  408. },
  409. {
  410. NativeType{proto: 2, typ: TypeInt},
  411. []byte(nil),
  412. (*int)(nil),
  413. },
  414. {
  415. NativeType{proto: 2, typ: TypeTimeUUID},
  416. []byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
  417. &UUID{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
  418. },
  419. {
  420. NativeType{proto: 2, typ: TypeTimeUUID},
  421. []byte{},
  422. (*UUID)(nil),
  423. },
  424. {
  425. NativeType{proto: 2, typ: TypeTimestamp},
  426. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  427. func() *time.Time {
  428. t := time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC)
  429. return &t
  430. }(),
  431. },
  432. {
  433. NativeType{proto: 2, typ: TypeTimestamp},
  434. []byte(nil),
  435. (*time.Time)(nil),
  436. },
  437. {
  438. NativeType{proto: 2, typ: TypeBoolean},
  439. []byte("\x00"),
  440. func() *bool {
  441. b := false
  442. return &b
  443. }(),
  444. },
  445. {
  446. NativeType{proto: 2, typ: TypeBoolean},
  447. []byte("\x01"),
  448. func() *bool {
  449. b := true
  450. return &b
  451. }(),
  452. },
  453. {
  454. NativeType{proto: 2, typ: TypeBoolean},
  455. []byte(nil),
  456. (*bool)(nil),
  457. },
  458. {
  459. NativeType{proto: 2, typ: TypeFloat},
  460. []byte("\x40\x49\x0f\xdb"),
  461. func() *float32 {
  462. f := float32(3.14159265)
  463. return &f
  464. }(),
  465. },
  466. {
  467. NativeType{proto: 2, typ: TypeFloat},
  468. []byte(nil),
  469. (*float32)(nil),
  470. },
  471. {
  472. NativeType{proto: 2, typ: TypeDouble},
  473. []byte("\x40\x09\x21\xfb\x53\xc8\xd4\xf1"),
  474. func() *float64 {
  475. d := float64(3.14159265)
  476. return &d
  477. }(),
  478. },
  479. {
  480. NativeType{proto: 2, typ: TypeDouble},
  481. []byte(nil),
  482. (*float64)(nil),
  483. },
  484. {
  485. NativeType{proto: 2, typ: TypeInet},
  486. []byte("\x7F\x00\x00\x01"),
  487. func() *net.IP {
  488. ip := net.ParseIP("127.0.0.1").To4()
  489. return &ip
  490. }(),
  491. },
  492. {
  493. NativeType{proto: 2, typ: TypeInet},
  494. []byte(nil),
  495. (*net.IP)(nil),
  496. },
  497. {
  498. CollectionType{
  499. NativeType: NativeType{proto: 2, typ: TypeList},
  500. Elem: NativeType{proto: 2, typ: TypeInt},
  501. },
  502. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  503. func() *[]int {
  504. l := []int{1, 2}
  505. return &l
  506. }(),
  507. },
  508. {
  509. CollectionType{
  510. NativeType: NativeType{proto: 2, typ: TypeList},
  511. Elem: NativeType{proto: 2, typ: TypeInt},
  512. },
  513. []byte(nil),
  514. (*[]int)(nil),
  515. },
  516. {
  517. CollectionType{
  518. NativeType: NativeType{proto: 2, typ: TypeMap},
  519. Key: NativeType{proto: 2, typ: TypeVarchar},
  520. Elem: NativeType{proto: 2, typ: TypeInt},
  521. },
  522. []byte("\x00\x01\x00\x03foo\x00\x04\x00\x00\x00\x01"),
  523. func() *map[string]int {
  524. m := map[string]int{"foo": 1}
  525. return &m
  526. }(),
  527. },
  528. {
  529. CollectionType{
  530. NativeType: NativeType{proto: 2, typ: TypeMap},
  531. Key: NativeType{proto: 2, typ: TypeVarchar},
  532. Elem: NativeType{proto: 2, typ: TypeInt},
  533. },
  534. []byte(nil),
  535. (*map[string]int)(nil),
  536. },
  537. {
  538. NativeType{proto: 2, typ: TypeVarchar},
  539. []byte("HELLO WORLD"),
  540. func() *CustomString {
  541. customString := CustomString("hello world")
  542. return &customString
  543. }(),
  544. },
  545. {
  546. NativeType{proto: 2, typ: TypeVarchar},
  547. []byte(nil),
  548. (*CustomString)(nil),
  549. },
  550. }
  551. func decimalize(s string) *inf.Dec {
  552. i, _ := new(inf.Dec).SetString(s)
  553. return i
  554. }
  555. func bigintize(s string) *big.Int {
  556. i, _ := new(big.Int).SetString(s, 10)
  557. return i
  558. }
  559. func TestMarshal(t *testing.T) {
  560. for i, test := range marshalTests {
  561. data, err := Marshal(test.Info, test.Value)
  562. if err != nil {
  563. t.Errorf("marshalTest[%d]: %v", i, err)
  564. continue
  565. }
  566. if !bytes.Equal(data, test.Data) {
  567. t.Errorf("marshalTest[%d]: expected %q, got %q (%#v)", i, test.Data, data, test.Value)
  568. }
  569. }
  570. }
  571. func TestUnmarshal(t *testing.T) {
  572. for i, test := range marshalTests {
  573. if test.Value != nil {
  574. v := reflect.New(reflect.TypeOf(test.Value))
  575. err := Unmarshal(test.Info, test.Data, v.Interface())
  576. if err != nil {
  577. t.Errorf("unmarshalTest[%d]: %v", i, err)
  578. continue
  579. }
  580. if !reflect.DeepEqual(v.Elem().Interface(), test.Value) {
  581. t.Errorf("unmarshalTest[%d]: expected %#v, got %#v.", i, test.Value, v.Elem().Interface())
  582. }
  583. } else {
  584. if err := Unmarshal(test.Info, test.Data, test.Value); nil == err {
  585. t.Errorf("unmarshalTest[%d]: %#v not return error.", i, test.Value)
  586. }
  587. }
  588. }
  589. }
  590. func TestMarshalVarint(t *testing.T) {
  591. varintTests := []struct {
  592. Value interface{}
  593. Marshaled []byte
  594. Unmarshaled *big.Int
  595. }{
  596. {
  597. Value: int8(0),
  598. Marshaled: []byte("\x00"),
  599. Unmarshaled: big.NewInt(0),
  600. },
  601. {
  602. Value: uint8(255),
  603. Marshaled: []byte("\x00\xFF"),
  604. Unmarshaled: big.NewInt(255),
  605. },
  606. {
  607. Value: int8(-1),
  608. Marshaled: []byte("\xFF"),
  609. Unmarshaled: big.NewInt(-1),
  610. },
  611. {
  612. Value: big.NewInt(math.MaxInt32),
  613. Marshaled: []byte("\x7F\xFF\xFF\xFF"),
  614. Unmarshaled: big.NewInt(math.MaxInt32),
  615. },
  616. {
  617. Value: big.NewInt(int64(math.MaxInt32) + 1),
  618. Marshaled: []byte("\x00\x80\x00\x00\x00"),
  619. Unmarshaled: big.NewInt(int64(math.MaxInt32) + 1),
  620. },
  621. {
  622. Value: big.NewInt(math.MinInt32),
  623. Marshaled: []byte("\x80\x00\x00\x00"),
  624. Unmarshaled: big.NewInt(math.MinInt32),
  625. },
  626. {
  627. Value: big.NewInt(int64(math.MinInt32) - 1),
  628. Marshaled: []byte("\xFF\x7F\xFF\xFF\xFF"),
  629. Unmarshaled: big.NewInt(int64(math.MinInt32) - 1),
  630. },
  631. {
  632. Value: math.MinInt64,
  633. Marshaled: []byte("\x80\x00\x00\x00\x00\x00\x00\x00"),
  634. Unmarshaled: big.NewInt(math.MinInt64),
  635. },
  636. {
  637. Value: uint64(math.MaxInt64) + 1,
  638. Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00"),
  639. Unmarshaled: bigintize("9223372036854775808"),
  640. },
  641. {
  642. Value: bigintize("2361183241434822606848"), // 2**71
  643. Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00"),
  644. Unmarshaled: bigintize("2361183241434822606848"),
  645. },
  646. {
  647. Value: bigintize("-9223372036854775809"), // -2**63 - 1
  648. Marshaled: []byte("\xFF\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF"),
  649. Unmarshaled: bigintize("-9223372036854775809"),
  650. },
  651. }
  652. for i, test := range varintTests {
  653. data, err := Marshal(NativeType{proto: 2, typ: TypeVarint}, test.Value)
  654. if err != nil {
  655. t.Errorf("error marshaling varint: %v (test #%d)", err, i)
  656. }
  657. if !bytes.Equal(test.Marshaled, data) {
  658. t.Errorf("marshaled varint mismatch: expected %v, got %v (test #%d)", test.Marshaled, data, i)
  659. }
  660. binder := new(big.Int)
  661. err = Unmarshal(NativeType{proto: 2, typ: TypeVarint}, test.Marshaled, binder)
  662. if err != nil {
  663. t.Errorf("error unmarshaling varint: %v (test #%d)", err, i)
  664. }
  665. if test.Unmarshaled.Cmp(binder) != 0 {
  666. t.Errorf("unmarshaled varint mismatch: expected %v, got %v (test #%d)", test.Unmarshaled, binder, i)
  667. }
  668. }
  669. varintUint64Tests := []struct {
  670. Value interface{}
  671. Marshaled []byte
  672. Unmarshaled uint64
  673. }{
  674. {
  675. Value: int8(0),
  676. Marshaled: []byte("\x00"),
  677. Unmarshaled: 0,
  678. },
  679. {
  680. Value: uint8(255),
  681. Marshaled: []byte("\x00\xFF"),
  682. Unmarshaled: 255,
  683. },
  684. {
  685. Value: big.NewInt(math.MaxInt32),
  686. Marshaled: []byte("\x7F\xFF\xFF\xFF"),
  687. Unmarshaled: uint64(math.MaxInt32),
  688. },
  689. {
  690. Value: big.NewInt(int64(math.MaxInt32) + 1),
  691. Marshaled: []byte("\x00\x80\x00\x00\x00"),
  692. Unmarshaled: uint64(int64(math.MaxInt32) + 1),
  693. },
  694. {
  695. Value: uint64(math.MaxInt64) + 1,
  696. Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00"),
  697. Unmarshaled: 9223372036854775808,
  698. },
  699. {
  700. Value: uint64(math.MaxUint64),
  701. Marshaled: []byte("\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"),
  702. Unmarshaled: uint64(math.MaxUint64),
  703. },
  704. }
  705. for i, test := range varintUint64Tests {
  706. data, err := Marshal(NativeType{proto: 2, typ: TypeVarint}, test.Value)
  707. if err != nil {
  708. t.Errorf("error marshaling varint: %v (test #%d)", err, i)
  709. }
  710. if !bytes.Equal(test.Marshaled, data) {
  711. t.Errorf("marshaled varint mismatch: expected %v, got %v (test #%d)", test.Marshaled, data, i)
  712. }
  713. var binder uint64
  714. err = Unmarshal(NativeType{proto: 2, typ: TypeVarint}, test.Marshaled, &binder)
  715. if err != nil {
  716. t.Errorf("error unmarshaling varint to uint64: %v (test #%d)", err, i)
  717. }
  718. if test.Unmarshaled != binder {
  719. t.Errorf("unmarshaled varint mismatch: expected %v, got %v (test #%d)", test.Unmarshaled, binder, i)
  720. }
  721. }
  722. }
  723. func equalStringSlice(leftList, rightList []string) bool {
  724. if len(leftList) != len(rightList) {
  725. return false
  726. }
  727. for index := range leftList {
  728. if rightList[index] != leftList[index] {
  729. return false
  730. }
  731. }
  732. return true
  733. }
  734. func TestMarshalList(t *testing.T) {
  735. typeInfo := CollectionType{
  736. NativeType: NativeType{proto: 2, typ: TypeList},
  737. Elem: NativeType{proto: 2, typ: TypeVarchar},
  738. }
  739. sourceLists := [][]string{
  740. {"valueA"},
  741. {"valueA", "valueB"},
  742. {"valueB"},
  743. }
  744. listDatas := [][]byte{}
  745. for _, list := range sourceLists {
  746. listData, marshalErr := Marshal(typeInfo, list)
  747. if nil != marshalErr {
  748. t.Errorf("Error marshal %+v of type %+v: %s", list, typeInfo, marshalErr)
  749. }
  750. listDatas = append(listDatas, listData)
  751. }
  752. outputLists := [][]string{}
  753. var outputList []string
  754. for _, listData := range listDatas {
  755. if unmarshalErr := Unmarshal(typeInfo, listData, &outputList); nil != unmarshalErr {
  756. t.Error(unmarshalErr)
  757. }
  758. outputLists = append(outputLists, outputList)
  759. }
  760. for index, sourceList := range sourceLists {
  761. outputList := outputLists[index]
  762. if !equalStringSlice(sourceList, outputList) {
  763. t.Errorf("Lists %+v not equal to lists %+v, but should", sourceList, outputList)
  764. }
  765. }
  766. }
  767. type CustomString string
  768. func (c CustomString) MarshalCQL(info TypeInfo) ([]byte, error) {
  769. return []byte(strings.ToUpper(string(c))), nil
  770. }
  771. func (c *CustomString) UnmarshalCQL(info TypeInfo, data []byte) error {
  772. *c = CustomString(strings.ToLower(string(data)))
  773. return nil
  774. }
  775. type MyString string
  776. type MyInt int
  777. var typeLookupTest = []struct {
  778. TypeName string
  779. ExpectedType Type
  780. }{
  781. {"AsciiType", TypeAscii},
  782. {"LongType", TypeBigInt},
  783. {"BytesType", TypeBlob},
  784. {"BooleanType", TypeBoolean},
  785. {"CounterColumnType", TypeCounter},
  786. {"DecimalType", TypeDecimal},
  787. {"DoubleType", TypeDouble},
  788. {"FloatType", TypeFloat},
  789. {"Int32Type", TypeInt},
  790. {"DateType", TypeTimestamp},
  791. {"TimestampType", TypeTimestamp},
  792. {"UUIDType", TypeUUID},
  793. {"UTF8Type", TypeVarchar},
  794. {"IntegerType", TypeVarint},
  795. {"TimeUUIDType", TypeTimeUUID},
  796. {"InetAddressType", TypeInet},
  797. {"MapType", TypeMap},
  798. {"ListType", TypeList},
  799. {"SetType", TypeSet},
  800. {"unknown", TypeCustom},
  801. }
  802. func testType(t *testing.T, cassType string, expectedType Type) {
  803. if computedType := getApacheCassandraType(apacheCassandraTypePrefix + cassType); computedType != expectedType {
  804. t.Errorf("Cassandra custom type lookup for %s failed. Expected %s, got %s.", cassType, expectedType.String(), computedType.String())
  805. }
  806. }
  807. func TestLookupCassType(t *testing.T) {
  808. for _, lookupTest := range typeLookupTest {
  809. testType(t, lookupTest.TypeName, lookupTest.ExpectedType)
  810. }
  811. }
  812. type MyPointerMarshaler struct{}
  813. func (m *MyPointerMarshaler) MarshalCQL(_ TypeInfo) ([]byte, error) {
  814. return []byte{42}, nil
  815. }
  816. func TestMarshalPointer(t *testing.T) {
  817. m := &MyPointerMarshaler{}
  818. typ := NativeType{proto: 2, typ: TypeInt}
  819. data, err := Marshal(typ, m)
  820. if err != nil {
  821. t.Errorf("Pointer marshaling failed. Error: %s", err)
  822. }
  823. if len(data) != 1 || data[0] != 42 {
  824. t.Errorf("Pointer marshaling failed. Expected %+v, got %+v", []byte{42}, data)
  825. }
  826. }
  827. func TestMarshalTimestamp(t *testing.T) {
  828. var marshalTimestampTests = []struct {
  829. Info TypeInfo
  830. Data []byte
  831. Value interface{}
  832. }{
  833. {
  834. NativeType{proto: 2, typ: TypeTimestamp},
  835. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  836. time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC),
  837. },
  838. {
  839. NativeType{proto: 2, typ: TypeTimestamp},
  840. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  841. int64(1376387523000),
  842. },
  843. {
  844. // 9223372036854 is the maximum time representable in ms since the epoch
  845. // with int64 if using UnixNano to convert
  846. NativeType{proto: 2, typ: TypeTimestamp},
  847. []byte("\x00\x00\x08\x63\x7b\xd0\x5a\xf6"),
  848. time.Date(2262, time.April, 11, 23, 47, 16, 854775807, time.UTC),
  849. },
  850. {
  851. // One nanosecond after causes overflow when using UnixNano
  852. // Instead it should resolve to the same time in ms
  853. NativeType{proto: 2, typ: TypeTimestamp},
  854. []byte("\x00\x00\x08\x63\x7b\xd0\x5a\xf6"),
  855. time.Date(2262, time.April, 11, 23, 47, 16, 854775808, time.UTC),
  856. },
  857. {
  858. // -9223372036855 is the minimum time representable in ms since the epoch
  859. // with int64 if using UnixNano to convert
  860. NativeType{proto: 2, typ: TypeTimestamp},
  861. []byte("\xff\xff\xf7\x9c\x84\x2f\xa5\x09"),
  862. time.Date(1677, time.September, 21, 00, 12, 43, 145224192, time.UTC),
  863. },
  864. {
  865. // One nanosecond earlier causes overflow when using UnixNano
  866. // it should resolve to the same time in ms
  867. NativeType{proto: 2, typ: TypeTimestamp},
  868. []byte("\xff\xff\xf7\x9c\x84\x2f\xa5\x09"),
  869. time.Date(1677, time.September, 21, 00, 12, 43, 145224191, time.UTC),
  870. },
  871. {
  872. // Store the zero time as a blank slice
  873. NativeType{proto: 2, typ: TypeTimestamp},
  874. []byte{},
  875. time.Time{},
  876. },
  877. }
  878. for i, test := range marshalTimestampTests {
  879. t.Log(i, test)
  880. data, err := Marshal(test.Info, test.Value)
  881. if err != nil {
  882. t.Errorf("marshalTest[%d]: %v", i, err)
  883. continue
  884. }
  885. if !bytes.Equal(data, test.Data) {
  886. t.Errorf("marshalTest[%d]: expected %x (%v), got %x (%v) for time %s", i,
  887. test.Data, decBigInt(test.Data), data, decBigInt(data), test.Value)
  888. }
  889. }
  890. }
  891. func TestMarshalTuple(t *testing.T) {
  892. info := TupleTypeInfo{
  893. NativeType: NativeType{proto: 3, typ: TypeTuple},
  894. Elems: []TypeInfo{
  895. NativeType{proto: 3, typ: TypeVarchar},
  896. NativeType{proto: 3, typ: TypeVarchar},
  897. },
  898. }
  899. expectedData := []byte("\x00\x00\x00\x03foo\x00\x00\x00\x03bar")
  900. value := []interface{}{"foo", "bar"}
  901. data, err := Marshal(info, value)
  902. if err != nil {
  903. t.Errorf("marshalTest: %v", err)
  904. return
  905. }
  906. if !bytes.Equal(data, expectedData) {
  907. t.Errorf("marshalTest: expected %x (%v), got %x (%v)",
  908. expectedData, decBigInt(expectedData), data, decBigInt(data))
  909. return
  910. }
  911. var s1, s2 string
  912. val := []interface{}{&s1, &s2}
  913. err = Unmarshal(info, expectedData, val)
  914. if err != nil {
  915. t.Errorf("unmarshalTest: %v", err)
  916. return
  917. }
  918. if s1 != "foo" || s2 != "bar" {
  919. t.Errorf("unmarshalTest: expected [foo, bar], got [%s, %s]", s1, s2)
  920. }
  921. }
  922. func TestMarshalNil(t *testing.T) {
  923. types := []Type{
  924. TypeAscii,
  925. TypeBlob,
  926. TypeBoolean,
  927. TypeBigInt,
  928. TypeCounter,
  929. TypeDecimal,
  930. TypeDouble,
  931. TypeFloat,
  932. TypeInt,
  933. TypeTimestamp,
  934. TypeUUID,
  935. TypeVarchar,
  936. TypeVarint,
  937. TypeTimeUUID,
  938. TypeInet,
  939. }
  940. for _, typ := range types {
  941. data, err := Marshal(NativeType{proto: 3, typ: typ}, nil)
  942. if err != nil {
  943. t.Errorf("unable to marshal nil %v: %v\n", typ, err)
  944. } else if data != nil {
  945. t.Errorf("expected to get nil byte for nil %v got % X", typ, data)
  946. }
  947. }
  948. }
  949. func TestUnmarshalInetCopyBytes(t *testing.T) {
  950. data := []byte{127, 0, 0, 1}
  951. var ip net.IP
  952. if err := unmarshalInet(NativeType{proto: 2, typ: TypeInet}, data, &ip); err != nil {
  953. t.Fatal(err)
  954. }
  955. copy(data, []byte{0xFF, 0xFF, 0xFF, 0xFF})
  956. ip2 := net.IP(data)
  957. if !ip.Equal(net.IPv4(127, 0, 0, 1)) {
  958. t.Fatalf("IP memory shared with data: ip=%v ip2=%v", ip, ip2)
  959. }
  960. }