validate_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2019 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package proto_test
  5. import (
  6. "fmt"
  7. "testing"
  8. "google.golang.org/protobuf/internal/impl"
  9. piface "google.golang.org/protobuf/runtime/protoiface"
  10. )
  11. // TestValidate tests the internal message validator.
  12. //
  13. // Despite being more properly associated with the internal/impl package,
  14. // it is located here to take advantage of the test wire encoder/decoder inputs.
  15. func TestValidateValid(t *testing.T) {
  16. for _, test := range testValidMessages {
  17. for _, m := range test.decodeTo {
  18. t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) {
  19. mt := m.ProtoReflect().Type()
  20. want := impl.ValidationValid
  21. if test.validationStatus != 0 {
  22. want = test.validationStatus
  23. }
  24. out, status := impl.Validate(mt, piface.UnmarshalInput{
  25. Buf: test.wire,
  26. })
  27. if status != want {
  28. t.Errorf("Validate(%x) = %v, want %v", test.wire, status, want)
  29. }
  30. if got, want := (out.Flags&piface.UnmarshalInitialized != 0), !test.partial; got != want && !test.nocheckValidInit && status == impl.ValidationValid {
  31. t.Errorf("Validate(%x): initialized = %v, want %v", test.wire, got, want)
  32. }
  33. })
  34. }
  35. }
  36. }
  37. func TestValidateInvalid(t *testing.T) {
  38. for _, test := range testInvalidMessages {
  39. for _, m := range test.decodeTo {
  40. t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) {
  41. mt := m.ProtoReflect().Type()
  42. _, got := impl.Validate(mt, piface.UnmarshalInput{
  43. Buf: test.wire,
  44. })
  45. want := impl.ValidationInvalid
  46. if got != want {
  47. t.Errorf("Validate(%x) = %v, want %v", test.wire, got, want)
  48. }
  49. })
  50. }
  51. }
  52. }