Andrew Maddison

Flowerchild.

Quick Test - Cos I Migrated Server Again

I’ve just moved servers again (again) - because of a slight PEBKAC issue, and this is really just to check I’ve got the database permissions correct.

On an unrelated note, I’m getting pretty good at configuring IIS7!
comments

Yet Another Server Migration - IIS7 SQL Express and Subtext

Hokay - I’ve just managed to migrate this blog over to yet another new server, this time a shiny (if slightly expensive) windows 2008 vps hosted by the nice people at www.daily.co.uk I doubt anyone cared or noticed, but sorry for the 24hrs of downtime.

I had the standard day of fighting with server 2008 and IIS 7, which are both still a bit new to me. First configuring the server role to get IIS to work at all, then having to install Chrome to download SQL express because IE simply refused to work with the active-x control on Microsoft’s own download page and after that a simple (once I looked it up) firewall configuration to allow FTP through.

The final stolen hour was spent trying to figure out how to assign permissions to an application pool identity account (the new magic windows accounts / aliases created for each app pool in IIS7). It turns out the magic incantation is to use ”IIS APPPOOL” as the domain, for example  ”IIS APPPOOL\myapppoolname” and not “machinename\myapppoolname” as you might assume.

Having done all of that, installing subtext was an absolute breeze. I simply restored the backed up database from my old host, unpacked the latest subtext code, edited the connection string in web.config, and fired it up. The blog started up and instantly ran the version upgrade tool (as I was running an older version on my old host) and within a couple of minutes it was up and running again complete with my preferred skin (although without the css hacks to fix said skin). I even took the opportunity to move the blog from the /blog/ subfolder into the root of the website. I’ve had a check using fiddler, and the old urls are throwing 301 (permanent) redirects to the correct new urls, which is awesome.

The only thing still missing is the images from the posts, which are unfortunately backed up on a computer down in London which I can’t access until I head home at the weekend, but they will return.

comments

More Subtext Eco Theme Fixing

After my last post I noticed that lists (<ul>) were styled with no indentation and no list-style (no blobs!). The designers of the Eco theme had reset these styles in pretty much line one of their stylesheet, presumably to make their lives easier formatting lists in menus and sidebars etc. Unfortunately they forgot to put it back on the post body.

Easy enough to rectify, I simply popped the following into the custom css field of the subtext options/configure page in admin.

div.body ul
{
list-style: disc outside none;
padding-left:40px;
}
div.body ul ul
{
list-style: circle outside none;
}

EDIT: 22 Aug 2011
I just noticed that the same problem happens with ordered lists (the numbers were missing), so I’ve ammended the above css as follows:

div.body ul
{
    list-style: disc outside none;
    padding-left:40px;
}
div.body ol
{
    list-style: decimal outside none;
    padding-left:40px;
}
div.body ul ul, div.body ol ul
{
    list-style: circle outside none;
}
div.body ul ol, div.body ol ol
{
    list-style: lower-roman outside none;
}
comments

Getting the RSA Software Key Working on Android or iPhone

I’ve just had a bit of a battle to get the RSA system Thoughworks use working on my Android phone, this is a quick “what I did” post in case I ever have to do it again. One of my colleagues has just done the same thing on iPhone, and the experience is pretty much identical.
EDIT: Thanks to Satras in the comments below - EMC (Who bought RSA) have published links to the Software Token Converter here:
  • First of all, I contacted IT and was sent the software Key as a .sdtid file.
  • I was a brand new RSA user, but if you already use RSA from a keyring or desktop computer, then you may skip the following: I followed the bog-standard Windows installation and setup process by following the standard instructions provided by RSA, including (importantly) setting a PIN, roughly this involved the following:
    • The Software was already installed on my box, but you might have to install it from here http://www.rsa.com/node.aspx?id=2521 (Link updated, 3rd Sep 2012)
    • Import the .sdtid file including adding a password (in this case my username - I think just to unlock the import file)
    • Log into your companies securid page, ours is http://securid.thoughtworks.com/webauthentication (This requires you to add the Vanilla code from your RSA software app (ie, when the windows app prompts for a pin, leave it blank)
    • Set a pin
    • Log into https://securid.thoughtworks.com/ again - this time using your newly created pin in the windows app. (I assume the PIN is simply hashed into the code if you enter one).
  • This then allowed me to fire up the windows app, enter my pin, and get an RSA code/hash/temp Password/thing.
Android easy bit…
  • Browse here: https://www.rsa.com/node.aspx?id=1081 then to the android link.
  • Download the instructions, and on your phone follow the link (QR code) to the Android app in the marketplace. Download/install it.
Android tricky bit…
  • You have to get the key onto your android phone somehow, after several false starts I realised you have to convert the token (.sdtid file) from XML into the “compressed token format”.
  • Download the Software Token converter from RSA, there’s a link from this page: https://www.rsa.com/node.aspx?id=1081 (You may need to register to get this)
  • Follow the instructions. In brief it’s a console app, and you need to pass it the file locations (.sdtid) password (my username in this case) and “-android”. For example:
    TokenConvert.exe c:\MyUserName.sdtid -iphone -p myUsername
    or
    TokenConvert.exe c:\MyUserName.sdtid -android -p myUsername
  • It then spits out an http URI(android) or a funny looking iphone protocol Uri, right there in the console window.
  • Paste that URI into an email, and send it to an account you can read on your android phone. You might need to make sure it is a link, not just text, in your source email client
  • Back on your phone, open the email and click the link, this prompted me to open the link in the RSA app, if it doesn’t, I think you can copy/paste the link into the RSA app manually.
  • Some colleagues of mine who tried this recently have been re-propmted for their password at this point (in our case - our username)
  • The RSA app declared all was well.
  • I was then able to open the app on my phone, enter my pin, and get one of the RSA number/key/things, which worked to log into the Thoughtworks secure sites. I haven’t tried it from home yet, but will report back if it doesn’t work.
comments

Regex to Find \n Without \r

I recently had a problem with the IIS6 SMTP server on one of our sites. We were getting lots of script kiddie attackes, which happened to trigger an exception which in turn fired of squillions of Error emails. After a while the SMTP server seemed to loose the will to live and quietly coughed and then died.

It turned out (Diagnosed thanks to this blog post from Dylan Beattie) that SMTP doesn’t strictly allow a \n (newline) without an \r (carriage return). I’d released a new bit of code that splatted precisely these characters into a nice verbose error email, designed to help me figure out what our script kiddie was up to. Long story short, this broke the smtp server (read Dylan’s post for details).

Anyway, this lead me to want to find any occurences of \n in string literals in all of my code. I assumed I could do this using Visual studio’s Find in Files with the Regex box ticked. A bit of regex wrangling ensued, but we think we settled on the following expression:

~(\r)\n

Which seemed to do the trick. At some point we may modify our email code to search for lone \n characters in message bodies, in which case the same regex could be modified (I think) by simply removing the escaping on the slash characters.

~(\r)\n
comments

Mocking (or Faking, or Maybe Stubbing) a WSDL.exe SOAP Service Proxy.

Mocking or faking a SOAP web service in .Net

A few days ago I came up against a problem with a 3rd party SOAP web service we’re consuming from an ASP.Net website. It’s one of those special SOAP web services that has about 100 methods, each of which has the same signature: takes one argument, a string, and returns a string. The problem is that those strings are huge bloody great XML documents, which have to validate against several different XSD schema’s each.

Not being particularly savy with “Enterprise grade” XML, I was causing the 3rd party’s development server to choke on my data and vomit up a nice exception. Whenever I asked for help, I was politely told that my XML didn’t validate, and to bugger off and stop abusing their poor server until it did. So I wanted to write tests which assert that the XML submitted to any particular method should validate against the relevant XSD schema(s). Only once that test passed would I dare to submit anything to the development service.

A little about our architecture:

We have a facade class which hides the crazy / wraps the service. Amongst other things it builds the XML, submits it to the service, and parses the results. I wanted to test my facade, by mocking out the actual web service bit to both capture the XML that was submitted (so I could validate it) and also stop me from having to wait 30 seconds for a response to wind it’s way back across the intertubes from the 3rd party.

“Easy” thinks I, “code to a contract, not an implementation, and use dependency injection to pass the web service proxy into the Facade”. On live and in my integration tests I can pass in the real thing, and in my fast facade tests (or any other time it suits me) I can pull the old switcheroo and pass in a fake (or a mock, or a spy…)

Herin lies the rub…

Being a bit Old school, I’d consumed the service by using WSDL.exe, which knocks you up a C# class containing a proxy for the webservice. You can then merrily call this proxy like any other class with out having to lick so much as a single SOAP envelope. The problem I had was how to swap it out for something else. How do I “code to a contract not an implementation” when the only implementation I have was written by a tool (snigger) and is decidedly concrete?

1st thought, does it implement any kind of interface?
No. Didn’t I just say that?

2nd thought, can I inherit from it to create my mock/fake/stub/whatchamacallit, and override the methods under test to capture the XML?
Sort of - but not nicely. The problem is the class is marked “internal”. Both my facade and the proxy live in the same business project (and hence assembly). This was fine when the only class that had any references to the proxy was the facade. But now I want to inject the damned things from all over the shop, and inherit from them in test projects and the like. All my Fakes, stubs and mocks would have to live in the same assembly as the proxy. Not wanting to pollute the business project with test code, I vetoed that option.

I could have manually edited the class to make it, and all it’s methods, public. However editing classes generated by a tool is not a clever idea. Firstly it would be really boring and take ages (ctrl-h), secondly two years from now when the web service changes ever so slightly, some poor sap (probably me) is going to naively regenerate the class, and cause about a thousand compiler errors. After sobbing into my coffee and checking the comments in the version logs, the afore mentioned sap will figure out why and then spend a mind numbing afternoon re-editing the automatically generated class. Again. (ctrl-h)

3rd thought - can I get WSDL.exe to generate interfaces for me?

I didn’t know, and google didn’t tell me after a cursory glance, So I asked on stack overflow. The answer is… nearly. WSDL.exe has an optional switch, /si which generates an interface instead of a proxy class. Unfortunately that’s all it generates and there’s no way to make it generate proxy classes which implement that interface. The idea seems to be that if you’re given a WSDL of a service you want to implement, rather than consume, you can get Visual Studio (or resharper) to stub out the generated interface, then just decorate it with [WebMethod] attributes and you’re away. Which leads one to ask…

4th thought - could you implement your mock as a web service, and merely change the URI to point your proxy at that instead of the 3rd party?

No. (but yes in theory). The idea is to write fast tests to validate some XML. I don’t want that to involve http, not to mention that the mock service would have to run somewhere (Cassini presumably) and I guess would therefore be in a different process to the tests which invoked it, making it a bugger to extract the XML back into the test to assert that it validates. There are even third party programs which will mock a web service based on it’s WSDL (http://www.soapui.org/Getting-Started/mock-services.html), but I still don’t think these belong in a fast test. Anyway, It’s the Facade I want to test, not the SOAP service or proxy classes. The facade barely knows anything about the service, and certainly doesn’t depend on the fact that the interwebs are carrying it’s calls across the ether and back again.

Epiphany!

Prompted by the answers on stack overflow, I realised I could create a class that inherited from the concrete proxy class /and/ the interface. The inherited concrete methods provide an implicit implementation of the interface. Awesome! I can now write my code against the interface, and use this little beastie to cast my concrete classes to said interface.

That’s the Adaptor pattern isn’t it?

The only problem is that the concrete class is still “internal”, which has two consequences. First, my adaptor has to live in the same project/assembly as the proxy class. Well that’s fine, that’s where it belongs anyway. The big problem is that a derived class can not be declared with laxer (more public) access modifiers than it’s base class. In other words my Adaptor has to be marked Internal too. That was a bit of a pain, as I wanted to be using this thing in another set of tests, my integration tests. I solved that problem by building a little factory class that news up an instance of the adaptor, but returns it as an instance of the interface. As a neat side effect it’s also the nicest place to worry about the URI of the web service when newing up the proxy class.

Now that my Facade was coded to an interface, it was the work of but a moment to get resharper to stub out another class in my test project to fake the web service (I’m sure it would have been just as easy to use moq or something). I’m still not sure if my new test class is a mock a fake or a spy. It captures the submitted string argument (our XML) and saves it safely in a property (called SuperSecretSpyXmlArgumentOfDoom or something). It then promptly loses the will to live and throws a NotImplementedException. (it probably shouldn’t do that, but it saves the fake from needing to return valid XML, which the facade would attempt to parse). The test catches and ignores the exception, and can extract the XML from the fake and validate it against the XML schema.

My XML has now been XSD certified as all natural and 100% organic, thus proving that the 3rd party web service must have been vomiting up exceptions because it’s a sickly, weak and feeble, excuse of a web service.

Eat that 3rd party web service.
comments

A Mobile Formatted TFL Tube Updates Page

For some reason Transport for London don’t seem to publish a mobile site with live tube updates (for smartphones - not WAP). I don’t really want to have to download a whole tfl app, it’s not like live tube updates are any use to me off-line, a webpage does the trick. TFL do publish an embedable javascript widget, which on an otherwise empty page does the job nicely, and so here is that missing page:

Mobile formatted live tube updates from TFL
(On a desktop computer it’s also faster to load that the full TFL homepage)

I first threw this up as a little page for my own use when I got my Android phone about 18 months ago, but I’ve just refreshed it a wee bit. The following should work perfectly on any smartphone or small-screen device which supports javascript (Android and iPhone for starters). Just bookmark the above page and then uninstall that irritating tfl live info app.

Edit: Of course, you could cut out the middle man and simply go here: http://www.tfl.gov.uk/tfl/syndication/feeds/serviceboard-fullscreen.htm (which also works without javascript, although with javascript it rather aggresively resizes itself to be fullscreen).
comments

Arduino Motor Shield

I’ve just spent a happy couple of hours soldering up a new motor shield for my Arduino. It’s the newer one that can control 2 steppers, 2 servos or 4 DC motors.

Arduino Motor Shield
Soldering away. Note the awesome quality lamp shade - recycling in action there.
Arduino motor shield taking shape
I found the instructions pretty easy to follow. The only problems I had were with the header pins for the Servos, which insisted on falling out before I could solder the little buggers in. I also had to clip the legs of the reset switch and connector block, because they were fouling the power connector on the Arduino below.
Motor shield finished and in place on my Arduino
And here it is in place on my Arduino. I haven’t actually tried it out yet, hope I didn’t fry anything.

I confess I didn’t really twig that I was getting a kit rather than a finished board until it arrived, but with hindsight the kit was far more fun. Kinda like getting a free Airfix model with your motor controller.

This motor controller ishopefully destined to go inside one of my woodwork projects if I ever finish it, but more on that another day.
comments

New Subtext Skin - Needed Fixing Though

I upgraded to the (almost) latest version of Subtext the other day, and with it came a bunch of new skins, including a very nice one called ”eco” Unfortunately, to get the background image centered relative to the content, they’d put everything in a container div with a fixed width, this looked fine on my widescreen monitor at work, but at home on my twin normal (1280x1024) monitors I’d get horizontal scrollbars and the content would be offset.

So I had a little faff with Firebug, and by removing the fixed width, and adding in “background-position:center top;” instead, everything lined up nicely and the scrollbars were banished. Made the changes in the skin’s style sheet and it seems to have done the trick.
comments

Goodbye Dash Timer - Hello Pomodoro Timer.

A few years ago I read an article over at 43 folders about running a dash. In short, a dash is a silly little mind hack to get you off your arse so that you get started on whatever task is waiting for you. Washing up to do, project to code, chapel ceiling to paint, whatever.

The idea is that you get a kitchen timer, and set it to an silly, unintimidatingly short time, like 3 minutes (seriously) and you start work. Anyone can manage 3 lousy minutes of hard graft, even someone as lazy as me. Then after 3 minutes, you’re absolutely allowed to stop. No guilt. The trick is, the biggest problem wasn’t working, it was starting, and usually by the time you’ve done 3 minutes you’re away. Procrastination defeated, awesome productivity and world domination within your grasp.

I found this works pretty well for me so I modded myself a little timer, the slightly geeky details of which are why I’m really writing this. I used to keep this timer on my desk at work in a room full of people who have their own productivity to worry about, so In order to avoid being glared at/ostracised/lynched I hacked the timer so that I could turn off the beep and put in an LED so there is a little flashing light instead. There’s a switch so I can flip between lamp and buzzer if I need a normal timer.

The timer was a cheap kitchen timer that I think I found in Maplin, where I probably also picked up the SPDT toggle switch. Wire and LEDs I have lying around in boxes because I like that sort of thing (and I hoard) .

Back of the timer showing contacts to piezoelectric buzzer

First up, I unscrewed the back and had a look around expecting to find the wires to the buzzer. Instead it simply contacts with those two little springs, which are soldered through from the far side. So I flipped it over, poked around with a multimeter for a bit, cut a track and soldered three wires straight on to the board.

Back of the timer circuit board showing extra wires

Then I cut a small hole in the back of the timer, soldered on the toggle switch and LED, and gobbed the whole affair up with Polymorph. In my mind this was going to be a neat and nearly seamless extension to the timer, in practice it’s tricky stuff to use, and sets to slightly translucent, grubby, off-white colour so the end result looks a little ..um.. obscene. Incidentally, I am never going to take this through an airport as I hate to think what it looks like on a baggage scanner. (battery, timer, wires, plastic mass).

The finished timer, with switch and little LED lamp

What I’ve found since I’ve been trying to learn TDD, is that for my home programming tasks writing a single test is a convenient quantum of work even though it usually takes me a lot longer than 3 minutes. I tried this the other day and ended up doing about 3 hours of good coding, Job done, and it frees up the timer to become a Pomodoro timer instead.

Speaking of procrastination, I took these photos with the vague idea that they could make blog post over two years ago, long before I even had a blog. So it’s evidently not a perfect solution to procrastination.
comments