github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/lib/encoder/filename/decode_test.go (about)

     1  package filename
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestDecode(t *testing.T) {
     8  	tests := []struct {
     9  		name    string
    10  		encoded string
    11  		want    string
    12  		wantErr bool
    13  	}{
    14  		{
    15  			name: "uncompressed",
    16  			// tableUncompressed
    17  			encoded: "AYS5i",
    18  			want:    "a.b",
    19  		},
    20  		{
    21  			name: "uncompressed-long",
    22  			// tableUncompressed
    23  			encoded: "AQnpHUVl4cUhCQTZsalRzaXI4MGdVTTVZQnpHUVl4cUhCQTZsalRzaXI4MGdVTTVZQnpHUVl4cUhCQTZsalRzaXI4MGdVTTVZQnpHUVl4cUhCQTZsalRzaXI4MGdVTTVZQnpHUVl4cUhCQTZsalRzaXI4MGdVTTVZQnpHUVl4cUhCQTZsalRzaXI4MGdVTTVZQnpHUVl4cUhCQTZsalRzaXI4MGdVTTVZQnpHUVl4cUhCQTZsalRzaXI4MGdVTTVZQnpHUVl4cUhCQTZsalRzaXI4MGdVTTVZQnpHUVl4cUhCQTZsalRzaXI4MGdVTTVZQnpHUVl4cUhCQTZsalRzaXI4MGdVTTVZQnpHUVl4cUhCQTZsalRzaXI4MGdVTTVZQnpHUVl4cUhCQTZsalRzaXI4MGdVTTVZQnpHUVl4cUhCQTZsalRzaXI4MGdVTTVZQnpHUVl4cUhCQTZsalRzaXI4MGdVTTVZQnpHUVl4cUhCQTZsalRzaXI4MGdVTTVZQnpHUVl4cUhCQTZsalRzaXI4MGdVTTVZQnpHUVl4cUhCQTZsalRzaXI4MGdVTTVZQnpHUVl4cUhCQTZsalRzaXI4MGdVTTVZQnpHUVl4cUhCQTZsalRzaXI4MGdVTTVZQnpHUVl4cUhCQTZsalRzaXI4MGdVTTVZQnpHUVl4cUhCQTZsalRzaXI4MGdVTTVZQnpHUVl4cUhCQTZsalRzaXI4MGdVTTVZQnpHUVl4cUhCQTZsalRzaXI4MGdVTTVZ",
    24  			want:    "BzGQYxqHBA6ljTsir80gUM5YBzGQYxqHBA6ljTsir80gUM5YBzGQYxqHBA6ljTsir80gUM5YBzGQYxqHBA6ljTsir80gUM5YBzGQYxqHBA6ljTsir80gUM5YBzGQYxqHBA6ljTsir80gUM5YBzGQYxqHBA6ljTsir80gUM5YBzGQYxqHBA6ljTsir80gUM5YBzGQYxqHBA6ljTsir80gUM5YBzGQYxqHBA6ljTsir80gUM5YBzGQYxqHBA6ljTsir80gUM5YBzGQYxqHBA6ljTsir80gUM5YBzGQYxqHBA6ljTsir80gUM5YBzGQYxqHBA6ljTsir80gUM5YBzGQYxqHBA6ljTsir80gUM5YBzGQYxqHBA6ljTsir80gUM5YBzGQYxqHBA6ljTsir80gUM5YBzGQYxqHBA6ljTsir80gUM5YBzGQYxqHBA6ljTsir80gUM5YBzGQYxqHBA6ljTsir80gUM5YBzGQYxqHBA6ljTsir80gUM5YBzGQYxqHBA6ljTsir80gUM5YBzGQYxqHBA6ljTsir80gUM5YBzGQYxqHBA6ljTsir80gUM5Y",
    25  		},
    26  		{
    27  			name: "plain-1",
    28  			// Table 2
    29  			encoded: "BzGQYxqHBA6ljTsir80gUM5Y=",
    30  			want:    "-Duplican99E8ZI4___9_",
    31  		},
    32  		{
    33  			name: "hex-1",
    34  			// Table 4
    35  			encoded: "D_--tHZROQpqqJ9PafqNa6STF",
    36  			want:    "13646871dfabbs43323564654bbefff",
    37  		},
    38  		{
    39  			name: "hex-2",
    40  			// Table 6
    41  			encoded: "GhIEAIOBQMFQeWm4SClVpXVldCXFZLj4uOgoJHChQ4KBiXQ==",
    42  			want:    "5368616e6e6f6e206c696d69743a203534353833206279746573-+._=!()",
    43  		},
    44  		{
    45  			name: "hex-3",
    46  			// Table 7
    47  			encoded: "HohwXBXoJcVFSHgpdVQlxHXIuVgpNCR06Eg5aBg==",
    48  			want:    "7461626C6520312073697A653A203335206572723A203C6E696C3E",
    49  		},
    50  		{
    51  			name: "base64-1",
    52  			// Table 5
    53  			encoded: "FMpABB9Ef0KP8OrVxjnE3LzUePuLZi8pPg7eW8bgyW2d3Ucckf4rlE0mkAvlILVpOmF3L-rFbmNrpUO2HQFlF4SCMPVPeCEX6LeOg5JVpUVCXV1WSazD9vSpr",
    54  			want:    "UxAYiB0FNTTkXRw9P8hwq-WmN7tYwbe-sFw8C3snDRG1d-yjrdOUVZQyLdtkJ8tuvhBSnuBiLjVieCAroWEZDIO4Hb_rKgdzPjMqFE7inwHJ2isF==",
    55  		},
    56  		{
    57  			name: "custom-1",
    58  			// Table 62, custom
    59  			encoded: "-BeADJCoG_________________xc=",
    60  			want:    "Uaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
    61  		},
    62  		{
    63  			name: "custom-2",
    64  			// Table 62, custom
    65  			encoded: "-BPABDWUppYyllDKW0sYYSymljJQx",
    66  			want:    "12312132123121321321321321312312312313132132131231213213213213123121321321321",
    67  		},
    68  		{
    69  			name: "rle-1",
    70  			// tableRLE
    71  			encoded: "9a2E=",
    72  			want:    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
    73  		},
    74  		{
    75  			name: "regular-1",
    76  			// Table 1
    77  			encoded: "BeSSrnzj0j3OXyR9K81M=",
    78  			want:    "regular-filename.txt",
    79  		},
    80  		{
    81  			name: "regular-3",
    82  			// Table 2
    83  			encoded: "COyCCD-42d9s=",
    84  			want:    "00012312.JPG",
    85  		},
    86  		{
    87  			name: "regular-4",
    88  			// Table 3
    89  			encoded: "DmqiJmrhNSDOJTCKTyCQ=",
    90  			want:    ". . . .txta123123123",
    91  		},
    92  		{
    93  			name: "unicode-1",
    94  			// tableSCSUPlain
    95  			encoded: "8D5V3MESVd-WEF7WuqaOvpKUWtYGEyw5UDQ==",
    96  			want:    "長い長いUNICODEファイル名",
    97  		},
    98  		{
    99  			name: "unicode-2",
   100  			// tableSCSUPlain
   101  			encoded: "8GyHV1N7u2OEg4ufQ3eHQ3Ngg6N3X0CDg4-HX0NXU2tg=",
   102  			want:    "ვეპხის ტყაოსანი შოთა რუსთაველი",
   103  		},
   104  		{
   105  			name: "unicode-3",
   106  			// tableSCSU
   107  			encoded: "7LpehMXOrWe7mcT_lpf2MN1Nmgu55jpXHLavZcXJb2UTJ-UmGU15iznkD",
   108  			want:    "Sønderjysk: Æ ka æe glass uhen at det go mæ naue.,",
   109  		},
   110  		{
   111  			name: "unicode-4",
   112  			// tableSCSU
   113  			encoded: "7TCSRm0liJDR0ulpBq4Lla_XB2mWdLFMEs8wEQKHAGa8FRr333ntJ6Ww6_f__N5VKeYM=",
   114  			want:    "Hello------world     時危兵甲滿天涯,載道流離起怨咨.bin",
   115  		},
   116  	}
   117  
   118  	for _, tt := range tests {
   119  		t.Run(tt.name, func(t *testing.T) {
   120  			got, err := Decode(tt.encoded)
   121  			if (err != nil) != tt.wantErr {
   122  				if tt.encoded == "" && tt.want != "" {
   123  					proposed := Encode(tt.want)
   124  					table := decodeMap[proposed[0]] - 1
   125  					t.Errorf("No encoded value, try '%s', table is %d", proposed, table)
   126  					return
   127  				}
   128  				t.Errorf("Decode() error = %v, wantErr %v", err, tt.wantErr)
   129  				return
   130  			}
   131  			if err == nil {
   132  				proposed := Encode(tt.want)
   133  				table := decodeMap[proposed[0]] - 1
   134  				if len(proposed) > len(tt.encoded) {
   135  					t.Errorf("Got longer encoded value than reference. Likely compression regression. Got %s, table %d", proposed, table)
   136  				}
   137  				if len(proposed) > len(tt.encoded) {
   138  					t.Logf("Got better encoded value, improved length %d, was %d", len(proposed), len(tt.encoded))
   139  				}
   140  			}
   141  
   142  			if got != tt.want {
   143  				t.Errorf("Decode() got = %v, want %v", got, tt.want)
   144  			}
   145  		})
   146  	}
   147  }