This commit is contained in:
@@ -1,9 +1,11 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/binary"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"net"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
"github.com/iancoleman/orderedmap"
|
"github.com/iancoleman/orderedmap"
|
||||||
@@ -223,6 +225,21 @@ func findRandom(key interface{}, data interface{}) interface{} {
|
|||||||
newInt := rand.Int()
|
newInt := rand.Int()
|
||||||
fmt.Printf("Generated random number '%d' to insert into JSON body\n", newInt)
|
fmt.Printf("Generated random number '%d' to insert into JSON body\n", newInt)
|
||||||
return newInt
|
return newInt
|
||||||
|
} else if isMac, isMacOk := randomObj["isMac"].(bool); isMacOk && isMac {
|
||||||
|
newMac := generateRandomMAC()
|
||||||
|
fmt.Printf("Generated random mac address '%s' to insert into JSON body for key '%s'\n", newMac, key.(string))
|
||||||
|
return newMac
|
||||||
|
} else if isIP, isIpOk := randomObj["isIP"].(bool); isIpOk && isIP {
|
||||||
|
if network, networkOk := randomObj["network"].(string); networkOk {
|
||||||
|
newIp, err := generateRandomIP(network)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Unable to generate random IP address: '%s'\n", err)
|
||||||
|
return ""
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Generated random IP '%s' address in network '%s' to insert into JSON body for key '%s'\n", newIp, network, key.(string))
|
||||||
|
return newIp
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -248,6 +265,51 @@ func generateRandomString(length int) string {
|
|||||||
return string(result)
|
return string(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func generateRandomMAC() string {
|
||||||
|
mac := make([]byte, 6)
|
||||||
|
for i := range mac {
|
||||||
|
mac[i] = byte(rand.Intn(256))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the locally administered and unicast bits
|
||||||
|
mac[0] = (mac[0] | 2) & 0xfe
|
||||||
|
|
||||||
|
return fmt.Sprintf("%02x:%02x:%02x:%02x:%02x:%02x",
|
||||||
|
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5])
|
||||||
|
}
|
||||||
|
|
||||||
|
// generateRandomIP generates a random IP address within the given subnet in CIDR notation.
|
||||||
|
func generateRandomIP(cidr string) (string, error) {
|
||||||
|
ip, ipnet, err := net.ParseCIDR(cidr)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("invalid CIDR: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert IP to uint32
|
||||||
|
ip4 := ip.To4()
|
||||||
|
if ip4 == nil {
|
||||||
|
return "", fmt.Errorf("not an IPv4 subnet")
|
||||||
|
}
|
||||||
|
ipInt := binary.BigEndian.Uint32(ip4)
|
||||||
|
mask := binary.BigEndian.Uint32(ipnet.Mask)
|
||||||
|
|
||||||
|
// Calculate network and broadcast addresses
|
||||||
|
network := ipInt & mask
|
||||||
|
broadcast := network | ^mask
|
||||||
|
|
||||||
|
if broadcast-network <= 1 {
|
||||||
|
return "", fmt.Errorf("subnet too small to allocate address")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate a random IP between network+1 and broadcast-1 (excluding network and broadcast)
|
||||||
|
randomIP := network + uint32(rand.Intn(int(broadcast-network-1))) + 1
|
||||||
|
|
||||||
|
// Convert back to net.IP
|
||||||
|
ipBytes := make([]byte, 4)
|
||||||
|
binary.BigEndian.PutUint32(ipBytes, randomIP)
|
||||||
|
return net.IP(ipBytes).String(), nil
|
||||||
|
}
|
||||||
|
|
||||||
func ReadHeaderTestCases(input orderedmap.OrderedMap, result *HeaderTests) {
|
func ReadHeaderTestCases(input orderedmap.OrderedMap, result *HeaderTests) {
|
||||||
result.Contains = make(map[string]string)
|
result.Contains = make(map[string]string)
|
||||||
result.Equals = make(map[string]string)
|
result.Equals = make(map[string]string)
|
||||||
|
23
run_tests.go
23
run_tests.go
@@ -196,6 +196,29 @@ func RunTest(testCase *TestCase) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for k, v := range captureData.Header.Data {
|
||||||
|
fmt.Printf("Searching header response for match on capture string '%s'\n", k)
|
||||||
|
|
||||||
|
results := findKey(k, testCase.ResultBodyMap)
|
||||||
|
//fmt.Printf("Results : '%v'\n", results)
|
||||||
|
|
||||||
|
if len(results) > 0 {
|
||||||
|
//fmt.Printf("Found %d results but only storing the first one\n", len(results))
|
||||||
|
|
||||||
|
// Get the type of the first element
|
||||||
|
valueType := reflect.TypeOf(results[0])
|
||||||
|
|
||||||
|
// Check if the type is not string
|
||||||
|
if valueType.Kind() != reflect.String {
|
||||||
|
// Convert the value to string
|
||||||
|
results[0] = fmt.Sprintf("%v", results[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Storing capture '%s' = '%s'\n", k, results[0].(string))
|
||||||
|
captureValues.Data[v] = results[0].(string)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Capture anything needed
|
// Capture anything needed
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user