Exemple #1
0
func ValidateConfigBase(v interface{}, param string) error {
	st := reflect.ValueOf(v)

	if st.Interface() == reflect.Zero(st.Type()).Interface() {
		ce := &confutils.ConfigError{
			Type: confutils.BaseType,
			Id:   param,
		}
		errStr, _ := confutils.GetErrExpln(ce.Type, ce.Id, validator.ErrZeroValue)
		ce.Errors = []error{errors.New(errStr)}
		return ce
	}

	if st.Kind() == reflect.Int {
		if reflect.ValueOf(st.Interface()).Int() < 0 {
			ce := &confutils.ConfigError{
				Type: confutils.BaseType,
				Id:   param,
			}
			errStr, _ := confutils.GetErrExpln(ce.Type, ce.Id, validator.ErrMin)
			ce.Errors = []error{errors.New(errStr)}
			return ce
		}
	}

	return nil
}
Exemple #2
0
func TestEmptyFileErrors(t *testing.T) {
	_, err := LoadConfig(EmptyFile)

	if len(err) == 0 {
		t.Fatalf("Validated an empty configuration file!\n")
	}

	usersEmptyErr, _ := confutils.GetErrExpln(confutils.UserType, "Users", validator.ErrZeroValue)
	caPathEmptyErr, _ := confutils.GetErrExpln(confutils.BaseType, "CAPath", validator.ErrZeroValue)

	expected := []error{
		&confutils.ConfigError{
			Type: confutils.UserType,
			Id:   "Users",
			Errors: []error{
				errors.New(usersEmptyErr),
			},
		},
		&confutils.ConfigError{
			Type: confutils.BaseType,
			Id:   "CAPath",
			Errors: []error{
				errors.New(caPathEmptyErr),
			},
		},
	}

	if len(err) != len(expected) {
		t.Fatalf("Invalid number of errors returned for %s.\nExpected: %d\nGot: %d\n", PartialFile, len(expected), len(err))
	}

	if !containsAll(expected, err) {
		t.Fatalf("Returned errors not equal to expected errors.\nExpected: %v\nGot: %v\n", expected, err)
	}
}
Exemple #3
0
func TestPartialFileErrors(t *testing.T) {
	_, err := LoadConfig(PartialFile)

	if len(err) == 0 {
		t.Fatalf("Validated a config with errors.")
	}

	adapterEmptyErr, _ := confutils.GetErrExpln(confutils.UserType, "Logging.Adapter", validator.ErrZeroValue)
	dbEmptyErr, _ := confutils.GetErrExpln(confutils.UserType, "Logging.Database", validator.ErrZeroValue)
	nickEmptyErr, _ := confutils.GetErrExpln(confutils.UserType, "Nick", validator.ErrZeroValue)
	altNickEmptyErr, _ := confutils.GetErrExpln(confutils.UserType, "AltNick", validator.ErrZeroValue)
	certsEmptyErr, _ := confutils.GetErrExpln(confutils.CertType, "Certs", validator.ErrZeroValue)
	caPathEmptyErr, _ := confutils.GetErrExpln(confutils.BaseType, "CAPath", validator.ErrZeroValue)
	netBlockEmptyErr, _ := confutils.GetErrExpln(confutils.NetworkType, "Networks", validator.ErrZeroValue)

	// TODO: Create generators(?) for these monsters.
	expUserErrors := &confutils.ConfigError{
		Type: confutils.UserType,
		Id:   "Users",
		Errors: []error{
			&confutils.ConfigError{
				Type: confutils.UserType,
				Id:   "zamn",
				Errors: []error{
					&confutils.ConfigError{
						Type: confutils.NetworkType,
						Id:   "Networks",
						Errors: []error{
							errors.New(netBlockEmptyErr),
						},
					},
					&confutils.ConfigError{
						Type: confutils.CertType,
						Id:   "Certs",
						Errors: []error{
							errors.New(certsEmptyErr),
						},
					},
					&confutils.ConfigError{
						Type: confutils.UserType,
						Id:   "Logging.Adapter",
						Errors: []error{
							errors.New(adapterEmptyErr),
						},
					},
					&confutils.ConfigError{
						Type: confutils.UserType,
						Id:   "Logging.Database",
						Errors: []error{
							errors.New(dbEmptyErr),
						},
					},
					&confutils.ConfigError{
						Type: confutils.UserType,
						Id:   "Nick",
						Errors: []error{
							errors.New(nickEmptyErr),
						},
					},
					&confutils.ConfigError{
						Type: confutils.UserType,
						Id:   "AltNick",
						Errors: []error{
							errors.New(altNickEmptyErr),
						},
					},
				},
			},
		},
	}

	expected := []error{
		&confutils.ConfigError{
			Type: confutils.BaseType,
			Id:   "CAPath",
			Errors: []error{
				errors.New(caPathEmptyErr),
			},
		},
		expUserErrors,
	}

	// Although this is checking at the baseline level, good enough
	if len(err) != len(expected) {
		t.Fatalf("Invalid number of errors returned for %s.\nExpected: %d\nGot: %d\n", PartialFile, len(expected), len(err))
	}

	if !containsAll(expected, err) {
		t.Fatalf("Returned errors not equal to expected errors.\nExpected: %v\nGot: %v\n", expected, err)
	}
}
Exemple #4
0
func TestNetworkErrors(t *testing.T) {
	_, err := LoadConfig(BadNetworkFile)

	if err == nil {
		t.Fatalf("Error(s) not found in bad networks config.")
	}

	servMinErr, _ := confutils.GetErrExpln(confutils.NetworkType, "Servers", validator.ErrMin)
	nameEmptyErr, _ := confutils.GetErrExpln(confutils.NetworkType, "Name", validator.ErrZeroValue)
	caPathEmptyErr, _ := confutils.GetErrExpln(confutils.BaseType, "CAPath", validator.ErrZeroValue)

	expUserErrors := &confutils.ConfigError{
		Type: confutils.UserType,
		Id:   "Users",
		Errors: []error{
			&confutils.ConfigError{
				Type: confutils.UserType,
				Id:   "zamn",
				Errors: []error{
					&confutils.ConfigError{
						Type: confutils.NetworkType,
						Id:   "Networks",
						Errors: []error{
							&confutils.ConfigError{
								Type: confutils.NetworkType,
								Id:   "GameSurge",
								Errors: []error{
									&confutils.ConfigError{
										Type: confutils.NetworkType,
										Id:   "Name",
										Errors: []error{
											errors.New(nameEmptyErr),
										},
									},
									&confutils.ConfigError{
										Type: confutils.NetworkType,
										Id:   "Servers",
										Errors: []error{
											errors.New(servMinErr),
										},
									},
								},
							},
						},
					},
				},
			},
		},
	}
	expected := []error{
		&confutils.ConfigError{
			Type: confutils.BaseType,
			Id:   "CAPath",
			Errors: []error{
				errors.New(caPathEmptyErr),
			},
		},
		expUserErrors,
	}

	if len(err) != len(expected) {
		t.Fatalf("Invalid number of errors returned for %s.\nExpected: %d\nGot: %d\n", PartialFile, len(expected), len(err))
	}

	if !containsAll(expected, err) {
		t.Fatalf("Returned errors not equal to expected errors.\nExpected: %v\nGot: %v\n", expected, err)
	}
}