ROOTPLOIT
Server: LiteSpeed
System: Linux in-mum-web1878.main-hosting.eu 5.14.0-570.21.1.el9_6.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Jun 11 07:22:35 EDT 2025 x86_64
User: u435929562 (435929562)
PHP: 7.4.33
Disabled: system, exec, shell_exec, passthru, mysql_list_dbs, ini_alter, dl, symlink, link, chgrp, leak, popen, apache_child_terminate, virtual, mb_send_mail
Upload Files
File: //proc/self/root/opt/go/pkg/mod/go.mongodb.org/[email protected]/mongo/bson_helpers_test.go
// Copyright (C) MongoDB, Inc. 2017-present.
//
// 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

package mongo

import (
	"testing"

	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/internal/assert"
)

// compare expected and actual BSON documents. comparison succeeds if actual contains each element in expected.
func compareDocuments(t *testing.T, expected, actual bson.Raw) {
	t.Helper()

	eElems, err := expected.Elements()
	assert.Nil(t, err, "error getting expected elements: %v", err)

	for _, e := range eElems {
		eKey := e.Key()
		aVal, err := actual.LookupErr(eKey)
		assert.Nil(t, err, "key %s not found in result", e.Key())
		compareBsonValues(t, eKey, e.Value(), aVal)
	}
}

func numberFromValue(t *testing.T, val bson.RawValue) int64 {
	switch val.Type {
	case bson.TypeInt32:
		return int64(val.Int32())
	case bson.TypeInt64:
		return val.Int64()
	case bson.TypeDouble:
		return int64(val.Double())
	default:
		t.Fatalf("unexpected type for number: %v", val.Type)
	}

	return 0
}

func compareNumberValues(t *testing.T, key string, expected, actual bson.RawValue) {
	eInt := numberFromValue(t, expected)
	aInt := numberFromValue(t, actual)
	assert.Equal(t, eInt, aInt, "value mismatch for key %s; expected %v, got %v", key, expected, actual)
}

// compare BSON values and fail if they are not equal. the key parameter is used for error strings.
// if the expected value is a numeric type (int32, int64, or double) and the value is 42, the function only asserts that
// the actual value is non-null.
func compareBsonValues(t *testing.T, key string, expected, actual bson.RawValue) {
	t.Helper()

	switch expected.Type {
	case bson.TypeInt32, bson.TypeInt64, bson.TypeDouble:
		compareNumberValues(t, key, expected, actual)
	case bson.TypeEmbeddedDocument:
		compareDocuments(t, expected.Document(), actual.Document())
	case bson.TypeArray:
		compareDocuments(t, expected.Array(), actual.Array())
	default:
		assert.Equal(t, expected.Value, actual.Value,
			"value mismatch for key %v; expected %v, got %v", key, expected.Value, actual.Value)
	}
}