/*---------------------------------------------------------------------- | CreateNewFile +---------------------------------------------------------------------*/ NPT_Result CreateNewFile(const char* filename, NPT_Size chunk_count, NPT_Size chunk_size=1) { NPT_File file(filename); NPT_CHECK(file.Open(NPT_FILE_OPEN_MODE_CREATE|NPT_FILE_OPEN_MODE_WRITE)); NPT_OutputStreamReference out; file.GetOutputStream(out); unsigned char* chunk_buffer = new unsigned char[chunk_size]; for (unsigned int i=0; i<chunk_size; i++) { chunk_buffer[i] = (unsigned char)i; } for (unsigned int i=0; i<chunk_count; i++) { NPT_ASSERT(NPT_SUCCEEDED(out->WriteFully(chunk_buffer, chunk_size))); } delete[] chunk_buffer; file.Close(); out = NULL; NPT_FileInfo info; NPT_Result result = NPT_File::GetInfo(filename, &info); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(info.m_Size == (NPT_LargeSize)chunk_count*(NPT_LargeSize)chunk_size); return NPT_SUCCESS; }
/*---------------------------------------------------------------------- | NPT_BufferedInputStream::ReleaseBuffer +---------------------------------------------------------------------*/ NPT_Result NPT_BufferedInputStream::ReleaseBuffer() { NPT_ASSERT(m_Buffer.size == 0); NPT_ASSERT(m_Buffer.offset == m_Buffer.valid); delete[] m_Buffer.data; m_Buffer.data = NULL; m_Buffer.offset = 0; m_Buffer.valid = 0; return NPT_SUCCESS; }
/*---------------------------------------------------------------------- | NPT_LogManager::GetLogger +---------------------------------------------------------------------*/ NPT_Logger* NPT_LogManager::GetLogger(const char* name) { // exit now if the log manager is disabled if (!LogManager.m_Enabled) return NULL; /* check that the manager is initialized */ if (!LogManager.m_Configured) { /* init the manager */ LogManager.Configure(); NPT_ASSERT(LogManager.m_Configured); } // auto lock until we return from this method NPT_LogManagerAutoLocker lock(LogManager); /* check if this logger is already configured */ NPT_Logger* logger = LogManager.FindLogger(name); if (logger) return logger; /* create a new logger */ logger = new NPT_Logger(name, LogManager); if (logger == NULL) return NULL; /* configure the logger */ LogManager.ConfigureLogger(logger); /* find which parent to attach to */ NPT_Logger* parent = LogManager.m_Root; NPT_String parent_name = name; for (;;) { NPT_Logger* candidate_parent; /* find the last dot */ int dot = parent_name.ReverseFind('.'); if (dot < 0) break; parent_name.SetLength(dot); /* see if the parent exists */ candidate_parent = LogManager.FindLogger(parent_name); if (candidate_parent) { parent = candidate_parent; break; } /* this parent name does not exist, see if we need to create it */ if (LogManager.HaveLoggerConfig(parent_name)) { parent = GetLogger(parent_name); break; } } /* attach to the parent */ logger->SetParent(parent); /* add this logger to the list */ LogManager.m_Loggers.Add(logger); return logger; }
/*---------------------------------------------------------------------- | PLT_ThreadTask::Kill +---------------------------------------------------------------------*/ NPT_Result PLT_ThreadTask::Kill() { Stop(); // A task can only be destroyed manually // when the m_AutoDestroy is false // otherwise the Task Manager takes // care of deleting it when the thread exits NPT_ASSERT(m_AutoDestroy == false); if (!m_AutoDestroy) delete this; return NPT_SUCCESS; }
/*---------------------------------------------------------------------- | NPT_BufferedInputStream::FillBuffer +---------------------------------------------------------------------*/ NPT_Result NPT_BufferedInputStream::FillBuffer() { // shortcut if (m_Eos) return NPT_ERROR_EOS; // check that there is nothing left in the buffer and the buffer // size is not 0 NPT_ASSERT(m_Buffer.valid == m_Buffer.offset); NPT_ASSERT(m_Buffer.size != 0); // allocate the read buffer if it has not been done yet if (m_Buffer.data == NULL) { m_Buffer.data = new NPT_Byte[m_Buffer.size]; if (m_Buffer.data == NULL) return NPT_ERROR_OUT_OF_MEMORY; } // refill the buffer m_Buffer.offset = 0; NPT_Result result = m_Source->Read(m_Buffer.data, m_Buffer.size, &m_Buffer.valid); if (NPT_FAILED(result)) m_Buffer.valid = 0; return result; }
/*---------------------------------------------------------------------- | NPT_String::PrepareToWrite +---------------------------------------------------------------------*/ inline char* NPT_String::PrepareToWrite(NPT_Size length) { NPT_ASSERT(length != 0); if (m_Chars == NULL || GetBuffer()->GetAllocated() < length) { // the buffer is too small, we need to allocate a new one. NPT_Size needed = length; if (m_Chars != NULL) { NPT_Size grow = GetBuffer()->GetAllocated()*2; if (grow > length) needed = grow; delete GetBuffer(); } m_Chars = Buffer::Create(needed); } GetBuffer()->SetLength(length); return m_Chars; }
/*---------------------------------------------------------------------- | NPT_InputStream::ReadFully +---------------------------------------------------------------------*/ NPT_Result NPT_InputStream::ReadFully(void* buffer, NPT_Size bytes_to_read) { // shortcut if (bytes_to_read == 0) return NPT_SUCCESS; // read until failure NPT_Size bytes_read; while (bytes_to_read) { NPT_Result result = Read(buffer, bytes_to_read, &bytes_read); if (NPT_FAILED(result)) return result; if (bytes_read == 0) return NPT_ERROR_INTERNAL; NPT_ASSERT(bytes_read <= bytes_to_read); bytes_to_read -= bytes_read; buffer = (void*)(((NPT_Byte*)buffer)+bytes_read); } return NPT_SUCCESS; }
/*---------------------------------------------------------------------- | NPT_OutputStream::WriteFully +---------------------------------------------------------------------*/ NPT_Result NPT_OutputStream::WriteFully(const void* buffer, NPT_Size bytes_to_write) { // shortcut if (bytes_to_write == 0) return NPT_SUCCESS; // write until failure NPT_Size bytes_written; while (bytes_to_write) { NPT_Result result = Write(buffer, bytes_to_write, &bytes_written); if (NPT_FAILED(result)) return result; if (bytes_written == 0) return NPT_ERROR_INTERNAL; NPT_ASSERT(bytes_written <= bytes_to_write); bytes_to_write -= bytes_written; buffer = (const void*)(((const NPT_Byte*)buffer)+bytes_written); } return NPT_SUCCESS; }
/*---------------------------------------------------------------------- | NPT_ThreadCallbackSlot::SendCallback +---------------------------------------------------------------------*/ NPT_Result NPT_ThreadCallbackSlot::SendCallback(void* args) { // protect against concurrent access //NPT_Debug("NPT_ThreadCallbackSlot::SendCallback - write locking\n"); NPT_AutoLock lock(m_WriteLock); // there should be nothing pending #if defined(NPT_DEBUG) NPT_ASSERT(m_Pending.GetValue() == 0); #endif // check if we have been shutdown if (m_Shutdown) return NPT_ERROR_CALLBACK_HANDLER_SHUTDOWN; // put the callback args m_CallbackArgs = args; //NPT_Debug("NPT_ThreadCallbackSlot::SendCallback - signalling\n"); m_Pending.SetValue(1); // call the helper before we wait if (m_NotificationHelper) { m_NotificationHelper->Notify(); } // wait until the callback has been process, or we've been shutdown //NPT_Debug("NPT_ThreadCallbackSlot::SendCallback - waiting...\n"); m_Ack.WaitUntilEquals(1); //NPT_Debug("NPT_ThreadCallbackSlot::SendCallback - got it\n"); // done m_Ack.SetValue(0); m_CallbackArgs = NULL; return m_Shutdown?NPT_ERROR_CALLBACK_HANDLER_SHUTDOWN:NPT_SUCCESS; }
/*---------------------------------------------------------------------- | main +---------------------------------------------------------------------*/ int main(int /*argc*/, char** /*argv*/) { NPT_Result result; NPT_String t = "hello"; NPT_String base64; NPT_DataBuffer data; result = NPT_Base64::Encode((const NPT_Byte*)t.GetChars(), t.GetLength(), base64); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(base64 == "aGVsbG8="); result = NPT_Base64::Decode(base64.GetChars(), base64.GetLength(), data); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(data.GetDataSize() == t.GetLength()); NPT_String tt((const char*)data.GetData(), data.GetDataSize()); NPT_ASSERT(tt == t); t = "hello!"; result = NPT_Base64::Encode((const NPT_Byte*)t.GetChars(), t.GetLength(), base64); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(base64 == "aGVsbG8h"); result = NPT_Base64::Decode(base64.GetChars(), base64.GetLength(), data); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(data.GetDataSize() == t.GetLength()); tt.Assign((const char*)data.GetData(), data.GetDataSize()); NPT_ASSERT(tt == t); t = "hello!!"; result = NPT_Base64::Encode((const NPT_Byte*)t.GetChars(), t.GetLength(), base64); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(base64 == "aGVsbG8hIQ=="); result = NPT_Base64::Decode(base64.GetChars(), base64.GetLength(), data); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(data.GetDataSize() == t.GetLength()); tt.Assign((const char*)data.GetData(), data.GetDataSize()); NPT_ASSERT(tt == t); unsigned char r256_bin[] = { 0x7d, 0x5f, 0xd0, 0xf4, 0x6a, 0xa8, 0xae, 0x34, 0x6e, 0x32, 0x1d, 0xa1, 0xef, 0x66, 0xdd, 0x82, 0x76, 0xa6, 0xfd, 0x8c, 0x75, 0x97, 0xa0, 0x01, 0x00, 0xde, 0x52, 0xef, 0xdf, 0xb6, 0x3e, 0xe4, 0x7b, 0x45, 0xdd, 0x2b, 0xa1, 0x9c, 0xb0, 0x6d, 0x2c, 0x75, 0xb1, 0x87, 0x43, 0x0f, 0xea, 0x24, 0x36, 0x11, 0x7e, 0xee, 0xd1, 0x91, 0x7f, 0x7b, 0x02, 0xea, 0x9a, 0x2a, 0x25, 0xc0, 0xac, 0x99, 0xa4, 0x89, 0x55, 0x5b, 0x82, 0xdf, 0xb0, 0x7e, 0xa1, 0x78, 0x0f, 0xdf, 0x25, 0x5f, 0x3d, 0xba, 0xcb, 0xbc, 0x35, 0x04, 0xc3, 0xf4, 0xb8, 0xc0, 0x17, 0x8e, 0x75, 0x01, 0xe6, 0x2f, 0x88, 0x2c, 0x76, 0x0a, 0x8c, 0x3f, 0x83, 0xd4, 0x10, 0xa8, 0x00, 0xfc, 0xa0, 0x92, 0x7b, 0xae, 0xa3, 0x8c, 0x47, 0xea, 0x25, 0xf9, 0x29, 0x81, 0x1c, 0x21, 0xf2, 0xf4, 0xfe, 0x07, 0x7e, 0x4b, 0x01, 0x79, 0x41, 0x3a, 0xb6, 0x71, 0x0b, 0x75, 0xa7, 0x9d, 0x1b, 0x12, 0xc4, 0x46, 0x06, 0xf3, 0x5f, 0x00, 0x05, 0x2a, 0x1b, 0x34, 0xd6, 0x87, 0xc4, 0x70, 0xcc, 0xc3, 0x9e, 0xa8, 0x24, 0x2c, 0x97, 0x4e, 0xfc, 0x91, 0x70, 0x1c, 0x29, 0x66, 0xc3, 0x23, 0xbf, 0xd7, 0x4d, 0x35, 0x51, 0xff, 0xeb, 0xde, 0x45, 0xbd, 0x8d, 0x80, 0x44, 0x2a, 0x8d, 0xc0, 0xe8, 0x6a, 0xe2, 0x86, 0x46, 0x9f, 0xf2, 0x3c, 0x93, 0x0d, 0x27, 0x02, 0xe4, 0x79, 0xa1, 0x21, 0xf4, 0x43, 0xcd, 0x4c, 0x22, 0x25, 0x9e, 0x93, 0xeb, 0x77, 0x8e, 0x1e, 0x57, 0x1e, 0x9b, 0xcb, 0x91, 0x86, 0xcf, 0x15, 0xaf, 0xd5, 0x03, 0x0f, 0x70, 0xbe, 0x6e, 0x37, 0xea, 0x37, 0xdd, 0xf6, 0xa1, 0xb1, 0xf7, 0x05, 0xbc, 0x2d, 0x44, 0x60, 0x35, 0xa4, 0x05, 0x0b, 0x22, 0x7d, 0x7a, 0x71, 0xe5, 0x1d, 0x8e, 0xcb, 0xc3, 0xb8, 0x3a, 0xe1 }; NPT_String b64; NPT_Base64::Encode(r256_bin, sizeof(r256_bin), b64); NPT_DataBuffer r256_out; NPT_Base64::Decode(b64.GetChars(), b64.GetLength(), r256_out); NPT_ASSERT(r256_out.GetDataSize() == sizeof(r256_bin)); NPT_ASSERT(r256_bin[sizeof(r256_bin)-1] == r256_out.GetData()[sizeof(r256_bin)-1]); unsigned char random_bytes[] = { 0xc7, 0xee, 0x49, 0x9e, 0x2c, 0x8b, 0x1c, 0x16, 0x9e, 0x7f, 0x30, 0xd0, 0xc6, 0x12, 0x30, 0x80, 0x81, 0xcd, 0x20, 0x20, 0x26, 0xaf, 0x4f, 0xd6, 0xfc, 0x86, 0x2e, 0x85, 0xf3, 0x10, 0x38, 0x2b, 0x0e, 0xbb, 0x80, 0x68, 0xbe, 0xff, 0x1c, 0xdc, 0x72, 0xb5, 0x0d, 0x8f, 0x8e, 0x6c, 0x09, 0x63, 0xba, 0x21, 0x23, 0xb2, 0x24, 0x17, 0xd3, 0x17, 0x69, 0x44, 0x77, 0x11, 0x36, 0x6a, 0x6e, 0xf2, 0x44, 0x87, 0xa1, 0xd3, 0xf3, 0x1f, 0x6c, 0x38, 0x22, 0x4a, 0x44, 0x70, 0x66, 0xef, 0x8c, 0x3a, 0x51, 0xc8, 0xee, 0x85, 0x00, 0x25, 0x93, 0x10, 0x2e, 0x0b, 0x1b, 0x03, 0x94, 0x47, 0x05, 0x22, 0xd0, 0xc4, 0xec, 0x2e, 0xcc, 0xbc, 0xbb, 0x67, 0xfd, 0xec, 0x0e, 0xb1, 0x3f, 0xbc, 0x82, 0xe0, 0xa7, 0x9c, 0xf3, 0xae, 0xbd, 0xb7, 0xab, 0x02, 0xf1, 0xd9, 0x17, 0x4c, 0x9d, 0xeb, 0xe2, 0x00, 0x1e, 0x19, 0x6e, 0xb3, 0xfd, 0x7d, 0xea, 0x49, 0x85, 0x43, 0x2f, 0x56, 0x81, 0x89, 0xba, 0x71, 0x37, 0x10, 0xb5, 0x74, 0xab, 0x90, 0x4d, 0xc4, 0xd1, 0x0d, 0x8d, 0x6f, 0x01, 0xf5, 0x2c, 0xc9, 0x1a, 0x79, 0xa1, 0x41, 0x71, 0x2b, 0xfb, 0xf3, 0xd5, 0xe4, 0x2a, 0xf5, 0xad, 0x80, 0x7a, 0x03, 0xff, 0x5f, 0x45, 0x8c, 0xec, 0x6a, 0x4b, 0x05, 0xe3, 0x65, 0x19, 0x70, 0x05, 0xad, 0xc4, 0xb8, 0x4e, 0x9e, 0x9a, 0x36, 0x4a, 0x86, 0x9d, 0xf5, 0x99, 0xcb, 0x00, 0xb8, 0xb9, 0xa7, 0x86, 0x18, 0xfc, 0x9a, 0xe7, 0x00, 0x6a, 0x67, 0xfa, 0x42, 0x9d, 0xff, 0x4d, 0x7a, 0xe4, 0xe8, 0x03, 0x88, 0xff, 0x60, 0xe1, 0x8d, 0x09, 0x5f, 0x6f, 0xde, 0x6b }; NPT_Array<unsigned char> random(random_bytes, NPT_ARRAY_SIZE(random_bytes)); t = "x+5JniyLHBaefzDQxhIwgIHNICAmr0/W/IYuhfMQOCsOu4Bovv8c3HK1DY+ObAlj\r\n" "uiEjsiQX0xdpRHcRNmpu8kSHodPzH2w4IkpEcGbvjDpRyO6FACWTEC4LGwOURwUi\r\n" "0MTsLsy8u2f97A6xP7yC4Kec8669t6sC8dkXTJ3r4gAeGW6z/X3qSYVDL1aBibpx\r\n" "NxC1dKuQTcTRDY1vAfUsyRp5oUFxK/vz1eQq9a2AegP/X0WM7GpLBeNlGXAFrcS4\r\n" "Tp6aNkqGnfWZywC4uaeGGPya5wBqZ/pCnf9NeuToA4j/YOGNCV9v3ms="; result = NPT_Base64::Decode(t.GetChars(), t.GetLength(), data); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(data.GetDataSize() == 233); NPT_Array<unsigned char> verif(data.GetData(), data.GetDataSize()); NPT_ASSERT(verif == random); result = NPT_Base64::Encode(&random[0], random.GetItemCount(), base64, NPT_BASE64_PEM_BLOCKS_PER_LINE); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(base64 == t); NPT_String t_url = t; t.Replace('/', '_'); t.Replace('+', '-'); result = NPT_Base64::Encode(&random[0], random.GetItemCount(), base64, NPT_BASE64_PEM_BLOCKS_PER_LINE, true); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(base64 == t); t = "76768484767685839"; result = NPT_Base64::Decode(t.GetChars(), t.GetLength(), data); NPT_ASSERT(result == NPT_ERROR_INVALID_FORMAT); t = "76869=978686"; result = NPT_Base64::Decode(t.GetChars(), t.GetLength(), data); NPT_ASSERT(result == NPT_ERROR_INVALID_FORMAT); t = "7686=8978686"; result = NPT_Base64::Decode(t.GetChars(), t.GetLength(), data); NPT_ASSERT(result == NPT_ERROR_INVALID_FORMAT); t = "7686==978686"; result = NPT_Base64::Decode(t.GetChars(), t.GetLength(), data); NPT_ASSERT(result == NPT_ERROR_INVALID_FORMAT); // test IP address parsing NPT_IpAddress ip; NPT_ASSERT(NPT_FAILED(ip.Parse(""))); NPT_ASSERT(NPT_FAILED(ip.Parse("a.b.c.d"))); NPT_ASSERT(NPT_FAILED(ip.Parse("1.2.3.4.5"))); NPT_ASSERT(NPT_FAILED(ip.Parse("1"))); NPT_ASSERT(NPT_FAILED(ip.Parse("1.2.3.4."))); NPT_ASSERT(NPT_FAILED(ip.Parse("1.2.3.4f"))); NPT_ASSERT(NPT_FAILED(ip.Parse("1.g.3.4"))); NPT_ASSERT(NPT_FAILED(ip.Parse("1.2..3.4"))); NPT_ASSERT(NPT_FAILED(ip.Parse("1.2.300.4"))); NPT_ASSERT(NPT_SUCCEEDED(ip.Parse("1.2.3.4"))); NPT_ASSERT(ip.AsBytes()[0] == 1); NPT_ASSERT(ip.AsBytes()[1] == 2); NPT_ASSERT(ip.AsBytes()[2] == 3); NPT_ASSERT(ip.AsBytes()[3] == 4); NPT_ASSERT(NPT_SUCCEEDED(ip.Parse("255.255.0.1"))); NPT_ASSERT(ip.AsBytes()[0] == 255); NPT_ASSERT(ip.AsBytes()[1] == 255); NPT_ASSERT(ip.AsBytes()[2] == 0); NPT_ASSERT(ip.AsBytes()[3] == 1); NPT_ASSERT(NPT_SUCCEEDED(ip.Parse("0.0.0.0"))); NPT_ASSERT(ip.AsBytes()[0] == 0); NPT_ASSERT(ip.AsBytes()[1] == 0); NPT_ASSERT(ip.AsBytes()[2] == 0); NPT_ASSERT(ip.AsBytes()[3] == 0); return 0; }
/*---------------------------------------------------------------------- | main +---------------------------------------------------------------------*/ int main(int /*argc*/, char** /*argv*/) { NPT_Result result; NPT_String t = "hello"; NPT_String base64; NPT_DataBuffer data; result = NPT_Base64::Encode((const NPT_Byte*)t.GetChars(), t.GetLength(), base64); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(base64 == "aGVsbG8="); result = NPT_Base64::Decode(base64.GetChars(), base64.GetLength(), data); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(data.GetDataSize() == t.GetLength()); NPT_String tt((const char*)data.GetData(), data.GetDataSize()); NPT_ASSERT(tt == t); t = "hello!"; result = NPT_Base64::Encode((const NPT_Byte*)t.GetChars(), t.GetLength(), base64); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(base64 == "aGVsbG8h"); result = NPT_Base64::Decode(base64.GetChars(), base64.GetLength(), data); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(data.GetDataSize() == t.GetLength()); tt.Assign((const char*)data.GetData(), data.GetDataSize()); NPT_ASSERT(tt == t); t = "hello!!"; result = NPT_Base64::Encode((const NPT_Byte*)t.GetChars(), t.GetLength(), base64); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(base64 == "aGVsbG8hIQ=="); result = NPT_Base64::Decode(base64.GetChars(), base64.GetLength(), data); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(data.GetDataSize() == t.GetLength()); tt.Assign((const char*)data.GetData(), data.GetDataSize()); NPT_ASSERT(tt == t); unsigned char r256_bin[] = { 0x7d, 0x5f, 0xd0, 0xf4, 0x6a, 0xa8, 0xae, 0x34, 0x6e, 0x32, 0x1d, 0xa1, 0xef, 0x66, 0xdd, 0x82, 0x76, 0xa6, 0xfd, 0x8c, 0x75, 0x97, 0xa0, 0x01, 0x00, 0xde, 0x52, 0xef, 0xdf, 0xb6, 0x3e, 0xe4, 0x7b, 0x45, 0xdd, 0x2b, 0xa1, 0x9c, 0xb0, 0x6d, 0x2c, 0x75, 0xb1, 0x87, 0x43, 0x0f, 0xea, 0x24, 0x36, 0x11, 0x7e, 0xee, 0xd1, 0x91, 0x7f, 0x7b, 0x02, 0xea, 0x9a, 0x2a, 0x25, 0xc0, 0xac, 0x99, 0xa4, 0x89, 0x55, 0x5b, 0x82, 0xdf, 0xb0, 0x7e, 0xa1, 0x78, 0x0f, 0xdf, 0x25, 0x5f, 0x3d, 0xba, 0xcb, 0xbc, 0x35, 0x04, 0xc3, 0xf4, 0xb8, 0xc0, 0x17, 0x8e, 0x75, 0x01, 0xe6, 0x2f, 0x88, 0x2c, 0x76, 0x0a, 0x8c, 0x3f, 0x83, 0xd4, 0x10, 0xa8, 0x00, 0xfc, 0xa0, 0x92, 0x7b, 0xae, 0xa3, 0x8c, 0x47, 0xea, 0x25, 0xf9, 0x29, 0x81, 0x1c, 0x21, 0xf2, 0xf4, 0xfe, 0x07, 0x7e, 0x4b, 0x01, 0x79, 0x41, 0x3a, 0xb6, 0x71, 0x0b, 0x75, 0xa7, 0x9d, 0x1b, 0x12, 0xc4, 0x46, 0x06, 0xf3, 0x5f, 0x00, 0x05, 0x2a, 0x1b, 0x34, 0xd6, 0x87, 0xc4, 0x70, 0xcc, 0xc3, 0x9e, 0xa8, 0x24, 0x2c, 0x97, 0x4e, 0xfc, 0x91, 0x70, 0x1c, 0x29, 0x66, 0xc3, 0x23, 0xbf, 0xd7, 0x4d, 0x35, 0x51, 0xff, 0xeb, 0xde, 0x45, 0xbd, 0x8d, 0x80, 0x44, 0x2a, 0x8d, 0xc0, 0xe8, 0x6a, 0xe2, 0x86, 0x46, 0x9f, 0xf2, 0x3c, 0x93, 0x0d, 0x27, 0x02, 0xe4, 0x79, 0xa1, 0x21, 0xf4, 0x43, 0xcd, 0x4c, 0x22, 0x25, 0x9e, 0x93, 0xeb, 0x77, 0x8e, 0x1e, 0x57, 0x1e, 0x9b, 0xcb, 0x91, 0x86, 0xcf, 0x15, 0xaf, 0xd5, 0x03, 0x0f, 0x70, 0xbe, 0x6e, 0x37, 0xea, 0x37, 0xdd, 0xf6, 0xa1, 0xb1, 0xf7, 0x05, 0xbc, 0x2d, 0x44, 0x60, 0x35, 0xa4, 0x05, 0x0b, 0x22, 0x7d, 0x7a, 0x71, 0xe5, 0x1d, 0x8e, 0xcb, 0xc3, 0xb8, 0x3a, 0xe1 }; NPT_String b64; NPT_Base64::Encode(r256_bin, sizeof(r256_bin), b64); NPT_DataBuffer r256_out; NPT_Base64::Decode(b64.GetChars(), b64.GetLength(), r256_out); NPT_ASSERT(r256_out.GetDataSize() == sizeof(r256_bin)); NPT_ASSERT(r256_bin[sizeof(r256_bin)-1] == r256_out.GetData()[sizeof(r256_bin)-1]); unsigned char random_bytes[] = { 0xc7, 0xee, 0x49, 0x9e, 0x2c, 0x8b, 0x1c, 0x16, 0x9e, 0x7f, 0x30, 0xd0, 0xc6, 0x12, 0x30, 0x80, 0x81, 0xcd, 0x20, 0x20, 0x26, 0xaf, 0x4f, 0xd6, 0xfc, 0x86, 0x2e, 0x85, 0xf3, 0x10, 0x38, 0x2b, 0x0e, 0xbb, 0x80, 0x68, 0xbe, 0xff, 0x1c, 0xdc, 0x72, 0xb5, 0x0d, 0x8f, 0x8e, 0x6c, 0x09, 0x63, 0xba, 0x21, 0x23, 0xb2, 0x24, 0x17, 0xd3, 0x17, 0x69, 0x44, 0x77, 0x11, 0x36, 0x6a, 0x6e, 0xf2, 0x44, 0x87, 0xa1, 0xd3, 0xf3, 0x1f, 0x6c, 0x38, 0x22, 0x4a, 0x44, 0x70, 0x66, 0xef, 0x8c, 0x3a, 0x51, 0xc8, 0xee, 0x85, 0x00, 0x25, 0x93, 0x10, 0x2e, 0x0b, 0x1b, 0x03, 0x94, 0x47, 0x05, 0x22, 0xd0, 0xc4, 0xec, 0x2e, 0xcc, 0xbc, 0xbb, 0x67, 0xfd, 0xec, 0x0e, 0xb1, 0x3f, 0xbc, 0x82, 0xe0, 0xa7, 0x9c, 0xf3, 0xae, 0xbd, 0xb7, 0xab, 0x02, 0xf1, 0xd9, 0x17, 0x4c, 0x9d, 0xeb, 0xe2, 0x00, 0x1e, 0x19, 0x6e, 0xb3, 0xfd, 0x7d, 0xea, 0x49, 0x85, 0x43, 0x2f, 0x56, 0x81, 0x89, 0xba, 0x71, 0x37, 0x10, 0xb5, 0x74, 0xab, 0x90, 0x4d, 0xc4, 0xd1, 0x0d, 0x8d, 0x6f, 0x01, 0xf5, 0x2c, 0xc9, 0x1a, 0x79, 0xa1, 0x41, 0x71, 0x2b, 0xfb, 0xf3, 0xd5, 0xe4, 0x2a, 0xf5, 0xad, 0x80, 0x7a, 0x03, 0xff, 0x5f, 0x45, 0x8c, 0xec, 0x6a, 0x4b, 0x05, 0xe3, 0x65, 0x19, 0x70, 0x05, 0xad, 0xc4, 0xb8, 0x4e, 0x9e, 0x9a, 0x36, 0x4a, 0x86, 0x9d, 0xf5, 0x99, 0xcb, 0x00, 0xb8, 0xb9, 0xa7, 0x86, 0x18, 0xfc, 0x9a, 0xe7, 0x00, 0x6a, 0x67, 0xfa, 0x42, 0x9d, 0xff, 0x4d, 0x7a, 0xe4, 0xe8, 0x03, 0x88, 0xff, 0x60, 0xe1, 0x8d, 0x09, 0x5f, 0x6f, 0xde, 0x6b }; NPT_Array<unsigned char> random(random_bytes, NPT_ARRAY_SIZE(random_bytes)); t = "x+5JniyLHBaefzDQxhIwgIHNICAmr0/W/IYuhfMQOCsOu4Bovv8c3HK1DY+ObAlj\r\n" "uiEjsiQX0xdpRHcRNmpu8kSHodPzH2w4IkpEcGbvjDpRyO6FACWTEC4LGwOURwUi\r\n" "0MTsLsy8u2f97A6xP7yC4Kec8669t6sC8dkXTJ3r4gAeGW6z/X3qSYVDL1aBibpx\r\n" "NxC1dKuQTcTRDY1vAfUsyRp5oUFxK/vz1eQq9a2AegP/X0WM7GpLBeNlGXAFrcS4\r\n" "Tp6aNkqGnfWZywC4uaeGGPya5wBqZ/pCnf9NeuToA4j/YOGNCV9v3ms="; result = NPT_Base64::Decode(t.GetChars(), t.GetLength(), data); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(data.GetDataSize() == 233); NPT_Array<unsigned char> verif(data.GetData(), data.GetDataSize()); NPT_ASSERT(verif == random); result = NPT_Base64::Encode(&random[0], random.GetItemCount(), base64, NPT_BASE64_PEM_BLOCKS_PER_LINE); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(base64 == t); NPT_String t_url = t; t.Replace('/', '_'); t.Replace('+', '-'); result = NPT_Base64::Encode(&random[0], random.GetItemCount(), base64, NPT_BASE64_PEM_BLOCKS_PER_LINE, true); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(base64 == t); t = "76768484767685839"; result = NPT_Base64::Decode(t.GetChars(), t.GetLength(), data); NPT_ASSERT(result == NPT_ERROR_INVALID_FORMAT); t = "76869=978686"; result = NPT_Base64::Decode(t.GetChars(), t.GetLength(), data); NPT_ASSERT(result == NPT_ERROR_INVALID_FORMAT); t = "7686=8978686"; result = NPT_Base64::Decode(t.GetChars(), t.GetLength(), data); NPT_ASSERT(result == NPT_ERROR_INVALID_FORMAT); t = "7686==978686"; result = NPT_Base64::Decode(t.GetChars(), t.GetLength(), data); NPT_ASSERT(result == NPT_ERROR_INVALID_FORMAT); // test IP address parsing NPT_IpAddress ip; NPT_ASSERT(NPT_FAILED(ip.Parse(""))); NPT_ASSERT(NPT_FAILED(ip.Parse("a.b.c.d"))); NPT_ASSERT(NPT_FAILED(ip.Parse("1.2.3.4.5"))); NPT_ASSERT(NPT_FAILED(ip.Parse("1"))); NPT_ASSERT(NPT_FAILED(ip.Parse("1.2.3.4."))); NPT_ASSERT(NPT_FAILED(ip.Parse("1.2.3.4f"))); NPT_ASSERT(NPT_FAILED(ip.Parse("1.g.3.4"))); NPT_ASSERT(NPT_FAILED(ip.Parse("1.2..3.4"))); NPT_ASSERT(NPT_FAILED(ip.Parse("1.2.300.4"))); NPT_ASSERT(NPT_SUCCEEDED(ip.Parse("1.2.3.4"))); NPT_ASSERT(ip.AsBytes()[0] == 1); NPT_ASSERT(ip.AsBytes()[1] == 2); NPT_ASSERT(ip.AsBytes()[2] == 3); NPT_ASSERT(ip.AsBytes()[3] == 4); NPT_ASSERT(NPT_SUCCEEDED(ip.Parse("255.255.0.1"))); NPT_ASSERT(ip.AsBytes()[0] == 255); NPT_ASSERT(ip.AsBytes()[1] == 255); NPT_ASSERT(ip.AsBytes()[2] == 0); NPT_ASSERT(ip.AsBytes()[3] == 1); NPT_ASSERT(NPT_SUCCEEDED(ip.Parse("0.0.0.0"))); NPT_ASSERT(ip.AsBytes()[0] == 0); NPT_ASSERT(ip.AsBytes()[1] == 0); NPT_ASSERT(ip.AsBytes()[2] == 0); NPT_ASSERT(ip.AsBytes()[3] == 0); // MIME parameter parser NPT_Map<NPT_String,NPT_String> params; result = NPT_ParseMimeParameters(NULL, params); NPT_ASSERT(result == NPT_ERROR_INVALID_PARAMETERS); result = NPT_ParseMimeParameters("", params); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(params.GetEntryCount() == 0); result = NPT_ParseMimeParameters("foo=bar", params); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(params.GetEntryCount() == 1); NPT_ASSERT(params["foo"] == "bar"); params.Clear(); result = NPT_ParseMimeParameters(" foo =bar", params); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(params.GetEntryCount() == 1); NPT_ASSERT(params["foo"] == "bar"); params.Clear(); result = NPT_ParseMimeParameters(" foo= bar", params); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(params.GetEntryCount() == 1); NPT_ASSERT(params["foo"] == "bar"); params.Clear(); result = NPT_ParseMimeParameters(" foo= bar;", params); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(params.GetEntryCount() == 1); NPT_ASSERT(params["foo"] == "bar"); params.Clear(); result = NPT_ParseMimeParameters("foo=\"bar\"", params); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(params.GetEntryCount() == 1); NPT_ASSERT(params["foo"] == "bar"); params.Clear(); result = NPT_ParseMimeParameters("foo=\"ba\"r\"", params); NPT_ASSERT(result == NPT_ERROR_INVALID_SYNTAX); params.Clear(); result = NPT_ParseMimeParameters("foo=\"ba\\\"r\"", params); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(params.GetEntryCount() == 1); NPT_ASSERT(params["foo"] == "ba\"r"); params.Clear(); result = NPT_ParseMimeParameters("foo=\"bar\\\"\"", params); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(params.GetEntryCount() == 1); NPT_ASSERT(params["foo"] == "bar\""); params.Clear(); result = NPT_ParseMimeParameters("foo=\"bar\\\\\"", params); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(params.GetEntryCount() == 1); NPT_ASSERT(params["foo"] == "bar\\"); params.Clear(); result = NPT_ParseMimeParameters("a=1;b=2; c=3; d=4 ; e=\"\\;\"; f=\";\"", params); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(params.GetEntryCount() == 6); NPT_ASSERT(params["a"] == "1"); NPT_ASSERT(params["b"] == "2"); NPT_ASSERT(params["c"] == "3"); NPT_ASSERT(params["d"] == "4"); NPT_ASSERT(params["e"] == ";"); NPT_ASSERT(params["f"] == ";"); params.Clear(); // number parsing float f; int i; NPT_Int32 i32; NPT_UInt32 ui32; NPT_Int64 i64; NPT_UInt64 ui64; SHOULD_FAIL(NPT_ParseInteger("ssdfsdf", i, false)); SHOULD_FAIL(NPT_ParseInteger("", i, false)); SHOULD_FAIL(NPT_ParseInteger(NULL, i, false)); SHOULD_FAIL(NPT_ParseInteger("123a", i, false)); SHOULD_FAIL(NPT_ParseInteger("a123", i, false)); SHOULD_FAIL(NPT_ParseInteger(" 123", i, false)); SHOULD_FAIL(NPT_ParseInteger("a 123", i, true)); SHOULD_FAIL(NPT_ParseInteger(" a123", i, true)); SHOULD_SUCCEED(NPT_ParseInteger("+1", i, false)); SHOULD_EQUAL_I(i, 1); SHOULD_SUCCEED(NPT_ParseInteger("+123", i, false)); SHOULD_EQUAL_I(i, 123); SHOULD_SUCCEED(NPT_ParseInteger("-1", i, false)); SHOULD_EQUAL_I(i, -1); SHOULD_SUCCEED(NPT_ParseInteger("-123", i, false)); SHOULD_EQUAL_I(i, -123); SHOULD_SUCCEED(NPT_ParseInteger("-123fgs", i, true)); SHOULD_EQUAL_I(i, -123); SHOULD_SUCCEED(NPT_ParseInteger(" -123fgs", i, true)); SHOULD_EQUAL_I(i, -123); SHOULD_SUCCEED(NPT_ParseInteger("0", i, true)); SHOULD_EQUAL_I(i, 0); SHOULD_SUCCEED(NPT_ParseInteger("7768", i, true)); SHOULD_EQUAL_I(i, 7768); SHOULD_SUCCEED(NPT_ParseInteger32("2147483647", i32, false)); SHOULD_EQUAL_I(i32, 2147483647); SHOULD_SUCCEED(NPT_ParseInteger32("-2147483647", i32, false)); SHOULD_EQUAL_I(i32, -2147483647); SHOULD_SUCCEED(NPT_ParseInteger32("-2147483648", i32, false)); SHOULD_EQUAL_I(i32, (-2147483647 - 1)); SHOULD_FAIL(NPT_ParseInteger32("2147483648", i32, false)); SHOULD_FAIL(NPT_ParseInteger32("-2147483649", i32, false)); SHOULD_FAIL(NPT_ParseInteger32("-21474836480", i32, false)); SHOULD_FAIL(NPT_ParseInteger32("21474836470", i32, false)); SHOULD_SUCCEED(NPT_ParseInteger32U("4294967295", ui32, false)); SHOULD_EQUAL_I(ui32, 4294967295U); SHOULD_FAIL(NPT_ParseInteger32U("4294967296", ui32, false)); SHOULD_FAIL(NPT_ParseInteger32U("-1", ui32, false)); SHOULD_SUCCEED(NPT_ParseInteger64("9223372036854775807", i64, false)); SHOULD_EQUAL_I(i64, NPT_INT64_C(9223372036854775807)); SHOULD_SUCCEED(NPT_ParseInteger64("-9223372036854775807", i64, false)); SHOULD_EQUAL_I(i64, NPT_INT64_C(-9223372036854775807)); SHOULD_SUCCEED(NPT_ParseInteger64("-9223372036854775808", i64, false)); SHOULD_EQUAL_I(i64, (NPT_INT64_C(-9223372036854775807) - NPT_INT64_C(1))); SHOULD_FAIL(NPT_ParseInteger64("9223372036854775808", i64, false)); SHOULD_FAIL(NPT_ParseInteger64("-9223372036854775809", i64, false)); SHOULD_FAIL(NPT_ParseInteger64("-9223372036854775897", i64, false)); SHOULD_FAIL(NPT_ParseInteger64("9223372036854775897", i64, false)); SHOULD_SUCCEED(NPT_ParseInteger64U("18446744073709551615", ui64, false)); SHOULD_EQUAL_I(ui64, NPT_UINT64_C(18446744073709551615)); SHOULD_FAIL(NPT_ParseInteger64U("18446744073709551616", ui64, false)); SHOULD_FAIL(NPT_ParseInteger64U("-1", ui64, false)); SHOULD_FAIL(NPT_ParseFloat("ssdfsdf", f, false)); SHOULD_FAIL(NPT_ParseFloat("", f, false)); SHOULD_FAIL(NPT_ParseFloat(NULL, f, false)); SHOULD_FAIL(NPT_ParseFloat("123.", f, false)); SHOULD_FAIL(NPT_ParseFloat("a123", f, false)); SHOULD_FAIL(NPT_ParseFloat(" 123", f, false)); SHOULD_FAIL(NPT_ParseFloat(" 127.89E5ff", f, false)); SHOULD_SUCCEED(NPT_ParseFloat("+1.0", f, false)); SHOULD_EQUAL_F(f, 1.0f); SHOULD_SUCCEED(NPT_ParseFloat("+123", f, false)); SHOULD_EQUAL_F(f, 123.0f); SHOULD_SUCCEED(NPT_ParseFloat("-0.1", f, false)); SHOULD_EQUAL_F(f, -0.1f); SHOULD_SUCCEED(NPT_ParseFloat("0.23e-13", f, false)); SHOULD_EQUAL_F(f, 0.23e-13f); SHOULD_SUCCEED(NPT_ParseFloat(" 127.89E5ff", f, true)); SHOULD_EQUAL_F(f, 127.89E5f); SHOULD_SUCCEED(NPT_ParseFloat("+0.3db", f, true)); SHOULD_EQUAL_F(f, 0.3f); SHOULD_SUCCEED(NPT_ParseFloat("+.3db", f, true)); SHOULD_EQUAL_F(f, 0.3f); SHOULD_SUCCEED(NPT_ParseFloat("-.3db", f, true)); SHOULD_EQUAL_F(f, -0.3f); SHOULD_SUCCEED(NPT_ParseFloat(".3db", f, true)); SHOULD_EQUAL_F(f, .3f); return 0; }
/*---------------------------------------------------------------------- | main +---------------------------------------------------------------------*/ int main(int argc, char** argv) { NPT_Result result; NPT_FileInfo info; NPT_ASSERT(NPT_File::GetInfo("foobar.doesnotexist", NULL) == NPT_ERROR_NO_SUCH_FILE); NPT_ASSERT(!NPT_File::Exists("foobar.doesnotexist")); // test special names NPT_File file(NPT_FILE_STANDARD_INPUT); NPT_ASSERT(NPT_SUCCEEDED(file.GetInfo(info))); NPT_ASSERT(info.m_Type == NPT_FileInfo::FILE_TYPE_SPECIAL); file = NPT_File(NPT_FILE_STANDARD_OUTPUT); NPT_ASSERT(NPT_SUCCEEDED(file.GetInfo(info))); NPT_ASSERT(info.m_Type == NPT_FileInfo::FILE_TYPE_SPECIAL); file = NPT_File(NPT_FILE_STANDARD_ERROR); NPT_ASSERT(NPT_SUCCEEDED(file.GetInfo(info))); NPT_ASSERT(info.m_Type == NPT_FileInfo::FILE_TYPE_SPECIAL); if (NPT_File::Exists("foobar.file1")) { result = NPT_File::DeleteFile("foobar.file1"); NPT_ASSERT(NPT_SUCCEEDED(result)); } result = CreateNewFile("foobar.file1", 9); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(NPT_File::Exists("foobar.file1")); result = NPT_File::GetInfo("foobar.file1", &info); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(info.m_Type == NPT_FileInfo::FILE_TYPE_REGULAR); NPT_ASSERT(info.m_Size == 9); { NPT_File f1("foobar.file1"); result = f1.GetInfo(info); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(info.m_Type == NPT_FileInfo::FILE_TYPE_REGULAR); NPT_ASSERT(info.m_Size == 9); } { NPT_File f1("foobar.file1"); NPT_LargeSize size; result = f1.GetSize(size); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(size == 9); } { NPT_File f1("foobar.file1"); result = f1.Rename("foobar.file1-r"); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(f1.GetPath() == "foobar.file1-r"); } NPT_ASSERT(NPT_File::Exists("foobar.file1-r")); result = NPT_File::GetInfo("foobar.file1-r", &info); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(info.m_Type == NPT_FileInfo::FILE_TYPE_REGULAR); NPT_ASSERT(info.m_Size == 9); // dirs NPT_ASSERT(!NPT_File::Exists("foobar.dir")); result = NPT_File::CreateDirectory("foobar.dir"); NPT_ASSERT(NPT_SUCCEEDED(result)); result = NPT_File::GetInfo("foobar.dir", &info); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(info.m_Type == NPT_FileInfo::FILE_TYPE_DIRECTORY); { NPT_File f1("foobar.dir"); result = f1.GetInfo(info); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(info.m_Type == NPT_FileInfo::FILE_TYPE_DIRECTORY); } NPT_String dirname = "foobar.dir"; NPT_String fname; fname = dirname; fname += NPT_FilePath::Separator; fname += "file1"; result = CreateNewFile(fname, 1); NPT_ASSERT(NPT_SUCCEEDED(result)); fname = dirname; fname += NPT_FilePath::Separator; fname += "file2"; result = CreateNewFile(fname, 2); NPT_ASSERT(NPT_SUCCEEDED(result)); fname = dirname; fname += NPT_FilePath::Separator; fname += "file3"; result = CreateNewFile(fname, 3); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_List<NPT_String> entries; result = NPT_File::ListDirectory("foobar.dir", entries); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(entries.GetItemCount() == 3); result = NPT_File::DeleteFile("foobar.dir"); NPT_ASSERT(NPT_FAILED(result)); result = NPT_File::DeleteDirectory("foobar.dir"); NPT_ASSERT(result == NPT_ERROR_DIRECTORY_NOT_EMPTY); result = NPT_File::Rename("foobar.dir", "foobar.dir-r"); NPT_ASSERT(NPT_SUCCEEDED(result)); dirname = "foobar.dir-r"; fname = dirname; fname += NPT_FilePath::Separator; fname += "file1"; result = NPT_File::DeleteFile(fname); NPT_ASSERT(NPT_SUCCEEDED(result)); fname = dirname; fname += NPT_FilePath::Separator; fname += "file2"; result = NPT_File::DeleteFile(fname); NPT_ASSERT(NPT_SUCCEEDED(result)); fname = dirname; fname += NPT_FilePath::Separator; fname += "file3"; result = NPT_File::DeleteFile(fname); NPT_ASSERT(NPT_SUCCEEDED(result)); result = NPT_File::DeleteDirectory("foobar.dir-r"); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(!NPT_File::Exists("foobar.dir-r")); // paths NPT_String test; test = NPT_FilePath::BaseName(""); NPT_ASSERT(test == ""); test = NPT_FilePath::BaseName("a"); NPT_ASSERT(test == "a"); test = NPT_FilePath::BaseName("a"+NPT_FilePath::Separator+"b"); NPT_ASSERT(test == "b"); test = NPT_FilePath::BaseName("a"+NPT_FilePath::Separator+"b"+NPT_FilePath::Separator); NPT_ASSERT(test == ""); test = NPT_FilePath::BaseName(NPT_FilePath::Separator+"a"); NPT_ASSERT(test == "a"); test = NPT_FilePath::BaseName(NPT_FilePath::Separator); NPT_ASSERT(test == ""); test = NPT_FilePath::DirectoryName(""); NPT_ASSERT(test == ""); test = NPT_FilePath::DirectoryName("a"); NPT_ASSERT(test == ""); test = NPT_FilePath::DirectoryName("a"+NPT_FilePath::Separator+"b"); NPT_ASSERT(test == "a"); test = NPT_FilePath::DirectoryName("a"+NPT_FilePath::Separator+"b"+NPT_FilePath::Separator); NPT_ASSERT(test == "a"+NPT_FilePath::Separator+"b"); test = NPT_FilePath::DirectoryName(NPT_FilePath::Separator+"a"); NPT_ASSERT(test == NPT_FilePath::Separator); test = NPT_FilePath::DirectoryName(NPT_FilePath::Separator); NPT_ASSERT(test == NPT_FilePath::Separator); // large files if (argc == 2) { result = CreateNewFile(argv[1], 0x10000, 0x10007); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_String new_name = argv[1]; new_name += ".renamed"; result = NPT_File::Rename(argv[1], new_name); NPT_ASSERT(NPT_SUCCEEDED(result)); file = NPT_File(new_name); result = file.Open(NPT_FILE_OPEN_MODE_READ); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_InputStreamReference input; file.GetInputStream(input); NPT_Position position; result = input->Tell(position); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(position == 0); NPT_LargeSize large_size = (NPT_LargeSize)0x10007 * (NPT_LargeSize)0x10000; result = input->Seek(large_size-0x10007); NPT_ASSERT(NPT_SUCCEEDED(result)); result = input->Tell(position); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(position == large_size-0x10007); unsigned char* buffer = new unsigned char[0x10007]; result = input->ReadFully(buffer, 0x10007); NPT_ASSERT(NPT_SUCCEEDED(result)); result = input->Tell(position); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(position == large_size); for (unsigned int i=0; i<0x10007; i++) { NPT_ASSERT(buffer[i] == (unsigned char)i); } file.Close(); NPT_File::DeleteFile(new_name); } return 0; }
/*---------------------------------------------------------------------- | main +---------------------------------------------------------------------*/ int main(int argc, char** argv) { NPT_Result result; NPT_FileInfo info; NPT_ASSERT(NPT_File::GetInfo("foobar.doesnotexist", NULL) == NPT_ERROR_NO_SUCH_FILE); NPT_ASSERT(!NPT_File::Exists("foobar.doesnotexist")); // test special names NPT_File file(NPT_FILE_STANDARD_INPUT); NPT_ASSERT(NPT_SUCCEEDED(file.GetInfo(info))); NPT_ASSERT(info.m_Type == NPT_FileInfo::FILE_TYPE_SPECIAL); file = NPT_File(NPT_FILE_STANDARD_OUTPUT); NPT_ASSERT(NPT_SUCCEEDED(file.GetInfo(info))); NPT_ASSERT(info.m_Type == NPT_FileInfo::FILE_TYPE_SPECIAL); file = NPT_File(NPT_FILE_STANDARD_ERROR); NPT_ASSERT(NPT_SUCCEEDED(file.GetInfo(info))); NPT_ASSERT(info.m_Type == NPT_FileInfo::FILE_TYPE_SPECIAL); if (NPT_File::Exists("foobar.file1")) { result = NPT_File::RemoveFile("foobar.file1"); NPT_ASSERT(NPT_SUCCEEDED(result)); } result = CreateNewFile("foobar.file1", 9); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(NPT_File::Exists("foobar.file1")); result = NPT_File::GetInfo("foobar.file1", &info); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(info.m_Type == NPT_FileInfo::FILE_TYPE_REGULAR); NPT_ASSERT(info.m_Size == 9); { NPT_File f1("foobar.file1"); result = f1.GetInfo(info); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(info.m_Type == NPT_FileInfo::FILE_TYPE_REGULAR); NPT_ASSERT(info.m_Size == 9); } { NPT_File f1("foobar.file1"); NPT_LargeSize size; result = f1.GetSize(size); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(size == 9); } { NPT_File f1("foobar.file1"); result = f1.Rename("foobar.file1-r"); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(f1.GetPath() == "foobar.file1-r"); } NPT_ASSERT(NPT_File::Exists("foobar.file1-r")); result = NPT_File::GetInfo("foobar.file1-r", &info); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(info.m_Type == NPT_FileInfo::FILE_TYPE_REGULAR); NPT_ASSERT(info.m_Size == 9); // dirs NPT_ASSERT(!NPT_File::Exists("foobar.dir")); result = NPT_File::CreateDir("foobar.dir"); NPT_ASSERT(NPT_SUCCEEDED(result)); result = NPT_File::GetInfo("foobar.dir", &info); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(info.m_Type == NPT_FileInfo::FILE_TYPE_DIRECTORY); { NPT_File f1("foobar.dir"); result = f1.GetInfo(info); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(info.m_Type == NPT_FileInfo::FILE_TYPE_DIRECTORY); } NPT_String dirname = "foobar.dir"; NPT_String fname; fname = dirname; fname += NPT_FilePath::Separator; fname += "file1"; result = CreateNewFile(fname, 1); NPT_ASSERT(NPT_SUCCEEDED(result)); fname = dirname; fname += NPT_FilePath::Separator; fname += "file2"; result = CreateNewFile(fname, 2); NPT_ASSERT(NPT_SUCCEEDED(result)); fname = dirname; fname += NPT_FilePath::Separator; fname += "file3"; result = CreateNewFile(fname, 3); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_List<NPT_String> entries; result = NPT_File::ListDir("foobar.dir", entries); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(entries.GetItemCount() == 3); result = NPT_File::RemoveFile("foobar.dir"); NPT_ASSERT(NPT_FAILED(result)); result = NPT_File::RemoveDir("foobar.dir"); NPT_ASSERT(result == NPT_ERROR_DIRECTORY_NOT_EMPTY); result = NPT_File::Rename("foobar.dir", "foobar.dir-r"); NPT_ASSERT(NPT_SUCCEEDED(result)); dirname = "foobar.dir-r"; fname = dirname; fname += NPT_FilePath::Separator; fname += "file1"; result = NPT_File::RemoveFile(fname); NPT_ASSERT(NPT_SUCCEEDED(result)); fname = dirname; fname += NPT_FilePath::Separator; fname += "file2"; result = NPT_File::RemoveFile(fname); NPT_ASSERT(NPT_SUCCEEDED(result)); fname = dirname; fname += NPT_FilePath::Separator; fname += "file3"; result = NPT_File::RemoveFile(fname); NPT_ASSERT(NPT_SUCCEEDED(result)); result = NPT_File::RemoveDir("foobar.dir-r"); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(!NPT_File::Exists("foobar.dir-r")); // paths NPT_String test; test = NPT_FilePath::BaseName(""); NPT_ASSERT(test == ""); test = NPT_FilePath::BaseName("a"); NPT_ASSERT(test == "a"); test = NPT_FilePath::BaseName("a"+NPT_FilePath::Separator+"b"); NPT_ASSERT(test == "b"); test = NPT_FilePath::BaseName("a"+NPT_FilePath::Separator+"b"+NPT_FilePath::Separator); NPT_ASSERT(test == ""); test = NPT_FilePath::BaseName(NPT_FilePath::Separator+"a"); NPT_ASSERT(test == "a"); test = NPT_FilePath::BaseName(NPT_FilePath::Separator); NPT_ASSERT(test == ""); test = NPT_FilePath::DirName(""); NPT_ASSERT(test == ""); test = NPT_FilePath::DirName("a"); NPT_ASSERT(test == ""); test = NPT_FilePath::DirName("a"+NPT_FilePath::Separator+"b"); NPT_ASSERT(test == "a"); test = NPT_FilePath::DirName("a"+NPT_FilePath::Separator+"b"+NPT_FilePath::Separator); NPT_ASSERT(test == "a"+NPT_FilePath::Separator+"b"); test = NPT_FilePath::DirName(NPT_FilePath::Separator+"a"); NPT_ASSERT(test == NPT_FilePath::Separator); test = NPT_FilePath::DirName(NPT_FilePath::Separator); NPT_ASSERT(test == NPT_FilePath::Separator); // small files result = CreateNewFile("small.bin", 0x100, 0x107); NPT_ASSERT(NPT_SUCCEEDED(result)); file = NPT_File("small.bin"); result = file.Open(NPT_FILE_OPEN_MODE_READ); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_InputStreamReference input; file.GetInputStream(input); NPT_Position position; result = input->Tell(position); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(position == 0); NPT_LargeSize large_size = (NPT_LargeSize)0x107 * (NPT_LargeSize)0x100; result = input->Seek(large_size-0x107); NPT_ASSERT(NPT_SUCCEEDED(result)); result = input->Tell(position); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(position == large_size-0x107); unsigned char* buffer = new unsigned char[0x107]; result = input->ReadFully(buffer, 0x107); NPT_ASSERT(NPT_SUCCEEDED(result)); result = input->Tell(position); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(position == large_size); for (unsigned int i=0; i<0x107; i++) { NPT_ASSERT(buffer[i] == (unsigned char)i); } file.Close(); NPT_File::RemoveFile(file.GetPath()); // large files if (argc == 2) { result = CreateNewFile(argv[1], 0x10000, 0x10007); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_String new_name = argv[1]; new_name += ".renamed"; result = NPT_File::Rename(argv[1], new_name); NPT_ASSERT(NPT_SUCCEEDED(result)); file = NPT_File(new_name); result = file.Open(NPT_FILE_OPEN_MODE_READ); NPT_ASSERT(NPT_SUCCEEDED(result)); file.GetInputStream(input); result = input->Tell(position); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(position == 0); large_size = (NPT_LargeSize)0x10007 * (NPT_LargeSize)0x10000; result = input->Seek(large_size-0x10007); NPT_ASSERT(NPT_SUCCEEDED(result)); result = input->Tell(position); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(position == large_size-0x10007); buffer = new unsigned char[0x10007]; result = input->ReadFully(buffer, 0x10007); NPT_ASSERT(NPT_SUCCEEDED(result)); result = input->Tell(position); NPT_ASSERT(NPT_SUCCEEDED(result)); NPT_ASSERT(position == large_size); for (unsigned int i=0; i<0x10007; i++) { NPT_ASSERT(buffer[i] == (unsigned char)i); } file.Close(); NPT_File::RemoveFile(new_name); } // test dynamic size //NPT_LargeSize size; unsigned char buff[16] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}; const char* filename = "pi.\xCF\x80.test"; NPT_TimeInterval wait(2.0f); if (argc > 1) { filename = argv[1]; } NPT_File file1(filename); NPT_OutputStreamReference output; NPT_ASSERT(NPT_SUCCEEDED(file1.Open(NPT_FILE_OPEN_MODE_CREATE | NPT_FILE_OPEN_MODE_WRITE | NPT_FILE_OPEN_MODE_READ | NPT_FILE_OPEN_MODE_TRUNCATE))); NPT_ASSERT(NPT_SUCCEEDED(file1.GetSize(size))); NPT_ASSERT(size == 0); NPT_ASSERT(NPT_SUCCEEDED(file1.GetOutputStream(output))); NPT_ASSERT(NPT_SUCCEEDED(file1.GetInputStream(input))); NPT_ASSERT(NPT_SUCCEEDED(output->Tell(position))); NPT_ASSERT(position == 0); NPT_ASSERT(NPT_SUCCEEDED(input->Tell(position))); NPT_ASSERT(position == 0); NPT_ASSERT(NPT_SUCCEEDED(output->WriteFully(buff, 16))); output->Flush(); NPT_System::Sleep(wait); NPT_ASSERT(NPT_SUCCEEDED(file1.GetSize(size))); NPT_ASSERT(size == 16); NPT_ASSERT(NPT_SUCCEEDED(output->Tell(position))); NPT_ASSERT(NPT_SUCCEEDED(input->GetSize(size))); NPT_ASSERT(size == 16); NPT_ASSERT(position == 16); NPT_ASSERT(NPT_SUCCEEDED(input->Tell(position))); NPT_ASSERT(position == 16); NPT_ASSERT(NPT_SUCCEEDED(output->Seek(8))); NPT_ASSERT(NPT_SUCCEEDED(output->Tell(position))); NPT_ASSERT(position == 8); NPT_File file2(filename); NPT_InputStreamReference input2; NPT_ASSERT(NPT_SUCCEEDED(file2.Open(NPT_FILE_OPEN_MODE_READ))); NPT_ASSERT(NPT_SUCCEEDED(file2.GetSize(size))); NPT_ASSERT(size == 16); NPT_ASSERT(NPT_SUCCEEDED(file2.GetInputStream(input2))); NPT_ASSERT(NPT_SUCCEEDED(input2->GetSize(size))); NPT_ASSERT(size == 16); NPT_ASSERT(NPT_SUCCEEDED(input2->Tell(position))); NPT_ASSERT(position == 0); NPT_ASSERT(NPT_SUCCEEDED(input2->Seek(8))); NPT_ASSERT(NPT_SUCCEEDED(input2->Tell(position))); NPT_ASSERT(position == 8); NPT_ASSERT(NPT_SUCCEEDED(output->WriteFully(buff, 16))); output->Flush(); NPT_System::Sleep(wait); NPT_ASSERT(NPT_SUCCEEDED(file2.GetSize(size))); NPT_ASSERT(size == 24); NPT_ASSERT(NPT_SUCCEEDED(output->Tell(position))); NPT_ASSERT(position == 24); NPT_ASSERT(NPT_SUCCEEDED(input->Tell(position))); NPT_ASSERT(position == 24); NPT_ASSERT(NPT_SUCCEEDED(input2->GetSize(size))); NPT_ASSERT(size == 24); NPT_ASSERT(NPT_SUCCEEDED(input2->Seek(20))); NPT_ASSERT(NPT_SUCCEEDED(input2->Read(buff, 4, NULL))); return 0; }