test/test_uri.rb in otp-0.0.9 vs test/test_uri.rb in otp-0.0.10

- old
+ new

@@ -5,21 +5,49 @@ uri = "otpauth://totp/account@example.com?secret=GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ" otp = OTP::URI.parse(uri) assert_equal("account@example.com", otp.accountname) assert_equal(nil, otp.issuer) + uri = "otpauth://totp/account@example.com?secret=GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ&issuer=Foo" + otp = OTP::URI.parse(uri) + assert_equal("account@example.com", otp.accountname) + assert_equal("Foo", otp.issuer) + uri = "otpauth://totp/My%20Company:%20%20account@example.com?secret=GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ" otp = OTP::URI.parse(uri) assert_equal("account@example.com", otp.accountname) assert_equal("My Company", otp.issuer) - uri = "otpauth://totp/My%20Company:%20%20account@example.com?secret=GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ" + uri = "otpauth://totp/My%20Company:%20%20account@example.com?secret=GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ&issuer=Foo" otp = OTP::URI.parse(uri) assert_equal("account@example.com", otp.accountname) assert_equal("My Company", otp.issuer) end + def test_totp_simple + secret = OTP::Base32.encode("12345678901234567890") + totp = OTP::TOTP.new + totp.secret = secret + totp.accountname = "account@example.com" + uri = totp.to_uri + assert_not_match(/algorithm=/, uri) + assert_not_match(/digits=/, uri) + assert_not_match(/issuer=/, uri) + assert_not_match(/period=/, uri) + + otp = OTP::URI.parse(uri) + assert_equal(OTP::TOTP, otp.class) + assert_equal(secret, otp.secret) + assert_equal("SHA1", otp.algorithm) + assert_equal(6, otp.digits) + assert_equal(30, otp.period) + assert_equal("account@example.com", otp.accountname) + assert_equal(nil, otp.issuer) + totp.time = otp.time = Time.now + assert_equal(otp.password, totp.password) + end + def test_totp secret = OTP::Base32.encode("12345678901234567890") totp = OTP::TOTP.new totp.secret = secret totp.algorithm = "SHA256" @@ -39,10 +67,32 @@ assert_equal("My Company", otp.issuer) totp.time = otp.time = Time.now assert_equal(otp.password, totp.password) end + def test_hotp_simple + secret = OTP::Base32.encode("12345678901234567890") + hotp = OTP::HOTP.new + hotp.secret = secret + hotp.accountname = "account@example.com" + uri = hotp.to_uri + assert_not_match(/algorithm=/, uri) + assert_not_match(/digits=/, uri) + assert_not_match(/issuer=/, uri) + assert_match(/count=0/, uri) + + otp = OTP::URI.parse(uri) + assert_equal(OTP::HOTP, otp.class) + assert_equal(secret, otp.secret) + assert_equal("SHA1", otp.algorithm) + assert_equal(6, otp.digits) + assert_equal(0, otp.count) + assert_equal("account@example.com", otp.accountname) + assert_equal(nil, otp.issuer) + assert_equal(otp.password, hotp.password) + end + def test_hotp secret = OTP::Base32.encode("12345678901234567890") hotp = OTP::HOTP.new hotp.secret = secret hotp.algorithm = "SHA256" @@ -62,9 +112,15 @@ assert_equal("My Company", otp.issuer) assert_equal(otp.password, hotp.password) end def test_parse_invalid - assert_raise(RuntimeError){ OTP::URI.parse("http://www.netlab.jp") } - assert_raise(RuntimeError){ OTP::URI.parse("otpauth://foo") } + e = assert_raise(RuntimeError){ OTP::URI.parse("http://www.netlab.jp") } + assert_match(/URI scheme not match/, e.message) + e = assert_raise(RuntimeError){ OTP::URI.parse("otpauth://foo") } + assert_match(/unknown OTP type/, e.message) + e = assert_raise(RuntimeError){ OTP::URI.parse("otpauth://version") } + assert_match(/unknown OTP type/, e.message) + e = assert_raise(RuntimeError){ OTP::URI.parse("otpauth://totp/") } + assert_match(/account name must be present/, e.message) end end