Skip to content

Commit

Permalink
add random number funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoegel committed Aug 25, 2024
1 parent 46110eb commit 77678e0
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 14 deletions.
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ Requires `gtest` to build unit tests. Requires [Botan](https://botan.randombit.n

```sh
mkdir build
cd build
cmake ..
cmake --build .
cmake build/
cmake --build build/ -j4
```

### Testing
```sh
cd build
ctest
ctest --test-dir build/
```

### Documenting
Expand All @@ -24,11 +22,11 @@ python3.9 -m sphinx -b html docs/source docs/html

## Installing
```sh
cd build
sudo `which cmake` --install .
sudo `which cmake` --install build/
```

### Uninstalling
```sh
cd build
sudo xargs rm <install_manifest.txt
```
3 changes: 3 additions & 0 deletions basis/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ add_library(fwoopbasis STATIC
fwoop_dnsquery.cpp
fwoop_filereader.cpp
fwoop_log.cpp
fwoop_random.cpp
fwoop_socketio.cpp
fwoop_threadpool.cpp
fwoop_tokenizer.cpp
Expand All @@ -19,6 +20,7 @@ install(FILES
fwoop_dnsquery.h
fwoop_filereader.h
fwoop_log.h
fwoop_random.h
fwoop_socketio.h
fwoop_threadpool.h
fwoop_tokenizer.h
Expand All @@ -32,6 +34,7 @@ add_executable(fwoopbasis_test
fwoop_dnsquery.g.cpp
fwoop_filereader.g.cpp
fwoop_log.g.cpp
fwoop_random.g.cpp
fwoop_threadpool.g.cpp
fwoop_tokenizer.g.cpp
)
Expand Down
9 changes: 5 additions & 4 deletions basis/fwoop_dnsquery.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#include "fwoop_array.h"
#include "fwoop_filereader.h"
#include <fwoop_dnsquery.h>

#include <algorithm>
#include <cmath>
Expand All @@ -10,7 +7,11 @@
#include <sstream>
#include <vector>

#include <fwoop_array.h>
#include <fwoop_dnsquery.h>
#include <fwoop_filereader.h>
#include <fwoop_log.h>
#include <fwoop_random.h>
#include <fwoop_socketio.h>
#include <fwoop_tokenizer.h>

Expand Down Expand Up @@ -317,7 +318,7 @@ std::shared_ptr<ResourceRecord> Query::getRecord(const Question &question)
Array request(requestLen);

// TODO make random
const uint16_t transactionID = 0x7598;
const uint16_t transactionID = Random::getUInt16();
const uint16_t flags = 0x0100;
const uint16_t numQuestions = 0x1;
const uint16_t answers = 0;
Expand Down
8 changes: 8 additions & 0 deletions basis/fwoop_filereader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ uint8_t *FileReader::loadFile(uint32_t &length)
return contents;
}

uint8_t *FileReader::head(uint32_t length)
{
uint8_t *contents = new uint8_t[length];
memset(contents, 0, length);
d_file.read((char *)contents, length);
return contents;
}

FileReader::Iterator FileReader::begin()
{
Iterator itr(*this, false);
Expand Down
2 changes: 2 additions & 0 deletions basis/fwoop_filereader.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <cstdint>
#include <fstream>
#include <string>
#include <system_error>
Expand All @@ -23,6 +24,7 @@ class FileReader {
void close();

uint8_t *loadFile(uint32_t &length);
uint8_t *head(uint32_t length);

Iterator begin();
Iterator end();
Expand Down
46 changes: 46 additions & 0 deletions basis/fwoop_random.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <cstdint>
#include <fwoop_filereader.h>
#include <fwoop_random.h>
#include <sys/types.h>

namespace {
const std::string RANDOM_FN = "/dev/random";
}

namespace fwoop {

uint8_t *Random::readRandomBytes(uint32_t numBytes)
{
FileReader fr(RANDOM_FN);
if (fr.open() != 0) {
// TODO handle error
return nullptr;
}
uint8_t *res = fr.head(numBytes);
fr.close();
return res;
}

uint16_t Random::getUInt16()
{
uint8_t *bytes = Random::readRandomBytes(2);
if (bytes) {
uint16_t r = (bytes[0] << 8) + bytes[1];
delete[] bytes;
return r;
}
return 0;
}

uint32_t Random::getUInt32()
{
uint8_t *bytes = Random::readRandomBytes(4);
if (bytes) {
uint32_t r = (bytes[3] << 24) + (bytes[2] << 16) + (bytes[1] << 8) + bytes[0];
delete[] bytes;
return r;
}
return 0;
}

} // namespace fwoop
16 changes: 16 additions & 0 deletions basis/fwoop_random.g.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <fwoop_random.h>
#include <gtest/gtest.h>

TEST(Random, getInt)
{
// GIVEN

// WHEN
uint16_t r1 = fwoop::Random::getUInt16();
uint32_t r2 = fwoop::Random::getUInt16();
std::cerr << r1 << " " << r2 << '\n';

// THEN
ASSERT_LT(0, r1);
ASSERT_LT(0, r2);
}
17 changes: 17 additions & 0 deletions basis/fwoop_random.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <cstdint>
#include <inttypes.h>

namespace fwoop {

class Random {
private:
static uint8_t *readRandomBytes(uint32_t numBytes);

public:
static uint16_t getUInt16();
static uint32_t getUInt32();
};

} // namespace fwoop
9 changes: 6 additions & 3 deletions basis/fwoop_threadpool.g.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <atomic>
#include <gtest/gtest.h>
#include <memory>
#include <thread>

using namespace std::chrono_literals;
Expand All @@ -11,23 +12,25 @@ TEST(ThreadPool, EnqueueAndWait)
// GIVEN
std::atomic_uint16_t count = 0;
struct Job {
std::atomic_uint16_t &c;
std::atomic_uint16_t *c;
void operator()()
{
fwoop::Log::Debug("job in progress");
c++;
(*c)++;
}
};
fwoop::ThreadPool<Job> pool(2);
Job j{count};
Job j{&count};

// WHEN
EXPECT_TRUE(pool.enqueue(std::move(j)));
EXPECT_TRUE(pool.enqueue(std::move(j)));
EXPECT_TRUE(pool.enqueue(std::move(j)));

pool.close();
std::cerr << "close done\n";
pool.wait();
std::cerr << "wait done\n";

// THEN
EXPECT_EQ(count.load(), 3);
Expand Down
3 changes: 3 additions & 0 deletions basis/fwoop_threadpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,13 @@ template <typename T> void ThreadPool<T>::close()

template <typename T> void ThreadPool<T>::wait()
{
std::cerr << "wait started\n";
const unsigned int totalJobs = d_pool.size();
while (d_numFinished < totalJobs) {
std::unique_lock lock(d_poolMutex);
std::cerr << "wait got lock\n";
d_poolCond.wait(lock, [&]() { return d_numFinished.load() == totalJobs; });
std::cerr << "num finished=" << d_numFinished.load() << '\n';
}
}

Expand Down

0 comments on commit 77678e0

Please sign in to comment.