Example #1
0
func IsAnyBinaryB32(outOptional *[]byte) btesting.EvalFunction {
	return func(evaluator btesting.Evaluator, actual interface{}) {
		if actual == nil {
			evaluator.T().Errorf("Expecting a binary but got nil")
			return
		}
		var theBinary []byte
		switch actual := actual.(type) {
		case []byte:
			theBinary = actual
		case string:
			var err error
			theBinary, err = typing.Base32Decode(actual)
			if err != nil {
				evaluator.T().Errorf("%v", err)
				return
			}
		default:
			evaluator.T().Errorf("Unknown type (valid: binary or base32 string): %T", actual)
			return
		}

		if outOptional != nil {
			*outOptional = theBinary
		}
	}
}
Example #2
0
func Key(e btesting.Evaluator, expecting interface{}, actual interface{}) {
	obj, ok := expecting.(typing.Key)
	if !ok {
		e.T().Errorf("Unknown type, need typing.Key")
		return
	}

	switch actual := actual.(type) {
	case [][]byte:
		e.Evaluate(obj.ToBinary(), actual)
	case []string:
		e.Evaluate(obj.ToBase32Slice(), actual)
	case []interface{}:
		if len(actual) != len(obj) {
			e.T().Errorf("Expecting a key with %v elements but have only %v elements",
				len(obj), len(actual))
			return
		}
		for index, actualElement := range actual {
			switch actualElement := actualElement.(type) {
			case string:
				binaryElement, err := typing.Base32Decode(actualElement)
				if err != nil {
					e.T().Errorf("Error decoding key element: %v", err)
					return
				}
				e.Evaluate(obj[index], binaryElement)
				if e.T().Failed() {
					return
				}
			case []byte:
				e.Evaluate(obj[index], actualElement)
				if e.T().Failed() {
					return
				}
			default:
				e.T().Errorf("Unknown key element type (expect string or []byte): %T", actualElement)
				return
			}
		}
	default:
		e.T().Errorf("Unknown 'actual' type. Need [][]byte or []string, have: %T", actual)
	}
}