marshal_test.go 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  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. MarshalError error
  20. UnmarshalError error
  21. }{
  22. {
  23. NativeType{proto: 2, typ: TypeVarchar},
  24. []byte("hello world"),
  25. []byte("hello world"),
  26. nil,
  27. nil,
  28. },
  29. {
  30. NativeType{proto: 2, typ: TypeVarchar},
  31. []byte("hello world"),
  32. "hello world",
  33. nil,
  34. nil,
  35. },
  36. {
  37. NativeType{proto: 2, typ: TypeVarchar},
  38. []byte(nil),
  39. []byte(nil),
  40. nil,
  41. nil,
  42. },
  43. {
  44. NativeType{proto: 2, typ: TypeVarchar},
  45. []byte("hello world"),
  46. MyString("hello world"),
  47. nil,
  48. nil,
  49. },
  50. {
  51. NativeType{proto: 2, typ: TypeVarchar},
  52. []byte("HELLO WORLD"),
  53. CustomString("hello world"),
  54. nil,
  55. nil,
  56. },
  57. {
  58. NativeType{proto: 2, typ: TypeBlob},
  59. []byte("hello\x00"),
  60. []byte("hello\x00"),
  61. nil,
  62. nil,
  63. },
  64. {
  65. NativeType{proto: 2, typ: TypeBlob},
  66. []byte(nil),
  67. []byte(nil),
  68. nil,
  69. nil,
  70. },
  71. {
  72. NativeType{proto: 2, typ: TypeTimeUUID},
  73. []byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
  74. func() UUID {
  75. x, _ := UUIDFromBytes([]byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0})
  76. return x
  77. }(),
  78. nil,
  79. nil,
  80. },
  81. {
  82. NativeType{proto: 2, typ: TypeTimeUUID},
  83. []byte{0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
  84. []byte{0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
  85. MarshalError("can not marshal []byte 6 bytes long into timeuuid, must be exactly 16 bytes long"),
  86. UnmarshalError("Unable to parse UUID: UUIDs must be exactly 16 bytes long"),
  87. },
  88. {
  89. NativeType{proto: 2, typ: TypeInt},
  90. []byte("\x00\x00\x00\x00"),
  91. 0,
  92. nil,
  93. nil,
  94. },
  95. {
  96. NativeType{proto: 2, typ: TypeInt},
  97. []byte("\x01\x02\x03\x04"),
  98. int(16909060),
  99. nil,
  100. nil,
  101. },
  102. {
  103. NativeType{proto: 2, typ: TypeInt},
  104. []byte("\x01\x02\x03\x04"),
  105. AliasInt(16909060),
  106. nil,
  107. nil,
  108. },
  109. {
  110. NativeType{proto: 2, typ: TypeInt},
  111. []byte("\x80\x00\x00\x00"),
  112. int32(math.MinInt32),
  113. nil,
  114. nil,
  115. },
  116. {
  117. NativeType{proto: 2, typ: TypeInt},
  118. []byte("\x7f\xff\xff\xff"),
  119. int32(math.MaxInt32),
  120. nil,
  121. nil,
  122. },
  123. {
  124. NativeType{proto: 2, typ: TypeInt},
  125. []byte("\x00\x00\x00\x00"),
  126. "0",
  127. nil,
  128. nil,
  129. },
  130. {
  131. NativeType{proto: 2, typ: TypeInt},
  132. []byte("\x01\x02\x03\x04"),
  133. "16909060",
  134. nil,
  135. nil,
  136. },
  137. {
  138. NativeType{proto: 2, typ: TypeInt},
  139. []byte("\x80\x00\x00\x00"),
  140. "-2147483648", // math.MinInt32
  141. nil,
  142. nil,
  143. },
  144. {
  145. NativeType{proto: 2, typ: TypeInt},
  146. []byte("\x7f\xff\xff\xff"),
  147. "2147483647", // math.MaxInt32
  148. nil,
  149. nil,
  150. },
  151. {
  152. NativeType{proto: 2, typ: TypeBigInt},
  153. []byte("\x00\x00\x00\x00\x00\x00\x00\x00"),
  154. 0,
  155. nil,
  156. nil,
  157. },
  158. {
  159. NativeType{proto: 2, typ: TypeBigInt},
  160. []byte("\x01\x02\x03\x04\x05\x06\x07\x08"),
  161. 72623859790382856,
  162. nil,
  163. nil,
  164. },
  165. {
  166. NativeType{proto: 2, typ: TypeBigInt},
  167. []byte("\x80\x00\x00\x00\x00\x00\x00\x00"),
  168. int64(math.MinInt64),
  169. nil,
  170. nil,
  171. },
  172. {
  173. NativeType{proto: 2, typ: TypeBigInt},
  174. []byte("\x7f\xff\xff\xff\xff\xff\xff\xff"),
  175. int64(math.MaxInt64),
  176. nil,
  177. nil,
  178. },
  179. {
  180. NativeType{proto: 2, typ: TypeBigInt},
  181. []byte("\x00\x00\x00\x00\x00\x00\x00\x00"),
  182. "0",
  183. nil,
  184. nil,
  185. },
  186. {
  187. NativeType{proto: 2, typ: TypeBigInt},
  188. []byte("\x01\x02\x03\x04\x05\x06\x07\x08"),
  189. "72623859790382856",
  190. nil,
  191. nil,
  192. },
  193. {
  194. NativeType{proto: 2, typ: TypeBigInt},
  195. []byte("\x80\x00\x00\x00\x00\x00\x00\x00"),
  196. "-9223372036854775808", // math.MinInt64
  197. nil,
  198. nil,
  199. },
  200. {
  201. NativeType{proto: 2, typ: TypeBigInt},
  202. []byte("\x7f\xff\xff\xff\xff\xff\xff\xff"),
  203. "9223372036854775807", // math.MaxInt64
  204. nil,
  205. nil,
  206. },
  207. {
  208. NativeType{proto: 2, typ: TypeBoolean},
  209. []byte("\x00"),
  210. false,
  211. nil,
  212. nil,
  213. },
  214. {
  215. NativeType{proto: 2, typ: TypeBoolean},
  216. []byte("\x01"),
  217. true,
  218. nil,
  219. nil,
  220. },
  221. {
  222. NativeType{proto: 2, typ: TypeFloat},
  223. []byte("\x40\x49\x0f\xdb"),
  224. float32(3.14159265),
  225. nil,
  226. nil,
  227. },
  228. {
  229. NativeType{proto: 2, typ: TypeDouble},
  230. []byte("\x40\x09\x21\xfb\x53\xc8\xd4\xf1"),
  231. float64(3.14159265),
  232. nil,
  233. nil,
  234. },
  235. {
  236. NativeType{proto: 2, typ: TypeDecimal},
  237. []byte("\x00\x00\x00\x00\x00"),
  238. inf.NewDec(0, 0),
  239. nil,
  240. nil,
  241. },
  242. {
  243. NativeType{proto: 2, typ: TypeDecimal},
  244. []byte("\x00\x00\x00\x00\x64"),
  245. inf.NewDec(100, 0),
  246. nil,
  247. nil,
  248. },
  249. {
  250. NativeType{proto: 2, typ: TypeDecimal},
  251. []byte("\x00\x00\x00\x02\x19"),
  252. decimalize("0.25"),
  253. nil,
  254. nil,
  255. },
  256. {
  257. NativeType{proto: 2, typ: TypeDecimal},
  258. []byte("\x00\x00\x00\x13\xD5\a;\x20\x14\xA2\x91"),
  259. decimalize("-0.0012095473475870063"), // From the iconara/cql-rb test suite
  260. nil,
  261. nil,
  262. },
  263. {
  264. NativeType{proto: 2, typ: TypeDecimal},
  265. []byte("\x00\x00\x00\x13*\xF8\xC4\xDF\xEB]o"),
  266. decimalize("0.0012095473475870063"), // From the iconara/cql-rb test suite
  267. nil,
  268. nil,
  269. },
  270. {
  271. NativeType{proto: 2, typ: TypeDecimal},
  272. []byte("\x00\x00\x00\x12\xF2\xD8\x02\xB6R\x7F\x99\xEE\x98#\x99\xA9V"),
  273. decimalize("-1042342234234.123423435647768234"), // From the iconara/cql-rb test suite
  274. nil,
  275. nil,
  276. },
  277. {
  278. NativeType{proto: 2, typ: TypeDecimal},
  279. []byte("\x00\x00\x00\r\nJ\x04\"^\x91\x04\x8a\xb1\x18\xfe"),
  280. decimalize("1243878957943.1234124191998"), // From the datastax/python-driver test suite
  281. nil,
  282. nil,
  283. },
  284. {
  285. NativeType{proto: 2, typ: TypeDecimal},
  286. []byte("\x00\x00\x00\x06\xe5\xde]\x98Y"),
  287. decimalize("-112233.441191"), // From the datastax/python-driver test suite
  288. nil,
  289. nil,
  290. },
  291. {
  292. NativeType{proto: 2, typ: TypeDecimal},
  293. []byte("\x00\x00\x00\x14\x00\xfa\xce"),
  294. decimalize("0.00000000000000064206"), // From the datastax/python-driver test suite
  295. nil,
  296. nil,
  297. },
  298. {
  299. NativeType{proto: 2, typ: TypeDecimal},
  300. []byte("\x00\x00\x00\x14\xff\x052"),
  301. decimalize("-0.00000000000000064206"), // From the datastax/python-driver test suite
  302. nil,
  303. nil,
  304. },
  305. {
  306. NativeType{proto: 2, typ: TypeDecimal},
  307. []byte("\xff\xff\xff\x9c\x00\xfa\xce"),
  308. inf.NewDec(64206, -100), // From the datastax/python-driver test suite
  309. nil,
  310. nil,
  311. },
  312. {
  313. NativeType{proto: 2, typ: TypeTimestamp},
  314. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  315. time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC),
  316. nil,
  317. nil,
  318. },
  319. {
  320. NativeType{proto: 2, typ: TypeTimestamp},
  321. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  322. int64(1376387523000),
  323. nil,
  324. nil,
  325. },
  326. {
  327. CollectionType{
  328. NativeType: NativeType{proto: 2, typ: TypeList},
  329. Elem: NativeType{proto: 2, typ: TypeInt},
  330. },
  331. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  332. []int{1, 2},
  333. nil,
  334. nil,
  335. },
  336. {
  337. CollectionType{
  338. NativeType: NativeType{proto: 2, typ: TypeList},
  339. Elem: NativeType{proto: 2, typ: TypeInt},
  340. },
  341. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  342. [2]int{1, 2},
  343. nil,
  344. nil,
  345. },
  346. {
  347. CollectionType{
  348. NativeType: NativeType{proto: 2, typ: TypeSet},
  349. Elem: NativeType{proto: 2, typ: TypeInt},
  350. },
  351. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  352. []int{1, 2},
  353. nil,
  354. nil,
  355. },
  356. {
  357. CollectionType{
  358. NativeType: NativeType{proto: 2, typ: TypeSet},
  359. Elem: NativeType{proto: 2, typ: TypeInt},
  360. },
  361. []byte{0, 0}, // encoding of a list should always include the size of the collection
  362. []int{},
  363. nil,
  364. nil,
  365. },
  366. {
  367. CollectionType{
  368. NativeType: NativeType{proto: 2, typ: TypeMap},
  369. Key: NativeType{proto: 2, typ: TypeVarchar},
  370. Elem: NativeType{proto: 2, typ: TypeInt},
  371. },
  372. []byte("\x00\x01\x00\x03foo\x00\x04\x00\x00\x00\x01"),
  373. map[string]int{"foo": 1},
  374. nil,
  375. nil,
  376. },
  377. {
  378. CollectionType{
  379. NativeType: NativeType{proto: 2, typ: TypeMap},
  380. Key: NativeType{proto: 2, typ: TypeVarchar},
  381. Elem: NativeType{proto: 2, typ: TypeInt},
  382. },
  383. []byte{0, 0},
  384. map[string]int{},
  385. nil,
  386. nil,
  387. },
  388. {
  389. CollectionType{
  390. NativeType: NativeType{proto: 2, typ: TypeList},
  391. Elem: NativeType{proto: 2, typ: TypeVarchar},
  392. },
  393. bytes.Join([][]byte{
  394. []byte("\x00\x01\xFF\xFF"),
  395. bytes.Repeat([]byte("X"), 65535)}, []byte("")),
  396. []string{strings.Repeat("X", 65535)},
  397. nil,
  398. nil,
  399. },
  400. {
  401. CollectionType{
  402. NativeType: NativeType{proto: 2, typ: TypeMap},
  403. Key: NativeType{proto: 2, typ: TypeVarchar},
  404. Elem: NativeType{proto: 2, typ: TypeVarchar},
  405. },
  406. bytes.Join([][]byte{
  407. []byte("\x00\x01\xFF\xFF"),
  408. bytes.Repeat([]byte("X"), 65535),
  409. []byte("\xFF\xFF"),
  410. bytes.Repeat([]byte("Y"), 65535)}, []byte("")),
  411. map[string]string{
  412. strings.Repeat("X", 65535): strings.Repeat("Y", 65535),
  413. },
  414. nil,
  415. nil,
  416. },
  417. {
  418. NativeType{proto: 2, typ: TypeVarint},
  419. []byte("\x00"),
  420. 0,
  421. nil,
  422. nil,
  423. },
  424. {
  425. NativeType{proto: 2, typ: TypeVarint},
  426. []byte("\x37\xE2\x3C\xEC"),
  427. int32(937573612),
  428. nil,
  429. nil,
  430. },
  431. {
  432. NativeType{proto: 2, typ: TypeVarint},
  433. []byte("\x37\xE2\x3C\xEC"),
  434. big.NewInt(937573612),
  435. nil,
  436. nil,
  437. },
  438. {
  439. NativeType{proto: 2, typ: TypeVarint},
  440. []byte("\x03\x9EV \x15\f\x03\x9DK\x18\xCDI\\$?\a["),
  441. bigintize("1231312312331283012830129382342342412123"), // From the iconara/cql-rb test suite
  442. nil,
  443. nil,
  444. },
  445. {
  446. NativeType{proto: 2, typ: TypeVarint},
  447. []byte("\xC9v\x8D:\x86"),
  448. big.NewInt(-234234234234), // From the iconara/cql-rb test suite
  449. nil,
  450. nil,
  451. },
  452. {
  453. NativeType{proto: 2, typ: TypeVarint},
  454. []byte("f\x1e\xfd\xf2\xe3\xb1\x9f|\x04_\x15"),
  455. bigintize("123456789123456789123456789"), // From the datastax/python-driver test suite
  456. nil,
  457. nil,
  458. },
  459. {
  460. NativeType{proto: 2, typ: TypeVarint},
  461. []byte(nil),
  462. nil,
  463. nil,
  464. UnmarshalError("can not unmarshal into non-pointer <nil>"),
  465. },
  466. {
  467. NativeType{proto: 2, typ: TypeInet},
  468. []byte("\x7F\x00\x00\x01"),
  469. net.ParseIP("127.0.0.1").To4(),
  470. nil,
  471. nil,
  472. },
  473. {
  474. NativeType{proto: 2, typ: TypeInet},
  475. []byte("\xFF\xFF\xFF\xFF"),
  476. net.ParseIP("255.255.255.255").To4(),
  477. nil,
  478. nil,
  479. },
  480. {
  481. NativeType{proto: 2, typ: TypeInet},
  482. []byte("\x7F\x00\x00\x01"),
  483. "127.0.0.1",
  484. nil,
  485. nil,
  486. },
  487. {
  488. NativeType{proto: 2, typ: TypeInet},
  489. []byte("\xFF\xFF\xFF\xFF"),
  490. "255.255.255.255",
  491. nil,
  492. nil,
  493. },
  494. {
  495. NativeType{proto: 2, typ: TypeInet},
  496. []byte("\x21\xDA\x00\xd3\x00\x00\x2f\x3b\x02\xaa\x00\xff\xfe\x28\x9c\x5a"),
  497. "21da:d3:0:2f3b:2aa:ff:fe28:9c5a",
  498. nil,
  499. nil,
  500. },
  501. {
  502. NativeType{proto: 2, typ: TypeInet},
  503. []byte("\xfe\x80\x00\x00\x00\x00\x00\x00\x02\x02\xb3\xff\xfe\x1e\x83\x29"),
  504. "fe80::202:b3ff:fe1e:8329",
  505. nil,
  506. nil,
  507. },
  508. {
  509. NativeType{proto: 2, typ: TypeInet},
  510. []byte("\x21\xDA\x00\xd3\x00\x00\x2f\x3b\x02\xaa\x00\xff\xfe\x28\x9c\x5a"),
  511. net.ParseIP("21da:d3:0:2f3b:2aa:ff:fe28:9c5a"),
  512. nil,
  513. nil,
  514. },
  515. {
  516. NativeType{proto: 2, typ: TypeInet},
  517. []byte("\xfe\x80\x00\x00\x00\x00\x00\x00\x02\x02\xb3\xff\xfe\x1e\x83\x29"),
  518. net.ParseIP("fe80::202:b3ff:fe1e:8329"),
  519. nil,
  520. nil,
  521. },
  522. {
  523. NativeType{proto: 2, typ: TypeInt},
  524. []byte(nil),
  525. nil,
  526. nil,
  527. UnmarshalError("can not unmarshal into non-pointer <nil>"),
  528. },
  529. {
  530. NativeType{proto: 2, typ: TypeVarchar},
  531. []byte("nullable string"),
  532. func() *string {
  533. value := "nullable string"
  534. return &value
  535. }(),
  536. nil,
  537. nil,
  538. },
  539. {
  540. NativeType{proto: 2, typ: TypeVarchar},
  541. []byte(nil),
  542. (*string)(nil),
  543. nil,
  544. nil,
  545. },
  546. {
  547. NativeType{proto: 2, typ: TypeInt},
  548. []byte("\x7f\xff\xff\xff"),
  549. func() *int {
  550. var value int = math.MaxInt32
  551. return &value
  552. }(),
  553. nil,
  554. nil,
  555. },
  556. {
  557. NativeType{proto: 2, typ: TypeInt},
  558. []byte(nil),
  559. (*int)(nil),
  560. nil,
  561. nil,
  562. },
  563. {
  564. NativeType{proto: 2, typ: TypeTimeUUID},
  565. []byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
  566. &UUID{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
  567. nil,
  568. nil,
  569. },
  570. {
  571. NativeType{proto: 2, typ: TypeTimeUUID},
  572. []byte(nil),
  573. (*UUID)(nil),
  574. nil,
  575. nil,
  576. },
  577. {
  578. NativeType{proto: 2, typ: TypeTimestamp},
  579. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  580. func() *time.Time {
  581. t := time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC)
  582. return &t
  583. }(),
  584. nil,
  585. nil,
  586. },
  587. {
  588. NativeType{proto: 2, typ: TypeTimestamp},
  589. []byte(nil),
  590. (*time.Time)(nil),
  591. nil,
  592. nil,
  593. },
  594. {
  595. NativeType{proto: 2, typ: TypeBoolean},
  596. []byte("\x00"),
  597. func() *bool {
  598. b := false
  599. return &b
  600. }(),
  601. nil,
  602. nil,
  603. },
  604. {
  605. NativeType{proto: 2, typ: TypeBoolean},
  606. []byte("\x01"),
  607. func() *bool {
  608. b := true
  609. return &b
  610. }(),
  611. nil,
  612. nil,
  613. },
  614. {
  615. NativeType{proto: 2, typ: TypeBoolean},
  616. []byte(nil),
  617. (*bool)(nil),
  618. nil,
  619. nil,
  620. },
  621. {
  622. NativeType{proto: 2, typ: TypeFloat},
  623. []byte("\x40\x49\x0f\xdb"),
  624. func() *float32 {
  625. f := float32(3.14159265)
  626. return &f
  627. }(),
  628. nil,
  629. nil,
  630. },
  631. {
  632. NativeType{proto: 2, typ: TypeFloat},
  633. []byte(nil),
  634. (*float32)(nil),
  635. nil,
  636. nil,
  637. },
  638. {
  639. NativeType{proto: 2, typ: TypeDouble},
  640. []byte("\x40\x09\x21\xfb\x53\xc8\xd4\xf1"),
  641. func() *float64 {
  642. d := float64(3.14159265)
  643. return &d
  644. }(),
  645. nil,
  646. nil,
  647. },
  648. {
  649. NativeType{proto: 2, typ: TypeDouble},
  650. []byte(nil),
  651. (*float64)(nil),
  652. nil,
  653. nil,
  654. },
  655. {
  656. NativeType{proto: 2, typ: TypeInet},
  657. []byte("\x7F\x00\x00\x01"),
  658. func() *net.IP {
  659. ip := net.ParseIP("127.0.0.1").To4()
  660. return &ip
  661. }(),
  662. nil,
  663. nil,
  664. },
  665. {
  666. NativeType{proto: 2, typ: TypeInet},
  667. []byte(nil),
  668. (*net.IP)(nil),
  669. nil,
  670. nil,
  671. },
  672. {
  673. CollectionType{
  674. NativeType: NativeType{proto: 2, typ: TypeList},
  675. Elem: NativeType{proto: 2, typ: TypeInt},
  676. },
  677. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  678. func() *[]int {
  679. l := []int{1, 2}
  680. return &l
  681. }(),
  682. nil,
  683. nil,
  684. },
  685. {
  686. CollectionType{
  687. NativeType: NativeType{proto: 2, typ: TypeList},
  688. Elem: NativeType{proto: 2, typ: TypeInt},
  689. },
  690. []byte(nil),
  691. (*[]int)(nil),
  692. nil,
  693. nil,
  694. },
  695. {
  696. CollectionType{
  697. NativeType: NativeType{proto: 2, typ: TypeMap},
  698. Key: NativeType{proto: 2, typ: TypeVarchar},
  699. Elem: NativeType{proto: 2, typ: TypeInt},
  700. },
  701. []byte("\x00\x01\x00\x03foo\x00\x04\x00\x00\x00\x01"),
  702. func() *map[string]int {
  703. m := map[string]int{"foo": 1}
  704. return &m
  705. }(),
  706. nil,
  707. nil,
  708. },
  709. {
  710. CollectionType{
  711. NativeType: NativeType{proto: 2, typ: TypeMap},
  712. Key: NativeType{proto: 2, typ: TypeVarchar},
  713. Elem: NativeType{proto: 2, typ: TypeInt},
  714. },
  715. []byte(nil),
  716. (*map[string]int)(nil),
  717. nil,
  718. nil,
  719. },
  720. {
  721. NativeType{proto: 2, typ: TypeVarchar},
  722. []byte("HELLO WORLD"),
  723. func() *CustomString {
  724. customString := CustomString("hello world")
  725. return &customString
  726. }(),
  727. nil,
  728. nil,
  729. },
  730. {
  731. NativeType{proto: 2, typ: TypeVarchar},
  732. []byte(nil),
  733. (*CustomString)(nil),
  734. nil,
  735. nil,
  736. },
  737. {
  738. NativeType{proto: 2, typ: TypeSmallInt},
  739. []byte("\x7f\xff"),
  740. 32767, // math.MaxInt16
  741. nil,
  742. nil,
  743. },
  744. {
  745. NativeType{proto: 2, typ: TypeSmallInt},
  746. []byte("\x7f\xff"),
  747. "32767", // math.MaxInt16
  748. nil,
  749. nil,
  750. },
  751. {
  752. NativeType{proto: 2, typ: TypeSmallInt},
  753. []byte("\x00\x01"),
  754. int16(1),
  755. nil,
  756. nil,
  757. },
  758. {
  759. NativeType{proto: 2, typ: TypeSmallInt},
  760. []byte("\xff\xff"),
  761. int16(-1),
  762. nil,
  763. nil,
  764. },
  765. {
  766. NativeType{proto: 2, typ: TypeSmallInt},
  767. []byte("\xff\xff"),
  768. uint16(65535),
  769. nil,
  770. nil,
  771. },
  772. {
  773. NativeType{proto: 2, typ: TypeTinyInt},
  774. []byte("\x7f"),
  775. 127, // math.MaxInt8
  776. nil,
  777. nil,
  778. },
  779. {
  780. NativeType{proto: 2, typ: TypeTinyInt},
  781. []byte("\x7f"),
  782. "127", // math.MaxInt8
  783. nil,
  784. nil,
  785. },
  786. {
  787. NativeType{proto: 2, typ: TypeTinyInt},
  788. []byte("\x01"),
  789. int16(1),
  790. nil,
  791. nil,
  792. },
  793. {
  794. NativeType{proto: 2, typ: TypeTinyInt},
  795. []byte("\xff"),
  796. int16(-1),
  797. nil,
  798. nil,
  799. },
  800. {
  801. NativeType{proto: 2, typ: TypeTinyInt},
  802. []byte("\xff"),
  803. uint8(255),
  804. nil,
  805. nil,
  806. },
  807. {
  808. NativeType{proto: 2, typ: TypeTinyInt},
  809. []byte("\xff"),
  810. uint64(255),
  811. nil,
  812. nil,
  813. },
  814. {
  815. NativeType{proto: 2, typ: TypeTinyInt},
  816. []byte("\xff"),
  817. uint32(255),
  818. nil,
  819. nil,
  820. },
  821. {
  822. NativeType{proto: 2, typ: TypeTinyInt},
  823. []byte("\xff"),
  824. uint16(255),
  825. nil,
  826. nil,
  827. },
  828. {
  829. NativeType{proto: 2, typ: TypeTinyInt},
  830. []byte("\xff"),
  831. uint(255),
  832. nil,
  833. nil,
  834. },
  835. {
  836. NativeType{proto: 2, typ: TypeBigInt},
  837. []byte("\xff\xff\xff\xff\xff\xff\xff\xff"),
  838. uint64(math.MaxUint64),
  839. nil,
  840. nil,
  841. },
  842. {
  843. NativeType{proto: 2, typ: TypeInt},
  844. []byte("\xff\xff\xff\xff"),
  845. uint32(math.MaxUint32),
  846. nil,
  847. nil,
  848. },
  849. {
  850. NativeType{proto: 2, typ: TypeBlob},
  851. []byte(nil),
  852. ([]byte)(nil),
  853. nil,
  854. nil,
  855. },
  856. {
  857. NativeType{proto: 2, typ: TypeVarchar},
  858. []byte{},
  859. func() interface{} {
  860. var s string
  861. return &s
  862. }(),
  863. nil,
  864. nil,
  865. },
  866. }
  867. func decimalize(s string) *inf.Dec {
  868. i, _ := new(inf.Dec).SetString(s)
  869. return i
  870. }
  871. func bigintize(s string) *big.Int {
  872. i, _ := new(big.Int).SetString(s, 10)
  873. return i
  874. }
  875. func TestMarshal_Encode(t *testing.T) {
  876. for i, test := range marshalTests {
  877. if test.MarshalError == nil {
  878. data, err := Marshal(test.Info, test.Value)
  879. if err != nil {
  880. t.Errorf("marshalTest[%d]: %v", i, err)
  881. continue
  882. }
  883. if !bytes.Equal(data, test.Data) {
  884. t.Errorf("marshalTest[%d]: expected %q, got %q (%#v)", i, test.Data, data, test.Value)
  885. }
  886. } else {
  887. if _, err := Marshal(test.Info, test.Value); err != test.MarshalError {
  888. t.Errorf("unmarshalTest[%d] (%v=>%t): %#v returned error %#v, want %#v.", i, test.Info, test.Value, test.Value, err, test.MarshalError)
  889. }
  890. }
  891. }
  892. }
  893. func TestMarshal_Decode(t *testing.T) {
  894. for i, test := range marshalTests {
  895. if test.UnmarshalError == nil {
  896. v := reflect.New(reflect.TypeOf(test.Value))
  897. err := Unmarshal(test.Info, test.Data, v.Interface())
  898. if err != nil {
  899. t.Errorf("unmarshalTest[%d] (%v=>%T): %v", i, test.Info, test.Value, err)
  900. continue
  901. }
  902. if !reflect.DeepEqual(v.Elem().Interface(), test.Value) {
  903. t.Errorf("unmarshalTest[%d] (%v=>%T): expected %#v, got %#v.", i, test.Info, test.Value, test.Value, v.Elem().Interface())
  904. }
  905. } else {
  906. if err := Unmarshal(test.Info, test.Data, test.Value); err != test.UnmarshalError {
  907. t.Errorf("unmarshalTest[%d] (%v=>%t): %#v returned error %#v, want %#v.", i, test.Info, test.Value, test.Value, err, test.UnmarshalError)
  908. }
  909. }
  910. }
  911. }
  912. func TestMarshalVarint(t *testing.T) {
  913. varintTests := []struct {
  914. Value interface{}
  915. Marshaled []byte
  916. Unmarshaled *big.Int
  917. }{
  918. {
  919. Value: int8(0),
  920. Marshaled: []byte("\x00"),
  921. Unmarshaled: big.NewInt(0),
  922. },
  923. {
  924. Value: uint8(255),
  925. Marshaled: []byte("\x00\xFF"),
  926. Unmarshaled: big.NewInt(255),
  927. },
  928. {
  929. Value: int8(-1),
  930. Marshaled: []byte("\xFF"),
  931. Unmarshaled: big.NewInt(-1),
  932. },
  933. {
  934. Value: big.NewInt(math.MaxInt32),
  935. Marshaled: []byte("\x7F\xFF\xFF\xFF"),
  936. Unmarshaled: big.NewInt(math.MaxInt32),
  937. },
  938. {
  939. Value: big.NewInt(int64(math.MaxInt32) + 1),
  940. Marshaled: []byte("\x00\x80\x00\x00\x00"),
  941. Unmarshaled: big.NewInt(int64(math.MaxInt32) + 1),
  942. },
  943. {
  944. Value: big.NewInt(math.MinInt32),
  945. Marshaled: []byte("\x80\x00\x00\x00"),
  946. Unmarshaled: big.NewInt(math.MinInt32),
  947. },
  948. {
  949. Value: big.NewInt(int64(math.MinInt32) - 1),
  950. Marshaled: []byte("\xFF\x7F\xFF\xFF\xFF"),
  951. Unmarshaled: big.NewInt(int64(math.MinInt32) - 1),
  952. },
  953. {
  954. Value: math.MinInt64,
  955. Marshaled: []byte("\x80\x00\x00\x00\x00\x00\x00\x00"),
  956. Unmarshaled: big.NewInt(math.MinInt64),
  957. },
  958. {
  959. Value: uint64(math.MaxInt64) + 1,
  960. Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00"),
  961. Unmarshaled: bigintize("9223372036854775808"),
  962. },
  963. {
  964. Value: bigintize("2361183241434822606848"), // 2**71
  965. Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00"),
  966. Unmarshaled: bigintize("2361183241434822606848"),
  967. },
  968. {
  969. Value: bigintize("-9223372036854775809"), // -2**63 - 1
  970. Marshaled: []byte("\xFF\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF"),
  971. Unmarshaled: bigintize("-9223372036854775809"),
  972. },
  973. }
  974. for i, test := range varintTests {
  975. data, err := Marshal(NativeType{proto: 2, typ: TypeVarint}, test.Value)
  976. if err != nil {
  977. t.Errorf("error marshaling varint: %v (test #%d)", err, i)
  978. }
  979. if !bytes.Equal(test.Marshaled, data) {
  980. t.Errorf("marshaled varint mismatch: expected %v, got %v (test #%d)", test.Marshaled, data, i)
  981. }
  982. binder := new(big.Int)
  983. err = Unmarshal(NativeType{proto: 2, typ: TypeVarint}, test.Marshaled, binder)
  984. if err != nil {
  985. t.Errorf("error unmarshaling varint: %v (test #%d)", err, i)
  986. }
  987. if test.Unmarshaled.Cmp(binder) != 0 {
  988. t.Errorf("unmarshaled varint mismatch: expected %v, got %v (test #%d)", test.Unmarshaled, binder, i)
  989. }
  990. }
  991. varintUint64Tests := []struct {
  992. Value interface{}
  993. Marshaled []byte
  994. Unmarshaled uint64
  995. }{
  996. {
  997. Value: int8(0),
  998. Marshaled: []byte("\x00"),
  999. Unmarshaled: 0,
  1000. },
  1001. {
  1002. Value: uint8(255),
  1003. Marshaled: []byte("\x00\xFF"),
  1004. Unmarshaled: 255,
  1005. },
  1006. {
  1007. Value: big.NewInt(math.MaxInt32),
  1008. Marshaled: []byte("\x7F\xFF\xFF\xFF"),
  1009. Unmarshaled: uint64(math.MaxInt32),
  1010. },
  1011. {
  1012. Value: big.NewInt(int64(math.MaxInt32) + 1),
  1013. Marshaled: []byte("\x00\x80\x00\x00\x00"),
  1014. Unmarshaled: uint64(int64(math.MaxInt32) + 1),
  1015. },
  1016. {
  1017. Value: uint64(math.MaxInt64) + 1,
  1018. Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00"),
  1019. Unmarshaled: 9223372036854775808,
  1020. },
  1021. {
  1022. Value: uint64(math.MaxUint64),
  1023. Marshaled: []byte("\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"),
  1024. Unmarshaled: uint64(math.MaxUint64),
  1025. },
  1026. }
  1027. for i, test := range varintUint64Tests {
  1028. data, err := Marshal(NativeType{proto: 2, typ: TypeVarint}, test.Value)
  1029. if err != nil {
  1030. t.Errorf("error marshaling varint: %v (test #%d)", err, i)
  1031. }
  1032. if !bytes.Equal(test.Marshaled, data) {
  1033. t.Errorf("marshaled varint mismatch: expected %v, got %v (test #%d)", test.Marshaled, data, i)
  1034. }
  1035. var binder uint64
  1036. err = Unmarshal(NativeType{proto: 2, typ: TypeVarint}, test.Marshaled, &binder)
  1037. if err != nil {
  1038. t.Errorf("error unmarshaling varint to uint64: %v (test #%d)", err, i)
  1039. }
  1040. if test.Unmarshaled != binder {
  1041. t.Errorf("unmarshaled varint mismatch: expected %v, got %v (test #%d)", test.Unmarshaled, binder, i)
  1042. }
  1043. }
  1044. }
  1045. func equalStringSlice(leftList, rightList []string) bool {
  1046. if len(leftList) != len(rightList) {
  1047. return false
  1048. }
  1049. for index := range leftList {
  1050. if rightList[index] != leftList[index] {
  1051. return false
  1052. }
  1053. }
  1054. return true
  1055. }
  1056. func TestMarshalList(t *testing.T) {
  1057. typeInfo := CollectionType{
  1058. NativeType: NativeType{proto: 2, typ: TypeList},
  1059. Elem: NativeType{proto: 2, typ: TypeVarchar},
  1060. }
  1061. sourceLists := [][]string{
  1062. {"valueA"},
  1063. {"valueA", "valueB"},
  1064. {"valueB"},
  1065. }
  1066. listDatas := [][]byte{}
  1067. for _, list := range sourceLists {
  1068. listData, marshalErr := Marshal(typeInfo, list)
  1069. if nil != marshalErr {
  1070. t.Errorf("Error marshal %+v of type %+v: %s", list, typeInfo, marshalErr)
  1071. }
  1072. listDatas = append(listDatas, listData)
  1073. }
  1074. outputLists := [][]string{}
  1075. var outputList []string
  1076. for _, listData := range listDatas {
  1077. if unmarshalErr := Unmarshal(typeInfo, listData, &outputList); nil != unmarshalErr {
  1078. t.Error(unmarshalErr)
  1079. }
  1080. outputLists = append(outputLists, outputList)
  1081. }
  1082. for index, sourceList := range sourceLists {
  1083. outputList := outputLists[index]
  1084. if !equalStringSlice(sourceList, outputList) {
  1085. t.Errorf("Lists %+v not equal to lists %+v, but should", sourceList, outputList)
  1086. }
  1087. }
  1088. }
  1089. type CustomString string
  1090. func (c CustomString) MarshalCQL(info TypeInfo) ([]byte, error) {
  1091. return []byte(strings.ToUpper(string(c))), nil
  1092. }
  1093. func (c *CustomString) UnmarshalCQL(info TypeInfo, data []byte) error {
  1094. *c = CustomString(strings.ToLower(string(data)))
  1095. return nil
  1096. }
  1097. type MyString string
  1098. type MyInt int
  1099. var typeLookupTest = []struct {
  1100. TypeName string
  1101. ExpectedType Type
  1102. }{
  1103. {"AsciiType", TypeAscii},
  1104. {"LongType", TypeBigInt},
  1105. {"BytesType", TypeBlob},
  1106. {"BooleanType", TypeBoolean},
  1107. {"CounterColumnType", TypeCounter},
  1108. {"DecimalType", TypeDecimal},
  1109. {"DoubleType", TypeDouble},
  1110. {"FloatType", TypeFloat},
  1111. {"Int32Type", TypeInt},
  1112. {"DateType", TypeTimestamp},
  1113. {"TimestampType", TypeTimestamp},
  1114. {"UUIDType", TypeUUID},
  1115. {"UTF8Type", TypeVarchar},
  1116. {"IntegerType", TypeVarint},
  1117. {"TimeUUIDType", TypeTimeUUID},
  1118. {"InetAddressType", TypeInet},
  1119. {"MapType", TypeMap},
  1120. {"ListType", TypeList},
  1121. {"SetType", TypeSet},
  1122. {"unknown", TypeCustom},
  1123. {"ShortType", TypeSmallInt},
  1124. {"ByteType", TypeTinyInt},
  1125. }
  1126. func testType(t *testing.T, cassType string, expectedType Type) {
  1127. if computedType := getApacheCassandraType(apacheCassandraTypePrefix + cassType); computedType != expectedType {
  1128. t.Errorf("Cassandra custom type lookup for %s failed. Expected %s, got %s.", cassType, expectedType.String(), computedType.String())
  1129. }
  1130. }
  1131. func TestLookupCassType(t *testing.T) {
  1132. for _, lookupTest := range typeLookupTest {
  1133. testType(t, lookupTest.TypeName, lookupTest.ExpectedType)
  1134. }
  1135. }
  1136. type MyPointerMarshaler struct{}
  1137. func (m *MyPointerMarshaler) MarshalCQL(_ TypeInfo) ([]byte, error) {
  1138. return []byte{42}, nil
  1139. }
  1140. func TestMarshalPointer(t *testing.T) {
  1141. m := &MyPointerMarshaler{}
  1142. typ := NativeType{proto: 2, typ: TypeInt}
  1143. data, err := Marshal(typ, m)
  1144. if err != nil {
  1145. t.Errorf("Pointer marshaling failed. Error: %s", err)
  1146. }
  1147. if len(data) != 1 || data[0] != 42 {
  1148. t.Errorf("Pointer marshaling failed. Expected %+v, got %+v", []byte{42}, data)
  1149. }
  1150. }
  1151. func TestMarshalTimestamp(t *testing.T) {
  1152. var marshalTimestampTests = []struct {
  1153. Info TypeInfo
  1154. Data []byte
  1155. Value interface{}
  1156. }{
  1157. {
  1158. NativeType{proto: 2, typ: TypeTimestamp},
  1159. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  1160. time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC),
  1161. },
  1162. {
  1163. NativeType{proto: 2, typ: TypeTimestamp},
  1164. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  1165. int64(1376387523000),
  1166. },
  1167. {
  1168. // 9223372036854 is the maximum time representable in ms since the epoch
  1169. // with int64 if using UnixNano to convert
  1170. NativeType{proto: 2, typ: TypeTimestamp},
  1171. []byte("\x00\x00\x08\x63\x7b\xd0\x5a\xf6"),
  1172. time.Date(2262, time.April, 11, 23, 47, 16, 854775807, time.UTC),
  1173. },
  1174. {
  1175. // One nanosecond after causes overflow when using UnixNano
  1176. // Instead it should resolve to the same time in ms
  1177. NativeType{proto: 2, typ: TypeTimestamp},
  1178. []byte("\x00\x00\x08\x63\x7b\xd0\x5a\xf6"),
  1179. time.Date(2262, time.April, 11, 23, 47, 16, 854775808, time.UTC),
  1180. },
  1181. {
  1182. // -9223372036855 is the minimum time representable in ms since the epoch
  1183. // with int64 if using UnixNano to convert
  1184. NativeType{proto: 2, typ: TypeTimestamp},
  1185. []byte("\xff\xff\xf7\x9c\x84\x2f\xa5\x09"),
  1186. time.Date(1677, time.September, 21, 00, 12, 43, 145224192, time.UTC),
  1187. },
  1188. {
  1189. // One nanosecond earlier causes overflow when using UnixNano
  1190. // it should resolve to the same time in ms
  1191. NativeType{proto: 2, typ: TypeTimestamp},
  1192. []byte("\xff\xff\xf7\x9c\x84\x2f\xa5\x09"),
  1193. time.Date(1677, time.September, 21, 00, 12, 43, 145224191, time.UTC),
  1194. },
  1195. {
  1196. // Store the zero time as a blank slice
  1197. NativeType{proto: 2, typ: TypeTimestamp},
  1198. []byte{},
  1199. time.Time{},
  1200. },
  1201. }
  1202. for i, test := range marshalTimestampTests {
  1203. t.Log(i, test)
  1204. data, err := Marshal(test.Info, test.Value)
  1205. if err != nil {
  1206. t.Errorf("marshalTest[%d]: %v", i, err)
  1207. continue
  1208. }
  1209. if !bytes.Equal(data, test.Data) {
  1210. t.Errorf("marshalTest[%d]: expected %x (%v), got %x (%v) for time %s", i,
  1211. test.Data, decBigInt(test.Data), data, decBigInt(data), test.Value)
  1212. }
  1213. }
  1214. }
  1215. func TestMarshalTuple(t *testing.T) {
  1216. info := TupleTypeInfo{
  1217. NativeType: NativeType{proto: 3, typ: TypeTuple},
  1218. Elems: []TypeInfo{
  1219. NativeType{proto: 3, typ: TypeVarchar},
  1220. NativeType{proto: 3, typ: TypeVarchar},
  1221. },
  1222. }
  1223. expectedData := []byte("\x00\x00\x00\x03foo\x00\x00\x00\x03bar")
  1224. value := []interface{}{"foo", "bar"}
  1225. data, err := Marshal(info, value)
  1226. if err != nil {
  1227. t.Errorf("marshalTest: %v", err)
  1228. return
  1229. }
  1230. if !bytes.Equal(data, expectedData) {
  1231. t.Errorf("marshalTest: expected %x (%v), got %x (%v)",
  1232. expectedData, decBigInt(expectedData), data, decBigInt(data))
  1233. return
  1234. }
  1235. var s1, s2 string
  1236. val := []interface{}{&s1, &s2}
  1237. err = Unmarshal(info, expectedData, val)
  1238. if err != nil {
  1239. t.Errorf("unmarshalTest: %v", err)
  1240. return
  1241. }
  1242. if s1 != "foo" || s2 != "bar" {
  1243. t.Errorf("unmarshalTest: expected [foo, bar], got [%s, %s]", s1, s2)
  1244. }
  1245. }
  1246. func TestMarshalNil(t *testing.T) {
  1247. types := []Type{
  1248. TypeAscii,
  1249. TypeBlob,
  1250. TypeBoolean,
  1251. TypeBigInt,
  1252. TypeCounter,
  1253. TypeDecimal,
  1254. TypeDouble,
  1255. TypeFloat,
  1256. TypeInt,
  1257. TypeTimestamp,
  1258. TypeUUID,
  1259. TypeVarchar,
  1260. TypeVarint,
  1261. TypeTimeUUID,
  1262. TypeInet,
  1263. }
  1264. for _, typ := range types {
  1265. data, err := Marshal(NativeType{proto: 3, typ: typ}, nil)
  1266. if err != nil {
  1267. t.Errorf("unable to marshal nil %v: %v\n", typ, err)
  1268. } else if data != nil {
  1269. t.Errorf("expected to get nil byte for nil %v got % X", typ, data)
  1270. }
  1271. }
  1272. }
  1273. func TestUnmarshalInetCopyBytes(t *testing.T) {
  1274. data := []byte{127, 0, 0, 1}
  1275. var ip net.IP
  1276. if err := unmarshalInet(NativeType{proto: 2, typ: TypeInet}, data, &ip); err != nil {
  1277. t.Fatal(err)
  1278. }
  1279. copy(data, []byte{0xFF, 0xFF, 0xFF, 0xFF})
  1280. ip2 := net.IP(data)
  1281. if !ip.Equal(net.IPv4(127, 0, 0, 1)) {
  1282. t.Fatalf("IP memory shared with data: ip=%v ip2=%v", ip, ip2)
  1283. }
  1284. }
  1285. func TestUnmarshalDate(t *testing.T) {
  1286. data := []uint8{0x80, 0x0, 0x43, 0x31}
  1287. var date time.Time
  1288. if err := unmarshalDate(NativeType{proto: 2, typ: TypeDate}, data, &date); err != nil {
  1289. t.Fatal(err)
  1290. }
  1291. expectedDate := "2017-02-04"
  1292. formattedDate := date.Format("2006-01-02")
  1293. if expectedDate != formattedDate {
  1294. t.Errorf("marshalTest: expected %v, got %v", expectedDate, formattedDate)
  1295. return
  1296. }
  1297. }
  1298. func TestMarshalDate(t *testing.T) {
  1299. now := time.Now()
  1300. timestamp := now.UnixNano() / int64(time.Millisecond)
  1301. expectedData := encInt(int32(timestamp/86400000 + int64(1<<31)))
  1302. var marshalDateTests = []struct {
  1303. Info TypeInfo
  1304. Data []byte
  1305. Value interface{}
  1306. }{
  1307. {
  1308. NativeType{proto: 4, typ: TypeDate},
  1309. expectedData,
  1310. timestamp,
  1311. },
  1312. {
  1313. NativeType{proto: 4, typ: TypeDate},
  1314. expectedData,
  1315. now,
  1316. },
  1317. {
  1318. NativeType{proto: 4, typ: TypeDate},
  1319. expectedData,
  1320. &now,
  1321. },
  1322. {
  1323. NativeType{proto: 4, typ: TypeDate},
  1324. expectedData,
  1325. now.Format("2006-01-02"),
  1326. },
  1327. }
  1328. for i, test := range marshalDateTests {
  1329. t.Log(i, test)
  1330. data, err := Marshal(test.Info, test.Value)
  1331. if err != nil {
  1332. t.Errorf("marshalTest[%d]: %v", i, err)
  1333. continue
  1334. }
  1335. if !bytes.Equal(data, test.Data) {
  1336. t.Errorf("marshalTest[%d]: expected %x (%v), got %x (%v) for time %s", i,
  1337. test.Data, decInt(test.Data), data, decInt(data), test.Value)
  1338. }
  1339. }
  1340. }