marshal_test.go 32 KB

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