Exemple #1
0
func TestNoMergeExtensionMerge(t *testing.T) {
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	bigm := test.NewPopulatedMyExtendable(r, true)
	m := test.NewPopulatedNinOptNative(r, true)
	err := proto.SetExtension(bigm, test.E_FieldB, m)
	if err != nil {
		panic(err)
	}
	data, err := proto.Marshal(bigm)
	if err != nil {
		panic(err)
	}
	key := uint32(101)<<3 | uint32(2)
	data2 := make([]byte, 10)
	n := binary.PutUvarint(data2, uint64(key))
	data2 = data2[:n]
	data = append(data, data2...)
	data4, err := proto.Marshal(test.NewPopulatedNinOptNative(r, true))
	if err != nil {
		panic(err)
	}
	data3 := make([]byte, 10)
	n = binary.PutUvarint(data3, uint64(len(data4)))
	data3 = data3[:n]
	data = append(data, data3...)
	data = append(data, data4...)
	err = fieldpath.NoMerge(data, test.ThetestDescription(), "test", "MyExtendable")
	if err == nil || !strings.Contains(err.Error(), "requires merging") {
		t.Fatalf("should require merging")
	}
}
Exemple #2
0
func TestNoMergeNoMerge(t *testing.T) {
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	m := test.NewPopulatedNinOptNative(r, true)
	data, err := proto.Marshal(m)
	if err != nil {
		panic(err)
	}
	err = fieldpath.NoMerge(data, test.ThetestDescription(), "test", "NinOptNative")
	if err != nil {
		panic(err)
	}
}
Exemple #3
0
func TestNoMergeMessageMerge(t *testing.T) {
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	bigm := test.NewPopulatedNidOptStruct(r, true)
	data, err := proto.Marshal(bigm)
	if err != nil {
		panic(err)
	}
	key := byte(uint32(4)<<3 | uint32(2))
	data = append(data, key, 5, byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)))
	err = fieldpath.NoMerge(data, test.ThetestDescription(), "test", "NidOptStruct")
	if err == nil || !strings.Contains(err.Error(), "requires merging") {
		panic(err)
	}
}
Exemple #4
0
func TestNoMergeMerge(t *testing.T) {
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	m := test.NewPopulatedNinOptNative(r, true)
	if m.Field1 == nil {
		m.Field1 = proto.Float64(1.1)
	}
	data, err := proto.Marshal(m)
	if err != nil {
		panic(err)
	}
	key := byte(uint32(1)<<3 | uint32(1))
	data = append(data, key, byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)))
	err = fieldpath.NoMerge(data, test.ThetestDescription(), "test", "NinOptNative")
	if err == nil || !strings.Contains(err.Error(), "NinOptNative.Field1 requires merging") {
		t.Fatalf("Field1 should require merging")
	}
}
Exemple #5
0
func TestNoMergeExtensionNoMerge(t *testing.T) {
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	bigm := test.NewPopulatedMyExtendable(r, true)
	m := test.NewPopulatedNinOptNative(r, true)
	err := proto.SetExtension(bigm, test.E_FieldB, m)
	if err != nil {
		panic(err)
	}
	data, err := proto.Marshal(bigm)
	if err != nil {
		panic(err)
	}
	err = fieldpath.NoMerge(data, test.ThetestDescription(), "test", "MyExtendable")
	if err != nil {
		panic(err)
	}
}
Exemple #6
0
func assert(s string, i int, input []byte, msg message, marshal marshalFunc, unmarshal unmarshalFunc) []byte {
	if err := unmarshal(input, msg); err != nil {
		debug(s+" unmarshal1", i, input, err)
	}
	output, err := marshal(msg)
	if err != nil {
		panic(err)
	}
	//check the length whenever it is possible, since I found most of the bugs this way.
	if hasUnrecognized(msg) && !hasPacked(msg) {
		s := reflect.TypeOf(msg).Elem()
		pkgPath := s.PkgPath()
		pkgPaths := strings.Split(pkgPath, "/")
		pkgName := pkgPaths[len(pkgPaths)-1]
		msgName := s.Name()
		descriptorSet := gogopop.NewFuncs[i]().(interface {
			Description() *descriptor.FileDescriptorSet
		}).Description()
		//if a field was merged then the length would have changed.
		if err := fieldpath.NoMerge(input, descriptorSet, pkgName, msgName); err == nil {
			//only length is checked since field orders can change
			if len(input) != len(output) {
				panic(fmt.Sprintf("[%d](%T):%s length has changed input %#v output %#v", i, msg, s, input, output))
			}
		}
	}
	msg.Reset()
	if err := unmarshal(output, msg); err != nil {
		debug(s+" unmarshal2", i, output, err)
	}
	output2, err := marshal(msg)
	if err != nil {
		panic(err)
	}
	if !bytes.Equal(output, output2) {
		panic(fmt.Sprintf("[%d](%T):%s is not idempotent input %#v output %#v", i, msg, s, output, output2))
	}
	return output
}