Short trip to London for a C/C++ Security course

Last evening of my first and unfortunately too short trip to London. The purpose of my trip was to attend a “Secure C/C++ Development” training kindly provided by Skype and MWR InfoSecurity. The course itself was really intense, professionally delivered and well structured, so I would highly recommend all my friend to attend, even if you don’t have much working experience or knowledge in C/C++ development (like me). I learned so many new security related topics including Secure Cookies, Canaries, DEP, ASLR, Buffer Overflow, Heap Overflow, exploitation principles and different mitigation techniques. Besides learning all those wonderful things I did a little bit of sightseeing in the evenings. Didn’t have enough time to see much, but London made a great first impression on me, gorgeous architecture synergy of modern and old elements, nice food, even though the city felt a little bit overcrowded with a slight feeling of social diversity, but first impressions can be misleading. From the places I had a chance to see the area around Big Ben is the most eye-catching and spectacular. Hopefully will be coming back shortly.

Tower Bridge Birmingham palace Tower BridgeBig Ben

Stretching ListView items to full width in WinRT XAML apps

If you have a ListView filled with child controls which you would like to stretch to full width setting HorizontalContentAlignment to Stretch for a ListView, unfortunately, will not do the trick here. In this situation you have to modify the ItemContainer style (as shown in code sample bellow). This should work for both Windows 8.1 and Windows Phone 8.1 apps.

<ListView>
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </ListView.ItemContainerStyle>
    <ListView.ItemTemplate>
        <DataTemplate>
          ...
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Shared Projects vs Portable Class Libraries for Windows 8.1 and Windows Phone 8.1

When you build a new modern application you might also think of sharing some of the sources between Windows 8.1 and Windows Phone 8.1 apps. To accomplish that Visual Studio 2013 Update 2 offers two main options: shared projects or portable class libraries. In this blog post I will try to compare those two options and gather all the information on the major differences between “Shared Projects” (for Universal apps) and Portable Class Libraries for Windows 8.1 and Windows Phone 8.1 projects.

Continue reading Shared Projects vs Portable Class Libraries for Windows 8.1 and Windows Phone 8.1

Switching my focus to Windows 8.1 and Windows Phone 8.1 development

After some big announcements at Build Conference 2014, I guess, it is time to switch my focus from building standard Silverlight Windows Phone apps to a Jupiter framework (XAML/C#) apps for Windows 8.1 and Windows Phone 8.1. I believe this switch will also make me post articles more frequently. Right now I am in a middle of building a new Universal application which will run on both platforms and it really feels like the future has already arrived.

Post-event summary of TechDay 2013 in Estonia, Latvia, Lithuania

More than one week has passed since one of the largest local Microsoft events took place in three Baltic countries: Estonia, Latvia and Lithuania. This year there were again 5 different tracks to cover essentially every IT person’s needs from Security to Kinect SDK. The event happened in the same three locations from last year and there were again around 500 participants in each of three countries, which is a relatively huge audience if we take the population of countries into consideration. In this post I will try to express my opinion about the event and share my feedback and observations.
Continue reading Post-event summary of TechDay 2013 in Estonia, Latvia, Lithuania

Unit testing thread-safety in C#

This time I am going to show a simple example of how to determine if method is thread safe using unit tests. With the approach described bellow you can verify thread safety of a particular code block in your console app, windows store app, windows phone app or almost any other application or project. Also this approach might beneficial if your team practices Test Driven Development(TDD).

For my basic example, I have created a new Portable Library project(named “Services”) and a standard MS Unit Tests Project. Inside Services project I have created a simple PopularNameService class, which represents a service for storing popular names. This class has two public methods: “AddName” for adding a new name to a list and “GetNamesCount” for returning the count of names currently stored.

public class PopularNameService
{
    private List<string> popularNames = new List<string>();

    public void AddName(string newPopularName)
    {
        popularNames.Add(newPopularName);
    }

    public int GetNamesCount()
    {
        return popularNames.Count;
    }
}

It is absolutely possible that AddName method will be called from different threads within our application at the same time. So assuming the functionality of AddName and GetNamesCount methods is already tested and verified, next step would be to determine how thread safe those methods actually are. For this very purpose I am going to create a unit test AddNameThreadSafetyTest, which will internally run two separate threads and each of those threads will try to call AddName method exactly 1000 times. After two threads are finished running, I am going to assert if PopularNameService now contains a list of 2000 names. Keep in mind that we need to call Join on each of the threads to wait till threads are done executing before asserting test results.

[TestClass]
public class PopularNameServiceTests
{
    private PopularNameService service;

    [TestInitialize]
    public void Setup()
    {
        service = new PopularNameService();
    }

    [TestMethod]
    public void AddNameThreadSafetyTest()
    {
        var t1 = new Thread(AddManyProducts);
        var t2 = new Thread(AddManyProducts);

        t1.Start();
        t2.Start();

        t1.Join();
        t2.Join();

        Assert.AreEqual(2000, service.GetNamesCount());
    }

    private void AddManyProducts()
    {
        for (int x = 0; x < 1000; x++)
        {
            var newName = string.Format("name {0}", x);
            service.AddName(newName);
        }
    }
}

After executing Unit Tests we are going to get a failed test with the following error message: Assert.AreEqual failed. Expected:. Actual:(Surprise! :)). (Note that second number may vary depending on hardware specs). A method or a resource is not locked properly while two different threads are trying to access it. This leads to an obvious conclusion that AddName method isn’t thread safe.

To fix this issue we might want to introduce a locking mechanism for a AddName method. After updating the code for PopularNameService it will probably looks as the one bellow:

public class PopularNameService
{
    private readonly Object popularNamesLock = new Object();

    private List<string> popularNames = new List<string>();

    public void AddName(string newPopularName)
    {
        lock (popularNamesLock)
        {
            popularNames.Add(newPopularName);
        }
    }

    public int GetNamesCount()
    {
        return popularNames.Count;
    }
}

There is also an option to use thead-safe collections(which are part of .NET since 4.0), but not everything is available for Portrable Class Library projects. Now after fixing the code for PopularNameService, the unit test will pass successfully and the AddName method can be considered thread safe.

%d bloggers like this: