Tuesday, March 29, 2011

Set up Wake on LAN under Ubuntu


Perform the following on the PC you wish to wake:
  1. turn on Wake on Lan, or WOL, in the PC's BIOS.  How to do this will vary widely depending on your PC so you will need to research this one yourself

  2. Get the device id of the Ethernet adapter on which you wish to enable Wake on LAN.

    Open a Terminal' and type ifconfig.  The device id will appear next to a block of text that mentions Ethernet. in the response below eth0 is the Ethernet adapter

    eth0      Link encap:Ethernet  HWaddr 01:23:45:67:89:ab
              inet addr:192.168.1.2  Bcast:192.168.1.255  Mask:255.255.255.0
              inet6 addr: fe80::215:f2ff:fe6f:3487/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:71495 errors:0 dropped:0 overruns:0 frame:0
              TX packets:76190 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000
              RX bytes:23164212 (22.0 MiB)  TX bytes:7625016 (7.2 MiB)
              Interrupt:217 Base address:0xd400
    
    lo        Link encap:Local Loopback
              inet addr:127.0.0.1  Mask:255.0.0.0
              inet6 addr: ::1/128 Scope:Host
              UP LOOPBACK RUNNING  MTU:16436  Metric:1
              RX packets:1290 errors:0 dropped:0 overruns:0 frame:0
              TX packets:1290 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0
              RX bytes:161182 (157.4 KiB)  TX bytes:161182 (157.4 KiB)

  3. Create a file under /etc/init.d/. This file is going to hook into your PC's boot processes and enable WOL each time your PC is booted. You may give this file whatever name you like.

  4. Inside the file add the code below.  Insert the ID of your Ethernet adapter in place of {adapter Id}.

    #!/bin/bash
    ethtool -s {adapter id} wol g
    exit

  5. Set appropriate permissions on your script file with the command below.  Change {script file} to the name of the script file you created above.

     sudo chmod a+x {script file}

  6. Ensure the script runs at start up with the following command

    update-rc.d -f {script file} defaults

Perform the following on the Wakee

To wake the pc you enabled WOL on above, follow the steps below:

  1. Get the mac address of the PC you wish to wake with the following command.  (obviously you will need to do this on the target PC):

    ifconfig | grep HW
  2. Wake the target PC with the following command:

    wakeonlan {mac address of the pc you are waking}

Thursday, March 24, 2011

Attach a test SSL certificate to an IIS 5.x web site

Ok this took me a while to figure out so I thought it was worth documenting.  I have a web service that I wanted to test on my local pc.  The web service is configured to accept SSL connections only so I needed a way to attach a certificate to my local IIS for the purposes of testing.  This turned out not to be so easy.  Steps to achieve this area detailed below:
  1. Open a command prompt. (Under Win XP go to Start > Run and type cmd)
  2. Create a test certificate and place it in your Personal certificate store by typing the following on the command line:

     cd C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin. 
    makecert -r -pe -n "CN=www.myserver.com" -b 01/01/2000 -e 01/01/2036 -eku 1.3.6.1.5.5.7.3.1 -ss my -sr localMachine -sky exchange -sp "Microsoft RSA SChannel Cryptographic Provider" -sy 12

    Note: C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin is the location of makecert on my pc.  do a search on your C drive if you cannot find the program under this location. Note also that you will need to change www.myserver.com to the domain for your server.  if you are installing the certificate on your local PC you can just use localhost.
  3. Add the certificate you just created to your Trusted Root Certification Authorities store.
    1. Access to your certificate stores
      1. go to Start > Run and type mmc
      2. File > Add / Remove snapin
      3. click on Add
      4. click on Certificates 
      5. click on Add
      6. click on Computer account 
      7. click on Next
      8. click on local computer: (the ...
      9. click on Finnish
      10. click on Close
      11. Click on Ok
    2. Open Personal > Certificates
    3. The certificate you created above should appear here. Right click on it.
    4. Click on Copy 
    5. Right click on  Trusted Root Certification Authorities > Certificates
    6. click on Paste
      you should now have a certificate which can be used in your local IIS and which will be trusted by local apps connecting to web apps and services that use the certificate.
  4. Attach the certificate you just created to IIS
    1. open IIS.
    2. right click on the web site node in IIS (obviously there will only be one if you are using IIS 5.x) and then click on Properties
    3. Click on Directory Security
    4. Click on Server Certificate.
    5. click on Next
    6. click on Assign an existing certificate
    7. click on the certificate you created above
    8. click on Next
    9. click on Finnish
Done

Thursday, January 13, 2011

Unit testing fun under Visual Studio 2010 Ulitmate

Recently I encountered a few interesting problem while unit testing an assembly in VS 2010. The assembly I was testing referenced web based code, HttpContext.Current, and was designed for use within a web application.

Unit tests don't run within a web context so testing assemblies like this is obviously problematic. One solution to this problem is to add the following to the head of your test methods:
[TestMethod()]
[HostType("ASP.NET")] 
[UrlToTest("http://localhost/SomeWebApp/Default.aspx")]
public void SomeTestMethod()
{
    ...
These directives instruct Visual Studio to run your unit test inside an  ASP.NET Host.  When the test is run the web app referenced by UrlToTest is fired up and the tests are run within this web site just after the page load event of the referenced web page.

This solution works fine in VS 2008 but when i try this in VS 2010 I get the following error:

The test adapter 'WebHostAdapter' threw an exception while running test 'testWebContext'. The web site could not be configured correctly; getting ASP.NET process information failed. Requesting 'http://localhost/someApp/VSEnterpriseHelper.axd' returned an error: The remote server returned an error: (500) Internal Server Error.
The remote server returned an error: (500) Internal Server Error.


I found the following "solutions" online:
  1. Set the test project and the web project to use version 4.0 of the .Net framework

  2. Add the following to your the Web.config file of your web site:
      <location path="VSEnterpriseHelper.axd">
        <system.web>
          <authorization>
            <allow users="*"/>
          </authorization>
        </system.web>
      </location>
  3. Add the following to your the Web.config file of your web site:
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"/>
    
  4. Give the Network Service group permissions to everything under the web root of your web site / application. I gave full permissions to the Internet guest account (pcname\IUSR_pcname), the IIS process account (pcname\IWAM_pcname), the Network Service account.
None of these things worked for me or at least not under IIS. The only thing that worked in the end, after one very frustrating day, was to set my web site to .Net 4.0 and to use the development server that comes bundled with Visual Studio as the host (the one that is used for "File System" web sites).

To use the Development Server you need to add one more directive to your test method:
[AspNetDevelopmentServerHost("D:\\SomeWebApp\""/SomeWebApp")]
Your Method should now look something like this:
[TestMethod()]
[HostType("ASP.NET")]
[AspNetDevelopmentServerHost("D:\\SomeWebApp\", "/SomeWebApp")]
[UrlToTest("http://localhost:2100/Default.aspx")]
public void SomeTestMethod()
{
     ...
Using the Development server  is not an ideal solution for me, nor is only being able to unit test version 4.0 apps, but this seems to be the only way it will work.

I hope this saves someone else having to bang their head against the wall for a day.

cheers