marshal_test.go 30 KB

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