public async Task GetAsync_InvokesJSAndUnprotects_ValidData(string customPurpose)
        {
            // Arrange
            var jsRuntime = new TestJSRuntime();
            var dataProtectionProvider  = new TestDataProtectionProvider();
            var protectedBrowserStorage = new TestProtectedBrowserStorage("test store", jsRuntime, dataProtectionProvider);
            var data = new TestModel {
                StringProperty = "Hello", IntProperty = 123
            };
            var keyName         = "test key";
            var expectedPurpose = customPurpose == null
                ? $"{typeof(TestProtectedBrowserStorage).FullName}:test store:{keyName}"
                : customPurpose;
            var storedJson = "{\"StringProperty\":\"Hello\",\"IntProperty\":123}";

            jsRuntime.NextInvocationResult = Task.FromResult(
                TestDataProtectionProvider.Protect(expectedPurpose, storedJson));

            // Act
            var result = customPurpose == null
                ? await protectedBrowserStorage.GetAsync <TestModel>(keyName)
                : await protectedBrowserStorage.GetAsync <TestModel>(customPurpose, keyName);

            // Assert
            Assert.Equal("Hello", result.StringProperty);
            Assert.Equal(123, result.IntProperty);

            var invocation = jsRuntime.Invocations.Single();

            Assert.Equal("protectedBrowserStorage.get", invocation.Identifier);
            Assert.Collection(invocation.Args,
                              arg => Assert.Equal("test store", arg),
                              arg => Assert.Equal(keyName, arg));
        }
Пример #2
0
    public async Task GetAsync_InvokesJSAndUnprotects_ValidData_CustomPurpose()
    {
        // Arrange
        var jsRuntime = new TestJSRuntime();
        var dataProtectionProvider  = new TestDataProtectionProvider();
        var protectedBrowserStorage = new TestProtectedBrowserStorage("testStore", jsRuntime, dataProtectionProvider);
        var data = new TestModel {
            StringProperty = "Hello", IntProperty = 123
        };
        var keyName       = "testKey";
        var customPurpose = "my custom purpose";
        var storedJson    = "{\"StringProperty\":\"Hello\",\"IntProperty\":123}";

        jsRuntime.NextInvocationResult = new ValueTask <string>(
            TestDataProtectionProvider.Protect(customPurpose, storedJson));

        // Act
        var result = await protectedBrowserStorage.GetAsync <TestModel>(customPurpose, keyName);

        // Assert
        Assert.True(result.Success);
        Assert.Equal("Hello", result.Value.StringProperty);
        Assert.Equal(123, result.Value.IntProperty);

        var invocation = jsRuntime.Invocations.Single();

        Assert.Equal("testStore.getItem", invocation.Identifier);
        Assert.Collection(invocation.Args, arg => Assert.Equal(keyName, arg));
    }
Пример #3
0
    public async Task GetAsync_InvokesJSAndUnprotects_InvalidProtection_Plaintext()
    {
        // Arrange
        var jsRuntime = new TestJSRuntime();
        var dataProtectionProvider  = new TestDataProtectionProvider();
        var protectedBrowserStorage = new TestProtectedBrowserStorage("testStore", jsRuntime, dataProtectionProvider);
        var storedString            = "This string is not even protected";

        jsRuntime.NextInvocationResult = new ValueTask <string>(storedString);

        // Act/Assert
        var ex = await Assert.ThrowsAsync <CryptographicException>(
            async() => await protectedBrowserStorage.GetAsync <TestModel>("testKey"));
    }
        public async Task GetAsync_InvokesJSAndUnprotects_NoValue()
        {
            // Arrange
            var jsRuntime = new TestJSRuntime();
            var dataProtectionProvider  = new TestDataProtectionProvider();
            var protectedBrowserStorage = new TestProtectedBrowserStorage("test store", jsRuntime, dataProtectionProvider);

            jsRuntime.NextInvocationResult = Task.FromResult((string)null);

            // Act
            var result = await protectedBrowserStorage.GetAsync <TestModel>("test key");

            // Assert
            Assert.Null(result);
        }
    public async Task GetAsync_InvokesJSAndUnprotects_InvalidJson()
    {
        // Arrange
        var jsRuntime = new TestJSRuntime();
        var dataProtectionProvider = new TestDataProtectionProvider();
        var protectedBrowserStorage = new TestProtectedBrowserStorage("testStore", jsRuntime, dataProtectionProvider);
        var expectedPurpose = $"{typeof(TestProtectedBrowserStorage).FullName}:testStore:testKey";
        var storedJson = "you can't parse this";
        jsRuntime.NextInvocationResult = new ValueTask<string>(
            TestDataProtectionProvider.Protect(expectedPurpose, storedJson));

        // Act/Assert
        var ex = await Assert.ThrowsAsync<JsonException>(
            async () => await protectedBrowserStorage.GetAsync<TestModel>("testKey"));
    }
    public async Task GetAsync_InvokesJSAndUnprotects_NoValue()
    {
        // Arrange
        var jsRuntime = new TestJSRuntime();
        var dataProtectionProvider = new TestDataProtectionProvider();
        var protectedBrowserStorage = new TestProtectedBrowserStorage("testStore", jsRuntime, dataProtectionProvider);
        jsRuntime.NextInvocationResult = new ValueTask<string>((string)null);

        // Act
        var result = await protectedBrowserStorage.GetAsync<TestModel>("testKey");

        // Assert
        Assert.False(result.Success);
        Assert.Null(result.Value);
    }
Пример #7
0
    public async Task GetAsync_InvokesJSAndUnprotects_InvalidProtection_Base64Encoded()
    {
        // Arrange
        var jsRuntime = new TestJSRuntime();
        var dataProtectionProvider  = new TestDataProtectionProvider();
        var protectedBrowserStorage = new TestProtectedBrowserStorage("testStore", jsRuntime, dataProtectionProvider);

        // DataProtection deals with strings by base64-encoding the results.
        // Depending on whether the stored data is base64-encoded or not,
        // it will trigger a different failure point in data protection.
        var storedString = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes("This string is not even protected"));

        jsRuntime.NextInvocationResult = new ValueTask <string>(storedString);

        // Act/Assert
        var ex = await Assert.ThrowsAsync <CryptographicException>(
            async() => await protectedBrowserStorage.GetAsync <TestModel>("testKey"));
    }
    public async Task GetValueOrDefaultAsync_InvokesJSAndUnprotects_WrongPurpose()
    {
        // Arrange
        var jsRuntime = new TestJSRuntime();
        var dataProtectionProvider = new TestDataProtectionProvider();
        var protectedBrowserStorage = new TestProtectedBrowserStorage("testStore", jsRuntime, dataProtectionProvider);
        var expectedPurpose = $"{typeof(TestProtectedBrowserStorage).FullName}:testStore:testKey";
        var storedJson = "we won't even try to parse this";
        jsRuntime.NextInvocationResult = new ValueTask<string>(
            TestDataProtectionProvider.Protect(expectedPurpose, storedJson));

        // Act/Assert
        var ex = await Assert.ThrowsAsync<CryptographicException>(
            async () => await protectedBrowserStorage.GetAsync<TestModel>("different key"));
        var innerException = ex.InnerException;
        Assert.IsType<ArgumentException>(innerException);
        Assert.Contains("The value is not protected with the expected purpose", innerException.Message);
    }