From 1bba0c789922dcdef7dbd2f05f37dd40304933b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arturo=20Filast=C3=B2?= Date: Fri, 22 Jun 2018 11:01:15 +0200 Subject: [PATCH] Add basic unittests for the config related functionality --- config/parser.go | 22 ++--------- config/parser_test.go | 19 ++++++++++ config/testdata/valid-config.json | 63 +++++++++++++++++++++++++++++++ internal/cli/run/run.go | 6 +++ ooni.go | 17 ++++++--- 5 files changed, 102 insertions(+), 25 deletions(-) create mode 100644 config/parser_test.go create mode 100644 config/testdata/valid-config.json diff --git a/config/parser.go b/config/parser.go index f013900..ea79923 100644 --- a/config/parser.go +++ b/config/parser.go @@ -3,7 +3,6 @@ package config import ( "encoding/json" "io/ioutil" - "os" "path/filepath" "sync" @@ -14,21 +13,6 @@ import ( // ReadConfig reads the configuration from the path func ReadConfig(path string) (*Config, error) { b, err := ioutil.ReadFile(path) - - if os.IsNotExist(err) { - c := &Config{} - - if err = c.Default(); err != nil { - return nil, errors.Wrap(err, "defaulting") - } - - if err = c.Validate(); err != nil { - return nil, errors.Wrap(err, "validating") - } - - return c, nil - } - if err != nil { return nil, errors.Wrap(err, "reading file") } @@ -43,9 +27,9 @@ func ReadConfig(path string) (*Config, error) { // ParseConfig returns config from JSON bytes. func ParseConfig(b []byte) (*Config, error) { - c := &Config{} + var c Config - if err := json.Unmarshal(b, c); err != nil { + if err := json.Unmarshal(b, &c); err != nil { return nil, errors.Wrap(err, "parsing json") } @@ -57,7 +41,7 @@ func ParseConfig(b []byte) (*Config, error) { return nil, errors.Wrap(err, "validating") } - return c, nil + return &c, nil } // Config for the OONI Probe installation diff --git a/config/parser_test.go b/config/parser_test.go new file mode 100644 index 0000000..010dbe6 --- /dev/null +++ b/config/parser_test.go @@ -0,0 +1,19 @@ +package config + +import ( + "testing" +) + +func TestParseConfig(t *testing.T) { + config, err := ReadConfig("testdata/valid-config.json") + if err != nil { + t.Error(err) + } + + if len(config.NettestGroups.Middlebox.EnabledTests) < 0 { + t.Error("at least one middlebox test should be enabled") + } + if config.Advanced.IncludeCountry == false { + t.Error("country should be included") + } +} diff --git a/config/testdata/valid-config.json b/config/testdata/valid-config.json new file mode 100644 index 0000000..3171bdc --- /dev/null +++ b/config/testdata/valid-config.json @@ -0,0 +1,63 @@ +{ + "_": "This is your OONI Probe config file. See https://ooni.io/help/probe-cli for help", + "_version": 0, + "_informed_consent": false, + "auto_update": true, + "sharing": { + "include_ip": false, + "include_asn": true, + "include_gps": true, + "upload_results": true, + "send_crash_reports": true + }, + "notifications": { + "enabled": true, + "notify_on_test_completion": true, + "notify_on_news": false + }, + "automated_testing": { + "enabled": false, + "enabled_tests": [ + "web-connectivity", + "facebook-messenger", + "whatsapp", + "telegram", + "dash", + "ndt", + "http-invalid-request-line", + "http-header-field-manipulation" + ], + "monthly_allowance": "300MB" + }, + "test_settings": { + "websites": { + "enabled_categories": [] + }, + "instant_messaging": { + "enabled_tests": [ + "facebook-messenger", + "whatsapp", + "telegram" + ] + }, + "performance": { + "enabled_tests": [ + "ndt" + ], + "ndt_server": "auto", + "ndt_server_port": "auto", + "dash_server": "auto", + "dash_server_port": "auto" + }, + "middlebox": { + "enabled_tests": [ + "http-invalid-request-line", + "http-header-field-manipulation" + ] + } + }, + "advanced": { + "include_country": true, + "use_domain_fronting": false + } +} diff --git a/internal/cli/run/run.go b/internal/cli/run/run.go index b6125bc..dacd23d 100644 --- a/internal/cli/run/run.go +++ b/internal/cli/run/run.go @@ -27,6 +27,12 @@ func init() { log.Errorf("%s", err) return err } + + if err = ctx.MaybeOnboarding(); err != nil { + log.WithError(err).Error("failed to perform onboarding") + return err + } + group, ok := groups.NettestGroups[*nettestGroup] if !ok { log.Errorf("No test group named %s", *nettestGroup) diff --git a/ooni.go b/ooni.go index 1c794e9..db1e391 100644 --- a/ooni.go +++ b/ooni.go @@ -66,6 +66,17 @@ func (c *Context) LocationLookup() error { return nil } +// MaybeOnboarding will run the onboarding process only if the informed consent +// config option is set to false +func (c *Context) MaybeOnboarding() error { + if c.Config.InformedConsent == false { + if err := Onboarding(c.Config); err != nil { + return errors.Wrap(err, "onboarding") + } + } + return nil +} + // Init the OONI manager func (c *Context) Init() error { var err error @@ -90,12 +101,6 @@ func (c *Context) Init() error { } c.dbPath = utils.DBDir(c.Home, "main") - if c.Config.InformedConsent == false { - if err = Onboarding(c.Config); err != nil { - return errors.Wrap(err, "onboarding") - } - } - log.Debugf("Connecting to database sqlite3://%s", c.dbPath) db, err := database.Connect(c.dbPath) if err != nil {