marshal_test.go 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946
  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. type AliasUint uint
  16. type AliasUint8 uint8
  17. type AliasUint16 uint16
  18. type AliasUint32 uint32
  19. type AliasUint64 uint64
  20. var marshalTests = []struct {
  21. Info TypeInfo
  22. Data []byte
  23. Value interface{}
  24. MarshalError error
  25. UnmarshalError error
  26. }{
  27. {
  28. NativeType{proto: 2, typ: TypeVarchar},
  29. []byte("hello world"),
  30. []byte("hello world"),
  31. nil,
  32. nil,
  33. },
  34. {
  35. NativeType{proto: 2, typ: TypeVarchar},
  36. []byte("hello world"),
  37. "hello world",
  38. nil,
  39. nil,
  40. },
  41. {
  42. NativeType{proto: 2, typ: TypeVarchar},
  43. []byte(nil),
  44. []byte(nil),
  45. nil,
  46. nil,
  47. },
  48. {
  49. NativeType{proto: 2, typ: TypeVarchar},
  50. []byte("hello world"),
  51. MyString("hello world"),
  52. nil,
  53. nil,
  54. },
  55. {
  56. NativeType{proto: 2, typ: TypeVarchar},
  57. []byte("HELLO WORLD"),
  58. CustomString("hello world"),
  59. nil,
  60. nil,
  61. },
  62. {
  63. NativeType{proto: 2, typ: TypeBlob},
  64. []byte("hello\x00"),
  65. []byte("hello\x00"),
  66. nil,
  67. nil,
  68. },
  69. {
  70. NativeType{proto: 2, typ: TypeBlob},
  71. []byte(nil),
  72. []byte(nil),
  73. nil,
  74. nil,
  75. },
  76. {
  77. NativeType{proto: 2, typ: TypeTimeUUID},
  78. []byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
  79. func() UUID {
  80. x, _ := UUIDFromBytes([]byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0})
  81. return x
  82. }(),
  83. nil,
  84. nil,
  85. },
  86. {
  87. NativeType{proto: 2, typ: TypeTimeUUID},
  88. []byte{0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
  89. []byte{0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
  90. MarshalError("can not marshal []byte 6 bytes long into timeuuid, must be exactly 16 bytes long"),
  91. UnmarshalError("Unable to parse UUID: UUIDs must be exactly 16 bytes long"),
  92. },
  93. {
  94. NativeType{proto: 2, typ: TypeInt},
  95. []byte("\x00\x00\x00\x00"),
  96. 0,
  97. nil,
  98. nil,
  99. },
  100. {
  101. NativeType{proto: 2, typ: TypeInt},
  102. []byte("\x01\x02\x03\x04"),
  103. int(16909060),
  104. nil,
  105. nil,
  106. },
  107. {
  108. NativeType{proto: 2, typ: TypeInt},
  109. []byte("\x01\x02\x03\x04"),
  110. AliasInt(16909060),
  111. nil,
  112. nil,
  113. },
  114. {
  115. NativeType{proto: 2, typ: TypeInt},
  116. []byte("\x80\x00\x00\x00"),
  117. int32(math.MinInt32),
  118. nil,
  119. nil,
  120. },
  121. {
  122. NativeType{proto: 2, typ: TypeInt},
  123. []byte("\x7f\xff\xff\xff"),
  124. int32(math.MaxInt32),
  125. nil,
  126. nil,
  127. },
  128. {
  129. NativeType{proto: 2, typ: TypeInt},
  130. []byte("\x00\x00\x00\x00"),
  131. "0",
  132. nil,
  133. nil,
  134. },
  135. {
  136. NativeType{proto: 2, typ: TypeInt},
  137. []byte("\x01\x02\x03\x04"),
  138. "16909060",
  139. nil,
  140. nil,
  141. },
  142. {
  143. NativeType{proto: 2, typ: TypeInt},
  144. []byte("\x80\x00\x00\x00"),
  145. "-2147483648", // math.MinInt32
  146. nil,
  147. nil,
  148. },
  149. {
  150. NativeType{proto: 2, typ: TypeInt},
  151. []byte("\x7f\xff\xff\xff"),
  152. "2147483647", // math.MaxInt32
  153. nil,
  154. nil,
  155. },
  156. {
  157. NativeType{proto: 2, typ: TypeBigInt},
  158. []byte("\x00\x00\x00\x00\x00\x00\x00\x00"),
  159. 0,
  160. nil,
  161. nil,
  162. },
  163. {
  164. NativeType{proto: 2, typ: TypeBigInt},
  165. []byte("\x01\x02\x03\x04\x05\x06\x07\x08"),
  166. 72623859790382856,
  167. nil,
  168. nil,
  169. },
  170. {
  171. NativeType{proto: 2, typ: TypeBigInt},
  172. []byte("\x80\x00\x00\x00\x00\x00\x00\x00"),
  173. int64(math.MinInt64),
  174. nil,
  175. nil,
  176. },
  177. {
  178. NativeType{proto: 2, typ: TypeBigInt},
  179. []byte("\x7f\xff\xff\xff\xff\xff\xff\xff"),
  180. int64(math.MaxInt64),
  181. nil,
  182. nil,
  183. },
  184. {
  185. NativeType{proto: 2, typ: TypeBigInt},
  186. []byte("\x00\x00\x00\x00\x00\x00\x00\x00"),
  187. "0",
  188. nil,
  189. nil,
  190. },
  191. {
  192. NativeType{proto: 2, typ: TypeBigInt},
  193. []byte("\x01\x02\x03\x04\x05\x06\x07\x08"),
  194. "72623859790382856",
  195. nil,
  196. nil,
  197. },
  198. {
  199. NativeType{proto: 2, typ: TypeBigInt},
  200. []byte("\x80\x00\x00\x00\x00\x00\x00\x00"),
  201. "-9223372036854775808", // math.MinInt64
  202. nil,
  203. nil,
  204. },
  205. {
  206. NativeType{proto: 2, typ: TypeBigInt},
  207. []byte("\x7f\xff\xff\xff\xff\xff\xff\xff"),
  208. "9223372036854775807", // math.MaxInt64
  209. nil,
  210. nil,
  211. },
  212. {
  213. NativeType{proto: 2, typ: TypeBoolean},
  214. []byte("\x00"),
  215. false,
  216. nil,
  217. nil,
  218. },
  219. {
  220. NativeType{proto: 2, typ: TypeBoolean},
  221. []byte("\x01"),
  222. true,
  223. nil,
  224. nil,
  225. },
  226. {
  227. NativeType{proto: 2, typ: TypeFloat},
  228. []byte("\x40\x49\x0f\xdb"),
  229. float32(3.14159265),
  230. nil,
  231. nil,
  232. },
  233. {
  234. NativeType{proto: 2, typ: TypeDouble},
  235. []byte("\x40\x09\x21\xfb\x53\xc8\xd4\xf1"),
  236. float64(3.14159265),
  237. nil,
  238. nil,
  239. },
  240. {
  241. NativeType{proto: 2, typ: TypeDecimal},
  242. []byte("\x00\x00\x00\x00\x00"),
  243. inf.NewDec(0, 0),
  244. nil,
  245. nil,
  246. },
  247. {
  248. NativeType{proto: 2, typ: TypeDecimal},
  249. []byte("\x00\x00\x00\x00\x64"),
  250. inf.NewDec(100, 0),
  251. nil,
  252. nil,
  253. },
  254. {
  255. NativeType{proto: 2, typ: TypeDecimal},
  256. []byte("\x00\x00\x00\x02\x19"),
  257. decimalize("0.25"),
  258. nil,
  259. nil,
  260. },
  261. {
  262. NativeType{proto: 2, typ: TypeDecimal},
  263. []byte("\x00\x00\x00\x13\xD5\a;\x20\x14\xA2\x91"),
  264. decimalize("-0.0012095473475870063"), // From the iconara/cql-rb test suite
  265. nil,
  266. nil,
  267. },
  268. {
  269. NativeType{proto: 2, typ: TypeDecimal},
  270. []byte("\x00\x00\x00\x13*\xF8\xC4\xDF\xEB]o"),
  271. decimalize("0.0012095473475870063"), // From the iconara/cql-rb test suite
  272. nil,
  273. nil,
  274. },
  275. {
  276. NativeType{proto: 2, typ: TypeDecimal},
  277. []byte("\x00\x00\x00\x12\xF2\xD8\x02\xB6R\x7F\x99\xEE\x98#\x99\xA9V"),
  278. decimalize("-1042342234234.123423435647768234"), // From the iconara/cql-rb test suite
  279. nil,
  280. nil,
  281. },
  282. {
  283. NativeType{proto: 2, typ: TypeDecimal},
  284. []byte("\x00\x00\x00\r\nJ\x04\"^\x91\x04\x8a\xb1\x18\xfe"),
  285. decimalize("1243878957943.1234124191998"), // From the datastax/python-driver test suite
  286. nil,
  287. nil,
  288. },
  289. {
  290. NativeType{proto: 2, typ: TypeDecimal},
  291. []byte("\x00\x00\x00\x06\xe5\xde]\x98Y"),
  292. decimalize("-112233.441191"), // From the datastax/python-driver test suite
  293. nil,
  294. nil,
  295. },
  296. {
  297. NativeType{proto: 2, typ: TypeDecimal},
  298. []byte("\x00\x00\x00\x14\x00\xfa\xce"),
  299. decimalize("0.00000000000000064206"), // From the datastax/python-driver test suite
  300. nil,
  301. nil,
  302. },
  303. {
  304. NativeType{proto: 2, typ: TypeDecimal},
  305. []byte("\x00\x00\x00\x14\xff\x052"),
  306. decimalize("-0.00000000000000064206"), // From the datastax/python-driver test suite
  307. nil,
  308. nil,
  309. },
  310. {
  311. NativeType{proto: 2, typ: TypeDecimal},
  312. []byte("\xff\xff\xff\x9c\x00\xfa\xce"),
  313. inf.NewDec(64206, -100), // From the datastax/python-driver test suite
  314. nil,
  315. nil,
  316. },
  317. {
  318. NativeType{proto: 4, typ: TypeTime},
  319. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  320. time.Duration(int64(1376387523000)),
  321. nil,
  322. nil,
  323. },
  324. {
  325. NativeType{proto: 4, typ: TypeTime},
  326. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  327. int64(1376387523000),
  328. nil,
  329. nil,
  330. },
  331. {
  332. NativeType{proto: 2, typ: TypeTimestamp},
  333. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  334. time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC),
  335. nil,
  336. nil,
  337. },
  338. {
  339. NativeType{proto: 2, typ: TypeTimestamp},
  340. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  341. int64(1376387523000),
  342. nil,
  343. nil,
  344. },
  345. {
  346. NativeType{proto: 5, typ: TypeDuration},
  347. []byte("\x89\xa2\xc3\xc2\x9a\xe0F\x91\x06"),
  348. Duration{Months: 1233, Days: 123213, Nanoseconds: 2312323},
  349. nil,
  350. nil,
  351. },
  352. {
  353. NativeType{proto: 5, typ: TypeDuration},
  354. []byte("\x89\xa1\xc3\xc2\x99\xe0F\x91\x05"),
  355. Duration{Months: -1233, Days: -123213, Nanoseconds: -2312323},
  356. nil,
  357. nil,
  358. },
  359. {
  360. NativeType{proto: 5, typ: TypeDuration},
  361. []byte("\x02\x04\x80\xe6"),
  362. Duration{Months: 1, Days: 2, Nanoseconds: 115},
  363. nil,
  364. nil,
  365. },
  366. {
  367. CollectionType{
  368. NativeType: NativeType{proto: 2, typ: TypeList},
  369. Elem: NativeType{proto: 2, typ: TypeInt},
  370. },
  371. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  372. []int{1, 2},
  373. nil,
  374. nil,
  375. },
  376. {
  377. CollectionType{
  378. NativeType: NativeType{proto: 2, typ: TypeList},
  379. Elem: NativeType{proto: 2, typ: TypeInt},
  380. },
  381. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  382. [2]int{1, 2},
  383. nil,
  384. nil,
  385. },
  386. {
  387. CollectionType{
  388. NativeType: NativeType{proto: 2, typ: TypeSet},
  389. Elem: NativeType{proto: 2, typ: TypeInt},
  390. },
  391. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  392. []int{1, 2},
  393. nil,
  394. nil,
  395. },
  396. {
  397. CollectionType{
  398. NativeType: NativeType{proto: 2, typ: TypeSet},
  399. Elem: NativeType{proto: 2, typ: TypeInt},
  400. },
  401. []byte{0, 0}, // encoding of a list should always include the size of the collection
  402. []int{},
  403. nil,
  404. nil,
  405. },
  406. {
  407. CollectionType{
  408. NativeType: NativeType{proto: 2, typ: TypeMap},
  409. Key: NativeType{proto: 2, typ: TypeVarchar},
  410. Elem: NativeType{proto: 2, typ: TypeInt},
  411. },
  412. []byte("\x00\x01\x00\x03foo\x00\x04\x00\x00\x00\x01"),
  413. map[string]int{"foo": 1},
  414. nil,
  415. nil,
  416. },
  417. {
  418. CollectionType{
  419. NativeType: NativeType{proto: 2, typ: TypeMap},
  420. Key: NativeType{proto: 2, typ: TypeVarchar},
  421. Elem: NativeType{proto: 2, typ: TypeInt},
  422. },
  423. []byte{0, 0},
  424. map[string]int{},
  425. nil,
  426. nil,
  427. },
  428. {
  429. CollectionType{
  430. NativeType: NativeType{proto: 2, typ: TypeList},
  431. Elem: NativeType{proto: 2, typ: TypeVarchar},
  432. },
  433. bytes.Join([][]byte{
  434. []byte("\x00\x01\xFF\xFF"),
  435. bytes.Repeat([]byte("X"), 65535)}, []byte("")),
  436. []string{strings.Repeat("X", 65535)},
  437. nil,
  438. nil,
  439. },
  440. {
  441. CollectionType{
  442. NativeType: NativeType{proto: 2, typ: TypeMap},
  443. Key: NativeType{proto: 2, typ: TypeVarchar},
  444. Elem: NativeType{proto: 2, typ: TypeVarchar},
  445. },
  446. bytes.Join([][]byte{
  447. []byte("\x00\x01\xFF\xFF"),
  448. bytes.Repeat([]byte("X"), 65535),
  449. []byte("\xFF\xFF"),
  450. bytes.Repeat([]byte("Y"), 65535)}, []byte("")),
  451. map[string]string{
  452. strings.Repeat("X", 65535): strings.Repeat("Y", 65535),
  453. },
  454. nil,
  455. nil,
  456. },
  457. {
  458. NativeType{proto: 2, typ: TypeVarint},
  459. []byte("\x00"),
  460. 0,
  461. nil,
  462. nil,
  463. },
  464. {
  465. NativeType{proto: 2, typ: TypeVarint},
  466. []byte("\x37\xE2\x3C\xEC"),
  467. int32(937573612),
  468. nil,
  469. nil,
  470. },
  471. {
  472. NativeType{proto: 2, typ: TypeVarint},
  473. []byte("\x37\xE2\x3C\xEC"),
  474. big.NewInt(937573612),
  475. nil,
  476. nil,
  477. },
  478. {
  479. NativeType{proto: 2, typ: TypeVarint},
  480. []byte("\x03\x9EV \x15\f\x03\x9DK\x18\xCDI\\$?\a["),
  481. bigintize("1231312312331283012830129382342342412123"), // From the iconara/cql-rb test suite
  482. nil,
  483. nil,
  484. },
  485. {
  486. NativeType{proto: 2, typ: TypeVarint},
  487. []byte("\xC9v\x8D:\x86"),
  488. big.NewInt(-234234234234), // From the iconara/cql-rb test suite
  489. nil,
  490. nil,
  491. },
  492. {
  493. NativeType{proto: 2, typ: TypeVarint},
  494. []byte("f\x1e\xfd\xf2\xe3\xb1\x9f|\x04_\x15"),
  495. bigintize("123456789123456789123456789"), // From the datastax/python-driver test suite
  496. nil,
  497. nil,
  498. },
  499. {
  500. NativeType{proto: 2, typ: TypeVarint},
  501. []byte(nil),
  502. nil,
  503. nil,
  504. UnmarshalError("can not unmarshal into non-pointer <nil>"),
  505. },
  506. {
  507. NativeType{proto: 2, typ: TypeInet},
  508. []byte("\x7F\x00\x00\x01"),
  509. net.ParseIP("127.0.0.1").To4(),
  510. nil,
  511. nil,
  512. },
  513. {
  514. NativeType{proto: 2, typ: TypeInet},
  515. []byte("\xFF\xFF\xFF\xFF"),
  516. net.ParseIP("255.255.255.255").To4(),
  517. nil,
  518. nil,
  519. },
  520. {
  521. NativeType{proto: 2, typ: TypeInet},
  522. []byte("\x7F\x00\x00\x01"),
  523. "127.0.0.1",
  524. nil,
  525. nil,
  526. },
  527. {
  528. NativeType{proto: 2, typ: TypeInet},
  529. []byte("\xFF\xFF\xFF\xFF"),
  530. "255.255.255.255",
  531. nil,
  532. nil,
  533. },
  534. {
  535. NativeType{proto: 2, typ: TypeInet},
  536. []byte("\x21\xDA\x00\xd3\x00\x00\x2f\x3b\x02\xaa\x00\xff\xfe\x28\x9c\x5a"),
  537. "21da:d3:0:2f3b:2aa:ff:fe28:9c5a",
  538. nil,
  539. nil,
  540. },
  541. {
  542. NativeType{proto: 2, typ: TypeInet},
  543. []byte("\xfe\x80\x00\x00\x00\x00\x00\x00\x02\x02\xb3\xff\xfe\x1e\x83\x29"),
  544. "fe80::202:b3ff:fe1e:8329",
  545. nil,
  546. nil,
  547. },
  548. {
  549. NativeType{proto: 2, typ: TypeInet},
  550. []byte("\x21\xDA\x00\xd3\x00\x00\x2f\x3b\x02\xaa\x00\xff\xfe\x28\x9c\x5a"),
  551. net.ParseIP("21da:d3:0:2f3b:2aa:ff:fe28:9c5a"),
  552. nil,
  553. nil,
  554. },
  555. {
  556. NativeType{proto: 2, typ: TypeInet},
  557. []byte("\xfe\x80\x00\x00\x00\x00\x00\x00\x02\x02\xb3\xff\xfe\x1e\x83\x29"),
  558. net.ParseIP("fe80::202:b3ff:fe1e:8329"),
  559. nil,
  560. nil,
  561. },
  562. {
  563. NativeType{proto: 2, typ: TypeInt},
  564. []byte(nil),
  565. nil,
  566. nil,
  567. UnmarshalError("can not unmarshal into non-pointer <nil>"),
  568. },
  569. {
  570. NativeType{proto: 2, typ: TypeVarchar},
  571. []byte("nullable string"),
  572. func() *string {
  573. value := "nullable string"
  574. return &value
  575. }(),
  576. nil,
  577. nil,
  578. },
  579. {
  580. NativeType{proto: 2, typ: TypeVarchar},
  581. []byte(nil),
  582. (*string)(nil),
  583. nil,
  584. nil,
  585. },
  586. {
  587. NativeType{proto: 2, typ: TypeInt},
  588. []byte("\x7f\xff\xff\xff"),
  589. func() *int {
  590. var value int = math.MaxInt32
  591. return &value
  592. }(),
  593. nil,
  594. nil,
  595. },
  596. {
  597. NativeType{proto: 2, typ: TypeInt},
  598. []byte(nil),
  599. (*int)(nil),
  600. nil,
  601. nil,
  602. },
  603. {
  604. NativeType{proto: 2, typ: TypeTimeUUID},
  605. []byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
  606. &UUID{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
  607. nil,
  608. nil,
  609. },
  610. {
  611. NativeType{proto: 2, typ: TypeTimeUUID},
  612. []byte(nil),
  613. (*UUID)(nil),
  614. nil,
  615. nil,
  616. },
  617. {
  618. NativeType{proto: 2, typ: TypeTimestamp},
  619. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  620. func() *time.Time {
  621. t := time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC)
  622. return &t
  623. }(),
  624. nil,
  625. nil,
  626. },
  627. {
  628. NativeType{proto: 2, typ: TypeTimestamp},
  629. []byte(nil),
  630. (*time.Time)(nil),
  631. nil,
  632. nil,
  633. },
  634. {
  635. NativeType{proto: 2, typ: TypeBoolean},
  636. []byte("\x00"),
  637. func() *bool {
  638. b := false
  639. return &b
  640. }(),
  641. nil,
  642. nil,
  643. },
  644. {
  645. NativeType{proto: 2, typ: TypeBoolean},
  646. []byte("\x01"),
  647. func() *bool {
  648. b := true
  649. return &b
  650. }(),
  651. nil,
  652. nil,
  653. },
  654. {
  655. NativeType{proto: 2, typ: TypeBoolean},
  656. []byte(nil),
  657. (*bool)(nil),
  658. nil,
  659. nil,
  660. },
  661. {
  662. NativeType{proto: 2, typ: TypeFloat},
  663. []byte("\x40\x49\x0f\xdb"),
  664. func() *float32 {
  665. f := float32(3.14159265)
  666. return &f
  667. }(),
  668. nil,
  669. nil,
  670. },
  671. {
  672. NativeType{proto: 2, typ: TypeFloat},
  673. []byte(nil),
  674. (*float32)(nil),
  675. nil,
  676. nil,
  677. },
  678. {
  679. NativeType{proto: 2, typ: TypeDouble},
  680. []byte("\x40\x09\x21\xfb\x53\xc8\xd4\xf1"),
  681. func() *float64 {
  682. d := float64(3.14159265)
  683. return &d
  684. }(),
  685. nil,
  686. nil,
  687. },
  688. {
  689. NativeType{proto: 2, typ: TypeDouble},
  690. []byte(nil),
  691. (*float64)(nil),
  692. nil,
  693. nil,
  694. },
  695. {
  696. NativeType{proto: 2, typ: TypeInet},
  697. []byte("\x7F\x00\x00\x01"),
  698. func() *net.IP {
  699. ip := net.ParseIP("127.0.0.1").To4()
  700. return &ip
  701. }(),
  702. nil,
  703. nil,
  704. },
  705. {
  706. NativeType{proto: 2, typ: TypeInet},
  707. []byte(nil),
  708. (*net.IP)(nil),
  709. nil,
  710. nil,
  711. },
  712. {
  713. CollectionType{
  714. NativeType: NativeType{proto: 2, typ: TypeList},
  715. Elem: NativeType{proto: 2, typ: TypeInt},
  716. },
  717. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  718. func() *[]int {
  719. l := []int{1, 2}
  720. return &l
  721. }(),
  722. nil,
  723. nil,
  724. },
  725. {
  726. CollectionType{
  727. NativeType: NativeType{proto: 2, typ: TypeList},
  728. Elem: NativeType{proto: 2, typ: TypeInt},
  729. },
  730. []byte(nil),
  731. (*[]int)(nil),
  732. nil,
  733. nil,
  734. },
  735. {
  736. CollectionType{
  737. NativeType: NativeType{proto: 2, typ: TypeMap},
  738. Key: NativeType{proto: 2, typ: TypeVarchar},
  739. Elem: NativeType{proto: 2, typ: TypeInt},
  740. },
  741. []byte("\x00\x01\x00\x03foo\x00\x04\x00\x00\x00\x01"),
  742. func() *map[string]int {
  743. m := map[string]int{"foo": 1}
  744. return &m
  745. }(),
  746. nil,
  747. nil,
  748. },
  749. {
  750. CollectionType{
  751. NativeType: NativeType{proto: 2, typ: TypeMap},
  752. Key: NativeType{proto: 2, typ: TypeVarchar},
  753. Elem: NativeType{proto: 2, typ: TypeInt},
  754. },
  755. []byte(nil),
  756. (*map[string]int)(nil),
  757. nil,
  758. nil,
  759. },
  760. {
  761. NativeType{proto: 2, typ: TypeVarchar},
  762. []byte("HELLO WORLD"),
  763. func() *CustomString {
  764. customString := CustomString("hello world")
  765. return &customString
  766. }(),
  767. nil,
  768. nil,
  769. },
  770. {
  771. NativeType{proto: 2, typ: TypeVarchar},
  772. []byte(nil),
  773. (*CustomString)(nil),
  774. nil,
  775. nil,
  776. },
  777. {
  778. NativeType{proto: 2, typ: TypeSmallInt},
  779. []byte("\x7f\xff"),
  780. 32767, // math.MaxInt16
  781. nil,
  782. nil,
  783. },
  784. {
  785. NativeType{proto: 2, typ: TypeSmallInt},
  786. []byte("\x7f\xff"),
  787. "32767", // math.MaxInt16
  788. nil,
  789. nil,
  790. },
  791. {
  792. NativeType{proto: 2, typ: TypeSmallInt},
  793. []byte("\x00\x01"),
  794. int16(1),
  795. nil,
  796. nil,
  797. },
  798. {
  799. NativeType{proto: 2, typ: TypeSmallInt},
  800. []byte("\xff\xff"),
  801. int16(-1),
  802. nil,
  803. nil,
  804. },
  805. {
  806. NativeType{proto: 2, typ: TypeSmallInt},
  807. []byte("\x00\xff"),
  808. uint8(255),
  809. nil,
  810. nil,
  811. },
  812. {
  813. NativeType{proto: 2, typ: TypeSmallInt},
  814. []byte("\xff\xff"),
  815. uint16(65535),
  816. nil,
  817. nil,
  818. },
  819. {
  820. NativeType{proto: 2, typ: TypeSmallInt},
  821. []byte("\xff\xff"),
  822. uint32(65535),
  823. nil,
  824. nil,
  825. },
  826. {
  827. NativeType{proto: 2, typ: TypeSmallInt},
  828. []byte("\xff\xff"),
  829. uint64(65535),
  830. nil,
  831. nil,
  832. },
  833. {
  834. NativeType{proto: 2, typ: TypeSmallInt},
  835. []byte("\x00\xff"),
  836. AliasUint8(255),
  837. nil,
  838. nil,
  839. },
  840. {
  841. NativeType{proto: 2, typ: TypeSmallInt},
  842. []byte("\xff\xff"),
  843. AliasUint16(65535),
  844. nil,
  845. nil,
  846. },
  847. {
  848. NativeType{proto: 2, typ: TypeSmallInt},
  849. []byte("\xff\xff"),
  850. AliasUint32(65535),
  851. nil,
  852. nil,
  853. },
  854. {
  855. NativeType{proto: 2, typ: TypeSmallInt},
  856. []byte("\xff\xff"),
  857. AliasUint64(65535),
  858. nil,
  859. nil,
  860. },
  861. {
  862. NativeType{proto: 2, typ: TypeSmallInt},
  863. []byte("\xff\xff"),
  864. AliasUint(65535),
  865. nil,
  866. nil,
  867. },
  868. {
  869. NativeType{proto: 2, typ: TypeTinyInt},
  870. []byte("\x7f"),
  871. 127, // math.MaxInt8
  872. nil,
  873. nil,
  874. },
  875. {
  876. NativeType{proto: 2, typ: TypeTinyInt},
  877. []byte("\x7f"),
  878. "127", // math.MaxInt8
  879. nil,
  880. nil,
  881. },
  882. {
  883. NativeType{proto: 2, typ: TypeTinyInt},
  884. []byte("\x01"),
  885. int16(1),
  886. nil,
  887. nil,
  888. },
  889. {
  890. NativeType{proto: 2, typ: TypeTinyInt},
  891. []byte("\xff"),
  892. int16(-1),
  893. nil,
  894. nil,
  895. },
  896. {
  897. NativeType{proto: 2, typ: TypeTinyInt},
  898. []byte("\xff"),
  899. uint8(255),
  900. nil,
  901. nil,
  902. },
  903. {
  904. NativeType{proto: 2, typ: TypeTinyInt},
  905. []byte("\xff"),
  906. uint64(255),
  907. nil,
  908. nil,
  909. },
  910. {
  911. NativeType{proto: 2, typ: TypeTinyInt},
  912. []byte("\xff"),
  913. uint32(255),
  914. nil,
  915. nil,
  916. },
  917. {
  918. NativeType{proto: 2, typ: TypeTinyInt},
  919. []byte("\xff"),
  920. uint16(255),
  921. nil,
  922. nil,
  923. },
  924. {
  925. NativeType{proto: 2, typ: TypeTinyInt},
  926. []byte("\xff"),
  927. uint(255),
  928. nil,
  929. nil,
  930. },
  931. {
  932. NativeType{proto: 2, typ: TypeTinyInt},
  933. []byte("\xff"),
  934. AliasUint8(255),
  935. nil,
  936. nil,
  937. },
  938. {
  939. NativeType{proto: 2, typ: TypeTinyInt},
  940. []byte("\xff"),
  941. AliasUint64(255),
  942. nil,
  943. nil,
  944. },
  945. {
  946. NativeType{proto: 2, typ: TypeTinyInt},
  947. []byte("\xff"),
  948. AliasUint32(255),
  949. nil,
  950. nil,
  951. },
  952. {
  953. NativeType{proto: 2, typ: TypeTinyInt},
  954. []byte("\xff"),
  955. AliasUint16(255),
  956. nil,
  957. nil,
  958. },
  959. {
  960. NativeType{proto: 2, typ: TypeTinyInt},
  961. []byte("\xff"),
  962. AliasUint(255),
  963. nil,
  964. nil,
  965. },
  966. {
  967. NativeType{proto: 2, typ: TypeBigInt},
  968. []byte("\x00\x00\x00\x00\x00\x00\x00\xff"),
  969. uint8(math.MaxUint8),
  970. nil,
  971. nil,
  972. },
  973. {
  974. NativeType{proto: 2, typ: TypeBigInt},
  975. []byte("\x00\x00\x00\x00\x00\x00\xff\xff"),
  976. uint64(math.MaxUint16),
  977. nil,
  978. nil,
  979. },
  980. {
  981. NativeType{proto: 2, typ: TypeBigInt},
  982. []byte("\x00\x00\x00\x00\xff\xff\xff\xff"),
  983. uint64(math.MaxUint32),
  984. nil,
  985. nil,
  986. },
  987. {
  988. NativeType{proto: 2, typ: TypeBigInt},
  989. []byte("\xff\xff\xff\xff\xff\xff\xff\xff"),
  990. uint64(math.MaxUint64),
  991. nil,
  992. nil,
  993. },
  994. {
  995. NativeType{proto: 2, typ: TypeInt},
  996. []byte("\xff\xff\xff\xff"),
  997. uint32(math.MaxUint32),
  998. nil,
  999. nil,
  1000. },
  1001. {
  1002. NativeType{proto: 2, typ: TypeInt},
  1003. []byte("\xff\xff\xff\xff"),
  1004. uint64(math.MaxUint32),
  1005. nil,
  1006. nil,
  1007. },
  1008. {
  1009. NativeType{proto: 2, typ: TypeBlob},
  1010. []byte(nil),
  1011. ([]byte)(nil),
  1012. nil,
  1013. nil,
  1014. },
  1015. {
  1016. NativeType{proto: 2, typ: TypeVarchar},
  1017. []byte{},
  1018. func() interface{} {
  1019. var s string
  1020. return &s
  1021. }(),
  1022. nil,
  1023. nil,
  1024. },
  1025. {
  1026. NativeType{proto: 2, typ: TypeTime},
  1027. encBigInt(1000),
  1028. time.Duration(1000),
  1029. nil,
  1030. nil,
  1031. },
  1032. }
  1033. var unmarshalTests = []struct {
  1034. Info TypeInfo
  1035. Data []byte
  1036. Value interface{}
  1037. UnmarshalError error
  1038. }{
  1039. {
  1040. NativeType{proto: 2, typ: TypeSmallInt},
  1041. []byte("\xff\xff"),
  1042. uint8(0),
  1043. UnmarshalError("unmarshal int: value -1 out of range for uint8"),
  1044. },
  1045. {
  1046. NativeType{proto: 2, typ: TypeSmallInt},
  1047. []byte("\x01\x00"),
  1048. uint8(0),
  1049. UnmarshalError("unmarshal int: value 256 out of range for uint8"),
  1050. },
  1051. {
  1052. NativeType{proto: 2, typ: TypeInt},
  1053. []byte("\xff\xff\xff\xff"),
  1054. uint8(0),
  1055. UnmarshalError("unmarshal int: value -1 out of range for uint8"),
  1056. },
  1057. {
  1058. NativeType{proto: 2, typ: TypeInt},
  1059. []byte("\x00\x00\x01\x00"),
  1060. uint8(0),
  1061. UnmarshalError("unmarshal int: value 256 out of range for uint8"),
  1062. },
  1063. {
  1064. NativeType{proto: 2, typ: TypeInt},
  1065. []byte("\xff\xff\xff\xff"),
  1066. uint16(0),
  1067. UnmarshalError("unmarshal int: value -1 out of range for uint16"),
  1068. },
  1069. {
  1070. NativeType{proto: 2, typ: TypeInt},
  1071. []byte("\x00\x01\x00\x00"),
  1072. uint16(0),
  1073. UnmarshalError("unmarshal int: value 65536 out of range for uint16"),
  1074. },
  1075. {
  1076. NativeType{proto: 2, typ: TypeBigInt},
  1077. []byte("\xff\xff\xff\xff\xff\xff\xff\xff"),
  1078. uint8(0),
  1079. UnmarshalError("unmarshal int: value -1 out of range for uint8"),
  1080. },
  1081. {
  1082. NativeType{proto: 2, typ: TypeBigInt},
  1083. []byte("\x00\x00\x00\x00\x00\x00\x01\x00"),
  1084. uint8(0),
  1085. UnmarshalError("unmarshal int: value 256 out of range for uint8"),
  1086. },
  1087. {
  1088. NativeType{proto: 2, typ: TypeBigInt},
  1089. []byte("\xff\xff\xff\xff\xff\xff\xff\xff"),
  1090. uint8(0),
  1091. UnmarshalError("unmarshal int: value -1 out of range for uint8"),
  1092. },
  1093. {
  1094. NativeType{proto: 2, typ: TypeBigInt},
  1095. []byte("\x00\x00\x00\x00\x00\x00\x01\x00"),
  1096. uint8(0),
  1097. UnmarshalError("unmarshal int: value 256 out of range for uint8"),
  1098. },
  1099. {
  1100. NativeType{proto: 2, typ: TypeBigInt},
  1101. []byte("\xff\xff\xff\xff\xff\xff\xff\xff"),
  1102. uint16(0),
  1103. UnmarshalError("unmarshal int: value -1 out of range for uint16"),
  1104. },
  1105. {
  1106. NativeType{proto: 2, typ: TypeBigInt},
  1107. []byte("\x00\x00\x00\x00\x00\x01\x00\x00"),
  1108. uint16(0),
  1109. UnmarshalError("unmarshal int: value 65536 out of range for uint16"),
  1110. },
  1111. {
  1112. NativeType{proto: 2, typ: TypeBigInt},
  1113. []byte("\xff\xff\xff\xff\xff\xff\xff\xff"),
  1114. uint32(0),
  1115. UnmarshalError("unmarshal int: value -1 out of range for uint32"),
  1116. },
  1117. {
  1118. NativeType{proto: 2, typ: TypeBigInt},
  1119. []byte("\x00\x00\x00\x01\x00\x00\x00\x00"),
  1120. uint32(0),
  1121. UnmarshalError("unmarshal int: value 4294967296 out of range for uint32"),
  1122. },
  1123. {
  1124. NativeType{proto: 2, typ: TypeSmallInt},
  1125. []byte("\xff\xff"),
  1126. AliasUint8(0),
  1127. UnmarshalError("unmarshal int: value -1 out of range for gocql.AliasUint8"),
  1128. },
  1129. {
  1130. NativeType{proto: 2, typ: TypeSmallInt},
  1131. []byte("\x01\x00"),
  1132. AliasUint8(0),
  1133. UnmarshalError("unmarshal int: value 256 out of range for gocql.AliasUint8"),
  1134. },
  1135. {
  1136. NativeType{proto: 2, typ: TypeInt},
  1137. []byte("\xff\xff\xff\xff"),
  1138. AliasUint8(0),
  1139. UnmarshalError("unmarshal int: value -1 out of range for gocql.AliasUint8"),
  1140. },
  1141. {
  1142. NativeType{proto: 2, typ: TypeInt},
  1143. []byte("\x00\x00\x01\x00"),
  1144. AliasUint8(0),
  1145. UnmarshalError("unmarshal int: value 256 out of range for gocql.AliasUint8"),
  1146. },
  1147. {
  1148. NativeType{proto: 2, typ: TypeInt},
  1149. []byte("\xff\xff\xff\xff"),
  1150. AliasUint16(0),
  1151. UnmarshalError("unmarshal int: value -1 out of range for gocql.AliasUint16"),
  1152. },
  1153. {
  1154. NativeType{proto: 2, typ: TypeInt},
  1155. []byte("\x00\x01\x00\x00"),
  1156. AliasUint16(0),
  1157. UnmarshalError("unmarshal int: value 65536 out of range for gocql.AliasUint16"),
  1158. },
  1159. {
  1160. NativeType{proto: 2, typ: TypeBigInt},
  1161. []byte("\xff\xff\xff\xff\xff\xff\xff\xff"),
  1162. AliasUint8(0),
  1163. UnmarshalError("unmarshal int: value -1 out of range for gocql.AliasUint8"),
  1164. },
  1165. {
  1166. NativeType{proto: 2, typ: TypeBigInt},
  1167. []byte("\x00\x00\x00\x00\x00\x00\x01\x00"),
  1168. AliasUint8(0),
  1169. UnmarshalError("unmarshal int: value 256 out of range for gocql.AliasUint8"),
  1170. },
  1171. {
  1172. NativeType{proto: 2, typ: TypeBigInt},
  1173. []byte("\xff\xff\xff\xff\xff\xff\xff\xff"),
  1174. AliasUint8(0),
  1175. UnmarshalError("unmarshal int: value -1 out of range for gocql.AliasUint8"),
  1176. },
  1177. {
  1178. NativeType{proto: 2, typ: TypeBigInt},
  1179. []byte("\x00\x00\x00\x00\x00\x00\x01\x00"),
  1180. AliasUint8(0),
  1181. UnmarshalError("unmarshal int: value 256 out of range for gocql.AliasUint8"),
  1182. },
  1183. {
  1184. NativeType{proto: 2, typ: TypeBigInt},
  1185. []byte("\xff\xff\xff\xff\xff\xff\xff\xff"),
  1186. AliasUint16(0),
  1187. UnmarshalError("unmarshal int: value -1 out of range for gocql.AliasUint16"),
  1188. },
  1189. {
  1190. NativeType{proto: 2, typ: TypeBigInt},
  1191. []byte("\x00\x00\x00\x00\x00\x01\x00\x00"),
  1192. AliasUint16(0),
  1193. UnmarshalError("unmarshal int: value 65536 out of range for gocql.AliasUint16"),
  1194. },
  1195. {
  1196. NativeType{proto: 2, typ: TypeBigInt},
  1197. []byte("\xff\xff\xff\xff\xff\xff\xff\xff"),
  1198. AliasUint32(0),
  1199. UnmarshalError("unmarshal int: value -1 out of range for gocql.AliasUint32"),
  1200. },
  1201. {
  1202. NativeType{proto: 2, typ: TypeBigInt},
  1203. []byte("\x00\x00\x00\x01\x00\x00\x00\x00"),
  1204. AliasUint32(0),
  1205. UnmarshalError("unmarshal int: value 4294967296 out of range for gocql.AliasUint32"),
  1206. },
  1207. }
  1208. func decimalize(s string) *inf.Dec {
  1209. i, _ := new(inf.Dec).SetString(s)
  1210. return i
  1211. }
  1212. func bigintize(s string) *big.Int {
  1213. i, _ := new(big.Int).SetString(s, 10)
  1214. return i
  1215. }
  1216. func TestMarshal_Encode(t *testing.T) {
  1217. for i, test := range marshalTests {
  1218. if test.MarshalError == nil {
  1219. data, err := Marshal(test.Info, test.Value)
  1220. if err != nil {
  1221. t.Errorf("marshalTest[%d]: %v", i, err)
  1222. continue
  1223. }
  1224. if !bytes.Equal(data, test.Data) {
  1225. t.Errorf("marshalTest[%d]: expected %q, got %q (%#v)", i, test.Data, data, test.Value)
  1226. }
  1227. } else {
  1228. if _, err := Marshal(test.Info, test.Value); err != test.MarshalError {
  1229. t.Errorf("unmarshalTest[%d] (%v=>%t): %#v returned error %#v, want %#v.", i, test.Info, test.Value, test.Value, err, test.MarshalError)
  1230. }
  1231. }
  1232. }
  1233. }
  1234. func TestMarshal_Decode(t *testing.T) {
  1235. for i, test := range marshalTests {
  1236. if test.UnmarshalError == nil {
  1237. v := reflect.New(reflect.TypeOf(test.Value))
  1238. err := Unmarshal(test.Info, test.Data, v.Interface())
  1239. if err != nil {
  1240. t.Errorf("unmarshalTest[%d] (%v=>%T): %v", i, test.Info, test.Value, err)
  1241. continue
  1242. }
  1243. if !reflect.DeepEqual(v.Elem().Interface(), test.Value) {
  1244. t.Errorf("unmarshalTest[%d] (%v=>%T): expected %#v, got %#v.", i, test.Info, test.Value, test.Value, v.Elem().Interface())
  1245. }
  1246. } else {
  1247. if err := Unmarshal(test.Info, test.Data, test.Value); err != test.UnmarshalError {
  1248. t.Errorf("unmarshalTest[%d] (%v=>%T): %#v returned error %#v, want %#v.", i, test.Info, test.Value, test.Value, err, test.UnmarshalError)
  1249. }
  1250. }
  1251. }
  1252. for i, test := range unmarshalTests {
  1253. v := reflect.New(reflect.TypeOf(test.Value))
  1254. if test.UnmarshalError == nil {
  1255. err := Unmarshal(test.Info, test.Data, v.Interface())
  1256. if err != nil {
  1257. t.Errorf("unmarshalTest[%d] (%v=>%T): %v", i, test.Info, test.Value, err)
  1258. continue
  1259. }
  1260. if !reflect.DeepEqual(v.Elem().Interface(), test.Value) {
  1261. t.Errorf("unmarshalTest[%d] (%v=>%T): expected %#v, got %#v.", i, test.Info, test.Value, test.Value, v.Elem().Interface())
  1262. }
  1263. } else {
  1264. if err := Unmarshal(test.Info, test.Data, v.Interface()); err != test.UnmarshalError {
  1265. t.Errorf("unmarshalTest[%d] (%v=>%T): %#v returned error %#v, want %#v.", i, test.Info, test.Value, test.Value, err, test.UnmarshalError)
  1266. }
  1267. }
  1268. }
  1269. }
  1270. func TestMarshalVarint(t *testing.T) {
  1271. varintTests := []struct {
  1272. Value interface{}
  1273. Marshaled []byte
  1274. Unmarshaled *big.Int
  1275. }{
  1276. {
  1277. Value: int8(0),
  1278. Marshaled: []byte("\x00"),
  1279. Unmarshaled: big.NewInt(0),
  1280. },
  1281. {
  1282. Value: uint8(255),
  1283. Marshaled: []byte("\x00\xFF"),
  1284. Unmarshaled: big.NewInt(255),
  1285. },
  1286. {
  1287. Value: int8(-1),
  1288. Marshaled: []byte("\xFF"),
  1289. Unmarshaled: big.NewInt(-1),
  1290. },
  1291. {
  1292. Value: big.NewInt(math.MaxInt32),
  1293. Marshaled: []byte("\x7F\xFF\xFF\xFF"),
  1294. Unmarshaled: big.NewInt(math.MaxInt32),
  1295. },
  1296. {
  1297. Value: big.NewInt(int64(math.MaxInt32) + 1),
  1298. Marshaled: []byte("\x00\x80\x00\x00\x00"),
  1299. Unmarshaled: big.NewInt(int64(math.MaxInt32) + 1),
  1300. },
  1301. {
  1302. Value: big.NewInt(math.MinInt32),
  1303. Marshaled: []byte("\x80\x00\x00\x00"),
  1304. Unmarshaled: big.NewInt(math.MinInt32),
  1305. },
  1306. {
  1307. Value: big.NewInt(int64(math.MinInt32) - 1),
  1308. Marshaled: []byte("\xFF\x7F\xFF\xFF\xFF"),
  1309. Unmarshaled: big.NewInt(int64(math.MinInt32) - 1),
  1310. },
  1311. {
  1312. Value: math.MinInt64,
  1313. Marshaled: []byte("\x80\x00\x00\x00\x00\x00\x00\x00"),
  1314. Unmarshaled: big.NewInt(math.MinInt64),
  1315. },
  1316. {
  1317. Value: uint64(math.MaxInt64) + 1,
  1318. Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00"),
  1319. Unmarshaled: bigintize("9223372036854775808"),
  1320. },
  1321. {
  1322. Value: bigintize("2361183241434822606848"), // 2**71
  1323. Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00"),
  1324. Unmarshaled: bigintize("2361183241434822606848"),
  1325. },
  1326. {
  1327. Value: bigintize("-9223372036854775809"), // -2**63 - 1
  1328. Marshaled: []byte("\xFF\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF"),
  1329. Unmarshaled: bigintize("-9223372036854775809"),
  1330. },
  1331. }
  1332. for i, test := range varintTests {
  1333. data, err := Marshal(NativeType{proto: 2, typ: TypeVarint}, test.Value)
  1334. if err != nil {
  1335. t.Errorf("error marshaling varint: %v (test #%d)", err, i)
  1336. }
  1337. if !bytes.Equal(test.Marshaled, data) {
  1338. t.Errorf("marshaled varint mismatch: expected %v, got %v (test #%d)", test.Marshaled, data, i)
  1339. }
  1340. binder := new(big.Int)
  1341. err = Unmarshal(NativeType{proto: 2, typ: TypeVarint}, test.Marshaled, binder)
  1342. if err != nil {
  1343. t.Errorf("error unmarshaling varint: %v (test #%d)", err, i)
  1344. }
  1345. if test.Unmarshaled.Cmp(binder) != 0 {
  1346. t.Errorf("unmarshaled varint mismatch: expected %v, got %v (test #%d)", test.Unmarshaled, binder, i)
  1347. }
  1348. }
  1349. varintUint64Tests := []struct {
  1350. Value interface{}
  1351. Marshaled []byte
  1352. Unmarshaled uint64
  1353. }{
  1354. {
  1355. Value: int8(0),
  1356. Marshaled: []byte("\x00"),
  1357. Unmarshaled: 0,
  1358. },
  1359. {
  1360. Value: uint8(255),
  1361. Marshaled: []byte("\x00\xFF"),
  1362. Unmarshaled: 255,
  1363. },
  1364. {
  1365. Value: big.NewInt(math.MaxInt32),
  1366. Marshaled: []byte("\x7F\xFF\xFF\xFF"),
  1367. Unmarshaled: uint64(math.MaxInt32),
  1368. },
  1369. {
  1370. Value: big.NewInt(int64(math.MaxInt32) + 1),
  1371. Marshaled: []byte("\x00\x80\x00\x00\x00"),
  1372. Unmarshaled: uint64(int64(math.MaxInt32) + 1),
  1373. },
  1374. {
  1375. Value: uint64(math.MaxInt64) + 1,
  1376. Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00"),
  1377. Unmarshaled: 9223372036854775808,
  1378. },
  1379. {
  1380. Value: uint64(math.MaxUint64),
  1381. Marshaled: []byte("\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"),
  1382. Unmarshaled: uint64(math.MaxUint64),
  1383. },
  1384. }
  1385. for i, test := range varintUint64Tests {
  1386. data, err := Marshal(NativeType{proto: 2, typ: TypeVarint}, test.Value)
  1387. if err != nil {
  1388. t.Errorf("error marshaling varint: %v (test #%d)", err, i)
  1389. }
  1390. if !bytes.Equal(test.Marshaled, data) {
  1391. t.Errorf("marshaled varint mismatch: expected %v, got %v (test #%d)", test.Marshaled, data, i)
  1392. }
  1393. var binder uint64
  1394. err = Unmarshal(NativeType{proto: 2, typ: TypeVarint}, test.Marshaled, &binder)
  1395. if err != nil {
  1396. t.Errorf("error unmarshaling varint to uint64: %v (test #%d)", err, i)
  1397. }
  1398. if test.Unmarshaled != binder {
  1399. t.Errorf("unmarshaled varint mismatch: expected %v, got %v (test #%d)", test.Unmarshaled, binder, i)
  1400. }
  1401. }
  1402. }
  1403. func equalStringSlice(leftList, rightList []string) bool {
  1404. if len(leftList) != len(rightList) {
  1405. return false
  1406. }
  1407. for index := range leftList {
  1408. if rightList[index] != leftList[index] {
  1409. return false
  1410. }
  1411. }
  1412. return true
  1413. }
  1414. func TestMarshalList(t *testing.T) {
  1415. typeInfo := CollectionType{
  1416. NativeType: NativeType{proto: 2, typ: TypeList},
  1417. Elem: NativeType{proto: 2, typ: TypeVarchar},
  1418. }
  1419. sourceLists := [][]string{
  1420. {"valueA"},
  1421. {"valueA", "valueB"},
  1422. {"valueB"},
  1423. }
  1424. listDatas := [][]byte{}
  1425. for _, list := range sourceLists {
  1426. listData, marshalErr := Marshal(typeInfo, list)
  1427. if nil != marshalErr {
  1428. t.Errorf("Error marshal %+v of type %+v: %s", list, typeInfo, marshalErr)
  1429. }
  1430. listDatas = append(listDatas, listData)
  1431. }
  1432. outputLists := [][]string{}
  1433. var outputList []string
  1434. for _, listData := range listDatas {
  1435. if unmarshalErr := Unmarshal(typeInfo, listData, &outputList); nil != unmarshalErr {
  1436. t.Error(unmarshalErr)
  1437. }
  1438. outputLists = append(outputLists, outputList)
  1439. }
  1440. for index, sourceList := range sourceLists {
  1441. outputList := outputLists[index]
  1442. if !equalStringSlice(sourceList, outputList) {
  1443. t.Errorf("Lists %+v not equal to lists %+v, but should", sourceList, outputList)
  1444. }
  1445. }
  1446. }
  1447. type CustomString string
  1448. func (c CustomString) MarshalCQL(info TypeInfo) ([]byte, error) {
  1449. return []byte(strings.ToUpper(string(c))), nil
  1450. }
  1451. func (c *CustomString) UnmarshalCQL(info TypeInfo, data []byte) error {
  1452. *c = CustomString(strings.ToLower(string(data)))
  1453. return nil
  1454. }
  1455. type MyString string
  1456. type MyInt int
  1457. var typeLookupTest = []struct {
  1458. TypeName string
  1459. ExpectedType Type
  1460. }{
  1461. {"AsciiType", TypeAscii},
  1462. {"LongType", TypeBigInt},
  1463. {"BytesType", TypeBlob},
  1464. {"BooleanType", TypeBoolean},
  1465. {"CounterColumnType", TypeCounter},
  1466. {"DecimalType", TypeDecimal},
  1467. {"DoubleType", TypeDouble},
  1468. {"FloatType", TypeFloat},
  1469. {"Int32Type", TypeInt},
  1470. {"DateType", TypeTimestamp},
  1471. {"TimestampType", TypeTimestamp},
  1472. {"UUIDType", TypeUUID},
  1473. {"UTF8Type", TypeVarchar},
  1474. {"IntegerType", TypeVarint},
  1475. {"TimeUUIDType", TypeTimeUUID},
  1476. {"InetAddressType", TypeInet},
  1477. {"MapType", TypeMap},
  1478. {"ListType", TypeList},
  1479. {"SetType", TypeSet},
  1480. {"unknown", TypeCustom},
  1481. {"ShortType", TypeSmallInt},
  1482. {"ByteType", TypeTinyInt},
  1483. }
  1484. func testType(t *testing.T, cassType string, expectedType Type) {
  1485. if computedType := getApacheCassandraType(apacheCassandraTypePrefix + cassType); computedType != expectedType {
  1486. t.Errorf("Cassandra custom type lookup for %s failed. Expected %s, got %s.", cassType, expectedType.String(), computedType.String())
  1487. }
  1488. }
  1489. func TestLookupCassType(t *testing.T) {
  1490. for _, lookupTest := range typeLookupTest {
  1491. testType(t, lookupTest.TypeName, lookupTest.ExpectedType)
  1492. }
  1493. }
  1494. type MyPointerMarshaler struct{}
  1495. func (m *MyPointerMarshaler) MarshalCQL(_ TypeInfo) ([]byte, error) {
  1496. return []byte{42}, nil
  1497. }
  1498. func TestMarshalPointer(t *testing.T) {
  1499. m := &MyPointerMarshaler{}
  1500. typ := NativeType{proto: 2, typ: TypeInt}
  1501. data, err := Marshal(typ, m)
  1502. if err != nil {
  1503. t.Errorf("Pointer marshaling failed. Error: %s", err)
  1504. }
  1505. if len(data) != 1 || data[0] != 42 {
  1506. t.Errorf("Pointer marshaling failed. Expected %+v, got %+v", []byte{42}, data)
  1507. }
  1508. }
  1509. func TestMarshalTime(t *testing.T) {
  1510. durationS := "1h10m10s"
  1511. duration, _ := time.ParseDuration(durationS)
  1512. expectedData := encBigInt(duration.Nanoseconds())
  1513. var marshalTimeTests = []struct {
  1514. Info TypeInfo
  1515. Data []byte
  1516. Value interface{}
  1517. }{
  1518. {
  1519. NativeType{proto: 4, typ: TypeTime},
  1520. expectedData,
  1521. duration.Nanoseconds(),
  1522. },
  1523. {
  1524. NativeType{proto: 4, typ: TypeTime},
  1525. expectedData,
  1526. duration,
  1527. },
  1528. {
  1529. NativeType{proto: 4, typ: TypeTime},
  1530. expectedData,
  1531. &duration,
  1532. },
  1533. }
  1534. for i, test := range marshalTimeTests {
  1535. t.Log(i, test)
  1536. data, err := Marshal(test.Info, test.Value)
  1537. if err != nil {
  1538. t.Errorf("marshalTest[%d]: %v", i, err)
  1539. continue
  1540. }
  1541. if !bytes.Equal(data, test.Data) {
  1542. t.Errorf("marshalTest[%d]: expected %x (%v), got %x (%v) for time %s", i,
  1543. test.Data, decInt(test.Data), data, decInt(data), test.Value)
  1544. }
  1545. }
  1546. }
  1547. func TestMarshalTimestamp(t *testing.T) {
  1548. var marshalTimestampTests = []struct {
  1549. Info TypeInfo
  1550. Data []byte
  1551. Value interface{}
  1552. }{
  1553. {
  1554. NativeType{proto: 2, typ: TypeTimestamp},
  1555. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  1556. time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC),
  1557. },
  1558. {
  1559. NativeType{proto: 2, typ: TypeTimestamp},
  1560. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  1561. int64(1376387523000),
  1562. },
  1563. {
  1564. // 9223372036854 is the maximum time representable in ms since the epoch
  1565. // with int64 if using UnixNano to convert
  1566. NativeType{proto: 2, typ: TypeTimestamp},
  1567. []byte("\x00\x00\x08\x63\x7b\xd0\x5a\xf6"),
  1568. time.Date(2262, time.April, 11, 23, 47, 16, 854775807, time.UTC),
  1569. },
  1570. {
  1571. // One nanosecond after causes overflow when using UnixNano
  1572. // Instead it should resolve to the same time in ms
  1573. NativeType{proto: 2, typ: TypeTimestamp},
  1574. []byte("\x00\x00\x08\x63\x7b\xd0\x5a\xf6"),
  1575. time.Date(2262, time.April, 11, 23, 47, 16, 854775808, time.UTC),
  1576. },
  1577. {
  1578. // -9223372036855 is the minimum time representable in ms since the epoch
  1579. // with int64 if using UnixNano to convert
  1580. NativeType{proto: 2, typ: TypeTimestamp},
  1581. []byte("\xff\xff\xf7\x9c\x84\x2f\xa5\x09"),
  1582. time.Date(1677, time.September, 21, 00, 12, 43, 145224192, time.UTC),
  1583. },
  1584. {
  1585. // One nanosecond earlier causes overflow when using UnixNano
  1586. // it should resolve to the same time in ms
  1587. NativeType{proto: 2, typ: TypeTimestamp},
  1588. []byte("\xff\xff\xf7\x9c\x84\x2f\xa5\x09"),
  1589. time.Date(1677, time.September, 21, 00, 12, 43, 145224191, time.UTC),
  1590. },
  1591. {
  1592. // Store the zero time as a blank slice
  1593. NativeType{proto: 2, typ: TypeTimestamp},
  1594. []byte{},
  1595. time.Time{},
  1596. },
  1597. }
  1598. for i, test := range marshalTimestampTests {
  1599. t.Log(i, test)
  1600. data, err := Marshal(test.Info, test.Value)
  1601. if err != nil {
  1602. t.Errorf("marshalTest[%d]: %v", i, err)
  1603. continue
  1604. }
  1605. if !bytes.Equal(data, test.Data) {
  1606. t.Errorf("marshalTest[%d]: expected %x (%v), got %x (%v) for time %s", i,
  1607. test.Data, decBigInt(test.Data), data, decBigInt(data), test.Value)
  1608. }
  1609. }
  1610. }
  1611. func TestMarshalTuple(t *testing.T) {
  1612. info := TupleTypeInfo{
  1613. NativeType: NativeType{proto: 3, typ: TypeTuple},
  1614. Elems: []TypeInfo{
  1615. NativeType{proto: 3, typ: TypeVarchar},
  1616. NativeType{proto: 3, typ: TypeVarchar},
  1617. },
  1618. }
  1619. expectedData := []byte("\x00\x00\x00\x03foo\x00\x00\x00\x03bar")
  1620. value := []interface{}{"foo", "bar"}
  1621. data, err := Marshal(info, value)
  1622. if err != nil {
  1623. t.Errorf("marshalTest: %v", err)
  1624. return
  1625. }
  1626. if !bytes.Equal(data, expectedData) {
  1627. t.Errorf("marshalTest: expected %x (%v), got %x (%v)",
  1628. expectedData, decBigInt(expectedData), data, decBigInt(data))
  1629. return
  1630. }
  1631. var s1, s2 string
  1632. val := []interface{}{&s1, &s2}
  1633. err = Unmarshal(info, expectedData, val)
  1634. if err != nil {
  1635. t.Errorf("unmarshalTest: %v", err)
  1636. return
  1637. }
  1638. if s1 != "foo" || s2 != "bar" {
  1639. t.Errorf("unmarshalTest: expected [foo, bar], got [%s, %s]", s1, s2)
  1640. }
  1641. }
  1642. func TestMarshalNil(t *testing.T) {
  1643. types := []Type{
  1644. TypeAscii,
  1645. TypeBlob,
  1646. TypeBoolean,
  1647. TypeBigInt,
  1648. TypeCounter,
  1649. TypeDecimal,
  1650. TypeDouble,
  1651. TypeFloat,
  1652. TypeInt,
  1653. TypeTimestamp,
  1654. TypeUUID,
  1655. TypeVarchar,
  1656. TypeVarint,
  1657. TypeTimeUUID,
  1658. TypeInet,
  1659. }
  1660. for _, typ := range types {
  1661. data, err := Marshal(NativeType{proto: 3, typ: typ}, nil)
  1662. if err != nil {
  1663. t.Errorf("unable to marshal nil %v: %v\n", typ, err)
  1664. } else if data != nil {
  1665. t.Errorf("expected to get nil byte for nil %v got % X", typ, data)
  1666. }
  1667. }
  1668. }
  1669. func TestUnmarshalInetCopyBytes(t *testing.T) {
  1670. data := []byte{127, 0, 0, 1}
  1671. var ip net.IP
  1672. if err := unmarshalInet(NativeType{proto: 2, typ: TypeInet}, data, &ip); err != nil {
  1673. t.Fatal(err)
  1674. }
  1675. copy(data, []byte{0xFF, 0xFF, 0xFF, 0xFF})
  1676. ip2 := net.IP(data)
  1677. if !ip.Equal(net.IPv4(127, 0, 0, 1)) {
  1678. t.Fatalf("IP memory shared with data: ip=%v ip2=%v", ip, ip2)
  1679. }
  1680. }
  1681. func TestUnmarshalDate(t *testing.T) {
  1682. data := []uint8{0x80, 0x0, 0x43, 0x31}
  1683. var date time.Time
  1684. if err := unmarshalDate(NativeType{proto: 2, typ: TypeDate}, data, &date); err != nil {
  1685. t.Fatal(err)
  1686. }
  1687. expectedDate := "2017-02-04"
  1688. formattedDate := date.Format("2006-01-02")
  1689. if expectedDate != formattedDate {
  1690. t.Errorf("marshalTest: expected %v, got %v", expectedDate, formattedDate)
  1691. return
  1692. }
  1693. var stringDate string
  1694. if err2 := unmarshalDate(NativeType{proto: 2, typ: TypeDate}, data, &stringDate); err2 != nil {
  1695. t.Fatal(err2)
  1696. }
  1697. if expectedDate != stringDate {
  1698. t.Errorf("marshalTest: expected %v, got %v", expectedDate, formattedDate)
  1699. return
  1700. }
  1701. }
  1702. func TestMarshalDate(t *testing.T) {
  1703. now := time.Now().UTC()
  1704. timestamp := now.UnixNano() / int64(time.Millisecond)
  1705. expectedData := encInt(int32(timestamp/86400000 + int64(1<<31)))
  1706. var marshalDateTests = []struct {
  1707. Info TypeInfo
  1708. Data []byte
  1709. Value interface{}
  1710. }{
  1711. {
  1712. NativeType{proto: 4, typ: TypeDate},
  1713. expectedData,
  1714. timestamp,
  1715. },
  1716. {
  1717. NativeType{proto: 4, typ: TypeDate},
  1718. expectedData,
  1719. now,
  1720. },
  1721. {
  1722. NativeType{proto: 4, typ: TypeDate},
  1723. expectedData,
  1724. &now,
  1725. },
  1726. {
  1727. NativeType{proto: 4, typ: TypeDate},
  1728. expectedData,
  1729. now.Format("2006-01-02"),
  1730. },
  1731. }
  1732. for i, test := range marshalDateTests {
  1733. t.Log(i, test)
  1734. data, err := Marshal(test.Info, test.Value)
  1735. if err != nil {
  1736. t.Errorf("marshalTest[%d]: %v", i, err)
  1737. continue
  1738. }
  1739. if !bytes.Equal(data, test.Data) {
  1740. t.Errorf("marshalTest[%d]: expected %x (%v), got %x (%v) for time %s", i,
  1741. test.Data, decInt(test.Data), data, decInt(data), test.Value)
  1742. }
  1743. }
  1744. }
  1745. func BenchmarkUnmarshalVarchar(b *testing.B) {
  1746. b.ReportAllocs()
  1747. src := make([]byte, 1024)
  1748. dst := make([]byte, len(src))
  1749. b.ResetTimer()
  1750. for i := 0; i < b.N; i++ {
  1751. if err := unmarshalVarchar(NativeType{}, src, &dst); err != nil {
  1752. b.Fatal(err)
  1753. }
  1754. }
  1755. }
  1756. func TestMarshalDuration(t *testing.T) {
  1757. durationS := "1h10m10s"
  1758. duration, _ := time.ParseDuration(durationS)
  1759. expectedData := append([]byte{0, 0}, encVint(duration.Nanoseconds())...)
  1760. var marshalDurationTests = []struct {
  1761. Info TypeInfo
  1762. Data []byte
  1763. Value interface{}
  1764. }{
  1765. {
  1766. NativeType{proto: 5, typ: TypeDuration},
  1767. expectedData,
  1768. duration.Nanoseconds(),
  1769. },
  1770. {
  1771. NativeType{proto: 5, typ: TypeDuration},
  1772. expectedData,
  1773. duration,
  1774. },
  1775. {
  1776. NativeType{proto: 5, typ: TypeDuration},
  1777. expectedData,
  1778. durationS,
  1779. },
  1780. {
  1781. NativeType{proto: 5, typ: TypeDuration},
  1782. expectedData,
  1783. &duration,
  1784. },
  1785. }
  1786. for i, test := range marshalDurationTests {
  1787. t.Log(i, test)
  1788. data, err := Marshal(test.Info, test.Value)
  1789. if err != nil {
  1790. t.Errorf("marshalTest[%d]: %v", i, err)
  1791. continue
  1792. }
  1793. if !bytes.Equal(data, test.Data) {
  1794. t.Errorf("marshalTest[%d]: expected %x (%v), got %x (%v) for time %s", i,
  1795. test.Data, decInt(test.Data), data, decInt(data), test.Value)
  1796. }
  1797. }
  1798. }
  1799. func TestReadCollectionSize(t *testing.T) {
  1800. listV2 := CollectionType{
  1801. NativeType: NativeType{proto: 2, typ: TypeList},
  1802. Elem: NativeType{proto: 2, typ: TypeVarchar},
  1803. }
  1804. listV3 := CollectionType{
  1805. NativeType: NativeType{proto: 3, typ: TypeList},
  1806. Elem: NativeType{proto: 3, typ: TypeVarchar},
  1807. }
  1808. tests := []struct {
  1809. name string
  1810. info CollectionType
  1811. data []byte
  1812. isError bool
  1813. expectedSize int
  1814. }{
  1815. {
  1816. name: "short read 0 proto 2",
  1817. info: listV2,
  1818. data: []byte{},
  1819. isError: true,
  1820. },
  1821. {
  1822. name: "short read 1 proto 2",
  1823. info: listV2,
  1824. data: []byte{0x01},
  1825. isError: true,
  1826. },
  1827. {
  1828. name: "good read proto 2",
  1829. info: listV2,
  1830. data: []byte{0x01, 0x38},
  1831. expectedSize: 0x0138,
  1832. },
  1833. {
  1834. name: "short read 0 proto 3",
  1835. info: listV3,
  1836. data: []byte{},
  1837. isError: true,
  1838. },
  1839. {
  1840. name: "short read 1 proto 3",
  1841. info: listV3,
  1842. data: []byte{0x01},
  1843. isError: true,
  1844. },
  1845. {
  1846. name: "short read 2 proto 3",
  1847. info: listV3,
  1848. data: []byte{0x01, 0x38},
  1849. isError: true,
  1850. },
  1851. {
  1852. name: "short read 3 proto 3",
  1853. info: listV3,
  1854. data: []byte{0x01, 0x38, 0x42},
  1855. isError: true,
  1856. },
  1857. {
  1858. name: "good read proto 3",
  1859. info: listV3,
  1860. data: []byte{0x01, 0x38, 0x42, 0x22},
  1861. expectedSize: 0x01384222,
  1862. },
  1863. }
  1864. for _, test := range tests {
  1865. t.Run(test.name, func(t *testing.T) {
  1866. size, _, err := readCollectionSize(test.info, test.data)
  1867. if test.isError {
  1868. if err == nil {
  1869. t.Fatal("Expected error, but it was nil")
  1870. }
  1871. } else {
  1872. if err != nil {
  1873. t.Fatalf("Expected no error, got %v", err)
  1874. }
  1875. if size != test.expectedSize {
  1876. t.Fatalf("Expected size of %d, but got %d", test.expectedSize, size)
  1877. }
  1878. }
  1879. })
  1880. }
  1881. }