From 6e27f663062dd6f174e95abf80e8910254d92db8 Mon Sep 17 00:00:00 2001 From: Jocelyn Fiat Date: Wed, 9 Apr 2014 16:56:40 +0200 Subject: [PATCH] Improved BASE64 to update has_error when decoding. Added manual tests. --- library/text/encoder/src/base64.e | 14 ++++++++--- library/text/encoder/tests/test_base64.e | 32 ++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/library/text/encoder/src/base64.e b/library/text/encoder/src/base64.e index 10e63952..2da2146a 100644 --- a/library/text/encoder/src/base64.e +++ b/library/text/encoder/src/base64.e @@ -125,17 +125,17 @@ feature -- Decoder byte_count := 0 pos := next_encoded_character_position (v, pos) - if pos <= n then + if pos < n then byte1 := base64chars.index_of (v[pos], 1) - 1 byte_count := byte_count + 1 pos := next_encoded_character_position (v, pos) - if pos <= n then + if pos < n then byte2 := base64chars.index_of (v[pos], 1) - 1 byte_count := byte_count + 1 pos := next_encoded_character_position (v, pos) - if pos <= n then + if pos < n then c := v[pos] if c /= '=' then byte3 := base64chars.index_of (c, 1) - 1 @@ -150,8 +150,14 @@ feature -- Decoder byte_count := byte_count + 1 end end + else + has_error := True end + else + has_error := True end + else + has_error := True end -- pos := pos + byte_count @@ -293,7 +299,7 @@ feature {NONE} -- Constants character_map: STRING = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=" note - copyright: "Copyright (c) 1984-2011, Eiffel Software and others" + copyright: "Copyright (c) 2011-2014, Eiffel Software and others" license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)" source: "[ Eiffel Software diff --git a/library/text/encoder/tests/test_base64.e b/library/text/encoder/tests/test_base64.e index e0b0d5ea..14530dc9 100644 --- a/library/text/encoder/tests/test_base64.e +++ b/library/text/encoder/tests/test_base64.e @@ -34,6 +34,38 @@ feature -- Test routines assert ("decoded encoded string is same", u ~ s) end +feature -- Tests + + test_valid_64_encoding + do + assert ("Expected encoded True:", is_valid_base64_encoding ((create {BASE64}).encoded_string ("content"))) + end + + test_not_valid64_encoding + do + assert ("Expected encoded False:", not is_valid_base64_encoding ("content")) + assert ("Expected encoded False:", not is_valid_base64_encoding ("!@#$%%^")) + end + +feature {NONE} -- Implementation + + is_valid_base64_encoding (a_string: STRING): BOOLEAN + -- is `a_string' base64 encoded? + local + l_encoder: BASE64 + l_string: STRING + l_retry: BOOLEAN + do + if not l_retry then + create l_encoder + l_string := l_encoder.decoded_string (a_string) + Result := not l_encoder.has_error + end + rescue + l_retry := True + retry + end + note copyright: "Copyright (c) 1984-2011, Eiffel Software and others" license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"