Add basic unittests for the config related functionality

This commit is contained in:
Arturo Filastò 2018-06-22 11:01:15 +02:00
parent 2653a3f67f
commit 1bba0c7899
5 changed files with 102 additions and 25 deletions

View File

@ -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

19
config/parser_test.go Normal file
View File

@ -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")
}
}

63
config/testdata/valid-config.json vendored Normal file
View File

@ -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
}
}

View File

@ -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)

17
ooni.go
View File

@ -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 {