Files
mfc/tests/atlutiltest.cpp
Claude 18d34fba15 Read and write animated GIFs through GDI+
GDI+ now decodes every frame of a GIF, exposes them along
FrameDimensionTime with GdipImageGetFrameCount and
GdipImageSelectActiveFrame, and reports each frame's delay and the
loop count as PropertyTagFrameDelay and PropertyTagLoopCount. Saving
with EncoderSaveFlag set to EncoderValueMultiFrame starts a session
that GdipSaveAdd and GdipSaveAddImage extend and EncoderValueFlush
ends; a multi-frame image saved whole is written animated. Each frame
gets its own color table and graphic control extension, with a
NETSCAPE2.0 loop block. Property items can be read, set and removed.

atlutiltest saves, reloads and resaves a three-frame GIF.

Signed-off-by: Claude <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B82eRRw4kkFHrrHRhkcuLw
2026-11-27 18:29:23 +00:00

2082 lines
80 KiB
C++

// atlutiltest.cpp - ATL's utility headers, in a program without MFC:
// - CString from atlstr.h (formatting, resources, conversions, errors);
// - the collections: CSimpleArray and CSimpleMap, CAtlArray, CAtlList,
// CAtlMap, CRBMap and CRBMultiMap, and the arrays and lists of
// CAutoPtr and interface pointers;
// - CHeapPtr, CComHeapPtr, CTempBuffer, CAutoPtr, CAutoVectorPtr,
// CHandle, AtlAdd and AtlMultiply;
// - atlmem.h's heaps, CAtlStringMgr, CFixedStringT,
// AtlGetStringResourceImage and atlcur.h's CComCurrency;
// - AtlThrow and CAtlException;
// - CComSafeArray, one- and two-dimensional;
// - CComDispatchDriver over an IDispatch of the test's own;
// - CAtlFile, CAtlTemporaryFile and CAtlFileMapping;
// - CPath from atlpath.h;
// - atlsecurity.h: CSid, CDacl, CSecurityDesc, CSecurityAttributes,
// AtlGetDacl/AtlSetDacl and CAccessToken;
// - atltime.h, atlcomtime.h and atlimage.h: CTime, CFileTime,
// COleDateTime and CImage;
// - atlsync.h, atlenc.h, atlcrypt.h, atlsocket.h, atlrx.h and atlutil.h:
// the synchronization objects, the encodings, CryptoAPI hashes and
// keys, CSocketAddr, CAtlRegExp, CUrl and the URL helpers,
// CWorkerThread and CThreadPool;
// - CAtlServiceModuleT: /Service, the service started by the service
// control manager and stopped, and /UnregServer. The service is this
// program again, started with -AsService.
// Writes atlutiltest.log in MFC_TEST_OUT; exits with 1 on a failure.
// Winsock 2 comes before windows.h.
#include <atlsocket.h>
#include <atlbase.h>
#include <atlstr.h>
#include <atlcoll.h>
#include <atlsafe.h>
#include <atlfile.h>
#include <atlpath.h>
#include <atlsecurity.h>
#include <atltime.h>
#include <atlimage.h>
#include <atlcomtime.h>
#include <atlsync.h>
#include <atlenc.h>
#include <atlcrypt.h>
#include <atlrx.h>
#include <atlutil.h>
#include <atlmem.h>
#include <atlcur.h>
#include <stdio.h>
#include <stdarg.h>
#include "atlutiltest.h"
static int g_nFailures;
static FILE* g_pLog;
static void Log(const char* fmt, ...) {
va_list args;
va_start(args, fmt);
if (g_pLog != NULL) {
vfprintf(g_pLog, fmt, args);
fputc('\n', g_pLog);
fflush(g_pLog);
}
va_end(args);
}
#define CHECK(c) \
do { \
if (!(c)) { \
Log("FAIL %s:%d: %s", __FILE__, __LINE__, #c); \
g_nFailures++; \
} \
} while (0)
// ---------------------------------------------------------------------
// The module: a service.
// Where the service tells the test what it did.
#define STATE_KEY TEXT("Software\\clang-retro\\AtlUtilTest")
static void SetState(LPCTSTR pszState) {
CRegKey key;
if (key.Create(HKEY_LOCAL_MACHINE, STATE_KEY) == ERROR_SUCCESS)
key.SetStringValue(TEXT("State"), pszState);
}
static CString GetState() {
CRegKey key;
TCHAR sz[64] = TEXT("");
ULONG n = 64;
if (key.Open(HKEY_LOCAL_MACHINE, STATE_KEY, KEY_READ) == ERROR_SUCCESS)
key.QueryStringValue(TEXT("State"), sz, &n);
return sz;
}
class CTestModule : public CAtlServiceModuleT<CTestModule, IDS_SERVICENAME> {
public:
static LPCOLESTR GetAppId() { return OLESTR("{3C1D7E52-8F4B-4A6D-9E21-5B7C0D9A1F64}"); }
static HRESULT WINAPI UpdateRegistryAppId(BOOL bRegister) {
CRegKey key;
if (bRegister) {
LONG l = key.Create(HKEY_CLASSES_ROOT, TEXT("AppID\\{3C1D7E52-8F4B-4A6D-9E21-5B7C0D9A1F64}"));
if (l == ERROR_SUCCESS) l = key.SetStringValue(NULL, TEXT("AtlUtilTest"));
return l == ERROR_SUCCESS ? S_OK : HRESULT_FROM_WIN32(l);
}
if (key.Open(HKEY_CLASSES_ROOT, TEXT("AppID"), KEY_WRITE) == ERROR_SUCCESS)
key.RecurseDeleteKey(TEXT("{3C1D7E52-8F4B-4A6D-9E21-5B7C0D9A1F64}"));
return S_OK;
}
// No type library to register.
HRESULT RegisterServer(BOOL = FALSE, const CLSID* pCLSID = NULL) {
return CAtlServiceModuleT<CTestModule, IDS_SERVICENAME>::RegisterServer(FALSE, pCLSID);
}
HRESULT UnregisterServer(BOOL = FALSE, const CLSID* pCLSID = NULL) {
return CAtlServiceModuleT<CTestModule, IDS_SERVICENAME>::UnregisterServer(FALSE, pCLSID);
}
HRESULT PreMessageLoop(int nShowCmd) {
HRESULT hr = CAtlServiceModuleT<CTestModule, IDS_SERVICENAME>::PreMessageLoop(nShowCmd);
SetState(m_bService ? TEXT("running") : TEXT("local"));
return hr;
}
void OnStop() {
SetState(TEXT("stopping"));
CAtlServiceModuleT<CTestModule, IDS_SERVICENAME>::OnStop();
}
};
CTestModule _AtlModule;
// ---------------------------------------------------------------------
// Strings
static void TestStrings() {
CString s = _T("abc");
CHECK(s.GetLength() == 3 && s == _T("abc"));
s.Format(_T("%d-%s-%5.2f-%%"), 42, _T("x"), 3.14159);
CHECK(s == _T("42-x- 3.14-%"));
CStringA a(s);
CHECK(a == "42-x- 3.14-%");
CStringW w = L"wide";
CStringA b = w;
CHECK(b == "wide");
CStringA c;
c.Format("%S and %s", L"other", "same");
CHECK(c == "other and same");
// Resources, through _AtlBaseModule.
CString h;
CHECK(h.LoadString(IDS_HELLO) && h == _T("Hello, %s"));
CString f;
f.Format(IDS_HELLO, _T("ATL"));
CHECK(f == _T("Hello, ATL"));
CString l((LPCTSTR)MAKEINTRESOURCE(IDS_LONG));
CHECK(l.GetLength() > 256 && l.Right(3) == _T("end"));
// Editing.
CString e = _T("one two three");
CHECK(e.Find(_T("two")) == 4 && e.Mid(4, 3) == _T("two"));
CHECK(e.Replace(_T("t"), _T("T")) == 2 && e == _T("one Two Three"));
e.MakeUpper();
CHECK(e == _T("ONE TWO THREE"));
int iStart = 0;
CHECK(e.Tokenize(_T(" "), iStart) == _T("ONE"));
// Copies share a buffer until one changes.
CString s1 = _T("shared");
CString s2 = s1;
CHECK((LPCTSTR)s1 == (LPCTSTR)s2);
s2 += _T("!");
CHECK(s1 == _T("shared") && s2 == _T("shared!"));
// BSTRs.
BSTR bstr = a.AllocSysString();
CHECK(bstr != NULL && ::SysStringLen(bstr) == 12 && bstr[0] == L'4');
::SysFreeString(bstr);
// ATL's names for it.
CAtlString at = _T("atl");
CAtlStringW atw(at);
CHECK(atw == L"atl");
// Errors as CAtlException.
bool bCaught = false;
try {
(void)s[100];
} catch (CAtlException& ex) {
bCaught = ex.m_hr == E_INVALIDARG;
}
CHECK(bCaught);
}
// ---------------------------------------------------------------------
// Collections
static int g_nThings;
struct CThing {
int n;
CThing(int i = 0) : n(i) { g_nThings++; }
CThing(const CThing& t) : n(t.n) { g_nThings++; }
~CThing() { g_nThings--; }
CThing& operator=(const CThing& t) {
n = t.n;
return *this;
}
bool operator==(const CThing& t) const { return n == t.n; }
};
// An object that counts its references.
class CCounted : public IUnknown {
public:
LONG m_nRef = 1;
STDMETHOD(QueryInterface)(REFIID riid, void** ppv) override {
if (IsEqualIID(riid, IID_IUnknown)) {
*ppv = this;
AddRef();
return S_OK;
}
*ppv = NULL;
return E_NOINTERFACE;
}
STDMETHOD_(ULONG, AddRef)() override { return ++m_nRef; }
STDMETHOD_(ULONG, Release)() override { return --m_nRef; }
};
static void TestSimpleCollections() {
{
CSimpleArray<CThing> arr;
for (int i = 0; i < 20; i++) CHECK(arr.Add(CThing(i)));
CHECK(arr.GetSize() == 20 && arr[7].n == 7 && g_nThings == 20);
// Adding one of its own elements as it grows.
CSimpleArray<CThing> grow;
grow.Add(CThing(5));
for (int i = 0; i < 6; i++) grow.Add(grow[0]);
CHECK(grow.GetSize() == 7 && grow[6].n == 5);
CHECK(arr.Find(CThing(12)) == 12 && arr.Find(CThing(99)) == -1);
CHECK(arr.Remove(CThing(0)) && arr.GetSize() == 19 && arr[0].n == 1);
CHECK(arr.RemoveAt(18) && !arr.RemoveAt(18));
CSimpleArray<CThing> copy(arr);
CHECK(copy.GetSize() == 18 && copy[17].n == 18);
CHECK(copy.SetAtIndex(0, CThing(100)) && copy[0].n == 100 && arr[0].n == 1);
CHECK(arr.m_nSize == 18 && arr.m_aT == arr.GetData());
}
CHECK(g_nThings == 0);
CSimpleArray<CComBSTR> bstrs;
bstrs.Add(CComBSTR(L"a"));
bstrs.Add(CComBSTR(L"b"));
CHECK(bstrs.Find(CComBSTR(L"b")) == 1);
CSimpleValArray<int> vals;
vals.Add(3);
vals.Add(4);
CHECK(vals[1] == 4 && vals.GetSize() == 2);
CSimpleMap<int, CString> map;
CHECK(map.Add(1, _T("one")) && map.Add(2, _T("two")) && map.Add(3, _T("three")));
CHECK(map.GetSize() == 3 && map.Lookup(2) == _T("two") && map.Lookup(9).IsEmpty());
CHECK(map.ReverseLookup(_T("three")) == 3 && map.FindKey(1) == 0);
CHECK(map.SetAt(2, _T("TWO")) && map.GetValueAt(1) == _T("TWO"));
CHECK(map.Remove(1) && map.GetSize() == 2 && map.GetKeyAt(0) == 2);
map.RemoveAll();
CHECK(map.GetSize() == 0);
}
static void TestAtlArray() {
{
CAtlArray<CString> arr;
CHECK(arr.IsEmpty());
for (int i = 0; i < 10; i++) {
CString s;
s.Format(_T("%d"), i);
CHECK(arr.Add(s) == (size_t)i);
}
CHECK(arr.GetCount() == 10 && arr[3] == _T("3"));
arr.InsertAt(2, _T("x"), 2);
CHECK(arr.GetCount() == 12 && arr[1] == _T("1") && arr[2] == _T("x") && arr[3] == _T("x") &&
arr[4] == _T("2"));
arr.RemoveAt(2, 2);
CHECK(arr.GetCount() == 10 && arr[2] == _T("2"));
arr.SetAtGrow(14, _T("far"));
CHECK(arr.GetCount() == 15 && arr[12].IsEmpty() && arr[14] == _T("far"));
CAtlArray<CString> other;
other.Add(_T("o"));
CHECK(arr.Append(other) == 15 && arr.GetCount() == 16 && arr[15] == _T("o"));
other.Copy(arr);
CHECK(other.GetCount() == 16 && other[14] == _T("far"));
arr.InsertArrayAt(0, &other);
CHECK(arr.GetCount() == 32 && arr[14] == _T("far") && arr[30] == _T("far"));
CHECK(arr.SetCount(3) && arr.GetCount() == 3);
arr.FreeExtra();
CHECK(arr[2] == _T("2"));
bool bThrown = false;
try {
arr.GetAt(3);
} catch (CAtlException& e) {
bThrown = e.m_hr == E_INVALIDARG;
}
CHECK(bThrown);
}
{
CAtlArray<CThing> things;
things.SetCount(5);
CHECK(g_nThings == 5);
things.RemoveAt(1);
CHECK(g_nThings == 4);
}
CHECK(g_nThings == 0);
{
CAutoPtrArray<CThing> autos;
CAutoPtr<CThing> p(new CThing(1));
autos.Add(p);
CHECK(p.m_p == NULL && g_nThings == 1 && autos[0]->n == 1);
CAutoPtr<CThing> p2(new CThing(2));
autos.Add(p2);
CHECK(g_nThings == 2);
}
CHECK(g_nThings == 0);
CCounted counted;
{
CInterfaceArray<IUnknown> unks;
unks.Add(&counted);
unks.Add(&counted);
CHECK(counted.m_nRef == 3);
unks.RemoveAt(0);
CHECK(counted.m_nRef == 2);
}
CHECK(counted.m_nRef == 1);
}
static void TestAtlList() {
CAtlList<int> list;
for (int i = 1; i <= 5; i++) list.AddTail(i);
list.AddHead(0);
CHECK(list.GetCount() == 6 && list.GetHead() == 0 && list.GetTail() == 5);
int sum = 0;
for (POSITION pos = list.GetHeadPosition(); pos != NULL;) sum += list.GetNext(pos);
CHECK(sum == 15);
POSITION pos3 = list.Find(3);
CHECK(pos3 != NULL && list.GetAt(pos3) == 3 && list.FindIndex(3) == pos3);
list.InsertBefore(pos3, 30);
list.InsertAfter(pos3, 31);
CHECK(list.GetCount() == 8 && list.GetAt(list.FindIndex(3)) == 30 &&
list.GetAt(list.FindIndex(5)) == 31);
list.RemoveAt(pos3);
CHECK(list.Find(3) == NULL && list.GetCount() == 7);
POSITION pos5 = list.Find(5);
list.MoveToHead(pos5);
CHECK(list.GetHead() == 5 && list.GetHeadPosition() == pos5);
POSITION pos0 = list.Find(0);
list.SwapElements(pos5, pos0);
CHECK(list.GetHead() == 0 && list.GetAt(list.FindIndex(1)) == 5);
CHECK(list.RemoveHead() == 0 && list.RemoveTail() == 4 && list.GetCount() == 5);
// Backwards.
int order[5];
int n = 0;
for (POSITION pos = list.GetTailPosition(); pos != NULL;) order[n++] = list.GetPrev(pos);
CHECK(n == 5 && order[0] == 31 && order[4] == 5);
list.RemoveAll();
CHECK(list.IsEmpty() && list.GetHeadPosition() == NULL);
{
CAtlList<CString> strs;
strs.AddTail(_T("b"));
strs.AddHead(_T("a"));
CAtlList<CString> more;
more.AddTailList(&strs);
more.AddHeadList(&strs);
CHECK(more.GetCount() == 4 && more.GetHead() == _T("a") && more.GetTail() == _T("b"));
CHECK(more.Find(_T("b")) == more.FindIndex(1));
}
{
CAutoPtrList<CThing> autos;
CAutoPtr<CThing> p7(new CThing(7));
autos.AddTail(p7);
CHECK(g_nThings == 1 && autos.GetHead()->n == 7);
}
CHECK(g_nThings == 0);
}
static void TestAtlMap() {
CAtlMap<CString, int, CStringElementTraits<CString>> map;
map.SetAt(_T("one"), 1);
map[_T("two")] = 2;
map.SetAt(_T("three"), 3);
int v = 0;
CHECK(map.GetCount() == 3 && map.Lookup(_T("two"), v) && v == 2);
CHECK(!map.Lookup(_T("four"), v));
const CAtlMap<CString, int, CStringElementTraits<CString>>::CPair* pPair = map.Lookup(_T("three"));
CHECK(pPair != NULL && pPair->m_key == _T("three") && pPair->m_value == 3);
map.SetAt(_T("one"), 11);
CHECK(map.GetCount() == 3 && map[_T("one")] == 11);
CHECK(map.RemoveKey(_T("two")) && !map.RemoveKey(_T("two")) && map.GetCount() == 2);
int nSeen = 0, nSum = 0;
for (POSITION pos = map.GetStartPosition(); pos != NULL;) {
CString key;
int val;
map.GetNextAssoc(pos, key, val);
nSeen++;
nSum += val;
}
CHECK(nSeen == 2 && nSum == 14);
// Ignoring case.
CAtlMap<CString, int, CStringElementTraitsI<CString>> mapI;
mapI[_T("Key")] = 5;
CHECK(mapI.Lookup(_T("KEY"), v) && v == 5);
// Many, rehashed as they come.
CAtlMap<int, int> big;
for (int i = 0; i < 5000; i++) big[i] = i * 2;
CHECK(big.GetCount() == 5000 && big.GetHashTableSize() > 17);
bool bAll = true;
for (int i = 0; i < 5000; i++)
if (!big.Lookup(i, v) || v != i * 2) bAll = false;
CHECK(bAll);
int nCount = 0;
for (POSITION pos = big.GetStartPosition(); pos != NULL; big.GetNext(pos)) nCount++;
CHECK(nCount == 5000);
for (int i = 0; i < 5000; i += 2) big.RemoveKey(i);
CHECK(big.GetCount() == 2500 && !big.Lookup(10, v) && big.Lookup(11, v) && v == 22);
POSITION pos = big.GetStartPosition();
big.RemoveAtPos(pos);
CHECK(big.GetCount() == 2499);
// GUID keys.
CAtlMap<GUID, int> guids;
guids[IID_IUnknown] = 1;
guids[IID_IDispatch] = 2;
CHECK(guids.Lookup(IID_IDispatch, v) && v == 2);
}
static void TestRBMap() {
CRBMap<int, CString> map;
map.SetAt(5, _T("five"));
map.SetAt(1, _T("one"));
map.SetAt(3, _T("three"));
map.SetAt(3, _T("THREE"));
CHECK(map.GetCount() == 3);
int keys[3];
int n = 0;
for (POSITION pos = map.GetHeadPosition(); pos != NULL;) keys[n++] = map.GetNextKey(pos);
CHECK(n == 3 && keys[0] == 1 && keys[1] == 3 && keys[2] == 5);
CString s;
CHECK(map.Lookup(3, s) && s == _T("THREE"));
POSITION posAfter = map.FindFirstKeyAfter(3);
CHECK(posAfter != NULL && map.GetKeyAt(posAfter) == 5);
POSITION posTail = map.GetTailPosition();
CHECK(map.GetPrev(posTail)->m_key == 5 && map.GetKeyAt(posTail) == 3);
CHECK(map.RemoveKey(1) && map.GetCount() == 2 && map.GetKeyAt(map.GetHeadPosition()) == 3);
CRBMultiMap<CString, int, CStringElementTraits<CString>> multi;
multi.Insert(_T("b"), 1);
multi.Insert(_T("a"), 2);
multi.Insert(_T("b"), 3);
multi.Insert(_T("c"), 4);
CHECK(multi.GetCount() == 4);
int nB = 0, nSum = 0;
for (POSITION pos = multi.FindFirstWithKey(_T("b")); pos != NULL;) {
nSum += multi.GetNextValueWithKey(pos, _T("b"));
nB++;
}
CHECK(nB == 2 && nSum == 4);
CHECK(multi.RemoveKey(_T("b")) == 2 && multi.GetCount() == 2);
CHECK(multi.FindFirstWithKey(_T("b")) == NULL);
}
// ---------------------------------------------------------------------
// Memory and errors
static void TestMemory() {
size_t r;
CHECK(AtlMultiply<size_t>(&r, 3, 4) == S_OK && r == 12);
CHECK(FAILED(AtlMultiply<size_t>(&r, (size_t)-1, 2)));
int ri;
CHECK(FAILED(AtlAdd<int>(&ri, 0x7FFFFFFF, 1)) && AtlAdd<int>(&ri, 2, 3) == S_OK && ri == 5);
CHeapPtr<int> p;
CHECK(p.Allocate(10));
for (int i = 0; i < 10; i++) p[i] = i;
CHECK(p.Reallocate(1000) && p[9] == 9);
CHeapPtr<int> q(p); // the memory passes to q
CHECK(p.m_pData == NULL && q[5] == 5);
q.Free();
CHECK(q.m_pData == NULL);
CComHeapPtr<WCHAR> co;
CHECK(co.Allocate(4));
::lstrcpyW(co, L"abc");
WCHAR* pDetached = co.Detach();
CHECK(co.m_pData == NULL);
::CoTaskMemFree(pDetached);
CTempBuffer<char, 16> small(10);
CHECK((char*)small != NULL);
::memcpy((char*)small, "0123456789", 10);
small.Reallocate(1000); // off the stack: the contents go along
CHECK(::memcmp((char*)small, "0123456789", 10) == 0);
small.Reallocate(8); // and back
CHECK(::memcmp((char*)small, "01234567", 8) == 0);
{
CAutoPtr<CThing> a(new CThing(4));
CAutoPtr<CThing> b;
b = a;
CHECK(a.m_p == NULL && b->n == 4 && g_nThings == 1);
CAutoVectorPtr<CThing> v;
CHECK(v.Allocate(3) && g_nThings == 4);
}
CHECK(g_nThings == 0);
HANDLE hEvent = ::CreateEvent(NULL, TRUE, FALSE, NULL);
{
CHandle h(hEvent);
CHandle h2(h); // passes too
CHECK(h.m_h == NULL && h2 == hEvent);
}
// Closed: the handle no longer works.
CHECK(!::SetEvent(hEvent));
bool bCaught = false;
try {
AtlThrow(E_ACCESSDENIED);
} catch (CAtlException& e) {
bCaught = e == E_ACCESSDENIED;
}
CHECK(bCaught);
bCaught = false;
try {
::SetLastError(ERROR_FILE_NOT_FOUND);
AtlThrowLastWin32();
} catch (CAtlException& e) {
bCaught = e.m_hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
}
CHECK(bCaught);
}
// ---------------------------------------------------------------------
// CComSafeArray
// The memory managers, the string manager over one, fixed strings, a
// string resource as it is in the module, and currency.
static void TestHeapsAndCurrency() {
CWin32Heap own(0, 0);
CCRTHeap crt;
CComHeap com;
CLocalHeap local;
CGlobalHeap global;
IAtlMemMgr* heaps[] = {&own, &crt, &com, &local, &global};
for (int i = 0; i < 5; i++) {
IAtlMemMgr* m = heaps[i];
char* p = (char*)m->Allocate(10);
CHECK(p != NULL);
if (p == NULL) continue;
memcpy(p, "123456789", 10);
CHECK(m->GetSize(p) >= 10);
p = (char*)m->Reallocate(p, 1000);
CHECK(p != NULL && strcmp(p, "123456789") == 0 && m->GetSize(p) >= 1000);
m->Free(p);
}
CHECK(crt.GetSize(crt.Allocate(0)) == 0); // leaks nothing much: 16 bytes
CWin32Heap process(::GetProcessHeap());
void* pp = process.Allocate(8);
CHECK(pp != NULL && ::HeapSize(::GetProcessHeap(), 0, pp) == 8);
process.Free(pp);
CHECK(process.Detach() == ::GetProcessHeap());
CAtlStringMgr mgr(&own);
CStringData* pd = mgr.Allocate(20, sizeof(TCHAR));
CHECK(pd != NULL && pd->nRefs == 1 && pd->nAllocLength >= 20 && pd->nDataLength == 0);
pd = mgr.Reallocate(pd, 200, sizeof(TCHAR));
CHECK(pd != NULL && pd->nAllocLength >= 200);
mgr.Free(pd);
CHECK(mgr.GetNilString()->nDataLength == 0 && mgr.Clone() == &mgr);
CString sMgr(_T("managed"), &mgr);
CHECK(sMgr == _T("managed") && sMgr.GetManager() != NULL);
CFixedStringT<CString, 100> fs;
CHECK(fs.IsEmpty() && fs.GetAllocLength() >= 100);
fs = _T("fixed");
fs += _T(" string");
CHECK(fs == _T("fixed string") && fs.GetAllocLength() >= 100);
CString other = _T("shared?");
fs = other;
CHECK(fs == _T("shared?") && fs.GetString() != other.GetString());
CFixedStringT<CStringW, 4> fw(L"longer than four");
CHECK(fw == L"longer than four");
const ATLSTRINGRESOURCEIMAGE* img = AtlGetStringResourceImage(IDS_HELLO);
CHECK(img != NULL && img->nLength == 9 && wcsncmp(img->achString, L"Hello, %s", 9) == 0);
CHECK(AtlGetStringResourceImage(999) == NULL);
CHECK(AtlGetVersion() == _ATL_VER);
CComCurrency c(12, 3456);
CHECK(c.GetInteger() == 12 && c.GetFraction() == 3456 && c.m_currency.int64 == 123456);
CComCurrency neg(-3, 2500);
CHECK(neg.m_currency.int64 == -32500 && neg.GetFraction() == -2500);
CHECK(c.SetFraction(10000) == E_INVALIDARG);
CComCurrency sum = c + neg;
CHECK(sum.m_currency.int64 == 90956);
CHECK((c - neg).m_currency.int64 == 155956);
// 12.3456 * 2.5 = 30.864; / 3 = 10.288
CComCurrency prod = c * CComCurrency(2.5);
CHECK(prod.m_currency.int64 == 308640);
CHECK((prod / CComCurrency(3L)).m_currency.int64 == 102880);
CHECK((prod / 3L).m_currency.int64 == 102880);
CHECK((c * 2L).m_currency.int64 == 246912);
// 0.0001 * 0.5: half a ten-thousandth, rounded to the even 0.
CComCurrency tiny;
tiny.m_currency.int64 = 1;
CHECK((tiny * CComCurrency(0.5)).m_currency.int64 == 0);
tiny.m_currency.int64 = 3;
CHECK((tiny * CComCurrency(0.5)).m_currency.int64 == 2); // 1.5 to 2
CComCurrency r(12, 3450);
CHECK(r.Round(2) == S_OK && r.m_currency.int64 == 123400); // 12.345 to 12.34
r = CComCurrency(12, 3350);
CHECK(r.Round(2) == S_OK && r.m_currency.int64 == 123400); // 12.335 to 12.34
CHECK(r.Round(5) == E_INVALIDARG);
CHECK(CComCurrency(L"1.5").m_currency.int64 == 15000);
CComVariant vr(2.25);
CComCurrency fromVar(vr);
CHECK(fromVar.m_currency.int64 == 22500);
CHECK(c > neg && neg < c && c != neg && c == CComCurrency(12, 3456));
CHECK((-neg).m_currency.int64 == 32500);
bool bThrew = false;
try {
CComCurrency big;
big.m_currency.int64 = 0x7fffffffffffffffLL;
big += CComCurrency(1L);
} catch (CAtlException& e) {
bThrew = e.m_hr == DISP_E_OVERFLOW;
}
CHECK(bThrew);
bThrew = false;
try {
CComCurrency z = c / CComCurrency();
(void)z;
} catch (CAtlException& e) {
bThrew = e.m_hr == DISP_E_DIVBYZERO;
}
CHECK(bThrew);
}
static void TestSafeArray() {
CComSafeArray<LONG> sa;
CHECK(SUCCEEDED(sa.Create(5, 1)));
CHECK(sa.GetCount() == 5 && sa.GetLowerBound() == 1 && sa.GetUpperBound() == 5);
CHECK(sa.GetType() == VT_I4 && sa.GetDimensions() == 1);
for (LONG i = 1; i <= 5; i++) CHECK(SUCCEEDED(sa.SetAt(i, i * 10)));
CHECK(sa[3] == 30 && sa.GetAt(5) == 50);
sa[2] = 22;
CHECK(sa.GetAt(2) == 22);
CHECK(SUCCEEDED(sa.Add(60)) && sa.GetCount() == 6 && sa[6] == 60);
LONG more[] = {70, 80};
CHECK(SUCCEEDED(sa.Add(2, more)) && sa.GetCount() == 8 && sa[8] == 80);
CHECK(SUCCEEDED(sa.Resize(3, 1)) && sa.GetCount() == 3 && sa[3] == 30);
CHECK(sa.SetAt(9, 1) == E_INVALIDARG);
// Through a VARIANT, as a server returns it.
CComVariant var;
var.vt = VT_ARRAY | VT_I4;
CHECK(SUCCEEDED(sa.CopyTo(&var.parray)));
CComSafeArray<LONG> back(var.parray);
CHECK(back.GetCount() == 3 && back[1] == 10 && back[2] == 22);
// Detached and attached again.
LPSAFEARRAY psa = back.Detach();
CHECK(back.m_psa == NULL && psa != NULL);
CHECK(SUCCEEDED(back.Attach(psa)) && back[3] == 30);
CComSafeArray<LONG> copy(back);
copy[1] = 99;
CHECK(back[1] == 10 && copy[1] == 99);
// An array of another type is refused.
CComSafeArray<double> dbl;
CHECK(dbl.Attach(psa) == E_INVALIDARG);
CComSafeArray<BSTR> strs;
CComBSTR bstr(L"first");
CHECK(SUCCEEDED(strs.Add(bstr)));
CHECK(SUCCEEDED(strs.Add(::SysAllocString(L"second"), FALSE)));
CHECK(strs.GetCount() == 2 && strs[0] != bstr.m_str && ::lstrcmpW(strs[0], L"first") == 0 &&
::lstrcmpW(strs[1], L"second") == 0);
CComSafeArray<VARIANT> vars(2);
CHECK(SUCCEEDED(vars.SetAt(0, CComVariant(L"text"))));
CHECK(SUCCEEDED(vars.SetAt(1, CComVariant(7L))));
CHECK(vars[0].vt == VT_BSTR && vars[1].vt == VT_I4 && vars[1].lVal == 7);
// Two dimensions.
CComSafeArrayBound bounds[2] = {CComSafeArrayBound(3), CComSafeArrayBound(4, 1)};
CComSafeArray<SHORT> grid(bounds, 2);
CHECK(grid.GetDimensions() == 2 && grid.GetCount(0) == 3 && grid.GetCount(1) == 4 &&
grid.GetLowerBound(1) == 1);
LONG idx[2] = {2, 4};
CHECK(SUCCEEDED(grid.MultiDimSetAt(idx, 42)));
SHORT sv = 0;
CHECK(SUCCEEDED(grid.MultiDimGetAt(idx, sv)) && sv == 42);
}
// ---------------------------------------------------------------------
// CComDispatchDriver, over a dispatch of the test's own: a property
// Value (1) and methods Add (2: Value += a) and Sub (3: a - b).
class CDisp : public IDispatch {
public:
LONG m_nRef = 1;
long m_nValue = 0;
STDMETHOD(QueryInterface)(REFIID riid, void** ppv) override {
if (IsEqualIID(riid, IID_IUnknown) || IsEqualIID(riid, IID_IDispatch)) {
*ppv = static_cast<IDispatch*>(this);
AddRef();
return S_OK;
}
*ppv = NULL;
return E_NOINTERFACE;
}
STDMETHOD_(ULONG, AddRef)() override { return ++m_nRef; }
STDMETHOD_(ULONG, Release)() override { return --m_nRef; }
STDMETHOD(GetTypeInfoCount)(UINT* pctinfo) override {
*pctinfo = 0;
return S_OK;
}
STDMETHOD(GetTypeInfo)(UINT, LCID, ITypeInfo**) override { return E_NOTIMPL; }
STDMETHOD(GetIDsOfNames)(REFIID, LPOLESTR* names, UINT n, LCID, DISPID* ids) override {
static const OLECHAR* known[] = {OLESTR("Value"), OLESTR("Add"), OLESTR("Sub")};
for (UINT i = 0; i < n; i++) {
ids[i] = DISPID_UNKNOWN;
for (int k = 0; k < 3; k++)
if (CComBSTR(names[i]) == CComBSTR(known[k])) ids[i] = k + 1;
if (ids[i] == DISPID_UNKNOWN) return DISP_E_UNKNOWNNAME;
}
return S_OK;
}
STDMETHOD(Invoke)(DISPID id, REFIID, LCID, WORD flags, DISPPARAMS* dp, VARIANT* pResult,
EXCEPINFO*, UINT*) override {
CComVariant a;
if (id == 1 && (flags & DISPATCH_PROPERTYGET)) {
if (pResult == NULL) return E_INVALIDARG;
CComVariant(m_nValue).Detach(pResult);
return S_OK;
}
if (id == 1 && (flags & DISPATCH_PROPERTYPUT)) {
if (dp->cArgs != 1 || dp->cNamedArgs != 1 || dp->rgdispidNamedArgs[0] != DISPID_PROPERTYPUT)
return DISP_E_BADPARAMCOUNT;
if (FAILED(a.ChangeType(VT_I4, &dp->rgvarg[0]))) return DISP_E_TYPEMISMATCH;
m_nValue = a.lVal;
return S_OK;
}
if (id == 2 && (flags & DISPATCH_METHOD)) {
if (dp->cArgs != 1) return DISP_E_BADPARAMCOUNT;
if (FAILED(a.ChangeType(VT_I4, &dp->rgvarg[0]))) return DISP_E_TYPEMISMATCH;
m_nValue += a.lVal;
if (pResult != NULL) CComVariant(m_nValue).Detach(pResult);
return S_OK;
}
if (id == 3 && (flags & DISPATCH_METHOD)) {
// Sub(a, b): the arguments come last first.
if (dp->cArgs != 2) return DISP_E_BADPARAMCOUNT;
CComVariant b;
a.ChangeType(VT_I4, &dp->rgvarg[1]);
b.ChangeType(VT_I4, &dp->rgvarg[0]);
if (pResult != NULL) CComVariant(a.lVal - b.lVal).Detach(pResult);
return S_OK;
}
return DISP_E_MEMBERNOTFOUND;
}
};
static void TestDispatchDriver() {
CDisp disp;
{
CComPtr<IUnknown> spUnk(&disp);
CComDispatchDriver dd(spUnk);
CHECK(dd.p != NULL);
CComVariant v(5L);
CHECK(SUCCEEDED(dd.PutPropertyByName(OLESTR("Value"), &v)) && disp.m_nValue == 5);
CComVariant r;
CHECK(SUCCEEDED(dd.GetProperty(1, &r)) && r.vt == VT_I4 && r.lVal == 5);
DISPID id;
CHECK(SUCCEEDED(dd.GetIDOfName(OLESTR("Add"), &id)) && id == 2);
CComVariant arg(3L), res;
CHECK(SUCCEEDED(dd.Invoke1(OLESTR("Add"), &arg, &res)) && res.lVal == 8 && disp.m_nValue == 8);
CComVariant x(10L), y(4L);
res.Clear();
CHECK(SUCCEEDED(dd.Invoke2(OLESTR("Sub"), &x, &y, &res)) && res.lVal == 6);
CComVariant args[2] = {CComVariant(1L), CComVariant(10L)}; // last first: 10 - 1
res.Clear();
CHECK(SUCCEEDED(dd.InvokeN((DISPID)3, args, 2, &res)) && res.lVal == 9);
CHECK(dd.Invoke0(OLESTR("Nothing")) == DISP_E_UNKNOWNNAME);
// The same on CComPtr<IDispatch>, and statically.
CComPtr<IDispatch> spDisp(&disp);
r.Clear();
CHECK(SUCCEEDED(spDisp.GetPropertyByName(OLESTR("Value"), &r)) && r.lVal == 8);
CComVariant v2(1L);
CHECK(SUCCEEDED(CComDispatchDriver::PutProperty(&disp, 1, &v2)) && disp.m_nValue == 1);
}
CHECK(disp.m_nRef == 1);
}
// ---------------------------------------------------------------------
// Files
static void TestFiles() {
TCHAR szDir[MAX_PATH], szPath[MAX_PATH];
::GetTempPath(MAX_PATH, szDir);
::lstrcpy(szPath, szDir);
::lstrcat(szPath, TEXT("atlutiltest.dat"));
{
CAtlFile file;
CHECK(SUCCEEDED(file.Create(szPath, GENERIC_READ | GENERIC_WRITE, 0, CREATE_ALWAYS)));
DWORD nWritten = 0;
CHECK(SUCCEEDED(file.Write("hello, file", 11, &nWritten)) && nWritten == 11);
ULONGLONG nPos = 0, nSize = 0;
CHECK(SUCCEEDED(file.GetPosition(nPos)) && nPos == 11);
CHECK(SUCCEEDED(file.GetSize(nSize)) && nSize == 11);
CHECK(SUCCEEDED(file.Seek(7, FILE_BEGIN)));
char buf[8] = {};
CHECK(SUCCEEDED(file.Read(buf, 4)) && ::memcmp(buf, "file", 4) == 0);
// Reading past the end.
CHECK(file.Read(buf, 4) == HRESULT_FROM_WIN32(ERROR_HANDLE_EOF));
CHECK(SUCCEEDED(file.LockRange(0, 5)) && SUCCEEDED(file.UnlockRange(0, 5)));
CHECK(SUCCEEDED(file.SetSize(5)) && SUCCEEDED(file.GetSize(nSize)) && nSize == 5);
CHECK(SUCCEEDED(file.Flush()));
}
{
CAtlFile file;
CHECK(SUCCEEDED(file.Create(szPath, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING)));
CAtlFileMapping<char> map;
CHECK(SUCCEEDED(map.MapFile(file)));
CHECK(map.GetMappingSize() == 5 && ::memcmp((char*)map, "hello", 5) == 0);
}
CAtlFile missing;
CHECK(missing.Create(TEXT("Z:\\no\\such\\file"), GENERIC_READ, 0, OPEN_EXISTING) ==
HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND) ||
missing.m_h == NULL);
::DeleteFile(szPath);
// A temporary file, kept under a name.
TCHAR szKept[MAX_PATH];
::lstrcpy(szKept, szDir);
::lstrcat(szKept, TEXT("atlutiltest.kept"));
{
CAtlTemporaryFile tmp;
CHECK(SUCCEEDED(tmp.Create()));
CString strTemp = tmp.TempFileName();
CHECK(::GetFileAttributes(strTemp) != INVALID_FILE_ATTRIBUTES);
CHECK(SUCCEEDED(tmp.Write("kept", 4)));
CHECK(SUCCEEDED(tmp.Close(szKept)));
CHECK(::GetFileAttributes(strTemp) == INVALID_FILE_ATTRIBUTES);
}
CHECK(::GetFileAttributes(szKept) != INVALID_FILE_ATTRIBUTES);
::DeleteFile(szKept);
{
// And one that goes.
CString strTemp;
{
CAtlTemporaryFile tmp;
CHECK(SUCCEEDED(tmp.Create()));
strTemp = tmp.TempFileName();
}
CHECK(::GetFileAttributes(strTemp) == INVALID_FILE_ATTRIBUTES);
}
// Shared memory, mapped twice.
CAtlFileMapping<int> shm1, shm2;
BOOL bExisted = TRUE;
CHECK(SUCCEEDED(shm1.MapSharedMem(4096, TEXT("AtlUtilTestShm"), &bExisted)) && !bExisted);
((int*)shm1)[10] = 1234;
CHECK(SUCCEEDED(shm2.OpenMapping(TEXT("AtlUtilTestShm"), 4096)) && ((int*)shm2)[10] == 1234);
}
// ---------------------------------------------------------------------
// The service
static bool WaitForState(SC_HANDLE hService, DWORD dwState) {
for (int i = 0; i < 300; i++) {
SERVICE_STATUS status;
if (::QueryServiceStatus(hService, &status) && status.dwCurrentState == dwState) return true;
::Sleep(100);
}
return false;
}
static void TestService() {
// As /Service gives it: installed, and the AppID names it.
CHECK(lstrcmp(_AtlModule.m_szServiceName, TEXT("AtlUtilTestSvc")) == 0);
HRESULT hr = E_FAIL;
CHECK(!_AtlModule.ParseCommandLine(TEXT("atlutiltest.exe /Service"), &hr));
CHECK(SUCCEEDED(hr));
CHECK(_AtlModule.IsInstalled());
CRegKey key;
TCHAR sz[64] = TEXT("");
ULONG n = 64;
CHECK(key.Open(HKEY_CLASSES_ROOT, TEXT("AppID\\{3C1D7E52-8F4B-4A6D-9E21-5B7C0D9A1F64}"),
KEY_READ) == ERROR_SUCCESS &&
key.QueryStringValue(TEXT("LocalService"), sz, &n) == ERROR_SUCCESS &&
lstrcmp(sz, TEXT("AtlUtilTestSvc")) == 0);
key.Close();
SC_HANDLE hSCM = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
CHECK(hSCM != NULL);
SC_HANDLE hService = hSCM ? ::OpenService(hSCM, TEXT("AtlUtilTestSvc"), SERVICE_ALL_ACCESS) : NULL;
CHECK(hService != NULL);
if (hService != NULL) {
// Its command line is this program's path, quoted.
BYTE buf[4096];
QUERY_SERVICE_CONFIG* pConfig = (QUERY_SERVICE_CONFIG*)buf;
DWORD cb = 0;
CHECK(::QueryServiceConfig(hService, pConfig, sizeof(buf), &cb));
TCHAR szExe[MAX_PATH + 2];
szExe[0] = TEXT('"');
DWORD nExe = ::GetModuleFileName(NULL, szExe + 1, MAX_PATH);
szExe[nExe + 1] = TEXT('"');
szExe[nExe + 2] = 0;
CHECK(lstrcmpi(pConfig->lpBinaryPathName, szExe) == 0);
// Started with -AsService, so that this program knows to be it.
TCHAR szCmd[MAX_PATH + 16];
lstrcpy(szCmd, szExe);
lstrcat(szCmd, TEXT(" -AsService"));
CHECK(::ChangeServiceConfig(hService, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE,
szCmd, NULL, NULL, NULL, NULL, NULL, NULL));
SetState(TEXT(""));
CHECK(::StartService(hService, 0, NULL));
CHECK(WaitForState(hService, SERVICE_RUNNING));
CHECK(GetState() == _T("running"));
SERVICE_STATUS status;
CHECK(::ControlService(hService, SERVICE_CONTROL_STOP, &status));
CHECK(WaitForState(hService, SERVICE_STOPPED));
CHECK(GetState() == _T("stopping"));
::CloseServiceHandle(hService);
}
if (hSCM != NULL) ::CloseServiceHandle(hSCM);
// /UnregServer: uninstalled, and the AppID gone.
CHECK(!_AtlModule.ParseCommandLine(TEXT("atlutiltest.exe -UnregServer"), &hr));
CHECK(SUCCEEDED(hr));
CHECK(!_AtlModule.IsInstalled());
CHECK(key.Open(HKEY_CLASSES_ROOT, TEXT("AppID\\{3C1D7E52-8F4B-4A6D-9E21-5B7C0D9A1F64}"),
KEY_READ) != ERROR_SUCCESS);
RegDeleteKey(HKEY_LOCAL_MACHINE, STATE_KEY);
}
// ---------------------------------------------------------------------
// CPath.
static void TestPath() {
CPath p(_T("C:\\dir"));
p.Append(_T("sub\\file.txt"));
CHECK(CString((LPCTSTR)p) == _T("C:\\dir\\sub\\file.txt"));
CHECK(p.FindExtension() == 15); // indexes, as in ATL
CHECK(p.GetExtension() == _T(".txt"));
CHECK(p.FindFileName() == 11);
CHECK(p.MatchSpec(_T("*.txt")));
CHECK(!p.MatchSpec(_T("*.doc")));
CHECK(!p.IsRelative());
CHECK(p.GetDriveNumber() == 2);
CHECK(p.RenameExtension(_T(".log")));
CHECK(CString(p.m_strPath) == _T("C:\\dir\\sub\\file.log"));
p.RemoveExtension();
CHECK(p.m_strPath == _T("C:\\dir\\sub\\file"));
p.AddExtension(_T(".bak"));
CHECK(p.m_strPath == _T("C:\\dir\\sub\\file.bak"));
CHECK(p.RemoveFileSpec());
CHECK(p.m_strPath == _T("C:\\dir\\sub"));
p.AddBackslash();
CHECK(p.m_strPath == _T("C:\\dir\\sub\\"));
p.RemoveBackslash();
CHECK(p.m_strPath == _T("C:\\dir\\sub"));
p.StripToRoot();
CHECK(p.m_strPath == _T("C:\\"));
CHECK(p.IsRoot());
CPath q(_T("C:\\a\\.\\b\\..\\c.txt"));
q.Canonicalize();
CHECK(q.m_strPath == _T("C:\\a\\c.txt"));
CPath r(_T("rel\\x"));
CHECK(r.IsRelative());
CPath c;
c.Combine(_T("C:\\one"), _T("two.txt"));
CHECK(c.m_strPath == _T("C:\\one\\two.txt"));
CPath s(_T("C:\\with space\\x"));
s.QuoteSpaces();
CHECK(s.m_strPath == _T("\"C:\\with space\\x\""));
s.UnquoteSpaces();
CHECK(s.m_strPath == _T("C:\\with space\\x"));
CPath f(_T("C:\\dir\\name.ext"));
f.StripPath();
CHECK(f.m_strPath == _T("name.ext"));
CHECK(f.IsFileSpec());
CHECK(CPath(_T("\\\\server\\share")).IsUNCServerShare());
CHECK(CPath(_T("C:\\a\\b")).CommonPrefix(_T("C:\\a\\c")).m_strPath == _T("C:\\a"));
CPath rel;
CHECK(rel.RelativePathTo(_T("C:\\a\\b"), FILE_ATTRIBUTE_DIRECTORY, _T("C:\\a\\c\\d.txt"), 0));
CHECK(rel.m_strPath == _T("..\\c\\d.txt"));
TCHAR szWin[MAX_PATH];
::GetWindowsDirectory(szWin, MAX_PATH);
CPath w(szWin);
CHECK(w.FileExists());
CHECK(w.IsDirectory());
w.Append(_T("no-such-file.xyz"));
CHECK(!w.FileExists());
// The ANSI and Unicode forms, whatever TCHAR is.
CPathA pa("x\\y.c");
CHECK(pa.FindExtension() == 3 && pa.GetExtension() == ".c");
CPathW pw(L"x\\y.c");
CHECK(pw.FindExtension() == 3 && pw.GetExtension() == L".c");
}
// ---------------------------------------------------------------------
// atlsecurity.h.
static void TestSecurity() {
// Well-known SIDs, their string forms and comparisons.
CHECK(CString(Sids::World().Sid()) == _T("S-1-1-0"));
CHECK(CString(Sids::System().Sid()) == _T("S-1-5-18"));
CHECK(CString(Sids::Admins().Sid()) == _T("S-1-5-32-544"));
CHECK(Sids::Admins() == Sids::Admins());
CHECK(Sids::Admins() != Sids::Users());
CHECK(Sids::Admins().EqualPrefix(Sids::Users()));
CHECK(Sids::Users().GetSubAuthority(1) == DOMAIN_ALIAS_RID_USERS);
CHECK(Sids::World().IsValid());
SID_IDENTIFIER_AUTHORITY nt = SECURITY_NT_AUTHORITY;
CSid admins(nt, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS);
CHECK(admins == Sids::Admins());
// CSid in an ATL collection.
CAtlMap<CSid, int> map;
map[Sids::World()] = 1;
map[Sids::Admins()] = 2;
int n = 0;
CHECK(map.Lookup(Sids::Admins(), n) && n == 2);
// The process token: its user, a SID that round-trips through a name.
CAccessToken tok;
CHECK(tok.GetProcessToken(TOKEN_QUERY));
CSid user;
CHECK(tok.GetUser(&user));
CHECK(user.IsValid());
Log("token user %s\\%s (%s)", (const char*)CStringA(user.Domain()),
(const char*)CStringA(user.AccountName()), (const char*)CStringA(user.Sid()));
CHECK(*user.AccountName() != 0);
CSid byName;
CString strFull = CString(user.Domain()) + _T("\\") + user.AccountName();
CHECK(byName.LoadAccount(strFull));
CHECK(byName == user);
CSid::CSidArray groups;
CHECK(tok.GetGroups(&groups));
CHECK(groups.GetCount() > 0);
TOKEN_TYPE type;
CHECK(tok.GetType(&type) && type == TokenPrimary);
// A DACL: the denied entry goes first, whatever the order of adds.
CDacl dacl;
CHECK(dacl.IsEmpty());
CHECK(dacl.AddAllowedAce(Sids::World(), GENERIC_READ));
CHECK(dacl.AddAllowedAce(user, GENERIC_ALL));
CHECK(dacl.AddDeniedAce(Sids::Guests(), GENERIC_WRITE));
CHECK(dacl.GetAceCount() == 3);
CSid sid0;
ACCESS_MASK mask0 = 0;
BYTE type0 = 0;
dacl.GetAclEntry(0, &sid0, &mask0, &type0);
CHECK(sid0 == Sids::Guests() && type0 == ACCESS_DENIED_ACE_TYPE && mask0 == GENERIC_WRITE);
CHECK(::IsValidAcl((PACL)dacl.GetPACL()));
dacl.RemoveAces(Sids::Guests());
CHECK(dacl.GetAceCount() == 2);
// A security descriptor: built, to SDDL and back.
CSecurityDesc sd;
sd.SetOwner(Sids::Admins());
sd.SetGroup(Sids::System());
sd.SetDacl(dacl);
CString strSddl;
CHECK(sd.ToString(&strSddl));
Log("sddl %s", (const char*)CStringA(strSddl));
CHECK(strSddl.Find(_T("O:BA")) == 0);
CHECK(strSddl.Find(_T("G:SY")) > 0);
CHECK(strSddl.Find(_T("D:")) > 0);
CSecurityDesc sd2;
CHECK(sd2.FromString(strSddl));
CSid owner;
CHECK(sd2.GetOwner(&owner) && owner == Sids::Admins());
CDacl dacl2;
bool bPresent = false;
CHECK(sd2.GetDacl(&dacl2, &bPresent) && bPresent);
CHECK(dacl2.GetAceCount() == 2);
CHECK(::IsValidSecurityDescriptor((PSECURITY_DESCRIPTOR)sd2.GetPSECURITY_DESCRIPTOR()));
CSecurityDesc sd3;
CHECK(sd3.FromString(_T("D:(A;;GA;;;WD)")));
CDacl dacl3;
CHECK(sd3.GetDacl(&dacl3) && dacl3.GetAceCount() == 1);
// An event created with the descriptor; its DACL read back, and replaced.
CSecurityAttributes sa(sd);
HANDLE hEvent = ::CreateEvent(&sa, TRUE, FALSE, NULL);
CHECK(hEvent != NULL);
if (hEvent != NULL) {
CDacl got;
CHECK(AtlGetDacl(hEvent, SE_KERNEL_OBJECT, &got));
CHECK(got.GetAceCount() == 2);
CDacl one;
one.AddAllowedAce(user, EVENT_ALL_ACCESS);
CHECK(AtlSetDacl(hEvent, SE_KERNEL_OBJECT, one));
CHECK(AtlGetDacl(hEvent, SE_KERNEL_OBJECT, &got));
CHECK(got.GetAceCount() == 1);
::CloseHandle(hEvent);
}
// A file's owner and DACL, by name.
TCHAR szTmp[MAX_PATH], szFile[MAX_PATH];
::GetTempPath(MAX_PATH, szTmp);
::GetTempFileName(szTmp, _T("sec"), 0, szFile);
CSid fileOwner;
CHECK(AtlGetOwnerSid(szFile, SE_FILE_OBJECT, &fileOwner));
CHECK(fileOwner.IsValid());
CDacl fileDacl;
fileDacl.AddAllowedAce(user, FILE_ALL_ACCESS);
fileDacl.AddAllowedAce(Sids::World(), FILE_GENERIC_READ);
CHECK(AtlSetDacl(szFile, SE_FILE_OBJECT, fileDacl));
CDacl fileDacl2;
CHECK(AtlGetDacl(szFile, SE_FILE_OBJECT, &fileDacl2));
CHECK(fileDacl2.GetAceCount() >= 2);
::DeleteFile(szFile);
}
static void TestTime() {
// CTime and CTimeSpan without MFC, and ATL's names for them.
ATL::CTime t(2003, 7, 15, 13, 45, 30);
CHECK(t.GetYear() == 2003 && t.GetMonth() == 7 && t.GetDay() == 15);
CHECK(t.GetHour() == 13 && t.GetMinute() == 45 && t.GetSecond() == 30);
CHECK(t.GetDayOfWeek() == 3); // a Tuesday
CHECK(t.Format(_T("%Y-%m-%d %H:%M:%S")) == _T("2003-07-15 13:45:30"));
CHECK(t.Format(_T("%A, %B %#d")) == _T("Tuesday, July 15"));
CTimeSpan span(1, 2, 3, 4);
CHECK(span.GetTotalSeconds() == 93784);
CHECK(span.Format(_T("%D:%H:%M:%S")) == _T("1:02:03:04"));
CTime u = t + span;
CHECK(u - t == span);
CHECK(u.GetDay() == 16 && u.GetHour() == 15);
CHECK(CTime::GetCurrentTime() > t);
// CFileTime: 100-nanosecond units, and spans of them.
CFileTime ft(t.GetTime() * CFileTime::Second + 116444736000000000ULL);
CHECK(CTime(ft).GetTime() == t.GetTime()); // a FILETIME, so CTime takes it
CFileTime ft2 = ft + CFileTimeSpan(CFileTime::Day + 5 * CFileTime::Millisecond);
CHECK((ft2 - ft).GetTimeSpan() == (LONGLONG)(CFileTime::Day + 50000));
CHECK(ft2 > ft && ft != ft2);
ft2 -= CFileTimeSpan(CFileTime::Day);
CHECK(ft2.GetTime() == ft.GetTime() + 50000);
CHECK(ft.UTCToLocal().LocalToUTC() == ft);
CFileTime now = CFileTime::GetCurrentTime();
CHECK(now > ft);
CHECK(CFileTime::Week == 7 * 24 * 3600 * 10000000ULL);
ATL::CFileTime copy = now;
CHECK(copy == now);
SYSTEMTIME st;
CHECK(::FileTimeToSystemTime(&ft, &st) && st.wYear == 2003);
}
static void TestImage() {
TCHAR szDir[MAX_PATH], szPng[MAX_PATH], szBmp[MAX_PATH], szJpg[MAX_PATH];
::GetTempPath(MAX_PATH, szDir);
wsprintf(szPng, TEXT("%satlimg.png"), szDir);
wsprintf(szBmp, TEXT("%satlimg.bmp"), szDir);
wsprintf(szJpg, TEXT("%satlimg.jpg"), szDir);
// A 32-bit image with an alpha channel, through PNG and back.
CImage img;
CHECK(img.IsNull());
CHECK(img.Create(8, 6, 32, CImage::createAlphaChannel));
CHECK(!img.IsNull() && img.IsDIBSection() && !img.IsIndexed());
CHECK(img.GetWidth() == 8 && img.GetHeight() == 6 && img.GetBPP() == 32);
CHECK(img.GetPitch() == -32); // bottom-up: GetBits is the top row
for (int y = 0; y < 6; y++)
for (int x = 0; x < 8; x++)
*(DWORD*)img.GetPixelAddress(x, y) = (x < 4 ? 0xFF000000u : 0x80000000u) |
(DWORD)(x * 30) << 16 | (DWORD)(y * 40) << 8 | 0x11;
CHECK((img.GetPixel(2, 3) & 0xFFFFFF) == RGB(60, 120, 0x11));
CHECK(SUCCEEDED(img.Save(szPng))); // the format from the extension
CImage png;
CHECK(SUCCEEDED(png.Load(szPng)));
CHECK(png.GetWidth() == 8 && png.GetHeight() == 6 && png.GetBPP() == 32);
CHECK(*(DWORD*)png.GetPixelAddress(2, 3) == *(DWORD*)img.GetPixelAddress(2, 3));
CHECK(*(DWORD*)png.GetPixelAddress(6, 5) == *(DWORD*)img.GetPixelAddress(6, 5));
CHECK((*(DWORD*)png.GetPixelAddress(6, 5) >> 24) == 0x80); // alpha kept, unpremultiplied
// Drawn onto white: the opaque half as it is, the other half blended.
CImage dest;
CHECK(dest.Create(8, 6, 24));
HDC hdc = dest.GetDC();
RECT rc = {0, 0, 8, 6};
::FillRect(hdc, &rc, (HBRUSH)::GetStockObject(WHITE_BRUSH));
CHECK(png.Draw(hdc, 0, 0));
dest.ReleaseDC();
CHECK(dest.GetPixel(1, 1) == RGB(30, 40, 0x11));
// At (5, 0), (150, 0, 0x11) at alpha 0x80: AlphaBlend takes the pixels
// as premultiplied, so white shows through by half. (Red, 150 + 127,
// is out of range, as it is for unpremultiplied pixels on Windows.)
COLORREF c = dest.GetPixel(5, 0);
CHECK(GetGValue(c) >= 0x7E && GetGValue(c) <= 0x80);
CHECK(GetBValue(c) >= 0x8F && GetBValue(c) <= 0x91);
// 24 bits, through BMP and JPEG, saved by GUID; a stream too.
CImage rgb;
CHECK(rgb.Create(5, -4, 24)); // top-down
CHECK(rgb.GetPitch() == 16);
rgb.SetPixelRGB(1, 2, 10, 200, 30);
CHECK(rgb.GetPixel(1, 2) == RGB(10, 200, 30));
CHECK(SUCCEEDED(rgb.Save(szBmp, Gdiplus::ImageFormatBMP)));
CHECK(SUCCEEDED(rgb.Save(szJpg, Gdiplus::ImageFormatJPEG)));
CImage bmp;
CHECK(SUCCEEDED(bmp.Load(szBmp)));
CHECK(bmp.GetBPP() == 24 && bmp.GetPixel(1, 2) == RGB(10, 200, 30));
CImage jpg;
CHECK(SUCCEEDED(jpg.Load(szJpg)) && jpg.GetWidth() == 5 && jpg.GetHeight() == 4);
IStream* pStream = NULL;
CHECK(SUCCEEDED(::CreateStreamOnHGlobal(NULL, TRUE, &pStream)));
CHECK(SUCCEEDED(rgb.Save(pStream, Gdiplus::ImageFormatPNG)));
LARGE_INTEGER zero = {};
pStream->Seek(zero, STREAM_SEEK_SET, NULL);
CImage fromStream;
CHECK(SUCCEEDED(fromStream.Load(pStream)));
CHECK(fromStream.GetPixel(1, 2) == RGB(10, 200, 30));
pStream->Release();
CHECK(FAILED(fromStream.Save(szPng, GUID_NULL)) == false);
CImage missing;
CHECK(FAILED(missing.Load(TEXT("no such file.png"))) && missing.IsNull());
// An 8-bit indexed image: its color table, SetPixelIndexed, and a
// transparent color for Draw.
CImage ind;
CHECK(ind.Create(4, 4, 8));
CHECK(ind.IsIndexed() && ind.GetMaxColorTableEntries() == 256);
RGBQUAD q[2] = {{0, 0, 0xFF, 0}, {0xFF, 0, 0, 0}}; // red, blue
ind.SetColorTable(0, 2, q);
RGBQUAD back[2];
ind.GetColorTable(0, 2, back);
CHECK(back[0].rgbRed == 0xFF && back[1].rgbBlue == 0xFF);
for (int y = 0; y < 4; y++)
for (int x = 0; x < 4; x++) ind.SetPixelIndexed(x, y, x < 2 ? 0 : 1);
CHECK(ind.GetPixel(0, 0) == RGB(0xFF, 0, 0) && ind.GetPixel(3, 3) == RGB(0, 0, 0xFF));
CHECK(ind.SetTransparentColor(0) == -1 && ind.GetTransparentColor() == 0);
CImage canvas;
CHECK(canvas.Create(4, 4, 24));
hdc = canvas.GetDC();
RECT rc4 = {0, 0, 4, 4};
::FillRect(hdc, &rc4, (HBRUSH)::GetStockObject(WHITE_BRUSH));
CHECK(ind.Draw(hdc, 0, 0));
canvas.ReleaseDC();
CHECK(canvas.GetPixel(0, 0) == RGB(0xFF, 0xFF, 0xFF)); // red was transparent
CHECK(canvas.GetPixel(3, 0) == RGB(0, 0, 0xFF));
CHECK(SUCCEEDED(ind.Save(szBmp)));
CImage ind2;
CHECK(SUCCEEDED(ind2.Load(szBmp)) && ind2.GetBPP() == 8);
CHECK(ind2.GetPixel(3, 3) == RGB(0, 0, 0xFF));
// GIF: an indexed image keeps its colors, and so does a large one of
// all 256, noisy enough that the LZW table fills and starts over.
TCHAR szGif[MAX_PATH];
wsprintf(szGif, TEXT("%satlimg.gif"), szDir);
CHECK(SUCCEEDED(ind.Save(szGif)));
CImage gif;
CHECK(SUCCEEDED(gif.Load(szGif)) && gif.GetWidth() == 4 && gif.GetHeight() == 4);
CHECK(gif.GetPixel(0, 0) == RGB(0xFF, 0, 0) && gif.GetPixel(3, 3) == RGB(0, 0, 0xFF));
gif.Destroy();
CImage noise;
CHECK(noise.Create(97, 61, 8));
RGBQUAD pal[256];
for (int i = 0; i < 256; i++) {
pal[i].rgbRed = (BYTE)i;
pal[i].rgbGreen = (BYTE)(255 - i);
pal[i].rgbBlue = (BYTE)(i * 7);
pal[i].rgbReserved = 0;
}
noise.SetColorTable(0, 256, pal);
UINT seed = 12345;
for (int y = 0; y < 61; y++)
for (int x = 0; x < 97; x++) {
seed = seed * 1103515245u + 12345u;
noise.SetPixelIndexed(x, y, (int)((seed >> 16) & 0xFF));
}
CHECK(SUCCEEDED(noise.Save(szGif, Gdiplus::ImageFormatGIF)));
CHECK(SUCCEEDED(gif.Load(szGif)) && gif.GetWidth() == 97 && gif.GetHeight() == 61);
int nDiff = 0;
for (int y = 0; y < 61; y++)
for (int x = 0; x < 97; x++)
if (gif.GetPixel(x, y) != noise.GetPixel(x, y)) nDiff++;
CHECK(nDiff == 0);
gif.Destroy();
#ifdef _WINPORT
// Here a true-color image of 256 colors or fewer keeps them exactly,
// one of more comes through a color cube, and a pixel of no alpha is
// transparent. (GDI+ itself takes every true-color image through its
// halftone palette.)
CHECK(SUCCEEDED(rgb.Save(szGif, Gdiplus::ImageFormatGIF)));
CHECK(SUCCEEDED(gif.Load(szGif)) && gif.GetWidth() == 5 && gif.GetHeight() == 4);
CHECK(gif.GetPixel(1, 2) == RGB(10, 200, 30) && gif.GetPixel(0, 0) == RGB(0, 0, 0));
gif.Destroy();
CImage ramp;
CHECK(ramp.Create(32, 32, 32, CImage::createAlphaChannel));
for (int y = 0; y < 32; y++)
for (int x = 0; x < 32; x++)
*(DWORD*)ramp.GetPixelAddress(x, y) = 0xFF000000u | (DWORD)(x * 8) << 16 | (DWORD)(y * 8) << 8 | 0x40;
*(DWORD*)ramp.GetPixelAddress(0, 0) = 0;
CHECK(SUCCEEDED(ramp.Save(szGif, Gdiplus::ImageFormatGIF)));
CHECK(SUCCEEDED(gif.Load(szGif)) && gif.GetBPP() == 32);
CHECK((*(DWORD*)gif.GetPixelAddress(0, 0) >> 24) == 0);
COLORREF cr = gif.GetPixel(20, 10), want = ramp.GetPixel(20, 10);
CHECK(abs(GetRValue(cr) - GetRValue(want)) <= 26 && abs(GetGValue(cr) - GetGValue(want)) <= 22 &&
abs(GetBValue(cr) - GetBValue(want)) <= 26);
CHECK((*(DWORD*)gif.GetPixelAddress(20, 10) >> 24) == 0xFF);
gif.Destroy();
#endif
::DeleteFile(szGif);
// Attach and Detach, and a bitmap that is not a DIB section.
HBITMAP h = img.Detach();
CHECK(img.IsNull() && h != NULL);
img.Attach(h);
CHECK(img.GetWidth() == 8 && img.GetPitch() == -32);
HDC hScreen = ::GetDC(NULL);
HBITMAP hddb = ::CreateCompatibleBitmap(hScreen, 3, 3);
::ReleaseDC(NULL, hScreen);
CImage ddb;
ddb.Attach(hddb);
CHECK(!ddb.IsDIBSection() && ddb.GetWidth() == 3 && ddb.GetBits() == NULL);
CHECK(SUCCEEDED(ddb.Save(szBmp)));
// The file dialogs' filter strings.
CString strFilter;
CSimpleArray<GUID> guids;
CHECK(SUCCEEDED(CImage::GetImporterFilterString(strFilter, guids, TEXT("All Image Files"))));
CHECK(strFilter.Find(TEXT("All Image Files (")) == 0);
CHECK(strFilter.Find(TEXT("*.PNG")) > 0 && strFilter.Right(2) == TEXT("||"));
CHECK(guids.GetSize() > 4 && guids[0] == GUID_NULL);
CString strSave;
CSimpleArray<GUID> saveGuids;
CHECK(SUCCEEDED(CImage::GetExporterFilterString(strSave, saveGuids, NULL,
CImage::excludeDefaultSave | CImage::excludeGIF)));
CHECK(strSave.Find(TEXT("GIF")) < 0 && strSave.Find(TEXT("*.BMP")) >= 0);
CHECK(saveGuids.Find(Gdiplus::ImageFormatPNG) >= 0);
::DeleteFile(szPng);
::DeleteFile(szBmp);
::DeleteFile(szJpg);
}
#ifdef _WINPORT
// An animated GIF through GDI+'s flat API: frames saved one by one with
// their delays and loop count, read back along FrameDimensionTime, saved
// again whole, and one not flushed written when its image goes.
static Gdiplus::GpBitmap* Solid(Gdiplus::ARGB color) {
using namespace Gdiplus::DllExports;
Gdiplus::GpBitmap* b = NULL;
GdipCreateBitmapFromScan0(6, 4, 0, PixelFormat32bppARGB, NULL, &b);
for (int y = 0; y < 4; y++)
for (int x = 0; x < 6; x++) GdipBitmapSetPixel(b, x, y, x == 0 && y == 0 ? 0 : color);
return b;
}
static void TestAnimatedGif() {
using namespace Gdiplus;
using namespace Gdiplus::DllExports;
ULONG_PTR token;
GdiplusStartupInput input;
CHECK(GdiplusStartup(&token, &input, NULL) == Ok);
WCHAR dir[MAX_PATH], file[MAX_PATH], again[MAX_PATH], loose[MAX_PATH];
::GetTempPathW(MAX_PATH, dir);
wsprintfW(file, L"%sanim.gif", dir);
wsprintfW(again, L"%sanim2.gif", dir);
wsprintfW(loose, L"%sanim3.gif", dir);
UINT n = 0, size = 0;
CHECK(GdipGetImageEncodersSize(&n, &size) == Ok);
std::vector<BYTE> buf(size);
ImageCodecInfo* codecs = (ImageCodecInfo*)buf.data();
CHECK(GdipGetImageEncoders(n, size, codecs) == Ok);
CLSID gif = GUID_NULL;
for (UINT i = 0; i < n; i++)
if (codecs[i].FormatID == ImageFormatGIF) gif = codecs[i].Clsid;
GpBitmap* first = Solid(0xFFFF0000);
GpBitmap* second = Solid(0xFF00FF00);
GpBitmap* third = Solid(0xFF0000FF);
LONG delays[3] = {10, 20, 30};
PropertyItem delay = {PropertyTagFrameDelay, sizeof delays, PropertyTagTypeLong, delays};
CHECK(GdipSetPropertyItem(first, &delay) == Ok);
SHORT loops = 2;
PropertyItem loop = {PropertyTagLoopCount, sizeof loops, PropertyTagTypeShort, &loops};
CHECK(GdipSetPropertyItem(first, &loop) == Ok);
LONG d2 = 20, d3 = 30;
PropertyItem delay2 = {PropertyTagFrameDelay, 4, PropertyTagTypeLong, &d2};
PropertyItem delay3 = {PropertyTagFrameDelay, 4, PropertyTagTypeLong, &d3};
CHECK(GdipSetPropertyItem(second, &delay2) == Ok && GdipSetPropertyItem(third, &delay3) == Ok);
EncoderParameters p;
p.Count = 1;
p.Parameter[0].Guid = EncoderSaveFlag;
p.Parameter[0].NumberOfValues = 1;
p.Parameter[0].Type = EncoderParameterValueTypeLong;
ULONG flag = EncoderValueMultiFrame;
p.Parameter[0].Value = &flag;
CHECK(GdipSaveAdd(first, &p) == WrongState); // nothing under way yet
CHECK(GdipSaveImageToFile(first, file, &gif, &p) == Ok);
flag = EncoderValueFrameDimensionTime;
CHECK(GdipSaveAddImage(first, second, &p) == Ok);
CHECK(GdipSaveAddImage(first, third, &p) == Ok);
flag = EncoderValueFlush;
CHECK(GdipSaveAdd(first, &p) == Ok);
GpImage* img = NULL;
CHECK(GdipLoadImageFromFile(file, &img) == Ok);
UINT dims = 0, frames = 0;
GUID dim;
CHECK(GdipImageGetFrameDimensionsCount(img, &dims) == Ok && dims == 1);
CHECK(GdipImageGetFrameDimensionsList(img, &dim, 1) == Ok && dim == FrameDimensionTime);
CHECK(GdipImageGetFrameCount(img, &FrameDimensionTime, &frames) == Ok && frames == 3);
UINT psize = 0;
CHECK(GdipGetPropertyItemSize(img, PropertyTagFrameDelay, &psize) == Ok && psize == sizeof(PropertyItem) + 12);
std::vector<BYTE> pbuf(psize);
PropertyItem* got = (PropertyItem*)pbuf.data();
CHECK(GdipGetPropertyItem(img, PropertyTagFrameDelay, psize, got) == Ok && got->type == PropertyTagTypeLong);
CHECK(((LONG*)got->value)[0] == 10 && ((LONG*)got->value)[1] == 20 && ((LONG*)got->value)[2] == 30);
CHECK(GdipGetPropertyItemSize(img, PropertyTagLoopCount, &psize) == Ok);
pbuf.resize(psize);
got = (PropertyItem*)pbuf.data();
CHECK(GdipGetPropertyItem(img, PropertyTagLoopCount, psize, got) == Ok && *(SHORT*)got->value == 2);
ARGB c = 0;
CHECK(GdipBitmapGetPixel((GpBitmap*)img, 3, 2, &c) == Ok && c == 0xFFFF0000);
CHECK(GdipBitmapGetPixel((GpBitmap*)img, 0, 0, &c) == Ok && (c >> 24) == 0);
CHECK(GdipImageSelectActiveFrame(img, &FrameDimensionTime, 2) == Ok);
CHECK(GdipBitmapGetPixel((GpBitmap*)img, 3, 2, &c) == Ok && c == 0xFF0000FF);
CHECK(GdipImageSelectActiveFrame(img, &FrameDimensionTime, 1) == Ok);
CHECK(GdipBitmapGetPixel((GpBitmap*)img, 3, 2, &c) == Ok && c == 0xFF00FF00);
CHECK(GdipImageSelectActiveFrame(img, &FrameDimensionTime, 3) == InvalidParameter);
// Saved as a GIF again, it keeps its frames.
CHECK(GdipSaveImageToFile(img, again, &gif, NULL) == Ok);
GpImage* img2 = NULL;
CHECK(GdipLoadImageFromFile(again, &img2) == Ok);
CHECK(GdipImageGetFrameCount(img2, &FrameDimensionTime, &frames) == Ok && frames == 3);
GdipDisposeImage(img2);
GdipDisposeImage(img);
// A still image has one page.
CHECK(GdipImageGetFrameDimensionsList(second, &dim, 1) == Ok && dim == FrameDimensionPage);
CHECK(GdipImageGetFrameCount(second, &FrameDimensionPage, &frames) == Ok && frames == 1);
CHECK(GdipGetPropertyItemSize(second, PropertyTagLoopCount, &psize) == PropertyNotFound);
CHECK(GdipRemovePropertyItem(second, PropertyTagFrameDelay) == Ok);
UINT props = 9;
CHECK(GdipGetPropertyCount(second, &props) == Ok && props == 0);
// Not flushed: written when the image goes.
flag = EncoderValueMultiFrame;
CHECK(GdipSaveImageToFile(second, loose, &gif, &p) == Ok);
flag = EncoderValueFrameDimensionTime;
CHECK(GdipSaveAddImage(second, third, &p) == Ok);
GdipDisposeImage(second);
CHECK(GdipLoadImageFromFile(loose, &img) == Ok);
CHECK(GdipImageGetFrameCount(img, &FrameDimensionTime, &frames) == Ok && frames == 2);
GdipDisposeImage(img);
// CImage takes the first frame.
CImage ci;
CHECK(SUCCEEDED(ci.Load(CString(file))) && ci.GetWidth() == 6 && ci.GetPixel(3, 2) == RGB(255, 0, 0));
ci.Destroy();
GdipDisposeImage(first);
GdipDisposeImage(third);
::DeleteFileW(file);
::DeleteFileW(again);
::DeleteFileW(loose);
GdiplusShutdown(token);
}
#endif
static void TestComTime() {
// COleDateTime and COleDateTimeSpan without MFC.
COleDateTime dt(2003, 7, 15, 13, 45, 30);
CHECK(dt.GetStatus() == COleDateTime::valid);
CHECK(dt.GetYear() == 2003 && dt.GetMonth() == 7 && dt.GetDay() == 15);
CHECK(dt.GetHour() == 13 && dt.GetMinute() == 45 && dt.GetSecond() == 30);
CHECK(dt.GetDayOfWeek() == 3 && dt.GetDayOfYear() == 196);
CHECK(dt.Format(_T("%Y-%m-%d %H:%M:%S")) == _T("2003-07-15 13:45:30"));
COleDateTimeSpan span(1, 2, 3, 4);
CHECK(span.GetDays() == 1 && span.GetHours() == 2 && span.GetMinutes() == 3 &&
span.GetSeconds() == 4);
CHECK(span.Format(_T("%D %H:%M:%S")) == _T("1 02:03:04"));
COleDateTime dt2 = dt + span;
CHECK(dt2.GetDay() == 16 && dt2.GetHour() == 15);
CHECK(dt2 > dt && (dt2 - dt).GetTotalSeconds() > 93783.9);
COleDateTime bad(2003, 2, 30, 0, 0, 0);
CHECK(bad.GetStatus() == COleDateTime::invalid);
CHECK(COleDateTime(1899, 12, 30, 0, 0, 0).m_dt == 0.0);
COleDateTime parsed;
CHECK(parsed.ParseDateTime(_T("2003-07-15 13:45:30"), 0, LANG_USER_DEFAULT));
CHECK(parsed.GetYear() == 2003 && parsed.GetMinute() == 45);
SYSTEMTIME st;
CHECK(dt.GetAsSystemTime(st) && st.wYear == 2003 && st.wDayOfWeek == 2);
CHECK(COleDateTime::GetCurrentTime().GetYear() >= 2003);
ATL::COleDateTime copy = dt;
CHECK(copy == dt);
}
static DWORD WINAPI SyncThread(LPVOID pv) {
ATL::CEvent* pEvents = (ATL::CEvent*)pv;
::WaitForSingleObject(pEvents[0], INFINITE);
pEvents[1].Set();
return 0;
}
static void TestSync() {
ATL::CCriticalSection cs;
cs.Enter();
CHECK(cs.TryEnter()); // recursive
cs.Leave();
cs.Leave();
ATL::CEvent events[2];
CHECK(events[0].Create(NULL, FALSE, FALSE, NULL) && events[1].Create(NULL, TRUE, FALSE, NULL));
HANDLE hThread = ::CreateThread(NULL, 0, SyncThread, events, 0, NULL);
CHECK(::WaitForSingleObject(events[1], 0) == WAIT_TIMEOUT);
events[0].Set();
CHECK(::WaitForSingleObject(events[1], 5000) == WAIT_OBJECT_0);
CHECK(::WaitForSingleObject(events[1], 0) == WAIT_OBJECT_0); // manual reset
events[1].Reset();
CHECK(::WaitForSingleObject(events[1], 0) == WAIT_TIMEOUT);
::WaitForSingleObject(hThread, 5000);
::CloseHandle(hThread);
ATL::CMutex mtx(FALSE);
{
ATL::CMutexLock lock(mtx);
CHECK(::WaitForSingleObject(mtx, 0) == WAIT_OBJECT_0); // owner may take it again
mtx.Release();
}
ATL::CMutex named;
CHECK(named.Create(NULL, FALSE, _T("atlutiltest-mutex")));
ATL::CMutex opened;
CHECK(opened.Open(SYNCHRONIZE, FALSE, _T("atlutiltest-mutex")));
ATL::CSemaphore sem(2, 3);
CHECK(::WaitForSingleObject(sem, 0) == WAIT_OBJECT_0);
CHECK(::WaitForSingleObject(sem, 0) == WAIT_OBJECT_0);
CHECK(::WaitForSingleObject(sem, 0) == WAIT_TIMEOUT);
LONG nOld = -1;
CHECK(sem.Release(1, &nOld) && nOld == 0);
bool bThrown = false;
try {
ATL::CSemaphore badSem(5, 1); // more than the most
} catch (CAtlException& e) {
bThrown = FAILED((HRESULT)e);
}
CHECK(bThrown);
}
static void TestEncodings() {
// Base64, with and without line breaks and padding.
const BYTE abData[] = "Many hands make light work.";
int nLen = (int)sizeof(abData) - 1;
char sz[256];
int n = sizeof(sz);
CHECK(Base64Encode(abData, nLen, sz, &n, ATL_BASE64_FLAG_NOCRLF));
CHECK(n == 36 && memcmp(sz, "TWFueSBoYW5kcyBtYWtlIGxpZ2h0IHdvcmsu", 36) == 0);
n = sizeof(sz);
CHECK(Base64Encode((const BYTE*)"ab", 2, sz, &n, ATL_BASE64_FLAG_NOCRLF) && n == 4 &&
memcmp(sz, "YWI=", 4) == 0);
n = sizeof(sz);
CHECK(Base64Encode((const BYTE*)"ab", 2, sz, &n, ATL_BASE64_FLAG_NOCRLF | ATL_BASE64_FLAG_NOPAD) &&
n == 3);
n = sizeof(sz);
CHECK(Base64Encode(abData, nLen, sz, &n) && n == 38 && sz[36] == '\r' && sz[37] == '\n');
CHECK(Base64EncodeGetRequiredLength(nLen) >= n);
BYTE ab[256];
int nOut = sizeof(ab);
CHECK(Base64Decode(sz, n, ab, &nOut) && nOut == nLen && memcmp(ab, abData, nLen) == 0);
int nSmall = 0;
CHECK(!Base64Encode(abData, nLen, sz, &nSmall) && nSmall >= 36);
// Hex.
n = sizeof(sz);
CHECK(AtlHexEncode((const BYTE*)"\x01\xAB\xff", 3, sz, &n) && n == 6 && memcmp(sz, "01ABFF", 6) == 0);
nOut = sizeof(ab);
CHECK(AtlHexDecode("01abFF", 6, ab, &nOut) && nOut == 3 && ab[1] == 0xab && ab[2] == 0xff);
// UUEncode, round trip.
static char szUU[1024];
n = sizeof(szUU);
CHECK(UUEncode(abData, nLen, szUU, &n, _T("work.txt"), ATLSMTP_UUENCODE_HEADER | ATLSMTP_UUENCODE_END));
CHECK(n > 0 && memcmp(szUU, "begin 666 work.txt\r\n", 20) == 0);
CHECK(szUU[20] == (char)(0x20 + nLen));
nOut = sizeof(ab);
CHECK(UUDecode((BYTE*)szUU, n, ab, &nOut) && nOut == nLen && memcmp(ab, abData, nLen) == 0);
// Quoted-printable.
const char* pszQP = "caf\xe9 = ok \r\nnext";
n = sizeof(sz);
CHECK(QPEncode((BYTE*)pszQP, (int)strlen(pszQP), sz, &n));
sz[n] = 0;
CHECK(strcmp(sz, "caf=E9 =3D ok=20\r\nnext") == 0);
char szBack[64];
nOut = sizeof(szBack);
CHECK(QPDecode((BYTE*)sz, n, szBack, &nOut) && nOut == (int)strlen(pszQP) &&
memcmp(szBack, pszQP, nOut) == 0);
// The B encoding, XML escaping, UTF-8.
n = sizeof(sz);
CHECK(BEncode((BYTE*)"hi", 2, sz, &n, "utf-8") && n == 16 && memcmp(sz, "=?utf-8?B?aGk=?=", 16) == 0);
wchar_t wsz[64];
int nEsc = EscapeXML(L"<a href=\"x\">&</a>", 17, wsz, 64, ATL_ESC_FLAG_ATTR);
CHECK(nEsc > 0 && wcsncmp(wsz, L"&lt;a href=&quot;x&quot;&gt;&amp;&lt;/a&gt;", nEsc) == 0);
char szUtf8[16];
int nU = AtlUnicodeToUTF8(L"\x00e9", 1, szUtf8, 16);
CHECK(nU == 2 && (BYTE)szUtf8[0] == 0xc3 && (BYTE)szUtf8[1] == 0xa9);
CHECK(AtlHexValue('c') == 12 && AtlHexValue('x') == -1);
}
static void TestCrypt() {
CCryptProv prov;
HRESULT hr = prov.InitVerifyContext();
CHECK(SUCCEEDED(hr));
if (FAILED(hr)) return;
BYTE abRandom[16] = {0};
CHECK(SUCCEEDED(prov.GenRandom(sizeof(abRandom), abRandom)));
// MD5 and SHA-1 of "abc", from RFC 1321 and FIPS 180.
CCryptMD5Hash md5;
CHECK(SUCCEEDED(md5.Initialize(prov)));
CHECK(SUCCEEDED(md5.AddData((const BYTE*)"abc", 3)));
BYTE ab[64];
DWORD dw = sizeof(ab);
CHECK(SUCCEEDED(md5.GetValue(ab, &dw)) && dw == 16);
static const BYTE md5abc[] = {0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0,
0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72};
CHECK(memcmp(ab, md5abc, 16) == 0);
CCryptSHA1Hash sha;
CHECK(SUCCEEDED(sha.Initialize(prov)));
CHECK(SUCCEEDED(sha.AddData((const BYTE*)"abc", 3)));
DWORD dwSize = 0;
CHECK(SUCCEEDED(sha.GetSize(&dwSize)) && dwSize == 20);
dw = sizeof(ab);
CHECK(SUCCEEDED(sha.GetValue(ab, &dw)) && dw == 20 && ab[0] == 0xa9 && ab[1] == 0x99 &&
ab[19] == 0x9d);
ALG_ID alg = 0;
CHECK(SUCCEEDED(sha.GetAlgId(&alg)) && alg == CALG_SHA1);
// A key derived from a hash: encrypt, then decrypt.
CCryptMD5Hash keyHash;
CHECK(SUCCEEDED(keyHash.Initialize(prov, _T("password"))));
CCryptDerivedKey key;
hr = key.Initialize(prov, keyHash, CALG_RC4);
CHECK(SUCCEEDED(hr));
if (SUCCEEDED(hr)) {
BYTE abBuf[64];
memcpy(abBuf, "secret message", 14);
DWORD dwLen = 14;
CHECK(SUCCEEDED(key.Encrypt(TRUE, abBuf, &dwLen, sizeof(abBuf))));
CHECK(memcmp(abBuf, "secret message", 14) != 0);
CCryptDerivedKey key2;
CCryptMD5Hash keyHash2;
keyHash2.Initialize(prov, _T("password"));
CHECK(SUCCEEDED(key2.Initialize(prov, keyHash2, CALG_RC4)));
CHECK(SUCCEEDED(key2.Decrypt(TRUE, abBuf, &dwLen)) && dwLen == 14 &&
memcmp(abBuf, "secret message", 14) == 0);
}
CCryptHash copy = sha; // duplicated
CHECK(copy.GetHandle() != 0 && copy.GetHandle() != sha.GetHandle());
}
// A 512-bit key pair made outside CryptoAPI, as a PRIVATEKEYBLOB; its
// PKCS #1 v1.5 signature of SHA-1("abc"), and "hello" encrypted with its
// public half, both least significant byte first.
static const BYTE kRsaPair[] = {
0x07, 0x02, 0x00, 0x00, 0x00, 0xa4, 0x00, 0x00, 0x52, 0x53, 0x41, 0x32,
0x00, 0x02, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x4d, 0x50, 0x1b, 0x2c,
0x8b, 0x67, 0x7b, 0x32, 0x2a, 0xd7, 0x30, 0xb0, 0xc7, 0x38, 0xdf, 0x49,
0xa2, 0xbb, 0xef, 0xaa, 0xb7, 0x84, 0xcb, 0xaa, 0xb4, 0x3f, 0x38, 0x1f,
0xa2, 0x3b, 0xe7, 0xf1, 0x4f, 0x8d, 0x28, 0x50, 0xde, 0x1a, 0x5b, 0x1e,
0xbe, 0x5c, 0x31, 0x01, 0x07, 0x46, 0xa0, 0x38, 0x4a, 0xff, 0x57, 0x13,
0xa3, 0x39, 0x2e, 0x66, 0xe7, 0xd5, 0x22, 0x28, 0x04, 0x16, 0xdb, 0xb2,
0x07, 0xff, 0x94, 0x4a, 0xc3, 0x19, 0xdf, 0xaa, 0xfe, 0x34, 0xd4, 0x35,
0x89, 0x5b, 0x95, 0x26, 0xe0, 0xe0, 0xa3, 0x80, 0xc0, 0xb5, 0x1d, 0x0e,
0xae, 0x7d, 0x98, 0x2b, 0xbb, 0xc1, 0x5a, 0xd8, 0x0b, 0x0d, 0xf7, 0x31,
0x1d, 0x25, 0x2a, 0x11, 0x95, 0x0d, 0x0b, 0x92, 0x96, 0x3a, 0x29, 0xca,
0x98, 0x72, 0x53, 0x01, 0x42, 0x67, 0xd5, 0x1b, 0xc7, 0x26, 0x00, 0x66,
0x63, 0x41, 0xa1, 0xd3, 0x21, 0xad, 0x57, 0x50, 0xd1, 0x95, 0x4a, 0xc6,
0xe3, 0x86, 0x27, 0xb2, 0x39, 0xdb, 0x66, 0x8e, 0x03, 0x6e, 0xe1, 0xc9,
0x84, 0x07, 0xab, 0xc6, 0xb1, 0xbc, 0xca, 0x1b, 0x14, 0x6a, 0x43, 0x67,
0x9d, 0x5b, 0x4c, 0xc1, 0xdd, 0xae, 0x65, 0x04, 0x00, 0xa2, 0x9c, 0x76,
0x01, 0xc3, 0x55, 0xe6, 0xf4, 0x1a, 0x66, 0xd6, 0x32, 0xa8, 0x06, 0x7c,
0xe0, 0x8d, 0x80, 0x98, 0x0f, 0xfd, 0x9e, 0xbb, 0x85, 0x62, 0x26, 0x6b,
0x59, 0x89, 0x47, 0x3f, 0x6a, 0xac, 0x2a, 0x65, 0xab, 0x44, 0x8a, 0xa1,
0x76, 0x8e, 0xca, 0x69, 0x37, 0x97, 0x37, 0x35, 0x53, 0x5a, 0x4f, 0xbb,
0x09, 0x94, 0xba, 0x87, 0x31, 0xd4, 0x16, 0x7e, 0xad, 0xc7, 0x3e, 0x85,
0x45, 0xe0, 0x53, 0xf0, 0xca, 0x5a, 0xc4, 0x66, 0x08, 0xec, 0x56, 0x98,
0x62, 0x1c, 0x33, 0x01, 0x3c, 0xb7, 0x6d, 0x0d, 0xea, 0xac, 0xa0, 0x60,
0x84, 0xdb, 0x23, 0x16, 0xbe, 0x0d, 0xc4, 0xc3, 0x0f, 0x62, 0xa7, 0x18,
0x86, 0x8f, 0x58, 0x5d, 0x90, 0x75, 0xdc, 0x27, 0x81, 0xbf, 0x5b, 0x42,
0x4e, 0x9f, 0x21, 0x93, 0x1c, 0x87, 0x52, 0x71,
};
static const BYTE kRsaSigAbc[] = {
0x5f, 0xa3, 0xb3, 0x09, 0x5d, 0xe7, 0xf4, 0x5d, 0x3f, 0x04, 0x65, 0xe6,
0xed, 0x0e, 0x8a, 0xa8, 0xaa, 0xc0, 0x1e, 0x64, 0x83, 0x6f, 0xe9, 0x09,
0x0b, 0xba, 0x13, 0xe4, 0xae, 0x0b, 0x53, 0x5c, 0xc4, 0x41, 0x21, 0xf5,
0x7b, 0xb3, 0x2c, 0x2e, 0x69, 0x1c, 0x07, 0xb7, 0x2d, 0x93, 0xd4, 0x5c,
0xe8, 0xb0, 0x3d, 0xb0, 0xa7, 0x07, 0xa4, 0xb8, 0x93, 0x62, 0x0b, 0x3e,
0x52, 0xd0, 0x69, 0x18,
};
static const BYTE kRsaHello[] = {
0x91, 0x4d, 0x45, 0xf6, 0xa1, 0x65, 0xee, 0x14, 0xef, 0xa4, 0xcf, 0x89,
0x52, 0xb0, 0x7f, 0x18, 0x71, 0x7b, 0x3f, 0x88, 0xbd, 0xad, 0x0a, 0x2d,
0x7d, 0x5a, 0xb1, 0x0e, 0xf7, 0xa5, 0x5e, 0x69, 0x36, 0x5e, 0x53, 0x4b,
0xae, 0xcc, 0x1e, 0x9f, 0x67, 0xb5, 0x16, 0x43, 0xb0, 0x04, 0x70, 0xb6,
0xda, 0xa8, 0x9d, 0x99, 0x81, 0x9c, 0x9d, 0xe9, 0x1a, 0x7e, 0xea, 0x42,
0xdc, 0x52, 0xc9, 0x11,
};
static void TestRsa() {
CCryptProv prov;
HRESULT hr = prov.InitVerifyContext(PROV_RSA_FULL, MS_ENHANCED_PROV);
CHECK(SUCCEEDED(hr));
if (FAILED(hr)) return;
CCryptImportKey pair;
hr = pair.Initialize(prov, (BYTE*)kRsaPair, sizeof(kRsaPair), CCryptKey::EmptyKey, CRYPT_EXPORTABLE);
CHECK(SUCCEEDED(hr));
if (FAILED(hr)) return;
DWORD dw = 0, n = sizeof(dw);
CHECK(SUCCEEDED(pair.GetParam(KP_KEYLEN, (BYTE*)&dw, &n)) && dw == 512);
ALG_ID alg = 0;
n = sizeof(alg);
CHECK(SUCCEEDED(pair.GetParam(KP_ALGID, (BYTE*)&alg, &n)) && alg == CALG_RSA_KEYX);
// It exports as it came in.
BYTE abBlob[400];
n = sizeof(abBlob);
CHECK(SUCCEEDED(pair.ExportPrivateKeyBlob(CCryptKey::EmptyKey, 0, abBlob, &n)) && n == sizeof(kRsaPair) &&
memcmp(abBlob, kRsaPair, n) == 0);
// The signature PKCS #1 gives, with it as the exchange key.
CCryptSHA1Hash sha;
CHECK(SUCCEEDED(sha.Initialize(prov)) && SUCCEEDED(sha.AddData((const BYTE*)"abc", 3)));
BYTE abSig[64];
DWORD dwSig = 0;
CHECK(SUCCEEDED(sha.Sign(NULL, &dwSig, 0, AT_KEYEXCHANGE)) && dwSig == 64);
CHECK(SUCCEEDED(sha.Sign(abSig, &dwSig, 0, AT_KEYEXCHANGE)) && dwSig == 64 &&
memcmp(abSig, kRsaSigAbc, 64) == 0);
// Its public half checks that, and refuses it for other data.
BYTE abPub[200];
DWORD dwPub = sizeof(abPub);
CHECK(SUCCEEDED(pair.ExportPublicKeyBlob(CCryptKey::EmptyKey, 0, abPub, &dwPub)) && dwPub == 84);
CHECK(abPub[0] == PUBLICKEYBLOB && memcmp(abPub + 8, "RSA1", 4) == 0 &&
memcmp(abPub + 12, kRsaPair + 12, 72) == 0);
CCryptImportKey pub;
CHECK(SUCCEEDED(pub.Initialize(prov, abPub, dwPub, CCryptKey::EmptyKey, 0)));
CCryptSHA1Hash shaOk, shaBad;
CHECK(SUCCEEDED(shaOk.Initialize(prov)) && SUCCEEDED(shaOk.AddData((const BYTE*)"abc", 3)));
CHECK(SUCCEEDED(shaOk.VerifySignature(abSig, dwSig, pub)));
CHECK(SUCCEEDED(shaBad.Initialize(prov)) && SUCCEEDED(shaBad.AddData((const BYTE*)"abd", 3)));
CHECK(shaBad.VerifySignature(abSig, dwSig, pub) == NTE_BAD_SIGNATURE);
// What was encrypted elsewhere decrypts; and a round trip through the
// public half, with PKCS #1 v1.5 and with OAEP.
BYTE abBuf[64];
memcpy(abBuf, kRsaHello, 64);
DWORD dwLen = 64;
CHECK(SUCCEEDED(pair.Decrypt(TRUE, abBuf, &dwLen)) && dwLen == 5 && memcmp(abBuf, "hello", 5) == 0);
memcpy(abBuf, "round trip", 10);
dwLen = 10;
CHECK(SUCCEEDED(pub.Encrypt(TRUE, abBuf, &dwLen, sizeof(abBuf))) && dwLen == 64);
CHECK(memcmp(abBuf, "round trip", 10) != 0);
CHECK(SUCCEEDED(pair.Decrypt(TRUE, abBuf, &dwLen)) && dwLen == 10 && memcmp(abBuf, "round trip", 10) == 0);
memcpy(abBuf, "oaep", 4);
dwLen = 4;
CHECK(::CryptEncrypt(pub.m_hKey, 0, TRUE, CRYPT_OAEP, abBuf, &dwLen, sizeof(abBuf)) && dwLen == 64);
CHECK(::CryptDecrypt(pair.m_hKey, 0, TRUE, CRYPT_OAEP, abBuf, &dwLen) && dwLen == 4 &&
memcmp(abBuf, "oaep", 4) == 0);
dwLen = 60; // more than one block holds
CHECK(!::CryptEncrypt(pub.m_hKey, 0, TRUE, 0, abBuf, &dwLen, sizeof(abBuf)));
// A session key wrapped for the pair, and unwrapped: the same key.
CCryptRandomKey session;
CHECK(SUCCEEDED(session.Initialize(prov, CALG_RC4, CRYPT_EXPORTABLE | (128 << 16))));
BYTE abWrap[128];
DWORD dwWrap = sizeof(abWrap);
CHECK(SUCCEEDED(session.ExportSimpleBlob(pub, 0, abWrap, &dwWrap)) && dwWrap == 76 &&
abWrap[0] == SIMPLEBLOB);
CCryptImportKey unwrapped;
CHECK(SUCCEEDED(unwrapped.Initialize(prov, abWrap, dwWrap, pair, 0)));
BYTE abA[8] = "1234567", abB[8] = "1234567";
DWORD dwA = 7, dwB = 7;
CHECK(SUCCEEDED(session.Encrypt(TRUE, abA, &dwA, 8)) && SUCCEEDED(unwrapped.Encrypt(TRUE, abB, &dwB, 8)));
CHECK(memcmp(abA, abB, 7) == 0 && memcmp(abA, "1234567", 7) != 0);
// A new signature pair of the length asked for.
CCryptKeyPair sigPair;
hr = sigPair.Initialize(prov, AT_SIGNATURE, CRYPT_EXPORTABLE | (1024 << 16));
CHECK(SUCCEEDED(hr));
if (SUCCEEDED(hr)) {
n = sizeof(dw);
CHECK(SUCCEEDED(sigPair.GetParam(KP_KEYLEN, (BYTE*)&dw, &n)) && dw == 1024);
n = sizeof(alg);
CHECK(SUCCEEDED(sigPair.GetParam(KP_ALGID, (BYTE*)&alg, &n)) && alg == CALG_RSA_SIGN);
CCryptMD5Hash md5;
CHECK(SUCCEEDED(md5.Initialize(prov, _T("signed text"))));
BYTE abSig2[128];
DWORD dwSig2 = sizeof(abSig2);
CHECK(SUCCEEDED(md5.Sign(abSig2, &dwSig2)) && dwSig2 == 128);
dwPub = sizeof(abPub);
CHECK(SUCCEEDED(sigPair.ExportPublicKeyBlob(CCryptKey::EmptyKey, 0, abPub, &dwPub)) && dwPub == 148);
CCryptImportKey pub2;
CHECK(SUCCEEDED(pub2.Initialize(prov, abPub, dwPub, CCryptKey::EmptyKey, 0)));
CCryptMD5Hash md5Again;
CHECK(SUCCEEDED(md5Again.Initialize(prov, _T("signed text"))));
CHECK(SUCCEEDED(md5Again.VerifySignature(abSig2, dwSig2, pub2)));
}
// A named container: not there, made, its pair kept and found again
// (and not exportable as it was not made so), then deleted.
LPCTSTR pszName = _T("clang-retro atlutiltest");
HCRYPTPROV hProv = 0;
::CryptAcquireContext(&hProv, pszName, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_DELETEKEYSET);
CHECK(!::CryptAcquireContext(&hProv, pszName, MS_ENHANCED_PROV, PROV_RSA_FULL, 0) &&
::GetLastError() == (DWORD)NTE_BAD_KEYSET);
CHECK(::CryptAcquireContext(&hProv, pszName, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET));
HCRYPTKEY hKey = 0;
CHECK(!::CryptGetUserKey(hProv, AT_KEYEXCHANGE, &hKey) && ::GetLastError() == (DWORD)NTE_NO_KEY);
CHECK(::CryptGenKey(hProv, AT_KEYEXCHANGE, 512 << 16, &hKey));
BYTE abPub1[100], abPub2[100];
DWORD dwPub1 = sizeof(abPub1), dwPub2 = sizeof(abPub2);
CHECK(::CryptExportKey(hKey, 0, PUBLICKEYBLOB, 0, abPub1, &dwPub1) && dwPub1 == 84);
n = sizeof(abBlob);
CHECK(!::CryptExportKey(hKey, 0, PRIVATEKEYBLOB, 0, abBlob, &n) && ::GetLastError() == (DWORD)NTE_BAD_KEY_STATE);
::CryptDestroyKey(hKey);
::CryptReleaseContext(hProv, 0);
CHECK(!::CryptAcquireContext(&hProv, pszName, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET) &&
::GetLastError() == (DWORD)NTE_EXISTS);
CHECK(::CryptAcquireContext(&hProv, pszName, MS_ENHANCED_PROV, PROV_RSA_FULL, 0));
hKey = 0;
CHECK(::CryptGetUserKey(hProv, AT_KEYEXCHANGE, &hKey));
CHECK(::CryptExportKey(hKey, 0, PUBLICKEYBLOB, 0, abPub2, &dwPub2) && dwPub2 == dwPub1 &&
memcmp(abPub1, abPub2, dwPub1) == 0);
::CryptDestroyKey(hKey);
::CryptReleaseContext(hProv, 0);
CHECK(::CryptAcquireContext(&hProv, pszName, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_DELETEKEYSET));
CHECK(!::CryptAcquireContext(&hProv, pszName, MS_ENHANCED_PROV, PROV_RSA_FULL, 0) &&
::GetLastError() == (DWORD)NTE_BAD_KEYSET);
}
static void TestSocketAddr() {
WSADATA wsa;
CHECK(::WSAStartup(MAKEWORD(2, 2), &wsa) == 0);
CSocketAddr addr;
CHECK(addr.FindINET4Addr(_T("127.0.0.1"), 8080, AI_NUMERICHOST) == 0);
addrinfo* pai = addr.GetAddrInfo();
CHECK(pai != NULL && pai->ai_family == AF_INET);
if (pai != NULL) {
sockaddr_in* psin = (sockaddr_in*)pai->ai_addr;
CHECK(ntohs(psin->sin_port) == 8080 && psin->sin_addr.s_addr == htonl(INADDR_LOOPBACK));
}
CHECK(addr.GetAddrInfoList() == pai);
CHECK(addr.FindAddr(_T("not a host name!"), 80, AI_NUMERICHOST, PF_INET, SOCK_STREAM, 0) ==
SOCKET_ERROR);
CHECK(addr.GetAddrInfo() == NULL);
::WSACleanup();
}
static void TestRegExp() {
CAtlRegExp<> re;
CAtlREMatchContext<> mc;
CHECK(re.Parse(_T("{[0-9]+}-{[a-z]+}")) == REPARSE_ERROR_OK);
CHECK(re.GetGroupCount() == 2);
const TCHAR* pszIn = _T("id 42-abc!");
const TCHAR* pszEnd = NULL;
CHECK(re.Match(pszIn, &mc, &pszEnd));
CHECK(mc.m_uNumGroups == 2);
CHECK(mc.m_Match.szStart == pszIn + 3 && mc.m_Match.szEnd == pszIn + 9 && pszEnd == pszIn + 9);
const TCHAR *s, *e;
mc.GetMatch(0, &s, &e);
CHECK(s == pszIn + 3 && e == pszIn + 5);
mc.GetMatch(1, &s, &e);
CHECK(s == pszIn + 6 && e == pszIn + 9);
CHECK(re.Parse(_T("^ab*c$")) == REPARSE_ERROR_OK);
CHECK(re.Match(_T("abbbc"), &mc) && re.Match(_T("ac"), &mc) && !re.Match(_T("xabc"), &mc));
CHECK(re.Parse(_T("(cat|dog)s?")) == REPARSE_ERROR_OK);
CHECK(re.Match(_T("hotdogs"), &mc) && mc.m_Match.szEnd - mc.m_Match.szStart == 4);
CHECK(re.Parse(_T("HELLO"), FALSE) == REPARSE_ERROR_OK && re.Match(_T("say hello"), &mc));
CHECK(re.Parse(_T("{\\d+}\\.{\\d+}")) == REPARSE_ERROR_OK);
CHECK(re.Match(_T("v3.14"), &mc));
mc.GetMatch(1, &s, &e);
CHECK(e - s == 2 && s[0] == '1');
// Lazy and greedy.
CHECK(re.Parse(_T("<{.*?}>")) == REPARSE_ERROR_OK && re.Match(_T("<a><b>"), &mc));
mc.GetMatch(0, &s, &e);
CHECK(e - s == 1);
CHECK(re.Parse(_T("<{.*}>")) == REPARSE_ERROR_OK && re.Match(_T("<a><b>"), &mc));
mc.GetMatch(0, &s, &e);
CHECK(e - s == 4);
// Negation, classes, quoted strings.
CHECK(re.Parse(_T("a!b")) == REPARSE_ERROR_OK);
CHECK(!re.Match(_T("ab"), &mc) && re.Match(_T("ac"), &mc));
CHECK(re.Parse(_T("^[^0-9]+$")) == REPARSE_ERROR_OK && re.Match(_T("abc"), &mc) &&
!re.Match(_T("a1c"), &mc));
CHECK(re.Parse(_T("{\\q}")) == REPARSE_ERROR_OK && re.Match(_T("say \"hi there\" now"), &mc));
mc.GetMatch(0, &s, &e);
CHECK(e - s == 10);
CHECK(re.Parse(_T("(a*)*b")) == REPARSE_ERROR_OK && re.Match(_T("aaab"), &mc));
// Errors.
CHECK(re.Parse(_T("(ab")) == REPARSE_ERROR_PAREN_EXPECTED);
CHECK(re.Parse(_T("{ab")) == REPARSE_ERROR_BRACE_EXPECTED);
CHECK(re.Parse(_T("[ab")) == REPARSE_ERROR_BRACKET_EXPECTED);
CHECK(re.Parse(_T("*a")) == REPARSE_ERROR_EMPTY_REPEATOP);
CHECK(re.Parse(_T("[z-a]")) == REPARSE_ERROR_INVALID_RANGE);
}
static void TestUrl() {
CUrl url;
CHECK(url.CrackUrl(_T("http://user:pw@www.example.com:8080/dir/page.htm?q=1#top")));
CHECK(url.GetScheme() == ATL_URL_SCHEME_HTTP);
CHECK(lstrcmp(url.GetSchemeName(), _T("http")) == 0);
CHECK(lstrcmp(url.GetHostName(), _T("www.example.com")) == 0);
CHECK(url.GetPortNumber() == 8080);
CHECK(lstrcmp(url.GetUserName(), _T("user")) == 0 && lstrcmp(url.GetPassword(), _T("pw")) == 0);
CHECK(lstrcmp(url.GetUrlPath(), _T("/dir/page.htm")) == 0);
CHECK(lstrcmp(url.GetExtraInfo(), _T("?q=1#top")) == 0);
TCHAR sz[512];
DWORD dw = 512;
CHECK(url.CreateUrl(sz, &dw));
CHECK(lstrcmp(sz, _T("http://user:pw@www.example.com:8080/dir/page.htm?q=1#top")) == 0);
CHECK(dw == (DWORD)lstrlen(sz) && url.GetUrlLength() == dw);
CUrl https;
CHECK(https.CrackUrl(_T("https://example.com")));
CHECK(https.GetPortNumber() == ATL_URL_DEFAULT_HTTPS_PORT);
https.SetUrlPath(_T("/a b"));
dw = 512;
CHECK(https.CreateUrl(sz, &dw, ATL_URL_ESCAPE) && lstrcmp(sz, _T("https://example.com/a%20b")) == 0);
CHECK(!url.CrackUrl(_T("no scheme here")));
dw = 512;
CHECK(AtlEscapeUrl("a b<c>", (LPSTR)sz, &dw, 512) && strcmp((LPSTR)sz, "a%20b%3Cc%3E") == 0);
char szBack[64];
dw = 64;
CHECK(AtlUnescapeUrl("a%20b%3cc", szBack, &dw, 64) && strcmp(szBack, "a b<c") == 0 && dw == 5);
dw = 512;
CHECK(AtlCanonicalizeUrl(_T("HTTP://Example.COM/a/./b/../c"), sz, &dw));
CHECK(lstrcmp(sz, _T("http://example.com/a/c")) == 0);
dw = 512;
CHECK(AtlCombineUrl(_T("http://example.com/a/b/c.htm"), _T("../d.htm"), sz, &dw));
CHECK(lstrcmp(sz, _T("http://example.com/a/d.htm")) == 0);
dw = 512;
CHECK(AtlCombineUrl(_T("http://example.com/a/b/c.htm"), _T("/e"), sz, &dw) &&
lstrcmp(sz, _T("http://example.com/e")) == 0);
CHECK(AtlIsFullPath(_T("C:\\x")) && AtlIsFullPath(_T("\\\\srv\\share")) && !AtlIsFullPath(_T("x\\y")));
CHECK(AtlGetErrorDescription(HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)).GetLength() > 0);
}
// A worker thread client counting its event and timer.
class CCountingClient : public IWorkerThreadClient {
public:
LONG m_nEvent, m_nTimer, m_nClosed;
HANDLE m_hEvent, m_hDone;
CCountingClient() : m_nEvent(0), m_nTimer(0), m_nClosed(0) {
m_hEvent = ::CreateEvent(NULL, FALSE, FALSE, NULL);
m_hDone = ::CreateEvent(NULL, TRUE, FALSE, NULL);
}
~CCountingClient() { ::CloseHandle(m_hDone); }
HRESULT Execute(DWORD_PTR dwParam, HANDLE) {
if (dwParam == 1) {
::InterlockedIncrement(&m_nEvent);
} else if (::InterlockedIncrement(&m_nTimer) == 3) {
::SetEvent(m_hDone);
}
return S_OK;
}
HRESULT CloseHandle(HANDLE h) {
::InterlockedIncrement(&m_nClosed);
::CloseHandle(h);
return S_OK;
}
};
// A thread pool worker adding up its requests.
static LONG g_nPoolSum, g_nPoolWorkers, g_nPoolTerminated;
static HANDLE g_hPoolDone;
class CSumWorker {
public:
typedef DWORD_PTR RequestType;
BOOL Initialize(void* pvParam) {
CHECK(pvParam == (void*)&g_nPoolSum);
::InterlockedIncrement(&g_nPoolWorkers);
return TRUE;
}
void Execute(RequestType request, void*, OVERLAPPED*) {
if (::InterlockedExchangeAdd(&g_nPoolSum, (LONG)request) + (LONG)request == 5050)
::SetEvent(g_hPoolDone);
}
void Terminate(void*) { ::InterlockedIncrement(&g_nPoolTerminated); }
};
static void TestThreads() {
CWorkerThread<> worker;
CHECK(SUCCEEDED(worker.Initialize()));
CCountingClient client;
CHECK(SUCCEEDED(worker.AddHandle(client.m_hEvent, &client, 1)));
HANDLE hTimer = NULL;
CHECK(SUCCEEDED(worker.AddTimer(20, &client, 2, &hTimer)) && hTimer != NULL);
::SetEvent(client.m_hEvent);
CHECK(::WaitForSingleObject(client.m_hDone, 5000) == WAIT_OBJECT_0);
CHECK(client.m_nEvent == 1 && client.m_nTimer >= 3);
CHECK(SUCCEEDED(worker.RemoveHandle(hTimer)) && client.m_nClosed == 1);
CHECK(worker.GetThreadId() != 0 && worker.GetThreadHandle() != NULL);
CHECK(SUCCEEDED(worker.Shutdown()));
CHECK(client.m_nClosed == 2); // the event too
g_hPoolDone = ::CreateEvent(NULL, TRUE, FALSE, NULL);
{
CThreadPool<CSumWorker> pool;
CHECK(SUCCEEDED(pool.Initialize(&g_nPoolSum, 3)));
CHECK(pool.GetNumThreads() == 3 && g_nPoolWorkers == 3);
for (DWORD_PTR i = 1; i <= 100; i++) CHECK(pool.QueueRequest(i));
CHECK(::WaitForSingleObject(g_hPoolDone, 5000) == WAIT_OBJECT_0);
CHECK(g_nPoolSum == 5050);
int nSize = 0;
CHECK(SUCCEEDED(pool.SetSize(1)) && SUCCEEDED(pool.GetSize(&nSize)) && nSize == 1);
CHECK(g_nPoolTerminated == 2);
pool.Shutdown();
CHECK(g_nPoolTerminated == 3);
}
::CloseHandle(g_hPoolDone);
Log("threads: timer ticks %ld, pool sum %ld", client.m_nTimer, g_nPoolSum);
}
int main() {
// Started by the service control manager: be the service.
if (_tcsstr(::GetCommandLine(), TEXT("-AsService")) != NULL) return _AtlModule.WinMain(SW_HIDE);
char szPath[MAX_PATH];
DWORD n = ::GetEnvironmentVariableA("MFC_TEST_OUT", szPath, MAX_PATH);
if (n == 0 || n >= MAX_PATH - 20) lstrcpyA(szPath, ".");
lstrcatA(szPath, "/atlutiltest.log");
g_pLog = fopen(szPath, "w");
::CoInitialize(NULL);
TestStrings();
Log("strings: %d failure(s) so far", g_nFailures);
TestSimpleCollections();
TestAtlArray();
TestAtlList();
TestAtlMap();
TestRBMap();
Log("collections: %d failure(s) so far", g_nFailures);
TestMemory();
TestHeapsAndCurrency();
TestSafeArray();
TestDispatchDriver();
Log("memory, safe arrays, dispatch: %d failure(s) so far", g_nFailures);
TestFiles();
Log("files: %d failure(s) so far", g_nFailures);
TestTime();
Log("times: %d failure(s) so far", g_nFailures);
TestImage();
#ifdef _WINPORT
TestAnimatedGif();
#endif
Log("images: %d failure(s) so far", g_nFailures);
TestComTime();
TestSync();
Log("COleDateTime, sync: %d failure(s) so far", g_nFailures);
TestEncodings();
TestCrypt();
TestRsa();
Log("encodings, crypt: %d failure(s) so far", g_nFailures);
TestSocketAddr();
TestRegExp();
TestUrl();
Log("sockets, regexp, urls: %d failure(s) so far", g_nFailures);
TestThreads();
Log("worker threads, pool: %d failure(s) so far", g_nFailures);
TestPath();
Log("paths: %d failure(s) so far", g_nFailures);
TestSecurity();
Log("security: %d failure(s) so far", g_nFailures);
TestService();
Log("service: %d failure(s) so far", g_nFailures);
::CoUninitialize();
Log(g_nFailures ? "FAIL: %d failure(s)" : "PASS: %d failure(s)", g_nFailures);
if (g_pLog != NULL) fclose(g_pLog);
return g_nFailures ? 1 : 0;
}