98fca000e1
Uploads land only once complete, text saves keep their encoding and refuse a file that moved on, and folders stream as zips. AllowFileAccess turns all of it off locally, whatever the server asks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
502 lines
21 KiB
C#
502 lines
21 KiB
C#
using System.IO.Compression;
|
|
using System.Text;
|
|
using Agent.Files;
|
|
using Shared.Contracts.Messages;
|
|
using Xunit;
|
|
|
|
namespace Agent.Tests;
|
|
|
|
/// <summary>What the file browser does to a real folder.</summary>
|
|
public sealed class DeviceFileServiceTests : IDisposable
|
|
{
|
|
private readonly string root = Directory.CreateTempSubdirectory("device-files-").FullName;
|
|
|
|
public void Dispose()
|
|
{
|
|
foreach (var file in Directory.EnumerateFiles(root, "*", SearchOption.AllDirectories))
|
|
{
|
|
File.SetAttributes(file, FileAttributes.Normal);
|
|
}
|
|
|
|
Directory.Delete(root, recursive: true);
|
|
}
|
|
|
|
private string At(params string[] parts) => Path.Combine([root, .. parts]);
|
|
|
|
private string Touch(string name, string content = "x")
|
|
{
|
|
var path = At(name);
|
|
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
|
|
File.WriteAllText(path, content);
|
|
return path;
|
|
}
|
|
|
|
[Fact]
|
|
public void AListHasFoldersFirstThenNames_AndPagesAndFilters()
|
|
{
|
|
Touch("b.txt");
|
|
Touch("A.log");
|
|
Directory.CreateDirectory(At("z-folder"));
|
|
|
|
var all = DeviceFileService.List(new DeviceFileRequest(DeviceFileOperations.List, Path: root));
|
|
Assert.Equal(DeviceFileStatuses.Ok, all.Status);
|
|
Assert.Equal(["z-folder", "A.log", "b.txt"], all.Entries!.Select(e => e.Name));
|
|
Assert.Equal(3, all.Total);
|
|
Assert.True(all.Entries![0].IsFolder);
|
|
Assert.Equal(1, all.Entries![1].SizeBytes);
|
|
|
|
var page = DeviceFileService.List(new DeviceFileRequest(DeviceFileOperations.List, Path: root, Skip: 1, Take: 1));
|
|
Assert.Equal(["A.log"], page.Entries!.Select(e => e.Name));
|
|
Assert.Equal(3, page.Total);
|
|
|
|
var found = DeviceFileService.List(new DeviceFileRequest(DeviceFileOperations.List, Path: root, Search: "LOG"));
|
|
Assert.Equal(["A.log"], found.Entries!.Select(e => e.Name));
|
|
}
|
|
|
|
[Fact]
|
|
public void AListExpandsEnvironmentVariablesOnTheDevice()
|
|
{
|
|
var name = "EXECUTER_TEST_" + Guid.NewGuid().ToString("N");
|
|
Environment.SetEnvironmentVariable(name, root);
|
|
try
|
|
{
|
|
var asked = OperatingSystem.IsWindows() ? $"%{name}%" : root;
|
|
var reply = DeviceFileService.List(new DeviceFileRequest(DeviceFileOperations.List, Path: asked));
|
|
Assert.Equal(Path.GetFullPath(root), reply.Path);
|
|
}
|
|
finally
|
|
{
|
|
Environment.SetEnvironmentVariable(name, null);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void AListOfAFileOrNothingSaysSo()
|
|
{
|
|
var file = Touch("a.txt");
|
|
Assert.Equal(DeviceFileStatuses.Invalid, DeviceFileService.List(new DeviceFileRequest(DeviceFileOperations.List, Path: file)).Status);
|
|
Assert.Equal(DeviceFileStatuses.NotFound, DeviceFileService.List(new DeviceFileRequest(DeviceFileOperations.List, Path: At("nope"))).Status);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("relative/path")]
|
|
[InlineData("")]
|
|
public void APathMustBeAbsolute(string path)
|
|
{
|
|
var reply = DeviceFileService.Guarded(() => DeviceFileService.List(new DeviceFileRequest(DeviceFileOperations.List, Path: path)));
|
|
Assert.Equal(DeviceFileStatuses.Invalid, reply.Status);
|
|
}
|
|
|
|
[Fact]
|
|
public void RootsIncludeWhereWeAre()
|
|
{
|
|
var reply = DeviceFileService.Roots();
|
|
Assert.Contains(reply.Roots!, r => string.Equals(r.Path, Path.GetPathRoot(root), StringComparison.OrdinalIgnoreCase));
|
|
Assert.Equal(Path.DirectorySeparatorChar.ToString(), reply.Separator);
|
|
}
|
|
|
|
[Fact]
|
|
public void StatReturnsOnlyWhatExists()
|
|
{
|
|
var file = Touch("a.txt");
|
|
var reply = DeviceFileService.Stat(new DeviceFileRequest(DeviceFileOperations.Stat, Paths: [file, At("missing.txt")]));
|
|
Assert.Equal([file], reply.Entries!.Select(e => e.Path));
|
|
}
|
|
|
|
[Fact]
|
|
public void AFolderIsCreatedOnce()
|
|
{
|
|
var path = At("new", "nested");
|
|
Assert.Equal(DeviceFileStatuses.Ok, DeviceFileService.CreateFolder(new DeviceFileRequest(DeviceFileOperations.CreateFolder, Path: path)).Status);
|
|
Assert.True(Directory.Exists(path));
|
|
Assert.Equal(DeviceFileStatuses.Conflict, DeviceFileService.CreateFolder(new DeviceFileRequest(DeviceFileOperations.CreateFolder, Path: path)).Status);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("..")]
|
|
[InlineData("a/b")]
|
|
[InlineData("a\\b")]
|
|
[InlineData("")]
|
|
public void ARenameStaysInItsFolder(string name)
|
|
{
|
|
var file = Touch("a.txt");
|
|
var reply = DeviceFileService.Rename(new DeviceFileRequest(DeviceFileOperations.Rename, Path: file, Name: name));
|
|
Assert.Equal(DeviceFileStatuses.Invalid, reply.Status);
|
|
Assert.True(File.Exists(file));
|
|
}
|
|
|
|
[Fact]
|
|
public void ARenameMovesFilesAndFolders_AndNeverOverwrites()
|
|
{
|
|
var file = Touch("a.txt", "a");
|
|
Touch("b.txt", "b");
|
|
Assert.Equal(DeviceFileStatuses.Conflict, DeviceFileService.Rename(new DeviceFileRequest(DeviceFileOperations.Rename, Path: file, Name: "b.txt")).Status);
|
|
Assert.Equal("b", File.ReadAllText(At("b.txt")));
|
|
|
|
Assert.Equal(DeviceFileStatuses.Ok, DeviceFileService.Rename(new DeviceFileRequest(DeviceFileOperations.Rename, Path: file, Name: "c.txt")).Status);
|
|
Assert.Equal("a", File.ReadAllText(At("c.txt")));
|
|
|
|
Directory.CreateDirectory(At("folder"));
|
|
var folder = DeviceFileService.Rename(new DeviceFileRequest(DeviceFileOperations.Rename, Path: At("folder"), Name: "renamed"));
|
|
Assert.Equal(At("renamed"), folder.Entry!.Path);
|
|
}
|
|
|
|
[Fact]
|
|
public void ADeleteTakesEachPathOnItsOwn()
|
|
{
|
|
var file = Touch("a.txt");
|
|
var reply = DeviceFileService.Delete(new DeviceFileRequest(DeviceFileOperations.Delete, Paths: [At("missing"), file]));
|
|
|
|
Assert.Equal([DeviceFileStatuses.NotFound, DeviceFileStatuses.Ok], reply.Deleted!.Select(d => d.Status));
|
|
Assert.False(File.Exists(file));
|
|
}
|
|
|
|
[Fact]
|
|
public void AFolderWithAnythingInItGoesOnlyWhenAsked()
|
|
{
|
|
Touch(Path.Combine("full", "sub", "deep.txt"));
|
|
var first = DeviceFileService.Delete(new DeviceFileRequest(DeviceFileOperations.Delete, Paths: [At("full")]));
|
|
Assert.Equal(DeviceFileStatuses.Conflict, first.Deleted![0].Status);
|
|
Assert.True(Directory.Exists(At("full")));
|
|
|
|
var second = DeviceFileService.Delete(new DeviceFileRequest(DeviceFileOperations.Delete, Paths: [At("full")], Recursive: true));
|
|
Assert.Equal(DeviceFileStatuses.Ok, second.Deleted![0].Status);
|
|
Assert.False(Directory.Exists(At("full")));
|
|
}
|
|
|
|
[Fact]
|
|
public void ADriveRootIsNeverDeleted()
|
|
{
|
|
var reply = DeviceFileService.Delete(new DeviceFileRequest(DeviceFileOperations.Delete, Paths: [Path.GetPathRoot(root)!], Recursive: true));
|
|
Assert.Equal(DeviceFileStatuses.Invalid, reply.Deleted![0].Status);
|
|
}
|
|
|
|
[Fact]
|
|
public void ReadOnlyFilesCanBeDeleted_AlsoInsideAFolder()
|
|
{
|
|
File.SetAttributes(Touch("ro.txt"), FileAttributes.ReadOnly);
|
|
File.SetAttributes(Touch(Path.Combine("folder", "ro.txt")), FileAttributes.ReadOnly);
|
|
|
|
var reply = DeviceFileService.Delete(new DeviceFileRequest(DeviceFileOperations.Delete, Paths: [At("ro.txt"), At("folder")], Recursive: true));
|
|
|
|
Assert.All(reply.Deleted!, d => Assert.Equal(DeviceFileStatuses.Ok, d.Status));
|
|
}
|
|
|
|
[Fact]
|
|
public void DeletingALinkLeavesWhatItPointsAt()
|
|
{
|
|
Touch(Path.Combine("real", "keep.txt"));
|
|
try
|
|
{
|
|
Directory.CreateSymbolicLink(At("link"), At("real"));
|
|
}
|
|
catch (IOException)
|
|
{
|
|
Assert.Skip("Creating a symbolic link needs a privilege this run doesn't have.");
|
|
}
|
|
catch (UnauthorizedAccessException)
|
|
{
|
|
Assert.Skip("Creating a symbolic link needs a privilege this run doesn't have.");
|
|
}
|
|
|
|
var reply = DeviceFileService.Delete(new DeviceFileRequest(DeviceFileOperations.Delete, Paths: [At("link")], Recursive: true));
|
|
|
|
Assert.Equal(DeviceFileStatuses.Ok, reply.Deleted![0].Status);
|
|
Assert.False(Directory.Exists(At("link")));
|
|
Assert.True(File.Exists(At("real", "keep.txt")));
|
|
}
|
|
|
|
[Fact]
|
|
public void OneFileReadsAsItself_AndAnythingElseAsAZip()
|
|
{
|
|
var file = Touch("a.txt", "12345");
|
|
Directory.CreateDirectory(At("f"));
|
|
|
|
var single = DeviceFileService.PrepareRead(new DeviceFileRequest(DeviceFileOperations.Read, Paths: [file]));
|
|
Assert.Equal(5, single.Length);
|
|
Assert.Equal(file, single.Entry!.Path);
|
|
|
|
var folder = DeviceFileService.PrepareRead(new DeviceFileRequest(DeviceFileOperations.Read, Paths: [At("f")]));
|
|
Assert.Equal(DeviceFileStatuses.Ok, folder.Status);
|
|
Assert.Null(folder.Entry);
|
|
|
|
Assert.Equal(DeviceFileStatuses.NotFound, DeviceFileService.PrepareRead(new DeviceFileRequest(DeviceFileOperations.Read, Paths: [file, At("gone")])).Status);
|
|
}
|
|
|
|
/// <summary>A stream that can't seek, as a transfer is.</summary>
|
|
private sealed class ForwardOnly(Stream inner) : Stream
|
|
{
|
|
public override bool CanRead => false;
|
|
public override bool CanSeek => false;
|
|
public override bool CanWrite => true;
|
|
public override long Length => throw new NotSupportedException();
|
|
public override long Position { get => throw new NotSupportedException(); set => throw new NotSupportedException(); }
|
|
public override void Flush() => inner.Flush();
|
|
public override int Read(byte[] buffer, int offset, int count) => throw new NotSupportedException();
|
|
public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
|
|
public override void SetLength(long value) => throw new NotSupportedException();
|
|
public override void Write(byte[] buffer, int offset, int count) => inner.Write(buffer, offset, count);
|
|
}
|
|
|
|
[Fact]
|
|
public void AZipHoldsFoldersAndFiles_NamedUnderTheirOwnFolder()
|
|
{
|
|
Touch(Path.Combine("logs", "a.log"), "aaa");
|
|
Touch(Path.Combine("logs", "sub", "b.log"), "bbb");
|
|
Directory.CreateDirectory(At("logs", "empty"));
|
|
var other = Touch("other.txt", "ccc");
|
|
|
|
using var bytes = new MemoryStream();
|
|
DeviceFileService.WriteZip([At("logs"), other], new ForwardOnly(bytes), TestContext.Current.CancellationToken);
|
|
|
|
bytes.Position = 0;
|
|
using var zip = new ZipArchive(bytes);
|
|
Assert.Equal(
|
|
["logs/a.log", "logs/empty/", "logs/sub/b.log", "other.txt"],
|
|
zip.Entries.Select(e => e.FullName).Order(StringComparer.Ordinal));
|
|
using var reader = new StreamReader(zip.GetEntry("logs/sub/b.log")!.Open());
|
|
Assert.Equal("bbb", reader.ReadToEnd());
|
|
}
|
|
|
|
[Fact]
|
|
public void ALockedFileIsListedInTheZip_NotTheEndOfIt()
|
|
{
|
|
if (!OperatingSystem.IsWindows())
|
|
{
|
|
Assert.Skip("Only Windows holds a file exclusively.");
|
|
}
|
|
|
|
var locked = Touch(Path.Combine("logs", "locked.log"));
|
|
Touch(Path.Combine("logs", "ok.log"));
|
|
using var holder = new FileStream(locked, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
|
|
|
|
using var bytes = new MemoryStream();
|
|
DeviceFileService.WriteZip([At("logs")], new ForwardOnly(bytes), TestContext.Current.CancellationToken);
|
|
|
|
bytes.Position = 0;
|
|
using var zip = new ZipArchive(bytes);
|
|
Assert.NotNull(zip.GetEntry("logs/ok.log"));
|
|
Assert.Null(zip.GetEntry("logs/locked.log"));
|
|
using var reader = new StreamReader(zip.GetEntry("ExecuteR.Skipped.txt")!.Open());
|
|
Assert.Contains("locked.log", reader.ReadToEnd());
|
|
}
|
|
|
|
private static DeviceFileRequest Write(string path, bool overwrite = false, long? length = null)
|
|
=> new(DeviceFileOperations.Write, Path: path, Overwrite: overwrite, Length: length);
|
|
|
|
[Fact]
|
|
public async Task AnUploadAppearsOnlyOnceAllOfItArrived()
|
|
{
|
|
var target = At("new", "folder", "file.bin");
|
|
var (reply, upload) = DeviceFileService.PrepareWrite(Write(target, length: 3));
|
|
Assert.Equal(DeviceFileStatuses.Ok, reply.Status);
|
|
|
|
using (upload)
|
|
{
|
|
await upload!.WriteAsync(new byte[] { 1, 2, 3 }, TestContext.Current.CancellationToken);
|
|
Assert.False(File.Exists(target));
|
|
|
|
var done = upload.Commit(3);
|
|
Assert.Equal(DeviceFileStatuses.Ok, done.Status);
|
|
Assert.Equal("039058c6f2c0cb492c533b0a4d14ef77cc0f78abccced5287d84a1a2011cfb81", done.Sha256);
|
|
}
|
|
|
|
Assert.Equal(new byte[] { 1, 2, 3 }, File.ReadAllBytes(target));
|
|
Assert.Single(Directory.EnumerateFileSystemEntries(At("new", "folder")));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ABrokenUploadLeavesNothingBehind()
|
|
{
|
|
var existing = Touch("keep.txt", "original");
|
|
var (_, upload) = DeviceFileService.PrepareWrite(Write(existing, overwrite: true));
|
|
|
|
await upload!.WriteAsync(new byte[] { 9 }, TestContext.Current.CancellationToken);
|
|
upload.Dispose();
|
|
|
|
Assert.Equal("original", File.ReadAllText(existing));
|
|
Assert.Equal([existing], Directory.EnumerateFileSystemEntries(root));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AShortUploadIsNotPutInPlace()
|
|
{
|
|
var target = At("short.bin");
|
|
var (_, upload) = DeviceFileService.PrepareWrite(Write(target, length: 10));
|
|
using (upload)
|
|
{
|
|
await upload!.WriteAsync(new byte[] { 1 }, TestContext.Current.CancellationToken);
|
|
Assert.Equal(DeviceFileStatuses.Failed, upload.Commit(1).Status);
|
|
}
|
|
|
|
Assert.False(File.Exists(target));
|
|
}
|
|
|
|
[Fact]
|
|
public void AnUploadSweepsWhatADeadAgentLeft_NotWhatIsInUse()
|
|
{
|
|
var stale = Touch(".executer-aaaaaaaaaaaa.tmp");
|
|
File.SetLastWriteTimeUtc(stale, DateTime.UtcNow.AddDays(-2));
|
|
var recent = Touch(".executer-bbbbbbbbbbbb.tmp");
|
|
var other = Touch(".mine.tmp");
|
|
File.SetLastWriteTimeUtc(other, DateTime.UtcNow.AddDays(-2));
|
|
|
|
var (_, upload) = DeviceFileService.PrepareWrite(Write(At("new.bin")));
|
|
upload!.Dispose();
|
|
|
|
Assert.False(File.Exists(stale));
|
|
Assert.True(File.Exists(recent));
|
|
Assert.True(File.Exists(other));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AnUploadReplacesOnlyWhenAsked_AndThenKeepsWhatTheFileWas()
|
|
{
|
|
var existing = Touch("a.txt", "old");
|
|
var (refused, none) = DeviceFileService.PrepareWrite(Write(existing));
|
|
Assert.Equal(DeviceFileStatuses.Conflict, refused.Status);
|
|
Assert.Null(none);
|
|
|
|
var created = new DateTime(2020, 1, 2, 3, 4, 5, DateTimeKind.Utc);
|
|
File.SetCreationTimeUtc(existing, created);
|
|
|
|
var (_, upload) = DeviceFileService.PrepareWrite(Write(existing, overwrite: true));
|
|
using (upload)
|
|
{
|
|
await upload!.WriteAsync("new"u8.ToArray(), TestContext.Current.CancellationToken);
|
|
Assert.Equal(DeviceFileStatuses.Ok, upload.Commit(3).Status);
|
|
}
|
|
|
|
Assert.Equal("new", File.ReadAllText(existing));
|
|
if (OperatingSystem.IsWindows())
|
|
{
|
|
Assert.Equal(created, File.GetCreationTimeUtc(existing));
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AnUploadLosesToAFileThatAppearedMeanwhile()
|
|
{
|
|
var target = At("race.txt");
|
|
var (_, upload) = DeviceFileService.PrepareWrite(Write(target));
|
|
using (upload)
|
|
{
|
|
await upload!.WriteAsync("mine"u8.ToArray(), TestContext.Current.CancellationToken);
|
|
File.WriteAllText(target, "theirs");
|
|
Assert.Equal(DeviceFileStatuses.Conflict, upload.Commit(4).Status);
|
|
}
|
|
|
|
Assert.Equal("theirs", File.ReadAllText(target));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(new byte[] { 0x68, 0x69, 0x0D, 0x0A }, DeviceTextEncodings.Utf8, DeviceTextLineEndings.Crlf)]
|
|
[InlineData(new byte[] { 0x68, 0x69, 0x0A }, DeviceTextEncodings.Utf8, DeviceTextLineEndings.Lf)]
|
|
[InlineData(new byte[] { 0xEF, 0xBB, 0xBF, 0x68, 0x69, 0x0A }, DeviceTextEncodings.Utf8Bom, DeviceTextLineEndings.Lf)]
|
|
[InlineData(new byte[] { 0xFF, 0xFE, 0x68, 0x00, 0x0D, 0x00, 0x0A, 0x00 }, DeviceTextEncodings.Utf16Le, DeviceTextLineEndings.Crlf)]
|
|
[InlineData(new byte[] { 0xFE, 0xFF, 0x00, 0x68, 0x00, 0x0A }, DeviceTextEncodings.Utf16Be, DeviceTextLineEndings.Lf)]
|
|
public void TextRoundTripsByteForByte(byte[] original, string encoding, string lineEndings)
|
|
{
|
|
var decoded = DeviceTextCodec.Decode(original);
|
|
|
|
Assert.NotNull(decoded);
|
|
Assert.Equal(encoding, decoded.Encoding);
|
|
Assert.Equal(lineEndings, decoded.LineEndings);
|
|
Assert.Equal(original, DeviceTextCodec.Encode(decoded.Text, decoded.Encoding, decoded.LineEndings));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(new byte[] { 0x4D, 0x5A, 0x00, 0x03 })] // A NUL.
|
|
[InlineData(new byte[] { 0xC3, 0x28 })] // Invalid UTF-8, as a legacy code page is.
|
|
[InlineData(new byte[] { 0xFF, 0xFE, 0x00, 0x00, 0x68, 0x00, 0x00, 0x00 })] // UTF-32.
|
|
[InlineData(new byte[] { 0x01, 0x02, 0x03, 0x04 })] // Control characters.
|
|
public void BinaryIsNotText(byte[] bytes)
|
|
=> Assert.Null(DeviceTextCodec.Decode(bytes));
|
|
|
|
[Fact]
|
|
public void MixedLineEndingsOpenAsTheMoreCommon_AndASaveMakesThemOneWay()
|
|
{
|
|
var decoded = DeviceTextCodec.Decode("a\r\nb\r\nc\n"u8);
|
|
|
|
Assert.True(decoded!.MixedLineEndings);
|
|
Assert.Equal(DeviceTextLineEndings.Crlf, decoded.LineEndings);
|
|
Assert.Equal("a\r\nb\r\nc\r\n"u8.ToArray(), DeviceTextCodec.Encode(decoded.Text, decoded.Encoding, decoded.LineEndings));
|
|
}
|
|
|
|
private static DeviceFileRequest SaveAs(string path, string? expected, bool createOnly = false, string encoding = DeviceTextEncodings.Utf8)
|
|
=> new(DeviceFileOperations.WriteText, Path: path, Encoding: encoding, LineEndings: DeviceTextLineEndings.Lf, ExpectedSha256: expected, CreateOnly: createOnly);
|
|
|
|
[Fact]
|
|
public void ASaveKeepsTheVersionPromise()
|
|
{
|
|
var path = Touch("a.conf", "one\n");
|
|
var (opened, _) = DeviceFileService.ReadText(new DeviceFileRequest(DeviceFileOperations.ReadText, Path: path));
|
|
Assert.Equal(DeviceFileStatuses.Ok, opened.Status);
|
|
|
|
// Unchanged: saved.
|
|
var saved = DeviceFileService.WriteText(SaveAs(path, opened.Version!.Sha256), "two\n"u8.ToArray());
|
|
Assert.Equal(DeviceFileStatuses.Ok, saved.Status);
|
|
Assert.Equal("two\n", File.ReadAllText(path));
|
|
Assert.Single(Directory.EnumerateFileSystemEntries(root));
|
|
|
|
// Stale: refused, with what is there now, so Overwrite can name it.
|
|
var stale = DeviceFileService.WriteText(SaveAs(path, opened.Version.Sha256), "three\n"u8.ToArray());
|
|
Assert.Equal(DeviceFileStatuses.Conflict, stale.Status);
|
|
Assert.Equal(saved.Version!.Sha256, stale.Version!.Sha256);
|
|
Assert.Equal("two\n", File.ReadAllText(path));
|
|
|
|
Assert.Equal(DeviceFileStatuses.Ok, DeviceFileService.WriteText(SaveAs(path, stale.Version.Sha256), "three\n"u8.ToArray()).Status);
|
|
Assert.Equal("three\n", File.ReadAllText(path));
|
|
}
|
|
|
|
[Fact]
|
|
public void ANewFileIsNeverOverAnother_AndARemovedOneIsAConflict()
|
|
{
|
|
var path = At("new.txt");
|
|
Assert.Equal(DeviceFileStatuses.Ok, DeviceFileService.WriteText(SaveAs(path, null, createOnly: true), []).Status);
|
|
Assert.True(File.Exists(path));
|
|
Assert.Equal(DeviceFileStatuses.Conflict, DeviceFileService.WriteText(SaveAs(path, null, createOnly: true), []).Status);
|
|
|
|
File.Delete(path);
|
|
var gone = DeviceFileService.WriteText(SaveAs(path, new string('0', 64)), []);
|
|
Assert.Equal(DeviceFileStatuses.Conflict, gone.Status);
|
|
Assert.Null(gone.Version);
|
|
}
|
|
|
|
[Fact]
|
|
public void AReadOnlyFileOpensButIsNotSaved()
|
|
{
|
|
var path = Touch("ro.conf", "x");
|
|
File.SetAttributes(path, FileAttributes.ReadOnly);
|
|
|
|
var (opened, _) = DeviceFileService.ReadText(new DeviceFileRequest(DeviceFileOperations.ReadText, Path: path));
|
|
Assert.True(opened.ReadOnly);
|
|
|
|
var saved = DeviceFileService.WriteText(SaveAs(path, opened.Version!.Sha256), "y"u8.ToArray());
|
|
Assert.Equal(DeviceFileStatuses.Denied, saved.Status);
|
|
}
|
|
|
|
[Fact]
|
|
public void WhatIsTooLargeOrNotTextIsAnAnswerNotAnError()
|
|
{
|
|
var binary = At("bin.dat");
|
|
File.WriteAllBytes(binary, [0, 1, 2]);
|
|
Assert.Equal(DeviceFileStatuses.NotText, DeviceFileService.ReadText(new DeviceFileRequest(DeviceFileOperations.ReadText, Path: binary)).Reply.Status);
|
|
|
|
var large = At("large.txt");
|
|
using (var file = File.Create(large))
|
|
{
|
|
file.SetLength(DeviceFileDefaults.TextMaxBytes + 1);
|
|
}
|
|
|
|
Assert.Equal(DeviceFileStatuses.TooLarge, DeviceFileService.ReadText(new DeviceFileRequest(DeviceFileOperations.ReadText, Path: large)).Reply.Status);
|
|
}
|
|
|
|
[Fact]
|
|
public void ASavedFileWithBadEncodingsIsRefused()
|
|
{
|
|
var reply = DeviceFileService.WriteText(SaveAs(At("a.txt"), null, encoding: "latin1"), Encoding.UTF8.GetBytes("x"));
|
|
Assert.Equal(DeviceFileStatuses.Invalid, reply.Status);
|
|
}
|
|
}
|