go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/auth/integration/devshell/server_test.go (about) 1 // Copyright 2017 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package devshell 16 17 import ( 18 "bytes" 19 "context" 20 "fmt" 21 "io" 22 "net" 23 "strconv" 24 "strings" 25 "testing" 26 "time" 27 28 "golang.org/x/oauth2" 29 30 "go.chromium.org/luci/common/clock" 31 "go.chromium.org/luci/common/clock/testclock" 32 33 . "github.com/smartystreets/goconvey/convey" 34 ) 35 36 func TestProtocol(t *testing.T) { 37 t.Parallel() 38 39 ctx := context.Background() 40 ctx, _ = testclock.UseTime(ctx, testclock.TestRecentTimeUTC) 41 42 Convey("With server", t, func(c C) { 43 s := Server{ 44 Source: oauth2.StaticTokenSource(&oauth2.Token{ 45 AccessToken: "tok1", 46 Expiry: clock.Now(ctx).Add(30 * time.Minute), 47 }), 48 Email: "some@example.com", 49 } 50 p, err := s.Start(ctx) 51 So(err, ShouldBeNil) 52 defer s.Stop(ctx) 53 54 conn, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", p.Port)) 55 if err != nil { 56 panic(err) 57 } 58 59 Convey("Happy path", func() { 60 So(call(conn, "[]"), ShouldEqual, `["some@example.com",null,"tok1",1800]`) 61 }) 62 63 Convey("Wrong format", func() { 64 So(call(conn, "{BADJSON"), ShouldEqual, `["failed to deserialize from JSON: invalid character 'B' looking for beginning of object key string"]`) 65 }) 66 }) 67 } 68 69 func call(conn net.Conn, req string) string { 70 var buf bytes.Buffer 71 buf.WriteString(fmt.Sprintf("%d\n", len(req))) 72 buf.Write([]byte(req)) 73 if _, err := conn.Write(buf.Bytes()); err != nil { 74 panic(err) 75 } 76 77 blob, err := io.ReadAll(conn) 78 if err != nil { 79 panic(err) 80 } 81 82 str := strings.SplitN(string(blob), "\n", 2) 83 if len(str) != 2 { 84 panic(err) 85 } 86 87 _, err = strconv.Atoi(str[0]) 88 if err != nil { 89 panic(err) 90 } 91 92 return str[1] 93 }