/* * smb_test.cpp * Host test harness for the SMB client library (programs/lib/smb) * Copyright (c) 2026 Daniel Hammer */ // Builds the library for the host and runs it over a POSIX socket. // // smb_test vectors crypto / NTLM known-answer tests // smb_test live HOST PORT USER PASS SHARE end-to-end file operations // // tests/smb/run.sh starts a throwaway impacket SMB server and runs both. #include #include "../../programs/lib/smb/internal.hpp" #include #include #include #include #include #include #include #include #include #include #include #include using namespace smb; using namespace smb::detail; static int g_failures = 0; static void check(bool ok, const char* what) { std::printf(" %s %s\n", ok ? "PASS" : "FAIL", what); if (!ok) g_failures++; } static std::vector unhex(const char* s) { std::vector out; while (s[0] && s[1]) { unsigned v; std::sscanf(s, "%2x", &v); out.push_back((uint8_t)v); s += 2; } return out; } static bool eq(const uint8_t* got, const char* hex, size_t n) { auto want = unhex(hex); return want.size() == n && std::memcmp(got, want.data(), n) == 0; } // ---- Known-answer tests ---- static void run_vectors() { std::printf("crypto vectors\n"); uint8_t out[32]; // RFC 1320 md4((const uint8_t*)"", 0, out); check(eq(out, "31d6cfe0d16ae931b73c59d7e0c089c0", 16), "MD4 empty"); md4((const uint8_t*)"abc", 3, out); check(eq(out, "a448017aaf21d8525fc10ae87aa6729d", 16), "MD4 abc"); const char* longMsg = "12345678901234567890123456789012345678901234567890123456789012345678901234567890"; md4((const uint8_t*)longMsg, 80, out); check(eq(out, "e33b4ddc9c38f2199c3e7b164fcc0536", 16), "MD4 80 bytes"); // RFC 2202 HMAC-MD5 case 2, RFC 4231 HMAC-SHA256 case 2 hmac_md5((const uint8_t*)"Jefe", 4, (const uint8_t*)"what do ya want ", 16, (const uint8_t*)"for nothing?", 12, out); check(eq(out, "750c783e6ab0b503eaa86e310a5db738", 16), "HMAC-MD5"); hmac_sha256((const uint8_t*)"Jefe", 4, (const uint8_t*)"what do ya want for nothing?", 28, nullptr, 0, out); check(eq(out, "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843", 32), "HMAC-SHA256"); // RFC 4493 AES-CMAC auto key = unhex("2b7e151628aed2a6abf7158809cf4f3c"); auto m = unhex("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e51" "30c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710"); aes_cmac(key.data(), m.data(), 0, out); check(eq(out, "bb1d6929e95937287fa37d129b756746", 16), "AES-CMAC len 0"); aes_cmac(key.data(), m.data(), 16, out); check(eq(out, "070a16b46b4d4144f79bdd9dd04a287c", 16), "AES-CMAC len 16"); aes_cmac(key.data(), m.data(), 40, out); check(eq(out, "dfa66747de9ae63030ca32611497c827", 16), "AES-CMAC len 40"); aes_cmac(key.data(), m.data(), 64, out); check(eq(out, "51f0bebf7e3b9d92fc49741779363cfe", 16), "AES-CMAC len 64"); // SMB 3.0 signing key derivation; reference computed with Python's hmac // (SP800-108 counter mode over HMAC-SHA256). { uint8_t k[16]; for (int i = 0; i < 16; i++) k[i] = (uint8_t)i; smb3_kdf(k, "SMB2AESCMAC", 12, "SmbSign", 8, out); check(eq(out, "6234814cbb8ea9227440ebfeb5eacbe1", 16), "SMB3 signing key KDF"); } // MS-NLMP 4.2.4 (NTLMv2): user "User", domain "Domain", password "Password". std::printf("NTLMv2 (MS-NLMP 4.2.4)\n"); { // CHALLENGE_MESSAGE from 4.2.4.3 auto challenge = unhex( "4e544c4d53535000020000000c000c003800000033828ae20123456789abcdef" "00000000000000002400240044000000060070170000000f5300650072007600" "6500720002000c0044006f006d00610069006e0001000c005300650072007600" "6500720000000000"); NtlmAuthParams p; p.user = "User"; p.password = "Password"; p.domain = "Domain"; p.anonymous = false; std::memset(p.clientChallenge, 0xaa, 8); p.fallbackTime = 0; uint8_t msg[1024]; uint8_t sessionKey[16]; int n = ntlm_authenticate(challenge.data(), challenge.size(), p, msg, sizeof(msg), sessionKey); check(n > 0, "authenticate message built"); if (n > 0) { uint16_t ntLen = get16(msg + 20); uint32_t ntOff = get32(msg + 24); check(ntLen >= 16 && eq(msg + ntOff, "68cd0ab851e51c96aabc927bebef6a1c", 16), "NTProofStr"); check(eq(sessionKey, "8de40ccadbc14a82f15cb0ad0de95ca3", 16), "SessionBaseKey"); uint32_t lmOff = get32(msg + 16); check(eq(msg + lmOff, "86c35097ac9cec102554764a57cccc19aaaaaaaaaaaaaaaa", 24), "LMv2 response"); } } std::printf("SPNEGO\n"); { uint8_t tok[NTLM_NEGOTIATE_LEN]; ntlm_negotiate(tok); uint8_t blob[256]; int n = spnego_wrap(tok, sizeof(tok), true, blob, sizeof(blob)); check(n > 0 && blob[0] == 0x60 && (size_t)blob[1] + 2 == (size_t)n, "NegTokenInit framing"); n = spnego_wrap(tok, sizeof(tok), false, blob, sizeof(blob)); const uint8_t* inner = nullptr; size_t innerLen = 0; check(n > 0 && spnego_unwrap(blob, (size_t)n, &inner, &innerLen) && innerLen == sizeof(tok) && std::memcmp(inner, tok, sizeof(tok)) == 0, "NegTokenResp round trip"); } std::printf("text\n"); { uint8_t w[64]; int n = utf8_to_utf16("/a//b\xC3\xA9/\xF0\x9F\x98\x80/", w, sizeof(w), true); char back[64]; utf16_to_utf8(w, (uint32_t)n, back, sizeof(back)); check(n == 14 && std::strcmp(back, "a\\b\xC3\xA9\\\xF0\x9F\x98\x80") == 0, "path UTF-8 <-> UTF-16"); Url u; check(parse_url("smb://WORK;alice@nas.local:4455/media/films/2020/", &u) && !std::strcmp(u.host, "nas.local") && u.port == 4455 && !std::strcmp(u.share, "media") && !std::strcmp(u.path, "films/2020") && !std::strcmp(u.user, "alice") && !std::strcmp(u.domain, "WORK"), "parse_url full"); check(parse_url("\\\\nas\\share", &u) && !std::strcmp(u.host, "nas") && !std::strcmp(u.share, "share") && u.path[0] == 0 && u.port == 0, "parse_url UNC"); check(!parse_url("smb://", &u), "parse_url empty"); } } // ---- POSIX transport ---- static int posix_send(void* ctx, const void* data, uint32_t len) { int fd = *(int*)ctx; const char* p = (const char*)data; while (len > 0) { ssize_t r = ::send(fd, p, len, MSG_NOSIGNAL); if (r <= 0) return -1; p += r; len -= (uint32_t)r; } return 0; } static int posix_recv(void* ctx, void* buf, uint32_t len, uint32_t timeoutMs) { int fd = *(int*)ctx; pollfd pfd{fd, POLLIN, 0}; int pr = ::poll(&pfd, 1, (int)timeoutMs); if (pr == 0) return 0; if (pr < 0) return -1; ssize_t r = ::recv(fd, buf, len, 0); return r > 0 ? (int)r : -1; } static void posix_random(void*, void* buf, uint32_t len) { FILE* f = std::fopen("/dev/urandom", "rb"); if (f == nullptr || std::fread(buf, 1, len, f) != len) std::abort(); std::fclose(f); } static int dial(const char* host, int port) { addrinfo hints{}; hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; addrinfo* res = nullptr; char portStr[16]; std::snprintf(portStr, sizeof(portStr), "%d", port); if (getaddrinfo(host, portStr, &hints, &res) != 0) return -1; int fd = ::socket(AF_INET, SOCK_STREAM, 0); if (fd >= 0 && ::connect(fd, res->ai_addr, res->ai_addrlen) != 0) { ::close(fd); fd = -1; } freeaddrinfo(res); return fd; } // ---- Live test ---- struct Listing { std::vector entries; }; static bool collect(void* ctx, const DirEntry& e) { ((Listing*)ctx)->entries.push_back(e); return true; } static const DirEntry* find(const Listing& l, const char* name) { for (auto& e : l.entries) if (!std::strcmp(e.name, name)) return &e; return nullptr; } static int run_live(const char* host, int port, const char* user, const char* pass, const char* share) { std::printf("live test against %s:%d share %s as '%s'\n", host, port, share, user); int fd = dial(host, port); if (fd < 0) { std::printf(" cannot connect\n"); return 1; } Io io{&fd, posix_send, posix_recv, posix_random}; // Wrong password must be refused before anything else is tried. if (user[0]) { int fd2 = dial(host, port); Io io2{&fd2, posix_send, posix_recv, posix_random}; Client bad; Credentials wrong{user, "definitely-not-the-password", ""}; int r = connect(&bad, io2, host, wrong); check(r == ERR_AUTH, "wrong password -> ERR_AUTH"); disconnect(&bad); ::close(fd2); } Client c; Credentials creds{user, pass, ""}; int r = connect(&c, io, host, creds); std::printf(" connect: %d (%s), dialect 0x%04x, signing %d, guest %d\n", r, status_text(r), c.dialect, c.signing, c.guest); check(r == OK, "connect + logon"); if (r != OK) { disconnect(&c); return 1; } check(echo(&c) == OK, "echo"); ShareInfo shares[32]; int ns = list_shares(&c, shares, 32); std::printf(" shares: %d\n", ns); bool sawShare = false; for (int i = 0; i < ns; i++) { std::printf(" %-16s type %u%s %s\n", shares[i].name, shares[i].type, shares[i].hidden ? " (hidden)" : "", shares[i].comment); if (!strcasecmp(shares[i].name, share)) sawShare = true; } check(ns > 0 && sawShare, "list_shares finds the test share"); uint32_t tree = 0; check(tree_connect(&c, "no-such-share-xyz", &tree) == ERR_BAD_SHARE, "bad share -> ERR_BAD_SHARE"); r = tree_connect(&c, share, &tree); check(r == OK, "tree_connect"); if (r != OK) { disconnect(&c); return 1; } FileInfo info; check(stat(&c, tree, "", &info) == OK && info.isDir, "stat share root"); check(stat(&c, tree, "does/not/exist", &info) == ERR_NOT_FOUND, "stat missing -> ERR_NOT_FOUND"); // Directory with a non-ASCII name. const char* dir = "t\xC3\xA9st dir"; remove(&c, tree, "t\xC3\xA9st dir/big.bin"); remove(&c, tree, "t\xC3\xA9st dir/renamed.txt"); remove(&c, tree, "t\xC3\xA9st dir/hello.txt"); remove(&c, tree, dir); check(mkdir(&c, tree, dir) == OK, "mkdir"); check(mkdir(&c, tree, dir) == ERR_EXISTS, "mkdir again -> ERR_EXISTS"); check(stat(&c, tree, dir, &info) == OK && info.isDir, "stat new directory"); // Small file. std::string path = std::string(dir) + "/hello.txt"; FileId id; check(open(&c, tree, path.c_str(), OPEN_READ, &id) == ERR_NOT_FOUND, "open missing -> ERR_NOT_FOUND"); r = open(&c, tree, path.c_str(), OPEN_CREATE, &id); check(r == OK, "create file"); const char* text = "hello from MontaukOS\n"; check(write(&c, tree, id, 0, text, (uint32_t)std::strlen(text)) == (int)std::strlen(text), "write"); uint64_t size = 0; check(file_size(&c, tree, id, &size) == OK && size == std::strlen(text), "file_size"); check(close(&c, tree, id) == OK, "close"); check(open(&c, tree, path.c_str(), OPEN_CREATE_NEW, &id) == ERR_EXISTS, "create new on existing -> ERR_EXISTS"); char rbuf[128] = {}; r = open(&c, tree, path.c_str(), OPEN_READ, &id, &info); check(r == OK && info.size == std::strlen(text) && !info.isDir, "open for read + info"); int n = read(&c, tree, id, 0, rbuf, sizeof(rbuf)); check(n == (int)std::strlen(text) && !std::memcmp(rbuf, text, (size_t)n), "read back"); check(read(&c, tree, id, 1000, rbuf, sizeof(rbuf)) == 0, "read past EOF -> 0"); n = read(&c, tree, id, 6, rbuf, 4); check(n == 4 && !std::memcmp(rbuf, "from", 4), "read at offset"); close(&c, tree, id); // Large file crossing several MAX_IO chunks. std::vector big(300000); for (size_t i = 0; i < big.size(); i++) big[i] = (uint8_t)((i * 2654435761u) >> 13); std::string bigPath = std::string(dir) + "/big.bin"; r = open(&c, tree, bigPath.c_str(), OPEN_CREATE, &id); bool ok = r == OK; for (size_t off = 0; ok && off < big.size();) { int w = write(&c, tree, id, off, big.data() + off, (uint32_t)(big.size() - off)); if (w <= 0) { ok = false; break; } off += (size_t)w; } close(&c, tree, id); check(ok, "write 300000 bytes"); std::vector back(big.size()); r = open(&c, tree, bigPath.c_str(), OPEN_READWRITE, &id, &info); ok = r == OK && info.size == big.size(); for (size_t off = 0; ok && off < back.size();) { int g = read(&c, tree, id, off, back.data() + off, (uint32_t)(back.size() - off)); if (g <= 0) { ok = false; break; } off += (size_t)g; } close(&c, tree, id); check(ok && back == big, "read 300000 bytes back intact"); // Rename, times, listing. std::string renamed = std::string(dir) + "/renamed.txt"; check(rename(&c, tree, path.c_str(), renamed.c_str()) == OK, "rename"); check(stat(&c, tree, path.c_str(), &info) == ERR_NOT_FOUND, "old name gone"); check(rename(&c, tree, bigPath.c_str(), renamed.c_str()) == ERR_EXISTS, "rename onto existing -> ERR_EXISTS"); r = set_times(&c, tree, renamed.c_str(), 0, 1000000000); check(r == OK, "set_times"); // impacket's server accepts FileBasicInformation but does not apply it. if (stat(&c, tree, renamed.c_str(), &info) == OK && info.mtime != 1000000000) std::printf(" NOTE server ignored set_times (mtime %lld)\n", (long long)info.mtime); Listing l; n = list_dir(&c, tree, dir, collect, &l); check(n == 2 && find(l, "renamed.txt") && find(l, "big.bin"), "list_dir"); const DirEntry* be = find(l, "big.bin"); check(be && be->info.size == big.size() && !be->info.isDir, "list_dir sizes"); Listing root; n = list_dir(&c, tree, "/", collect, &root); const DirEntry* de = find(root, dir); check(n >= 1 && de && de->info.isDir, "root listing shows the directory (UTF-8 name)"); r = list_dir(&c, tree, renamed.c_str(), collect, &l); check(r == ERR_NOT_DIR || r == ERR_INVALID, "list_dir on a file is refused"); // Many entries: the listing must span several QUERY_DIRECTORY replies. std::string many = std::string(dir) + "/many"; mkdir(&c, tree, many.c_str()); ok = true; for (int i = 0; i < 300 && ok; i++) { char name[160]; std::snprintf(name, sizeof(name), "%s/a-file-with-a-fairly-long-name-to-fill-the-reply-%04d.dat", many.c_str(), i); ok = open(&c, tree, name, OPEN_CREATE, &id) == OK && close(&c, tree, id) == OK; } Listing ml; n = list_dir(&c, tree, many.c_str(), collect, &ml); check(ok && n == 300, "list_dir with 300 entries"); for (int i = 0; i < 300; i++) { char name[160]; std::snprintf(name, sizeof(name), "%s/a-file-with-a-fairly-long-name-to-fill-the-reply-%04d.dat", many.c_str(), i); remove(&c, tree, name); } check(remove(&c, tree, many.c_str()) == OK, "remove emptied directory"); // Removal. check(remove(&c, tree, dir) == ERR_NOT_EMPTY, "remove non-empty dir -> ERR_NOT_EMPTY"); check(remove(&c, tree, renamed.c_str()) == OK, "remove file"); check(remove(&c, tree, bigPath.c_str()) == OK, "remove big file"); check(remove(&c, tree, dir) == OK, "remove directory"); check(stat(&c, tree, dir, &info) == ERR_NOT_FOUND, "directory gone"); check(tree_disconnect(&c, tree) == OK, "tree_disconnect"); disconnect(&c); ::close(fd); return 0; } int main(int argc, char** argv) { if (argc >= 2 && !std::strcmp(argv[1], "vectors")) { run_vectors(); } else if (argc >= 7 && !std::strcmp(argv[1], "live")) { run_live(argv[2], std::atoi(argv[3]), argv[4], argv[5], argv[6]); } else { std::fprintf(stderr, "usage: smb_test vectors | live HOST PORT USER PASS SHARE\n"); return 2; } std::printf("%s (%d failure%s)\n", g_failures ? "FAILED" : "ALL PASS", g_failures, g_failures == 1 ? "" : "s"); return g_failures ? 1 : 0; }