aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build/lib/vfs/ScopeEscapableFileSystemTest.java
blob: 719ffde41c35f409f9957123c28ee1d2dcbcaa8e (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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
// Copyright 2014 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.vfs;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;

import org.junit.Before;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;

/**
 * Generic tests for any file system that implements {@link ScopeEscapableFileSystem},
 * i.e. any file system that supports symlinks that escape its scope.
 *
 * Each suitable file system test should inherit from this class, thereby obtaining
 * all the tests.
 */
public abstract class ScopeEscapableFileSystemTest extends SymlinkAwareFileSystemTest {

  /**
   * Trivial FileSystem implementation that can record the last path passed to each method
   * and read/write to a unified "state" variable (which can then be checked by tests) for
   * each data type this class manipulates.
   *
   * The default implementation of each method throws an exception. Each test case should
   * selectively override the methods it expects to be invoked.
   */
  private static class TestDelegator extends FileSystem {
    protected Path lastPath;
    protected boolean booleanState;
    protected long longState;
    protected Object objectState;

    public void setState(boolean state) { booleanState = state; }
    public void setState(long state) { longState = state; }
    public void setState(Object state) { objectState = state; }

    public boolean booleanState() { return booleanState; }
    public long longState() { return longState; }
    public Object objectState() { return objectState; }

    public PathFragment lastPath() {
      Path ans = lastPath;
      // Clear this out to protect against accidental matches when testing the same path multiple
      // consecutive times.
      lastPath = null;
      return ans != null ? ans.asFragment() : null;
    }

    @Override public boolean supportsModifications() { return true; }
    @Override public boolean supportsSymbolicLinks() { return true; }

    private static RuntimeException re() {
      return new RuntimeException("This method should not be called in this context");
    }

    @Override protected boolean isReadable(Path path) { throw re(); }
    @Override protected boolean isWritable(Path path) { throw re(); }
    @Override protected boolean isDirectory(Path path, boolean followSymlinks) { throw re(); }
    @Override protected boolean isFile(Path path, boolean followSymlinks) { throw re(); }
    @Override protected boolean isExecutable(Path path) { throw re(); }
    @Override protected boolean exists(Path path, boolean followSymlinks) {throw re(); }
    @Override protected boolean isSymbolicLink(Path path) { throw re(); }
    @Override protected boolean createDirectory(Path path) { throw re(); }
    @Override protected boolean delete(Path path) { throw re(); }

    @Override protected long getFileSize(Path path, boolean followSymlinks) { throw re(); }
    @Override protected long getLastModifiedTime(Path path, boolean followSymlinks) { throw re(); }

    @Override protected void setWritable(Path path, boolean writable) { throw re(); }
    @Override protected void setExecutable(Path path, boolean executable) { throw re(); }
    @Override protected void setReadable(Path path, boolean readable) { throw re(); }
    @Override protected void setLastModifiedTime(Path path, long newTime) { throw re(); }
    @Override protected void renameTo(Path sourcePath, Path targetPath) { throw re(); }
    @Override protected void createSymbolicLink(Path linkPath, PathFragment targetFragment) {
      throw re();
    }

    @Override protected PathFragment readSymbolicLink(Path path) { throw re(); }
    @Override protected InputStream getInputStream(Path path) { throw re(); }
    @Override protected Collection<Path> getDirectoryEntries(Path path) { throw re(); }
    @Override protected OutputStream getOutputStream(Path path, boolean append)  { throw re(); }
    @Override
    protected FileStatus statIfFound(Path path, boolean followSymlinks) throws IOException {
      throw re();
    }
  }

  protected static final PathFragment SCOPE_ROOT = new PathFragment("/fs/root");

  private Path fileLink;
  private PathFragment fileLinkTarget;
  private Path dirLink;
  private PathFragment dirLinkTarget;

  @Override
  @Before
  public void setUp() throws Exception {
    super.setUp();

    Preconditions.checkState(
        testFS instanceof ScopeEscapableFileSystem, "Not ScopeEscapable: %s", testFS);
    ((ScopeEscapableFileSystem) testFS).enableScopeChecking(false);
    for (int i = 1; i <= SCOPE_ROOT.segmentCount(); i++) {
      testFS.getPath(SCOPE_ROOT.subFragment(0, i)).createDirectory();
    }

    fileLink = testFS.getPath(SCOPE_ROOT.getRelative("link"));
    fileLinkTarget = new PathFragment("/should/be/delegated/fileLinkTarget");
    testFS.createSymbolicLink(fileLink, fileLinkTarget);

    dirLink = testFS.getPath(SCOPE_ROOT.getRelative("dirlink"));
    dirLinkTarget = new PathFragment("/should/be/delegated/dirLinkTarget");
    testFS.createSymbolicLink(dirLink, dirLinkTarget);
  }

  /**
   * Returns the file system supplied by {@link #getFreshFileSystem}, cast to
   * a {@link ScopeEscapableFileSystem}. Also enables scope checking within
   * the file system (which we keep disabled for inherited tests that aren't
   * intended to test scope boundaries).
   */
  private ScopeEscapableFileSystem scopedFS() {
    ScopeEscapableFileSystem fs = (ScopeEscapableFileSystem) testFS;
    fs.enableScopeChecking(true);
    return fs;
  }

  // Checks that the semi-resolved path passed to the delegator matches the expected value.
  private void checkPath(TestDelegator delegator, PathFragment expectedDelegatedPath) {
    assertEquals(delegator.lastPath(), expectedDelegatedPath);
  }

  // Asserts that the condition is false and checks that the expected path was delegated.
  private void assertFalseWithPathCheck(boolean result, TestDelegator delegator,
      PathFragment expectedDelegatedPath) {
    assertFalse(result);
    checkPath(delegator, expectedDelegatedPath);
  }

  // Asserts that the condition is true and checks that the expected path was delegated.
  private void assertTrueWithPathCheck(boolean result, TestDelegator delegator,
      PathFragment expectedDelegatedPath) {
    assertTrue(result);
    checkPath(delegator, expectedDelegatedPath);
  }

  /////////////////////////////////////////////////////////////////////////////
  // Tests:
  /////////////////////////////////////////////////////////////////////////////

  @Test
  public void testIsReadableCallOnEscapingSymlink() throws Exception {
    TestDelegator delegator = new TestDelegator() {
      @Override protected boolean isReadable(Path path) {
        lastPath = path;
        return booleanState();
      }
    };
    scopedFS().setDelegator(delegator);

    delegator.setState(false);
    assertFalseWithPathCheck(fileLink.isReadable(), delegator, fileLinkTarget);
    assertFalseWithPathCheck(dirLink.getRelative("a").isReadable(), delegator,
        dirLinkTarget.getRelative("a"));

    delegator.setState(true);
    assertTrueWithPathCheck(fileLink.isReadable(), delegator, fileLinkTarget);
    assertTrueWithPathCheck(dirLink.getRelative("a").isReadable(), delegator,
        dirLinkTarget.getRelative("a"));
  }

  @Test
  public void testIsWritableCallOnEscapingSymlink() throws Exception {
    TestDelegator delegator = new TestDelegator() {
      @Override protected boolean isWritable(Path path) {
        lastPath = path;
        return booleanState();
      }
    };
    scopedFS().setDelegator(delegator);

    delegator.setState(false);
    assertFalseWithPathCheck(fileLink.isWritable(), delegator, fileLinkTarget);
    assertFalseWithPathCheck(dirLink.getRelative("a").isWritable(), delegator,
        dirLinkTarget.getRelative("a"));

    delegator.setState(true);
    assertTrueWithPathCheck(fileLink.isWritable(), delegator, fileLinkTarget);
    assertTrueWithPathCheck(dirLink.getRelative("a").isWritable(), delegator,
        dirLinkTarget.getRelative("a"));
  }

  @Test
  public void testisExecutableCallOnEscapingSymlink() throws Exception {
    TestDelegator delegator = new TestDelegator() {
      @Override protected boolean isExecutable(Path path) {
        lastPath = path;
        return booleanState();
      }
    };
    scopedFS().setDelegator(delegator);

    delegator.setState(false);
    assertFalseWithPathCheck(fileLink.isExecutable(), delegator, fileLinkTarget);
    assertFalseWithPathCheck(dirLink.getRelative("a").isExecutable(), delegator,
        dirLinkTarget.getRelative("a"));

    delegator.setState(true);
    assertTrueWithPathCheck(fileLink.isExecutable(), delegator, fileLinkTarget);
    assertTrueWithPathCheck(dirLink.getRelative("a").isExecutable(), delegator,
        dirLinkTarget.getRelative("a"));
  }

  @Test
  public void testIsDirectoryCallOnEscapingSymlink() throws Exception {
    TestDelegator delegator = new TestDelegator() {
      @Override protected boolean isDirectory(Path path, boolean followSymlinks) {
        lastPath = path;
        return booleanState();
      }
      @Override protected boolean exists(Path path, boolean followSymlinks) { return true; }
      @Override protected long getLastModifiedTime(Path path, boolean followSymlinks) { return 0; }
    };
    scopedFS().setDelegator(delegator);

    delegator.setState(false);
    assertFalseWithPathCheck(fileLink.isDirectory(), delegator, fileLinkTarget);
    assertFalseWithPathCheck(dirLink.getRelative("a").isDirectory(), delegator,
        dirLinkTarget.getRelative("a"));

    delegator.setState(true);
    assertTrueWithPathCheck(fileLink.isDirectory(), delegator, fileLinkTarget);
    assertTrueWithPathCheck(dirLink.getRelative("a").isDirectory(), delegator,
        dirLinkTarget.getRelative("a"));
  }

  @Test
  public void testIsFileCallOnEscapingSymlink() throws Exception {
    TestDelegator delegator = new TestDelegator() {
      @Override protected boolean isFile(Path path, boolean followSymlinks) {
        lastPath = path;
        return booleanState();
      }
      @Override protected boolean exists(Path path, boolean followSymlinks) { return true; }
      @Override protected long getLastModifiedTime(Path path, boolean followSymlinks) { return 0; }
    };
    scopedFS().setDelegator(delegator);

    delegator.setState(false);
    assertFalseWithPathCheck(fileLink.isFile(), delegator, fileLinkTarget);
    assertFalseWithPathCheck(dirLink.getRelative("a").isFile(), delegator,
        dirLinkTarget.getRelative("a"));

    delegator.setState(true);
    assertTrueWithPathCheck(fileLink.isFile(), delegator, fileLinkTarget);
    assertTrueWithPathCheck(dirLink.getRelative("a").isFile(), delegator,
        dirLinkTarget.getRelative("a"));
  }

  @Test
  public void testIsSymbolicLinkCallOnEscapingSymlink() throws Exception {
    TestDelegator delegator = new TestDelegator() {
      @Override protected boolean isSymbolicLink(Path path) {
        lastPath = path;
        return booleanState();
      }
      @Override protected boolean exists(Path path, boolean followSymlinks) { return true; }
      @Override protected long getLastModifiedTime(Path path, boolean followSymlinks) { return 0; }
      @Override protected boolean isDirectory(Path path, boolean followSymlinks) { return true; }
    };
    scopedFS().setDelegator(delegator);

    // We shouldn't follow final-segment links, so they should never invoke the delegator.
    delegator.setState(false);
    assertTrue(fileLink.isSymbolicLink());
    assertNull(delegator.lastPath());

    assertFalseWithPathCheck(dirLink.getRelative("a").isSymbolicLink(), delegator,
        dirLinkTarget.getRelative("a"));

    delegator.setState(true);
    assertTrueWithPathCheck(dirLink.getRelative("a").isSymbolicLink(), delegator,
        dirLinkTarget.getRelative("a"));
  }

  /**
   * Returns a test delegator that reflects info passed to Path.exists() calls.
   */
  private TestDelegator newExistsDelegator() {
    return new TestDelegator() {
      @Override protected boolean exists(Path path, boolean followSymlinks) {
        lastPath = path;
        return booleanState();
      }
      @Override protected FileStatus stat(Path path, boolean followSymlinks) throws IOException {
        if (!exists(path, followSymlinks)) {
          throw new IOException("Expected exception on stat of non-existent file");
        }
        return super.stat(path, followSymlinks);
      }
      @Override protected long getLastModifiedTime(Path path, boolean followSymlinks) { return 0; }
    };
  }

  @Test
  public void testExistsCallOnEscapingSymlink() throws Exception {
    TestDelegator delegator = newExistsDelegator();
    scopedFS().setDelegator(delegator);

    delegator.setState(false);
    assertFalseWithPathCheck(fileLink.exists(), delegator, fileLinkTarget);
    assertFalseWithPathCheck(dirLink.getRelative("a").exists(), delegator,
        dirLinkTarget.getRelative("a"));

    delegator.setState(true);
    assertTrueWithPathCheck(fileLink.exists(), delegator, fileLinkTarget);
    assertTrueWithPathCheck(dirLink.getRelative("a").exists(), delegator,
        dirLinkTarget.getRelative("a"));
  }

  @Test
  public void testCreateDirectoryCallOnEscapingSymlink() throws Exception {
    TestDelegator delegator = new TestDelegator() {
      @Override protected boolean createDirectory(Path path) {
        lastPath = path;
        return booleanState();
      }
      @Override protected boolean isDirectory(Path path, boolean followSymlinks) { return true; }
    };
    scopedFS().setDelegator(delegator);

    delegator.setState(false);
    assertFalseWithPathCheck(dirLink.getRelative("a").createDirectory(), delegator,
        dirLinkTarget.getRelative("a"));

    delegator.setState(true);
    assertTrueWithPathCheck(dirLink.getRelative("a").createDirectory(), delegator,
        dirLinkTarget.getRelative("a"));
  }

  @Test
  public void testDeleteCallOnEscapingSymlink() throws Exception {
    TestDelegator delegator = new TestDelegator() {
      @Override protected boolean delete(Path path) {
        lastPath = path;
        return booleanState();
      }
      @Override protected boolean isDirectory(Path path, boolean followSymlinks) { return true; }
      @Override protected long getLastModifiedTime(Path path, boolean followSymlinks) { return 0; }
    };
    scopedFS().setDelegator(delegator);

    delegator.setState(false);
    assertTrue(fileLink.delete());
    assertNull(delegator.lastPath()); // Deleting a link shouldn't require delegation.
    assertFalseWithPathCheck(dirLink.getRelative("a").delete(), delegator,
        dirLinkTarget.getRelative("a"));

    delegator.setState(true);
    assertTrueWithPathCheck(dirLink.getRelative("a").delete(), delegator,
        dirLinkTarget.getRelative("a"));
  }

  @Test
  public void testCallGetFileSizeOnEscapingSymlink() throws Exception {
    TestDelegator delegator = new TestDelegator() {
      @Override protected long getFileSize(Path path, boolean followSymlinks) {
        lastPath = path;
        return longState();
      }
      @Override protected long getLastModifiedTime(Path path, boolean followSymlinks) { return 0; }
    };
    scopedFS().setDelegator(delegator);

    final int state1 = 10;
    delegator.setState(state1);
    assertEquals(state1, fileLink.getFileSize());
    checkPath(delegator, fileLinkTarget);
    assertEquals(state1, dirLink.getRelative("a").getFileSize());
    checkPath(delegator, dirLinkTarget.getRelative("a"));

    final int state2 = 10;
    delegator.setState(state2);
    assertEquals(state2, fileLink.getFileSize());
    checkPath(delegator, fileLinkTarget);
    assertEquals(state2, dirLink.getRelative("a").getFileSize());
    checkPath(delegator, dirLinkTarget.getRelative("a"));
   }

  @Test
  public void testCallGetLastModifiedTimeOnEscapingSymlink() throws Exception {
    TestDelegator delegator = new TestDelegator() {
      @Override protected long getLastModifiedTime(Path path, boolean followSymlinks) {
        lastPath = path;
        return longState();
      }
    };
    scopedFS().setDelegator(delegator);

    final int state1 = 10;
    delegator.setState(state1);
    assertEquals(state1, fileLink.getLastModifiedTime());
    checkPath(delegator, fileLinkTarget);
    assertEquals(state1, dirLink.getRelative("a").getLastModifiedTime());
    checkPath(delegator, dirLinkTarget.getRelative("a"));

    final int state2 = 10;
    delegator.setState(state2);
    assertEquals(state2, fileLink.getLastModifiedTime());
    checkPath(delegator, fileLinkTarget);
    assertEquals(state2, dirLink.getRelative("a").getLastModifiedTime());
    checkPath(delegator, dirLinkTarget.getRelative("a"));
  }

  @Test
  public void testCallSetReadableOnEscapingSymlink() throws Exception {
    TestDelegator delegator = new TestDelegator() {
      @Override protected void setReadable(Path path, boolean readable) {
        lastPath = path;
        setState(readable);
      }
    };
    scopedFS().setDelegator(delegator);

    delegator.setState(false);
    fileLink.setReadable(true);
    assertTrue(delegator.booleanState());
    checkPath(delegator, fileLinkTarget);
    fileLink.setReadable(false);
    assertFalse(delegator.booleanState());
    checkPath(delegator, fileLinkTarget);

    delegator.setState(false);
    dirLink.getRelative("a").setReadable(true);
    assertTrue(delegator.booleanState());
    checkPath(delegator, dirLinkTarget.getRelative("a"));
    dirLink.getRelative("a").setReadable(false);
    assertFalse(delegator.booleanState());
    checkPath(delegator, dirLinkTarget.getRelative("a"));
  }

  @Test
  public void testCallSetWritableOnEscapingSymlink() throws Exception {
    TestDelegator delegator = new TestDelegator() {
      @Override protected void setWritable(Path path, boolean writable) {
        lastPath = path;
        setState(writable);
      }
    };
    scopedFS().setDelegator(delegator);

    delegator.setState(false);
    fileLink.setWritable(true);
    assertTrue(delegator.booleanState());
    checkPath(delegator, fileLinkTarget);
    fileLink.setWritable(false);
    assertFalse(delegator.booleanState());
    checkPath(delegator, fileLinkTarget);

    delegator.setState(false);
    dirLink.getRelative("a").setWritable(true);
    assertTrue(delegator.booleanState());
    checkPath(delegator, dirLinkTarget.getRelative("a"));
    dirLink.getRelative("a").setWritable(false);
    assertFalse(delegator.booleanState());
    checkPath(delegator, dirLinkTarget.getRelative("a"));
  }

  @Test
  public void testCallSetExecutableOnEscapingSymlink() throws Exception {
    TestDelegator delegator = new TestDelegator() {
      @Override protected void setReadable(Path path, boolean readable) {
        lastPath = path;
        setState(readable);
      }
    };
    scopedFS().setDelegator(delegator);

    delegator.setState(false);
    fileLink.setReadable(true);
    assertTrue(delegator.booleanState());
    checkPath(delegator, fileLinkTarget);
    fileLink.setReadable(false);
    assertFalse(delegator.booleanState());
    checkPath(delegator, fileLinkTarget);

    delegator.setState(false);
    dirLink.getRelative("a").setReadable(true);
    assertTrue(delegator.booleanState());
    checkPath(delegator, dirLinkTarget.getRelative("a"));
    dirLink.getRelative("a").setReadable(false);
    assertFalse(delegator.booleanState());
    checkPath(delegator, dirLinkTarget.getRelative("a"));
  }

  @Test
  public void testCallSetLastModifiedTimeOnEscapingSymlink() throws Exception {
    TestDelegator delegator = new TestDelegator() {
      @Override protected void setLastModifiedTime(Path path, long newTime) {
        lastPath = path;
        setState(newTime);
      }
    };
    scopedFS().setDelegator(delegator);

    delegator.setState(0);
    fileLink.setLastModifiedTime(10);
    assertEquals(10, delegator.longState());
    checkPath(delegator, fileLinkTarget);
    fileLink.setLastModifiedTime(15);
    assertEquals(15, delegator.longState());
    checkPath(delegator, fileLinkTarget);

    dirLink.getRelative("a").setLastModifiedTime(20);
    assertEquals(20, delegator.longState());
    checkPath(delegator, dirLinkTarget.getRelative("a"));
    dirLink.getRelative("a").setLastModifiedTime(25);
    assertEquals(25, delegator.longState());
    checkPath(delegator, dirLinkTarget.getRelative("a"));
  }

  @Test
  public void testCallRenameToOnEscapingSymlink() throws Exception {
    TestDelegator delegator = new TestDelegator() {
      @Override protected void renameTo(Path sourcePath, Path targetPath) {
        lastPath = sourcePath;
        setState(targetPath);
      }
      @Override protected boolean isDirectory(Path path, boolean followSymlinks) { return true; }
    };
    scopedFS().setDelegator(delegator);

    // Renaming a link should work fine.
    delegator.setState(null);
    fileLink.renameTo(testFS.getPath(SCOPE_ROOT).getRelative("newname"));
    assertNull(delegator.lastPath()); // Renaming a link shouldn't require delegation.
    assertNull(delegator.objectState());

    // Renaming an out-of-scope path to an in-scope path should fail due to filesystem mismatch
    // errors.
    Path newPath = testFS.getPath(SCOPE_ROOT.getRelative("blah"));
    try {
      dirLink.getRelative("a").renameTo(newPath);
      fail("This is an attempt at a cross-filesystem renaming, which should fail");
    } catch (IOException e) {
      // Expected.
    }

    // Renaming an out-of-scope path to another out-of-scope path can be valid.
    newPath = dirLink.getRelative("b");
    dirLink.getRelative("a").renameTo(newPath);
    assertEquals(dirLinkTarget.getRelative("a"), delegator.lastPath());
    assertEquals(dirLinkTarget.getRelative("b"), ((Path) delegator.objectState()).asFragment());
  }

  @Test
  public void testCallCreateSymbolicLinkOnEscapingSymlink() throws Exception {
    TestDelegator delegator = new TestDelegator() {
      @Override protected void createSymbolicLink(Path linkPath, PathFragment targetFragment) {
        lastPath = linkPath;
        setState(targetFragment);
      }
      @Override protected boolean isDirectory(Path path, boolean followSymlinks) { return true; }
    };
    scopedFS().setDelegator(delegator);

    PathFragment newLinkTarget = new PathFragment("/something/else");
    dirLink.getRelative("a").createSymbolicLink(newLinkTarget);
    assertEquals(dirLinkTarget.getRelative("a"), delegator.lastPath());
    assertSame(newLinkTarget, delegator.objectState());
  }

  @Test
  public void testCallReadSymbolicLinkOnEscapingSymlink() throws Exception {
    TestDelegator delegator = new TestDelegator() {
      @Override protected PathFragment readSymbolicLink(Path path) {
        lastPath = path;
        return (PathFragment) objectState;
      }
      @Override protected boolean isDirectory(Path path, boolean followSymlinks) { return true; }
    };
    scopedFS().setDelegator(delegator);

    // Since we're not following the link, this shouldn't invoke delegation.
    delegator.setState(new PathFragment("whatever"));
    PathFragment p = fileLink.readSymbolicLink();
    assertNull(delegator.lastPath());
    assertNotSame(delegator.objectState(), p);

    // This should.
    p = dirLink.getRelative("a").readSymbolicLink();
    assertEquals(dirLinkTarget.getRelative("a"), delegator.lastPath());
    assertSame(delegator.objectState(), p);
  }

  @Test
  public void testCallGetInputStreamOnEscapingSymlink() throws Exception {
    TestDelegator delegator = new TestDelegator() {
      @Override protected InputStream getInputStream(Path path) {
        lastPath = path;
        return (InputStream) objectState;
      }
    };
    scopedFS().setDelegator(delegator);

    delegator.setState(new ByteArrayInputStream("blah".getBytes()));
    InputStream is = fileLink.getInputStream();
    assertEquals(fileLinkTarget, delegator.lastPath());
    assertSame(delegator.objectState(), is);

    delegator.setState(new ByteArrayInputStream("blah2".getBytes()));
    is = dirLink.getInputStream();
    assertEquals(dirLinkTarget, delegator.lastPath());
    assertSame(delegator.objectState(), is);
  }

  @Test
  public void testCallGetOutputStreamOnEscapingSymlink() throws Exception {
    TestDelegator delegator = new TestDelegator() {
      @Override protected OutputStream getOutputStream(Path path, boolean append)  {
        lastPath = path;
        return (OutputStream) objectState;
      }
      @Override protected boolean isDirectory(Path path, boolean followSymlinks) { return true; }
    };
    scopedFS().setDelegator(delegator);

    delegator.setState(new ByteArrayOutputStream());
    OutputStream os = fileLink.getOutputStream();
    assertEquals(fileLinkTarget, delegator.lastPath());
    assertSame(delegator.objectState(), os);

    delegator.setState(new ByteArrayOutputStream());
    os = dirLink.getOutputStream();
    assertEquals(dirLinkTarget, delegator.lastPath());
    assertSame(delegator.objectState(), os);
  }

  @Test
  public void testCallGetDirectoryEntriesOnEscapingSymlink() throws Exception {
    TestDelegator delegator = new TestDelegator() {
      @Override protected Collection<Path> getDirectoryEntries(Path path) {
        lastPath = path;
        return ImmutableList.of((Path) objectState);
      }
      @Override protected boolean isDirectory(Path path, boolean followSymlinks) { return true; }
    };
    scopedFS().setDelegator(delegator);

    delegator.setState(testFS.getPath("/anything"));
    Collection<Path> entries = dirLink.getDirectoryEntries();
    assertEquals(dirLinkTarget, delegator.lastPath());
    assertThat(entries).hasSize(1);
    assertSame(delegator.objectState(), entries.iterator().next());
  }

  /**
   * Asserts that "link" is an in-scope link that doesn't result in an out-of-FS
   * delegation. If link is relative, its path is relative to SCOPE_ROOT.
   *
   * Note that we don't actually check that the canonicalized target path matches
   * the link's target value. Such testing should be covered by
   * SymlinkAwareFileSystemTest.
   */
  private void assertInScopeLink(String link, String target, TestDelegator d) throws IOException {
    Path l = testFS.getPath(SCOPE_ROOT.getRelative(link));
    testFS.createSymbolicLink(l, new PathFragment(target));
    l.exists();
    assertNull(d.lastPath());
  }

  /**
   * Asserts that "link" is an out-of-scope link and that the re-delegated path
   * matches expectedPath. If link is relative, its path is relative to SCOPE_ROOT.
   */
  private void assertOutOfScopeLink(String link, String target, String expectedPath,
      TestDelegator d) throws IOException {
    Path l = testFS.getPath(SCOPE_ROOT.getRelative(link));
    testFS.createSymbolicLink(l, new PathFragment(target));
    l.exists();
    assertEquals(expectedPath, d.lastPath().getPathString());
  }

  /**
   * Returns the scope root with the final n segments chopped off (or a 0-segment path
   * if n > SCOPE_ROOT.segmentCount()).
   */
  private String chopScopeRoot(int n) {
    return SCOPE_ROOT
        .subFragment(0, n > SCOPE_ROOT.segmentCount() ? 0 : SCOPE_ROOT.segmentCount() - n)
        .getPathString();
  }

  /**
   * Tests that absolute symlinks with ".." and "." segments are delegated to
   * the expected paths.
   */
  @Test
  public void testAbsoluteSymlinksWithParentReferences() throws Exception {
    TestDelegator d = newExistsDelegator();
    scopedFS().setDelegator(d);
    testFS.createDirectory(testFS.getPath(SCOPE_ROOT.getRelative("dir")));
    String scopeRoot = SCOPE_ROOT.getPathString();
    String scopeBase = SCOPE_ROOT.getBaseName();

    // Symlinks that should never escape our scope.
    assertInScopeLink("ilink1", scopeRoot, d);
    assertInScopeLink("ilink2", scopeRoot + "/target", d);
    assertInScopeLink("ilink3", scopeRoot + "/dir/../target", d);
    assertInScopeLink("ilink4", scopeRoot + "/dir/../dir/dir2/../target", d);
    assertInScopeLink("ilink5", scopeRoot + "/./dir/.././target", d);
    assertInScopeLink("ilink6", scopeRoot + "/../" + scopeBase + "/target", d);
    assertInScopeLink("ilink7", "/some/path/../.." + scopeRoot + "/target", d);

    // Symlinks that should escape our scope.
    assertOutOfScopeLink("olink1", scopeRoot + "/../target", chopScopeRoot(1) + "/target", d);
    assertOutOfScopeLink("olink2", "/some/other/path", "/some/other/path", d);
    assertOutOfScopeLink("olink3", scopeRoot + "/../target", chopScopeRoot(1) + "/target", d);
    assertOutOfScopeLink("olink4", chopScopeRoot(1) + "/target", chopScopeRoot(1) + "/target", d);
    assertOutOfScopeLink("olink5", scopeRoot + "/../../../../target", "/target", d);

    // In-scope symlink that's not the final segment in a query.
    Path iDirLink = testFS.getPath(SCOPE_ROOT.getRelative("ilinkdir"));
    testFS.createSymbolicLink(iDirLink, SCOPE_ROOT.getRelative("dir"));
    iDirLink.getRelative("file").exists();
    assertNull(d.lastPath());

    // Out-of-scope symlink that's not the final segment in a query.
    Path oDirLink = testFS.getPath(SCOPE_ROOT.getRelative("olinkdir"));
    testFS.createSymbolicLink(oDirLink, new PathFragment("/some/other/dir"));
    oDirLink.getRelative("file").exists();
    assertEquals("/some/other/dir/file", d.lastPath().getPathString());
  }

  /**
   * Tests that relative symlinks with ".." and "." segments are delegated to
   * the expected paths.
   */
  @Test
  public void testRelativeSymlinksWithParentReferences() throws Exception {
    TestDelegator d = newExistsDelegator();
    scopedFS().setDelegator(d);
    testFS.createDirectory(testFS.getPath(SCOPE_ROOT.getRelative("dir")));
    testFS.createDirectory(testFS.getPath(SCOPE_ROOT.getRelative("dir/dir2")));
    testFS.createDirectory(testFS.getPath(SCOPE_ROOT.getRelative("dir/dir2/dir3")));
    String scopeRoot = SCOPE_ROOT.getPathString();
    String scopeBase = SCOPE_ROOT.getBaseName();

    // Symlinks that should never escape our scope.
    assertInScopeLink("ilink1", "target", d);
    assertInScopeLink("ilink2", "dir/../otherdir/target", d);
    assertInScopeLink("dir/ilink3", "../target", d);
    assertInScopeLink("dir/dir2/ilink4", "../../target", d);
    assertInScopeLink("dir/dir2/ilink5", ".././../dir/./target", d);
    assertInScopeLink("dir/dir2/ilink6", "../dir2/../../dir/dir2/dir3/../../../target", d);

    // Symlinks that should escape our scope.
    assertOutOfScopeLink("olink1", "../target", chopScopeRoot(1) + "/target", d);
    assertOutOfScopeLink("dir/olink2", "../../target", chopScopeRoot(1) + "/target", d);
    assertOutOfScopeLink("olink3", "../" + scopeBase + "/target", scopeRoot + "/target", d);
    assertOutOfScopeLink("dir/dir2/olink5", "../../../target", chopScopeRoot(1) + "/target", d);
    assertOutOfScopeLink("dir/dir2/olink6", "../dir2/../../dir/dir2/../../../target",
        chopScopeRoot(1) + "/target", d);
    assertOutOfScopeLink("dir/olink7", "../../../target", chopScopeRoot(2) + "target", d);
    assertOutOfScopeLink("olink8", "../../../../../target", "/target", d);

    // In-scope symlink that's not the final segment in a query.
    Path iDirLink = testFS.getPath(SCOPE_ROOT.getRelative("dir/dir2/ilinkdir"));
    testFS.createSymbolicLink(iDirLink, new PathFragment("../../dir"));
    iDirLink.getRelative("file").exists();
    assertNull(d.lastPath());

    // Out-of-scope symlink that's not the final segment in a query.
    Path oDirLink = testFS.getPath(SCOPE_ROOT.getRelative("dir/dir2/olinkdir"));
    testFS.createSymbolicLink(oDirLink, new PathFragment("../../../other/dir"));
    oDirLink.getRelative("file").exists();
    assertEquals(chopScopeRoot(1) + "/other/dir/file", d.lastPath().getPathString());
  }
}