File tree Expand file tree Collapse file tree 3 files changed +31
-2
lines changed Expand file tree Collapse file tree 3 files changed +31
-2
lines changed Original file line number Diff line number Diff line change @@ -295,6 +295,8 @@ class Net::BER::BerIdentifiedString < String
295295 attr_accessor :ber_identifier
296296 def initialize args
297297 super args
298+ # LDAP uses UTF-8 encoded strings
299+ force_encoding ( 'UTF-8' ) if respond_to? ( :encoding )
298300 end
299301end
300302
Original file line number Diff line number Diff line change @@ -12,9 +12,21 @@ module Net::BER::Extensions::String
1212 # User code should call either #to_ber_application_string or
1313 # #to_ber_contextspecific.
1414 def to_ber ( code = 0x04 )
15- [ code ] . pack ( 'C' ) + length . to_ber_length_encoding + self
15+ raw_string = raw_utf8_encoded
16+ [ code ] . pack ( 'C' ) + raw_string . length . to_ber_length_encoding + raw_string
1617 end
1718
19+ def raw_utf8_encoded
20+ if self . respond_to? ( :encode )
21+ # Strings should be UTF-8 encoded according to LDAP.
22+ # However, the BER code is not necessarily valid UTF-8
23+ self . encode ( 'UTF-8' ) . force_encoding ( 'ASCII-8BIT' )
24+ else
25+ self
26+ end
27+ end
28+ private :raw_utf8_encoded
29+
1830 ##
1931 # Creates an application-specific BER string encoded value with the
2032 # provided syntax code value.
Original file line number Diff line number Diff line change 7575 end
7676 end
7777 end
78+ if "Ruby 1.9" . respond_to? ( :encoding )
79+ context "strings" do
80+ it "should properly encode UTF-8 strings" do
81+ "\u00e5 " . force_encoding ( "UTF-8" ) . to_ber . should ==
82+ "\x04 \x02 \xC3 \xA5 "
83+ end
84+ it "should properly encode strings encodable as UTF-8" do
85+ "teststring" . encode ( "US-ASCII" ) . to_ber . should == "\x04 \n teststring"
86+ end
87+ it "should fail on strings that can not be converted to UTF-8" do
88+ error = Encoding ::UndefinedConversionError
89+ lambda { "\x81 " . to_ber } . should raise_exception ( error )
90+ end
91+ end
92+ end
7893end
7994
8095describe "BER decoding of" do
91106 [ 1 , [ 3 , "Administrator" , "ad_is_bogus" ] ]
92107 end
93108 end
94- end
109+ end
You can’t perform that action at this time.
0 commit comments