summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_codecencodings_jp.py
blob: 483b7dbe12886d99c85b93db77eaab12a9cd3e56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python
#
# test_codecencodings_jp.py
#   Codec encoding tests for Japanese encodings.
#
# $CJKCodecs: test_codecencodings_jp.py,v 1.3 2004/06/19 06:09:55 perky Exp $

from test import test_support
from test import test_multibytecodec_support
import unittest

class Test_CP932(test_multibytecodec_support.TestBase, unittest.TestCase):
    encoding = 'cp932'
    tstring = test_multibytecodec_support.load_teststring('shift_jis')
    codectests = (
        # invalid bytes
        ("abc\x81\x00\x81\x00\x82\x84", "strict",  None),
        ("abc\xf8", "strict",  None),
        ("abc\x81\x00\x82\x84", "replace", u"abc\ufffd\uff44"),
        ("abc\x81\x00\x82\x84\x88", "replace", u"abc\ufffd\uff44\ufffd"),
        ("abc\x81\x00\x82\x84", "ignore",  u"abc\uff44"),
        # sjis vs cp932
        ("\\\x7e", "replace", u"\\\x7e"),
        ("\x81\x5f\x81\x61\x81\x7c", "replace", u"\uff3c\u2225\uff0d"),
    )

class Test_EUC_JISX0213(test_multibytecodec_support.TestBase,
                        unittest.TestCase):
    encoding = 'euc_jisx0213'
    tstring = test_multibytecodec_support.load_teststring('euc_jisx0213')
    codectests = (
        # invalid bytes
        ("abc\x80\x80\xc1\xc4", "strict",  None),
        ("abc\xc8", "strict",  None),
        ("abc\x80\x80\xc1\xc4", "replace", u"abc\ufffd\u7956"),
        ("abc\x80\x80\xc1\xc4\xc8", "replace", u"abc\ufffd\u7956\ufffd"),
        ("abc\x80\x80\xc1\xc4", "ignore",  u"abc\u7956"),
        ("abc\x8f\x83\x83", "replace", u"abc\ufffd"),
        ("\xc1\x64", "strict", None),
        ("\xa1\xc0", "strict", u"\uff3c"),
    )
    xmlcharnametest = (
        u"\xab\u211c\xbb = \u2329\u1234\u232a",
        "\xa9\xa8ℜ\xa9\xb2 = ⟨ሴ⟩"
    )

eucjp_commontests = (
    ("abc\x80\x80\xc1\xc4", "strict",  None),
    ("abc\xc8", "strict",  None),
    ("abc\x80\x80\xc1\xc4", "replace", u"abc\ufffd\u7956"),
    ("abc\x80\x80\xc1\xc4\xc8", "replace", u"abc\ufffd\u7956\ufffd"),
    ("abc\x80\x80\xc1\xc4", "ignore",  u"abc\u7956"),
    ("abc\x8f\x83\x83", "replace", u"abc\ufffd"),
    ("\xc1\x64", "strict", None),
)

class Test_EUC_JP_COMPAT(test_multibytecodec_support.TestBase,
                         unittest.TestCase):
    encoding = 'euc_jp'
    tstring = test_multibytecodec_support.load_teststring('euc_jp')
    codectests = eucjp_commontests + (
        ("\xa1\xc0\\", "strict", u"\uff3c\\"),
        (u"\xa5", "strict", "\x5c"),
        (u"\u203e", "strict", "\x7e"),
    )

shiftjis_commonenctests = (
    ("abc\x80\x80\x82\x84", "strict",  None),
    ("abc\xf8", "strict",  None),
    ("abc\x80\x80\x82\x84", "replace", u"abc\ufffd\uff44"),
    ("abc\x80\x80\x82\x84\x88", "replace", u"abc\ufffd\uff44\ufffd"),
    ("abc\x80\x80\x82\x84def", "ignore",  u"abc\uff44def"),
)

class Test_SJIS_COMPAT(test_multibytecodec_support.TestBase, unittest.TestCase):
    encoding = 'shift_jis'
    tstring = test_multibytecodec_support.load_teststring('shift_jis')
    codectests = shiftjis_commonenctests + (
        ("\\\x7e", "strict", u"\\\x7e"),
        ("\x81\x5f\x81\x61\x81\x7c", "strict", u"\uff3c\u2016\u2212"),
    )

class Test_SJISX0213(test_multibytecodec_support.TestBase, unittest.TestCase):
    encoding = 'shift_jisx0213'
    tstring = test_multibytecodec_support.load_teststring('shift_jisx0213')
    codectests = (
        # invalid bytes
        ("abc\x80\x80\x82\x84", "strict",  None),
        ("abc\xf8", "strict",  None),
        ("abc\x80\x80\x82\x84", "replace", u"abc\ufffd\uff44"),
        ("abc\x80\x80\x82\x84\x88", "replace", u"abc\ufffd\uff44\ufffd"),
        ("abc\x80\x80\x82\x84def", "ignore",  u"abc\uff44def"),
        # sjis vs cp932
        ("\\\x7e", "replace", u"\xa5\u203e"),
        ("\x81\x5f\x81\x61\x81\x7c", "replace", u"\x5c\u2016\u2212"),
    )
    xmlcharnametest = (
        u"\xab\u211c\xbb = \u2329\u1234\u232a",
        "\x85Gℜ\x85Q = ⟨ሴ⟩"
    )

def test_main():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(Test_CP932))
    suite.addTest(unittest.makeSuite(Test_EUC_JISX0213))
    suite.addTest(unittest.makeSuite(Test_EUC_JP_COMPAT))
    suite.addTest(unittest.makeSuite(Test_SJIS_COMPAT))
    suite.addTest(unittest.makeSuite(Test_SJISX0213))
    test_support.run_suite(suite)

if __name__ == "__main__":
    test_main()
les/msg_constr_attr.c +++ /dev/null @@ -1,51 +0,0 @@ -struct nl_msg *build_msg(int ifindex, struct nl_addr *lladdr, int mtu) -{ - struct nl_msg *msg; - struct nlattr *info, *vlan; - struct ifinfomsg ifi = { - .ifi_family = AF_INET, - .ifi_index = ifindex, - }; - - /* Allocate a default sized netlink message */ - if (!(msg = nlmsg_alloc_simple(RTM_SETLINK, 0))) - return NULL; - - /* Append the protocol specific header (struct ifinfomsg)*/ - if (nlmsg_append(msg, &ifi, sizeof(ifi), NLMSG_ALIGNTO) < 0) - goto nla_put_failure - - /* Append a 32 bit integer attribute to carry the MTU */ - NLA_PUT_U32(msg, IFLA_MTU, mtu); - - /* Append a unspecific attribute to carry the link layer address */ - NLA_PUT_ADDR(msg, IFLA_ADDRESS, lladdr); - - /* Append a container for nested attributes to carry link information */ - if (!(info = nla_nest_start(msg, IFLA_LINKINFO))) - goto nla_put_failure; - - /* Put a string attribute into the container */ - NLA_PUT_STRING(msg, IFLA_INFO_KIND, "vlan"); - - /* - * Append another container inside the open container to carry - * vlan specific attributes - */ - if (!(vlan = nla_nest_start(msg, IFLA_INFO_DATA))) - goto nla_put_failure; - - /* add vlan specific info attributes here... */ - - /* Finish nesting the vlan attributes and close the second container. */ - nla_nest_end(msg, vlan); - - /* Finish nesting the link info attribute and close the first container. */ - nla_nest_end(msg, info); - - return msg; - -nla_put_failure: - nlmsg_free(msg); - return NULL; -} diff --git a/doc/src/examples/msg_parse_attr.c b/doc/src/examples/msg_parse_attr.c deleted file mode 100644 index 6e275e9..0000000 --- a/doc/src/examples/msg_parse_attr.c +++ /dev/null @@ -1,50 +0,0 @@ -int parse_message(struct nlmsghdr *hdr) -{ - /* - * The policy defines two attributes: a 32 bit integer and a container - * for nested attributes. - */ - struct nla_policy attr_policy[] = { - [ATTR_FOO] = { .type = NLA_U32 }, - [ATTR_BAR] = { .type = NLA_NESTED }, - }; - struct nlattr *attrs[ATTR_MAX+1]; - int err; - - /* - * The nlmsg_parse() function will make sure that the message contains - * enough payload to hold the header (struct my_hdr), validates any - * attributes attached to the messages and stores a pointer to each - * attribute in the attrs[] array accessable by attribute type. - */ - if ((err = nlmsg_parse(hdr, sizeof(struct my_hdr), attrs, ATTR_MAX, - attr_policy)) < 0) - goto errout; - - if (attrs[ATTR_FOO]) { - /* - * It is safe to directly access the attribute payload without - * any further checks since nlmsg_parse() enforced the policy. - */ - uint32_t foo = nla_get_u32(attrs[ATTR_FOO]); - } - - if (attrs[ATTR_BAR]) { - struct *nested[NESTED_MAX+1]; - - /* - * Attributes nested in a container can be parsed the same way - * as top level attributes. - */ - err = nla_parse_nested(nested, NESTED_MAX, attrs[ATTR_BAR], - nested_policy); - if (err < 0) - goto errout; - - // Process nested attributes here. - } - - err = 0; -errout: - return err; -} diff --git a/doc/src/examples/my_parse.c b/doc/src/examples/my_parse.c deleted file mode 100644 index c1ff637..0000000 --- a/doc/src/examples/my_parse.c +++ /dev/null @@ -1,11 +0,0 @@ -#include - -void my_parse(void *stream, int length) -{ - struct nlmsghdr *hdr = stream; - - while (nlmsg_ok(hdr, length)) { - // Parse message here - hdr = nlmsg_next(hdr, &length); - } -} diff --git a/doc/src/examples/nl_cb_set.c b/doc/src/examples/nl_cb_set.c deleted file mode 100644 index 4fbaefc..0000000 --- a/doc/src/examples/nl_cb_set.c +++ /dev/null @@ -1,14 +0,0 @@ -#include - -/* Allocate a callback set and initialize it to the verbose default set */ -struct nl_cb *cb = nl_cb_alloc(NL_CB_VERBOSE); - -/* Modify the set to call my_func() for all valid messages */ -nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, my_func, NULL); - -/* - * Set the error message handler to the verbose default implementation - * and direct it to print all errors to the given file descriptor. - */ -FILE *file = fopen(...); -nl_cb_err(cb, NL_CB_VERBOSE, NULL, file); diff --git a/doc/src/examples/nl_send_simple.c b/doc/src/examples/nl_send_simple.c deleted file mode 100644 index afbed7a..0000000 --- a/doc/src/examples/nl_send_simple.c +++ /dev/null @@ -1,11 +0,0 @@ -#include - -struct nl_sock *sk; -struct rtgenmsg rt_hdr = { - .rtgen_family = AF_UNSPEC, -}; - -sk = nl_socket_alloc(); -nl_connect(sk, NETLINK_ROUTE); - -nl_send_simple(sock, RTM_GETLINK, NLM_F_DUMP, &rt_hdr, sizeof(rt_hdr)); diff --git a/doc/src/examples/nla_flag.c b/doc/src/examples/nla_flag.c deleted file mode 100644 index dc3d0f9..0000000 --- a/doc/src/examples/nla_flag.c +++ /dev/null @@ -1,7 +0,0 @@ -/* nla_put_flag() appends a zero sized attribute to the message. */ -nla_put_flag(msg, ATTR_FLAG); - - -/* There is no need for a receival function, the presence is the value. */ -if (attrs[ATTR_FLAG]) - /* flag is present */ diff --git a/doc/src/examples/nla_for_each_attr.c b/doc/src/examples/nla_for_each_attr.c deleted file mode 100644 inde