@@ -58,26 +58,64 @@ func TestCreateZipFromTar(t *testing.T) {
58
58
if runtime .GOOS != "linux" {
59
59
t .Skip ("skipping this test on non-Linux platform" )
60
60
}
61
+ t .Run ("OK" , func (t * testing.T ) {
62
+ t .Parallel ()
63
+ tarBytes , err := os .ReadFile (filepath .Join ("." , "testdata" , "test.tar" ))
64
+ require .NoError (t , err , "failed to read sample tar file" )
61
65
62
- tarBytes , err := os .ReadFile (filepath .Join ("testdata" , "test.tar" ))
63
- require .NoError (t , err , "failed to read sample tar file" )
66
+ tr := tar .NewReader (bytes .NewReader (tarBytes ))
67
+ zipBytes , err := coderd .CreateZipFromTar (tr )
68
+ require .NoError (t , err )
64
69
65
- tr := tar .NewReader (bytes .NewReader (tarBytes ))
66
- zipBytes , err := coderd .CreateZipFromTar (tr )
67
- require .NoError (t , err )
70
+ assertSampleZipFile (t , zipBytes )
68
71
69
- assertSampleZipFile (t , zipBytes )
72
+ tempDir := t .TempDir ()
73
+ tempFilePath := filepath .Join (tempDir , "test.zip" )
74
+ err = os .WriteFile (tempFilePath , zipBytes , 0o600 )
75
+ require .NoError (t , err , "failed to write converted zip file" )
70
76
71
- tempDir := t .TempDir ()
72
- tempFilePath := filepath .Join (tempDir , "test.zip" )
73
- err = os .WriteFile (tempFilePath , zipBytes , 0o600 )
74
- require .NoError (t , err , "failed to write converted zip file" )
77
+ ctx := testutil .Context (t , testutil .WaitShort )
78
+ cmd := exec .CommandContext (ctx , "unzip" , tempFilePath , "-d" , tempDir )
79
+ require .NoError (t , cmd .Run (), "failed to extract converted zip file" )
75
80
76
- ctx := testutil .Context (t , testutil .WaitShort )
77
- cmd := exec .CommandContext (ctx , "unzip" , tempFilePath , "-d" , tempDir )
78
- require .NoError (t , cmd .Run (), "failed to extract converted zip file" )
81
+ assertExtractedFiles (t , tempDir , false )
82
+ })
79
83
80
- assertExtractedFiles (t , tempDir , false )
84
+ t .Run ("MissingSlashInDirectoryHeader" , func (t * testing.T ) {
85
+ t .Parallel ()
86
+
87
+ // Given: a tar archive containing a directory entry that has the directory
88
+ // mode bit set but the name is missing a trailing slash
89
+
90
+ var tarBytes bytes.Buffer
91
+ tw := tar .NewWriter (& tarBytes )
92
+ tw .WriteHeader (& tar.Header {
93
+ Name : "dir" ,
94
+ Typeflag : tar .TypeDir ,
95
+ Size : 0 ,
96
+ })
97
+ require .NoError (t , tw .Flush ())
98
+ require .NoError (t , tw .Close ())
99
+
100
+ // When: we convert this to a zip
101
+ tr := tar .NewReader (& tarBytes )
102
+ zipBytes , err := coderd .CreateZipFromTar (tr )
103
+ require .NoError (t , err )
104
+
105
+ // Then: the resulting zip should contain a corresponding directory
106
+ zr , err := zip .NewReader (bytes .NewReader (zipBytes ), int64 (len (zipBytes )))
107
+ require .NoError (t , err )
108
+ for _ , zf := range zr .File {
109
+ switch zf .Name {
110
+ case "dir" :
111
+ require .Fail (t , "missing trailing slash in directory name" )
112
+ case "dir/" :
113
+ require .True (t , zf .Mode ().IsDir (), "should be a directory" )
114
+ default :
115
+ require .Fail (t , "unexpected file in archive" )
116
+ }
117
+ }
118
+ })
81
119
}
82
120
83
121
// nolint:revive // this is a control flag but it's in a unit test
0 commit comments