marshal_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. "speter.net/go/exp/math/dec/inf"
  13. )
  14. var marshalTests = []struct {
  15. Info *TypeInfo
  16. Data []byte
  17. Value interface{}
  18. }{
  19. {
  20. &TypeInfo{Type: TypeVarchar},
  21. []byte("hello world"),
  22. []byte("hello world"),
  23. },
  24. {
  25. &TypeInfo{Type: TypeVarchar},
  26. []byte("hello world"),
  27. "hello world",
  28. },
  29. {
  30. &TypeInfo{Type: TypeVarchar},
  31. []byte(nil),
  32. []byte(nil),
  33. },
  34. {
  35. &TypeInfo{Type: TypeVarchar},
  36. []byte("hello world"),
  37. MyString("hello world"),
  38. },
  39. {
  40. &TypeInfo{Type: TypeVarchar},
  41. []byte("HELLO WORLD"),
  42. CustomString("hello world"),
  43. },
  44. {
  45. &TypeInfo{Type: TypeBlob},
  46. []byte("hello\x00"),
  47. []byte("hello\x00"),
  48. },
  49. {
  50. &TypeInfo{Type: TypeBlob},
  51. []byte(nil),
  52. []byte(nil),
  53. },
  54. {
  55. &TypeInfo{Type: TypeTimeUUID},
  56. []byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
  57. func() UUID {
  58. x, _ := UUIDFromBytes([]byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0})
  59. return x
  60. }(),
  61. },
  62. {
  63. &TypeInfo{Type: TypeInt},
  64. []byte("\x00\x00\x00\x00"),
  65. 0,
  66. },
  67. {
  68. &TypeInfo{Type: TypeInt},
  69. []byte("\x01\x02\x03\x04"),
  70. int(16909060),
  71. },
  72. {
  73. &TypeInfo{Type: TypeInt},
  74. []byte("\x80\x00\x00\x00"),
  75. int32(math.MinInt32),
  76. },
  77. {
  78. &TypeInfo{Type: TypeInt},
  79. []byte("\x7f\xff\xff\xff"),
  80. int32(math.MaxInt32),
  81. },
  82. {
  83. &TypeInfo{Type: TypeBigInt},
  84. []byte("\x00\x00\x00\x00\x00\x00\x00\x00"),
  85. 0,
  86. },
  87. {
  88. &TypeInfo{Type: TypeBigInt},
  89. []byte("\x01\x02\x03\x04\x05\x06\x07\x08"),
  90. 72623859790382856,
  91. },
  92. {
  93. &TypeInfo{Type: TypeBigInt},
  94. []byte("\x80\x00\x00\x00\x00\x00\x00\x00"),
  95. int64(math.MinInt64),
  96. },
  97. {
  98. &TypeInfo{Type: TypeBigInt},
  99. []byte("\x7f\xff\xff\xff\xff\xff\xff\xff"),
  100. int64(math.MaxInt64),
  101. },
  102. {
  103. &TypeInfo{Type: TypeBoolean},
  104. []byte("\x00"),
  105. false,
  106. },
  107. {
  108. &TypeInfo{Type: TypeBoolean},
  109. []byte("\x01"),
  110. true,
  111. },
  112. {
  113. &TypeInfo{Type: TypeFloat},
  114. []byte("\x40\x49\x0f\xdb"),
  115. float32(3.14159265),
  116. },
  117. {
  118. &TypeInfo{Type: TypeDouble},
  119. []byte("\x40\x09\x21\xfb\x53\xc8\xd4\xf1"),
  120. float64(3.14159265),
  121. },
  122. {
  123. &TypeInfo{Type: TypeDecimal},
  124. []byte("\x00\x00\x00\x00\x00"),
  125. inf.NewDec(0, 0),
  126. },
  127. {
  128. &TypeInfo{Type: TypeDecimal},
  129. []byte("\x00\x00\x00\x00\x64"),
  130. inf.NewDec(100, 0),
  131. },
  132. {
  133. &TypeInfo{Type: TypeDecimal},
  134. []byte("\x00\x00\x00\x02\x19"),
  135. decimalize("0.25"),
  136. },
  137. {
  138. &TypeInfo{Type: TypeDecimal},
  139. []byte("\x00\x00\x00\x13\xD5\a;\x20\x14\xA2\x91"),
  140. decimalize("-0.0012095473475870063"), // From the iconara/cql-rb test suite
  141. },
  142. {
  143. &TypeInfo{Type: TypeDecimal},
  144. []byte("\x00\x00\x00\x13*\xF8\xC4\xDF\xEB]o"),
  145. decimalize("0.0012095473475870063"), // From the iconara/cql-rb test suite
  146. },
  147. {
  148. &TypeInfo{Type: TypeDecimal},
  149. []byte("\x00\x00\x00\x12\xF2\xD8\x02\xB6R\x7F\x99\xEE\x98#\x99\xA9V"),
  150. decimalize("-1042342234234.123423435647768234"), // From the iconara/cql-rb test suite
  151. },
  152. {
  153. &TypeInfo{Type: TypeDecimal},
  154. []byte("\x00\x00\x00\r\nJ\x04\"^\x91\x04\x8a\xb1\x18\xfe"),
  155. decimalize("1243878957943.1234124191998"), // From the datastax/python-driver test suite
  156. },
  157. {
  158. &TypeInfo{Type: TypeDecimal},
  159. []byte("\x00\x00\x00\x06\xe5\xde]\x98Y"),
  160. decimalize("-112233.441191"), // From the datastax/python-driver test suite
  161. },
  162. {
  163. &TypeInfo{Type: TypeDecimal},
  164. []byte("\x00\x00\x00\x14\x00\xfa\xce"),
  165. decimalize("0.00000000000000064206"), // From the datastax/python-driver test suite
  166. },
  167. {
  168. &TypeInfo{Type: TypeDecimal},
  169. []byte("\x00\x00\x00\x14\xff\x052"),
  170. decimalize("-0.00000000000000064206"), // From the datastax/python-driver test suite
  171. },
  172. {
  173. &TypeInfo{Type: TypeDecimal},
  174. []byte("\xff\xff\xff\x9c\x00\xfa\xce"),
  175. inf.NewDec(64206, -100), // From the datastax/python-driver test suite
  176. },
  177. {
  178. &TypeInfo{Type: TypeTimestamp},
  179. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  180. time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC),
  181. },
  182. {
  183. &TypeInfo{Type: TypeTimestamp},
  184. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  185. int64(1376387523000),
  186. },
  187. {
  188. &TypeInfo{Type: TypeList, Elem: &TypeInfo{Type: TypeInt}},
  189. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  190. []int{1, 2},
  191. },
  192. {
  193. &TypeInfo{Type: TypeList, Elem: &TypeInfo{Type: TypeInt}},
  194. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  195. [2]int{1, 2},
  196. },
  197. {
  198. &TypeInfo{Type: TypeSet, Elem: &TypeInfo{Type: TypeInt}},
  199. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  200. []int{1, 2},
  201. },
  202. {
  203. &TypeInfo{Type: TypeSet, Elem: &TypeInfo{Type: TypeInt}},
  204. []byte(nil),
  205. []int(nil),
  206. },
  207. {
  208. &TypeInfo{Type: TypeMap,
  209. Key: &TypeInfo{Type: TypeVarchar},
  210. Elem: &TypeInfo{Type: TypeInt},
  211. },
  212. []byte("\x00\x01\x00\x03foo\x00\x04\x00\x00\x00\x01"),
  213. map[string]int{"foo": 1},
  214. },
  215. {
  216. &TypeInfo{Type: TypeMap,
  217. Key: &TypeInfo{Type: TypeVarchar},
  218. Elem: &TypeInfo{Type: TypeInt},
  219. },
  220. []byte(nil),
  221. map[string]int(nil),
  222. },
  223. {
  224. &TypeInfo{Type: TypeList, Elem: &TypeInfo{Type: TypeVarchar}},
  225. bytes.Join([][]byte{
  226. []byte("\x00\x01\xFF\xFF"),
  227. bytes.Repeat([]byte("X"), 65535)}, []byte("")),
  228. []string{strings.Repeat("X", 65535)},
  229. },
  230. {
  231. &TypeInfo{Type: TypeMap,
  232. Key: &TypeInfo{Type: TypeVarchar},
  233. Elem: &TypeInfo{Type: TypeVarchar},
  234. },
  235. bytes.Join([][]byte{
  236. []byte("\x00\x01\xFF\xFF"),
  237. bytes.Repeat([]byte("X"), 65535),
  238. []byte("\xFF\xFF"),
  239. bytes.Repeat([]byte("Y"), 65535)}, []byte("")),
  240. map[string]string{
  241. strings.Repeat("X", 65535): strings.Repeat("Y", 65535),
  242. },
  243. },
  244. {
  245. &TypeInfo{Type: TypeVarint},
  246. []byte("\x00"),
  247. 0,
  248. },
  249. {
  250. &TypeInfo{Type: TypeVarint},
  251. []byte("\x37\xE2\x3C\xEC"),
  252. int32(937573612),
  253. },
  254. {
  255. &TypeInfo{Type: TypeVarint},
  256. []byte("\x37\xE2\x3C\xEC"),
  257. big.NewInt(937573612),
  258. },
  259. {
  260. &TypeInfo{Type: TypeVarint},
  261. []byte("\x03\x9EV \x15\f\x03\x9DK\x18\xCDI\\$?\a["),
  262. bigintize("1231312312331283012830129382342342412123"), // From the iconara/cql-rb test suite
  263. },
  264. {
  265. &TypeInfo{Type: TypeVarint},
  266. []byte("\xC9v\x8D:\x86"),
  267. big.NewInt(-234234234234), // From the iconara/cql-rb test suite
  268. },
  269. {
  270. &TypeInfo{Type: TypeVarint},
  271. []byte("f\x1e\xfd\xf2\xe3\xb1\x9f|\x04_\x15"),
  272. bigintize("123456789123456789123456789"), // From the datastax/python-driver test suite
  273. },
  274. {
  275. &TypeInfo{Type: TypeInet},
  276. []byte("\x7F\x00\x00\x01"),
  277. net.ParseIP("127.0.0.1").To4(),
  278. },
  279. {
  280. &TypeInfo{Type: TypeInet},
  281. []byte("\xFF\xFF\xFF\xFF"),
  282. net.ParseIP("255.255.255.255").To4(),
  283. },
  284. {
  285. &TypeInfo{Type: TypeInet},
  286. []byte("\x7F\x00\x00\x01"),
  287. "127.0.0.1",
  288. },
  289. {
  290. &TypeInfo{Type: TypeInet},
  291. []byte("\xFF\xFF\xFF\xFF"),
  292. "255.255.255.255",
  293. },
  294. {
  295. &TypeInfo{Type: TypeInet},
  296. []byte("\x21\xDA\x00\xd3\x00\x00\x2f\x3b\x02\xaa\x00\xff\xfe\x28\x9c\x5a"),
  297. "21da:d3:0:2f3b:2aa:ff:fe28:9c5a",
  298. },
  299. {
  300. &TypeInfo{Type: TypeInet},
  301. []byte("\xfe\x80\x00\x00\x00\x00\x00\x00\x02\x02\xb3\xff\xfe\x1e\x83\x29"),
  302. "fe80::202:b3ff:fe1e:8329",
  303. },
  304. {
  305. &TypeInfo{Type: TypeInet},
  306. []byte("\x21\xDA\x00\xd3\x00\x00\x2f\x3b\x02\xaa\x00\xff\xfe\x28\x9c\x5a"),
  307. net.ParseIP("21da:d3:0:2f3b:2aa:ff:fe28:9c5a"),
  308. },
  309. {
  310. &TypeInfo{Type: TypeInet},
  311. []byte("\xfe\x80\x00\x00\x00\x00\x00\x00\x02\x02\xb3\xff\xfe\x1e\x83\x29"),
  312. net.ParseIP("fe80::202:b3ff:fe1e:8329"),
  313. },
  314. }
  315. func decimalize(s string) *inf.Dec {
  316. i, _ := new(inf.Dec).SetString(s)
  317. return i
  318. }
  319. func bigintize(s string) *big.Int {
  320. i, _ := new(big.Int).SetString(s, 10)
  321. return i
  322. }
  323. func TestMarshal(t *testing.T) {
  324. for i, test := range marshalTests {
  325. data, err := Marshal(test.Info, test.Value)
  326. if err != nil {
  327. t.Errorf("marshalTest[%d]: %v", i, err)
  328. continue
  329. }
  330. if !bytes.Equal(data, test.Data) {
  331. t.Errorf("marshalTest[%d]: expected %q, got %q.", i, test.Data, data)
  332. }
  333. }
  334. }
  335. func TestMarshalNil(t *testing.T) {
  336. data, err := Marshal(&TypeInfo{Type: TypeInt}, nil)
  337. if err != nil {
  338. t.Errorf("failed to marshal nil with err: %v", err)
  339. }
  340. if data != nil {
  341. t.Errorf("expected nil, got %v", data)
  342. }
  343. }
  344. func TestUnmarshal(t *testing.T) {
  345. for i, test := range marshalTests {
  346. v := reflect.New(reflect.TypeOf(test.Value))
  347. err := Unmarshal(test.Info, test.Data, v.Interface())
  348. if err != nil {
  349. t.Errorf("marshalTest[%d]: %v", i, err)
  350. continue
  351. }
  352. if !reflect.DeepEqual(v.Elem().Interface(), test.Value) {
  353. t.Errorf("marshalTest[%d]: expected %#v, got %#v.", i, test.Value, v.Elem().Interface())
  354. }
  355. }
  356. }
  357. func TestMarshalVarint(t *testing.T) {
  358. varintTests := []struct {
  359. Value interface{}
  360. Marshaled []byte
  361. Unmarshaled *big.Int
  362. }{
  363. {
  364. Value: int8(0),
  365. Marshaled: []byte("\x00"),
  366. Unmarshaled: big.NewInt(0),
  367. },
  368. {
  369. Value: uint8(255),
  370. Marshaled: []byte("\x00\xFF"),
  371. Unmarshaled: big.NewInt(255),
  372. },
  373. {
  374. Value: int8(-1),
  375. Marshaled: []byte("\xFF"),
  376. Unmarshaled: big.NewInt(-1),
  377. },
  378. {
  379. Value: big.NewInt(math.MaxInt32),
  380. Marshaled: []byte("\x7F\xFF\xFF\xFF"),
  381. Unmarshaled: big.NewInt(math.MaxInt32),
  382. },
  383. {
  384. Value: big.NewInt(int64(math.MaxInt32) + 1),
  385. Marshaled: []byte("\x00\x80\x00\x00\x00"),
  386. Unmarshaled: big.NewInt(int64(math.MaxInt32) + 1),
  387. },
  388. {
  389. Value: big.NewInt(math.MinInt32),
  390. Marshaled: []byte("\x80\x00\x00\x00"),
  391. Unmarshaled: big.NewInt(math.MinInt32),
  392. },
  393. {
  394. Value: big.NewInt(int64(math.MinInt32) - 1),
  395. Marshaled: []byte("\xFF\x7F\xFF\xFF\xFF"),
  396. Unmarshaled: big.NewInt(int64(math.MinInt32) - 1),
  397. },
  398. {
  399. Value: math.MinInt64,
  400. Marshaled: []byte("\x80\x00\x00\x00\x00\x00\x00\x00"),
  401. Unmarshaled: big.NewInt(math.MinInt64),
  402. },
  403. {
  404. Value: uint64(math.MaxInt64) + 1,
  405. Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00"),
  406. Unmarshaled: bigintize("9223372036854775808"),
  407. },
  408. {
  409. Value: bigintize("2361183241434822606848"), // 2**71
  410. Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00"),
  411. Unmarshaled: bigintize("2361183241434822606848"),
  412. },
  413. {
  414. Value: bigintize("-9223372036854775809"), // -2**63 - 1
  415. Marshaled: []byte("\xFF\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF"),
  416. Unmarshaled: bigintize("-9223372036854775809"),
  417. },
  418. }
  419. for i, test := range varintTests {
  420. data, err := Marshal(&TypeInfo{Type: TypeVarint}, test.Value)
  421. if err != nil {
  422. t.Errorf("error marshaling varint: %v (test #%d)", err, i)
  423. }
  424. if !bytes.Equal(test.Marshaled, data) {
  425. t.Errorf("marshaled varint mismatch: expected %v, got %v (test #%d)", test.Marshaled, data, i)
  426. }
  427. binder := new(big.Int)
  428. err = Unmarshal(&TypeInfo{Type: TypeVarint}, test.Marshaled, binder)
  429. if err != nil {
  430. t.Errorf("error unmarshaling varint: %v (test #%d)", err, i)
  431. }
  432. if test.Unmarshaled.Cmp(binder) != 0 {
  433. t.Errorf("unmarshaled varint mismatch: expected %v, got %v (test #%d)", test.Unmarshaled, binder, i)
  434. }
  435. }
  436. }
  437. type CustomString string
  438. func (c CustomString) MarshalCQL(info *TypeInfo) ([]byte, error) {
  439. return []byte(strings.ToUpper(string(c))), nil
  440. }
  441. func (c *CustomString) UnmarshalCQL(info *TypeInfo, data []byte) error {
  442. *c = CustomString(strings.ToLower(string(data)))
  443. return nil
  444. }
  445. type MyString string
  446. type MyInt int
  447. var typeLookupTest = []struct {
  448. TypeName string
  449. ExpectedType Type
  450. }{
  451. {"AsciiType", TypeAscii},
  452. {"LongType", TypeBigInt},
  453. {"BytesType", TypeBlob},
  454. {"BooleanType", TypeBoolean},
  455. {"CounterColumnType", TypeCounter},
  456. {"DecimalType", TypeDecimal},
  457. {"DoubleType", TypeDouble},
  458. {"FloatType", TypeFloat},
  459. {"Int32Type", TypeInt},
  460. {"DateType", TypeTimestamp},
  461. {"UUIDType", TypeUUID},
  462. {"UTF8Type", TypeVarchar},
  463. {"IntegerType", TypeVarint},
  464. {"TimeUUIDType", TypeTimeUUID},
  465. {"InetAddressType", TypeInet},
  466. {"MapType", TypeMap},
  467. {"ListType", TypeInet},
  468. {"SetType", TypeInet},
  469. {"unknown", TypeCustom},
  470. }
  471. func testType(t *testing.T, cassType string, expectedType Type) {
  472. if computedType := getApacheCassandraType(apacheCassandraTypePrefix + cassType); computedType != expectedType {
  473. t.Errorf("Cassandra custom type lookup for %s failed. Expected %s, got %s.", cassType, expectedType.String(), computedType.String())
  474. }
  475. }
  476. func TestLookupCassType(t *testing.T) {
  477. for _, lookupTest := range typeLookupTest {
  478. testType(t, lookupTest.TypeName, lookupTest.ExpectedType)
  479. }
  480. }