Skip to content

Commit 881e9f6

Browse files
Merge pull request #83 from unpoller/mocking-responses
[testing] Mocking rig to fuzz and get generated data based on our struct types
2 parents f675ae7 + 43cdaef commit 881e9f6

22 files changed

+946
-288
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/vendor
22
*.swp
3+
.vscode/

alarms.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,44 @@ import (
88

99
type Alarm struct {
1010
Archived FlexBool `json:"archived"`
11-
DestPort int `json:"dest_port"`
12-
SrcPort int `json:"src_port"`
11+
DestPort int `json:"dest_port" fake:"{port}"`
12+
SrcPort int `json:"src_port" fake:"{port}"`
1313
FlowID int64 `json:"flow_id"`
1414
InnerAlertGID int64 `json:"inner_alert_gid"`
1515
InnerAlertRev int64 `json:"inner_alert_rev"`
1616
InnerAlertSeverity int64 `json:"inner_alert_severity"`
1717
InnerAlertSignatureID int64 `json:"inner_alert_signature_id"`
18-
Time int64 `json:"time"`
19-
Timestamp int64 `json:"timestamp"`
18+
Time int64 `json:"time" fake:"{timestamp}"`
19+
Timestamp int64 `json:"timestamp" fake:"{timestamp}"`
2020
Datetime time.Time `json:"datetime"`
2121
HandledTime time.Time `json:"handled_time,omitempty"`
2222
AppProto string `json:"app_proto,omitempty"`
2323
Catname string `json:"catname"`
24-
DestIP string `json:"dest_ip"`
25-
DstMAC string `json:"dst_mac"`
24+
DestIP string `json:"dest_ip" fake:"{ipv4address}"`
25+
DstMAC string `json:"dst_mac" fake:"{macaddress}"`
2626
DstIPASN string `json:"dstipASN,omitempty"`
2727
DstIPCountry string `json:"dstipCountry,omitempty"`
2828
EventType string `json:"event_type"`
2929
HandledAdminID string `json:"handled_admin_id,omitempty"`
3030
Host string `json:"host"`
31-
ID string `json:"_id"`
32-
InIface string `json:"in_iface"`
31+
ID string `json:"_id" fake:"{uuid}"`
32+
InIface string `json:"in_iface" fake:"{randomstring:[eth0,eth1,lan1,wan1,wan2]}"`
3333
InnerAlertAction string `json:"inner_alert_action"`
3434
InnerAlertCategory string `json:"inner_alert_category"`
3535
InnerAlertSignature string `json:"inner_alert_signature"`
3636
Key string `json:"key"`
37-
Msg string `json:"msg"`
37+
Msg string `json:"msg" fake:"{sentence:5}"`
3838
Proto string `json:"proto"`
39-
SiteID string `json:"site_id"`
39+
SiteID string `json:"site_id" fake:"{uuid}"`
4040
SiteName string `json:"-"`
4141
SourceName string `json:"-"`
42-
SrcIP string `json:"src_ip"`
42+
SrcIP string `json:"src_ip" fake:"{ipv4address}"`
4343
SrcIPASN string `json:"srcipASN,omitempty"`
4444
SrcIPCountry string `json:"srcipCountry,omitempty"`
45-
SrcMAC string `json:"src_mac"`
45+
SrcMAC string `json:"src_mac" fake:"{macaddress}"`
4646
Subsystem string `json:"subsystem"`
4747
UniqueAlertID string `json:"unique_alertid"`
48-
USGIP string `json:"usgip"`
48+
USGIP string `json:"usgip" fake:"{ipv4address}"`
4949
USGIPASN string `json:"usgipASN"`
5050
USGIPCountry string `json:"usgipCountry"`
5151
TxID FlexInt `json:"tx_id,omitempty"`

anomalies.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
// anomaly is the type UniFi returns, but not the type this library returns.
1212
type anomaly struct {
1313
Anomaly string `json:"anomaly"`
14-
MAC string `json:"mac"`
15-
Timestamps []int64 `json:"timestamps"`
14+
MAC string `json:"mac" fake:"{macaddress}"`
15+
Timestamps []int64 `json:"timestamps" fake:"{timestamps}"`
1616
}
1717

1818
// Anomaly is the reformatted data type that this library returns.
@@ -21,7 +21,7 @@ type Anomaly struct {
2121
SourceName string
2222
SiteName string
2323
Anomaly string
24-
DeviceMAC string
24+
DeviceMAC string `fake:"{macaddress}"`
2525
// DeviceName string // we do not have this....
2626
}
2727

clients.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ func (u *Unifi) GetClientsDPI(sites []*Site) ([]*DPITable, error) {
6767
type Client struct {
6868
SourceName string `json:"-"`
6969
Anomalies FlexInt `json:"anomalies,omitempty"`
70-
ApMac string `json:"ap_mac"`
70+
ApMac string `json:"ap_mac" fake:"{macaddress}"`
7171
ApName string `json:"-"`
7272
AssocTime FlexInt `json:"assoc_time"`
7373
Blocked bool `json:"blocked,omitempty"`
74-
Bssid string `json:"bssid"`
74+
Bssid string `json:"bssid" fake:"{macaddress}"`
7575
BytesR FlexInt `json:"bytes-r"`
7676
Ccq FlexInt `json:"ccq"`
7777
Channel FlexInt `json:"channel"`
@@ -81,14 +81,14 @@ type Client struct {
8181
DevVendor FlexInt `json:"dev_vendor,omitempty"`
8282
DhcpendTime FlexInt `json:"dhcpend_time,omitempty"`
8383
Satisfaction FlexInt `json:"satisfaction,omitempty"`
84-
Essid string `json:"essid"`
84+
Essid string `json:"essid" fake:"{macaddress}"`
8585
FirstSeen FlexInt `json:"first_seen"`
86-
FixedIP string `json:"fixed_ip"`
87-
GwMac string `json:"gw_mac"`
86+
FixedIP string `json:"fixed_ip" fake:"{ipv4address}"`
87+
GwMac string `json:"gw_mac" fake:"{macaddress}"`
8888
GwName string `json:"-"`
8989
Hostname string `json:"hostname"`
90-
ID string `json:"_id"`
91-
IP string `json:"ip"`
90+
ID string `json:"_id" fake:"{uuid}"`
91+
IP string `json:"ip" fake:"{ipv4address}"`
9292
IdleTime FlexInt `json:"idle_time"`
9393
Is11R FlexBool `json:"is_11r"`
9494
IsGuest FlexBool `json:"is_guest"`
@@ -101,12 +101,12 @@ type Client struct {
101101
LastSeenByUGW FlexInt `json:"_last_seen_by_ugw"`
102102
LastSeenByUSW FlexInt `json:"_last_seen_by_usw"`
103103
LatestAssocTime FlexInt `json:"latest_assoc_time"`
104-
Mac string `json:"mac"`
104+
Mac string `json:"mac" fake:"{macaddress}"`
105105
Name string `json:"name"`
106106
Network string `json:"network"`
107-
NetworkID string `json:"network_id"`
107+
NetworkID string `json:"network_id" fake:"{uuid}"`
108108
Noise FlexInt `json:"noise"`
109-
Note string `json:"note"`
109+
Note string `json:"note" fake:"{sentence 20}"`
110110
Noted FlexBool `json:"noted"`
111111
OsClass FlexInt `json:"os_class"`
112112
OsName FlexInt `json:"os_name"`
@@ -124,10 +124,10 @@ type Client struct {
124124
RxPackets FlexInt `json:"rx_packets"`
125125
RxRate FlexInt `json:"rx_rate"`
126126
Signal FlexInt `json:"signal"`
127-
SiteID string `json:"site_id"`
127+
SiteID string `json:"site_id" fake:"{uuid}"`
128128
SiteName string `json:"-"`
129129
SwDepth int `json:"sw_depth"`
130-
SwMac string `json:"sw_mac"`
130+
SwMac string `json:"sw_mac" fake:"{macaddress}"`
131131
SwName string `json:"-"`
132132
SwPort FlexInt `json:"sw_port"`
133133
TxBytes FlexInt `json:"tx_bytes"`
@@ -141,8 +141,8 @@ type Client struct {
141141
UptimeByUGW FlexInt `json:"_uptime_by_ugw"`
142142
UptimeByUSW FlexInt `json:"_uptime_by_usw"`
143143
UseFixedIP FlexBool `json:"use_fixedip"`
144-
UserGroupID string `json:"usergroup_id"`
145-
UserID string `json:"user_id"`
144+
UserGroupID string `json:"usergroup_id" fake:"{uuid}"`
145+
UserID string `json:"user_id" fake:"{uuid}"`
146146
Vlan FlexInt `json:"vlan"`
147147
WifiTxAttempts FlexInt `json:"wifi_tx_attempts"`
148148
WiredRxBytes FlexInt `json:"wired-rx_bytes"`

devmgr.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ const (
2626

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

3636
// devMgrCommandReply is for commands with a return value.

dpi.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ type DPITable struct {
77
SourceName string `json:"-"`
88
SiteName string `json:"-"`
99
Name string `json:"-"`
10-
MAC string `json:"mac"`
11-
ByCat []DPIData `json:"by_cat"`
12-
ByApp []DPIData `json:"by_app"`
10+
MAC string `json:"mac" fake:"{macaddress}"`
11+
ByCat []DPIData `json:"by_cat" fakesize:"5"`
12+
ByApp []DPIData `json:"by_app" fakesize:"5"`
1313
LastUpdated FlexInt `json:"last_updated"`
1414
}
1515

@@ -21,13 +21,13 @@ type DPIData struct {
2121
TxBytes FlexInt `json:"tx_bytes"`
2222
RxPackets FlexInt `json:"rx_packets"`
2323
TxPackets FlexInt `json:"tx_packets"`
24-
Clients []*DPIClient `json:"clients,omitempty"`
24+
Clients []*DPIClient `json:"clients,omitempty" fakesize:"5"`
2525
KnownClients FlexInt `json:"known_clients,omitempty"`
2626
}
2727

2828
// DPIClient data is sometimes included in ByApp output.
2929
type DPIClient struct {
30-
Mac string `json:"mac"`
30+
Mac string `json:"mac" fake:"{macaddress}"`
3131
RxBytes FlexInt `json:"rx_bytes"`
3232
TxBytes FlexInt `json:"tx_bytes"`
3333
RxPackets FlexInt `json:"rx_packets"`

events.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ func (u *Unifi) GetSiteEvents(site *Site, hours time.Duration) ([]*Event, error)
7373
// API Path: /api/s/default/stat/event.
7474
type Event struct {
7575
IsAdmin FlexBool `json:"is_admin"`
76-
DestPort int `json:"dest_port"`
77-
SrcPort int `json:"src_port"`
76+
DestPort int `json:"dest_port" fake:"{port}"`
77+
SrcPort int `json:"src_port" fake:"{port}"`
7878
Bytes FlexInt `json:"bytes"`
7979
Duration FlexInt `json:"duration"`
8080
FlowID FlexInt `json:"flow_id"`
@@ -85,8 +85,8 @@ type Event struct {
8585
Channel FlexInt `json:"channel"`
8686
ChannelFrom FlexInt `json:"channel_from"`
8787
ChannelTo FlexInt `json:"channel_to"`
88-
Time int64 `json:"time"`
89-
Timestamp int64 `json:"timestamp"`
88+
Time int64 `json:"time" fake:"{timestamp}"`
89+
Timestamp int64 `json:"timestamp" fake:"{timestamp}"`
9090
Datetime time.Time `json:"datetime"`
9191
Admin string `json:"admin"`
9292
Ap string `json:"ap"`
@@ -95,43 +95,43 @@ type Event struct {
9595
ApTo string `json:"ap_to"`
9696
AppProto string `json:"app_proto"`
9797
Catname string `json:"catname"`
98-
DestIP string `json:"dest_ip"`
99-
DstMAC string `json:"dst_mac"`
98+
DestIP string `json:"dest_ip" fake:"{ipv4address}"`
99+
DstMAC string `json:"dst_mac" fake:"{macaddress}"`
100100
EventType string `json:"event_type"`
101101
Guest string `json:"guest"`
102102
Gw string `json:"gw"`
103103
GwName string `json:"gw_name"`
104104
Host string `json:"host"`
105105
Hostname string `json:"hostname"`
106-
ID string `json:"_id"`
107-
IP string `json:"ip"`
106+
ID string `json:"_id" fake:"{uuid}"`
107+
IP string `json:"ip" fake:"{ipv4address}"`
108108
InIface string `json:"in_iface"`
109109
InnerAlertAction string `json:"inner_alert_action"`
110110
InnerAlertCategory string `json:"inner_alert_category"`
111111
InnerAlertSignature string `json:"inner_alert_signature"`
112-
Key string `json:"key"`
113-
Msg string `json:"msg"`
112+
Key string `json:"key" fake:"{uuid}"`
113+
Msg string `json:"msg" fake:"{sentence:20}"`
114114
Network string `json:"network"`
115115
Proto string `json:"proto"`
116116
Radio string `json:"radio"`
117117
RadioFrom string `json:"radio_from"`
118118
RadioTo string `json:"radio_to"`
119-
SiteID string `json:"site_id"`
119+
SiteID string `json:"site_id" fake:"{}"`
120120
SiteName string `json:"-"`
121121
SourceName string `json:"-"`
122-
SrcIP string `json:"src_ip"`
123-
SrcMAC string `json:"src_mac"`
124-
SrcIPASN string `json:"srcipASN"`
125-
SrcIPCountry string `json:"srcipCountry"`
126-
SSID string `json:"ssid"`
122+
SrcIP string `json:"src_ip" fake:"{ipv4address}"`
123+
SrcMAC string `json:"src_mac" fake:"{macaddress}"`
124+
SrcIPASN string `json:"srcipASN" fake:"{address}"`
125+
SrcIPCountry string `json:"srcipCountry" fake:"{country}"`
126+
SSID string `json:"ssid" fake:"{macaddress}"`
127127
Subsystem string `json:"subsystem"`
128128
Sw string `json:"sw"`
129129
SwName string `json:"sw_name"`
130130
UniqueAlertID string `json:"unique_alertid"`
131131
User string `json:"user"`
132-
USGIP string `json:"usgip"`
133-
USGIPASN string `json:"usgipASN"`
134-
USGIPCountry string `json:"usgipCountry"`
132+
USGIP string `json:"usgip" fake:"{ipv4address}"`
133+
USGIPASN string `json:"usgipASN" fake:"{address}"`
134+
USGIPCountry string `json:"usgipCountry" fake:"{country}"`
135135
DestIPGeo IPGeo `json:"dstipGeo"`
136136
SourceIPGeo IPGeo `json:"srcipGeo"`
137137
USGIPGeo IPGeo `json:"usgipGeo"`
@@ -140,14 +140,14 @@ type Event struct {
140140
// IPGeo is part of the UniFi Event data. Each event may have up to three of these.
141141
// One for source, one for dest and one for the USG location.
142142
type IPGeo struct {
143-
Asn int64 `json:"asn"`
144-
Latitude float64 `json:"latitude"`
145-
Longitude float64 `json:"longitude"`
146-
City string `json:"city"`
143+
Asn int64 `json:"asn" fake:"{address}"`
144+
Latitude float64 `json:"latitude" fake:"{latitude}"`
145+
Longitude float64 `json:"longitude" fake:"{longitude}"`
146+
City string `json:"city" fake:"{city}"`
147147
ContinentCode string `json:"continent_code"`
148-
CountryCode string `json:"country_code"`
149-
CountryName string `json:"country_name"`
150-
Organization string `json:"organization"`
148+
CountryCode string `json:"country_code" fake:"{countryabr}"`
149+
CountryName string `json:"country_name" fake:"{country}"`
150+
Organization string `json:"organization" fake:"{company}"`
151151
}
152152

153153
// Events satisfied the sort.Interface.

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/unpoller/unifi
33
go 1.19
44

55
require (
6+
github.com/brianvoe/gofakeit/v6 v6.23.0
67
github.com/stretchr/testify v1.8.4
78
golang.org/x/net v0.12.0
89
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/brianvoe/gofakeit/v6 v6.23.0 h1:pgVhyWpYq4e0GEVCh2gdZnS/nBX+8SnyTBliHg5xjks=
2+
github.com/brianvoe/gofakeit/v6 v6.23.0/go.mod h1:Ow6qC71xtwm79anlwKRlWZW6zVq9D2XHE4QSSMP/rU8=
13
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
24
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
35
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

ids.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,44 +10,44 @@ import (
1010
// IDS holds an Intrusion Prevention System Event.
1111
type IDS struct {
1212
Archived FlexBool `json:"archived"`
13-
DestPort int `json:"dest_port,omitempty"`
14-
SrcPort int `json:"src_port,omitempty"`
15-
FlowID int64 `json:"flow_id"`
13+
DestPort int `json:"dest_port,omitempty" fake:"{port}"`
14+
SrcPort int `json:"src_port,omitempty" fake:"{port}"`
15+
FlowID int64 `json:"flow_id" fake:"{uuid}"`
1616
InnerAlertRev int64 `json:"inner_alert_rev"`
1717
InnerAlertSeverity int64 `json:"inner_alert_severity"`
1818
InnerAlertGID int64 `json:"inner_alert_gid"`
1919
InnerAlertSignatureID int64 `json:"inner_alert_signature_id"`
20-
Time int64 `json:"time"`
21-
Timestamp int64 `json:"timestamp"`
20+
Time int64 `json:"time" fake:"{timestamp}"`
21+
Timestamp int64 `json:"timestamp" fake:"{timestamp}"`
2222
Datetime time.Time `json:"datetime"`
2323
AppProto string `json:"app_proto,omitempty"`
2424
Catname string `json:"catname"`
25-
DestIP string `json:"dest_ip"`
26-
DstMAC string `json:"dst_mac"`
27-
DstIPASN string `json:"dstipASN"`
28-
DstIPCountry string `json:"dstipCountry"`
25+
DestIP string `json:"dest_ip" fake:"{ipv4address}"`
26+
DstMAC string `json:"dst_mac" fake:"{macaddress}"`
27+
DstIPASN string `json:"dstipASN" fake:"{address}"`
28+
DstIPCountry string `json:"dstipCountry" fake:"{country}"`
2929
EventType string `json:"event_type"`
3030
Host string `json:"host"`
31-
ID string `json:"_id"`
31+
ID string `json:"_id" fake:"{uuid}"`
3232
InIface string `json:"in_iface"`
3333
InnerAlertAction string `json:"inner_alert_action"`
3434
InnerAlertCategory string `json:"inner_alert_category"`
3535
InnerAlertSignature string `json:"inner_alert_signature"`
36-
Key string `json:"key"`
37-
Msg string `json:"msg"`
36+
Key string `json:"key" fake:"{uuid}"`
37+
Msg string `json:"msg" fake:"{sentence:20}"`
3838
Proto string `json:"proto"`
39-
SiteID string `json:"site_id"`
39+
SiteID string `json:"site_id" fake:"{uuid}"`
4040
SiteName string `json:"-"`
4141
SourceName string `json:"-"`
42-
SrcIP string `json:"src_ip"`
43-
SrcIPASN string `json:"srcipASN"`
44-
SrcIPCountry string `json:"srcipCountry"`
45-
SrcMAC string `json:"src_mac"`
42+
SrcIP string `json:"src_ip" fake:"{ipv4address}"`
43+
SrcIPASN string `json:"srcipASN" fake:"{address}"`
44+
SrcIPCountry string `json:"srcipCountry" fake:"{country}"`
45+
SrcMAC string `json:"src_mac" fake:"{macaddress}"`
4646
Subsystem string `json:"subsystem"`
47-
UniqueAlertID string `json:"unique_alertid"`
48-
USGIP string `json:"usgip"`
49-
USGIPASN string `json:"usgipASN"`
50-
USGIPCountry string `json:"usgipCountry"`
47+
UniqueAlertID string `json:"unique_alertid" fake:"{uuid}"`
48+
USGIP string `json:"usgip" fake:"{ipv4address}"`
49+
USGIPASN string `json:"usgipASN" fake:"{address}"`
50+
USGIPCountry string `json:"usgipCountry" fake:"{country}"`
5151
DestIPGeo IPGeo `json:"dstipGeo"`
5252
SourceIPGeo IPGeo `json:"srcipGeo"`
5353
USGIPGeo IPGeo `json:"usgipGeo"`
@@ -71,7 +71,7 @@ func (u *Unifi) GetIDS(sites []*Site, timeRange ...time.Time) ([]*IDS, error) {
7171
return data, nil
7272
}
7373

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

0 commit comments

Comments
 (0)