Example #1
0
func TestJsonFullCircleProtoToStd(t *testing.T) {
	seed := time.Now().UnixNano()
	popr := math_rand.New(math_rand.NewSource(seed))
	protoMsg := NewPopulatedProtoTypes(popr, true)
	j := &jsonpb.Marshaler{}
	protoData, err := j.MarshalToString(protoMsg)
	if err != nil {
		t.Fatal(err)
	}
	stdMsg := &StdTypes{}
	if err2 := jsonpb.UnmarshalString(protoData, stdMsg); err2 != nil {
		t.Fatal(err)
	}
	stdData, err := j.MarshalToString(stdMsg)
	if err != nil {
		t.Fatal(err)
	}
	protoMsgOut := &ProtoTypes{}
	if err3 := jsonpb.UnmarshalString(stdData, protoMsgOut); err3 != nil {
		t.Fatal(err)
	}
	if !protoMsg.Equal(protoMsgOut) {
		t.Fatalf("want %#v got %#v", protoMsg, protoMsgOut)
	}
}
Example #2
0
func TestMoreDefaultsBJSON(t *testing.T) {
	seed := time.Now().UnixNano()
	popr := math_rand.New(math_rand.NewSource(seed))
	p := NewPopulatedMoreDefaultsB(popr, true)
	marshaler := github_com_maditya_protobuf_jsonpb.Marshaler{}
	jsondata, err := marshaler.MarshalToString(p)
	if err != nil {
		t.Fatalf("seed = %d, err = %v", seed, err)
	}
	msg := &MoreDefaultsB{}
	err = github_com_maditya_protobuf_jsonpb.UnmarshalString(jsondata, msg)
	if err != nil {
		t.Fatalf("seed = %d, err = %v", seed, err)
	}
	if !p.Equal(msg) {
		t.Fatalf("seed = %d, %#v !Json Equal %#v", seed, msg, p)
	}
}
Example #3
0
func TestEncodedSignedEntryUpdateJSON(t *testing.T) {
	popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano()))
	p := NewPopulatedEncodedSignedEntryUpdate(popr, true)
	marshaler := github_com_gogo_protobuf_jsonpb.Marshaler{}
	jsondata, err := marshaler.MarshalToString(p)
	if err != nil {
		t.Fatal(err)
	}
	msg := &EncodedSignedEntryUpdate{}
	err = github_com_gogo_protobuf_jsonpb.UnmarshalString(jsondata, msg)
	if err != nil {
		t.Fatal(err)
	}
	if err := p.VerboseEqual(msg); err != nil {
		t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err)
	}
	if !p.Equal(msg) {
		t.Fatalf("%#v !Json Equal %#v", msg, p)
	}
}
Example #4
0
func TestWilsonJSON(t *testing.T) {
	seed := time.Now().UnixNano()
	popr := math_rand.New(math_rand.NewSource(seed))
	p := NewPopulatedWilson(popr, true)
	marshaler := github_com_maditya_protobuf_jsonpb.Marshaler{}
	jsondata, err := marshaler.MarshalToString(p)
	if err != nil {
		t.Fatalf("seed = %d, err = %v", seed, err)
	}
	msg := &Wilson{}
	err = github_com_maditya_protobuf_jsonpb.UnmarshalString(jsondata, msg)
	if err != nil {
		t.Fatalf("seed = %d, err = %v", seed, err)
	}
	if err := p.VerboseEqual(msg); err != nil {
		t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
	}
	if !p.Equal(msg) {
		t.Fatalf("seed = %d, %#v !Json Equal %#v", seed, msg, p)
	}
}
Example #5
0
func handle(req *pb.ConformanceRequest) *pb.ConformanceResponse {
	var err error
	var msg pb.TestAllTypes
	switch p := req.Payload.(type) {
	case *pb.ConformanceRequest_ProtobufPayload:
		err = proto.Unmarshal(p.ProtobufPayload, &msg)
	case *pb.ConformanceRequest_JsonPayload:
		err = jsonpb.UnmarshalString(p.JsonPayload, &msg)
		if err != nil && err.Error() == "unmarshaling Any not supported yet" {
			return &pb.ConformanceResponse{
				Result: &pb.ConformanceResponse_Skipped{
					Skipped: err.Error(),
				},
			}
		}
	default:
		return &pb.ConformanceResponse{
			Result: &pb.ConformanceResponse_RuntimeError{
				RuntimeError: "unknown request payload type",
			},
		}
	}
	if err != nil {
		return &pb.ConformanceResponse{
			Result: &pb.ConformanceResponse_ParseError{
				ParseError: err.Error(),
			},
		}
	}
	switch req.RequestedOutputFormat {
	case pb.WireFormat_PROTOBUF:
		p, err := proto.Marshal(&msg)
		if err != nil {
			return &pb.ConformanceResponse{
				Result: &pb.ConformanceResponse_SerializeError{
					SerializeError: err.Error(),
				},
			}
		}
		return &pb.ConformanceResponse{
			Result: &pb.ConformanceResponse_ProtobufPayload{
				ProtobufPayload: p,
			},
		}
	case pb.WireFormat_JSON:
		p, err := jsonMarshaler.MarshalToString(&msg)
		if err != nil {
			return &pb.ConformanceResponse{
				Result: &pb.ConformanceResponse_SerializeError{
					SerializeError: err.Error(),
				},
			}
		}
		return &pb.ConformanceResponse{
			Result: &pb.ConformanceResponse_JsonPayload{
				JsonPayload: p,
			},
		}
	default:
		return &pb.ConformanceResponse{
			Result: &pb.ConformanceResponse_RuntimeError{
				RuntimeError: "unknown output format",
			},
		}
	}
}
Example #6
0
func TestKeyserverHTTPFrontLookup(t *testing.T) {
	kss, caPool, clks, verifiers, ck, clientConfig, teardown := setupRealm(t, 3, 3)
	ks := kss[0]
	defer teardown()
	stop := stoppableSyncedClocks(clks)
	defer close(stop)
	waitForFirstEpoch(kss[0], clientConfig.Realms[0].VerificationPolicy.GetQuorum())

	clientTLS, err := clientConfig.Realms[0].ClientTLS.Config(ck)
	if err != nil {
		t.Fatal(err)
	}

	pgpKeyRef := []byte("this-is-alices-pgp-key")
	_, _, _, profile := doRegister(t, ks, clientConfig, clientTLS, caPool, clks[0].Now(), alice, 0, proto.Profile{
		Nonce: []byte("definitely used only once"),
		Keys:  map[string][]byte{"pgp": pgpKeyRef},
	})

	url := "https://" + ks.httpFrontListen.Addr().String() + "/lookup"

	lr := &proto.LookupRequest{UserId: alice,
		QuorumRequirement: clientConfig.Realms[0].VerificationPolicy.GetQuorum()}

	var b bytes.Buffer

	mr := &jsonpb.Marshaler{OrigName: true}
	err = mr.Marshal(&b, lr)
	if err != nil {
		t.Fatal(err)
	}
	req, err := http.NewRequest("POST", url, &b)
	tr := &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
	}
	c := &http.Client{Transport: tr}
	resp, err := c.Do(req)
	if err != nil {
		t.Fatal(err)
	}
	defer resp.Body.Close()
	body, e := ioutil.ReadAll(resp.Body)
	if resp.Status != "200 OK" {
		t.Fatalf("%s (%s)", body, e)
	}
	proof := &proto.LookupProof{}

	err = jsonpb.UnmarshalString(string(body), proof)
	if err != nil {
		t.Fatal(err)
	}
	if got, want := proof.Profile.Encoding, profile.Encoding; !bytes.Equal(got, want) {
		t.Errorf("profile didn't roundtrip: %x != %x", got, want)
	}
	if got, want := len(proof.Ratifications), majority(len(kss))+len(verifiers); got < want {
		t.Errorf("expected at least %d sehs, got %d", got, want)
	}
	_, err = coname.VerifyLookup(clientConfig, alice, proof, clks[0].Now())
	if err != nil {
		t.Fatal(err)
	}

	// To verify json response preserves the original field names
	// as specified in https://github.com/yahoo/coname/blob/master/proto/client.proto#L63-L89
	type lp struct {
		Entry      string `json:"entry"`
		Index      string `json:"index"`
		IndexProof string `json:"index_proof"`
		Profile    string `json:"profile"`
		UserId     string `json:"user_id"`
	}
	l := &lp{}
	err = json.Unmarshal(body, &l)
	if err != nil {
		t.Fatal(err)
	}
	if l.Entry == "" {
		t.Errorf("entry not found in the response")
	}
	if l.Index == "" {
		t.Errorf("index not found in the response")
	}
	if l.IndexProof == "" {
		t.Errorf("index_proof not found in the response")
	}
	if l.Profile == "" {
		t.Errorf("profile not found in the response")
	}
	if l.UserId == "" {
		t.Errorf("user_id not found in the response")
	}
}