From e56ce730064c01bbbfacf092a6c54a00fc0e3bea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arturo=20Filast=C3=B2?= Date: Fri, 23 Mar 2018 15:22:58 +0100 Subject: [PATCH] Add result summary support for IM tests --- nettests/groups/groups.go | 45 +++++++++++++++++++++++++++++-- nettests/im/facebook_messenger.go | 22 +++++++++++++-- nettests/im/telegram.go | 30 ++++++++++++++++++--- nettests/im/whatsapp.go | 28 ++++++++++++++++--- 4 files changed, 114 insertions(+), 11 deletions(-) diff --git a/nettests/groups/groups.go b/nettests/groups/groups.go index 5ab291a..c3f9f1a 100644 --- a/nettests/groups/groups.go +++ b/nettests/groups/groups.go @@ -34,7 +34,8 @@ type MiddleboxSummary struct { // IMSummary is the summary for the im tests type IMSummary struct { - Detected bool + Tested uint + Blocked uint } // WebsitesSummary is the summary for the websites test @@ -148,7 +149,47 @@ var NettestGroups = map[string]NettestGroup{ im.WhatsApp{}, }, Summary: func(m database.SummaryMap) (string, error) { - return "{}", nil + var ( + err error + waSummary im.WhatsAppSummary + tgSummary im.TelegramSummary + fbSummary im.FacebookMessengerSummary + summary IMSummary + ) + err = json.Unmarshal([]byte(m["Whatsapp"][0]), &waSummary) + if err != nil { + log.WithError(err).Error("failed to unmarshal whatsapp summary") + return "", err + } + err = json.Unmarshal([]byte(m["Telegram"][0]), &tgSummary) + if err != nil { + log.WithError(err).Error("failed to unmarshal telegram summary") + return "", err + } + err = json.Unmarshal([]byte(m["FacebookMessenger"][0]), &fbSummary) + if err != nil { + log.WithError(err).Error("failed to unmarshal facebook summary") + return "", err + } + // XXX it could actually be that some are not tested when the + // configuration is changed. + summary.Tested = 3 + summary.Blocked = 0 + if fbSummary.Blocked == true { + summary.Blocked++ + } + if tgSummary.Blocked == true { + summary.Blocked++ + } + if waSummary.Blocked == true { + summary.Blocked++ + } + + summaryBytes, err := json.Marshal(summary) + if err != nil { + return "", err + } + return string(summaryBytes), nil }, }, } diff --git a/nettests/im/facebook_messenger.go b/nettests/im/facebook_messenger.go index bb7fcac..0108824 100644 --- a/nettests/im/facebook_messenger.go +++ b/nettests/im/facebook_messenger.go @@ -20,13 +20,31 @@ func (h FacebookMessenger) Run(ctl *nettests.Controller) error { type FacebookMessengerSummary struct { DNSBlocking bool TCPBlocking bool + Blocked bool } // Summary generates a summary for a test run func (h FacebookMessenger) Summary(tk map[string]interface{}) interface{} { + var ( + dnsBlocking bool + tcpBlocking bool + ) + if tk["facebook_dns_blocking"] == nil { + dnsBlocking = false + } else { + dnsBlocking = tk["facebook_dns_blocking"].(bool) + } + + if tk["facebook_tcp_blocking"] == nil { + tcpBlocking = false + } else { + tcpBlocking = tk["facebook_tcp_blocking"].(bool) + } + return FacebookMessengerSummary{ - DNSBlocking: tk["facebook_dns_blocking"].(bool), - TCPBlocking: tk["facebook_tcp_blocking"].(bool), + DNSBlocking: dnsBlocking, + TCPBlocking: tcpBlocking, + Blocked: dnsBlocking || tcpBlocking, } } diff --git a/nettests/im/telegram.go b/nettests/im/telegram.go index 503d55c..8d3f450 100644 --- a/nettests/im/telegram.go +++ b/nettests/im/telegram.go @@ -21,14 +21,38 @@ type TelegramSummary struct { HTTPBlocking bool TCPBlocking bool WebBlocking bool + Blocked bool } // Summary generates a summary for a test run func (h Telegram) Summary(tk map[string]interface{}) interface{} { + var ( + tcpBlocking bool + httpBlocking bool + webBlocking bool + ) + + if tk["telegram_tcp_blocking"] == nil { + tcpBlocking = false + } else { + tcpBlocking = tk["telegram_tcp_blocking"].(bool) + } + if tk["telegram_http_blocking"] == nil { + httpBlocking = false + } else { + httpBlocking = tk["telegram_http_blocking"].(bool) + } + if tk["telegram_web_status"] == nil { + webBlocking = false + } else { + webBlocking = tk["telegram_web_status"].(string) == "blocked" + } + return TelegramSummary{ - TCPBlocking: tk["telegram_tcp_blocking"].(bool) == true, - HTTPBlocking: tk["telegram_http_blocking"].(bool) == true, - WebBlocking: tk["telegram_web_status"].(string) == "blocked", + TCPBlocking: tcpBlocking, + HTTPBlocking: httpBlocking, + WebBlocking: webBlocking, + Blocked: webBlocking || httpBlocking || tcpBlocking, } } diff --git a/nettests/im/whatsapp.go b/nettests/im/whatsapp.go index 5dc26ba..24c8b59 100644 --- a/nettests/im/whatsapp.go +++ b/nettests/im/whatsapp.go @@ -21,16 +21,36 @@ type WhatsAppSummary struct { RegistrationServerBlocking bool WebBlocking bool EndpointsBlocking bool + Blocked bool } // Summary generates a summary for a test run func (h WhatsApp) Summary(tk map[string]interface{}) interface{} { - const blk = "blocked" + var ( + webBlocking bool + registrationBlocking bool + endpointsBlocking bool + ) + + var computeBlocking = func(key string) bool { + const blk = "blocked" + if tk[key] == nil { + return false + } + if tk[key].(string) == blk { + return true + } + return false + } + registrationBlocking = computeBlocking("registration_server_status") + webBlocking = computeBlocking("whatsapp_web_status") + endpointsBlocking = computeBlocking("whatsapp_endpoints_status") return WhatsAppSummary{ - RegistrationServerBlocking: tk["registration_server_status"].(string) == blk, - WebBlocking: tk["whatsapp_web_status"].(string) == blk, - EndpointsBlocking: tk["whatsapp_endpoints_status"].(string) == blk, + RegistrationServerBlocking: registrationBlocking, + WebBlocking: webBlocking, + EndpointsBlocking: endpointsBlocking, + Blocked: registrationBlocking || webBlocking || endpointsBlocking, } }