| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package butlerproto |
| 6 |
| 7 import ( |
| 8 "bytes" |
| 9 "io" |
| 10 "strings" |
| 11 "testing" |
| 12 |
| 13 "github.com/golang/protobuf/proto" |
| 14 "github.com/luci/luci-go/common/logdog/protocol" |
| 15 "github.com/luci/luci-go/common/recordio" |
| 16 . "github.com/smartystreets/goconvey/convey" |
| 17 ) |
| 18 |
| 19 func read(ir io.Reader) (*Reader, error) { |
| 20 r := Reader{} |
| 21 if err := r.Read(ir); err != nil { |
| 22 return nil, err |
| 23 } |
| 24 return &r, nil |
| 25 } |
| 26 |
| 27 func TestReader(t *testing.T) { |
| 28 Convey(`A Reader instance`, t, func() { |
| 29 r := Reader{} |
| 30 buf := bytes.Buffer{} |
| 31 fw := recordio.NewWriter(&buf) |
| 32 |
| 33 writeFrame := func(data []byte) error { |
| 34 if _, err := fw.Write(data); err != nil { |
| 35 return err |
| 36 } |
| 37 if err := fw.Flush(); err != nil { |
| 38 return err |
| 39 } |
| 40 return nil |
| 41 } |
| 42 |
| 43 push := func(m proto.Message) error { |
| 44 data, err := proto.Marshal(m) |
| 45 if err != nil { |
| 46 return err |
| 47 } |
| 48 return writeFrame(data) |
| 49 } |
| 50 |
| 51 Convey(`Can read a ButlerLogBundle entry.`, func() { |
| 52 md := protocol.ButlerMetadata{ |
| 53 Type: protocol.ButlerMetadata_ButlerLogBundle, |
| 54 } |
| 55 bundle := protocol.ButlerLogBundle{ |
| 56 Source: "test source", |
| 57 } |
| 58 |
| 59 So(push(&md), ShouldBeNil) |
| 60 So(push(&bundle), ShouldBeNil) |
| 61 |
| 62 So(r.Read(&buf), ShouldBeNil) |
| 63 So(r.Bundle, ShouldNotBeNil) |
| 64 So(r.Bundle.Source, ShouldEqual, "test source") |
| 65 }) |
| 66 |
| 67 Convey(`Will fail to Read an unknown type.`, func() { |
| 68 // Assert that we are testing an unknown type. |
| 69 unknownType := protocol.ButlerMetadata_ContentType(-1) |
| 70 So(protocol.ButlerMetadata_ContentType_name[int32(unknow
nType)], ShouldEqual, "") |
| 71 |
| 72 md := protocol.ButlerMetadata{ |
| 73 Type: unknownType, |
| 74 } |
| 75 So(push(&md), ShouldBeNil) |
| 76 |
| 77 err := r.Read(&buf) |
| 78 So(err, ShouldNotBeNil) |
| 79 So(err.Error(), ShouldContainSubstring, "unknown data ty
pe") |
| 80 }) |
| 81 |
| 82 Convey(`Will fail to read junk metadata.`, func() { |
| 83 So(writeFrame([]byte{0xd0, 0x6f, 0x00, 0xd5}), ShouldBeN
il) |
| 84 |
| 85 err := r.Read(&buf) |
| 86 So(err, ShouldNotBeNil) |
| 87 So(err.Error(), ShouldContainSubstring, "failed to unmar
shal Metadata frame") |
| 88 }) |
| 89 |
| 90 Convey(`With a proper Metadata frame`, func() { |
| 91 md := protocol.ButlerMetadata{ |
| 92 Type: protocol.ButlerMetadata_ButlerLogBundle, |
| 93 } |
| 94 So(push(&md), ShouldBeNil) |
| 95 |
| 96 Convey(`Will fail if the bundle data is junk.`, func() { |
| 97 So(writeFrame([]byte{0xd0, 0x6f, 0x00, 0xd5}), S
houldBeNil) |
| 98 |
| 99 err := r.Read(&buf) |
| 100 So(err, ShouldNotBeNil) |
| 101 So(err.Error(), ShouldContainSubstring, "failed
to unmarshal Bundle frame") |
| 102 }) |
| 103 }) |
| 104 |
| 105 Convey(`With a proper compressed Metadata frame`, func() { |
| 106 md := protocol.ButlerMetadata{ |
| 107 Type: protocol.ButlerMetadata_ButlerLogBu
ndle, |
| 108 Compression: protocol.ButlerMetadata_ZLIB, |
| 109 } |
| 110 So(push(&md), ShouldBeNil) |
| 111 |
| 112 Convey(`Will fail if the data frame is missing.`, func()
{ |
| 113 err := r.Read(&buf) |
| 114 So(err, ShouldNotBeNil) |
| 115 So(err.Error(), ShouldContainSubstring, "failed
to read Bundle data") |
| 116 }) |
| 117 |
| 118 Convey(`Will fail if there is junk compressed data.`, fu
nc() { |
| 119 So(writeFrame(bytes.Repeat([]byte{0x55, 0xAA}, 1
6)), ShouldBeNil) |
| 120 |
| 121 err := r.Read(&buf) |
| 122 So(err, ShouldNotBeNil) |
| 123 So(err.Error(), ShouldContainSubstring, "failed
to initialize zlib reader") |
| 124 }) |
| 125 }) |
| 126 |
| 127 Convey(`Will refuse to read a frame larger than our maximum size
.`, func() { |
| 128 r.maxSize = 16 |
| 129 So(writeFrame(bytes.Repeat([]byte{0x55}, 17)), ShouldBeN
il) |
| 130 |
| 131 err := r.Read(&buf) |
| 132 So(err, ShouldEqual, recordio.ErrFrameTooLarge) |
| 133 }) |
| 134 |
| 135 Convey(`Will refuse to read a compressed protobuf larger than ou
r maximum size.`, func() { |
| 136 // We are crafting this data such that its compressed (f
rame) size is |
| 137 // below our threshold (16), but its compressed size exc
eeds it. Repeated |
| 138 // bytes compress very well :) |
| 139 // |
| 140 // Because the frame it smaller than our threshold, our
FrameReader will |
| 141 // not outright reject the frame. However, the data is s
till larger than |
| 142 // we're allowed, and we must reject it. |
| 143 r.maxSize = 16 |
| 144 w := Writer{ |
| 145 Compress: true, |
| 146 CompressThreshold: 0, |
| 147 } |
| 148 So(w.writeData(recordio.NewWriter(&buf), protocol.Butler
Metadata_ButlerLogBundle, |
| 149 bytes.Repeat([]byte{0x55}, 64)), ShouldBeNil) |
| 150 |
| 151 err := r.Read(&buf) |
| 152 So(err, ShouldNotBeNil) |
| 153 So(err.Error(), ShouldContainSubstring, "limit exceeded"
) |
| 154 }) |
| 155 }) |
| 156 } |
| 157 |
| 158 func TestWriter(t *testing.T) { |
| 159 Convey(`A Writer instance outputting to a Buffer`, t, func() { |
| 160 buf := bytes.Buffer{} |
| 161 w := Writer{} |
| 162 bundle := protocol.ButlerLogBundle{ |
| 163 Source: "test source", |
| 164 Entries: []*protocol.ButlerLogBundle_Entry{ |
| 165 {}, |
| 166 }, |
| 167 } |
| 168 |
| 169 Convey(`When configured to compress with a threshold of 64`, fun
c() { |
| 170 w.Compress = true |
| 171 w.CompressThreshold = 64 |
| 172 |
| 173 Convey(`Will not compress if below the compression thres
hold.`, func() { |
| 174 So(w.Write(&buf, &bundle), ShouldBeNil) |
| 175 |
| 176 r, err := read(&buf) |
| 177 So(err, ShouldBeNil) |
| 178 So(r.Metadata.Compression, ShouldEqual, protocol
.ButlerMetadata_NONE) |
| 179 }) |
| 180 |
| 181 Convey(`Will not write data larger than the maximum bund
le size.`, func() { |
| 182 w.maxSize = 16 |
| 183 bundle.Source = strings.Repeat("A", 17) |
| 184 err := w.Write(&buf, &bundle) |
| 185 So(err, ShouldNotBeNil) |
| 186 So(err.Error(), ShouldContainSubstring, "exceeds
soft cap") |
| 187 }) |
| 188 |
| 189 Convey(`Will compress data >= the threshold.`, func() { |
| 190 bundle.Source = strings.Repeat("A", 64) |
| 191 So(w.Write(&buf, &bundle), ShouldBeNil) |
| 192 |
| 193 r, err := read(&buf) |
| 194 So(err, ShouldBeNil) |
| 195 So(r.Metadata.Compression, ShouldEqual, protocol
.ButlerMetadata_ZLIB) |
| 196 So(r.Bundle.Source, ShouldResemble, bundle.Sourc
e) |
| 197 |
| 198 Convey(`And can be reused.`, func() { |
| 199 bundle.Source = strings.Repeat("A", 64) |
| 200 So(w.Write(&buf, &bundle), ShouldBeNil) |
| 201 |
| 202 r, err := read(&buf) |
| 203 So(err, ShouldBeNil) |
| 204 So(r.Metadata.Compression, ShouldEqual,
protocol.ButlerMetadata_ZLIB) |
| 205 So(r.Bundle.Source, ShouldEqual, bundle.
Source) |
| 206 }) |
| 207 }) |
| 208 }) |
| 209 }) |
| 210 } |
| OLD | NEW |