marshal_test.go 34 KB

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