aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build/lib/sandbox/LinuxSandboxedStrategyTest.java
blob: d5d2439b7712b27df1b634510b920d4c7657352b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// Copyright 2015 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.sandbox;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.lib.vfs.PathFragment;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Tests for {@code LinuxSandboxedStrategy}.
 *
 * <p>The general idea for each test is to provide a file tree consisting of symlinks, directories
 * and empty files and then handing that together with an arbitrary number of input files (what
 * would be specified in the "srcs" attribute, for example) to the LinuxSandboxedStrategy.
 *
 * <p>The algorithm that processes the mounts must then always find (and thus mount) the expected
 * tree of files given only the set of input files.
 */
@RunWith(JUnit4.class)
public class LinuxSandboxedStrategyTest extends LinuxSandboxedStrategyTestCase {
  /**
   * Strips the working directory (which can be very long) from the file names in the input map, to
   * make assertion failures easier to read.
   */
  private ImmutableMap<String, String> userFriendlyMap(Map<Path, Path> input) {
    ImmutableMap.Builder<String, String> userFriendlyMap = ImmutableMap.builder();
    for (Entry<Path, Path> entry : input.entrySet()) {
      String key = entry.getKey().getPathString().replace(workspaceDir.getPathString(), "");
      String value = entry.getValue().getPathString().replace(workspaceDir.getPathString(), "");
      userFriendlyMap.put(key, value);
    }
    return userFriendlyMap.build();
  }

  /**
   * Takes a map of file specifications, creates the necessary files / symlinks / dirs,
   * mounts files listed in customMount at their canonical location in the sandbox and returns the
   * output of {@code LinuxSandboxedStrategy#fixMounts} for it.
   */
  private ImmutableMap<String, String> userFriendlyMounts(
      Map<String, String> linksAndFiles, List<String> customMounts) throws Exception {
    return userFriendlyMap(mounts(linksAndFiles, customMounts));
  }

  private ImmutableMap<Path, Path> mounts(
      Map<String, String> linksAndFiles, List<String> customMounts) throws Exception {
    createTreeStructure(linksAndFiles);

    ImmutableMap.Builder<Path, Path> mounts = ImmutableMap.builder();
    for (String customMount : customMounts) {
      Path customMountPath = workspaceDir.getRelative(customMount);
      mounts.put(customMountPath, customMountPath);
    }
    return ImmutableMap.copyOf(LinuxSandboxedStrategy.finalizeMounts(mounts.build()));
  }

  /**
   * Takes a map of file specifications, creates the necessary files / symlinks / dirs,
   * mounts the first file of the specification at its canonical location in the sandbox and returns
   * the output of {@code LinuxSandboxedStrategy#fixMounts} for it.
   */
  private Map<String, String> userFriendlyMounts(Map<String, String> linksAndFiles)
      throws Exception {
    return userFriendlyMap(mounts(linksAndFiles));
  }

  private Map<Path, Path> mounts(Map<String, String> linksAndFiles) throws Exception {
    return mounts(
        linksAndFiles, ImmutableList.of(Iterables.getFirst(linksAndFiles.keySet(), null)));
  }

  /**
   * Returns a map of mount entries for a list files, which can be used to assert that all
   * expected mounts have been made by the LinuxSandboxedStrategy.
   */
  private ImmutableMap<String, String> userFriendlyAsserts(List<String> asserts) {
    return userFriendlyMap(asserts(asserts));
  }
  private ImmutableMap<String, String> userFriendlyAsserts(Map<String, String> asserts) {
    return userFriendlyMap(asserts(asserts));
  }

  private ImmutableMap<Path, Path> asserts(List<String> asserts) {
    ImmutableMap.Builder<Path, Path> pathifiedAsserts = ImmutableMap.builder();
    for (String fileName : asserts) {
      Path inputPath = workspaceDir.getRelative(fileName);
      pathifiedAsserts.put(inputPath, inputPath);
    }
    return pathifiedAsserts.build();
  }

  private ImmutableMap<Path, Path> asserts(Map<String, String> asserts) {
    ImmutableMap.Builder<Path, Path> pathifiedAsserts = ImmutableMap.builder();
    for (Map.Entry<String, String> file : asserts.entrySet()) {
      pathifiedAsserts.put(
          workspaceDir.getRelative(file.getKey()), workspaceDir.getRelative(file.getValue()));
    }
    return pathifiedAsserts.build();
  }

  private void createTreeStructure(Map<String, String> linksAndFiles) throws Exception {
    for (Entry<String, String> entry : linksAndFiles.entrySet()) {
      Path filePath = workspaceDir.getRelative(entry.getKey());
      String linkTarget = entry.getValue();

      FileSystemUtils.createDirectoryAndParents(filePath.getParentDirectory());

      if (!linkTarget.isEmpty()) {
        filePath.createSymbolicLink(new PathFragment(linkTarget));
      } else if (filePath.getPathString().endsWith("/")) {
        filePath.createDirectory();
      } else {
        FileSystemUtils.createEmptyFile(filePath);
      }
    }
  }

  @Test
  public void testResolvesRelativeFileToFileSymlinkInSameDir() throws Exception {
    Map<String, String> testFiles = new LinkedHashMap<>();
    testFiles.put("symlink.txt", "goal.txt");
    testFiles.put("goal.txt", "");
    testFiles.put("other.txt", "");

    Map<String, String> assertMounts = ImmutableMap.of("symlink.txt", "goal.txt");

    assertThat(userFriendlyMounts(testFiles)).isEqualTo(userFriendlyAsserts(assertMounts));
  }

  @Test
  public void testResolvesRelativeFileToFileSymlinkInSubDir() throws Exception {
    Map<String, String> testFiles =
        ImmutableMap.of(
            "symlink.txt", "x/goal.txt",
            "x/goal.txt", "",
            "x/other.txt", "");

    Map<String, String> assertMounts = ImmutableMap.of("symlink.txt", "x/goal.txt");
    assertThat(userFriendlyMounts(testFiles)).isEqualTo(userFriendlyAsserts(assertMounts));
  }

  @Test
  public void testResolvesRelativeFileToFileSymlinkInParentDir() throws Exception {
    Map<String, String> testFiles =
        ImmutableMap.of(
            "x/symlink.txt", "../goal.txt",
            "goal.txt", "",
            "x/other.txt", "");

    Map<String, String> assertMounts = ImmutableMap.of("x/symlink.txt", "goal.txt");

    assertThat(userFriendlyMounts(testFiles)).isEqualTo(userFriendlyAsserts(assertMounts));
  }

  @Test
  public void testRecursesSubDirs() throws Exception {
    ImmutableList<String> inputFile = ImmutableList.of("a/b");

    Map<String, String> testFiles =
        ImmutableMap.of(
            "a/b/x.txt", "",
            "a/b/y.txt", "z.txt",
            "a/b/z.txt", "");

    List<String> assertMounts = ImmutableList.of("a/b");

    assertThat(userFriendlyMounts(testFiles, inputFile))
        .isEqualTo(userFriendlyAsserts(assertMounts));
  }

  @Test
  public void testDetectsWholeDir() throws Exception {
    ImmutableList<String> inputFile = ImmutableList.of("a/x.txt", "a/z.txt");

    Map<String, String> testFiles =
        ImmutableMap.of(
            "a/x.txt", "",
            "a/z.txt", "");

    List<String> assertMounts = ImmutableList.of("a");

    assertThat(userFriendlyMounts(testFiles, inputFile))
        .isEqualTo(userFriendlyAsserts(assertMounts));
  }

  @Test
  public void testExcludesOtherDir() throws Exception {
    ImmutableList<String> inputFile = ImmutableList.of("a/x.txt", "a/y.txt");

    Map<String, String> testFiles =
        ImmutableMap.of(
            "a/x.txt", "",
            "a/y.txt", "",
            "a/b/", "");

    List<String> assertMounts = ImmutableList.of("a/x.txt", "a/y.txt");

    assertThat(userFriendlyMounts(testFiles, inputFile))
        .isEqualTo(userFriendlyAsserts(assertMounts));
  }

  @Test
  public void testExcludesOtherFiles() throws Exception {
    ImmutableList<String> inputFile = ImmutableList.of("a/x.txt", "a/z.txt");

    Map<String, String> testFiles =
        ImmutableMap.of(
            "a/x.txt", "",
            "a/y.txt", "z.txt",
            "a/z.txt", "");

    List<String> assertMounts = ImmutableList.of("a/x.txt", "a/z.txt");

    assertThat(userFriendlyMounts(testFiles, inputFile))
        .isEqualTo(userFriendlyAsserts(assertMounts));
  }

  @Test
  public void testRecognizesOtherSymlinks() throws Exception {
    ImmutableList<String> inputFile = ImmutableList.of("a/a/x.txt", "a/a/y.txt");

    Map<String, String> testFiles =
        ImmutableMap.of(
            "a/a/x.txt", "../b/x.txt",
            "a/a/y.txt", "",
            "a/b/x.txt", "");

    Map<String, String> assertMounts =
        ImmutableMap.of(
            "a/a/x.txt", "a/b/x.txt",
            "a/a/y.txt", "a/a/y.txt");

    assertThat(userFriendlyMounts(testFiles, inputFile))
        .isEqualTo(userFriendlyAsserts(assertMounts));
  }

  /**
   * Test that the algorithm correctly identifies and refuses symlink loops.
   */
  @Test
  public void testCatchesSymlinkLoop() throws Exception {
    try {
      mounts(
          ImmutableMap.of(
              "a", "b",
              "b", "a"));
      fail();
    } catch (IOException e) {
      assertThat(e)
          .hasMessage(
              String.format(
                  "%s (Too many levels of symbolic links)",
                  workspaceDir.getRelative("a").getPathString()));
    }
  }

  /**
   * Test that the algorithm correctly detects and refuses symlinks whose subcomponents are not all
   * directories (e.g. "a -> dir/file/file").
   */
  @Test
  public void testCatchesIllegalSymlink() throws Exception {
    try {
      mounts(
          ImmutableMap.of(
              "b", "a/c",
              "a", ""));
      fail();
    } catch (IOException e) {
      assertThat(e)
          .hasMessage(
              String.format(
                  "%s (Not a directory)", workspaceDir.getRelative("a/c").getPathString()));
    }
  }

  @Test
  public void testParseManifestFile() throws Exception {
    Path targetDir = workspaceDir.getRelative("runfiles");
    targetDir.createDirectory();

    Path testFile = workspaceDir.getRelative("testfile");
    FileSystemUtils.createEmptyFile(testFile);

    Path manifestFile = workspaceDir.getRelative("MANIFEST");
    FileSystemUtils.writeContent(
        manifestFile,
        Charset.defaultCharset(),
        String.format("x/testfile %s\nx/emptyfile \n", testFile.getPathString()));

    Map mounts =
        LinuxSandboxedStrategy.parseManifestFile(targetDir, manifestFile.getPathFile(), false, "");

    assertThat(userFriendlyMap(mounts))
        .isEqualTo(
            userFriendlyMap(
                ImmutableMap.of(
                    fileSystem.getPath("/runfiles/x/testfile"),
                    testFile,
                    fileSystem.getPath("/runfiles/x/emptyfile"),
                    fileSystem.getPath("/dev/null"))));
  }

  @Test
  public void testParseFilesetManifestFile() throws Exception {
    Path targetDir = workspaceDir.getRelative("fileset");
    targetDir.createDirectory();

    Path testFile = workspaceDir.getRelative("testfile");
    FileSystemUtils.createEmptyFile(testFile);

    Path manifestFile = workspaceDir.getRelative("MANIFEST");
    FileSystemUtils.writeContent(
        manifestFile,
        Charset.defaultCharset(),
        String.format("workspace/x/testfile %s\n0\n", testFile.getPathString()));

    Map mounts =
        LinuxSandboxedStrategy.parseManifestFile(
            targetDir, manifestFile.getPathFile(), true, "workspace");

    assertThat(userFriendlyMap(mounts))
        .isEqualTo(
            userFriendlyMap(ImmutableMap.of(fileSystem.getPath("/fileset/x/testfile"), testFile)));
  }
}