// Copyright 2012 The Obvious Corporation.

/*
 * Tests of ursa
 */

/*
 * Modules used
 */

var assert = require("assert");

var fixture = require("./fixture");
var ursa =    fixture.ursa;


/*
 * Helper functions
 */

function test_getExponent(key) {
    var buf = key.getExponent();
    assert.equal(buf.toString(fixture.HEX), fixture.EXPONENT_HEX);

    var result = key.getExponent(fixture.HEX);
    assert.equal(result, fixture.EXPONENT_HEX);

    result = key.getExponent(fixture.BASE64);
    assert.equal(result, buf.toString(fixture.BASE64));

    result = key.getExponent(fixture.BINARY);
    assert.equal(result, buf.toString(fixture.BINARY));

    result = key.getExponent(fixture.UTF8);
    assert.equal(result, buf.toString(fixture.UTF8));
}

function test_getModulus(key) {
    var buf = key.getModulus();
    assert.equal(buf.toString(fixture.HEX), fixture.MODULUS_HEX);

    var result = key.getModulus(fixture.HEX);
    assert.equal(result, fixture.MODULUS_HEX);

    result = key.getModulus(fixture.BASE64);
    assert.equal(result, buf.toString(fixture.BASE64));

    result = key.getModulus(fixture.BINARY);
    assert.equal(result, buf.toString(fixture.BINARY));

    result = key.getModulus(fixture.UTF8);
    assert.equal(result, buf.toString(fixture.UTF8));
}

function test_toPublicPem(key) {
    var keyString = fixture.PUBLIC_KEY.toString(fixture.UTF8);
    var result = key.toPublicPem().toString(fixture.UTF8);
    assert.equal(result, keyString);

    result = key.toPublicPem(fixture.UTF8);
    assert.equal(result, keyString);
}

function test_encrypt(key) {
    // The sanest way to test this is to do a round trip.
    var privKey = ursa.createPrivateKey(fixture.PRIVATE_KEY)
    var encoded = key.encrypt(new Buffer(fixture.PLAINTEXT, fixture.UTF8));
    var decoded = privKey.decrypt(encoded, undefined, fixture.UTF8);
    assert.equal(decoded, fixture.PLAINTEXT);

    encoded = key.encrypt(fixture.PLAINTEXT, fixture.UTF8, fixture.BASE64);
    decoded = privKey.decrypt(encoded, fixture.BASE64, fixture.UTF8);
    assert.equal(decoded, fixture.PLAINTEXT);
}

function test_publicDecrypt(key) {
    var encoded = new Buffer(fixture.PUBLIC_CIPHERTEXT_HEX, fixture.HEX);
    var decoded = key.publicDecrypt(encoded).toString(fixture.UTF8);
    assert.equal(decoded, fixture.PLAINTEXT);

    decoded = key.publicDecrypt(fixture.PUBLIC_CIPHERTEXT_HEX, fixture.HEX,
                                fixture.UTF8);
    assert.equal(decoded, fixture.PLAINTEXT);
}

function testPublicKeyMethods(key) {
    test_getExponent(key);
    test_getModulus(key);
    test_toPublicPem(key);
    test_encrypt(key);
    test_publicDecrypt(key);
}

function testPrivateKeyMethods(key) {
    // FIXME
    // test_toPrivatePem(key);
    // test_decrypt(key);
    // test_privateEncrypt(key);
}


/*
 * Test functions
 */

function testBasics() {
    ursa.createPublicKey(fixture.PUBLIC_KEY);
    ursa.createPrivateKey(fixture.PRIVATE_KEY);
    ursa.createPrivateKey(fixture.PRIVATE_KEY, fixture.PASSWORD);
    ursa.generatePrivateKey(512);
}

function testPublicKey() {
    var key = ursa.createPublicKey(fixture.PUBLIC_KEY);
    testPublicKeyMethods(key);
}

function testPrivateKey() {
    var key = ursa.createPrivateKey(fixture.PRIVATE_KEY);
    testPublicKeyMethods(key);
    testPrivateKeyMethods(key);
}

function testGeneratedKey() {
    // Just do a round trip. If that works, then it's safe to believe
    // the native tests (which are more comprehensive).
    var key = ursa.generatePrivateKey();
    var encoded = key.encrypt(fixture.PLAINTEXT, fixture.UTF8);
    var decoded = key.decrypt(encoded, undefined, fixture.UTF8);
    assert.equal(decoded, fixture.PLAINTEXT);
}


/*
 * Main test script
 */

// Test the native code (reasonably) directly.
require("./native").test();

testBasics();
testPublicKey();
testPrivateKey();
testGeneratedKey();

console.log("All tests pass!");
@@ -2669,31 +2669,1037 @@
 unction test
+_to
 Private
+Pem(key) %7B%0A    var keyString = fixture.PRIVATE_KEY.toString(fixture.UTF8);%0A    var result = key.toPrivatePem().toString(fixture.UTF8);%0A    assert.equal(result, keyString);%0A%0A    result = key.toPrivatePem(fixture.UTF8);%0A    assert.equal(result, keyString);%0A%7D%0A%0Afunction test_decrypt(key) %7B%0A    var encoded = new Buffer(fixture.PRIVATE_CIPHERTEXT_HEX, fixture.HEX);%0A    var decoded = key.decrypt(encoded).toString(fixture.UTF8);%0A    assert.equal(decoded, fixture.PLAINTEXT);%0A%0A    decoded = key.decrypt(fixture.PRIVATE_CIPHERTEXT_HEX, fixture.HEX,%0A                          fixture.UTF8);%0A    assert.equal(decoded, fixture.PLAINTEXT);%0A%7D%0A%0Afunction test_privateEncrypt(key) %7B%0A    var encoded = key.privateEncrypt(%0A        new Buffer(fixture.PLAINTEXT, fixture.UTF8)).toString(fixture.HEX);%0A    assert.equal(encoded, fixture.PUBLIC_CIPHERTEXT_HEX);%0A%0A    encoded = key.privateEncrypt(fixture.PLAINTEXT, fixture.UTF8, fixture.HEX);%0A    assert.equal(encoded, fixture.PUBLIC_CIPHERTEXT_HEX);%0A%7D%0A%0Afunction testPrivate
 KeyMethods(k
@@ -3711,24 +3711,8 @@
 %0A   
- // FIXME%0A    //
  tes
@@ -3731,27 +3731,24 @@
 em(key);%0A   
- //
  test_decryp
@@ -3758,19 +3758,16 @@
 ey);%0A   
- //
  test_pr

Add tests for the private key methods.