1 require File.dirname(__FILE__) + '/../test_helper'
3 class MessageTest < ActiveSupport::TestCase
7 EURO = "\xe2\x82\xac" #euro symbol
9 # This needs to be updated when new fixtures are added
11 def test_check_message_count
12 assert_equal 2, Message.count
15 def test_check_empty_message_fails
17 assert !message.valid?
18 assert message.errors[:title].any?
19 assert message.errors[:body].any?
20 assert message.errors[:sent_on].any?
21 assert true, message.message_read
24 def test_validating_msgs
25 message = messages(:one)
27 massage = messages(:two)
31 def test_invalid_send_recipient
32 message = messages(:one)
34 message.recipient = nil
35 assert !message.valid?
37 assert_raise(ActiveRecord::RecordNotFound) { User.find(0) }
38 message.from_user_id = 0
39 message.to_user_id = 0
40 assert_raise(ActiveRecord::RecordInvalid) {message.save!}
43 def test_utf8_roundtrip
45 assert_message_ok('c', i)
46 assert_message_ok(EURO, i)
50 def test_length_oversize
51 assert_raise(ActiveRecord::RecordInvalid) { make_message('c', 256).save! }
52 assert_raise(ActiveRecord::RecordInvalid) { make_message(EURO, 256).save! }
56 # See e.g http://en.wikipedia.org/wiki/UTF-8 for byte sequences
57 # FIXME - Invalid Unicode characters can still be encoded into "valid" utf-8 byte sequences - maybe check this too?
58 invalid_sequences = ["\xC0", # always invalid utf8
59 "\xC2\x4a", # 2-byte multibyte identifier, followed by plain ASCII
60 "\xC2\xC2", # 2-byte multibyte identifier, followed by another one
61 "\x4a\x82", # plain ASCII, followed by multibyte continuation
62 "\x82\x82", # multibyte continuations without multibyte identifier
63 "\xe1\x82\x4a", # three-byte identifier, contination and (incorrectly) plain ASCII
65 invalid_sequences.each do |char|
67 # create a message and save to the database
68 msg = make_message(char, 1)
69 # if the save throws, thats fine and the test should pass, as we're
70 # only testing invalid sequences anyway.
73 # get the saved message back and check that it is identical - i.e:
74 # its OK to accept invalid UTF-8 as long as we return it unmodified.
75 db_msg = msg.class.find(msg.id)
76 assert_equal char, db_msg.title, "Database silently truncated message title"
78 rescue ActiveRecord::RecordInvalid
79 # because we only test invalid sequences it is OK to barf on them
84 def make_message(char, count)
85 message = messages(:one)
86 message.title = char * count
90 def assert_message_ok(char, count)
91 message = make_message(char, count)
93 response = message.class.find(message.id) # stand by for some über-generalisation...
94 assert_equal char * count, response.title, "message with #{count} #{char} chars (i.e. #{char.length*count} bytes) fails"