feat: avoid safe options to be serialized into the measurement (#859)

Skip options that begin with the `Safe` prefix from appearing in the
serialization of a Measurement that will be submitted to the OONI
backend.

Fixes https://github.com/ooni/probe/issues/2214
This commit is contained in:
Ain Ghazal 2022-08-17 13:48:59 +02:00 committed by GitHub
commit d50a39ae92
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 2 deletions

View file

@ -3,6 +3,8 @@ package oonirun
import (
"context"
"os"
"reflect"
"sort"
"testing"
"time"
@ -58,3 +60,45 @@ func TestExperimentRunWithExample(t *testing.T) {
t.Fatal(err)
}
}
func Test_experimentOptionsToStringList(t *testing.T) {
type args struct {
options map[string]any
}
tests := []struct {
name string
args args
wantOut []string
}{
{
name: "happy path: a map with three entries returns three items",
args: args{
map[string]any{
"foo": 1,
"bar": 2,
"baaz": 3,
},
},
wantOut: []string{"baaz=3", "bar=2", "foo=1"},
},
{
name: "an option beginning with `Safe` is skipped from the output",
args: args{
map[string]any{
"foo": 1,
"Safefoo": 42,
},
},
wantOut: []string{"foo=1"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotOut := experimentOptionsToStringList(tt.args.options)
sort.Strings(gotOut)
if !reflect.DeepEqual(gotOut, tt.wantOut) {
t.Errorf("experimentOptionsToStringList() = %v, want %v", gotOut, tt.wantOut)
}
})
}
}