marshal_test.go 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  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. func decimalize(s string) *inf.Dec {
  851. i, _ := new(inf.Dec).SetString(s)
  852. return i
  853. }
  854. func bigintize(s string) *big.Int {
  855. i, _ := new(big.Int).SetString(s, 10)
  856. return i
  857. }
  858. func TestMarshal_Encode(t *testing.T) {
  859. for i, test := range marshalTests {
  860. if test.MarshalError == nil {
  861. data, err := Marshal(test.Info, test.Value)
  862. if err != nil {
  863. t.Errorf("marshalTest[%d]: %v", i, err)
  864. continue
  865. }
  866. if !bytes.Equal(data, test.Data) {
  867. t.Errorf("marshalTest[%d]: expected %q, got %q (%#v)", i, test.Data, data, test.Value)
  868. }
  869. } else {
  870. if _, err := Marshal(test.Info, test.Value); err != test.MarshalError {
  871. t.Errorf("unmarshalTest[%d] (%v=>%t): %#v returned error %#v, want %#v.", i, test.Info, test.Value, test.Value, err, test.MarshalError)
  872. }
  873. }
  874. }
  875. }
  876. func TestMarshal_Decode(t *testing.T) {
  877. for i, test := range marshalTests {
  878. if test.UnmarshalError == nil {
  879. v := reflect.New(reflect.TypeOf(test.Value))
  880. err := Unmarshal(test.Info, test.Data, v.Interface())
  881. if err != nil {
  882. t.Errorf("unmarshalTest[%d] (%v=>%T): %v", i, test.Info, test.Value, err)
  883. continue
  884. }
  885. if !reflect.DeepEqual(v.Elem().Interface(), test.Value) {
  886. t.Errorf("unmarshalTest[%d] (%v=>%T): expected %#v, got %#v.", i, test.Info, test.Value, test.Value, v.Elem().Interface())
  887. }
  888. } else {
  889. if err := Unmarshal(test.Info, test.Data, test.Value); err != test.UnmarshalError {
  890. t.Errorf("unmarshalTest[%d] (%v=>%t): %#v returned error %#v, want %#v.", i, test.Info, test.Value, test.Value, err, test.UnmarshalError)
  891. }
  892. }
  893. }
  894. }
  895. func TestMarshalVarint(t *testing.T) {
  896. varintTests := []struct {
  897. Value interface{}
  898. Marshaled []byte
  899. Unmarshaled *big.Int
  900. }{
  901. {
  902. Value: int8(0),
  903. Marshaled: []byte("\x00"),
  904. Unmarshaled: big.NewInt(0),
  905. },
  906. {
  907. Value: uint8(255),
  908. Marshaled: []byte("\x00\xFF"),
  909. Unmarshaled: big.NewInt(255),
  910. },
  911. {
  912. Value: int8(-1),
  913. Marshaled: []byte("\xFF"),
  914. Unmarshaled: big.NewInt(-1),
  915. },
  916. {
  917. Value: big.NewInt(math.MaxInt32),
  918. Marshaled: []byte("\x7F\xFF\xFF\xFF"),
  919. Unmarshaled: big.NewInt(math.MaxInt32),
  920. },
  921. {
  922. Value: big.NewInt(int64(math.MaxInt32) + 1),
  923. Marshaled: []byte("\x00\x80\x00\x00\x00"),
  924. Unmarshaled: big.NewInt(int64(math.MaxInt32) + 1),
  925. },
  926. {
  927. Value: big.NewInt(math.MinInt32),
  928. Marshaled: []byte("\x80\x00\x00\x00"),
  929. Unmarshaled: big.NewInt(math.MinInt32),
  930. },
  931. {
  932. Value: big.NewInt(int64(math.MinInt32) - 1),
  933. Marshaled: []byte("\xFF\x7F\xFF\xFF\xFF"),
  934. Unmarshaled: big.NewInt(int64(math.MinInt32) - 1),
  935. },
  936. {
  937. Value: math.MinInt64,
  938. Marshaled: []byte("\x80\x00\x00\x00\x00\x00\x00\x00"),
  939. Unmarshaled: big.NewInt(math.MinInt64),
  940. },
  941. {
  942. Value: uint64(math.MaxInt64) + 1,
  943. Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00"),
  944. Unmarshaled: bigintize("9223372036854775808"),
  945. },
  946. {
  947. Value: bigintize("2361183241434822606848"), // 2**71
  948. Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00"),
  949. Unmarshaled: bigintize("2361183241434822606848"),
  950. },
  951. {
  952. Value: bigintize("-9223372036854775809"), // -2**63 - 1
  953. Marshaled: []byte("\xFF\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF"),
  954. Unmarshaled: bigintize("-9223372036854775809"),
  955. },
  956. }
  957. for i, test := range varintTests {
  958. data, err := Marshal(NativeType{proto: 2, typ: TypeVarint}, test.Value)
  959. if err != nil {
  960. t.Errorf("error marshaling varint: %v (test #%d)", err, i)
  961. }
  962. if !bytes.Equal(test.Marshaled, data) {
  963. t.Errorf("marshaled varint mismatch: expected %v, got %v (test #%d)", test.Marshaled, data, i)
  964. }
  965. binder := new(big.Int)
  966. err = Unmarshal(NativeType{proto: 2, typ: TypeVarint}, test.Marshaled, binder)
  967. if err != nil {
  968. t.Errorf("error unmarshaling varint: %v (test #%d)", err, i)
  969. }
  970. if test.Unmarshaled.Cmp(binder) != 0 {
  971. t.Errorf("unmarshaled varint mismatch: expected %v, got %v (test #%d)", test.Unmarshaled, binder, i)
  972. }
  973. }
  974. varintUint64Tests := []struct {
  975. Value interface{}
  976. Marshaled []byte
  977. Unmarshaled uint64
  978. }{
  979. {
  980. Value: int8(0),
  981. Marshaled: []byte("\x00"),
  982. Unmarshaled: 0,
  983. },
  984. {
  985. Value: uint8(255),
  986. Marshaled: []byte("\x00\xFF"),
  987. Unmarshaled: 255,
  988. },
  989. {
  990. Value: big.NewInt(math.MaxInt32),
  991. Marshaled: []byte("\x7F\xFF\xFF\xFF"),
  992. Unmarshaled: uint64(math.MaxInt32),
  993. },
  994. {
  995. Value: big.NewInt(int64(math.MaxInt32) + 1),
  996. Marshaled: []byte("\x00\x80\x00\x00\x00"),
  997. Unmarshaled: uint64(int64(math.MaxInt32) + 1),
  998. },
  999. {
  1000. Value: uint64(math.MaxInt64) + 1,
  1001. Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00"),
  1002. Unmarshaled: 9223372036854775808,
  1003. },
  1004. {
  1005. Value: uint64(math.MaxUint64),
  1006. Marshaled: []byte("\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"),
  1007. Unmarshaled: uint64(math.MaxUint64),
  1008. },
  1009. }
  1010. for i, test := range varintUint64Tests {
  1011. data, err := Marshal(NativeType{proto: 2, typ: TypeVarint}, test.Value)
  1012. if err != nil {
  1013. t.Errorf("error marshaling varint: %v (test #%d)", err, i)
  1014. }
  1015. if !bytes.Equal(test.Marshaled, data) {
  1016. t.Errorf("marshaled varint mismatch: expected %v, got %v (test #%d)", test.Marshaled, data, i)
  1017. }
  1018. var binder uint64
  1019. err = Unmarshal(NativeType{proto: 2, typ: TypeVarint}, test.Marshaled, &binder)
  1020. if err != nil {
  1021. t.Errorf("error unmarshaling varint to uint64: %v (test #%d)", err, i)
  1022. }
  1023. if test.Unmarshaled != binder {
  1024. t.Errorf("unmarshaled varint mismatch: expected %v, got %v (test #%d)", test.Unmarshaled, binder, i)
  1025. }
  1026. }
  1027. }
  1028. func equalStringSlice(leftList, rightList []string) bool {
  1029. if len(leftList) != len(rightList) {
  1030. return false
  1031. }
  1032. for index := range leftList {
  1033. if rightList[index] != leftList[index] {
  1034. return false
  1035. }
  1036. }
  1037. return true
  1038. }
  1039. func TestMarshalList(t *testing.T) {
  1040. typeInfo := CollectionType{
  1041. NativeType: NativeType{proto: 2, typ: TypeList},
  1042. Elem: NativeType{proto: 2, typ: TypeVarchar},
  1043. }
  1044. sourceLists := [][]string{
  1045. {"valueA"},
  1046. {"valueA", "valueB"},
  1047. {"valueB"},
  1048. }
  1049. listDatas := [][]byte{}
  1050. for _, list := range sourceLists {
  1051. listData, marshalErr := Marshal(typeInfo, list)
  1052. if nil != marshalErr {
  1053. t.Errorf("Error marshal %+v of type %+v: %s", list, typeInfo, marshalErr)
  1054. }
  1055. listDatas = append(listDatas, listData)
  1056. }
  1057. outputLists := [][]string{}
  1058. var outputList []string
  1059. for _, listData := range listDatas {
  1060. if unmarshalErr := Unmarshal(typeInfo, listData, &outputList); nil != unmarshalErr {
  1061. t.Error(unmarshalErr)
  1062. }
  1063. outputLists = append(outputLists, outputList)
  1064. }
  1065. for index, sourceList := range sourceLists {
  1066. outputList := outputLists[index]
  1067. if !equalStringSlice(sourceList, outputList) {
  1068. t.Errorf("Lists %+v not equal to lists %+v, but should", sourceList, outputList)
  1069. }
  1070. }
  1071. }
  1072. type CustomString string
  1073. func (c CustomString) MarshalCQL(info TypeInfo) ([]byte, error) {
  1074. return []byte(strings.ToUpper(string(c))), nil
  1075. }
  1076. func (c *CustomString) UnmarshalCQL(info TypeInfo, data []byte) error {
  1077. *c = CustomString(strings.ToLower(string(data)))
  1078. return nil
  1079. }
  1080. type MyString string
  1081. type MyInt int
  1082. var typeLookupTest = []struct {
  1083. TypeName string
  1084. ExpectedType Type
  1085. }{
  1086. {"AsciiType", TypeAscii},
  1087. {"LongType", TypeBigInt},
  1088. {"BytesType", TypeBlob},
  1089. {"BooleanType", TypeBoolean},
  1090. {"CounterColumnType", TypeCounter},
  1091. {"DecimalType", TypeDecimal},
  1092. {"DoubleType", TypeDouble},
  1093. {"FloatType", TypeFloat},
  1094. {"Int32Type", TypeInt},
  1095. {"DateType", TypeTimestamp},
  1096. {"TimestampType", TypeTimestamp},
  1097. {"UUIDType", TypeUUID},
  1098. {"UTF8Type", TypeVarchar},
  1099. {"IntegerType", TypeVarint},
  1100. {"TimeUUIDType", TypeTimeUUID},
  1101. {"InetAddressType", TypeInet},
  1102. {"MapType", TypeMap},
  1103. {"ListType", TypeList},
  1104. {"SetType", TypeSet},
  1105. {"unknown", TypeCustom},
  1106. {"ShortType", TypeSmallInt},
  1107. {"ByteType", TypeTinyInt},
  1108. }
  1109. func testType(t *testing.T, cassType string, expectedType Type) {
  1110. if computedType := getApacheCassandraType(apacheCassandraTypePrefix + cassType); computedType != expectedType {
  1111. t.Errorf("Cassandra custom type lookup for %s failed. Expected %s, got %s.", cassType, expectedType.String(), computedType.String())
  1112. }
  1113. }
  1114. func TestLookupCassType(t *testing.T) {
  1115. for _, lookupTest := range typeLookupTest {
  1116. testType(t, lookupTest.TypeName, lookupTest.ExpectedType)
  1117. }
  1118. }
  1119. type MyPointerMarshaler struct{}
  1120. func (m *MyPointerMarshaler) MarshalCQL(_ TypeInfo) ([]byte, error) {
  1121. return []byte{42}, nil
  1122. }
  1123. func TestMarshalPointer(t *testing.T) {
  1124. m := &MyPointerMarshaler{}
  1125. typ := NativeType{proto: 2, typ: TypeInt}
  1126. data, err := Marshal(typ, m)
  1127. if err != nil {
  1128. t.Errorf("Pointer marshaling failed. Error: %s", err)
  1129. }
  1130. if len(data) != 1 || data[0] != 42 {
  1131. t.Errorf("Pointer marshaling failed. Expected %+v, got %+v", []byte{42}, data)
  1132. }
  1133. }
  1134. func TestMarshalTimestamp(t *testing.T) {
  1135. var marshalTimestampTests = []struct {
  1136. Info TypeInfo
  1137. Data []byte
  1138. Value interface{}
  1139. }{
  1140. {
  1141. NativeType{proto: 2, typ: TypeTimestamp},
  1142. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  1143. time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC),
  1144. },
  1145. {
  1146. NativeType{proto: 2, typ: TypeTimestamp},
  1147. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  1148. int64(1376387523000),
  1149. },
  1150. {
  1151. // 9223372036854 is the maximum time representable in ms since the epoch
  1152. // with int64 if using UnixNano to convert
  1153. NativeType{proto: 2, typ: TypeTimestamp},
  1154. []byte("\x00\x00\x08\x63\x7b\xd0\x5a\xf6"),
  1155. time.Date(2262, time.April, 11, 23, 47, 16, 854775807, time.UTC),
  1156. },
  1157. {
  1158. // One nanosecond after causes overflow when using UnixNano
  1159. // Instead it should resolve to the same time in ms
  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, 854775808, time.UTC),
  1163. },
  1164. {
  1165. // -9223372036855 is the minimum time representable in ms since the epoch
  1166. // with int64 if using UnixNano to convert
  1167. NativeType{proto: 2, typ: TypeTimestamp},
  1168. []byte("\xff\xff\xf7\x9c\x84\x2f\xa5\x09"),
  1169. time.Date(1677, time.September, 21, 00, 12, 43, 145224192, time.UTC),
  1170. },
  1171. {
  1172. // One nanosecond earlier causes overflow when using UnixNano
  1173. // it should resolve to the same time in ms
  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, 145224191, time.UTC),
  1177. },
  1178. {
  1179. // Store the zero time as a blank slice
  1180. NativeType{proto: 2, typ: TypeTimestamp},
  1181. []byte{},
  1182. time.Time{},
  1183. },
  1184. }
  1185. for i, test := range marshalTimestampTests {
  1186. t.Log(i, test)
  1187. data, err := Marshal(test.Info, test.Value)
  1188. if err != nil {
  1189. t.Errorf("marshalTest[%d]: %v", i, err)
  1190. continue
  1191. }
  1192. if !bytes.Equal(data, test.Data) {
  1193. t.Errorf("marshalTest[%d]: expected %x (%v), got %x (%v) for time %s", i,
  1194. test.Data, decBigInt(test.Data), data, decBigInt(data), test.Value)
  1195. }
  1196. }
  1197. }
  1198. func TestMarshalTuple(t *testing.T) {
  1199. info := TupleTypeInfo{
  1200. NativeType: NativeType{proto: 3, typ: TypeTuple},
  1201. Elems: []TypeInfo{
  1202. NativeType{proto: 3, typ: TypeVarchar},
  1203. NativeType{proto: 3, typ: TypeVarchar},
  1204. },
  1205. }
  1206. expectedData := []byte("\x00\x00\x00\x03foo\x00\x00\x00\x03bar")
  1207. value := []interface{}{"foo", "bar"}
  1208. data, err := Marshal(info, value)
  1209. if err != nil {
  1210. t.Errorf("marshalTest: %v", err)
  1211. return
  1212. }
  1213. if !bytes.Equal(data, expectedData) {
  1214. t.Errorf("marshalTest: expected %x (%v), got %x (%v)",
  1215. expectedData, decBigInt(expectedData), data, decBigInt(data))
  1216. return
  1217. }
  1218. var s1, s2 string
  1219. val := []interface{}{&s1, &s2}
  1220. err = Unmarshal(info, expectedData, val)
  1221. if err != nil {
  1222. t.Errorf("unmarshalTest: %v", err)
  1223. return
  1224. }
  1225. if s1 != "foo" || s2 != "bar" {
  1226. t.Errorf("unmarshalTest: expected [foo, bar], got [%s, %s]", s1, s2)
  1227. }
  1228. }
  1229. func TestMarshalNil(t *testing.T) {
  1230. types := []Type{
  1231. TypeAscii,
  1232. TypeBlob,
  1233. TypeBoolean,
  1234. TypeBigInt,
  1235. TypeCounter,
  1236. TypeDecimal,
  1237. TypeDouble,
  1238. TypeFloat,
  1239. TypeInt,
  1240. TypeTimestamp,
  1241. TypeUUID,
  1242. TypeVarchar,
  1243. TypeVarint,
  1244. TypeTimeUUID,
  1245. TypeInet,
  1246. }
  1247. for _, typ := range types {
  1248. data, err := Marshal(NativeType{proto: 3, typ: typ}, nil)
  1249. if err != nil {
  1250. t.Errorf("unable to marshal nil %v: %v\n", typ, err)
  1251. } else if data != nil {
  1252. t.Errorf("expected to get nil byte for nil %v got % X", typ, data)
  1253. }
  1254. }
  1255. }
  1256. func TestUnmarshalInetCopyBytes(t *testing.T) {
  1257. data := []byte{127, 0, 0, 1}
  1258. var ip net.IP
  1259. if err := unmarshalInet(NativeType{proto: 2, typ: TypeInet}, data, &ip); err != nil {
  1260. t.Fatal(err)
  1261. }
  1262. copy(data, []byte{0xFF, 0xFF, 0xFF, 0xFF})
  1263. ip2 := net.IP(data)
  1264. if !ip.Equal(net.IPv4(127, 0, 0, 1)) {
  1265. t.Fatalf("IP memory shared with data: ip=%v ip2=%v", ip, ip2)
  1266. }
  1267. }
  1268. func TestUnmarshalDate(t *testing.T) {
  1269. data := []uint8{0x80, 0x0, 0x43, 0x31}
  1270. var date time.Time
  1271. if err := unmarshalDate(NativeType{proto: 2, typ: TypeDate}, data, &date); err != nil {
  1272. t.Fatal(err)
  1273. }
  1274. expectedDate := "2017-02-04"
  1275. formattedDate := date.Format("2006-01-02")
  1276. if expectedDate != formattedDate {
  1277. t.Errorf("marshalTest: expected %v, got %v", expectedDate, formattedDate)
  1278. return
  1279. }
  1280. }
  1281. func TestMarshalDate(t *testing.T) {
  1282. now := time.Now()
  1283. timestamp := now.UnixNano()/int64(time.Millisecond)
  1284. expectedData := encInt(int32(timestamp/86400000 + int64(1 << 31)))
  1285. var marshalDateTests = []struct {
  1286. Info TypeInfo
  1287. Data []byte
  1288. Value interface{}
  1289. }{
  1290. {
  1291. NativeType{proto: 4, typ: TypeDate},
  1292. expectedData,
  1293. timestamp,
  1294. },
  1295. {
  1296. NativeType{proto: 4, typ: TypeDate},
  1297. expectedData,
  1298. now,
  1299. },
  1300. {
  1301. NativeType{proto: 4, typ: TypeDate},
  1302. expectedData,
  1303. &now,
  1304. },
  1305. {
  1306. NativeType{proto: 4, typ: TypeDate},
  1307. expectedData,
  1308. now.Format("2006-01-02"),
  1309. },
  1310. }
  1311. for i, test := range marshalDateTests {
  1312. t.Log(i, test)
  1313. data, err := Marshal(test.Info, test.Value)
  1314. if err != nil {
  1315. t.Errorf("marshalTest[%d]: %v", i, err)
  1316. continue
  1317. }
  1318. if !bytes.Equal(data, test.Data) {
  1319. t.Errorf("marshalTest[%d]: expected %x (%v), got %x (%v) for time %s", i,
  1320. test.Data, decInt(test.Data), data, decInt(data), test.Value)
  1321. }
  1322. }
  1323. }