aboutsummaryrefslogtreecommitdiff
path: root/builtin/game/item_entity.lua
blob: 9b1b23bfd14d6013297190013b8784dbfcfbf367 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
-- Minetest: builtin/item_entity.lua

function core.spawn_item(pos, item)
	-- Take item in any format
	local stack = ItemStack(item)
	local obj = core.add_entity(pos, "__builtin:item")
	-- Don't use obj if it couldn't be added to the map.
	if obj then
		obj:get_luaentity():set_item(stack:to_string())
	end
	return obj
end

-- If item_entity_ttl is not set, enity will have default life time
-- Setting it to -1 disables the feature

local time_to_live = tonumber(core.settings:get("item_entity_ttl")) or 900
local gravity = tonumber(core.settings:get("movement_gravity")) or 9.81


core.register_entity(":__builtin:item", {
	initial_properties = {
		hp_max = 1,
		physical = true,
		collide_with_objects = false,
		collisionbox = {-0.3, -0.3, -0.3, 0.3, 0.3, 0.3},
		visual = "wielditem",
		visual_size = {x = 0.4, y = 0.4},
		textures = {""},
		is_visible = false,
	},

	itemstring = "",
	moving_state = true,
	physical_state = true,
	-- Item expiry
	age = 0,
	-- Pushing item out of solid nodes
	force_out = nil,
	force_out_start = nil,

	set_item = function(self, item)
		local stack = ItemStack(item or self.itemstring)
		self.itemstring = stack:to_string()
		if self.itemstring == "" then
			-- item not yet known
			return
		end

		-- Backwards compatibility: old clients use the texture
		-- to get the type of the item
		local itemname = stack:is_known() and stack:get_name() or "unknown"

		local max_count = stack:get_stack_max()
		local count = math.min(stack:get_count(), max_count)
		local size = 0.2 + 0.1 * (count / max_count) ^ (1 / 3)
		local def = core.registered_items[itemname]
		local glow = def and def.light_source and
			math.floor(def.light_source / 2 + 0.5)

		self.object:set_properties({
			is_visible = true,
			visual = "wielditem",
			textures = {itemname},
			visual_size = {x = size, y = size},
			collisionbox = {-size, -size, -size, size, size, size},
			automatic_rotate = math.pi * 0.5 * 0.2 / size,
			wield_item = self.itemstring,
			glow = glow,
		})

	end,

	get_staticdata = function(self)
		return core.serialize({
			itemstring = self.itemstring,
			age = self.age,
			dropped_by = self.dropped_by
		})
	end,

	on_activate = function(self, staticdata, dtime_s)
		if string.sub(staticdata, 1, string.len("return")) == "return" then
			local data = core.deserialize(staticdata)
			if data and type(data) == "table" then
				self.itemstring = data.itemstring
				self.age = (data.age or 0) + dtime_s
				self.dropped_by = data.dropped_by
			end
		else
			self.itemstring = staticdata
		end
		self.object:set_armor_groups({immortal = 1})
		self.object:set_velocity({x = 0, y = 2, z = 0})
		self.object:set_acceleration({x = 0, y = -gravity, z = 0})
		self:set_item()
	end,

	try_merge_with = function(self, own_stack, object, entity)
		if self.age == entity.age then
			-- Can not merge with itself
			return false
		end

		local stack = ItemStack(entity.itemstring)
		local name = stack:get_name()
		if own_stack:get_name() ~= name or
				own_stack:get_meta() ~= stack:get_meta() or
				own_stack:get_wear() ~= stack:get_wear() or
				own_stack:get_free_space() == 0 then
			-- Can not merge different or full stack
			return false
		end

		local count = own_stack:get_count()
		local total_count = stack:get_count() + count
		local max_count = stack:get_stack_max()

		if total_count > max_count then
			return false
		end
		-- Merge the remote stack into this one

		local pos = object:get_pos()
		pos.y = pos.y + ((total_count - count) / max_count) * 0.15
		self.object:move_to(pos)

		self.age = 0 -- Handle as new entity
		own_stack:set_count(total_count)
		self:set_item(own_stack)

		entity.itemstring = ""
		object:remove()
		return true
	end,

	enable_physics = function(self)
		if not self.physical_state then
			self.physical_state = true
			self.object:set_properties({physical = true})
			self.object:set_velocity({x=0, y=0, z=0})
			self.object:set_acceleration({x=0, y=-gravity, z=0})
		end
	end,

	disable_physics = function(self)
		if self.physical_state then
			self.physical_state = false
			self.object:set_properties({physical = false})
			self.object:set_velocity({x=0, y=0, z=0})
			self.object:set_acceleration({x=0, y=0, z=0})
		end
	end,

	on_step = function(self, dtime, moveresult)
		self.age = self.age + dtime
		if time_to_live > 0 and self.age > time_to_live then
			self.itemstring = ""
			self.object:remove()
			return
		end

		local pos = self.object:get_pos()
		local node = core.get_node_or_nil({
			x = pos.x,
			y = pos.y + self.object:get_properties().collisionbox[2] - 0.05,
			z = pos.z
		})
		-- Delete in 'ignore' nodes
		if node and node.name == "ignore" then
			self.itemstring = ""
			self.object:remove()
			return
		end

		if self.force_out then
			-- This code runs after the entity got a push from the is_stuck code.
			-- It makes sure the entity is entirely outside the solid node
			local c = self.object:get_properties().collisionbox
			local s = self.force_out_start
			local f = self.force_out
			local ok = (f.x > 0 and pos.x + c[1] > s.x + 0.5) or
				(f.y > 0 and pos.y + c[2] > s.y + 0.5) or
				(f.z > 0 and pos.z + c[3] > s.z + 0.5) or
				(f.x < 0 and pos.x + c[4] < s.x - 0.5) or
				(f.z < 0 and pos.z + c[6] < s.z - 0.5)
			if ok then
				-- Item was successfully forced out
				self.force_out = nil
				self:enable_physics()
				return
			end
		end

		if not self.physical_state then
			return -- Don't do anything
		end

		assert(moveresult,
			"Collision info missing, this is caused by an out-of-date/buggy mod or game")

		if not moveresult.collides then
			-- future TODO: items should probably decelerate in air
			return
		end

		-- Push item out when stuck inside solid node
		local is_stuck = false
		local snode = core.get_node_or_nil(pos)
		if snode then
			local sdef = core.registered_nodes[snode.name] or {}
			is_stuck = (sdef.walkable == nil or sdef.walkable == true)
				and (sdef.collision_box == nil or sdef.collision_box.type == "regular")
				and (sdef.node_box == nil or sdef.node_box.type == "regular")
		end

		if is_stuck then
			local shootdir
			local order = {
				{x=1, y=0, z=0}, {x=-1, y=0, z= 0},
				{x=0, y=0, z=1}, {x= 0, y=0, z=-1},
			}

			-- Check which one of the 4 sides is free
			for o = 1, #order do
				local cnode = core.get_node(vector.add(pos, order[o])).name
				local cdef = core.registered_nodes[cnode] or {}
				if cnode ~= "ignore" and cdef.walkable == false then
					shootdir = order[o]
					break
				end
			end
			-- If none of the 4 sides is free, check upwards
			if not shootdir then
				shootdir = {x=0, y=1, z=0}
				local cnode = core.get_node(vector.add(pos, shootdir)).name
				if cnode == "ignore" then
					shootdir = nil -- Do not push into ignore
				end
			end

			if shootdir then
				-- Set new item moving speed accordingly
				local newv = vector.multiply(shootdir, 3)
				self:disable_physics()
				self.object:set_velocity(newv)

				self.force_out = newv
				self.force_out_start = vector.round(pos)
				return
			end
		end

		node = nil -- ground node we're colliding with
		if moveresult.touching_ground then
			for _, info in ipairs(moveresult.collisions) do
				if info.axis == "y" then
					node = core.get_node(info.node_pos)
					break
				end
			end
		end

		-- Slide on slippery nodes
		local def = node and core.registered_nodes[node.name]
		local keep_movement = false

		if def then
			local slippery = core.get_item_group(node.name, "slippery")
			local vel = self.object:get_velocity()
			if slippery ~= 0 and (math.abs(vel.x) > 0.1 or math.abs(vel.z) > 0.1) then
				-- Horizontal deceleration
				local factor = math.min(4 / (slippery + 4) * dtime, 1)
				self.object:set_velocity({
					x = vel.x * (1 - factor),
					y = 0,
					z = vel.z * (1 - factor)
				})
				keep_movement = true
			end
		end

		if not keep_movement then
			self.object:set_velocity({x=0, y=0, z=0})
		end

		if self.moving_state == keep_movement then
			-- Do not update anything until the moving state changes
			return
		end
		self.moving_state = keep_movement

		-- Only collect items if not moving
		if self.moving_state then
			return
		end
		-- Collect the items around to merge with
		local own_stack = ItemStack(self.itemstring)
		if own_stack:get_free_space() == 0 then
			return
		end
		local objects = core.get_objects_inside_radius(pos, 1.0)
		for k, obj in pairs(objects) do
			local entity = obj:get_luaentity()
			if entity and entity.name == "__builtin:item" then
				if self:try_merge_with(own_stack, obj, entity) then
					own_stack = ItemStack(self.itemstring)
					if own_stack:get_free_space() == 0 then
						return
					end
				end
			end
		end
	end,

	on_punch = function(self, hitter)
		local inv = hitter:get_inventory()
		if inv and self.itemstring ~= "" then
			local left = inv:add_item("main", self.itemstring)
			if left and not left:is_empty() then
				self:set_item(left)
				return
			end
		end
		self.itemstring = ""
		self.object:remove()
	end,
})
void TestUtilities::testStartsWith() { UASSERT(str_starts_with(std::string(), std::string()) == true); UASSERT(str_starts_with(std::string("the sharp pickaxe"), std::string()) == true); UASSERT(str_starts_with(std::string("the sharp pickaxe"), std::string("the")) == true); UASSERT(str_starts_with(std::string("the sharp pickaxe"), std::string("The")) == false); UASSERT(str_starts_with(std::string("the sharp pickaxe"), std::string("The"), true) == true); UASSERT(str_starts_with(std::string("T"), std::string("The")) == false); } void TestUtilities::testStrEqual() { UASSERT(str_equal(utf8_to_wide("abc"), utf8_to_wide("abc"))); UASSERT(str_equal(utf8_to_wide("ABC"), utf8_to_wide("abc"), true)); } void TestUtilities::testStringTrim() { UASSERT(trim(" a") == "a"); UASSERT(trim(" a ") == "a"); UASSERT(trim("a ") == "a"); UASSERT(trim("") == ""); } void TestUtilities::testStrToIntConversion() { UASSERT(mystoi("123", 0, 1000) == 123); UASSERT(mystoi("123", 0, 10) == 10); } void TestUtilities::testStringReplace() { std::string test_str; test_str = "Hello there"; str_replace(test_str, "there", "world"); UASSERT(test_str == "Hello world"); test_str = "ThisAisAaAtest"; str_replace(test_str, 'A', ' '); UASSERT(test_str == "This is a test"); } void TestUtilities::testStringAllowed() { UASSERT(string_allowed("hello", "abcdefghijklmno") == true); UASSERT(string_allowed("123", "abcdefghijklmno") == false); UASSERT(string_allowed_blacklist("hello", "123") == true); UASSERT(string_allowed_blacklist("hello123", "123") == false); } void TestUtilities::testAsciiPrintableHelper() { UASSERT(IS_ASCII_PRINTABLE_CHAR('e') == true); UASSERT(IS_ASCII_PRINTABLE_CHAR('\0') == false); // Ensures that there is no cutting off going on... // If there were, 331 would be cut to 75 in this example // and 73 is a valid ASCII char. int ch = 331; UASSERT(IS_ASCII_PRINTABLE_CHAR(ch) == false); } void TestUtilities::testUTF8() { UASSERT(utf8_to_wide("¤") == L"¤"); UASSERT(wide_to_utf8(L"¤") == "¤"); UASSERTEQ(std::string, wide_to_utf8(utf8_to_wide("")), ""); UASSERTEQ(std::string, wide_to_utf8(utf8_to_wide("the shovel dug a crumbly node!")), "the shovel dug a crumbly node!"); UASSERTEQ(std::string, wide_to_utf8(utf8_to_wide("-ä-")), "-ä-"); UASSERTEQ(std::string, wide_to_utf8(utf8_to_wide("-\xF0\xA0\x80\x8B-")), "-\xF0\xA0\x80\x8B-"); } void TestUtilities::testRemoveEscapes() { UASSERT(unescape_enriched<wchar_t>( L"abc\x1bXdef") == L"abcdef"); UASSERT(unescape_enriched<wchar_t>( L"abc\x1b(escaped)def") == L"abcdef"); UASSERT(unescape_enriched<wchar_t>( L"abc\x1b((escaped with parenthesis\\))def") == L"abcdef"); UASSERT(unescape_enriched<wchar_t>( L"abc\x1b(incomplete") == L"abc"); UASSERT(unescape_enriched<wchar_t>( L"escape at the end\x1b") == L"escape at the end"); // Nested escapes not supported UASSERT(unescape_enriched<wchar_t>( L"abc\x1b(outer \x1b(inner escape)escape)def") == L"abcescape)def"); } void TestUtilities::testWrapRows() { UASSERT(wrap_rows("12345678",4) == "1234\n5678"); // test that wrap_rows doesn't wrap inside multibyte sequences { const unsigned char s[] = { 0x2f, 0x68, 0x6f, 0x6d, 0x65, 0x2f, 0x72, 0x61, 0x70, 0x74, 0x6f, 0x72, 0x2f, 0xd1, 0x82, 0xd0, 0xb5, 0xd1, 0x81, 0xd1, 0x82, 0x2f, 0x6d, 0x69, 0x6e, 0x65, 0x74, 0x65, 0x73, 0x74, 0x2f, 0x62, 0x69, 0x6e, 0x2f, 0x2e, 0x2e, 0}; std::string str((char *)s); UASSERT(utf8_to_wide(wrap_rows(str, 20)) != L"<invalid UTF-8 string>"); }; { const unsigned char s[] = { 0x74, 0x65, 0x73, 0x74, 0x20, 0xd1, 0x82, 0xd0, 0xb5, 0xd1, 0x81, 0xd1, 0x82, 0x20, 0xd1, 0x82, 0xd0, 0xb5, 0xd1, 0x81, 0xd1, 0x82, 0x20, 0xd1, 0x82, 0xd0, 0xb5, 0xd1, 0x81, 0xd1, 0x82, 0}; std::string str((char *)s); UASSERT(utf8_to_wide(wrap_rows(str, 8)) != L"<invalid UTF-8 string>"); } } void TestUtilities::testEnrichedString() { EnrichedString str(L"Test bar"); irr::video::SColor color(0xFF, 0, 0, 0xFF); UASSERT(str.substr(1, 3).getString() == L"est"); str += L" BUZZ"; UASSERT(str.substr(9, std::string::npos).getString() == L"BUZZ"); str.setDefaultColor(color); // Blue foreground UASSERT(str.getColors()[5] == color); // Green background, then white and yellow text str = L"\x1b(b@#0F0)Regular \x1b(c@#FF0)yellow"; UASSERT(str.getColors()[2] == 0xFFFFFFFF); str.setDefaultColor(color); // Blue foreground UASSERT(str.getColors()[13] == 0xFFFFFF00); // Still yellow text UASSERT(str.getBackground() == 0xFF00FF00); // Green background } void TestUtilities::testIsNumber() { UASSERT(is_number("123") == true); UASSERT(is_number("") == false); UASSERT(is_number("123a") == false); } void TestUtilities::testIsPowerOfTwo() { UASSERT(is_power_of_two(0) == false); UASSERT(is_power_of_two(1) == true); UASSERT(is_power_of_two(2) == true); UASSERT(is_power_of_two(3) == false); for (int exponent = 2; exponent <= 31; ++exponent) { UASSERT(is_power_of_two((1U << exponent) - 1) == false); UASSERT(is_power_of_two((1U << exponent)) == true); UASSERT(is_power_of_two((1U << exponent) + 1) == false); } UASSERT(is_power_of_two(U32_MAX) == false); } void TestUtilities::testMyround() { UASSERT(myround(4.6f) == 5); UASSERT(myround(1.2f) == 1); UASSERT(myround(-3.1f) == -3); UASSERT(myround(-6.5f) == -7); } void TestUtilities::testStringJoin() { std::vector<std::string> input; UASSERT(str_join(input, ",") == ""); input.emplace_back("one"); UASSERT(str_join(input, ",") == "one"); input.emplace_back("two"); UASSERT(str_join(input, ",") == "one,two"); input.emplace_back("three"); UASSERT(str_join(input, ",") == "one,two,three"); input[1] = ""; UASSERT(str_join(input, ",") == "one,,three"); input[1] = "two"; UASSERT(str_join(input, " and ") == "one and two and three"); } static bool within(const f32 value1, const f32 value2, const f32 precision) { return std::fabs(value1 - value2) <= precision; } static bool within(const v3f &v1, const v3f &v2, const f32 precision) { return within(v1.X, v2.X, precision) && within(v1.Y, v2.Y, precision) && within(v1.Z, v2.Z, precision); } static bool within(const core::matrix4 &m1, const core::matrix4 &m2, const f32 precision) { const f32 *M1 = m1.pointer(); const f32 *M2 = m2.pointer(); for (int i = 0; i < 16; i++) if (! within(M1[i], M2[i], precision)) return false; return true; } static bool roundTripsDeg(const v3f &v, const f32 precision) { core::matrix4 m; setPitchYawRoll(m, v); return within(v, getPitchYawRoll(m), precision); } void TestUtilities::testEulerConversion() { // This test may fail on non-IEEE systems. // Low tolerance is 4 ulp(1.0) for binary floats with 24 bit mantissa. // (ulp = unit in the last place; ulp(1.0) = 2^-23). const f32 tolL = 4.76837158203125e-7f; // High tolerance is 2 ulp(180.0), needed for numbers in degrees. // ulp(180.0) = 2^-16 const f32 tolH = 3.0517578125e-5f; v3f v1, v2; core::matrix4 m1, m2; const f32 *M1 = m1.pointer(); const f32 *M2 = m2.pointer(); // Check that the radians version and the degrees version // produce the same results. Check also that the conversion // works both ways for these values. v1 = v3f(M_PI/3.0, M_PI/5.0, M_PI/4.0); v2 = v3f(60.0f, 36.0f, 45.0f); setPitchYawRollRad(m1, v1); setPitchYawRoll(m2, v2); UASSERT(within(m1, m2, tolL)); UASSERT(within(getPitchYawRollRad(m1), v1, tolL)); UASSERT(within(getPitchYawRoll(m2), v2, tolH)); // Check the rotation matrix produced. UASSERT(within(M1[0], 0.932004869f, tolL)); UASSERT(within(M1[1], 0.353553385f, tolL)); UASSERT(within(M1[2], 0.0797927827f, tolL)); UASSERT(within(M1[4], -0.21211791f, tolL)); UASSERT(within(M1[5], 0.353553355f, tolL)); UASSERT(within(M1[6], 0.911046684f, tolL)); UASSERT(within(M1[8], 0.293892622f, tolL)); UASSERT(within(M1[9], -0.866025448f, tolL)); UASSERT(within(M1[10], 0.404508471f, tolL)); // Check that the matrix is still homogeneous with no translation UASSERT(M1[3] == 0.0f); UASSERT(M1[7] == 0.0f); UASSERT(M1[11] == 0.0f); UASSERT(M1[12] == 0.0f); UASSERT(M1[13] == 0.0f); UASSERT(M1[14] == 0.0f); UASSERT(M1[15] == 1.0f); UASSERT(M2[3] == 0.0f); UASSERT(M2[7] == 0.0f); UASSERT(M2[11] == 0.0f); UASSERT(M2[12] == 0.0f); UASSERT(M2[13] == 0.0f); UASSERT(M2[14] == 0.0f); UASSERT(M2[15] == 1.0f); // Compare to Irrlicht's results. To be comparable, the // angles must come in a different order and the matrix // elements to compare are different too. m2.setRotationRadians(v3f(v1.Z, v1.X, v1.Y)); UASSERT(within(M1[0], M2[5], tolL)); UASSERT(within(M1[1], M2[6], tolL)); UASSERT(within(M1[2], M2[4], tolL)); UASSERT(within(M1[4], M2[9], tolL)); UASSERT(within(M1[5], M2[10], tolL)); UASSERT(within(M1[6], M2[8], tolL)); UASSERT(within(M1[8], M2[1], tolL)); UASSERT(within(M1[9], M2[2], tolL)); UASSERT(within(M1[10], M2[0], tolL)); // Check that Eulers that produce near gimbal-lock still round-trip UASSERT(roundTripsDeg(v3f(89.9999f, 17.f, 0.f), tolH)); UASSERT(roundTripsDeg(v3f(89.9999f, 0.f, 19.f), tolH)); UASSERT(roundTripsDeg(v3f(89.9999f, 17.f, 19.f), tolH)); // Check that Eulers at an angle > 90 degrees may not round-trip... v1 = v3f(90.00001f, 1.f, 1.f); setPitchYawRoll(m1, v1); v2 = getPitchYawRoll(m1); //UASSERT(within(v1, v2, tolL)); // this is typically false // ... however the rotation matrix is the same for both setPitchYawRoll(m2, v2); UASSERT(within(m1, m2, tolL)); } void TestUtilities::testBase64() { // Test character set UASSERT(base64_is_valid("ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789+/") == true); UASSERT(base64_is_valid("/+9876543210" "zyxwvutsrqponmlkjihgfedcba" "ZYXWVUTSRQPONMLKJIHGFEDCBA") == true); UASSERT(base64_is_valid("ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789+.") == false); UASSERT(base64_is_valid("ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789 /") == false); // Test empty string UASSERT(base64_is_valid("") == true); // Test different lengths, with and without padding, // with correct and incorrect padding UASSERT(base64_is_valid("A") == false); UASSERT(base64_is_valid("AA") == true); UASSERT(base64_is_valid("AAA") == true); UASSERT(base64_is_valid("AAAA") == true); UASSERT(base64_is_valid("AAAAA") == false); UASSERT(base64_is_valid("AAAAAA") == true); UASSERT(base64_is_valid("AAAAAAA") == true); UASSERT(base64_is_valid("AAAAAAAA") == true); UASSERT(base64_is_valid("A===") == false); UASSERT(base64_is_valid("AA==") == true); UASSERT(base64_is_valid("AAA=") == true); UASSERT(base64_is_valid("AAAA") == true); UASSERT(base64_is_valid("AAAA====") == false); UASSERT(base64_is_valid("AAAAA===") == false); UASSERT(base64_is_valid("AAAAAA==") == true); UASSERT(base64_is_valid("AAAAAAA=") == true); UASSERT(base64_is_valid("AAAAAAA==") == false); UASSERT(base64_is_valid("AAAAAAA===") == false); UASSERT(base64_is_valid("AAAAAAA====") == false); UASSERT(base64_is_valid("AAAAAAAA") == true); UASSERT(base64_is_valid("AAAAAAAA=") == false); UASSERT(base64_is_valid("AAAAAAAA==") == false); UASSERT(base64_is_valid("AAAAAAAA===") == false); UASSERT(base64_is_valid("AAAAAAAA====") == false); // Test if canonical encoding // Last character limitations, length % 4 == 3 UASSERT(base64_is_valid("AAB") == false); UASSERT(base64_is_valid("AAE") == true); UASSERT(base64_is_valid("AAQ") == true); UASSERT(base64_is_valid("AAB=") == false); UASSERT(base64_is_valid("AAE=") == true); UASSERT(base64_is_valid("AAQ=") == true); UASSERT(base64_is_valid("AAAAAAB=") == false); UASSERT(base64_is_valid("AAAAAAE=") == true); UASSERT(base64_is_valid("AAAAAAQ=") == true); // Last character limitations, length % 4 == 2 UASSERT(base64_is_valid("AB") == false); UASSERT(base64_is_valid("AE") == false); UASSERT(base64_is_valid("AQ") == true); UASSERT(base64_is_valid("AB==") == false); UASSERT(base64_is_valid("AE==") == false); UASSERT(base64_is_valid("AQ==") == true); UASSERT(base64_is_valid("AAAAAB==") == false); UASSERT(base64_is_valid("AAAAAE==") == false); UASSERT(base64_is_valid("AAAAAQ==") == true); // Extraneous character present UASSERT(base64_is_valid(".") == false); UASSERT(base64_is_valid("A.") == false); UASSERT(base64_is_valid("AA.") == false); UASSERT(base64_is_valid("AAA.") == false); UASSERT(base64_is_valid("AAAA.") == false); UASSERT(base64_is_valid("AAAAA.") == false); UASSERT(base64_is_valid("A.A") == false); UASSERT(base64_is_valid("AA.A") == false); UASSERT(base64_is_valid("AAA.A") == false); UASSERT(base64_is_valid("AAAA.A") == false); UASSERT(base64_is_valid("AAAAA.A") == false); UASSERT(base64_is_valid("\xE1""AAA") == false); // Padding in wrong position UASSERT(base64_is_valid("A=A") == false); UASSERT(base64_is_valid("AA=A") == false); UASSERT(base64_is_valid("AAA=A") == false); UASSERT(base64_is_valid("AAAA=A") == false); UASSERT(base64_is_valid("AAAAA=A") == false); }