Twitter Stream Mysterious 401:Unauthorized Status with Oauth and Clock Issue

I’ve been facing the following problem with Twitter Stream while using Python: The REST API was working fine, but the Stream API was returning me 401:Unauthorized.

Both, REST and Stream were working fine when using curl with the command provided by the Oauth tool, but while I was trying to make it work with Python, I was not being successful at all, only receiving the 401-thing. At that point nothing seemed to be strange, the response header was:

Content-Type: text/html
WWW-Authenticate: Basic realm="Firehose"
Cache-Control: must-revalidate,no-cache,no-store
Content-Length: 1505
Connection: close

After 2 months struggling with that problem, I discovered that Twitter API will return 401 if your clock is not synchronized. To solve this you need to execute ntpdate. On Ubuntu you can execute the following command:

sudo ntpdate ntp.ubuntu.com

and I guess that you will be able to connect with the Twitter Stream again. On the end, I discovered that this happened because I’ve moved to a different country, and I changed only my clock, without changing my Zone Time. I discovered this because the hour on my clock changed to the one from my home country when I used ntpdate. If this is your case, and you are using Ubuntu, change your Zone Time with the following comand:

sudo dpkg-reconfigure tzdata

Well, I learned a bunch of things in a row 🙂

Unknown or ambiguous terminal type; type just ‘set terminal’ for a list

This appeared when I tried to “set terminal pdf” on Gnuplot. Im using Ubuntu 10.04.  Making the long story short, I needed to install the Maverick (10.10) packets for Gnuplot.

The direct links for those packages, depending on the arch, are amd64, gnuplot-nox and gnuplot-x11. For i386, gnuplot-nox and gnuplot-x11. Download, install and the same output should be something like

gnuplot> set terminal pdf
Terminal type set to 'pdfcairo'

and that is it!

Adding a Samsung ML-1865w laser printer to Ubuntu

Samsung ML-1865w laser printer is a good alternative for those who print much more in black and white than in color. I was trying to add it to Ubuntu, but it was not found automatically by Ubuntu. So I need to add its driver manually, following the steps below:

# enter in a temporarty directory
cd /var/tmp

# downloads the unified linux driver
wget http://org.downloadcenter.samsung.com/downloadfile/ContentsFile.aspx?CDSite=UNI_HK_EN&CttFileID=3182440&CDCttType=DR&ModelType=N&ModelName=ML-1865W&VPath=DR/201106/20110602161236937/UnifiedLinuxDriver_0.98.tar.gz

# unconpressing driver 
tar -xzf UnifiedLinuxDriver_0.98.tar.gz

# remove compressed drive
rm UnifiedLinuxDriver_0.98.tar.gz

# enter 
cd cdroot

# run autorun -> It will open a window -> Next -> I accept -> Next -> Add user: vianna -> Next -> Mark "Disable LPT support" -> Wait installing pr
sudo ./autorun

# remove install directory
cd ..; rm -rf cdroot

See ya!

Building Google Test Framework with CMake

For those who does not know GTest (Google Test Framework), it is a simple way to make unit tests in C and C++ source code.

But what is an unit test?

In GTest Documentation you will find an in-depth description, but in general terms, one unit test consists in a specific test of one function of one source file from an application. It has basically an initialization phase to allows you to call the correspondent function. Then it comprises on make some sanity tests (assertions) to check if your code is behaving as expected.

How GTest works?

GTest provides some assertion macros which simplify the tests as the following numerical comparison assertions (there is also some macros for binary and string comparison):

Fatal assertion Nonfatal assertion Verifies
ASSERT_EQ(expected, actual); EXPECT_EQ(expected, actual); expected == actual
ASSERT_NE(val1, val2); EXPECT_NE(val1, val2); val1 != val2
ASSERT_LT(val1, val2); EXPECT_LT(val1, val2); val1 < val2
ASSERT_LE(val1, val2); EXPECT_LE(val1, val2); val1 <= val2
ASSERT_GT(val1, val2); EXPECT_GT(val1, val2); val1 > val2
ASSERT_GE(val1, val2); EXPECT_GE(val1, val2); val1 >= val2

How to build a simple example?

The first step is download the source code and build the GTest library, which can be performed using g++ compiler (replace $(GTEST_DIR) by the place of GTest directory):

wget http://googletest.googlecode.com/files/gtest-1.6.0.zip
unzip gtest-1.6.0.zip
g++ -I ${GTEST_DIR}/include -I ${GTEST_DIR} -c ${GTEST_DIR}/src/gtest-all.cc
ar -rv libgtest.a gtest-all.o

It will generates the libraries libgtest.a (which contains GTest binaries). Consider a simple unit test example of a C source code named test1.c:

#include <gtest/gtest.h>
TEST(MathTest, TwoPlusTwoEqualsFour) {
EXPECT_EQ(2 + 2, 4);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest( &argc, argv );
return RUN_ALL_TESTS();
}

To build it, it is necessary to defines the GTest headers directory (parameter includes dir -I), compile the source code and link it with the GTest library (libgtest.a) and pthread, as shown below (again, replace $(GTEST_DIR) by the GTest directory):

g++ -I ${GTEST_DIR}/include test1.c libgtest.a -lpthread -o test1

The call and the output is as follows:

./test1
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from MathTest
[ RUN      ] MathTest.TwoPlusTwoEqualsFour
[       OK ] MathTest.TwoPlusTwoEqualsFour (0 ms)
[----------] 1 test from MathTest (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
[  PASSED  ] 1 test.

How to build GTest samples?

Jointly with the GTest, it also comes some C++ unit test examples, found in the samples directory. You can build this examples using CMake a simpler and powerful tool to generate Makefiles. It can be installed using apt-get:

sudo apt-get install cmake

The CMake uses a configuration file named CMakeLists.txt. The common way to build a project with CMake is create a build directory, generate a Makefile using CMake and build it with make. One advantage of using CMake is that you separate the deploy from source code and can make the deploy in multiple places with the same CMake file.

cd gtest-1.6.0
mkdir build
cd build
cmake -Dgtest_build_samples=ON ..
make

Now you can execute all examples that comes GTest.

cd gtest-1.6.0/build
./sample1_unittest
./sample2_unittest
...
./sample10_unittest

How to build your own CMake?

A last tip is how to build a CMakeFile.txt for your project. Instead of use the CMake file provided by GTest, let’s make our own CMake to build the GTest’s sample 1 (Factorial test). The CMake file will requires to know where you unzip GTest, which can be performed through an environment variable named GTEST_ROOT (replace “/tmp/gtest-1.6.0” by the GTest directory).

GTEST_ROOT="/tmp/gtest-1.6.0"
export GTEST_ROOT

Then, creates a CMakeLists.txt with the content below:

cmake_minimum_required(VERSION 2.6)
project(sample1)
enable_testing()
find_package (Threads)
message(STATUS GTEST_ROOT=$ENV{GTEST_ROOT})
include_directories($ENV{GTEST_ROOT}/include)
link_directories($ENV{GTEST_ROOT}/mybuild)
add_executable(sample1 sample1.cc sample1_unittest.cc)
target_link_libraries(sample1 gtest gtest_main)
target_link_libraries(sample1 ${CMAKE_THREAD_LIBS_INIT})
add_test(NAME sample1 COMMAND sample1)

To build it you should repeat the same steps above:

mkdir build
cd build
cmake ..
make

The call and output of this example is as follows:

./sample1
[==========] Running 6 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 3 tests from FactorialTest
[ RUN      ] FactorialTest.Negative
[       OK ] FactorialTest.Negative (0 ms)
[ RUN      ] FactorialTest.Zero
[       OK ] FactorialTest.Zero (0 ms)
[ RUN      ] FactorialTest.Positive
[       OK ] FactorialTest.Positive (0 ms)
[----------] 3 tests from FactorialTest (0 ms total)

[----------] 3 tests from IsPrimeTest
[ RUN      ] IsPrimeTest.Negative
[       OK ] IsPrimeTest.Negative (0 ms)
[ RUN      ] IsPrimeTest.Trivial
[       OK ] IsPrimeTest.Trivial (0 ms)
[ RUN      ] IsPrimeTest.Positive
[       OK ] IsPrimeTest.Positive (0 ms)
[----------] 3 tests from IsPrimeTest (0 ms total)

[----------] Global test environment tear-down
[==========] 6 tests from 2 test cases ran. (1 ms total)
[  PASSED  ] 6 tests.

See ya!

Force Google Chrome Close a Hanged Tab

My Chrome 9.0 had two hanged tabs, and those were not being identified by the browser, I mean, informing me that they were problematic. The computer was 2 days on but Chrome was endlessly loading those 2 tabs. In order to force a tab being closed in Chrome read this Google Support’s article. Basically you have to go to the Menu > Tools > Task Manager > Choose tab > Close it.