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
295
295
attr_accessor :ber_identifier
296
296
def initialize args
297
297
super args
298
+ # LDAP uses UTF-8 encoded strings
299
+ force_encoding ( 'UTF-8' ) if respond_to? ( :encoding )
298
300
end
299
301
end
300
302
Original file line number Diff line number Diff line change @@ -12,9 +12,21 @@ module Net::BER::Extensions::String
12
12
# User code should call either #to_ber_application_string or
13
13
# #to_ber_contextspecific.
14
14
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
16
17
end
17
18
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
+
18
30
##
19
31
# Creates an application-specific BER string encoded value with the
20
32
# provided syntax code value.
Original file line number Diff line number Diff line change 75
75
end
76
76
end
77
77
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
78
93
end
79
94
80
95
describe "BER decoding of" do
91
106
[ 1 , [ 3 , "Administrator" , "ad_is_bogus" ] ]
92
107
end
93
108
end
94
- end
109
+ end
You can’t perform that action at this time.
0 commit comments