Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ In addition to being used to specify the order of operations in an expression, p

For more information, see [Casting and Type Conversions](../../../csharp/programming-guide/types/casting-and-type-conversions.md).

A cast expression could lead to ambiguous syntax. For example, the expression `(x)–y` could be either interpreted as a cast expression (a cast of –y to type x) or as an additive expression combined with a parenthesized expression, which computes the value x – y.

For more information about method invocation, see [Methods](../../../csharp/programming-guide/classes-and-structs/methods.md).

## C# Language Specification
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ms.date: 10/08/2017
# Test ASP.NET Core MVC Apps

> _"If you don't like unit testing your product, most likely your customers won't like to test it, either."_
> _- Anonymous-
> _- Anonymous-_

## Summary

Expand All @@ -32,7 +32,7 @@ Integration tests will often have more complex setup and teardown procedures tha

The LocalFileImageService implementation class implements the logic for fetching and returning the bytes of an image file from a particular folder given an id:

```cs
```csharp
public class LocalFileImageService : IImageService
{
private readonly IHostingEnvironment _env;
Expand All @@ -47,6 +47,13 @@ public class LocalFileImageService : IImageService
var contentRoot = _env.ContentRootPath + "//Pics";
var path = Path.Combine(contentRoot, id + ".png");
return File.ReadAllBytes(path);
}
catch (FileNotFoundException ex)
{
throw new CatalogImageMissingException(ex);
}
}
}
```

### Functional Tests
Expand Down Expand Up @@ -95,19 +102,19 @@ Figure 9-3 Add an xUnit Test Project in Visual Studio

You should name your tests in a consistent fashion, with names that indicate what each test does. One approach I've had great success with is to name test classes according to the class and method they are testing. This results in many small test classes, but it makes it extremely clear what each test is responsible for. With the test class name set up to identify the class and method to be tested, the test method name can be used to specify the behavior being tested. This should include the expected behavior and any inputs or assumptions that should yield this behavior. Some example test names:

- CatalogControllerGetImage.CallsImageServiceWithId
- CatalogControllerGetImage.CallsImageServiceWithId

- CatalogControllerGetImage.LogsWarningGivenImageMissingException
- CatalogControllerGetImage.LogsWarningGivenImageMissingException

- CatalogControllerGetImage.ReturnsFileResultWithBytesGivenSuccess
- CatalogControllerGetImage.ReturnsFileResultWithBytesGivenSuccess

- CatalogControllerGetImage.ReturnsNotFoundResultGivenImageMissingException
- CatalogControllerGetImage.ReturnsNotFoundResultGivenImageMissingException

A variation of this approach ends each test class name with "Should" and modifies the tense slightly:

- CatalogControllerGetImage**Should**.**Call**ImageServiceWithId
- CatalogControllerGetImage**Should**.**Call**ImageServiceWithId

- CatalogControllerGetImage**Should**.**Log**WarningGivenImageMissingException
- CatalogControllerGetImage**Should**.**Log**WarningGivenImageMissingException

Some teams find the second naming approach clearer, though slightly more verbose. In any case, try to use a naming convention that provides insight into test behavior, so that when one or more tests fail, it's obvious from their names what cases have failed. Avoid naming you tests vaguely, such as ControllerTests.Test1, as these offer no value when you see them in test results.

Expand All @@ -125,7 +132,7 @@ In a well-designed ASP.NET Core application, most of the complexity and business

Sometimes you'll need to refactor your code in order to unit test it. Frequently this involves identifying abstractions and using dependency injection to access the abstraction in the code you'd like to test, rather than coding directly against infrastructure. For example, consider this simple action method for displaying images:

```cs
```csharp
[HttpGet("[controller]/pic/{id}")]
public IActionResult GetImage(int id)
{
Expand All @@ -140,7 +147,7 @@ Unit testing this method is made difficult by its direct dependency on System.IO

If you can't unit test the file system behavior directly, and you can't test the route, what is there to test? Well, after refactoring to make unit testing possible, you may discover some test cases and missing behavior, such as error handling. What does the method do when a file isn't found? What should it do? In this example, the refactored method looks like this:

```cs
```csharp
[HttpGet("[controller]/pic/{id}")\]
public IActionResult GetImage(int id)
{
Expand All @@ -162,21 +169,11 @@ The \_logger and \_imageService are both injected as dependencies. Now you can t

## Integration Testing ASP.NET Core Apps

```cs
}
catch (FileNotFoundException ex)
{
throw new CatalogImageMissingException(ex);
}
}
}
```

This service uses the IHostingEnvironment, just as the CatalogController code did before it was refactored into a separate service. Since this was the only code in the controller that used IHostingEnvironment, that dependency was removed from CatalogController's constructor.

To test that this service works correctly, you need to create a known test image file and verify that the service returns it given a specific input. You should take care not to use mock objects on the behavior you actually want to test (in this case, reading from the file system). However, mock objects may still be useful to set up integration tests. In this case, you can mock IHostingEnvironment so that its ContentRootPath points to the folder you're going to use for your test image. The complete working integration test class is shown here:

```cs
```csharp
public class LocalFileImageServiceGetImageBytesById
{
private byte[] _testBytes = new byte[] { 0x01, 0x02, 0x03 };
Expand Down Expand Up @@ -218,7 +215,7 @@ public class LocalFileImageServiceGetImageBytesById

For ASP.NET Core applications, the TestServer class makes functional tests fairly easy to write. You configure a TestServer using a WebHostBuilder, just as you normally do for your application. This WebHostBuilder should be configured just like your application's real host, but you can modify any aspects of it that make testing easier. Most of the time, you'll reuse the same TestServer for many test cases, so you can encapsulate it in a reusable method (perhaps in a base class):

```cs
```csharp
public abstract class BaseWebTest
{
protected readonly HttpClient _client;
Expand All @@ -228,14 +225,14 @@ public abstract class BaseWebTest
{
_client = GetClient();
}

protected HttpClient GetClient()
{
var startupAssembly = typeof(Startup).GetTypeInfo().Assembly;
_contentRoot = GetProjectPath("src", startupAssembly);
var builder = new WebHostBuilder()
.UseContentRoot(_contentRoot)
.UseStartup<Startup>();
.UseStartup<Startup>();
var server = new TestServer(builder);
var client = server.CreateClient();
return client;
Expand All @@ -245,7 +242,7 @@ public abstract class BaseWebTest

The GetProjectPath method simply returns the physical path to the web project (download sample solution). The WebHostBuilder in this case simply specifies where the content root for the web application is, and references the same Startup class the real web application uses. To work with the TestServer, you use the standard System.Net.HttpClient type to make requests to it. TestServer exposes a helpful CreateClient method that provides a pre-configured client that is ready to make requests to the application running on the TestServer. You use this client (set to the protected \_client member on the base test above) when writing functional tests for your ASP.NET Core application:

```cs
```csharp
public class CatalogControllerGetImage : BaseWebTest
{
[Fact]
Expand Down
3 changes: 1 addition & 2 deletions docs/standard/security/vulnerabilities-cbc-mode.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ ms.author: "mairaw"
---
# Timing vulnerabilities with CBC-mode symmetric decryption using padding

Microsoft, based on currently known cryptographic research, believes that, except for very specific circumstances, it's no longer safe to
decrypt data encrypted with the Cipher-Block-Chaining (CBC) mode of symmetric encryption when verifiable padding has been applied without first ensuring the integrity of the ciphertext.
Microsoft believes that it's no longer safe to decrypt data encrypted with the Cipher-Block-Chaining (CBC) mode of symmetric encryption when verifiable padding has been applied without first ensuring the integrity of the ciphertext, except for very specific circumstances. This judgement is based on currently known cryptographic research.

## Introduction

Expand Down