Beispiel #1
0
func testObjKVs(t *testing.T) {
	counter, countingValidator := createCountingValidator()

	sch := v.Object(
		v.ObjKV(nil, v.And(v.String(v.StrIs("1")), v.Function(countingValidator))),
		v.ObjKV("1", v.And(v.String(v.StrIs("b")), v.Function(countingValidator))),
		v.ObjKV(true, v.And(v.Number(v.NumIs(3)), v.Function(countingValidator))),
	)
	m := maep{
		nil:  "1",
		"1":  "b",
		true: 3,
	}
	test(t, "mixed objkvs", true, sch, m)
	if *counter != 3 {
		t.Fatalf("value counter should be 3, got %d", *counter)
	}

	m = maep{
		nil:  "1",
		"1":  2,
		true: 3,
	}
	test(t, "!mixed objkvs", false, sch, m)

	m = maep{
		nil:  "1",
		"1":  nil,
		true: 3,
	}
	test(t, "!mixed objkvs (nil)", false, sch, m)
}
Beispiel #2
0
func TestOptional(t *testing.T) {
	var np *bool
	test(t, "string", true, v.Optional(v.String(v.StrIs("a"))), "a")
	test(t, "wrong string", false, v.Optional(v.String(v.StrIs("a"))), "b")

	test(t, "nil ptr", true, v.Optional(v.String(v.StrIs("a"))), np)
}
Beispiel #3
0
func (this SimulationView) GetValidationSchema() valid.Validator {
	return valid.Object(
		valid.ObjKV("data", valid.Object(
			valid.ObjKV("pairs", valid.Array(valid.ArrEach(valid.Optional(valid.Object(
				valid.ObjKV("request", valid.Object(
					valid.ObjKV("requestType", valid.Optional(valid.String())),
					valid.ObjKV("path", valid.Optional(valid.String())),
					valid.ObjKV("method", valid.Optional(valid.String())),
					valid.ObjKV("scheme", valid.Optional(valid.String())),
					valid.ObjKV("query", valid.Optional(valid.String())),
					valid.ObjKV("body", valid.Optional(valid.String())),
					valid.ObjKV("headers", valid.Optional(valid.Object())),
				)),
				valid.ObjKV("response", valid.Object(
					valid.ObjKV("status", valid.Optional(valid.Number())),
					valid.ObjKV("body", valid.Optional(valid.String())),
					valid.ObjKV("encodedBody", valid.Optional(valid.Boolean())),
					valid.ObjKV("headers", valid.Optional(valid.Object())),
				)),
			))))),
			valid.ObjKV("globalActions", valid.Optional(valid.Object(
				valid.ObjKV("delays", valid.Array(valid.ArrEach(valid.Optional(valid.Object(
					valid.ObjKV("urlPattern", valid.Optional(valid.String())),
					valid.ObjKV("httpMethod", valid.Optional(valid.String())),
					valid.ObjKV("delay", valid.Optional(valid.Number())),
				))))),
			))),
		)),
		valid.ObjKV("meta", valid.Object(
			valid.ObjKV("schemaVersion", valid.String()),
		)),
	)
}
Beispiel #4
0
func TestFlowTable_jsonFlowConversationEthernetPath(t *testing.T) {
	ft := flow.NewTestFlowTableComplex(t, nil, nil)
	fa := &FlowApi{
		FlowTable: ft,
	}

	statStr := fa.jsonFlowConversationEthernetPath(flow.FlowProtocol_ETHERNET)
	if statStr == `{"nodes":[],"links":[]}` {
		t.Error("stat should not be empty")
	}

	var decoded interface{}
	if err := json.Unmarshal([]byte(statStr), &decoded); err != nil {
		t.Error("JSON parsing failed:", err)
	}

	schema := v.Object(
		v.ObjKV("nodes", v.Array(v.ArrEach(v.Object(
			v.ObjKV("name", v.String(v.StrMin(1))),
			v.ObjKV("group", v.Number(v.NumMin(0), v.NumMax(20))),
		)))),
		v.ObjKV("links", v.Array(v.ArrEach(v.Object(
			v.ObjKV("source", v.Number(v.NumMin(0), v.NumMax(20))),
			v.ObjKV("target", v.Number(v.NumMin(0), v.NumMax(20))),
			v.ObjKV("value", v.Number(v.NumMin(0), v.NumMax(9999))),
		)))),
	)

	if path, err := schema.Validate(decoded); err != nil {
		t.Errorf("Failed (%s). Path: %s", err, path)
	}
}
Beispiel #5
0
// Validates config structure
func validateConfig(configMap map[string]interface{}) error {
	schema := v.Object(v.ObjKeys(v.String()),
		v.ObjValues(v.Array(v.ArrEach(v.Object(
			v.ObjKV("docker", v.Object(
				v.ObjKV("image", v.String()),
				v.ObjKV("command", v.String()),
				v.ObjKV("permissions", v.Optional(v.Object(
					v.ObjKV("cwd", v.Boolean()),
				))),
			)),
		)))),
	)
	if path, err := schema.Validate(configMap); err != nil {
		return fmt.Errorf("Invalid hop definition at %s. Error (%s)",
			path, err)
	}
	return nil
}
Beispiel #6
0
func test_jsonFlowDiscovery(t *testing.T, DiscoType discoType) {
	ft := flow.NewTestFlowTableComplex(t, nil, nil)
	fa := &FlowApi{
		FlowTable: ft,
	}
	disco := fa.jsonFlowDiscovery(DiscoType)

	if disco == `{"name":"root","children":[]}` {
		t.Error("disco should not be empty")
	}

	var decoded interface{}
	if err := json.Unmarshal([]byte(disco), &decoded); err != nil {
		t.Error("JSON parsing failed:", err)
	}

	schema := v.Object(
		v.ObjKV("name", v.String(v.StrMin(1))), // root
		v.ObjKV("children", v.Array(v.ArrEach(v.Object(
			v.ObjKV("name", v.String(v.StrMin(1))), // Ethernet
			v.ObjKV("children", v.Array(v.ArrEach(v.Object(
				v.ObjKV("name", v.String(v.StrMin(1))), // IPv4
				v.ObjKV("children", v.Array(v.ArrEach(v.Object(
					v.ObjKV("name", v.String(v.StrMin(1))), // TCP or UDP
					v.ObjKV("children", v.Array(v.ArrEach(v.Object(
						v.ObjKV("name", v.String(v.StrMin(1))), // Payload
						v.ObjKV("size", v.Number(v.NumMin(0), v.NumMax(9999))),
					)))),
				)))),
			)))),
		)))),
	)

	if path, err := schema.Validate(decoded); err != nil {
		t.Errorf("Failed (%s). Path: %s", err, path)
	}
}
Beispiel #7
0
func testObjValues(t *testing.T) {
	counter, countingValidator := createCountingValidator()

	sch := v.Object(
		v.ObjValues(v.String()),
		v.ObjValues(v.Function(countingValidator)),
	)
	m := maep{
		nil:  "1",
		1:    "b",
		true: "c",
	}
	test(t, "only string objvalues", true, sch, m)
	if *counter != 3 {
		t.Fatalf("value counter should be 3, got %d", *counter)
	}

	m = maep{
		nil: "1",
		1:   1,
	}
	test(t, "!only string objvalues", false, sch, m)
}
Beispiel #8
0
func testObjKeys(t *testing.T) {
	counter, countingValidator := createCountingValidator()

	sch := v.Object(
		v.ObjKeys(v.String()),
		v.ObjKeys(v.Function(countingValidator)),
	)
	m := maep{
		"a": nil,
		"b": 1,
		"c": true,
	}
	test(t, "only string objkeys", true, sch, m)
	if *counter != 3 {
		t.Fatalf("key counter should be 3, got %d", *counter)
	}

	m = maep{
		"a": nil,
		1:   1,
	}
	test(t, "!only string objkeys", false, sch, m)
}
Beispiel #9
0
func TestString(t *testing.T) {
	var np *string
	var nnp string = "a"

	test(t, "non-string", false, v.String(), 3)
	test(t, "basic string", true, v.String(), "")
	test(t, "nil", false, v.String(), nil)
	test(t, "nil string pointer", false, v.String(), np)
	test(t, "string pointer", true, v.String(), &nnp)

	test(t, "equals", true, v.String(v.StrIs("abc")), "abc")
	test(t, "!equals", false, v.String(v.StrIs("abc")), "abd")

	test(t, "minlen1", false, v.String(v.StrMin(3)), "aa")
	test(t, "minlen2", true, v.String(v.StrMin(3)), "aaa")
	test(t, "minlen3", true, v.String(v.StrMin(3)), "aaaa")

	test(t, "maxlen1", true, v.String(v.StrMax(4)), "aaa")
	test(t, "maxlen2", true, v.String(v.StrMax(4)), "aaaa")
	test(t, "maxlen3", false, v.String(v.StrMax(4)), "aaaaa")

	test(t, "regexp1", true, v.String(v.StrRegExp("^.{3}$")), "bbb")
	test(t, "regexp2", false, v.String(v.StrRegExp("^.{3}$")), "bbbb")
	test(t, "regexp3", false, v.String(v.StrRegExp("[")), "c")

	test(t, "combination1", false, v.String(v.StrMin(3), v.StrMax(3)), "cc")
	test(t, "combination2", true, v.String(v.StrMin(3), v.StrMax(3)), "ccc")
	test(t, "combination1", false, v.String(v.StrMin(3), v.StrMax(3)), "cccc")

}
Beispiel #10
0
func TestFlowJSON(t *testing.T) {
	f := Flow{
		UUID:       "uuid-1",
		LayersPath: "layerpath-1",
		Statistics: &FlowStatistics{
			Start: 1111,
			Last:  222,
			Endpoints: []*FlowEndpointsStatistics{
				{
					Type: FlowEndpointType_ETHERNET,
					AB: &FlowEndpointStatistics{
						Value:   "value-1",
						Bytes:   33,
						Packets: 34,
					},
					BA: &FlowEndpointStatistics{
						Value:   "value-2",
						Bytes:   44,
						Packets: 55,
					},
				},
			},
		},
		ProbeGraphPath: "probepath-1",
		IfSrcGraphPath: "srcgraphpath-1",
		IfDstGraphPath: "dstgraphpath-1",
	}

	j, err := json.Marshal(f)
	if err != nil {
		t.Error(err.Error())
	}

	schema := v.Object(
		v.ObjKV("UUID", v.String()),
		v.ObjKV("LayersPath", v.String()),
		v.ObjKV("ProbeGraphPath", v.String()),
		v.ObjKV("IfSrcGraphPath", v.String()),
		v.ObjKV("IfDstGraphPath", v.String()),
		v.ObjKV("Statistics", v.Object(
			v.ObjKV("Start", v.Number()),
			v.ObjKV("Last", v.Number()),
			v.ObjKV("Endpoints", v.Array(v.ArrEach(v.Object(
				v.ObjKV("Type", v.String()),
				v.ObjKV("AB", v.Object(
					v.ObjKV("Value", v.String()),
					v.ObjKV("Packets", v.Number()),
					v.ObjKV("Bytes", v.Number()),
				)),
				v.ObjKV("AB", v.Object(
					v.ObjKV("Value", v.String()),
					v.ObjKV("Packets", v.Number()),
					v.ObjKV("Bytes", v.Number()),
				)),
			)))),
		)),
	)

	var data interface{}
	if err := json.Unmarshal(j, &data); err != nil {
		t.Fatal("JSON parsing failed. Err =", err)
	}

	if path, err := schema.Validate(data); err != nil {
		t.Fatalf("Validation failed at %s. Error (%s)", path, err)
	}

	var e Flow
	if err := json.Unmarshal(j, &e); err != nil {
		t.Fatal("JSON parsing failed. Err =", err)
	}

	if !reflect.DeepEqual(f, e) {
		t.Fatal("Unmarshalled flow not equal to the original")
	}
}
Beispiel #11
0
func TestFlowJSON(t *testing.T) {
	f := Flow{
		UUID:       "uuid-1",
		LayersPath: "layerpath-1",
		Link: &FlowLayer{
			Protocol: FlowProtocol_ETHERNET,
			A:        "value-1",
			B:        "value-2",
		},
		Metric: &FlowMetric{
			Start: 1111,
			Last:  222,

			ABBytes:   33,
			ABPackets: 34,

			BABytes:   44,
			BAPackets: 55,
		},
		NodeTID:  "probe-tid",
		ANodeTID: "anode-tid",
		BNodeTID: "bnode-tid",
	}

	j, err := json.Marshal(f)
	if err != nil {
		t.Error(err.Error())
	}

	schema := v.Object(
		v.ObjKV("UUID", v.String()),
		v.ObjKV("LayersPath", v.String()),
		v.ObjKV("NodeTID", v.String()),
		v.ObjKV("ANodeTID", v.String()),
		v.ObjKV("BNodeTID", v.String()),
		v.ObjKV("Link", v.Object(
			v.ObjKV("Protocol", v.String()),
			v.ObjKV("A", v.String()),
			v.ObjKV("B", v.String()),
		)),
		v.ObjKV("Metric", v.Object(
			v.ObjKV("Start", v.Number()),
			v.ObjKV("Last", v.Number()),
			v.ObjKV("ABPackets", v.Number()),
			v.ObjKV("ABBytes", v.Number()),
			v.ObjKV("BAPackets", v.Number()),
			v.ObjKV("BABytes", v.Number()),
		)),
	)

	var data interface{}
	if err := json.Unmarshal(j, &data); err != nil {
		t.Fatal("JSON parsing failed. Err =", err)
	}

	if path, err := schema.Validate(data); err != nil {
		t.Fatalf("Validation failed at %s. Error (%s)", path, err)
	}

	var e Flow
	if err := json.Unmarshal(j, &e); err != nil {
		t.Fatal("JSON parsing failed. Err =", err)
	}

	if !reflect.DeepEqual(f, e) {
		t.Fatal("Unmarshalled flow not equal to the original")
	}
}
Beispiel #12
0
func TestOr(t *testing.T) {
	test(t, "combination1", true, v.Or(v.String(v.StrIs("a")), v.String(v.StrIs("b"))), "a")
	test(t, "combination2", true, v.Or(v.String(v.StrIs("a")), v.String(v.StrIs("b"))), "b")
	test(t, "combination3", false, v.Or(v.String(v.StrIs("a")), v.String(v.StrIs("b"))), 3)

	test(t, "combination4", true, v.Or(v.String(v.StrIs("a")), v.String(v.StrIs("b")), v.String(v.StrIs("c"))), "b")

	test(t, "combination5", true, v.Or(v.String(v.StrIs("a"))), "a")
	test(t, "combination6", false, v.Or(v.String(v.StrIs("a"))), "b")

	test(t, "combination7", true, v.Or(), nil)
}
Beispiel #13
0
func init() {
	// set up raw json data
	rawJson := []byte(`
		{
    	"status": true,
      "data": {
      	"token": "CAFED00D",
	      "debug": 69306,
      	"items": [
      	  { "url": "https://news.ycombinator.com/", "comment": "clickbaits" },
          { "url": "http://golang.org/", "comment": "some darn gophers" },
          { "url": "http://www.kickstarter.com/", "comment": "opensource projects. yeah.." }
       	],
       	"ghost2": null,
       	"meta": {
       	 "g": 1,
         "xyzzy": 0.25,
         "blöö": 0.5
       	}
      }
		}`)

	// decode json
	if err := json.Unmarshal(rawJson, &decoded); err != nil {
		log.Panic("JSON parsing failed. Err =", err)
	}

	// set up a custom validator function
	myValidatorFunc := func(data interface{}) (path string, err error) {
		path = "myValidatorFunc"

		validate, ok := data.(string)
		if !ok {
			return path, fmt.Errorf("expected string, got %v", reflect.TypeOf(data))
		}

		if validate != "CAFED00D" {
			return path, fmt.Errorf("expected CAFED00D, got %s", validate)
		}

		return "", nil
	}

	// construct the schema which is used to validate data
	schema = v.Object(
		v.ObjKV("status", v.Boolean()),
		v.ObjKV("data", v.Object(
			v.ObjKV("token", v.Function(myValidatorFunc)),
			v.ObjKV("debug", v.Number(v.NumMin(1), v.NumMax(99999))),
			v.ObjKV("items", v.Array(v.ArrEach(v.Object(
				v.ObjKV("url", v.String(v.StrMin(1))),
				v.ObjKV("comment", v.Optional(v.String())),
			)))),
			v.ObjKV("ghost", v.Optional(v.String())),
			v.ObjKV("ghost2", v.Optional(v.String())),
			v.ObjKV("meta", v.Object(
				v.ObjKeys(v.String()),
				v.ObjValues(v.Or(v.Number(v.NumMin(.01), v.NumMax(1.1)), v.String())),
			)),
		)),
	)
}
Beispiel #14
0
func TestAnd(t *testing.T) {
	test(t, "combination1", true, v.And(), nil)

	test(t, "combination2", false, v.And(v.String(v.StrMin(3)), v.String(v.StrMax(3))), "aa")
	test(t, "combination3", true, v.And(v.String(v.StrMin(3)), v.String(v.StrMax(3))), "aaa")
	test(t, "combination4", false, v.And(v.String(v.StrMin(3)), v.String(v.StrMax(3))), "aaaa")

	test(t, "combination5", false, v.And(v.String(v.StrMin(3)), v.String(v.StrMax(4))), "bb")
	test(t, "combination6", true, v.And(v.String(v.StrMin(3)), v.String(v.StrMax(4))), "bbb")
	test(t, "combination7", true, v.And(v.String(v.StrMin(3)), v.String(v.StrMax(4))), "bbbb")
	test(t, "combination8", false, v.And(v.String(v.StrMin(3)), v.String(v.StrMax(4))), "bbbbb")
}