marshal_test.go 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567
  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. NativeType{proto: 5, typ: TypeDuration},
  328. []byte("\x89\xa2\xc3\xc2\x9a\xe0F\x91\x06"),
  329. Duration{Months: 1233, Days: 123213, Nanoseconds: 2312323},
  330. nil,
  331. nil,
  332. },
  333. {
  334. NativeType{proto: 5, typ: TypeDuration},
  335. []byte("\x89\xa1\xc3\xc2\x99\xe0F\x91\x05"),
  336. Duration{Months: -1233, Days: -123213, Nanoseconds: -2312323},
  337. nil,
  338. nil,
  339. },
  340. {
  341. NativeType{proto: 5, typ: TypeDuration},
  342. []byte("\x02\x04\x80\xe6"),
  343. Duration{Months: 1, Days: 2, Nanoseconds: 115},
  344. nil,
  345. nil,
  346. },
  347. {
  348. CollectionType{
  349. NativeType: NativeType{proto: 2, typ: TypeList},
  350. Elem: NativeType{proto: 2, typ: TypeInt},
  351. },
  352. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  353. []int{1, 2},
  354. nil,
  355. nil,
  356. },
  357. {
  358. CollectionType{
  359. NativeType: NativeType{proto: 2, typ: TypeList},
  360. Elem: NativeType{proto: 2, typ: TypeInt},
  361. },
  362. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  363. [2]int{1, 2},
  364. nil,
  365. nil,
  366. },
  367. {
  368. CollectionType{
  369. NativeType: NativeType{proto: 2, typ: TypeSet},
  370. Elem: NativeType{proto: 2, typ: TypeInt},
  371. },
  372. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  373. []int{1, 2},
  374. nil,
  375. nil,
  376. },
  377. {
  378. CollectionType{
  379. NativeType: NativeType{proto: 2, typ: TypeSet},
  380. Elem: NativeType{proto: 2, typ: TypeInt},
  381. },
  382. []byte{0, 0}, // encoding of a list should always include the size of the collection
  383. []int{},
  384. nil,
  385. nil,
  386. },
  387. {
  388. CollectionType{
  389. NativeType: NativeType{proto: 2, typ: TypeMap},
  390. Key: NativeType{proto: 2, typ: TypeVarchar},
  391. Elem: NativeType{proto: 2, typ: TypeInt},
  392. },
  393. []byte("\x00\x01\x00\x03foo\x00\x04\x00\x00\x00\x01"),
  394. map[string]int{"foo": 1},
  395. nil,
  396. nil,
  397. },
  398. {
  399. CollectionType{
  400. NativeType: NativeType{proto: 2, typ: TypeMap},
  401. Key: NativeType{proto: 2, typ: TypeVarchar},
  402. Elem: NativeType{proto: 2, typ: TypeInt},
  403. },
  404. []byte{0, 0},
  405. map[string]int{},
  406. nil,
  407. nil,
  408. },
  409. {
  410. CollectionType{
  411. NativeType: NativeType{proto: 2, typ: TypeList},
  412. Elem: NativeType{proto: 2, typ: TypeVarchar},
  413. },
  414. bytes.Join([][]byte{
  415. []byte("\x00\x01\xFF\xFF"),
  416. bytes.Repeat([]byte("X"), 65535)}, []byte("")),
  417. []string{strings.Repeat("X", 65535)},
  418. nil,
  419. nil,
  420. },
  421. {
  422. CollectionType{
  423. NativeType: NativeType{proto: 2, typ: TypeMap},
  424. Key: NativeType{proto: 2, typ: TypeVarchar},
  425. Elem: NativeType{proto: 2, typ: TypeVarchar},
  426. },
  427. bytes.Join([][]byte{
  428. []byte("\x00\x01\xFF\xFF"),
  429. bytes.Repeat([]byte("X"), 65535),
  430. []byte("\xFF\xFF"),
  431. bytes.Repeat([]byte("Y"), 65535)}, []byte("")),
  432. map[string]string{
  433. strings.Repeat("X", 65535): strings.Repeat("Y", 65535),
  434. },
  435. nil,
  436. nil,
  437. },
  438. {
  439. NativeType{proto: 2, typ: TypeVarint},
  440. []byte("\x00"),
  441. 0,
  442. nil,
  443. nil,
  444. },
  445. {
  446. NativeType{proto: 2, typ: TypeVarint},
  447. []byte("\x37\xE2\x3C\xEC"),
  448. int32(937573612),
  449. nil,
  450. nil,
  451. },
  452. {
  453. NativeType{proto: 2, typ: TypeVarint},
  454. []byte("\x37\xE2\x3C\xEC"),
  455. big.NewInt(937573612),
  456. nil,
  457. nil,
  458. },
  459. {
  460. NativeType{proto: 2, typ: TypeVarint},
  461. []byte("\x03\x9EV \x15\f\x03\x9DK\x18\xCDI\\$?\a["),
  462. bigintize("1231312312331283012830129382342342412123"), // From the iconara/cql-rb test suite
  463. nil,
  464. nil,
  465. },
  466. {
  467. NativeType{proto: 2, typ: TypeVarint},
  468. []byte("\xC9v\x8D:\x86"),
  469. big.NewInt(-234234234234), // From the iconara/cql-rb test suite
  470. nil,
  471. nil,
  472. },
  473. {
  474. NativeType{proto: 2, typ: TypeVarint},
  475. []byte("f\x1e\xfd\xf2\xe3\xb1\x9f|\x04_\x15"),
  476. bigintize("123456789123456789123456789"), // From the datastax/python-driver test suite
  477. nil,
  478. nil,
  479. },
  480. {
  481. NativeType{proto: 2, typ: TypeVarint},
  482. []byte(nil),
  483. nil,
  484. nil,
  485. UnmarshalError("can not unmarshal into non-pointer <nil>"),
  486. },
  487. {
  488. NativeType{proto: 2, typ: TypeInet},
  489. []byte("\x7F\x00\x00\x01"),
  490. net.ParseIP("127.0.0.1").To4(),
  491. nil,
  492. nil,
  493. },
  494. {
  495. NativeType{proto: 2, typ: TypeInet},
  496. []byte("\xFF\xFF\xFF\xFF"),
  497. net.ParseIP("255.255.255.255").To4(),
  498. nil,
  499. nil,
  500. },
  501. {
  502. NativeType{proto: 2, typ: TypeInet},
  503. []byte("\x7F\x00\x00\x01"),
  504. "127.0.0.1",
  505. nil,
  506. nil,
  507. },
  508. {
  509. NativeType{proto: 2, typ: TypeInet},
  510. []byte("\xFF\xFF\xFF\xFF"),
  511. "255.255.255.255",
  512. nil,
  513. nil,
  514. },
  515. {
  516. NativeType{proto: 2, typ: TypeInet},
  517. []byte("\x21\xDA\x00\xd3\x00\x00\x2f\x3b\x02\xaa\x00\xff\xfe\x28\x9c\x5a"),
  518. "21da:d3:0:2f3b:2aa:ff:fe28:9c5a",
  519. nil,
  520. nil,
  521. },
  522. {
  523. NativeType{proto: 2, typ: TypeInet},
  524. []byte("\xfe\x80\x00\x00\x00\x00\x00\x00\x02\x02\xb3\xff\xfe\x1e\x83\x29"),
  525. "fe80::202:b3ff:fe1e:8329",
  526. nil,
  527. nil,
  528. },
  529. {
  530. NativeType{proto: 2, typ: TypeInet},
  531. []byte("\x21\xDA\x00\xd3\x00\x00\x2f\x3b\x02\xaa\x00\xff\xfe\x28\x9c\x5a"),
  532. net.ParseIP("21da:d3:0:2f3b:2aa:ff:fe28:9c5a"),
  533. nil,
  534. nil,
  535. },
  536. {
  537. NativeType{proto: 2, typ: TypeInet},
  538. []byte("\xfe\x80\x00\x00\x00\x00\x00\x00\x02\x02\xb3\xff\xfe\x1e\x83\x29"),
  539. net.ParseIP("fe80::202:b3ff:fe1e:8329"),
  540. nil,
  541. nil,
  542. },
  543. {
  544. NativeType{proto: 2, typ: TypeInt},
  545. []byte(nil),
  546. nil,
  547. nil,
  548. UnmarshalError("can not unmarshal into non-pointer <nil>"),
  549. },
  550. {
  551. NativeType{proto: 2, typ: TypeVarchar},
  552. []byte("nullable string"),
  553. func() *string {
  554. value := "nullable string"
  555. return &value
  556. }(),
  557. nil,
  558. nil,
  559. },
  560. {
  561. NativeType{proto: 2, typ: TypeVarchar},
  562. []byte(nil),
  563. (*string)(nil),
  564. nil,
  565. nil,
  566. },
  567. {
  568. NativeType{proto: 2, typ: TypeInt},
  569. []byte("\x7f\xff\xff\xff"),
  570. func() *int {
  571. var value int = math.MaxInt32
  572. return &value
  573. }(),
  574. nil,
  575. nil,
  576. },
  577. {
  578. NativeType{proto: 2, typ: TypeInt},
  579. []byte(nil),
  580. (*int)(nil),
  581. nil,
  582. nil,
  583. },
  584. {
  585. NativeType{proto: 2, typ: TypeTimeUUID},
  586. []byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
  587. &UUID{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
  588. nil,
  589. nil,
  590. },
  591. {
  592. NativeType{proto: 2, typ: TypeTimeUUID},
  593. []byte(nil),
  594. (*UUID)(nil),
  595. nil,
  596. nil,
  597. },
  598. {
  599. NativeType{proto: 2, typ: TypeTimestamp},
  600. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  601. func() *time.Time {
  602. t := time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC)
  603. return &t
  604. }(),
  605. nil,
  606. nil,
  607. },
  608. {
  609. NativeType{proto: 2, typ: TypeTimestamp},
  610. []byte(nil),
  611. (*time.Time)(nil),
  612. nil,
  613. nil,
  614. },
  615. {
  616. NativeType{proto: 2, typ: TypeBoolean},
  617. []byte("\x00"),
  618. func() *bool {
  619. b := false
  620. return &b
  621. }(),
  622. nil,
  623. nil,
  624. },
  625. {
  626. NativeType{proto: 2, typ: TypeBoolean},
  627. []byte("\x01"),
  628. func() *bool {
  629. b := true
  630. return &b
  631. }(),
  632. nil,
  633. nil,
  634. },
  635. {
  636. NativeType{proto: 2, typ: TypeBoolean},
  637. []byte(nil),
  638. (*bool)(nil),
  639. nil,
  640. nil,
  641. },
  642. {
  643. NativeType{proto: 2, typ: TypeFloat},
  644. []byte("\x40\x49\x0f\xdb"),
  645. func() *float32 {
  646. f := float32(3.14159265)
  647. return &f
  648. }(),
  649. nil,
  650. nil,
  651. },
  652. {
  653. NativeType{proto: 2, typ: TypeFloat},
  654. []byte(nil),
  655. (*float32)(nil),
  656. nil,
  657. nil,
  658. },
  659. {
  660. NativeType{proto: 2, typ: TypeDouble},
  661. []byte("\x40\x09\x21\xfb\x53\xc8\xd4\xf1"),
  662. func() *float64 {
  663. d := float64(3.14159265)
  664. return &d
  665. }(),
  666. nil,
  667. nil,
  668. },
  669. {
  670. NativeType{proto: 2, typ: TypeDouble},
  671. []byte(nil),
  672. (*float64)(nil),
  673. nil,
  674. nil,
  675. },
  676. {
  677. NativeType{proto: 2, typ: TypeInet},
  678. []byte("\x7F\x00\x00\x01"),
  679. func() *net.IP {
  680. ip := net.ParseIP("127.0.0.1").To4()
  681. return &ip
  682. }(),
  683. nil,
  684. nil,
  685. },
  686. {
  687. NativeType{proto: 2, typ: TypeInet},
  688. []byte(nil),
  689. (*net.IP)(nil),
  690. nil,
  691. nil,
  692. },
  693. {
  694. CollectionType{
  695. NativeType: NativeType{proto: 2, typ: TypeList},
  696. Elem: NativeType{proto: 2, typ: TypeInt},
  697. },
  698. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  699. func() *[]int {
  700. l := []int{1, 2}
  701. return &l
  702. }(),
  703. nil,
  704. nil,
  705. },
  706. {
  707. CollectionType{
  708. NativeType: NativeType{proto: 2, typ: TypeList},
  709. Elem: NativeType{proto: 2, typ: TypeInt},
  710. },
  711. []byte(nil),
  712. (*[]int)(nil),
  713. nil,
  714. nil,
  715. },
  716. {
  717. CollectionType{
  718. NativeType: NativeType{proto: 2, typ: TypeMap},
  719. Key: NativeType{proto: 2, typ: TypeVarchar},
  720. Elem: NativeType{proto: 2, typ: TypeInt},
  721. },
  722. []byte("\x00\x01\x00\x03foo\x00\x04\x00\x00\x00\x01"),
  723. func() *map[string]int {
  724. m := map[string]int{"foo": 1}
  725. return &m
  726. }(),
  727. nil,
  728. nil,
  729. },
  730. {
  731. CollectionType{
  732. NativeType: NativeType{proto: 2, typ: TypeMap},
  733. Key: NativeType{proto: 2, typ: TypeVarchar},
  734. Elem: NativeType{proto: 2, typ: TypeInt},
  735. },
  736. []byte(nil),
  737. (*map[string]int)(nil),
  738. nil,
  739. nil,
  740. },
  741. {
  742. NativeType{proto: 2, typ: TypeVarchar},
  743. []byte("HELLO WORLD"),
  744. func() *CustomString {
  745. customString := CustomString("hello world")
  746. return &customString
  747. }(),
  748. nil,
  749. nil,
  750. },
  751. {
  752. NativeType{proto: 2, typ: TypeVarchar},
  753. []byte(nil),
  754. (*CustomString)(nil),
  755. nil,
  756. nil,
  757. },
  758. {
  759. NativeType{proto: 2, typ: TypeSmallInt},
  760. []byte("\x7f\xff"),
  761. 32767, // math.MaxInt16
  762. nil,
  763. nil,
  764. },
  765. {
  766. NativeType{proto: 2, typ: TypeSmallInt},
  767. []byte("\x7f\xff"),
  768. "32767", // math.MaxInt16
  769. nil,
  770. nil,
  771. },
  772. {
  773. NativeType{proto: 2, typ: TypeSmallInt},
  774. []byte("\x00\x01"),
  775. int16(1),
  776. nil,
  777. nil,
  778. },
  779. {
  780. NativeType{proto: 2, typ: TypeSmallInt},
  781. []byte("\xff\xff"),
  782. int16(-1),
  783. nil,
  784. nil,
  785. },
  786. {
  787. NativeType{proto: 2, typ: TypeSmallInt},
  788. []byte("\xff\xff"),
  789. uint16(65535),
  790. nil,
  791. nil,
  792. },
  793. {
  794. NativeType{proto: 2, typ: TypeTinyInt},
  795. []byte("\x7f"),
  796. 127, // math.MaxInt8
  797. nil,
  798. nil,
  799. },
  800. {
  801. NativeType{proto: 2, typ: TypeTinyInt},
  802. []byte("\x7f"),
  803. "127", // math.MaxInt8
  804. nil,
  805. nil,
  806. },
  807. {
  808. NativeType{proto: 2, typ: TypeTinyInt},
  809. []byte("\x01"),
  810. int16(1),
  811. nil,
  812. nil,
  813. },
  814. {
  815. NativeType{proto: 2, typ: TypeTinyInt},
  816. []byte("\xff"),
  817. int16(-1),
  818. nil,
  819. nil,
  820. },
  821. {
  822. NativeType{proto: 2, typ: TypeTinyInt},
  823. []byte("\xff"),
  824. uint8(255),
  825. nil,
  826. nil,
  827. },
  828. {
  829. NativeType{proto: 2, typ: TypeTinyInt},
  830. []byte("\xff"),
  831. uint64(255),
  832. nil,
  833. nil,
  834. },
  835. {
  836. NativeType{proto: 2, typ: TypeTinyInt},
  837. []byte("\xff"),
  838. uint32(255),
  839. nil,
  840. nil,
  841. },
  842. {
  843. NativeType{proto: 2, typ: TypeTinyInt},
  844. []byte("\xff"),
  845. uint16(255),
  846. nil,
  847. nil,
  848. },
  849. {
  850. NativeType{proto: 2, typ: TypeTinyInt},
  851. []byte("\xff"),
  852. uint(255),
  853. nil,
  854. nil,
  855. },
  856. {
  857. NativeType{proto: 2, typ: TypeBigInt},
  858. []byte("\xff\xff\xff\xff\xff\xff\xff\xff"),
  859. uint64(math.MaxUint64),
  860. nil,
  861. nil,
  862. },
  863. {
  864. NativeType{proto: 2, typ: TypeInt},
  865. []byte("\xff\xff\xff\xff"),
  866. uint32(math.MaxUint32),
  867. nil,
  868. nil,
  869. },
  870. {
  871. NativeType{proto: 2, typ: TypeBlob},
  872. []byte(nil),
  873. ([]byte)(nil),
  874. nil,
  875. nil,
  876. },
  877. {
  878. NativeType{proto: 2, typ: TypeVarchar},
  879. []byte{},
  880. func() interface{} {
  881. var s string
  882. return &s
  883. }(),
  884. nil,
  885. nil,
  886. },
  887. {
  888. NativeType{proto: 2, typ: TypeTime},
  889. encBigInt(1000),
  890. time.Duration(1000),
  891. nil,
  892. nil,
  893. },
  894. }
  895. func decimalize(s string) *inf.Dec {
  896. i, _ := new(inf.Dec).SetString(s)
  897. return i
  898. }
  899. func bigintize(s string) *big.Int {
  900. i, _ := new(big.Int).SetString(s, 10)
  901. return i
  902. }
  903. func TestMarshal_Encode(t *testing.T) {
  904. for i, test := range marshalTests {
  905. if test.MarshalError == nil {
  906. data, err := Marshal(test.Info, test.Value)
  907. if err != nil {
  908. t.Errorf("marshalTest[%d]: %v", i, err)
  909. continue
  910. }
  911. if !bytes.Equal(data, test.Data) {
  912. t.Errorf("marshalTest[%d]: expected %q, got %q (%#v)", i, test.Data, data, test.Value)
  913. }
  914. } else {
  915. if _, err := Marshal(test.Info, test.Value); err != test.MarshalError {
  916. t.Errorf("unmarshalTest[%d] (%v=>%t): %#v returned error %#v, want %#v.", i, test.Info, test.Value, test.Value, err, test.MarshalError)
  917. }
  918. }
  919. }
  920. }
  921. func TestMarshal_Decode(t *testing.T) {
  922. for i, test := range marshalTests {
  923. if test.UnmarshalError == nil {
  924. v := reflect.New(reflect.TypeOf(test.Value))
  925. err := Unmarshal(test.Info, test.Data, v.Interface())
  926. if err != nil {
  927. t.Errorf("unmarshalTest[%d] (%v=>%T): %v", i, test.Info, test.Value, err)
  928. continue
  929. }
  930. if !reflect.DeepEqual(v.Elem().Interface(), test.Value) {
  931. t.Errorf("unmarshalTest[%d] (%v=>%T): expected %#v, got %#v.", i, test.Info, test.Value, test.Value, v.Elem().Interface())
  932. }
  933. } else {
  934. if err := Unmarshal(test.Info, test.Data, test.Value); err != test.UnmarshalError {
  935. t.Errorf("unmarshalTest[%d] (%v=>%t): %#v returned error %#v, want %#v.", i, test.Info, test.Value, test.Value, err, test.UnmarshalError)
  936. }
  937. }
  938. }
  939. }
  940. func TestMarshalVarint(t *testing.T) {
  941. varintTests := []struct {
  942. Value interface{}
  943. Marshaled []byte
  944. Unmarshaled *big.Int
  945. }{
  946. {
  947. Value: int8(0),
  948. Marshaled: []byte("\x00"),
  949. Unmarshaled: big.NewInt(0),
  950. },
  951. {
  952. Value: uint8(255),
  953. Marshaled: []byte("\x00\xFF"),
  954. Unmarshaled: big.NewInt(255),
  955. },
  956. {
  957. Value: int8(-1),
  958. Marshaled: []byte("\xFF"),
  959. Unmarshaled: big.NewInt(-1),
  960. },
  961. {
  962. Value: big.NewInt(math.MaxInt32),
  963. Marshaled: []byte("\x7F\xFF\xFF\xFF"),
  964. Unmarshaled: big.NewInt(math.MaxInt32),
  965. },
  966. {
  967. Value: big.NewInt(int64(math.MaxInt32) + 1),
  968. Marshaled: []byte("\x00\x80\x00\x00\x00"),
  969. Unmarshaled: big.NewInt(int64(math.MaxInt32) + 1),
  970. },
  971. {
  972. Value: big.NewInt(math.MinInt32),
  973. Marshaled: []byte("\x80\x00\x00\x00"),
  974. Unmarshaled: big.NewInt(math.MinInt32),
  975. },
  976. {
  977. Value: big.NewInt(int64(math.MinInt32) - 1),
  978. Marshaled: []byte("\xFF\x7F\xFF\xFF\xFF"),
  979. Unmarshaled: big.NewInt(int64(math.MinInt32) - 1),
  980. },
  981. {
  982. Value: math.MinInt64,
  983. Marshaled: []byte("\x80\x00\x00\x00\x00\x00\x00\x00"),
  984. Unmarshaled: big.NewInt(math.MinInt64),
  985. },
  986. {
  987. Value: uint64(math.MaxInt64) + 1,
  988. Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00"),
  989. Unmarshaled: bigintize("9223372036854775808"),
  990. },
  991. {
  992. Value: bigintize("2361183241434822606848"), // 2**71
  993. Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00"),
  994. Unmarshaled: bigintize("2361183241434822606848"),
  995. },
  996. {
  997. Value: bigintize("-9223372036854775809"), // -2**63 - 1
  998. Marshaled: []byte("\xFF\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF"),
  999. Unmarshaled: bigintize("-9223372036854775809"),
  1000. },
  1001. }
  1002. for i, test := range varintTests {
  1003. data, err := Marshal(NativeType{proto: 2, typ: TypeVarint}, test.Value)
  1004. if err != nil {
  1005. t.Errorf("error marshaling varint: %v (test #%d)", err, i)
  1006. }
  1007. if !bytes.Equal(test.Marshaled, data) {
  1008. t.Errorf("marshaled varint mismatch: expected %v, got %v (test #%d)", test.Marshaled, data, i)
  1009. }
  1010. binder := new(big.Int)
  1011. err = Unmarshal(NativeType{proto: 2, typ: TypeVarint}, test.Marshaled, binder)
  1012. if err != nil {
  1013. t.Errorf("error unmarshaling varint: %v (test #%d)", err, i)
  1014. }
  1015. if test.Unmarshaled.Cmp(binder) != 0 {
  1016. t.Errorf("unmarshaled varint mismatch: expected %v, got %v (test #%d)", test.Unmarshaled, binder, i)
  1017. }
  1018. }
  1019. varintUint64Tests := []struct {
  1020. Value interface{}
  1021. Marshaled []byte
  1022. Unmarshaled uint64
  1023. }{
  1024. {
  1025. Value: int8(0),
  1026. Marshaled: []byte("\x00"),
  1027. Unmarshaled: 0,
  1028. },
  1029. {
  1030. Value: uint8(255),
  1031. Marshaled: []byte("\x00\xFF"),
  1032. Unmarshaled: 255,
  1033. },
  1034. {
  1035. Value: big.NewInt(math.MaxInt32),
  1036. Marshaled: []byte("\x7F\xFF\xFF\xFF"),
  1037. Unmarshaled: uint64(math.MaxInt32),
  1038. },
  1039. {
  1040. Value: big.NewInt(int64(math.MaxInt32) + 1),
  1041. Marshaled: []byte("\x00\x80\x00\x00\x00"),
  1042. Unmarshaled: uint64(int64(math.MaxInt32) + 1),
  1043. },
  1044. {
  1045. Value: uint64(math.MaxInt64) + 1,
  1046. Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00"),
  1047. Unmarshaled: 9223372036854775808,
  1048. },
  1049. {
  1050. Value: uint64(math.MaxUint64),
  1051. Marshaled: []byte("\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"),
  1052. Unmarshaled: uint64(math.MaxUint64),
  1053. },
  1054. }
  1055. for i, test := range varintUint64Tests {
  1056. data, err := Marshal(NativeType{proto: 2, typ: TypeVarint}, test.Value)
  1057. if err != nil {
  1058. t.Errorf("error marshaling varint: %v (test #%d)", err, i)
  1059. }
  1060. if !bytes.Equal(test.Marshaled, data) {
  1061. t.Errorf("marshaled varint mismatch: expected %v, got %v (test #%d)", test.Marshaled, data, i)
  1062. }
  1063. var binder uint64
  1064. err = Unmarshal(NativeType{proto: 2, typ: TypeVarint}, test.Marshaled, &binder)
  1065. if err != nil {
  1066. t.Errorf("error unmarshaling varint to uint64: %v (test #%d)", err, i)
  1067. }
  1068. if test.Unmarshaled != binder {
  1069. t.Errorf("unmarshaled varint mismatch: expected %v, got %v (test #%d)", test.Unmarshaled, binder, i)
  1070. }
  1071. }
  1072. }
  1073. func equalStringSlice(leftList, rightList []string) bool {
  1074. if len(leftList) != len(rightList) {
  1075. return false
  1076. }
  1077. for index := range leftList {
  1078. if rightList[index] != leftList[index] {
  1079. return false
  1080. }
  1081. }
  1082. return true
  1083. }
  1084. func TestMarshalList(t *testing.T) {
  1085. typeInfo := CollectionType{
  1086. NativeType: NativeType{proto: 2, typ: TypeList},
  1087. Elem: NativeType{proto: 2, typ: TypeVarchar},
  1088. }
  1089. sourceLists := [][]string{
  1090. {"valueA"},
  1091. {"valueA", "valueB"},
  1092. {"valueB"},
  1093. }
  1094. listDatas := [][]byte{}
  1095. for _, list := range sourceLists {
  1096. listData, marshalErr := Marshal(typeInfo, list)
  1097. if nil != marshalErr {
  1098. t.Errorf("Error marshal %+v of type %+v: %s", list, typeInfo, marshalErr)
  1099. }
  1100. listDatas = append(listDatas, listData)
  1101. }
  1102. outputLists := [][]string{}
  1103. var outputList []string
  1104. for _, listData := range listDatas {
  1105. if unmarshalErr := Unmarshal(typeInfo, listData, &outputList); nil != unmarshalErr {
  1106. t.Error(unmarshalErr)
  1107. }
  1108. outputLists = append(outputLists, outputList)
  1109. }
  1110. for index, sourceList := range sourceLists {
  1111. outputList := outputLists[index]
  1112. if !equalStringSlice(sourceList, outputList) {
  1113. t.Errorf("Lists %+v not equal to lists %+v, but should", sourceList, outputList)
  1114. }
  1115. }
  1116. }
  1117. type CustomString string
  1118. func (c CustomString) MarshalCQL(info TypeInfo) ([]byte, error) {
  1119. return []byte(strings.ToUpper(string(c))), nil
  1120. }
  1121. func (c *CustomString) UnmarshalCQL(info TypeInfo, data []byte) error {
  1122. *c = CustomString(strings.ToLower(string(data)))
  1123. return nil
  1124. }
  1125. type MyString string
  1126. type MyInt int
  1127. var typeLookupTest = []struct {
  1128. TypeName string
  1129. ExpectedType Type
  1130. }{
  1131. {"AsciiType", TypeAscii},
  1132. {"LongType", TypeBigInt},
  1133. {"BytesType", TypeBlob},
  1134. {"BooleanType", TypeBoolean},
  1135. {"CounterColumnType", TypeCounter},
  1136. {"DecimalType", TypeDecimal},
  1137. {"DoubleType", TypeDouble},
  1138. {"FloatType", TypeFloat},
  1139. {"Int32Type", TypeInt},
  1140. {"DateType", TypeTimestamp},
  1141. {"TimestampType", TypeTimestamp},
  1142. {"UUIDType", TypeUUID},
  1143. {"UTF8Type", TypeVarchar},
  1144. {"IntegerType", TypeVarint},
  1145. {"TimeUUIDType", TypeTimeUUID},
  1146. {"InetAddressType", TypeInet},
  1147. {"MapType", TypeMap},
  1148. {"ListType", TypeList},
  1149. {"SetType", TypeSet},
  1150. {"unknown", TypeCustom},
  1151. {"ShortType", TypeSmallInt},
  1152. {"ByteType", TypeTinyInt},
  1153. }
  1154. func testType(t *testing.T, cassType string, expectedType Type) {
  1155. if computedType := getApacheCassandraType(apacheCassandraTypePrefix + cassType); computedType != expectedType {
  1156. t.Errorf("Cassandra custom type lookup for %s failed. Expected %s, got %s.", cassType, expectedType.String(), computedType.String())
  1157. }
  1158. }
  1159. func TestLookupCassType(t *testing.T) {
  1160. for _, lookupTest := range typeLookupTest {
  1161. testType(t, lookupTest.TypeName, lookupTest.ExpectedType)
  1162. }
  1163. }
  1164. type MyPointerMarshaler struct{}
  1165. func (m *MyPointerMarshaler) MarshalCQL(_ TypeInfo) ([]byte, error) {
  1166. return []byte{42}, nil
  1167. }
  1168. func TestMarshalPointer(t *testing.T) {
  1169. m := &MyPointerMarshaler{}
  1170. typ := NativeType{proto: 2, typ: TypeInt}
  1171. data, err := Marshal(typ, m)
  1172. if err != nil {
  1173. t.Errorf("Pointer marshaling failed. Error: %s", err)
  1174. }
  1175. if len(data) != 1 || data[0] != 42 {
  1176. t.Errorf("Pointer marshaling failed. Expected %+v, got %+v", []byte{42}, data)
  1177. }
  1178. }
  1179. func TestMarshalTimestamp(t *testing.T) {
  1180. var marshalTimestampTests = []struct {
  1181. Info TypeInfo
  1182. Data []byte
  1183. Value interface{}
  1184. }{
  1185. {
  1186. NativeType{proto: 2, typ: TypeTimestamp},
  1187. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  1188. time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC),
  1189. },
  1190. {
  1191. NativeType{proto: 2, typ: TypeTimestamp},
  1192. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  1193. int64(1376387523000),
  1194. },
  1195. {
  1196. // 9223372036854 is the maximum time representable in ms since the epoch
  1197. // with int64 if using UnixNano to convert
  1198. NativeType{proto: 2, typ: TypeTimestamp},
  1199. []byte("\x00\x00\x08\x63\x7b\xd0\x5a\xf6"),
  1200. time.Date(2262, time.April, 11, 23, 47, 16, 854775807, time.UTC),
  1201. },
  1202. {
  1203. // One nanosecond after causes overflow when using UnixNano
  1204. // Instead it should resolve to the same time in ms
  1205. NativeType{proto: 2, typ: TypeTimestamp},
  1206. []byte("\x00\x00\x08\x63\x7b\xd0\x5a\xf6"),
  1207. time.Date(2262, time.April, 11, 23, 47, 16, 854775808, time.UTC),
  1208. },
  1209. {
  1210. // -9223372036855 is the minimum time representable in ms since the epoch
  1211. // with int64 if using UnixNano to convert
  1212. NativeType{proto: 2, typ: TypeTimestamp},
  1213. []byte("\xff\xff\xf7\x9c\x84\x2f\xa5\x09"),
  1214. time.Date(1677, time.September, 21, 00, 12, 43, 145224192, time.UTC),
  1215. },
  1216. {
  1217. // One nanosecond earlier causes overflow when using UnixNano
  1218. // it should resolve to the same time in ms
  1219. NativeType{proto: 2, typ: TypeTimestamp},
  1220. []byte("\xff\xff\xf7\x9c\x84\x2f\xa5\x09"),
  1221. time.Date(1677, time.September, 21, 00, 12, 43, 145224191, time.UTC),
  1222. },
  1223. {
  1224. // Store the zero time as a blank slice
  1225. NativeType{proto: 2, typ: TypeTimestamp},
  1226. []byte{},
  1227. time.Time{},
  1228. },
  1229. }
  1230. for i, test := range marshalTimestampTests {
  1231. t.Log(i, test)
  1232. data, err := Marshal(test.Info, test.Value)
  1233. if err != nil {
  1234. t.Errorf("marshalTest[%d]: %v", i, err)
  1235. continue
  1236. }
  1237. if !bytes.Equal(data, test.Data) {
  1238. t.Errorf("marshalTest[%d]: expected %x (%v), got %x (%v) for time %s", i,
  1239. test.Data, decBigInt(test.Data), data, decBigInt(data), test.Value)
  1240. }
  1241. }
  1242. }
  1243. func TestMarshalTuple(t *testing.T) {
  1244. info := TupleTypeInfo{
  1245. NativeType: NativeType{proto: 3, typ: TypeTuple},
  1246. Elems: []TypeInfo{
  1247. NativeType{proto: 3, typ: TypeVarchar},
  1248. NativeType{proto: 3, typ: TypeVarchar},
  1249. },
  1250. }
  1251. expectedData := []byte("\x00\x00\x00\x03foo\x00\x00\x00\x03bar")
  1252. value := []interface{}{"foo", "bar"}
  1253. data, err := Marshal(info, value)
  1254. if err != nil {
  1255. t.Errorf("marshalTest: %v", err)
  1256. return
  1257. }
  1258. if !bytes.Equal(data, expectedData) {
  1259. t.Errorf("marshalTest: expected %x (%v), got %x (%v)",
  1260. expectedData, decBigInt(expectedData), data, decBigInt(data))
  1261. return
  1262. }
  1263. var s1, s2 string
  1264. val := []interface{}{&s1, &s2}
  1265. err = Unmarshal(info, expectedData, val)
  1266. if err != nil {
  1267. t.Errorf("unmarshalTest: %v", err)
  1268. return
  1269. }
  1270. if s1 != "foo" || s2 != "bar" {
  1271. t.Errorf("unmarshalTest: expected [foo, bar], got [%s, %s]", s1, s2)
  1272. }
  1273. }
  1274. func TestMarshalNil(t *testing.T) {
  1275. types := []Type{
  1276. TypeAscii,
  1277. TypeBlob,
  1278. TypeBoolean,
  1279. TypeBigInt,
  1280. TypeCounter,
  1281. TypeDecimal,
  1282. TypeDouble,
  1283. TypeFloat,
  1284. TypeInt,
  1285. TypeTimestamp,
  1286. TypeUUID,
  1287. TypeVarchar,
  1288. TypeVarint,
  1289. TypeTimeUUID,
  1290. TypeInet,
  1291. }
  1292. for _, typ := range types {
  1293. data, err := Marshal(NativeType{proto: 3, typ: typ}, nil)
  1294. if err != nil {
  1295. t.Errorf("unable to marshal nil %v: %v\n", typ, err)
  1296. } else if data != nil {
  1297. t.Errorf("expected to get nil byte for nil %v got % X", typ, data)
  1298. }
  1299. }
  1300. }
  1301. func TestUnmarshalInetCopyBytes(t *testing.T) {
  1302. data := []byte{127, 0, 0, 1}
  1303. var ip net.IP
  1304. if err := unmarshalInet(NativeType{proto: 2, typ: TypeInet}, data, &ip); err != nil {
  1305. t.Fatal(err)
  1306. }
  1307. copy(data, []byte{0xFF, 0xFF, 0xFF, 0xFF})
  1308. ip2 := net.IP(data)
  1309. if !ip.Equal(net.IPv4(127, 0, 0, 1)) {
  1310. t.Fatalf("IP memory shared with data: ip=%v ip2=%v", ip, ip2)
  1311. }
  1312. }
  1313. func TestUnmarshalDate(t *testing.T) {
  1314. data := []uint8{0x80, 0x0, 0x43, 0x31}
  1315. var date time.Time
  1316. if err := unmarshalDate(NativeType{proto: 2, typ: TypeDate}, data, &date); err != nil {
  1317. t.Fatal(err)
  1318. }
  1319. expectedDate := "2017-02-04"
  1320. formattedDate := date.Format("2006-01-02")
  1321. if expectedDate != formattedDate {
  1322. t.Errorf("marshalTest: expected %v, got %v", expectedDate, formattedDate)
  1323. return
  1324. }
  1325. }
  1326. func TestMarshalDate(t *testing.T) {
  1327. now := time.Now().UTC()
  1328. timestamp := now.UnixNano() / int64(time.Millisecond)
  1329. expectedData := encInt(int32(timestamp/86400000 + int64(1<<31)))
  1330. var marshalDateTests = []struct {
  1331. Info TypeInfo
  1332. Data []byte
  1333. Value interface{}
  1334. }{
  1335. {
  1336. NativeType{proto: 4, typ: TypeDate},
  1337. expectedData,
  1338. timestamp,
  1339. },
  1340. {
  1341. NativeType{proto: 4, typ: TypeDate},
  1342. expectedData,
  1343. now,
  1344. },
  1345. {
  1346. NativeType{proto: 4, typ: TypeDate},
  1347. expectedData,
  1348. &now,
  1349. },
  1350. {
  1351. NativeType{proto: 4, typ: TypeDate},
  1352. expectedData,
  1353. now.Format("2006-01-02"),
  1354. },
  1355. }
  1356. for i, test := range marshalDateTests {
  1357. t.Log(i, test)
  1358. data, err := Marshal(test.Info, test.Value)
  1359. if err != nil {
  1360. t.Errorf("marshalTest[%d]: %v", i, err)
  1361. continue
  1362. }
  1363. if !bytes.Equal(data, test.Data) {
  1364. t.Errorf("marshalTest[%d]: expected %x (%v), got %x (%v) for time %s", i,
  1365. test.Data, decInt(test.Data), data, decInt(data), test.Value)
  1366. }
  1367. }
  1368. }
  1369. func BenchmarkUnmarshalVarchar(b *testing.B) {
  1370. b.ReportAllocs()
  1371. src := make([]byte, 1024)
  1372. dst := make([]byte, len(src))
  1373. b.ResetTimer()
  1374. for i := 0; i < b.N; i++ {
  1375. if err := unmarshalVarchar(NativeType{}, src, &dst); err != nil {
  1376. b.Fatal(err)
  1377. }
  1378. }
  1379. }
  1380. func TestMarshalDuration(t *testing.T) {
  1381. durationS := "1h10m10s"
  1382. duration, _ := time.ParseDuration(durationS)
  1383. expectedData := append([]byte{0, 0}, encVint(duration.Nanoseconds())...)
  1384. var marshalDurationTests = []struct {
  1385. Info TypeInfo
  1386. Data []byte
  1387. Value interface{}
  1388. }{
  1389. {
  1390. NativeType{proto: 5, typ: TypeDuration},
  1391. expectedData,
  1392. duration.Nanoseconds(),
  1393. },
  1394. {
  1395. NativeType{proto: 5, typ: TypeDuration},
  1396. expectedData,
  1397. duration,
  1398. },
  1399. {
  1400. NativeType{proto: 5, typ: TypeDuration},
  1401. expectedData,
  1402. durationS,
  1403. },
  1404. {
  1405. NativeType{proto: 5, typ: TypeDuration},
  1406. expectedData,
  1407. &duration,
  1408. },
  1409. }
  1410. for i, test := range marshalDurationTests {
  1411. t.Log(i, test)
  1412. data, err := Marshal(test.Info, test.Value)
  1413. if err != nil {
  1414. t.Errorf("marshalTest[%d]: %v", i, err)
  1415. continue
  1416. }
  1417. if !bytes.Equal(data, test.Data) {
  1418. t.Errorf("marshalTest[%d]: expected %x (%v), got %x (%v) for time %s", i,
  1419. test.Data, decInt(test.Data), data, decInt(data), test.Value)
  1420. }
  1421. }
  1422. }
  1423. func TestReadCollectionSize(t *testing.T) {
  1424. listV2 := CollectionType{
  1425. NativeType: NativeType{proto: 2, typ: TypeList},
  1426. Elem: NativeType{proto: 2, typ: TypeVarchar},
  1427. }
  1428. listV3 := CollectionType{
  1429. NativeType: NativeType{proto: 3, typ: TypeList},
  1430. Elem: NativeType{proto: 3, typ: TypeVarchar},
  1431. }
  1432. tests := []struct {
  1433. name string
  1434. info CollectionType
  1435. data []byte
  1436. isError bool
  1437. expectedSize int
  1438. }{
  1439. {
  1440. name: "short read 0 proto 2",
  1441. info: listV2,
  1442. data: []byte{},
  1443. isError: true,
  1444. },
  1445. {
  1446. name: "short read 1 proto 2",
  1447. info: listV2,
  1448. data: []byte{0x01},
  1449. isError: true,
  1450. },
  1451. {
  1452. name: "good read proto 2",
  1453. info: listV2,
  1454. data: []byte{0x01, 0x38},
  1455. expectedSize: 0x0138,
  1456. },
  1457. {
  1458. name: "short read 0 proto 3",
  1459. info: listV3,
  1460. data: []byte{},
  1461. isError: true,
  1462. },
  1463. {
  1464. name: "short read 1 proto 3",
  1465. info: listV3,
  1466. data: []byte{0x01},
  1467. isError: true,
  1468. },
  1469. {
  1470. name: "short read 2 proto 3",
  1471. info: listV3,
  1472. data: []byte{0x01, 0x38},
  1473. isError: true,
  1474. },
  1475. {
  1476. name: "short read 3 proto 3",
  1477. info: listV3,
  1478. data: []byte{0x01, 0x38, 0x42},
  1479. isError: true,
  1480. },
  1481. {
  1482. name: "good read proto 3",
  1483. info: listV3,
  1484. data: []byte{0x01, 0x38, 0x42, 0x22},
  1485. expectedSize: 0x01384222,
  1486. },
  1487. }
  1488. for _, test := range tests {
  1489. t.Run(test.name, func(t *testing.T) {
  1490. size, _, err := readCollectionSize(test.info, test.data)
  1491. if test.isError {
  1492. if err == nil {
  1493. t.Fatal("Expected error, but it was nil")
  1494. }
  1495. } else {
  1496. if err != nil {
  1497. t.Fatalf("Expected no error, got %v", err)
  1498. }
  1499. if size != test.expectedSize {
  1500. t.Fatalf("Expected size of %d, but got %d", test.expectedSize, size)
  1501. }
  1502. }
  1503. })
  1504. }
  1505. }