forked from Josh-Murray/fuzzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSVGenerator.go
391 lines (337 loc) · 7.74 KB
/
CSVGenerator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package main
import (
"encoding/csv"
"fmt"
"io"
"os"
"strings"
)
type mCSVHolder struct {
lines [][]string
rows, columns int
description []string
}
/*
* Create a new CSVHolder initialised with the initial description
*
*/
func newCSV(initialDescription string) mCSVHolder {
s := mCSVHolder{}
s.description = append(s.description, initialDescription)
return s
}
/*
* read the CSV specified by file into the CSVHolder
*/
func (s *mCSVHolder) read(file string) {
csvFile, _ := os.Open(file)
reader := csv.NewReader(csvFile)
defer csvFile.Close()
for {
line, err := reader.Read()
if err == io.EOF {
break
} else if err != nil {
panic(err)
}
s.lines = append(s.lines, line)
s.columns = len(line)
}
s.rows = len(s.lines)
operation := fmt.Sprintf("Read in CSV from: %s with %d rows, %d cols \n", file, s.rows, s.columns)
s.addToDesc(operation)
}
/*
* Add a change to the description
*/
func (s *mCSVHolder) addToDesc(change string) {
s.description = append(s.description, change)
}
/*
* Display the current CSV in STDOUT
*/
func (s *mCSVHolder) display() {
fmt.Printf("Description: %s\n", s.description)
fmt.Printf("Rows: %d, Columns: %d\n", s.rows, s.columns)
fmt.Print("Column \t\t|")
for i := 0; i < s.columns; i++ {
fmt.Printf("\t%d\t", i)
}
fmt.Println("\n\t\t", strings.Repeat("-", s.columns*15))
for i, line := range s.lines {
fmt.Printf("row %2d: \t|", i)
for _, c := range line {
//fmt.Printf(" %d> %4s <", j, c)
fmt.Printf("\t%2s\t", c)
}
fmt.Println("")
}
}
/*
* Delete row u
*/
func (s *mCSVHolder) deleteRow(u int) {
if u < s.rows {
s.lines = append(s.lines[:u], s.lines[u+1:]...)
s.addToDesc(fmt.Sprintf("Removed row %d from CSV\n", u))
s.rows--
} else {
s.addToDesc(fmt.Sprintf("No row %d to remove from CSV\n", u))
}
}
/*
* Delete column u
*/
func (s *mCSVHolder) deleteCol(u int) {
if u < s.columns {
for i, line := range s.lines {
s.lines[i] = append(line[:u], line[u+1:]...)
}
s.addToDesc(fmt.Sprintf("Removed column %d from CSV\n", u))
s.columns--
} else {
s.addToDesc(fmt.Sprintf("No Column %d to delete from CSV\n", u))
}
}
/*
* Insert a row rowToAdd into location l
*/
func (s *mCSVHolder) addRow(l int, rowToAdd []string) {
var operation string
if l < s.rows {
if len(rowToAdd) == s.columns {
end := s.lines[l:]
beginning := append(s.lines[:l], rowToAdd)
s.lines = append(beginning, end...)
operation = fmt.Sprintln("added csv row ", l, " content ", rowToAdd)
s.rows++
} else {
operation += "Number of columns in the row being added does not match"
}
} else {
operation += fmt.Sprintf("%d is not a valid location to insert a row\n", l)
}
s.addToDesc(operation)
}
/*
* Insert a column colToAdd into location l
*/
func (s *mCSVHolder) addColumn(l int, colToAdd []string) {
var operation string
if l > s.columns {
l = s.columns
}
if len(colToAdd) == s.rows {
for i := 0; i < s.rows; i++ {
end := s.lines[i][l:]
beginning := append(s.lines[i][:l], colToAdd[i])
s.lines[i] = append(beginning, end...)
}
operation = fmt.Sprintln("added csv col ", l, " content ", colToAdd)
s.columns++
}
s.addToDesc(operation)
}
/*
* Return a []string representing column c in s
*/
func (s *mCSVHolder) getCol(c int) []string {
col := []string{}
for _, row := range s.lines {
col = append(col, row[c])
}
return col
}
/*
* Return a deep copy of row r
* TODO: Investigate why this isnt working
*/
func (s *mCSVHolder) getrRow(r int) []string {
cpy := []string{}
for _, str := range s.lines[r] {
cpy = append(cpy, fmt.Sprintf("%s", str))
}
return cpy
}
/*
* Insert a duplicate of column c
*/
func (s *mCSVHolder) copyCol(c int) {
if c < s.columns {
col := s.getCol(c)
s.addColumn(c, col)
s.addToDesc(fmt.Sprintln("coppied csv column", c))
}
}
/*
* Insert a duplicate of row r
*/
func (s *mCSVHolder) copyRow(r int) {
if r < s.rows {
c := s.getrRow(r)
s.addRow(r, c)
s.addToDesc(fmt.Sprintln("coppied csv row", r))
}
}
/*
* convert s into a string representing the content of the CSV
*/
func (s *mCSVHolder) flatten() string {
o := ""
for _, row := range s.lines {
for j, col := range row {
o += fmt.Sprint(col)
if j < s.columns-1 {
o += fmt.Sprint(",")
}
}
o += fmt.Sprint("\n")
}
return fmt.Sprint(o)
}
/*
* parse a string into the CSVHolder
*/
func (s *mCSVHolder) expand(in string) {
// TODO: can probably refactor this to reduce code duplication with readCSV
reader := csv.NewReader(strings.NewReader(in))
s.lines = [][]string{}
for {
line, err := reader.Read()
if err == io.EOF {
break
} else if err != nil {
panic(err)
}
s.lines = append(s.lines, line)
s.columns = len(line)
}
s.rows = len(s.lines)
}
/*
* Flatten then reexpand inplace
*/
func (s *mCSVHolder) reExpand() {
s.expand(s.flatten())
}
/*
* Insert a blank row into position r
*/
func (s *mCSVHolder) addBlankRow(r int) {
blankRow := make([]string, s.columns)
s.addRow(r, blankRow)
}
/*
* Insert a blank column into position c
*/
func (s *mCSVHolder) addBlankCol(c int) {
blankCol := make([]string, s.rows)
s.addColumn(c, blankCol)
}
/*
* Deep copy (hopefully) s into a testcase
*/
func (s *mCSVHolder) generateTestCase() TestCase {
ts := TestCase{}
ts.changes = append(ts.changes, s.description...)
content := s.flatten()
ts.input = append(ts.input, content...)
return ts
}
/*
* Teses making lots of copies of a row. If copies is true then copy the last row
* otherwise add blank rows
*/
func spamRows(copies bool, tests chan<- TestCase, s mCSVHolder) {
//TODO: abstract magic numbers
for i := 1; i < 4096; i++ {
// copy the last row
if copies {
s.copyRow(s.rows - 1)
} else {
s.addBlankRow(s.rows - 1)
}
//only send every nth case to test
if i < 3 || i%16 == 0 {
tests <- s.generateTestCase()
}
}
}
/*
* Teses making lots of copies of a column. If copies is true then copy the last row
* otherwise add blank rows
*/
func spamCols(copies bool, tests chan<- TestCase, s mCSVHolder) {
//TODO: abstract magic numbers
for i := 1; i < 4096; i++ {
// copy the last row
if copies {
s.copyCol(s.columns - 1)
} else {
s.addBlankCol(s.columns - 1)
}
//only send every nth case to test
if i < 3 || i%16 == 0 {
tests <- s.generateTestCase()
}
}
}
/*
* Blank out the csv
*/
func blankCSV(tests chan<- TestCase, s mCSVHolder) {
blankRow := make([]string, s.columns)
t := newCSV("Blank csv with original number of rows and columns")
for i := 0; i < s.rows; i++ {
t.addRow(1, blankRow)
}
tests<- t.generateTestCase()
}
/*
* Take a CSV file as base and permute variations into the test channel
*/
func generateCSVs(tests chan<- TestCase, file string) {
input := newCSV("Initial input")
input.read(file)
//put the original input file into the test
tests <- input.generateTestCase()
//test blanking the content of the csv
blankCSV(tests, input)
//spam adding blank rows
input = newCSV("Spamming blank CSV rows")
input.read(file)
spamRows(false, tests, input)
//spam adding copies of the last row
input = newCSV("Spamming copies CSV rows")
input.read(file)
spamRows(true, tests, input)
//spam adding blank columns
input = newCSV("Spamming blank CSV cols")
input.read(file)
spamCols(false, tests, input)
//spam adding copies of the last column
input = newCSV("Spamming copies CSV cols")
input.read(file)
spamCols(true, tests, input)
//TODO: probably close the channel here?
}
/*
* visual test of the CSV generator
*/
func testCSVGenerator() {
input := newCSV("Initial input")
input.read("valid.csv")
input.copyRow(0)
input.copyRow(0)
input.copyRow(0)
input.copyRow(0)
input.copyRow(0)
input.reExpand()
input.addBlankRow(0)
input.copyRow(0)
input.copyRow(0)
input.reExpand()
input.lines[0][0] = "s"
input.lines[3][0] = "s"
input.display()
}