Exemple #1
0
        public void JwtSignatureDecode(string base64, int length, byte first, byte fiftyOneth, byte last)
        {
            var decode = Base64Url.Decode(base64);
            var bytes  = new UTF8Encoding(false).GetBytes(decode);

            bytes.Length.Should().Be(length);
            bytes.First().Should().Be(first);
            bytes.Skip(51).First().Should().Be(fiftyOneth);
            bytes.Last().Should().Be(last);
        }
        public async Task VerifyChecksumAsync_Data_Is_Truncated_If_Verification_Fails_Using_Single_Chunk()
        {
            // Checksum is for "hello world"
            const string incorrectChecksum = "Kq5sNclPz7QV2+lfQIuc6R7oRu0=";
            const string message           = "Hello World 12345!!@@åäö";

            var buffer = new UTF8Encoding(false).GetBytes(message);

            var bytesWritten = 0L;

            var fileId = await _fixture.Store.CreateFileAsync(buffer.Length, null, CancellationToken.None);

            // Emulate several requests
            do
            {
                // Create a new store and cancellation token source on each request as one would do in a real scenario.
                _fixture.CreateNewStore();
                var cts = new CancellationTokenSource();

                var requestStream = new RequestStreamFake(
                    (stream, bufferToFill, offset, count, ct) =>
                {
                    // Emulate that the request completed
                    if (bytesWritten == buffer.Length)
                    {
                        return(Task.FromResult(0));
                    }

                    // Emulate client disconnect after two bytes read.
                    if (stream.Position == 2)
                    {
                        cts.Cancel();
                        cts.Token.ThrowIfCancellationRequested();
                    }

                    bytesWritten += 2;
                    return(stream.ReadBackingStreamAsync(bufferToFill, offset, 2, ct));
                },
                    buffer.Skip((int)bytesWritten).ToArray());

                await ClientDisconnectGuard.ExecuteAsync(
                    () => _fixture.Store.AppendDataAsync(fileId, requestStream, cts.Token),
                    cts.Token);
            } while (bytesWritten < buffer.Length);

            var checksumOk = await _fixture.Store
                             .VerifyChecksumAsync(fileId, "sha1", Convert.FromBase64String(incorrectChecksum), CancellationToken.None);

            // File should not have been saved.
            checksumOk.ShouldBeFalse();
            var filePath = Path.Combine(_fixture.Path, fileId);

            new FileInfo(filePath).Length.ShouldBe(0);
        }
Exemple #3
0
        public async Task VerifyChecksumAsync_Data_Is_Truncated_If_Verification_Fails()
        {
            // Checksum is for "hello world"
            const string incorrectChecksum = "Kq5sNclPz7QV2+lfQIuc6R7oRu0=";
            const string message           = "Hello World 12345!!@@åäö";

            var buffer = new UTF8Encoding(false).GetBytes(message);

            // Test complete upload
            var fileId = await _fixture.Store.CreateFileAsync(buffer.Length, null, CancellationToken.None);

            using (var stream = new MemoryStream(buffer))
            {
                await _fixture.Store.AppendDataAsync(fileId, stream, CancellationToken.None);
            }

            var checksumOk = await _fixture.Store
                             .VerifyChecksumAsync(fileId, "sha1", Convert.FromBase64String(incorrectChecksum), CancellationToken.None);

            // File should not have been saved.
            checksumOk.ShouldBeFalse();
            var filePath = Path.Combine(_fixture.Path, fileId);

            new FileInfo(filePath).Length.ShouldBe(0);

            // Test chunked upload
            fileId = await _fixture.Store.CreateFileAsync(buffer.Length, null, CancellationToken.None);

            using (var stream = new MemoryStream(buffer.Take(10).ToArray()))
            {
                // Write first 10 bytes
                await _fixture.Store.AppendDataAsync(fileId, stream, CancellationToken.None);
            }

            using (var stream = new MemoryStream(buffer.Skip(10).ToArray()))
            {
                // Skip first 10 bytes and write the rest
                await _fixture.Store.AppendDataAsync(fileId, stream, CancellationToken.None);
            }

            checksumOk = await _fixture.Store.VerifyChecksumAsync(fileId, "sha1", Convert.FromBase64String(incorrectChecksum),
                                                                  CancellationToken.None);

            // Only first chunk should have been saved.
            checksumOk.ShouldBeFalse();
            filePath = Path.Combine(_fixture.Path, fileId);
            new FileInfo(filePath).Length.ShouldBe(10);
        }