Example #1
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)
	}
}
Example #2
0
func TestArray(t *testing.T) {
	var np *[]interface{}
	var nnsp = []interface{}{1, 2, 3}
	var nnap = [3]interface{}{1, 2, 3}

	test(t, "basic slice", true, v.Array(), []interface{}{1, 2, 3})
	test(t, "nil", false, v.Array(), nil)
	test(t, "nil slice pointer", false, v.Array(), np)
	test(t, "slice pointer", true, v.Array(), &nnsp)
	test(t, "non-array/slice", false, v.Array(), 3)

	test(t, "basic array ", true, v.Array(), [3]interface{}{1, 2, 3})
	test(t, "nil array pointer", true, v.Array(), [3]interface{}{1, 2, 3})
	test(t, "array pointer", true, v.Array(), &nnap)

	test(t, "int slice", true, v.Array(), []int{1, 2, 3})

	test(t, "minlen1", false, v.Array(v.ArrMin(3)), []interface{}{1, 2})
	test(t, "minlen2", true, v.Array(v.ArrMin(3)), []interface{}{1, 2, 3})
	test(t, "minlen3", true, v.Array(v.ArrMin(3)), []interface{}{1, 2, 3, 4})

	test(t, "maxlen", true, v.Array(v.ArrMax(3)), []interface{}{1, 2})
	test(t, "maxlen2", true, v.Array(v.ArrMax(3)), []interface{}{1, 2, 3})
	test(t, "maxlen3", false, v.Array(v.ArrMax(3)), []interface{}{1, 2, 3, 4})

	test(t, "combination1", false, v.Array(v.ArrMin(3), v.ArrMax(3)), []interface{}{1, 2})
	test(t, "combination2", true, v.Array(v.ArrMin(3), v.ArrMax(3)), []interface{}{1, 2, 3})
	test(t, "combination3", false, v.Array(v.ArrMin(3), v.ArrMax(3)), []interface{}{1, 2, 3, 4})

	test(t, "each1", true, v.Array(v.ArrEach(v.Number(v.NumMin(3)))), []interface{}{})
	test(t, "each2", false, v.Array(v.ArrEach(v.Number(v.NumMin(3)))), []interface{}{2, 3})
	test(t, "each3", true, v.Array(v.ArrEach(v.Number(v.NumMin(3)))), []interface{}{3, 4, 5})
}
Example #3
0
func TestNumber(t *testing.T) {
	var np *int
	var nnp int = 3

	test(t, "basic number", true, v.Number(), 3)
	test(t, "non-number", false, v.Number(), "")
	test(t, "nil", false, v.Number(), nil)
	test(t, "nil int pointer", false, v.Number(), np)
	test(t, "int pointer", true, v.Number(), &nnp)

	test(t, "numis", true, v.Number(v.NumIs(3)), 3)
	test(t, "!numis", false, v.Number(v.NumIs(3)), 2)

	test(t, "minlen1", false, v.Number(v.NumMin(3)), 2)
	test(t, "minlen2", true, v.Number(v.NumMin(3)), 3)
	test(t, "minlen2", true, v.Number(v.NumMin(3)), 4)

	test(t, "maxlen1", true, v.Number(v.NumMax(3)), 2)
	test(t, "maxlen2", true, v.Number(v.NumMax(3)), 3)
	test(t, "maxlen3", false, v.Number(v.NumMax(3)), 4)

	test(t, "combination1", false, v.Number(v.NumMin(3), v.NumMax(3)), 2)
	test(t, "combination2", true, v.Number(v.NumMin(3), v.NumMax(3)), 3)
	test(t, "combination3", false, v.Number(v.NumMin(3), v.NumMax(3)), 4)
}
Example #4
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)
	}
}
Example #5
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())),
			)),
		)),
	)
}