| 1 | package testutils |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "bytes" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "io" |
| 9 | "net/textproto" |
| 10 | "strings" |
| 11 | ) |
| 12 | |
| 13 | // Eavesdropper is an interface that provides a way to get a summarized output of a connection RX and TX. |
| 14 | type Eavesdropper interface { |
| 15 | GetConversation(includeGreeting bool) string |
| 16 | GetClientSentences() []string |
| 17 | } |
| 18 | |
| 19 | type ioFaker struct { |
| 20 | io.ReadWriter |
| 21 | } |
| 22 | |
| 23 | type failWriter struct { |
| 24 | writeLimit int |
| 25 | writeCount int |
| 26 | } |
| 27 | |
| 28 | type textConFaker struct { |
| 29 | inputBuffer *bytes.Buffer |
| 30 | inputWriter *bufio.Writer |
| 31 | outputReader *bufio.Reader |
| 32 | responses []string |
| 33 | delim string |
| 34 | } |
| 35 | |
| 36 | // ErrWriteLimitReached is returned when the write limit has been reached. |
| 37 | var ErrWriteLimitReached = errors.New("reached write limit") |
| 38 | |
| 39 | // Close is just a dummy function to implement the io.Closer interface. |
| 40 | func (iof ioFaker) Close() error { |
| 41 | return nil |
| 42 | } |
| 43 | |
| 44 | // Close is just a dummy function to implement io.Closer. |
| 45 | func (fw *failWriter) Close() error { |
| 46 | return nil |
| 47 | } |
| 48 | |
| 49 | // Write returns an error if the write limit has been reached. |
| 50 | func (fw *failWriter) Write(data []byte) (int, error) { |
| 51 | fw.writeCount++ |
| 52 | if fw.writeCount > fw.writeLimit { |
| 53 | return 0, fmt.Errorf("%w: %d", ErrWriteLimitReached, fw.writeLimit) |
| 54 | } |
| 55 | |
| 56 | return len(data), nil |
| 57 | } |
| 58 | |
| 59 | // CreateFailWriter returns a io.WriteCloser that returns an error after the amount of writes indicated by writeLimit. |
| 60 | func CreateFailWriter(writeLimit int) io.WriteCloser { |
| 61 | return &failWriter{ |
| 62 | writeLimit: writeLimit, |
| 63 | writeCount: 0, |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // CreateReadWriter returns a ReadWriter from the textConFakers internal reader and writer. |
| 68 | func (tcf *textConFaker) CreateReadWriter() *bufio.ReadWriter { |
| 69 | return bufio.NewReadWriter(tcf.outputReader, tcf.inputWriter) |
| 70 | } |
| 71 | |
| 72 | // GetClientSentences returns all the input received from the client separated by the delimiter. |
| 73 | func (tcf *textConFaker) GetClientSentences() []string { |
| 74 | _ = tcf.inputWriter.Flush() |
| 75 | |
| 76 | return strings.Split(tcf.inputBuffer.String(), tcf.delim) |
| 77 | } |
| 78 | |
| 79 | // GetConversation returns the input and output streams as a conversation. |
| 80 | func (tcf *textConFaker) GetConversation(includeGreeting bool) string { |
| 81 | conv := "" |
| 82 | inSequence := false |
| 83 | input := strings.Split(tcf.GetInput(), tcf.delim) |
| 84 | responseIndex := 0 |
| 85 | |
| 86 | if includeGreeting { |
| 87 | conv += fmt.Sprintf(" %-55s << %-50s\n", "(server greeting)", tcf.responses[0]) |
| 88 | responseIndex = 1 |
| 89 | } |
| 90 | |
| 91 | // conversationBuilder accumulates the conversation output in correct order: main lines followed by their continuations. |
| 92 | var conversationBuilder strings.Builder |
| 93 | |
| 94 | for i, query := range input { |
| 95 | if query == "." { |
| 96 | inSequence = false |
| 97 | } |
| 98 | |
| 99 | resp := "" |
| 100 | if len(tcf.responses) > responseIndex && !inSequence { |
| 101 | resp = tcf.responses[responseIndex] |
| 102 | } |
| 103 | |
| 104 | if query == "" && resp == "" && i == len(input)-1 { |
| 105 | break |
| 106 | } |
| 107 | |
| 108 | fmt.Fprintf(&conversationBuilder, " #%2d >> %50s << %-50s\n", i, query, resp) |
| 109 | |
| 110 | for len(resp) > 3 && resp[3] == '-' { |
| 111 | responseIndex++ |
| 112 | if responseIndex >= len(tcf.responses) { |
| 113 | break |
| 114 | } |
| 115 | |
| 116 | resp = tcf.responses[responseIndex] |
| 117 | fmt.Fprintf(&conversationBuilder, " %50s << %-50s\n", " ", resp) |
| 118 | } |
| 119 | |
| 120 | if !inSequence { |
| 121 | responseIndex++ |
| 122 | } |
| 123 | |
| 124 | if resp != "" && resp[0] == '3' { |
| 125 | inSequence = true |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | conv += conversationBuilder.String() |
| 130 | |
| 131 | return conv |
| 132 | } |
| 133 | |
| 134 | func (tcf *textConFaker) GetInput() string { |
| 135 | _ = tcf.inputWriter.Flush() |
| 136 | |
| 137 | return tcf.inputBuffer.String() |
| 138 | } |
| 139 | |
| 140 | func (tcf *textConFaker) init() { |
| 141 | tcf.inputBuffer = &bytes.Buffer{} |
| 142 | stringReader := strings.NewReader(strings.Join(tcf.responses, tcf.delim)) |
| 143 | tcf.outputReader = bufio.NewReader(stringReader) |
| 144 | tcf.inputWriter = bufio.NewWriter(tcf.inputBuffer) |
| 145 | } |
| 146 | |
| 147 | // CreateTextConFaker returns a textproto.Conn to fake textproto based connections. |
| 148 | func CreateTextConFaker(responses []string, delim string) (*textproto.Conn, Eavesdropper) { |
| 149 | tcfaker := textConFaker{ |
| 150 | inputBuffer: nil, |
| 151 | inputWriter: nil, |
| 152 | outputReader: nil, |
| 153 | responses: responses, |
| 154 | delim: delim, |
| 155 | } |
| 156 | tcfaker.init() |
| 157 | |
| 158 | faker := ioFaker{ |
| 159 | ReadWriter: tcfaker.CreateReadWriter(), |
| 160 | } |
| 161 | |
| 162 | return textproto.NewConn(faker), &tcfaker |
| 163 | } |