-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutator_insert.go
156 lines (124 loc) · 4.47 KB
/
mutator_insert.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
// archivo: mutator_inser.go
package mutator
import (
"encoding/json"
"fmt"
"math/rand"
)
func (m *Mutator) insertChars(runes []rune, mutationType MutationType) []rune {
var counterInsert int
for counterInsert < mutationType.Length {
pos := rand.Intn(len(runes) + 1)
runes = insertChar(runes, pos, func() rune { return m.randomChar(mutationType) })
counterInsert++
}
return runes
}
func (m *Mutator) insertBoundaryValues(runes []rune) []rune {
boundaryValues := []string{
"0", "1", "-1", "32767", "-32768", "2147483647", "-2147483648", "9223372036854775807", "-9223372036854775808",
}
// Select a random boundary value
randomBoundary := boundaryValues[rand.Intn(len(boundaryValues))]
// Convert the boundary value to a slice of runes
boundaryRunes := []rune(randomBoundary)
// Select a random position in the slice of runes
pos := rand.Intn(len(runes) + 1)
// Insert the boundary value at the random position
newRunes := append(runes[:pos], append(boundaryRunes, runes[pos:]...)...)
return newRunes
}
func (m *Mutator) insertHTML(runes []rune, mutationType MutationType) []rune {
// Define some sample HTML tags to insert
htmlTags := []string{
"<div>", "</div>", "<span>", "</span>", "<a href='#'>", "</a>",
"<b>", "</b>", "<i>", "</i>", "<p>", "</p>",
"<br>", "<img src='#'/>", "<h1>", "</h1>",
}
for i := 0; i < mutationType.Length; i++ {
// Generate a random HTML tag from the list
randomHTML := htmlTags[rand.Intn(len(htmlTags))]
// Convert the HTML string to a slice of runes
htmlRunes := []rune(randomHTML)
// Select a random position in the slice of runes
pos := rand.Intn(len(runes) + 1)
// Insert the HTML string at the random position
runes = append(runes[:pos], append(htmlRunes, runes[pos:]...)...)
}
return runes
}
func (m *Mutator) insertFakeWord(runes []rune, mutationType MutationType) []rune {
vowels := []rune("aeiou")
consonants := []rune("bcdfghjklmnpqrstvwxyz")
// Generar la palabra ficticia
var fakeWord []rune
for i := 0; i < mutationType.Length; i++ {
if i%2 == 0 {
// Insertar una consonante
randomConsonant := consonants[rand.Intn(len(consonants))]
fakeWord = append(fakeWord, randomConsonant)
} else {
// Insertar una vocal
randomVowel := vowels[rand.Intn(len(vowels))]
fakeWord = append(fakeWord, randomVowel)
}
}
// Seleccionar una posición aleatoria en el slice runes
pos := rand.Intn(len(runes) + 1)
// Insertar la palabra generada en la posición seleccionada
newRunes := append(runes[:pos], append(fakeWord, runes[pos:]...)...)
return newRunes
}
func (m *Mutator) insertRandomJSON(runes []rune, maxDepth int) []rune {
// Generate a random JSON object
jsonData := m.generateRandomJSON(maxDepth, 0)
// Convert the generated JSON object to a string
jsonBytes, err := json.Marshal(jsonData)
if err != nil {
fmt.Println("Error marshalling JSON:", err)
return runes
}
jsonString := string(jsonBytes)
// Convert the JSON string to a slice of runes
jsonRunes := []rune(jsonString)
// Select a random position in the slice of runes
pos := rand.Intn(len(runes) + 1)
// Insert the JSON string at the random position
newRunes := append(runes[:pos], append(jsonRunes, runes[pos:]...)...)
return newRunes
}
// Método de Mutator para insertar una palabra aleatoria en runes
func (m *Mutator) insertRandomWord(runes []rune, mutationType MutationType) []rune {
var words []string
var err error
// Cargar el diccionario si está especificado
if mutationType.Dictionary != "" {
words, err = loadDictionary(mutationType.Dictionary)
if err != nil {
fmt.Println("Error cargando el diccionario:", err)
return runes
}
}
// Si no hay palabras en la lista de palabras ni en el diccionario, retornar los runes originales
if len(mutationType.Wordslist) == 0 && len(words) == 0 {
return runes
}
// Si hay palabras en la lista de palabras, usarlas
if len(mutationType.Wordslist) > 0 {
words = mutationType.Wordslist
}
// Insertar palabras aleatorias en la posición seleccionada
var counterInsert int
for counterInsert < mutationType.Length {
// Seleccionar una palabra aleatoria
randomWord := getRandomWord(words)
// randomRunes := []rune(randomWord + " ") // or just delete?
randomRunes := []rune(randomWord) // or just delete?
// Seleccionar una posición aleatoria para insertar la palabra
pos := rand.Intn(len(runes) + 1)
// Insertar la palabra en la posición seleccionada
runes = append(runes[:pos], append(randomRunes, runes[pos:]...)...)
counterInsert++
}
return runes
}