github.com/pkg/sftp@v1.13.6/internal/encoding/ssh/filexfer/fx_test.go (about)

     1  package sshfx
     2  
     3  import (
     4  	"bufio"
     5  	"errors"
     6  	"regexp"
     7  	"strconv"
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  // This string data is copied verbatim from https://filezilla-project.org/specs/draft-ietf-secsh-filexfer-13.txt
    13  var fxStandardsText = `
    14  SSH_FX_OK                            0
    15  SSH_FX_EOF                           1
    16  SSH_FX_NO_SUCH_FILE                  2
    17  SSH_FX_PERMISSION_DENIED             3
    18  SSH_FX_FAILURE                       4
    19  SSH_FX_BAD_MESSAGE                   5
    20  SSH_FX_NO_CONNECTION                 6
    21  SSH_FX_CONNECTION_LOST               7
    22  SSH_FX_OP_UNSUPPORTED                8
    23  SSH_FX_INVALID_HANDLE                9
    24  SSH_FX_NO_SUCH_PATH                  10
    25  SSH_FX_FILE_ALREADY_EXISTS           11
    26  SSH_FX_WRITE_PROTECT                 12
    27  SSH_FX_NO_MEDIA                      13
    28  SSH_FX_NO_SPACE_ON_FILESYSTEM        14
    29  SSH_FX_QUOTA_EXCEEDED                15
    30  SSH_FX_UNKNOWN_PRINCIPAL             16
    31  SSH_FX_LOCK_CONFLICT                 17
    32  SSH_FX_DIR_NOT_EMPTY                 18
    33  SSH_FX_NOT_A_DIRECTORY               19
    34  SSH_FX_INVALID_FILENAME              20
    35  SSH_FX_LINK_LOOP                     21
    36  SSH_FX_CANNOT_DELETE                 22
    37  SSH_FX_INVALID_PARAMETER             23
    38  SSH_FX_FILE_IS_A_DIRECTORY           24
    39  SSH_FX_BYTE_RANGE_LOCK_CONFLICT      25
    40  SSH_FX_BYTE_RANGE_LOCK_REFUSED       26
    41  SSH_FX_DELETE_PENDING                27
    42  SSH_FX_FILE_CORRUPT                  28
    43  SSH_FX_OWNER_INVALID                 29
    44  SSH_FX_GROUP_INVALID                 30
    45  SSH_FX_NO_MATCHING_BYTE_RANGE_LOCK   31
    46  `
    47  
    48  func TestFxNames(t *testing.T) {
    49  	whitespace := regexp.MustCompile(`[[:space:]]+`)
    50  
    51  	scan := bufio.NewScanner(strings.NewReader(fxStandardsText))
    52  
    53  	for scan.Scan() {
    54  		line := scan.Text()
    55  		if i := strings.Index(line, "//"); i >= 0 {
    56  			line = line[:i]
    57  		}
    58  
    59  		line = strings.TrimSpace(line)
    60  		if line == "" {
    61  			continue
    62  		}
    63  
    64  		fields := whitespace.Split(line, 2)
    65  		if len(fields) < 2 {
    66  			t.Fatalf("unexpected standards text line: %q", line)
    67  		}
    68  
    69  		name, value := fields[0], fields[1]
    70  		n, err := strconv.Atoi(value)
    71  		if err != nil {
    72  			t.Fatal("unexpected error:", err)
    73  		}
    74  
    75  		fx := Status(n)
    76  
    77  		if got := fx.String(); got != name {
    78  			t.Errorf("fx name mismatch for %d: got %q, but want %q", n, got, name)
    79  		}
    80  	}
    81  
    82  	if err := scan.Err(); err != nil {
    83  		t.Fatal("unexpected error:", err)
    84  	}
    85  }
    86  
    87  func TestStatusIs(t *testing.T) {
    88  	status := StatusFailure
    89  
    90  	if !errors.Is(status, StatusFailure) {
    91  		t.Error("errors.Is(StatusFailure, StatusFailure) != true")
    92  	}
    93  	if !errors.Is(status, &StatusPacket{StatusCode: StatusFailure}) {
    94  		t.Error("errors.Is(StatusFailure, StatusPacket{StatusFailure}) != true")
    95  	}
    96  	if errors.Is(status, StatusOK) {
    97  		t.Error("errors.Is(StatusFailure, StatusFailure) == true")
    98  	}
    99  	if errors.Is(status, &StatusPacket{StatusCode: StatusOK}) {
   100  		t.Error("errors.Is(StatusFailure, StatusPacket{StatusFailure}) == true")
   101  	}
   102  }