v2 / vlib / x / encoding / asn1 / utf8string_test.v
41 lines · 37 sloc · 1.21 KB · fdc49dc51a0d7e83abd5b383afaaab3f2793f2cf
Raw
1// Copyright (c) 2022, 2024 blackshirt. All rights reserved.
2// Use of this source code is governed by a MIT License
3// that can be found in the LICENSE file.
4module asn1
5
6// UTFString test
7struct UTF8StringTest {
8 s string
9 out []u8
10 err IError
11}
12
13fn test_uf8string_handling() ! {
14 data := [
15 UTF8StringTest{'test', [u8(12), 4, 116, 101, 115, 116], none},
16 UTF8StringTest{'abc', '\x0c\x03abc'.bytes(), none},
17 // rocket := `🚀` was rune, single unicode, rocket.bytes() = [240, 159, 154, 128]
18 UTF8StringTest{`🚀`.str(), [u8(12), 4, 240, 159, 154, 128], none},
19 UTF8StringTest{')', '\x0c\x01)'.bytes(), none},
20 UTF8StringTest{'\x13\x03ab\x00', [u8(12), 5, 19, 3, 97, 98, 0], none},
21 UTF8StringTest{'Test User 1', '\x0c\x0bTest User 1'.bytes(), none},
22 // invalid utf8 string, emoji with removed first and fifth byte
23 UTF8StringTest{'🐶🐶🐶🚀'.substr(0, 5), []u8{}, error('Utf8String: invalid UTF-8 string')},
24 ]
25
26 for c in data {
27 us := Utf8String.new(c.s) or {
28 assert err == c.err
29 continue
30 }
31 out := encode(us) or {
32 assert err == c.err
33 continue
34 }
35 assert out == c.out
36
37 uss, _ := Utf8String.decode(out)!
38 assert uss.tag().tag_number() == int(TagType.utf8string)
39 assert uss.value == c.s
40 }
41}
42