Make username lookup case-insensitive
finger [email protected] (or any mixed-case name) failed because the plan path was built from the raw username while plan files are lower-case on disk. Lower-case the requested name before resolving the plan path; the original spelling is still echoed back when no plan file exists. Add a mock test asserting Pete -> .../pete.
This commit is contained in:
+11
-1
@@ -1,4 +1,6 @@
|
|||||||
#include "handler.hpp"
|
#include "handler.hpp"
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cctype>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
@@ -65,8 +67,16 @@ std::string process(const std::string &username, const IFilesystemWrapper &fs,
|
|||||||
return std::string("InvalidInput: ") + e.what() + std::string("\r\n");
|
return std::string("InvalidInput: ") + e.what() + std::string("\r\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Plan-file lookup is case-insensitive: normalise the requested name to
|
||||||
|
// lower-case so e.g. "Pete" resolves the on-disk "pete" plan. Plan filenames
|
||||||
|
// are always lower-case; the original spelling is still echoed back below
|
||||||
|
// when no plan exists.
|
||||||
|
std::string lookup = username;
|
||||||
|
std::transform(lookup.begin(), lookup.end(), lookup.begin(),
|
||||||
|
[](unsigned char c) { return std::tolower(c); });
|
||||||
|
|
||||||
// Attempt to open the plan file (if any) and return the contents as a string
|
// Attempt to open the plan file (if any) and return the contents as a string
|
||||||
std::filesystem::path planPath = basepath / username;
|
std::filesystem::path planPath = basepath / lookup;
|
||||||
|
|
||||||
// Check if the plan file exists using the filesystem wrapper
|
// Check if the plan file exists using the filesystem wrapper
|
||||||
if (!fs.exists(planPath)) {
|
if (!fs.exists(planPath)) {
|
||||||
|
|||||||
@@ -64,6 +64,21 @@ TEST_F(ProcessMockTest, ProcessWithEmptyFile) {
|
|||||||
EXPECT_EQ(result, "emptyfileuser");
|
EXPECT_EQ(result, "emptyfileuser");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Username lookup is case-insensitive: a mixed-case request is lowercased
|
||||||
|
// before the plan-file path is built, so "Pete" reads .../pete.
|
||||||
|
TEST_F(ProcessMockTest, ProcessLowercasesUsernameForLookup) {
|
||||||
|
using ::testing::Return;
|
||||||
|
const std::filesystem::path base{"/var/finger/users/"};
|
||||||
|
|
||||||
|
EXPECT_CALL(*mock_filesystem, exists(base / "pete"))
|
||||||
|
.WillOnce(Return(true));
|
||||||
|
EXPECT_CALL(*mock_filesystem, read_file(base / "pete"))
|
||||||
|
.WillOnce(Return("Just another hacker.\r\n"));
|
||||||
|
|
||||||
|
std::string result = process("Pete", *mock_filesystem, base);
|
||||||
|
EXPECT_EQ(result, "Just another hacker.\r\n");
|
||||||
|
}
|
||||||
|
|
||||||
// Test showing multiple expectations
|
// Test showing multiple expectations
|
||||||
TEST_F(ProcessMockTest, MultipleFileOperations) {
|
TEST_F(ProcessMockTest, MultipleFileOperations) {
|
||||||
using ::testing::_;
|
using ::testing::_;
|
||||||
|
|||||||
Reference in New Issue
Block a user