summaryrefslogtreecommitdiffstats
path: root/third_party/wasm2c/src/test-utf8.cc
blob: 8e420628bc21aa83adfdeafcf7c7b8408215a1d0 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * Copyright 2017 WebAssembly Community Group participants
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "gtest/gtest.h"

#include "src/utf8.h"

using namespace wabt;

namespace {

void assert_is_valid_utf8(bool expected,
                          int length,
                          int cu0 = 0,
                          int cu1 = 0,
                          int cu2 = 0,
                          int cu3 = 0) {
  assert(length <= 4);
  char buf[4] = {static_cast<char>(cu0), static_cast<char>(cu1),
                 static_cast<char>(cu2), static_cast<char>(cu3)};
  if (expected) {
    // Make sure it fails if there are continuation bytes past the end of the
    // string.
    for (int bad_length = 1; bad_length < length; ++bad_length) {
      ASSERT_FALSE(IsValidUtf8(buf, bad_length))
          << cu0 << ", " << cu1 << ", " << cu2 << ", " << cu3;
    }
  }

  ASSERT_TRUE(expected == IsValidUtf8(buf, length))
      << cu0 << ", " << cu1 << ", " << cu2 << ", " << cu3;
}

bool is_in_range(int x, int low, int high) {
  return x >= low && x < high;
}

}  // end anonymous namespace

#define FOR_RANGE(var, low, high) for (int var = low; var < high; var++)
#define FOR_EACH_BYTE(var) FOR_RANGE(var, 0, 0x100)

TEST(utf8, valid_empty) {
  assert_is_valid_utf8(true, 0);
}

TEST(utf8, valid_1_byte) {
  FOR_RANGE(cu0, 0, 0x80) { assert_is_valid_utf8(true, 1, cu0); }
}

TEST(utf8, invalid_continuation_bytes) {
  FOR_RANGE(cu0, 0x80, 0xc0) { assert_is_valid_utf8(false, 1, cu0); }
}

TEST(utf8, invalid_2_byte) {
  FOR_RANGE(cu0, 0xc0, 0xc2) { assert_is_valid_utf8(false, 1, cu0); }
}

TEST(utf8, valid_2_bytes) {
  FOR_RANGE(cu0, 0xc2, 0xe0) {
    FOR_EACH_BYTE(cu1) {
      bool is_valid = is_in_range(cu1, 0x80, 0xc0);
      assert_is_valid_utf8(is_valid, 2, cu0, cu1);
    }
  }
}

TEST(utf8, valid_3_bytes_e0) {
  int cu0 = 0xe0;
  FOR_EACH_BYTE(cu1) {
    FOR_EACH_BYTE(cu2) {
      bool is_valid =
          is_in_range(cu1, 0xa0, 0xc0) && is_in_range(cu2, 0x80, 0xc0);
      assert_is_valid_utf8(is_valid, 3, cu0, cu1, cu2);
    }
  }
}

TEST(utf8, valid_3_bytes) {
  FOR_RANGE(cu0, 0xe1, 0xf0) {
    // Handle 0xed in valid_3_bytes_ed.
    if (cu0 == 0xed) {
      continue;
    }

    FOR_EACH_BYTE(cu1) {
      FOR_EACH_BYTE(cu2) {
        bool is_valid =
            is_in_range(cu1, 0x80, 0xc0) && is_in_range(cu2, 0x80, 0xc0);
        assert_is_valid_utf8(is_valid, 3, cu0, cu1, cu2);
      }
    }
  }
}

TEST(utf8, valid_3_bytes_ed) {
  int cu0 = 0xed;
  FOR_EACH_BYTE(cu1) {
    FOR_EACH_BYTE(cu2) {
      bool is_valid =
          is_in_range(cu1, 0x80, 0xa0) && is_in_range(cu2, 0x80, 0xc0);
      assert_is_valid_utf8(is_valid, 3, cu0, cu1, cu2);
    }
  }
}

TEST(utf8, valid_4_bytes_f0) {
  int cu0 = 0xf0;
  FOR_EACH_BYTE(cu1) {
    FOR_EACH_BYTE(cu2) {
      FOR_EACH_BYTE(cu3) {
        bool is_valid = is_in_range(cu1, 0x90, 0xc0) &&
                        is_in_range(cu2, 0x80, 0xc0) &&
                        is_in_range(cu3, 0x80, 0xc0);
        assert_is_valid_utf8(is_valid, 4, cu0, cu1, cu2, cu3);
      }
    }
  }
}

TEST(utf8, valid_4_bytes) {
  FOR_RANGE(cu0, 0xf1, 0xf4) {
    FOR_EACH_BYTE(cu1) {
      FOR_EACH_BYTE(cu2) {
        FOR_EACH_BYTE(cu3) {
          bool is_valid = is_in_range(cu1, 0x80, 0xc0) &&
                          is_in_range(cu2, 0x80, 0xc0) &&
                          is_in_range(cu3, 0x80, 0xc0);
          assert_is_valid_utf8(is_valid, 4, cu0, cu1, cu2, cu3);
        }
      }
    }
  }
}

TEST(utf8, valid_4_bytes_f4) {
  int cu0 = 0xf4;
  FOR_EACH_BYTE(cu1) {
    FOR_EACH_BYTE(cu2) {
      FOR_EACH_BYTE(cu3) {
        bool is_valid = is_in_range(cu1, 0x80, 0x90) &&
                        is_in_range(cu2, 0x80, 0xc0) &&
                        is_in_range(cu3, 0x80, 0xc0);
        assert_is_valid_utf8(is_valid, 4, cu0, cu1, cu2, cu3);
      }
    }
  }
}

TEST(utf8, invalid_4_bytes) {
  FOR_RANGE(cu0, 0xf5, 0x100) {
    assert_is_valid_utf8(false, 4, cu0, 0x80, 0x80, 0x80);
  }
}