func TestMarshalUnmarshalBase64(t *testing.T) { var encbuf []byte var decbuf []byte t.Logf("start with buf %s\n", proto.CompactTextString(&cr)) t.Log("marshalling protobuf") buf, err := proto.Marshal(&cr) if err != nil { t.Error("marshal error: ", err) } t.Log("marshalled") t.Log("urlencoding") t.Logf("need %d size buffer\n", base64.URLEncoding.EncodedLen(len(buf)-1)) t.Log(buf) t.Logf("%v %s\n", buf, buf) encbuf = make([]byte, base64.URLEncoding.EncodedLen(len(buf)), base64.URLEncoding.EncodedLen(len(buf))) base64.URLEncoding.Encode(encbuf, buf) t.Log("urlencoded") t.Log("urldecoding") t.Logf("need %d size buffer\n", base64.URLEncoding.DecodedLen(len(encbuf))) t.Logf("%v %s\n", encbuf, encbuf) decbuf = make([]byte, base64.URLEncoding.DecodedLen(len(encbuf)), base64.URLEncoding.DecodedLen(len(encbuf))) n, err := base64.URLEncoding.Decode(decbuf, encbuf) t.Logf("wrote %d bytes from encbuf to decbuf. len(encbuf)=%d, len(buf)=%d\n", n, len(encbuf), len(buf)) if err != nil { t.Error("urldecode error: ", err) } t.Log("urldecoded") t.Log(buf, decbuf) rcr := &CheckResult{} t.Log("unmarshalling") err = proto.Unmarshal(decbuf, rcr) t.Logf("%s\n", proto.CompactTextString(rcr)) }
func TestJsonToProto(t *testing.T) { for i, v := range jsonTests { ncr := &CheckResult{} jsonBytes := []byte(v.json) t.Logf("test %d: unmarshalling json to protobuf struct", i) err := json.Unmarshal(jsonBytes, ncr) if err != nil { t.Errorf("test %d: %s", i, err) } if proto.CompactTextString(ncr) != proto.CompactTextString(v.cr) { t.Errorf("test %d: mismatch\noutput: %s\nexpected: %s", i, ncr, v.cr) } t.Logf("%s\n", proto.CompactTextString(ncr)) } }
// tryParse attempts to parse the input, and verifies that it matches // the FileDescriptorProto represented in text format. func tryParse(t *testing.T, input, output string) { expected := new(FileDescriptorProto) if err := proto.UnmarshalText(output, expected); err != nil { t.Fatalf("Test failure parsing an expected proto: %v", err) } actual := new(FileDescriptorProto) p := newParser(input) if pe := p.readFile(actual); pe != nil { t.Errorf("Failed parsing input: %v", pe) return } if !reflect.DeepEqual(expected, actual) { t.Errorf("Mismatch! Expected:\n%v\nActual\n%v", proto.CompactTextString(expected), proto.CompactTextString(actual)) } }
func root(w http.ResponseWriter, r *http.Request) { // check header if *flagPassword != "" && *flagUsername != "" { auth, ok := r.Header["Authorization"] if ok && strings.HasPrefix(auth[0], "Basic ") { str := strings.TrimLeft(auth[0], "Basic ") decode, err := base64.StdEncoding.DecodeString(str) if err != nil { log.Print("cannot decode auth string: ", err) return } user, pass, err := url.UnescapeUserinfo(string(decode)) if err != nil { log.Print("auth: couldn't decode user/pass: "******"auth: wrong user/pass: "******"/"+pass, *r) return } /* log.Printf("auth: %#v, user: %s, pass: %s", auth, user, pass)*/ } else { log.Print("auth: no authorization") return } } checkpb := new(CheckResultSet) if r.Method == "POST" { cout := new(bytes.Buffer) if _, err := cout.ReadFrom(r.Body); err != nil { log.Print("error! ", err) return } switch r.Header["Content-Type"][0] { case "application/x-protobuf": err := proto.Unmarshal(cout.Bytes(), checkpb) if err != nil { log.Printf("unmarshalling error: ", err) } case "application/json", "text/plain", "application/x-www-form-urlencoded", "multipart/form-data": err := json.Unmarshal(cout.Bytes(), checkpb) if err != nil { log.Printf("unmarshalling error: ", err) } } logger.Printf("check returned! %s", proto.CompactTextString(checkpb)) for _, v := range checkpb.Results { _, err := WriteCheck(v, *flagSpoolDir) if err != nil { logger.Print("writecheck failed: ", err) } } } else { /* logger.Printf("NOT POST!! %s", r.Method)*/ } templ.Execute(w, nil) }
func TestParsePerfDataElement(t *testing.T) { for i, v := range petests { pd, err := parsePerfDataElement(v.in) if err != nil { t.Errorf("test %d: parsePerfDataElement returned error: %v", i, err) } out := proto.CompactTextString(pd) if v.out != out { t.Errorf("test %d: mismatch:\noutput: %v\nexpected: %v", i, out, v.out) } } }
func main() { p := example.Person{ Name: proto.String("Taro Yamada"), Age: proto.Int32(8), } pet := example.Pet{Name: proto.String("Mike")} p.Pet = append(p.Pet, &pet) fmt.Println("-- p.String() --") fmt.Println(p.String()) fmt.Println("-- MarshalText --") fmt.Print(PrintToString(&p)) // not compact fmt.Println("-- Marshal --") m, _ := proto.Marshal(&p) fmt.Println(m) fmt.Println("-- CompactTextString --") fmt.Println(proto.CompactTextString(&p)) }
func (this *CreateLogoutURLResponse) String() string { return proto.CompactTextString(this) }
func (this *UserServiceError) String() string { return proto.CompactTextString(this) }
func (this *CreateFederatedLoginRequest) String() string { return proto.CompactTextString(this) }
func (this *Response) String() string { return proto.CompactTextString(this) }
func (this *Request) String() string { return proto.CompactTextString(this) }
func (this *ReadKeyValueResponse_KeyValue) String() string { return proto.CompactTextString(this) }
func (this *BytesProto) String() string { return proto.CompactTextString(this) }
func (this *DoubleProto) String() string { return proto.CompactTextString(this) }
func (this *Integer64Proto) String() string { return proto.CompactTextString(this) }
func (this *FileStat) String() string { return proto.CompactTextString(this) }
func (this *AppendKeyValueResponse) String() string { return proto.CompactTextString(this) }
func (this *VoidProto) String() string { return proto.CompactTextString(this) }
func (this *GetShuffleStatusResponse) String() string { return proto.CompactTextString(this) }
func (this *ShuffleEnums) String() string { return proto.CompactTextString(this) }
func (this *ApplicationError) String() string { return proto.CompactTextString(this) }
func (this *ShuffleOutputSpecification) String() string { return proto.CompactTextString(this) }
func (this *CheckOAuthSignatureResponse) String() string { return proto.CompactTextString(this) }
func (this *FileServiceErrors) String() string { return proto.CompactTextString(this) }
func (this *CreateFederatedLogoutResponse) String() string { return proto.CompactTextString(this) }
func (this *KeyValues) String() string { return proto.CompactTextString(this) }
func (this *CreateLoginURLRequest) String() string { return proto.CompactTextString(this) }
func (this *FileContentType) String() string { return proto.CompactTextString(this) }
func (this *GetOAuthUserRequest) String() string { return proto.CompactTextString(this) }
func (this *CreateRequest_Parameter) String() string { return proto.CompactTextString(this) }