|
| 1 | +//go:build fuzz |
| 2 | +// +build fuzz |
| 3 | + |
| 4 | +package gocql |
| 5 | + |
| 6 | +import ( |
| 7 | + "bytes" |
| 8 | + "fmt" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +// FuzzMarshalFloat64Ptr aimed to repeatedly test float64 marshaling with generated inputs based on seed corpus. |
| 13 | +func FuzzMarshalFloat64Ptr(f *testing.F) { |
| 14 | + f.Add(float64(7500), float64(7500.00)) |
| 15 | + |
| 16 | + f.Fuzz(func(t *testing.T, num, numWithPoints float64) { |
| 17 | + session := createSession(t) |
| 18 | + |
| 19 | + defer session.Close() |
| 20 | + |
| 21 | + if err := createTable(session, "CREATE TABLE IF NOT EXISTS gocql_test.float_test (id double, test double, primary key (id))"); err != nil { |
| 22 | + t.Fatal("create table:", err) |
| 23 | + } |
| 24 | + |
| 25 | + if err := session.Query(`TRUNCATE TABLE gocql_test.float_test`).Exec(); err != nil { |
| 26 | + t.Fatal("truncate table:", err) |
| 27 | + } |
| 28 | + |
| 29 | + if err := session.Query(`INSERT INTO float_test (id,test) VALUES (?,?)`, numWithPoints, &num).Exec(); err != nil { |
| 30 | + t.Fatal("insert float64:", err) |
| 31 | + } |
| 32 | + }) |
| 33 | +} |
| 34 | + |
| 35 | +func FuzzTracing(f *testing.F) { |
| 36 | + f.Add(42) |
| 37 | + |
| 38 | + f.Fuzz(func(t *testing.T, id int) { |
| 39 | + session := createSession(t) |
| 40 | + defer session.Close() |
| 41 | + |
| 42 | + if err := createTable(session, `CREATE TABLE gocql_test.trace (id int primary key)`); err != nil { |
| 43 | + t.Fatal("create:", err) |
| 44 | + } |
| 45 | + |
| 46 | + buf := &bytes.Buffer{} |
| 47 | + trace := &traceWriter{session: session, w: buf} |
| 48 | + if err := session.Query(`INSERT INTO trace (id) VALUES (?)`, id).Trace(trace).Exec(); err != nil { |
| 49 | + t.Fatal("insert:", err) |
| 50 | + } else if buf.Len() == 0 { |
| 51 | + t.Fatal("insert: failed to obtain any tracing") |
| 52 | + } |
| 53 | + trace.mu.Lock() |
| 54 | + buf.Reset() |
| 55 | + trace.mu.Unlock() |
| 56 | + |
| 57 | + var value int |
| 58 | + if err := session.Query(`SELECT id FROM trace WHERE id = ?`, id).Trace(trace).Scan(&value); err != nil { |
| 59 | + t.Fatal("select:", err) |
| 60 | + } else if value != id { |
| 61 | + t.Fatalf("value: expected %d, got %d", id, value) |
| 62 | + } else if buf.Len() == 0 { |
| 63 | + t.Fatal("select: failed to obtain any tracing") |
| 64 | + } |
| 65 | + |
| 66 | + // also works from session tracer |
| 67 | + session.SetTrace(trace) |
| 68 | + trace.mu.Lock() |
| 69 | + buf.Reset() |
| 70 | + trace.mu.Unlock() |
| 71 | + if err := session.Query(`SELECT id FROM trace WHERE id = ?`, id).Scan(&value); err != nil { |
| 72 | + t.Fatal("select:", err) |
| 73 | + } |
| 74 | + if buf.Len() == 0 { |
| 75 | + t.Fatal("select: failed to obtain any tracing") |
| 76 | + } |
| 77 | + }) |
| 78 | +} |
| 79 | +func FuzzDurationType(f *testing.F) { |
| 80 | + f.Add(int32(1), int32(500), int32(0x7FFFFFFF), int64(0x7FFFFFFFFFFFFFFF)) |
| 81 | + |
| 82 | + f.Fuzz(func(t *testing.T, id, month, day int32, nanoseconds int64) { |
| 83 | + |
| 84 | + session := createSession(t) |
| 85 | + defer session.Close() |
| 86 | + |
| 87 | + fmt.Println("\nLOL") |
| 88 | + fmt.Printf("\nid:%v.\nmonth:%v.\nday:%v.\nnanoseconds:%v\n", id, month, day, nanoseconds) |
| 89 | + if session.cfg.ProtoVersion < 4 { |
| 90 | + t.Skip("Duration type is not supported. Please use protocol version >= 4 and cassandra version >= 3.11") |
| 91 | + } |
| 92 | + |
| 93 | + if err := createTable(session, `CREATE TABLE gocql_test.duration_table ( |
| 94 | + k int primary key, v duration |
| 95 | + )`); err != nil { |
| 96 | + t.Fatal("create:", err) |
| 97 | + } |
| 98 | + |
| 99 | + duration := Duration{ |
| 100 | + Months: month, |
| 101 | + Days: day, |
| 102 | + Nanoseconds: nanoseconds, |
| 103 | + } |
| 104 | + |
| 105 | + if err := session.Query(`INSERT INTO gocql_test.duration_table (k, v) VALUES (?, ?)`, id, duration).Exec(); err != nil { |
| 106 | + t.Fatal(err) |
| 107 | + } |
| 108 | + |
| 109 | + var scanedID int |
| 110 | + var selectedDuration Duration |
| 111 | + if err := session.Query(`SELECT k, v FROM gocql_test.duration_table`).Scan(&scanedID, &selectedDuration); err != nil { |
| 112 | + t.Fatal(err) |
| 113 | + } |
| 114 | + if selectedDuration.Months != duration.Months || selectedDuration.Days != duration.Days || selectedDuration.Nanoseconds != duration.Nanoseconds { |
| 115 | + t.Fatalf("Unexpeted value returned, expected=%v, received=%v", duration, selectedDuration) |
| 116 | + } |
| 117 | + }) |
| 118 | +} |
0 commit comments