summaryrefslogtreecommitdiffstats
path: root/security/nss/gtests/base_gtest/utf8_unittest.cc
blob: a906529ea9b51764d50bc43d6fdaeffb47446b03 (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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "gtest/gtest.h"

#include "nss.h"
#include "base.h"
#include "secerr.h"

namespace nss_test {

class Utf8Test : public ::testing::Test {};

// Tests nssUTF8_Length rejects overlong forms, surrogates, etc.
TEST_F(Utf8Test, Utf8Length) {
  PRStatus status;

  EXPECT_EQ(0u, nssUTF8_Length("", &status));
  EXPECT_EQ(PR_SUCCESS, status);

  // U+0000..U+007F
  EXPECT_EQ(1u, nssUTF8_Length("\x01", &status));
  EXPECT_EQ(PR_SUCCESS, status);
  EXPECT_EQ(1u, nssUTF8_Length("\x7F", &status));
  EXPECT_EQ(PR_SUCCESS, status);

  // lone trailing byte
  EXPECT_EQ(0u, nssUTF8_Length("\x80", &status));
  EXPECT_EQ(PR_FAILURE, status);
  EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError());
  EXPECT_EQ(0u, nssUTF8_Length("\xBF", &status));
  EXPECT_EQ(PR_FAILURE, status);
  EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError());

  // overlong U+0000..U+007F
  EXPECT_EQ(0u, nssUTF8_Length("\xC0\x80", &status));
  EXPECT_EQ(PR_FAILURE, status);
  EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError());
  EXPECT_EQ(0u, nssUTF8_Length("\xC1\xBF", &status));
  EXPECT_EQ(PR_FAILURE, status);
  EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError());

  // U+0080..U+07FF
  EXPECT_EQ(2u, nssUTF8_Length("\xC2\x80", &status));
  EXPECT_EQ(PR_SUCCESS, status);
  EXPECT_EQ(2u, nssUTF8_Length("\xDF\xBF", &status));
  EXPECT_EQ(PR_SUCCESS, status);

  // overlong U+0000..U+07FF
  EXPECT_EQ(0u, nssUTF8_Length("\xE0\x80\x80", &status));
  EXPECT_EQ(PR_FAILURE, status);
  EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError());
  EXPECT_EQ(0u, nssUTF8_Length("\xE0\x9F\xBF", &status));
  EXPECT_EQ(PR_FAILURE, status);
  EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError());

  // U+0800..U+D7FF
  EXPECT_EQ(3u, nssUTF8_Length("\xE0\xA0\x80", &status));
  EXPECT_EQ(PR_SUCCESS, status);
  EXPECT_EQ(3u, nssUTF8_Length("\xE0\xBF\xBF", &status));
  EXPECT_EQ(PR_SUCCESS, status);
  EXPECT_EQ(3u, nssUTF8_Length("\xE1\x80\x80", &status));
  EXPECT_EQ(PR_SUCCESS, status);
  EXPECT_EQ(3u, nssUTF8_Length("\xEC\xBF\xBF", &status));
  EXPECT_EQ(PR_SUCCESS, status);
  EXPECT_EQ(3u, nssUTF8_Length("\xED\x80\x80", &status));
  EXPECT_EQ(PR_SUCCESS, status);
  EXPECT_EQ(3u, nssUTF8_Length("\xED\x9F\xBF", &status));
  EXPECT_EQ(PR_SUCCESS, status);

  // lone surrogate
  EXPECT_EQ(0u, nssUTF8_Length("\xED\xA0\x80", &status));
  EXPECT_EQ(PR_FAILURE, status);
  EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError());
  EXPECT_EQ(0u, nssUTF8_Length("\xED\xBF\xBF", &status));
  EXPECT_EQ(PR_FAILURE, status);
  EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError());

  // U+E000..U+FFFF
  EXPECT_EQ(3u, nssUTF8_Length("\xEE\x80\x80", &status));
  EXPECT_EQ(PR_SUCCESS, status);
  EXPECT_EQ(3u, nssUTF8_Length("\xEF\xBF\xBF", &status));
  EXPECT_EQ(PR_SUCCESS, status);

  // overlong U+0000..U+FFFF
  EXPECT_EQ(0u, nssUTF8_Length("\xF0\x80\x80\x80", &status));
  EXPECT_EQ(PR_FAILURE, status);
  EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError());
  EXPECT_EQ(0u, nssUTF8_Length("\xF0\x8F\xBF\xBF", &status));
  EXPECT_EQ(PR_FAILURE, status);
  EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError());

  // U+10000..U+10FFFF
  EXPECT_EQ(4u, nssUTF8_Length("\xF0\x90\x80\x80", &status));
  EXPECT_EQ(PR_SUCCESS, status);
  EXPECT_EQ(4u, nssUTF8_Length("\xF0\xBF\xBF\xBF", &status));
  EXPECT_EQ(PR_SUCCESS, status);
  EXPECT_EQ(4u, nssUTF8_Length("\xF1\x80\x80\x80", &status));
  EXPECT_EQ(PR_SUCCESS, status);
  EXPECT_EQ(4u, nssUTF8_Length("\xF3\xBF\xBF\xBF", &status));
  EXPECT_EQ(PR_SUCCESS, status);
  EXPECT_EQ(4u, nssUTF8_Length("\xF4\x80\x80\x80", &status));
  EXPECT_EQ(PR_SUCCESS, status);
  EXPECT_EQ(4u, nssUTF8_Length("\xF4\x8F\xBF\xBF", &status));
  EXPECT_EQ(PR_SUCCESS, status);

  // out of Unicode range
  EXPECT_EQ(0u, nssUTF8_Length("\xF4\x90\x80\x80", &status));
  EXPECT_EQ(PR_FAILURE, status);
  EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError());
  EXPECT_EQ(0u, nssUTF8_Length("\xF4\xBF\xBF\xBF", &status));
  EXPECT_EQ(PR_FAILURE, status);
  EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError());
  EXPECT_EQ(0u, nssUTF8_Length("\xF5\x80\x80\x80", &status));
  EXPECT_EQ(PR_FAILURE, status);
  EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError());
  EXPECT_EQ(0u, nssUTF8_Length("\xF7\xBF\xBF\xBF", &status));
  EXPECT_EQ(PR_FAILURE, status);
  EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError());

  // former 5-byte sequence
  EXPECT_EQ(0u, nssUTF8_Length("\xF8\x80\x80\x80\x80", &status));
  EXPECT_EQ(PR_FAILURE, status);
  EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError());
  EXPECT_EQ(0u, nssUTF8_Length("\xFB\xBF\xBF\xBF\xBF", &status));
  EXPECT_EQ(PR_FAILURE, status);
  EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError());

  // former 6-byte sequence
  EXPECT_EQ(0u, nssUTF8_Length("\xFC\x80\x80\x80\x80\x80", &status));
  EXPECT_EQ(PR_FAILURE, status);
  EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError());
  EXPECT_EQ(0u, nssUTF8_Length("\xFD\xBF\xBF\xBF\xBF\xBF", &status));
  EXPECT_EQ(PR_FAILURE, status);
  EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError());

  // invalid lead byte
  EXPECT_EQ(0u, nssUTF8_Length("\xFE", &status));
  EXPECT_EQ(PR_FAILURE, status);
  EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError());
  EXPECT_EQ(0u, nssUTF8_Length("\xFF", &status));
  EXPECT_EQ(PR_FAILURE, status);
  EXPECT_EQ(NSS_ERROR_INVALID_STRING, NSS_GetError());

  nss_DestroyErrorStack();
}
}  // namespace nss_test