Skip to content

Commit

Permalink
Merge pull request #83 from unpoller/mocking-responses
Browse files Browse the repository at this point in the history
[testing] Mocking rig to fuzz and get generated data based on our struct types
  • Loading branch information
platinummonkey authored Jul 20, 2023
2 parents f675ae7 + 43cdaef commit 881e9f6
Show file tree
Hide file tree
Showing 22 changed files with 946 additions and 288 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/vendor
*.swp
.vscode/
26 changes: 13 additions & 13 deletions alarms.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,44 @@ import (

type Alarm struct {
Archived FlexBool `json:"archived"`
DestPort int `json:"dest_port"`
SrcPort int `json:"src_port"`
DestPort int `json:"dest_port" fake:"{port}"`
SrcPort int `json:"src_port" fake:"{port}"`
FlowID int64 `json:"flow_id"`
InnerAlertGID int64 `json:"inner_alert_gid"`
InnerAlertRev int64 `json:"inner_alert_rev"`
InnerAlertSeverity int64 `json:"inner_alert_severity"`
InnerAlertSignatureID int64 `json:"inner_alert_signature_id"`
Time int64 `json:"time"`
Timestamp int64 `json:"timestamp"`
Time int64 `json:"time" fake:"{timestamp}"`
Timestamp int64 `json:"timestamp" fake:"{timestamp}"`
Datetime time.Time `json:"datetime"`
HandledTime time.Time `json:"handled_time,omitempty"`
AppProto string `json:"app_proto,omitempty"`
Catname string `json:"catname"`
DestIP string `json:"dest_ip"`
DstMAC string `json:"dst_mac"`
DestIP string `json:"dest_ip" fake:"{ipv4address}"`
DstMAC string `json:"dst_mac" fake:"{macaddress}"`
DstIPASN string `json:"dstipASN,omitempty"`
DstIPCountry string `json:"dstipCountry,omitempty"`
EventType string `json:"event_type"`
HandledAdminID string `json:"handled_admin_id,omitempty"`
Host string `json:"host"`
ID string `json:"_id"`
InIface string `json:"in_iface"`
ID string `json:"_id" fake:"{uuid}"`
InIface string `json:"in_iface" fake:"{randomstring:[eth0,eth1,lan1,wan1,wan2]}"`
InnerAlertAction string `json:"inner_alert_action"`
InnerAlertCategory string `json:"inner_alert_category"`
InnerAlertSignature string `json:"inner_alert_signature"`
Key string `json:"key"`
Msg string `json:"msg"`
Msg string `json:"msg" fake:"{sentence:5}"`
Proto string `json:"proto"`
SiteID string `json:"site_id"`
SiteID string `json:"site_id" fake:"{uuid}"`
SiteName string `json:"-"`
SourceName string `json:"-"`
SrcIP string `json:"src_ip"`
SrcIP string `json:"src_ip" fake:"{ipv4address}"`
SrcIPASN string `json:"srcipASN,omitempty"`
SrcIPCountry string `json:"srcipCountry,omitempty"`
SrcMAC string `json:"src_mac"`
SrcMAC string `json:"src_mac" fake:"{macaddress}"`
Subsystem string `json:"subsystem"`
UniqueAlertID string `json:"unique_alertid"`
USGIP string `json:"usgip"`
USGIP string `json:"usgip" fake:"{ipv4address}"`
USGIPASN string `json:"usgipASN"`
USGIPCountry string `json:"usgipCountry"`
TxID FlexInt `json:"tx_id,omitempty"`
Expand Down
6 changes: 3 additions & 3 deletions anomalies.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
// anomaly is the type UniFi returns, but not the type this library returns.
type anomaly struct {
Anomaly string `json:"anomaly"`
MAC string `json:"mac"`
Timestamps []int64 `json:"timestamps"`
MAC string `json:"mac" fake:"{macaddress}"`
Timestamps []int64 `json:"timestamps" fake:"{timestamps}"`
}

// Anomaly is the reformatted data type that this library returns.
Expand All @@ -21,7 +21,7 @@ type Anomaly struct {
SourceName string
SiteName string
Anomaly string
DeviceMAC string
DeviceMAC string `fake:"{macaddress}"`
// DeviceName string // we do not have this....
}

Expand Down
28 changes: 14 additions & 14 deletions clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ func (u *Unifi) GetClientsDPI(sites []*Site) ([]*DPITable, error) {
type Client struct {
SourceName string `json:"-"`
Anomalies FlexInt `json:"anomalies,omitempty"`
ApMac string `json:"ap_mac"`
ApMac string `json:"ap_mac" fake:"{macaddress}"`
ApName string `json:"-"`
AssocTime FlexInt `json:"assoc_time"`
Blocked bool `json:"blocked,omitempty"`
Bssid string `json:"bssid"`
Bssid string `json:"bssid" fake:"{macaddress}"`
BytesR FlexInt `json:"bytes-r"`
Ccq FlexInt `json:"ccq"`
Channel FlexInt `json:"channel"`
Expand All @@ -81,14 +81,14 @@ type Client struct {
DevVendor FlexInt `json:"dev_vendor,omitempty"`
DhcpendTime FlexInt `json:"dhcpend_time,omitempty"`
Satisfaction FlexInt `json:"satisfaction,omitempty"`
Essid string `json:"essid"`
Essid string `json:"essid" fake:"{macaddress}"`
FirstSeen FlexInt `json:"first_seen"`
FixedIP string `json:"fixed_ip"`
GwMac string `json:"gw_mac"`
FixedIP string `json:"fixed_ip" fake:"{ipv4address}"`
GwMac string `json:"gw_mac" fake:"{macaddress}"`
GwName string `json:"-"`
Hostname string `json:"hostname"`
ID string `json:"_id"`
IP string `json:"ip"`
ID string `json:"_id" fake:"{uuid}"`
IP string `json:"ip" fake:"{ipv4address}"`
IdleTime FlexInt `json:"idle_time"`
Is11R FlexBool `json:"is_11r"`
IsGuest FlexBool `json:"is_guest"`
Expand All @@ -101,12 +101,12 @@ type Client struct {
LastSeenByUGW FlexInt `json:"_last_seen_by_ugw"`
LastSeenByUSW FlexInt `json:"_last_seen_by_usw"`
LatestAssocTime FlexInt `json:"latest_assoc_time"`
Mac string `json:"mac"`
Mac string `json:"mac" fake:"{macaddress}"`
Name string `json:"name"`
Network string `json:"network"`
NetworkID string `json:"network_id"`
NetworkID string `json:"network_id" fake:"{uuid}"`
Noise FlexInt `json:"noise"`
Note string `json:"note"`
Note string `json:"note" fake:"{sentence 20}"`
Noted FlexBool `json:"noted"`
OsClass FlexInt `json:"os_class"`
OsName FlexInt `json:"os_name"`
Expand All @@ -124,10 +124,10 @@ type Client struct {
RxPackets FlexInt `json:"rx_packets"`
RxRate FlexInt `json:"rx_rate"`
Signal FlexInt `json:"signal"`
SiteID string `json:"site_id"`
SiteID string `json:"site_id" fake:"{uuid}"`
SiteName string `json:"-"`
SwDepth int `json:"sw_depth"`
SwMac string `json:"sw_mac"`
SwMac string `json:"sw_mac" fake:"{macaddress}"`
SwName string `json:"-"`
SwPort FlexInt `json:"sw_port"`
TxBytes FlexInt `json:"tx_bytes"`
Expand All @@ -141,8 +141,8 @@ type Client struct {
UptimeByUGW FlexInt `json:"_uptime_by_ugw"`
UptimeByUSW FlexInt `json:"_uptime_by_usw"`
UseFixedIP FlexBool `json:"use_fixedip"`
UserGroupID string `json:"usergroup_id"`
UserID string `json:"user_id"`
UserGroupID string `json:"usergroup_id" fake:"{uuid}"`
UserID string `json:"user_id" fake:"{uuid}"`
Vlan FlexInt `json:"vlan"`
WifiTxAttempts FlexInt `json:"wifi_tx_attempts"`
WiredRxBytes FlexInt `json:"wired-rx_bytes"`
Expand Down
10 changes: 5 additions & 5 deletions devmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ const (

// devMgrCmd is the type marshalled and sent to APIDevMgrPath.
type devMgrCmd struct {
Cmd string `json:"cmd"` // Required.
Mac string `json:"mac"` // Device MAC (required for most, but not all).
URL string `json:"url,omitempty"` // External Upgrade only.
Inform string `json:"inform_url,omitempty"` // Migration only.
Port int `json:"port_idx,omitempty"` // Power Cycle only.
Cmd string `json:"cmd"` // Required.
Mac string `json:"mac" fake:"{macaddress}"` // Device MAC (required for most, but not all).
URL string `json:"url,omitempty" fake:"{url}"` // External Upgrade only.
Inform string `json:"inform_url,omitempty" fake:"{url}"` // Migration only.
Port int `json:"port_idx,omitempty"` // Power Cycle only.
}

// devMgrCommandReply is for commands with a return value.
Expand Down
10 changes: 5 additions & 5 deletions dpi.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ type DPITable struct {
SourceName string `json:"-"`
SiteName string `json:"-"`
Name string `json:"-"`
MAC string `json:"mac"`
ByCat []DPIData `json:"by_cat"`
ByApp []DPIData `json:"by_app"`
MAC string `json:"mac" fake:"{macaddress}"`
ByCat []DPIData `json:"by_cat" fakesize:"5"`
ByApp []DPIData `json:"by_app" fakesize:"5"`
LastUpdated FlexInt `json:"last_updated"`
}

Expand All @@ -21,13 +21,13 @@ type DPIData struct {
TxBytes FlexInt `json:"tx_bytes"`
RxPackets FlexInt `json:"rx_packets"`
TxPackets FlexInt `json:"tx_packets"`
Clients []*DPIClient `json:"clients,omitempty"`
Clients []*DPIClient `json:"clients,omitempty" fakesize:"5"`
KnownClients FlexInt `json:"known_clients,omitempty"`
}

// DPIClient data is sometimes included in ByApp output.
type DPIClient struct {
Mac string `json:"mac"`
Mac string `json:"mac" fake:"{macaddress}"`
RxBytes FlexInt `json:"rx_bytes"`
TxBytes FlexInt `json:"tx_bytes"`
RxPackets FlexInt `json:"rx_packets"`
Expand Down
52 changes: 26 additions & 26 deletions events.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ func (u *Unifi) GetSiteEvents(site *Site, hours time.Duration) ([]*Event, error)
// API Path: /api/s/default/stat/event.
type Event struct {
IsAdmin FlexBool `json:"is_admin"`
DestPort int `json:"dest_port"`
SrcPort int `json:"src_port"`
DestPort int `json:"dest_port" fake:"{port}"`
SrcPort int `json:"src_port" fake:"{port}"`
Bytes FlexInt `json:"bytes"`
Duration FlexInt `json:"duration"`
FlowID FlexInt `json:"flow_id"`
Expand All @@ -85,8 +85,8 @@ type Event struct {
Channel FlexInt `json:"channel"`
ChannelFrom FlexInt `json:"channel_from"`
ChannelTo FlexInt `json:"channel_to"`
Time int64 `json:"time"`
Timestamp int64 `json:"timestamp"`
Time int64 `json:"time" fake:"{timestamp}"`
Timestamp int64 `json:"timestamp" fake:"{timestamp}"`
Datetime time.Time `json:"datetime"`
Admin string `json:"admin"`
Ap string `json:"ap"`
Expand All @@ -95,43 +95,43 @@ type Event struct {
ApTo string `json:"ap_to"`
AppProto string `json:"app_proto"`
Catname string `json:"catname"`
DestIP string `json:"dest_ip"`
DstMAC string `json:"dst_mac"`
DestIP string `json:"dest_ip" fake:"{ipv4address}"`
DstMAC string `json:"dst_mac" fake:"{macaddress}"`
EventType string `json:"event_type"`
Guest string `json:"guest"`
Gw string `json:"gw"`
GwName string `json:"gw_name"`
Host string `json:"host"`
Hostname string `json:"hostname"`
ID string `json:"_id"`
IP string `json:"ip"`
ID string `json:"_id" fake:"{uuid}"`
IP string `json:"ip" fake:"{ipv4address}"`
InIface string `json:"in_iface"`
InnerAlertAction string `json:"inner_alert_action"`
InnerAlertCategory string `json:"inner_alert_category"`
InnerAlertSignature string `json:"inner_alert_signature"`
Key string `json:"key"`
Msg string `json:"msg"`
Key string `json:"key" fake:"{uuid}"`
Msg string `json:"msg" fake:"{sentence:20}"`
Network string `json:"network"`
Proto string `json:"proto"`
Radio string `json:"radio"`
RadioFrom string `json:"radio_from"`
RadioTo string `json:"radio_to"`
SiteID string `json:"site_id"`
SiteID string `json:"site_id" fake:"{}"`
SiteName string `json:"-"`
SourceName string `json:"-"`
SrcIP string `json:"src_ip"`
SrcMAC string `json:"src_mac"`
SrcIPASN string `json:"srcipASN"`
SrcIPCountry string `json:"srcipCountry"`
SSID string `json:"ssid"`
SrcIP string `json:"src_ip" fake:"{ipv4address}"`
SrcMAC string `json:"src_mac" fake:"{macaddress}"`
SrcIPASN string `json:"srcipASN" fake:"{address}"`
SrcIPCountry string `json:"srcipCountry" fake:"{country}"`
SSID string `json:"ssid" fake:"{macaddress}"`
Subsystem string `json:"subsystem"`
Sw string `json:"sw"`
SwName string `json:"sw_name"`
UniqueAlertID string `json:"unique_alertid"`
User string `json:"user"`
USGIP string `json:"usgip"`
USGIPASN string `json:"usgipASN"`
USGIPCountry string `json:"usgipCountry"`
USGIP string `json:"usgip" fake:"{ipv4address}"`
USGIPASN string `json:"usgipASN" fake:"{address}"`
USGIPCountry string `json:"usgipCountry" fake:"{country}"`
DestIPGeo IPGeo `json:"dstipGeo"`
SourceIPGeo IPGeo `json:"srcipGeo"`
USGIPGeo IPGeo `json:"usgipGeo"`
Expand All @@ -140,14 +140,14 @@ type Event struct {
// IPGeo is part of the UniFi Event data. Each event may have up to three of these.
// One for source, one for dest and one for the USG location.
type IPGeo struct {
Asn int64 `json:"asn"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
City string `json:"city"`
Asn int64 `json:"asn" fake:"{address}"`
Latitude float64 `json:"latitude" fake:"{latitude}"`
Longitude float64 `json:"longitude" fake:"{longitude}"`
City string `json:"city" fake:"{city}"`
ContinentCode string `json:"continent_code"`
CountryCode string `json:"country_code"`
CountryName string `json:"country_name"`
Organization string `json:"organization"`
CountryCode string `json:"country_code" fake:"{countryabr}"`
CountryName string `json:"country_name" fake:"{country}"`
Organization string `json:"organization" fake:"{company}"`
}

// Events satisfied the sort.Interface.
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/unpoller/unifi
go 1.19

require (
github.com/brianvoe/gofakeit/v6 v6.23.0
github.com/stretchr/testify v1.8.4
golang.org/x/net v0.12.0
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/brianvoe/gofakeit/v6 v6.23.0 h1:pgVhyWpYq4e0GEVCh2gdZnS/nBX+8SnyTBliHg5xjks=
github.com/brianvoe/gofakeit/v6 v6.23.0/go.mod h1:Ow6qC71xtwm79anlwKRlWZW6zVq9D2XHE4QSSMP/rU8=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
44 changes: 22 additions & 22 deletions ids.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,44 @@ import (
// IDS holds an Intrusion Prevention System Event.
type IDS struct {
Archived FlexBool `json:"archived"`
DestPort int `json:"dest_port,omitempty"`
SrcPort int `json:"src_port,omitempty"`
FlowID int64 `json:"flow_id"`
DestPort int `json:"dest_port,omitempty" fake:"{port}"`
SrcPort int `json:"src_port,omitempty" fake:"{port}"`
FlowID int64 `json:"flow_id" fake:"{uuid}"`
InnerAlertRev int64 `json:"inner_alert_rev"`
InnerAlertSeverity int64 `json:"inner_alert_severity"`
InnerAlertGID int64 `json:"inner_alert_gid"`
InnerAlertSignatureID int64 `json:"inner_alert_signature_id"`
Time int64 `json:"time"`
Timestamp int64 `json:"timestamp"`
Time int64 `json:"time" fake:"{timestamp}"`
Timestamp int64 `json:"timestamp" fake:"{timestamp}"`
Datetime time.Time `json:"datetime"`
AppProto string `json:"app_proto,omitempty"`
Catname string `json:"catname"`
DestIP string `json:"dest_ip"`
DstMAC string `json:"dst_mac"`
DstIPASN string `json:"dstipASN"`
DstIPCountry string `json:"dstipCountry"`
DestIP string `json:"dest_ip" fake:"{ipv4address}"`
DstMAC string `json:"dst_mac" fake:"{macaddress}"`
DstIPASN string `json:"dstipASN" fake:"{address}"`
DstIPCountry string `json:"dstipCountry" fake:"{country}"`
EventType string `json:"event_type"`
Host string `json:"host"`
ID string `json:"_id"`
ID string `json:"_id" fake:"{uuid}"`
InIface string `json:"in_iface"`
InnerAlertAction string `json:"inner_alert_action"`
InnerAlertCategory string `json:"inner_alert_category"`
InnerAlertSignature string `json:"inner_alert_signature"`
Key string `json:"key"`
Msg string `json:"msg"`
Key string `json:"key" fake:"{uuid}"`
Msg string `json:"msg" fake:"{sentence:20}"`
Proto string `json:"proto"`
SiteID string `json:"site_id"`
SiteID string `json:"site_id" fake:"{uuid}"`
SiteName string `json:"-"`
SourceName string `json:"-"`
SrcIP string `json:"src_ip"`
SrcIPASN string `json:"srcipASN"`
SrcIPCountry string `json:"srcipCountry"`
SrcMAC string `json:"src_mac"`
SrcIP string `json:"src_ip" fake:"{ipv4address}"`
SrcIPASN string `json:"srcipASN" fake:"{address}"`
SrcIPCountry string `json:"srcipCountry" fake:"{country}"`
SrcMAC string `json:"src_mac" fake:"{macaddress}"`
Subsystem string `json:"subsystem"`
UniqueAlertID string `json:"unique_alertid"`
USGIP string `json:"usgip"`
USGIPASN string `json:"usgipASN"`
USGIPCountry string `json:"usgipCountry"`
UniqueAlertID string `json:"unique_alertid" fake:"{uuid}"`
USGIP string `json:"usgip" fake:"{ipv4address}"`
USGIPASN string `json:"usgipASN" fake:"{address}"`
USGIPCountry string `json:"usgipCountry" fake:"{country}"`
DestIPGeo IPGeo `json:"dstipGeo"`
SourceIPGeo IPGeo `json:"srcipGeo"`
USGIPGeo IPGeo `json:"usgipGeo"`
Expand All @@ -71,7 +71,7 @@ func (u *Unifi) GetIDS(sites []*Site, timeRange ...time.Time) ([]*IDS, error) {
return data, nil
}

// GetIDSSite retreives the Intrusion Detection System Data for a single Site.
// GetIDSSite retrieves the Intrusion Detection System Data for a single Site.
// timeRange may have a length of 0, 1 or 2. The first time is Start, the second is End.
// Events between start and end are returned. End defaults to time.Now().
func (u *Unifi) GetIDSSite(site *Site, timeRange ...time.Time) ([]*IDS, error) {
Expand Down
Loading

0 comments on commit 881e9f6

Please sign in to comment.