Skip to content

Commit

Permalink
implementation, fixtures and tests for myRnd()
Browse files Browse the repository at this point in the history
  • Loading branch information
iamcal authored and felixge committed Sep 2, 2010
1 parent 274280b commit 0ee929b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/mysql/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ exports.randomInit = function(seed1, seed2) {
};
};

exports.myRnd = function(rand_struct) {
exports.myRnd = function(r){

r.seed1 = (r.seed1 * 3 + r.seed2) % r.max_value;
r.seed2 = (r.seed1 + r.seed2 + 33) % r.max_value;

return r.seed1 / r.max_value_dbl;
};

exports.scramble323 = function(message, password) {
Expand Down
8 changes: 8 additions & 0 deletions test/fixture/libmysql_password.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ int main() {
const char password3[] = "saf789yasfbsd89f";
ulong result[2];
struct rand_struct rand_st;
int i;

// test hash_password
hash_password((ulong*)result, password1, strlen(password1));
Expand All @@ -103,6 +104,13 @@ int main() {
printf("randominit(0xFFFFFFFF,0xFFFFFFFF) = %08x, %08x\n", rand_st.seed1, rand_st.seed2);


// test my_rnd
randominit(&rand_st, 3252345, 7149734);
printf("randominit(3252345, 7149734) = %08x, %08x\n", rand_st.seed1, rand_st.seed2);
for (i=0; i<10; i++){
printf("my_rnd() : %.16f\n", my_rnd(&rand_st));
}

return 23;
}

35 changes: 34 additions & 1 deletion test/simple/test-auth-old.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,46 @@ function test_randominit(in1, in2, out1, out2){
sys.print('ok\n');
}

function test_myrnd_sequence(seed1, seed2, expected){

var r = auth.randomInit(seed1, seed2);

for(var i=0; i<expected.length; i++){
var n = auth.myRnd(r);

// we will test to 14 digits, since
// we only ever use this function mutliplied
// by small numbers anyway

var a = ":"+n;
var b = ":"+expected[i];

sys.print('myRnd()->'+a.substr(1, 16)+'...');
assert.equal(a.substr(1, 16), b.substr(1, 16));
sys.print('ok\n');
}
}

test_hash_password('root', [0x67, 0x45, 0x7E, 0x22, 0x6a, 0x1a, 0x15, 0xbd]);
test_hash_password('long password test', [0x6c, 0x24, 0x68, 0x41, 0x2c, 0xa6, 0x86, 0x56]);
test_hash_password('saf789yasfbsd89f', [0x6c, 0x9b, 0x2f, 0x07, 0x17, 0xeb, 0x95, 0xc6]);


test_randominit(0x00000000, 0x00000000, 0x00000000, 0x00000000);
test_randominit(0x0000FFFF, 0x0000FFFF, 0x0000ffff, 0x0000ffff);
test_randominit(0x50000000, 0x50000000, 0x10000001, 0x10000001);
test_randominit(0xFFFFFFFF, 0xFFFFFFFF, 0x00000003, 0x00000003);
test_randominit(3252345, 7149734, 0x0031a079, 0x006d18a6);

test_myrnd_sequence(3252345, 7149734, [
0.0157456556481734,
0.0696413620092360,
0.3009698738353047,
0.2959253138824602,
0.5767169786400320,
0.9958089822864243,
0.2488940062456708,
0.2570431151027261,
0.5385335875102631,
0.9215386229767824,
]);

0 comments on commit 0ee929b

Please sign in to comment.