marshal_test.go 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629
  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. var stringDate string
  1378. if err2 := unmarshalDate(NativeType{proto: 2, typ: TypeDate}, data, &stringDate); err2 != nil {
  1379. t.Fatal(err2)
  1380. }
  1381. if expectedDate != stringDate {
  1382. t.Errorf("marshalTest: expected %v, got %v", expectedDate, formattedDate)
  1383. return
  1384. }
  1385. }
  1386. func TestMarshalDate(t *testing.T) {
  1387. now := time.Now().UTC()
  1388. timestamp := now.UnixNano() / int64(time.Millisecond)
  1389. expectedData := encInt(int32(timestamp/86400000 + int64(1<<31)))
  1390. var marshalDateTests = []struct {
  1391. Info TypeInfo
  1392. Data []byte
  1393. Value interface{}
  1394. }{
  1395. {
  1396. NativeType{proto: 4, typ: TypeDate},
  1397. expectedData,
  1398. timestamp,
  1399. },
  1400. {
  1401. NativeType{proto: 4, typ: TypeDate},
  1402. expectedData,
  1403. now,
  1404. },
  1405. {
  1406. NativeType{proto: 4, typ: TypeDate},
  1407. expectedData,
  1408. &now,
  1409. },
  1410. {
  1411. NativeType{proto: 4, typ: TypeDate},
  1412. expectedData,
  1413. now.Format("2006-01-02"),
  1414. },
  1415. }
  1416. for i, test := range marshalDateTests {
  1417. t.Log(i, test)
  1418. data, err := Marshal(test.Info, test.Value)
  1419. if err != nil {
  1420. t.Errorf("marshalTest[%d]: %v", i, err)
  1421. continue
  1422. }
  1423. if !bytes.Equal(data, test.Data) {
  1424. t.Errorf("marshalTest[%d]: expected %x (%v), got %x (%v) for time %s", i,
  1425. test.Data, decInt(test.Data), data, decInt(data), test.Value)
  1426. }
  1427. }
  1428. }
  1429. func BenchmarkUnmarshalVarchar(b *testing.B) {
  1430. b.ReportAllocs()
  1431. src := make([]byte, 1024)
  1432. dst := make([]byte, len(src))
  1433. b.ResetTimer()
  1434. for i := 0; i < b.N; i++ {
  1435. if err := unmarshalVarchar(NativeType{}, src, &dst); err != nil {
  1436. b.Fatal(err)
  1437. }
  1438. }
  1439. }
  1440. func TestMarshalDuration(t *testing.T) {
  1441. durationS := "1h10m10s"
  1442. duration, _ := time.ParseDuration(durationS)
  1443. expectedData := append([]byte{0, 0}, encVint(duration.Nanoseconds())...)
  1444. var marshalDurationTests = []struct {
  1445. Info TypeInfo
  1446. Data []byte
  1447. Value interface{}
  1448. }{
  1449. {
  1450. NativeType{proto: 5, typ: TypeDuration},
  1451. expectedData,
  1452. duration.Nanoseconds(),
  1453. },
  1454. {
  1455. NativeType{proto: 5, typ: TypeDuration},
  1456. expectedData,
  1457. duration,
  1458. },
  1459. {
  1460. NativeType{proto: 5, typ: TypeDuration},
  1461. expectedData,
  1462. durationS,
  1463. },
  1464. {
  1465. NativeType{proto: 5, typ: TypeDuration},
  1466. expectedData,
  1467. &duration,
  1468. },
  1469. }
  1470. for i, test := range marshalDurationTests {
  1471. t.Log(i, test)
  1472. data, err := Marshal(test.Info, test.Value)
  1473. if err != nil {
  1474. t.Errorf("marshalTest[%d]: %v", i, err)
  1475. continue
  1476. }
  1477. if !bytes.Equal(data, test.Data) {
  1478. t.Errorf("marshalTest[%d]: expected %x (%v), got %x (%v) for time %s", i,
  1479. test.Data, decInt(test.Data), data, decInt(data), test.Value)
  1480. }
  1481. }
  1482. }
  1483. func TestReadCollectionSize(t *testing.T) {
  1484. listV2 := CollectionType{
  1485. NativeType: NativeType{proto: 2, typ: TypeList},
  1486. Elem: NativeType{proto: 2, typ: TypeVarchar},
  1487. }
  1488. listV3 := CollectionType{
  1489. NativeType: NativeType{proto: 3, typ: TypeList},
  1490. Elem: NativeType{proto: 3, typ: TypeVarchar},
  1491. }
  1492. tests := []struct {
  1493. name string
  1494. info CollectionType
  1495. data []byte
  1496. isError bool
  1497. expectedSize int
  1498. }{
  1499. {
  1500. name: "short read 0 proto 2",
  1501. info: listV2,
  1502. data: []byte{},
  1503. isError: true,
  1504. },
  1505. {
  1506. name: "short read 1 proto 2",
  1507. info: listV2,
  1508. data: []byte{0x01},
  1509. isError: true,
  1510. },
  1511. {
  1512. name: "good read proto 2",
  1513. info: listV2,
  1514. data: []byte{0x01, 0x38},
  1515. expectedSize: 0x0138,
  1516. },
  1517. {
  1518. name: "short read 0 proto 3",
  1519. info: listV3,
  1520. data: []byte{},
  1521. isError: true,
  1522. },
  1523. {
  1524. name: "short read 1 proto 3",
  1525. info: listV3,
  1526. data: []byte{0x01},
  1527. isError: true,
  1528. },
  1529. {
  1530. name: "short read 2 proto 3",
  1531. info: listV3,
  1532. data: []byte{0x01, 0x38},
  1533. isError: true,
  1534. },
  1535. {
  1536. name: "short read 3 proto 3",
  1537. info: listV3,
  1538. data: []byte{0x01, 0x38, 0x42},
  1539. isError: true,
  1540. },
  1541. {
  1542. name: "good read proto 3",
  1543. info: listV3,
  1544. data: []byte{0x01, 0x38, 0x42, 0x22},
  1545. expectedSize: 0x01384222,
  1546. },
  1547. }
  1548. for _, test := range tests {
  1549. t.Run(test.name, func(t *testing.T) {
  1550. size, _, err := readCollectionSize(test.info, test.data)
  1551. if test.isError {
  1552. if err == nil {
  1553. t.Fatal("Expected error, but it was nil")
  1554. }
  1555. } else {
  1556. if err != nil {
  1557. t.Fatalf("Expected no error, got %v", err)
  1558. }
  1559. if size != test.expectedSize {
  1560. t.Fatalf("Expected size of %d, but got %d", test.expectedSize, size)
  1561. }
  1562. }
  1563. })
  1564. }
  1565. }