0

My boss wants me to unit test this License controller for our ASP.NET application.

The problem im having is that normally you go to the application create a license that license is tied to the current instance of a keygenerator and keypair (from portable=license library ) i have a static class called LicenseService that creates a keygenerator and keypair when called. So you generate a license and then go to the verification page, which is on the same instance of the keygenerator and keypair. So the verify will go through, if the license was created on another instance it won't verify (different public/private key) what would be a possible work around for this?

because Im unit testing a valid license and invalid license so i need to have created a license beforehand but each license is tied to a unique instance, so passing a license created with a different key pair to another instance (with its own keypair) will be invalid (even if the license itself is invalid).

here are some ideas i have

  1. hard code the keys (im testing locally so it shouldnt be too much of an issue) but i feel there is a security flaw with this

  2. somehow force the controller and all subsequent instances of the controller to use one specific instance of the generator while having multiple key pairs (this seems very hard and im unsure as to how to approach it)

this is the library btw

http://dev.nauck-it.de/projects/portable-licensing/wiki/GettingStarted

ynot269
  • 305
  • 2
  • 14
  • This seems like a duplicate of http://stackoverflow.com/questions/5503702/patterns-or-practices-for-unit-testing-methods-that-call-a-static-method – Fran Mar 17 '17 at 20:28

1 Answers1

0

Code would be nice, but without it.

If this license generator is your class then

  1.  don't make you LicenseService static.  
  2.  have this LicenseService inherit from an interface.  define the interface with whatever the public methods of the current
service are.
  3.  pass the interface to the constructor of your controller
  4.  use IoC to pass an instance of the class that implements the interface or create a new constructor that instantiates the class
for the constructor

If you can't modify the license generator class.

  1.  define the interface with whatever the public methods of the current
service are. 
  2.  create a wrapper class that passes through to the static methods
  3.  pass the interface to the constructor of your controller
  4.  use IoC to pass an instance of the class that implements the interface or create a new constructor that instantiates the class
for the constructor

Some thrown together code

public interface IGenerateLicenseKeys
{
    SomeType GenerateKeys();
}

public class LicenseKeyGenerator : IGenerateLicenseKeys
{
    public SomeType GenerateKeys()
    {
        //Generate your keys or  pass through to the other component with the static method. 
        throw new NotImplementedException();                         
    }
}

Once you have that, mock out the LicenseKeyGenerator and return whatever values you want from the mocked methods.

Fran
  • 6,440
  • 1
  • 23
  • 35