From 800448e9bfeafc4c74ec5c362f757a686379b9d8 Mon Sep 17 00:00:00 2001 From: DimVlas Date: Wed, 25 Dec 2024 20:31:42 +0000 Subject: [PATCH 1/6] HW09 is completed. work on mistakes 2 --- hw09_struct_validator/rules/errors.go | 6 +----- hw09_struct_validator/validator.go | 12 ------------ hw09_struct_validator/validator_test.go | 1 - 3 files changed, 1 insertion(+), 18 deletions(-) diff --git a/hw09_struct_validator/rules/errors.go b/hw09_struct_validator/rules/errors.go index 0b545ee..c8ccd97 100644 --- a/hw09_struct_validator/rules/errors.go +++ b/hw09_struct_validator/rules/errors.go @@ -16,10 +16,6 @@ var ( var ( // программные ошибки функций валидации. - // // правило применимо только к строкам - // ErrOnlyStringRule = errors.New("rule applies only to the string") - // // правило применимо только к целым - // ErrOnlyIntRule = errors.New("rule applies only to the int") // недопустимое условие для правила. ErrInvalidCond = errors.New("invalid condition for the rule") // ошибка компиляции регулярного выражения. @@ -40,7 +36,7 @@ var ( var ( // целое не может быть меньше условия. ErrIntCantBeLess = errors.New("cannot be less") - // целое не может быть меньше больше. + // целое не может быть больше условия. ErrIntCantBeGreater = errors.New("cannot be greater") // целое на входит в список. ErrIntNotInList = errors.New("int is not in the list") diff --git a/hw09_struct_validator/validator.go b/hw09_struct_validator/validator.go index 24f7336..267cf23 100644 --- a/hw09_struct_validator/validator.go +++ b/hw09_struct_validator/validator.go @@ -7,18 +7,6 @@ import ( r "github.com/DimVlas/otus_hw/hw09_struct_validator/rules" ) -// implemented in errors.go file -// type ValidationError struct { -// Field string -// Err error -// } - -// type ValidationErrors []ValidationError - -// func (v ValidationErrors) Error() string { -// panic("implement me") -// } - func Validate(v interface{}) error { // nothing to validate if v == nil { diff --git a/hw09_struct_validator/validator_test.go b/hw09_struct_validator/validator_test.go index 92d33eb..d3d8c9f 100644 --- a/hw09_struct_validator/validator_test.go +++ b/hw09_struct_validator/validator_test.go @@ -10,7 +10,6 @@ import ( "github.com/stretchr/testify/require" ) -// Test the function on different structures and other types. type ( UserRole string From e1c352a9345019a70cefd81a1edd39a442d7581e Mon Sep 17 00:00:00 2001 From: DimVlas Date: Sun, 29 Dec 2024 11:55:37 +0000 Subject: [PATCH 2/6] HW11. parse Agrs --- hw11_telnet_client/.sync | 0 hw11_telnet_client/go.mod | 2 +- hw11_telnet_client/main.go | 125 ++++++++++++++++++++++++++++++++++- hw11_telnet_client/params.go | 56 ++++++++++++++++ 4 files changed, 180 insertions(+), 3 deletions(-) delete mode 100644 hw11_telnet_client/.sync create mode 100644 hw11_telnet_client/params.go diff --git a/hw11_telnet_client/.sync b/hw11_telnet_client/.sync deleted file mode 100644 index e69de29..0000000 diff --git a/hw11_telnet_client/go.mod b/hw11_telnet_client/go.mod index 839d5b8..ad27adc 100644 --- a/hw11_telnet_client/go.mod +++ b/hw11_telnet_client/go.mod @@ -1,4 +1,4 @@ -module github.com/fixme_my_friend/hw11_telnet_client +module github.com/DimVlas/otus_hw/hw11_telnet_client go 1.22 diff --git a/hw11_telnet_client/main.go b/hw11_telnet_client/main.go index 307acaf..827ba7e 100644 --- a/hw11_telnet_client/main.go +++ b/hw11_telnet_client/main.go @@ -1,6 +1,127 @@ package main +import ( + "bufio" + "context" + "fmt" + "log" + "net" + "os" + "sync" + "time" +) + +func readRoutine(ctx context.Context, conn net.Conn, wg *sync.WaitGroup) { + defer wg.Done() + scanner := bufio.NewScanner(conn) +OUTER: + for { + select { + case <-ctx.Done(): + break OUTER + default: + if !scanner.Scan() { + log.Printf("CANNOT SCAN") + if err := scanner.Err(); err != nil { + log.Printf("scanner error: %s", err) + } + break OUTER + } + text := scanner.Text() + log.Printf("From server: %s", text) + } + } + log.Printf("Finished readRoutine") + +} + +func writeRoutine(ctx context.Context, conn net.Conn, wg *sync.WaitGroup, stdin chan string) { + defer wg.Done() + //scanner := bufio.NewScanner(os.Stdin) +OUTER: + for { + select { + case <-ctx.Done(): + break OUTER + case str := <-stdin: + //if !scanner.Scan() { + // break OUTER + //} + //str := scanner.Text() + log.Printf("To server %v\n", str) + + conn.Write([]byte(fmt.Sprintf("%s\n", str))) + } + + } + log.Printf("Finished writeRoutine") +} + +func stdinScan() chan string { + out := make(chan string) + go func() { + scanner := bufio.NewScanner(os.Stdin) + for scanner.Scan() { + out <- scanner.Text() + } + if err := scanner.Err(); err != nil { + log.Printf("stdin scanner error: %s", err) + close(out) + } + }() + return out +} + func main() { - // Place your code here, - // P.S. Do not rush to throw context down, think think if it is useful with blocking operation? + + log.Println("os.Args[i]:") + log.SetPrefix("\t") + for i, _ := range os.Args { + log.Println(os.Args[i]) + } + log.SetPrefix("") + + var ( + timeout time.Duration + host string + port int + ) + + err := parseArgs(&timeout, &host, &port) + if err != nil { + log.Println(err) + os.Exit(0) + } + + log.Println("timeout: ", timeout) + log.Println("port: ", port) + log.Println("host: ", host) + + // dialer := &net.Dialer{} + // ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute) + + // //conn, err := dialer.DialContext(ctx, "tcp", "localhost:4242") + // addr := os.Args[1] + ":" + os.Args[2] + // log.Println(addr) + // conn, err := dialer.DialContext(ctx, "tcp", addr) + // if err != nil { + // log.Fatalf("Cannot connect: %v", err) + // } + + // stdin := stdinScan() + + // wg := &sync.WaitGroup{} + // wg.Add(1) + // go func() { + // readRoutine(ctx, conn, wg) + // cancel() + // }() + + // wg.Add(1) + // go func() { + // writeRoutine(ctx, conn, wg, stdin) + // }() + + // wg.Wait() + // conn.Close() } diff --git a/hw11_telnet_client/params.go b/hw11_telnet_client/params.go new file mode 100644 index 0000000..99b86d2 --- /dev/null +++ b/hw11_telnet_client/params.go @@ -0,0 +1,56 @@ +package main + +import ( + "errors" + "fmt" + "os" + "regexp" + "strconv" + "strings" + "time" +) + +func parseArgs(timeout *time.Duration, host *string, port *int) error { + for _, s := range os.Args[1:] { + if strings.Contains(s, "--timeout=") { + if err := parseTimeout(s, timeout); err != nil { + return fmt.Errorf("parseTimeout: %w", err) + } + continue + } + p, err := strconv.Atoi(s) + if err != nil { + if err := parseHost(s, host); err != nil { + return fmt.Errorf("parseHost: %w", err) + } + continue + } + + *port = p + } + + return nil +} + +func parseHost(s string, host *string) (err error) { + hostCheck := regexp.MustCompile(`^[A-Za-z0-9-.]+$`) + + if hostCheck.MatchString(s) { + *host = s + return nil + } + + return errors.New("parseHost: string \"" + s + "\" cannot be a host name or address") +} + +func parseTimeout(s string, timeout *time.Duration) (err error) { + d := strings.Split(s, "=")[1] + + *timeout, err = time.ParseDuration(d) + + if err != nil { + return + } + + return nil +} From 4734dd9459496d1cf6c384aaeb00a904924cdf2c Mon Sep 17 00:00:00 2001 From: DimVlas Date: Sun, 5 Jan 2025 21:03:47 +0000 Subject: [PATCH 3/6] hw11. is completed. need add tests --- hw11_telnet_client/main.go | 167 ++++++++++++------------------ hw11_telnet_client/params.go | 54 ++++------ hw11_telnet_client/telnet.go | 72 ++++++++++++- hw11_telnet_client/telnet_test.go | 61 +++++++++++ 4 files changed, 217 insertions(+), 137 deletions(-) diff --git a/hw11_telnet_client/main.go b/hw11_telnet_client/main.go index 827ba7e..7f185c0 100644 --- a/hw11_telnet_client/main.go +++ b/hw11_telnet_client/main.go @@ -1,127 +1,94 @@ package main import ( - "bufio" - "context" + "errors" "fmt" "log" - "net" "os" - "sync" + "os/signal" + "path" + "syscall" "time" ) -func readRoutine(ctx context.Context, conn net.Conn, wg *sync.WaitGroup) { - defer wg.Done() - scanner := bufio.NewScanner(conn) -OUTER: - for { - select { - case <-ctx.Done(): - break OUTER - default: - if !scanner.Scan() { - log.Printf("CANNOT SCAN") - if err := scanner.Err(); err != nil { - log.Printf("scanner error: %s", err) - } - break OUTER - } - text := scanner.Text() - log.Printf("From server: %s", text) - } - } - log.Printf("Finished readRoutine") - -} - -func writeRoutine(ctx context.Context, conn net.Conn, wg *sync.WaitGroup, stdin chan string) { - defer wg.Done() - //scanner := bufio.NewScanner(os.Stdin) -OUTER: - for { - select { - case <-ctx.Done(): - break OUTER - case str := <-stdin: - //if !scanner.Scan() { - // break OUTER - //} - //str := scanner.Text() - log.Printf("To server %v\n", str) - - conn.Write([]byte(fmt.Sprintf("%s\n", str))) - } - - } - log.Printf("Finished writeRoutine") -} - -func stdinScan() chan string { - out := make(chan string) - go func() { - scanner := bufio.NewScanner(os.Stdin) - for scanner.Scan() { - out <- scanner.Text() - } - if err := scanner.Err(); err != nil { - log.Printf("stdin scanner error: %s", err) - close(out) - } - }() - return out -} +var ( + ErrParseArgs = errors.New("parse args error") + // соединение не установлено. + ErrConnNotEstablish = errors.New("connection is not established") + // завершение клиента + ErrClientExit = errors.New("connection was closed by client") + // завершение сервера + ErrServerExit = errors.New("connection was closed by peer") +) func main() { - - log.Println("os.Args[i]:") - log.SetPrefix("\t") - for i, _ := range os.Args { - log.Println(os.Args[i]) - } - log.SetPrefix("") - var ( timeout time.Duration host string port int ) - err := parseArgs(&timeout, &host, &port) - if err != nil { + if err := parseArgs(os.Args[1:], &timeout, &host, &port); err != nil { log.Println(err) - os.Exit(0) + log.Println("Usage: go-telnet [--timeout=] ") + os.Exit(1) } - log.Println("timeout: ", timeout) - log.Println("port: ", port) - log.Println("host: ", host) + addr := fmt.Sprintf("%s:%d", host, port) + client := NewTelnetClient(addr, timeout, os.Stdin, os.Stdout) - // dialer := &net.Dialer{} - // ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute) + if err := client.Connect(); err != nil { + log.Fatalln(err) + } - // //conn, err := dialer.DialContext(ctx, "tcp", "localhost:4242") - // addr := os.Args[1] + ":" + os.Args[2] - // log.Println(addr) - // conn, err := dialer.DialContext(ctx, "tcp", addr) - // if err != nil { - // log.Fatalf("Cannot connect: %v", err) - // } + log.Printf("...Connected to %s", addr) - // stdin := stdinScan() + c := make(chan os.Signal, 1) + clientErr := make(chan error) + + defer func() { + client.Close() + close(c) + close(clientErr) + }() - // wg := &sync.WaitGroup{} - // wg.Add(1) - // go func() { - // readRoutine(ctx, conn, wg) - // cancel() - // }() + signal.Notify(c, syscall.SIGINT) - // wg.Add(1) - // go func() { - // writeRoutine(ctx, conn, wg, stdin) - // }() + go func() { + err := client.Send() + if err != nil { + clientErr <- err + return + } + + clientErr <- ErrClientExit + }() - // wg.Wait() - // conn.Close() + go func() { + err := client.Receive() + if err != nil { + clientErr <- err + return + } + clientErr <- ErrServerExit + }() + + //for { + select { + case <-c: + log.Println("...Connection was closed by", path.Base(os.Args[0])) + return + case err := <-clientErr: + if errors.Is(err, ErrClientExit) { + log.Println("...EOF") + return + } + if errors.Is(err, ErrServerExit) { + log.Println("...Connection was closed by peer") + return + } + log.Println(err) + return + } + //} } diff --git a/hw11_telnet_client/params.go b/hw11_telnet_client/params.go index 99b86d2..6cd0285 100644 --- a/hw11_telnet_client/params.go +++ b/hw11_telnet_client/params.go @@ -1,56 +1,40 @@ package main import ( + "bytes" "errors" + "flag" "fmt" - "os" "regexp" "strconv" - "strings" "time" ) -func parseArgs(timeout *time.Duration, host *string, port *int) error { - for _, s := range os.Args[1:] { - if strings.Contains(s, "--timeout=") { - if err := parseTimeout(s, timeout); err != nil { - return fmt.Errorf("parseTimeout: %w", err) - } - continue - } - p, err := strconv.Atoi(s) - if err != nil { - if err := parseHost(s, host); err != nil { - return fmt.Errorf("parseHost: %w", err) - } - continue - } +func parseArgs(args []string, timeout *time.Duration, host *string, port *int) error { + fs := flag.NewFlagSet("telnet", flag.ContinueOnError) + fs.SetOutput(bytes.NewBuffer(nil)) - *port = p + fs.DurationVar(timeout, "timeout", 10*time.Second, "Connection timeout") + if err := fs.Parse(args); err != nil { + return fmt.Errorf("%w: %w", ErrParseArgs, err) } - return nil -} - -func parseHost(s string, host *string) (err error) { - hostCheck := regexp.MustCompile(`^[A-Za-z0-9-.]+$`) - - if hostCheck.MatchString(s) { - *host = s - return nil + if fs.NArg() != 2 { + return fmt.Errorf("%w: %w", ErrParseArgs, errors.New("not enough arguments")) } - return errors.New("parseHost: string \"" + s + "\" cannot be a host name or address") -} - -func parseTimeout(s string, timeout *time.Duration) (err error) { - d := strings.Split(s, "=")[1] - - *timeout, err = time.ParseDuration(d) + h := fs.Arg(0) + b, _ := regexp.MatchString(`^[A-Za-z0-9-.]+$`, h) + if !b { + return fmt.Errorf("%w %w", ErrParseArgs, errors.New("host: string \""+h+"\" cannot be a host name or address")) + } + *host = h + p, err := strconv.Atoi(fs.Arg(1)) if err != nil { - return + return fmt.Errorf("%w %w", ErrParseArgs, fmt.Errorf("port: %w", err)) } + *port = p return nil } diff --git a/hw11_telnet_client/telnet.go b/hw11_telnet_client/telnet.go index 369caa1..21c3651 100644 --- a/hw11_telnet_client/telnet.go +++ b/hw11_telnet_client/telnet.go @@ -1,7 +1,10 @@ package main import ( + "bufio" + "errors" "io" + "net" "time" ) @@ -13,9 +16,74 @@ type TelnetClient interface { } func NewTelnetClient(address string, timeout time.Duration, in io.ReadCloser, out io.Writer) TelnetClient { - // Place your code here. - return nil + return &telnetClient{ + addr: address, + timeout: timeout, + in: in, + out: out, + } } // Place your code here. // P.S. Author's solution takes no more than 50 lines. + +type telnetClient struct { + timeout time.Duration + addr string + conn net.Conn + in io.ReadCloser + out io.Writer +} + +func (tc *telnetClient) Connect() (err error) { + tc.conn, err = net.DialTimeout("tcp", tc.addr, tc.timeout) + return +} + +func (tc *telnetClient) Close() error { + if tc.conn != nil { + return tc.conn.Close() + } + return nil +} + +func (tc *telnetClient) Send() error { + if tc.conn == nil { + return ErrConnNotEstablish + } + + scanner := bufio.NewScanner(tc.in) + for scanner.Scan() { + mess := append(scanner.Bytes(), '\n') + + if _, err := tc.conn.Write(mess); err != nil { + return err + } + } + if err := scanner.Err(); err != nil { + return err + } + + return nil +} + +func (tc *telnetClient) Receive() error { + if tc.conn == nil { + return ErrConnNotEstablish + } + + reader := bufio.NewReader(tc.conn) + for { + message, err := reader.ReadBytes('\n') + if err != nil { + if errors.Is(err, io.EOF) { + return nil + } + return err + } + _, err = tc.out.Write(message) + if err != nil { + return err + } + } +} diff --git a/hw11_telnet_client/telnet_test.go b/hw11_telnet_client/telnet_test.go index fb89f6d..a517547 100644 --- a/hw11_telnet_client/telnet_test.go +++ b/hw11_telnet_client/telnet_test.go @@ -63,3 +63,64 @@ func TestTelnetClient(t *testing.T) { wg.Wait() }) } + +var testParamData = []struct { + name string + args []string + timeout time.Duration + host string + port int + err error +}{ + { + name: "parseArgs success", + args: []string{ + "--timeout=5s", + "localhost", + "12345", + }, + timeout: 5 * time.Second, + host: "localhost", + port: 12345, + err: nil, + }, + { + name: "parseArgs bad timeout", + args: []string{ + "--timeout=5ass", + "localhost", + "12345", + }, + timeout: 5 * time.Second, + host: "", + port: 0, + err: ErrParseArgs, + }, +} + +func TestParseArgs(t *testing.T) { + + for _, data := range testParamData { + t.Run(data.name, func(t *testing.T) { + var ( + timeout time.Duration + host string + port int + ) + + err := parseArgs(data.args, &timeout, &host, &port) + + if data.err != nil { + require.ErrorIs(t, err, data.err) + } else { + require.NoError(t, err) + } + + require.Equal(t, data.host, host) + require.Equal(t, data.port, port) + + }) + + } + +} From 9585f74c5a5bc5abdeb3b0a3a74c2ef8d5983a5b Mon Sep 17 00:00:00 2001 From: DimVlas Date: Mon, 6 Jan 2025 23:36:56 +0300 Subject: [PATCH 4/6] hw11. is comleted --- hw11_telnet_client/main.go | 2 - hw11_telnet_client/telnet.go | 4 -- hw11_telnet_client/telnet_test.go | 103 ++++++++++++++++++++++++++++-- hw11_telnet_client/test.sh | 2 +- 4 files changed, 97 insertions(+), 14 deletions(-) diff --git a/hw11_telnet_client/main.go b/hw11_telnet_client/main.go index 7f185c0..ec0439f 100644 --- a/hw11_telnet_client/main.go +++ b/hw11_telnet_client/main.go @@ -73,7 +73,6 @@ func main() { clientErr <- ErrServerExit }() - //for { select { case <-c: log.Println("...Connection was closed by", path.Base(os.Args[0])) @@ -90,5 +89,4 @@ func main() { log.Println(err) return } - //} } diff --git a/hw11_telnet_client/telnet.go b/hw11_telnet_client/telnet.go index 21c3651..bbe86f6 100644 --- a/hw11_telnet_client/telnet.go +++ b/hw11_telnet_client/telnet.go @@ -24,9 +24,6 @@ func NewTelnetClient(address string, timeout time.Duration, in io.ReadCloser, ou } } -// Place your code here. -// P.S. Author's solution takes no more than 50 lines. - type telnetClient struct { timeout time.Duration addr string @@ -55,7 +52,6 @@ func (tc *telnetClient) Send() error { scanner := bufio.NewScanner(tc.in) for scanner.Scan() { mess := append(scanner.Bytes(), '\n') - if _, err := tc.conn.Write(mess); err != nil { return err } diff --git a/hw11_telnet_client/telnet_test.go b/hw11_telnet_client/telnet_test.go index a517547..972518b 100644 --- a/hw11_telnet_client/telnet_test.go +++ b/hw11_telnet_client/telnet_test.go @@ -62,6 +62,49 @@ func TestTelnetClient(t *testing.T) { wg.Wait() }) + + t.Run("bad_connect", func(t *testing.T) { + in := &bytes.Buffer{} + out := &bytes.Buffer{} + client := NewTelnetClient("localhost:4242", 1*time.Second, io.NopCloser(in), out) + + require.EqualError(t, client.Connect(), "dial tcp [::1]:4242: connect: connection refused") + }) + + t.Run("without_connect", func(t *testing.T) { + in := &bytes.Buffer{} + out := &bytes.Buffer{} + client := NewTelnetClient("localhost:4242", 5*time.Second, io.NopCloser(in), out) + + require.ErrorIs(t, client.Send(), ErrConnNotEstablish) + require.ErrorIs(t, client.Receive(), ErrConnNotEstablish) + require.NoError(t, client.Close()) + }) + + t.Run("close_listen", func(t *testing.T) { + l, err := net.Listen("tcp", "127.0.0.1:") + require.NoError(t, err) + + in := &bytes.Buffer{} + out := &bytes.Buffer{} + + timeout, err := time.ParseDuration("1s") + require.NoError(t, err) + + client := NewTelnetClient(l.Addr().String(), timeout, io.NopCloser(in), out) + require.NoError(t, client.Connect()) + defer func() { require.NoError(t, client.Close()) }() + + l.Close() + + conn := client.(*telnetClient).conn + + in.WriteString("hello\n") + require.EqualError(t, client.Send(), "write tcp "+conn.LocalAddr().String()+"->"+conn.RemoteAddr().String()+": write: connection reset by peer") + + // при получении возращает EOF, который перехватывается т.к. трактуется как корректное окончание + require.NoError(t, client.Receive()) + }) } var testParamData = []struct { @@ -70,10 +113,10 @@ var testParamData = []struct { timeout time.Duration host string port int - err error + err string }{ { - name: "parseArgs success", + name: "parseArgs_success", args: []string{ "--timeout=5s", "localhost", @@ -82,10 +125,21 @@ var testParamData = []struct { timeout: 5 * time.Second, host: "localhost", port: 12345, - err: nil, + err: "", + }, + { + name: "parseArgs_success_without_timeout", + args: []string{ + "localhost", + "12345", + }, + timeout: 10 * time.Second, + host: "localhost", + port: 12345, + err: "", }, { - name: "parseArgs bad timeout", + name: "parseArgs_bad_timeout", args: []string{ "--timeout=5ass", "localhost", @@ -94,7 +148,42 @@ var testParamData = []struct { timeout: 5 * time.Second, host: "", port: 0, - err: ErrParseArgs, + err: `parse args error: invalid value "5ass" for flag -timeout: parse error`, + }, + { + name: "parseArgs_bad_args_count", + args: []string{ + "--timeout=5s", + "localhost", + }, + timeout: 5 * time.Second, + host: "", + port: 0, + err: "parse args error: not enough arguments", + }, + { + name: "parseArgs_invalid_host", + args: []string{ + "--timeout=5s", + "domain/localhost", + "123", + }, + timeout: 5 * time.Second, + host: "", + port: 0, + err: "parse args error host: string \"domain/localhost\" cannot be a host name or address", + }, + { + name: "parseArgs_invalid_port", + args: []string{ + "--timeout=5s", + "localhost", + "a123", + }, + timeout: 5 * time.Second, + host: "localhost", + port: 0, + err: "parse args error port: strconv.Atoi: parsing \"a123\": invalid syntax", }, } @@ -110,8 +199,8 @@ func TestParseArgs(t *testing.T) { err := parseArgs(data.args, &timeout, &host, &port) - if data.err != nil { - require.ErrorIs(t, err, data.err) + if data.err != "" { + require.EqualError(t, err, data.err) } else { require.NoError(t, err) } diff --git a/hw11_telnet_client/test.sh b/hw11_telnet_client/test.sh index 422b594..ae4ed75 100755 --- a/hw11_telnet_client/test.sh +++ b/hw11_telnet_client/test.sh @@ -3,7 +3,7 @@ set -xeuo pipefail go build -o go-telnet -(echo -e "Hello\nFrom\nNC\n" && cat 2>/dev/null) | nc -l localhost 4242 >/tmp/nc.out & +(echo -e "Hello\nFrom\nNC\n" && cat 2>/dev/null) | nc -l 127.0.0.1 -p 4242 >/tmp/nc.out & NC_PID=$! sleep 1 From 27a612b1f72939893f326ca5bf518d8270a3a3a1 Mon Sep 17 00:00:00 2001 From: DimVlas Date: Mon, 6 Jan 2025 23:43:15 +0300 Subject: [PATCH 5/6] hw11. is completed. linter check --- hw11_telnet_client/main.go | 4 ++-- hw11_telnet_client/telnet_test.go | 7 ++----- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/hw11_telnet_client/main.go b/hw11_telnet_client/main.go index ec0439f..b722012 100644 --- a/hw11_telnet_client/main.go +++ b/hw11_telnet_client/main.go @@ -15,9 +15,9 @@ var ( ErrParseArgs = errors.New("parse args error") // соединение не установлено. ErrConnNotEstablish = errors.New("connection is not established") - // завершение клиента + // завершение клиента. ErrClientExit = errors.New("connection was closed by client") - // завершение сервера + // завершение сервера. ErrServerExit = errors.New("connection was closed by peer") ) diff --git a/hw11_telnet_client/telnet_test.go b/hw11_telnet_client/telnet_test.go index 972518b..816e09c 100644 --- a/hw11_telnet_client/telnet_test.go +++ b/hw11_telnet_client/telnet_test.go @@ -100,7 +100,8 @@ func TestTelnetClient(t *testing.T) { conn := client.(*telnetClient).conn in.WriteString("hello\n") - require.EqualError(t, client.Send(), "write tcp "+conn.LocalAddr().String()+"->"+conn.RemoteAddr().String()+": write: connection reset by peer") + require.EqualError(t, client.Send(), + "write tcp "+conn.LocalAddr().String()+"->"+conn.RemoteAddr().String()+": write: connection reset by peer") // при получении возращает EOF, который перехватывается т.к. трактуется как корректное окончание require.NoError(t, client.Receive()) @@ -188,7 +189,6 @@ var testParamData = []struct { } func TestParseArgs(t *testing.T) { - for _, data := range testParamData { t.Run(data.name, func(t *testing.T) { var ( @@ -207,9 +207,6 @@ func TestParseArgs(t *testing.T) { require.Equal(t, data.host, host) require.Equal(t, data.port, port) - }) - } - } From 92d712c4547734c669828ffa22235eb5fa41cb12 Mon Sep 17 00:00:00 2001 From: DimVlas Date: Mon, 6 Jan 2025 23:49:35 +0300 Subject: [PATCH 6/6] hw11. reject test.sh --- hw11_telnet_client/test.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hw11_telnet_client/test.sh b/hw11_telnet_client/test.sh index ae4ed75..422b594 100755 --- a/hw11_telnet_client/test.sh +++ b/hw11_telnet_client/test.sh @@ -3,7 +3,7 @@ set -xeuo pipefail go build -o go-telnet -(echo -e "Hello\nFrom\nNC\n" && cat 2>/dev/null) | nc -l 127.0.0.1 -p 4242 >/tmp/nc.out & +(echo -e "Hello\nFrom\nNC\n" && cat 2>/dev/null) | nc -l localhost 4242 >/tmp/nc.out & NC_PID=$! sleep 1